-
Notifications
You must be signed in to change notification settings - Fork 0
/
structpack.py
225 lines (203 loc) · 6.82 KB
/
structpack.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import os
import struct
import json
VALUE = "value"
FMT = "fmt"
STRUCTURE = "structure"
DEFINES = "defines"
# types = ("uint8", "int8", "uint16", "int16", "uint", "int", "uint64", "int64", \
# "float", "double", "string", "struct")
types = {
"uint8":'B',
"int8":'b',
"uint16":'H',
"int16":'h',
"uint":"I",
"int":'i',
"uint64":'Q',
"int64":'q',
"float":'f',
"double":'d',
"string":'c'
}
sizes = {
"uint8":1,
"int8":1,
"uint16":2,
"int16":2,
"uint":4,
"int":4,
"uint64":8,
"int64":8,
"float":4,
"double":4,
"string":1
}
numbers = ("uint8", "int8", "uint16", "int16", "uint", "int", "uint64", "int64")
types_size = {}
def _type2fmt(type, num):
for key, value in types.items():
if type == key:
return value * num
return None
def _dict2fmt(s_dict):
fmt = ''
for key, value in s_dict.items():
if type(value) is type({}):
print("value type error!")
continue
num = 1
if '[' in value:
print(key, value)
#截取数组大小
num = int(value[value.find('[') + 1: value.find(']')])
value = value[:value.find('[')]
fmt = fmt + _type2fmt(value, num)
return fmt
def _dict2value(d_dict):
values = ()
for key, value in d_dict.items():
if type(value) is type({}):
print("value type error!")
continue
return values
def struct_pack(json_path):
fd = open(json_path, "r")
struct_json =json.load(fd)
fd.close()
#print(struct_json)
if STRUCTURE not in struct_json.keys() or DEFINES not in struct_json.keys():
print("[%s] or [%s] not exist!" % STRUCTURE, DEFINES)
return
ss = struct_json[STRUCTURE]
dd = struct_json[DEFINES]
s_dict = {}
for s_key in ss.keys():
s_dict[s_key] = {}
s_dict[s_key][FMT] = _dict2fmt(ss[s_key])
for d_key in dd.keys():
if d_key not in s_dict.keys():
print("not this structure[%s]" % d_key)
continue
#todo check key
value = _dict2value(dd[d_key])
s = struct.Struct(s_dict[d_key][FMT])
s_dict[d_key][VALUE] = s.pack(*value)
return s_dict
class structpack(object):
def __init__(self, json_str):
self.__s_info = json.loads(json_str)
self.__convert()
def __type2fmt(self, type):
for key, value in types.items():
if type == key:
return value
return None
def __type2size(self, type):
for key, value in sizes.items():
if type == key:
return value
return None
def __struct_info_gen(self, s_key, info):
struct_info = {}
for member, value in info.items():
struct_info[member] = {}
struct_info[member]["name"] = member
num = 1
if '[' in value:
#截取数组大小
num = int(value[value.find('[') + 1: value.find(']')])
value = value[:value.find('[')]
if value not in types.keys():
print("member type not define!")
return
struct_info[member]["type"] = value
struct_info[member]["num"] = num
struct_info[member]["size"] = self.__type2size(value)
struct_info[member]["fmt"] = (str(num) if num > 1 else '') + self.__type2fmt(value)
self.__s_dict[s_key] = struct_info
def __val_convert(self, value_info, value):
value_type = value_info["type"]
num = value_info["num"]
if value_type == "string":
if num > len(value):
value = value + '\0' * (num - len(value))
else:
value = value[:num]
return value
elif value_type == "float" or value_type == "double":
if num == 1:
return float(value)
else:
data = []
values = value.split(",")
for i in range(num):
data.append(float(values[i]) if i < len(values) else 0.0)
return tuple(data)
elif value_type in numbers:
if num == 1:
return int(value, 16 if ('0x' or '0X') in value else 10)
else:
values = value.split(",")
data = []
for i in range(num):
data.append(int(values[i], 16 if ('0x' or '0X') in values else 10) \
if i < len(values) else 0)
return tuple(data)
else:
print("value type unsupport!")
return None
def __value_gen(self, d_key, dd):
for key, value in dd.items():
value_info = self.__s_dict[d_key][key]
data = self.__val_convert(value_info, value)
self.__s_dict[d_key][key].update({"value":data})
def __convert(self):
self.__s_dict = {}
if STRUCTURE not in self.__s_info.keys() or DEFINES not in self.__s_info.keys():
print("[%s] or [%s] not exist!" % STRUCTURE, DEFINES)
return
ss = self.__s_info[STRUCTURE] #struct info
dd = self.__s_info[DEFINES] #struct defines
for s_key in ss.keys(): #s_key: struct define
self.__struct_info_gen(s_key, ss[s_key])
#defines find
for d_key in dd.keys():
if d_key not in self.__s_dict.keys():
print("not this structure[%s]" % d_key)
continue
self.__value_gen(d_key, dd[d_key])
def fmt(self, struct_name):
fmt = ''
for member, info in self.__s_dict[struct_name].items():
fmt = fmt + info["fmt"]
return fmt
def value(self, struct_name, prefix_fmt = ''):
if struct_name not in self.__s_dict.keys():
return None
values = []
for member, info in self.__s_dict[struct_name].items():
value = info["value"]
if type(value) == str:
for i in range(len(value)):
values.append(bytes(value[i], encoding="utf-8"))
elif type(value) == tuple or type == list:
for data in value:
values.append(data)
elif type(value) == int or type(type) == float:
values.append(value)
else:
print("data type error!")
s = struct.Struct(prefix_fmt + self.fmt(struct_name))
return s.pack(*tuple(values))
if __name__ == "__main__":
path = "./samples/demo.json"
# print(struct_pack(path))
with open(path, "r") as fd:
json_str = fd.read()
binhead = structpack(json_str)
print(binhead.fmt("bin_head"))
with open("bindata.bin", "wb") as fd:
data = binhead.value("bin_head")
fd.write(data)
print(data)