-
Notifications
You must be signed in to change notification settings - Fork 5
/
dat.py
161 lines (102 loc) · 3.11 KB
/
dat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
""" Copyright 2015-2017 sta256+mpskit at gmail.com
This file is part of mpskit.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See LICENSE file for more details.
"""
from common import *
from fab import *
def save_mdat_messages(mdat_name, h):
oname = "{}.msg.json".format(mdat_name)
with open(oname, 'w') as f:
json.dump(h, f, indent=2, ensure_ascii=False)
output(oname)
def save_mdat_metainfo(mdat_name, h):
oname = "{}.id.json".format(mdat_name)
with open(oname, 'w') as f:
json.dump(h, f, indent=2)
output(oname)
def load_mdat_messages(mdat_name):
with open("{}.msg.json".format(mdat_name), 'r') as f:
return json.load(f)
def load_mdat_metainfo(mdat_name):
with open("{}.id.json".format(mdat_name), 'r') as f:
return json.load(f)
def read_mdat(fname, verbose = 0):
if fname != 'MESSAGES.DAT':
warning('mdat decoder: only MESSAGES.DAT allowed')
entries = []
metas = []
with open(fname, 'rb') as f:
# number of entries
num = read_uint16(f)
if verbose:
print('mdat: count={}'.format(num))
curr_header = f.tell()
for _ in range(num):
# entry header
f.seek(curr_header)
sid,offset,length = read_struct(f, "<IIH")
curr_header = f.tell()
# entry content
f.seek(offset)
dest = read_fab(f, length, verbose = 0)
clength = f.tell() - offset
if verbose:
print("sid = {}; length = {}; clength = {};".format(sid, length, clength))
entry = decode_string(dest.read())
entries.append(entry)
metas.append({"id": sid})
save_mdat_messages(fname, entries)
save_mdat_metainfo(fname, metas)
def write_mdat(fname, verbose = 0):
if fname != 'MESSAGES.DAT':
warning('mdat decoder: only MESSAGES.DAT allowed')
messages = load_mdat_messages(fname)
metas = load_mdat_metainfo(fname)
#
msgs = [encode_string(s, null_term=True) for s in messages]
num = len(msgs)
with open(fname, 'wb') as f:
write_uint16(f, num)
if verbose:
print('mdat: count={}'.format(num))
curr_header = f.tell()
curr_offset = f.tell() + num * struct.calcsize("<IIH")
#sid = 1
for meta,data in zip(metas, msgs):
assert data[-1] == 0
length = len(data)
#if verbose:
# print("sid = {}; offset = {}; msg = {};".format(sid, curr_offset, data))
sid = meta['id']
# head
f.seek(curr_header)
write_struct(f, "<IIH", (sid,curr_offset,length))
curr_header = f.tell()
# body
f.seek(curr_offset)
write_fab(f, data)
clength = f.tell() - curr_offset
curr_offset = f.tell()
if verbose:
print("sid = {}; length = {}; clength = {};".format(sid, length, clength))
#sid += 1
output(fname)
'''
return struct.pack("<3sBH15sH1cBBB",
b'FAB',
0x0C,
0xFFFF,
b'1234567890abcde',
0xFFFE,
b'f',
0x00,
0x00,
0x00,
)
'''