Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
syail committed Apr 21, 2024
1 parent e36d24f commit c2675e8
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.vs
MMKVParser/bin
MMKVParser/obj
MMKVParser.Tests/bin
MMKVParser.Tests/obj
TestResults
27 changes: 27 additions & 0 deletions MMKVParser.Tests/MMKVParser.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MMKVParser\MMKVParser.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions MMKVParser.Tests/MMKVParserTests.cs
Original file line number Diff line number Diff line change
@@ -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]));
}
}
}
31 changes: 31 additions & 0 deletions MMKVParser.sln
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions MMKVParser/MMKVBinaryReader.cs
Original file line number Diff line number Diff line change
@@ -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;
}

/// <summary>
/// Reads a proto-buf field
///
/// Format: | Field Length (VarInt) | Field Value |
/// </summary>
/// <returns></returns>
public byte[] ReadProtoField()
{
long length = ReadVarint();
return ReadBytes((int)length);
}

public string ReadUTF8StringValue()
{
return Encoding.UTF8.GetString(ReadProtoField());
}
}
}
54 changes: 54 additions & 0 deletions MMKVParser/MMKVParser.cs
Original file line number Diff line number Diff line change
@@ -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<string, List<byte[]>> Load()
{
Dictionary<string, List<byte[]>> 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<byte[]>? 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();
}
}
}
9 changes: 9 additions & 0 deletions MMKVParser/MMKVParser.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit c2675e8

Please sign in to comment.