feat: fix mmkv parser, support for ios ekey mmkv

This commit is contained in:
鲁树人 2023-06-15 19:30:33 +01:00
parent cc885164c1
commit 59ecc0847b
6 changed files with 36 additions and 10 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
*.mmkv binary

View File

@ -52,7 +52,7 @@ export function ImportFileModal({ onClose, show }: ImportFileModalProps) {
alert(`不是支持的 SQLite 数据库文件。\n表名${qmc2Keys}`); alert(`不是支持的 SQLite 数据库文件。\n表名${qmc2Keys}`);
return; return;
} }
} else if (/MMKVStreamEncryptId/i.test(file.name)) { } else if (/MMKVStreamEncryptId|filenameEkeyMap/i.test(file.name)) {
const fileBuffer = await file.arrayBuffer(); const fileBuffer = await file.arrayBuffer();
const map = MMKVParser.toStringMap(new DataView(fileBuffer)); const map = MMKVParser.toStringMap(new DataView(fileBuffer));
qmc2Keys = Array.from(map.entries(), ([name, key]) => ({ name: getFileName(name), key })); qmc2Keys = Array.from(map.entries(), ([name, key]) => ({ name: getFileName(name), key }));

View File

@ -1,28 +1,29 @@
import { formatHex } from './formatHex';
const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true }); const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
export class MMKVParser { export class MMKVParser {
private offset = 8; private offset = 4;
private length: number; private length: number;
constructor(private view: DataView) { constructor(private view: DataView) {
const payloadLength = view.getUint32(0, true); const payloadLength = view.getUint32(0, true);
this.length = 4 + payloadLength; this.length = 4 + payloadLength;
// skip unused str
this.readInt();
} }
toString() { toString() {
const offset = this.offset.toString(16).padStart(8, '0'); const offset = formatHex(this.offset, 8);
const length = this.length.toString(16).padStart(8, '0'); const length = formatHex(this.length, 8);
return `<MMKVParser offset=0x${offset} len=0x${length}>`; return `<MMKVParser offset=${offset} len=${length}>`;
} }
get eof() { get eof() {
return this.offset >= this.length; return this.offset >= this.length;
} }
peek() {
return this.view.getUint8(this.offset);
}
public readByte() { public readByte() {
return this.view.getUint8(this.offset++); return this.view.getUint8(this.offset++);
} }
@ -77,7 +78,9 @@ export class MMKVParser {
const newOffset = this.offset + containerLen; const newOffset = this.offset + containerLen;
const result = this.readString(); const result = this.readString();
if (newOffset !== this.offset) { if (newOffset !== this.offset) {
throw new Error('readVariantString failed: offset does not match'); const expected = formatHex(newOffset);
const actual = formatHex(this.offset);
throw new Error(`readVariantString failed: offset does mismatch (expect: ${expected}, actual: ${actual})`);
} }
return result; return result;
} }

View File

@ -0,0 +1,19 @@
import { MMKVParser } from '../MMKVParser';
import { readFileSync } from 'node:fs';
test('parse mmkv file as expected', () => {
const buff = readFileSync(__dirname + '/__fixture__/test.mmkv');
const view = new DataView(buff.buffer.slice(buff.byteOffset, buff.byteOffset + buff.byteLength));
expect(MMKVParser.toStringMap(view)).toEqual(
new Map([
['key', 'value'],
[
'Lorem Ipsum',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' +
'Vestibulum congue volutpat metus non molestie. Quisque id est sapien. ' +
'Fusce eget tristique sem. Donec tellus lacus, viverra sed lectus eget, elementum ultrices dolor. ' +
'Integer non urna justo.',
],
])
);
});

Binary file not shown.

3
src/util/formatHex.ts Normal file
View File

@ -0,0 +1,3 @@
export function formatHex(value: number, len = 8) {
return '0x' + (value | 0).toString(16).padStart(len, '0');
}