import
numpy as np
import
mmkv
ed
=
'little'
class
MMKVReader():
def
__init__(
self
, f_path)
-
>
None
:
self
.f
=
open
(f_path,
'rb'
)
self
.real_size
=
int
.from_bytes(
self
.f.read(
4
), ed)
self
.get_key_len()
self
.d
=
{}
def
read_int8(
self
):
size
=
int
.from_bytes(
self
.f.read(
1
), ed)
tmp
=
np.int8(size)
return
tmp
def
get_key_len(
self
):
tmp
=
self
.read_int8()
if
tmp >
=
0
:
return
tmp
result
=
tmp &
0x7f
tmp
=
self
.read_int8()
if
tmp >
=
0
:
result |
=
tmp <<
7
else
:
result |
=
(tmp &
0x7f
) <<
7
tmp
=
self
.read_int8()
if
tmp >
=
0
:
result |
=
tmp <<
14
else
:
result |
=
(tmp &
0x7f
) <<
14
tmp
=
self
.read_int8()
if
tmp >
=
0
:
result |
=
tmp <<
21
else
:
result |
=
(tmp &
0x7f
) <<
21
tmp
=
self
.read_int8()
result |
=
tmp <<
28
if
tmp <
0
:
for
i
in
range
(
5
):
tmp
=
self
.read_int8()
if
tmp >
=
0
:
return
result
raise
ValueError(
'协议错误'
)
return
result
def
get_data(
self
):
while
True
:
if
(
self
.f.tell()
-
4
) >
=
self
.real_size:
break
ken_len
=
self
.get_key_len()
key
=
self
.f.read(ken_len)
data_len
=
self
.get_key_len()
data
=
self
.f.read(data_len)
self
.d[key]
=
data
self
.f.close()
if
__name__
=
=
"__main__"
:
mr
=
MMKVReader(
'./mmkv.default'
)
print
(mr.get_data())
mmkv.MMKV.initializeMMKV(
'.'
, mmkv.MMKVLogLevel.Debug)
kv
=
mmkv.MMKV.defaultMMKV()
for
key
in
kv.keys():
print
(key)