From c2675e8ff4799be3e8458426befacc6c097a4615 Mon Sep 17 00:00:00 2001 From: syail Date: Mon, 22 Apr 2024 01:12:13 +0900 Subject: [PATCH] Initial commit --- .gitignore | 6 +++ MMKVParser.Tests/MMKVParser.Tests.csproj | 27 ++++++++++++ MMKVParser.Tests/MMKVParserTests.cs | 24 +++++++++++ MMKVParser.sln | 31 ++++++++++++++ MMKVParser/MMKVBinaryReader.cs | 38 +++++++++++++++++ MMKVParser/MMKVParser.cs | 54 ++++++++++++++++++++++++ MMKVParser/MMKVParser.csproj | 9 ++++ 7 files changed, 189 insertions(+) create mode 100644 .gitignore create mode 100644 MMKVParser.Tests/MMKVParser.Tests.csproj create mode 100644 MMKVParser.Tests/MMKVParserTests.cs create mode 100644 MMKVParser.sln create mode 100644 MMKVParser/MMKVBinaryReader.cs create mode 100644 MMKVParser/MMKVParser.cs create mode 100644 MMKVParser/MMKVParser.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f817db --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.vs +MMKVParser/bin +MMKVParser/obj +MMKVParser.Tests/bin +MMKVParser.Tests/obj +TestResults diff --git a/MMKVParser.Tests/MMKVParser.Tests.csproj b/MMKVParser.Tests/MMKVParser.Tests.csproj new file mode 100644 index 0000000..e7fe446 --- /dev/null +++ b/MMKVParser.Tests/MMKVParser.Tests.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + diff --git a/MMKVParser.Tests/MMKVParserTests.cs b/MMKVParser.Tests/MMKVParserTests.cs new file mode 100644 index 0000000..7e99349 --- /dev/null +++ b/MMKVParser.Tests/MMKVParserTests.cs @@ -0,0 +1,24 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MMKVParser; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MMKVParser.Tests +{ + [TestClass()] + public class MMKVParserTests + { + [TestMethod()] + public void LoadTest() + { + MMKVParser parser = new("mmkv.default"); + + var map = parser.Load(); + + Assert.IsNotNull(MMKVParser.ReadUTF8StringValue(map["TOKEN"][1])); + } + } +} diff --git a/MMKVParser.sln b/MMKVParser.sln new file mode 100644 index 0000000..82acb6d --- /dev/null +++ b/MMKVParser.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34622.214 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MMKVParser", "MMKVParser\MMKVParser.csproj", "{D73CDC04-A714-4DC8-8DA5-36BE7688F8A9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MMKVParser.Tests", "MMKVParser.Tests\MMKVParser.Tests.csproj", "{40BE8A3E-5FCF-4DE7-9A38-6323C4FD1084}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D73CDC04-A714-4DC8-8DA5-36BE7688F8A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D73CDC04-A714-4DC8-8DA5-36BE7688F8A9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D73CDC04-A714-4DC8-8DA5-36BE7688F8A9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D73CDC04-A714-4DC8-8DA5-36BE7688F8A9}.Release|Any CPU.Build.0 = Release|Any CPU + {40BE8A3E-5FCF-4DE7-9A38-6323C4FD1084}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {40BE8A3E-5FCF-4DE7-9A38-6323C4FD1084}.Debug|Any CPU.Build.0 = Debug|Any CPU + {40BE8A3E-5FCF-4DE7-9A38-6323C4FD1084}.Release|Any CPU.ActiveCfg = Release|Any CPU + {40BE8A3E-5FCF-4DE7-9A38-6323C4FD1084}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B723F6C1-A94C-4346-B087-20579ADD7D79} + EndGlobalSection +EndGlobal diff --git a/MMKVParser/MMKVBinaryReader.cs b/MMKVParser/MMKVBinaryReader.cs new file mode 100644 index 0000000..0592286 --- /dev/null +++ b/MMKVParser/MMKVBinaryReader.cs @@ -0,0 +1,38 @@ +using System.Text; + +namespace MMKVParser +{ + internal class MMKVBinaryReader(Stream input) : BinaryReader(input) + { + public long ReadVarint() + { + long result = 0; + int shift = 0; + byte b; + do + { + b = ReadByte(); + result |= ((long)b & 0x7F) << shift; + shift += 7; + } while ((b & 0x80) != 0); + return result; + } + + /// + /// Reads a proto-buf field + /// + /// Format: | Field Length (VarInt) | Field Value | + /// + /// + public byte[] ReadProtoField() + { + long length = ReadVarint(); + return ReadBytes((int)length); + } + + public string ReadUTF8StringValue() + { + return Encoding.UTF8.GetString(ReadProtoField()); + } + } +} diff --git a/MMKVParser/MMKVParser.cs b/MMKVParser/MMKVParser.cs new file mode 100644 index 0000000..093b8d7 --- /dev/null +++ b/MMKVParser/MMKVParser.cs @@ -0,0 +1,54 @@ +using System.Text; + +namespace MMKVParser +{ + public class MMKVParser : IDisposable + { + private readonly MMKVBinaryReader _reader; + + public MMKVParser(string databasePath) + { + _reader = new(File.OpenRead(databasePath)); + } + + public MMKVParser(Stream stream) + { + _reader = new(stream); + } + + public Dictionary> Load() + { + Dictionary> result = []; + + int size = _reader.ReadInt32(); + _reader.ReadVarint(); + + while(_reader.BaseStream.Position < size) + { + string key = Encoding.UTF8.GetString(_reader.ReadProtoField()); + byte[] valueBytes = _reader.ReadProtoField(); + + if (!result.TryGetValue(key, out List? list)) + { + list = ([]); + result[key] = list; + } + list.Add(valueBytes); + } + return result; + } + + public static string ReadUTF8StringValue(byte[] bytes) + { + using MemoryStream stream = new(bytes); + using MMKVBinaryReader reader = new(stream); + + return reader.ReadUTF8StringValue(); + } + + public void Dispose() + { + _reader.Dispose(); + } + } +} diff --git a/MMKVParser/MMKVParser.csproj b/MMKVParser/MMKVParser.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/MMKVParser/MMKVParser.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + +