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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 | class AbstractCStruct(metaclass=CStructMeta):
"""
Abstract C struct to Python class
"""
__size__: int = 0
" Size in bytes "
__fields__: List[str] = []
" Struct/union fileds "
__fields_types__: Dict[str, FieldType]
" Dictionary mapping field names to types "
__byte_order__: Optional[str] = None
" Byte order "
__alignment__: int = 0
" Alignament "
__is_union__: bool = False
" True if the class is an union, False if it is a struct "
def __init__(
self, buffer: Optional[Union[bytes, BinaryIO]] = None, flexible_array_length: Optional[int] = None, **kargs: Dict[str, Any]
) -> None:
self.set_flexible_array_length(flexible_array_length)
self.__fields__ = [x for x in self.__fields__] # Create a copy
self.__fields_types__ = OrderedDict({k: v.copy() for k, v in self.__fields_types__.items()}) # Create a copy
if buffer is not None:
self.unpack(buffer)
else:
try:
self.unpack(buffer)
except Exception:
pass
for key, value in kargs.items():
setattr(self, key, value)
@classmethod
def parse(
cls,
__struct__: Union[str, Tokens, Dict[str, Any]],
__name__: Optional[str] = None,
__byte_order__: Optional[str] = None,
__is_union__: Optional[bool] = False,
**kargs: Dict[str, Any],
) -> Type["AbstractCStruct"]:
"""
Return a new class mapping a C struct/union definition.
Args:
__struct__: definition of the struct (or union) in C syntax
__name__: name of the new class. If empty, a name based on the __struct__ hash is generated
__byte_order__: byte order, valid values are LITTLE_ENDIAN, BIG_ENDIAN, NATIVE_ORDER
__is_union__: True for union, False for struct
Returns:
cls: a new class mapping the defintion
"""
cls_kargs: Dict[str, Any] = dict(kargs)
if __byte_order__ is not None:
cls_kargs["__byte_order__"] = __byte_order__
if __is_union__ is not None:
cls_kargs["__is_union__"] = __is_union__
cls_kargs["__struct__"] = __struct__
if isinstance(__struct__, (str, Tokens)):
del cls_kargs["__struct__"]
cls_kargs.update(parse_struct_def(__struct__, __cls__=cls, **cls_kargs))
cls_kargs["__struct__"] = None
elif isinstance(__struct__, dict):
del cls_kargs["__struct__"]
cls_kargs.update(__struct__)
cls_kargs["__struct__"] = None
__name__ = cls_kargs.get("__name__") or __name__
if __name__ is None: # Anonymous struct
__name__ = cls.__name__ + "_" + hashlib.sha1(str(__struct__).encode("utf-8")).hexdigest()
cls_kargs["__anonymous__"] = True
cls_kargs["__name__"] = __name__
return type(__name__, (cls,), cls_kargs)
def set_flexible_array_length(self, flexible_array_length: Optional[int]) -> None:
"""
Set flexible array length (i.e. number of elements)
Args:
flexible_array_length: flexible array length
Raises:
CStructException: If flexible array is not present in the structure
"""
if flexible_array_length is not None:
# Search for the flexible array
flexible_array: Optional[FieldType] = [x for x in self.__fields_types__.values() if x.flexible_array][0]
if flexible_array is None:
raise CStructException("Flexible array not found in struct")
flexible_array.vlen = flexible_array_length
def unpack(self, buffer: Optional[Union[bytes, BinaryIO]], flexible_array_length: Optional[int] = None) -> bool:
"""
Unpack bytes containing packed C structure data
Args:
buffer: bytes or binary stream to be unpacked
flexible_array_length: flexible array length
"""
self.set_flexible_array_length(flexible_array_length)
if hasattr(buffer, "read"):
buffer = buffer.read(self.size) # type: ignore
if not buffer:
return False
return self.unpack_from(buffer)
def unpack_from(
self, buffer: Optional[bytes], offset: int = 0, flexible_array_length: Optional[int] = None
) -> bool: # pragma: no cover
"""
Unpack bytes containing packed C structure data
Args:
buffer: bytes to be unpacked
offset: optional buffer offset
flexible_array_length: flexible array length
"""
raise NotImplementedError
def pack(self) -> bytes: # pragma: no cover
"""
Pack the structure data into bytes
Returns:
bytes: The packed structure
"""
raise NotImplementedError
def clear(self) -> None:
self.unpack(None)
def __len__(self) -> int:
"Actual structure size (in bytes)"
return self.size
@property
def size(self) -> int:
"Actual structure size (in bytes)"
if not self.__fields_types__: # no fields
return 0
elif self.__is_union__: # C union
# Calculate the sizeof union as size of its largest element
return max(x.vsize for x in self.__fields_types__.values())
else: # C struct
# Calculate the sizeof struct as last item's offset + size + padding
last_field_type = list(self.__fields_types__.values())[-1]
size = last_field_type.offset + last_field_type.vsize
padding = calculate_padding(self.__byte_order__, self.__alignment__, size)
return size + padding
@classmethod
def sizeof(cls) -> int:
"Structure size in bytes (flexible array member size is omitted)"
return cls.__size__
def inspect(self, start_addr: Optional[int] = None, end_addr: Optional[int] = None) -> str:
"""
Return memory content in hexadecimal
Args:
start_addr: start address
end_addr: end address
"""
buffer = StringIO()
if hasattr(self, "__mem__"):
mem = self.__mem__[self.__base__ :]
else:
mem = self.pack()
if end_addr is None:
end_addr = self.size
for i in range(start_addr or 0, end_addr, 16):
row = mem[i : min(i + 16, end_addr)]
buffer.write(f"{i:08x} ")
for j, c in enumerate(row):
separator = " " if j == 7 else ""
buffer.write(f" {c:02x}{separator}")
for j in range(len(row) - 1, 15):
separator = " " if j == 7 else ""
buffer.write(f" {separator}")
buffer.write(" |")
for c in row:
buffer.write(chr(c) if c >= 32 and c < 127 else ".")
for j in range(len(row) - 1, 15):
buffer.write(" ")
buffer.write("|")
buffer.write("\n")
buffer.seek(0, 0)
return buffer.read()
def __eq__(self, other: Any) -> bool:
return other is not None and isinstance(other, self.__class__) and self.__dict__ == other.__dict__
def __ne__(self, other: Any) -> bool:
return not self.__eq__(other)
def __str__(self) -> str:
result = []
for field in self.__fields__:
result.append(field + "=" + str(getattr(self, field, None)))
return type(self).__name__ + "(" + ", ".join(result) + ")"
def __repr__(self) -> str: # pragma: no cover
return self.__str__()
def __getstate__(self) -> bytes:
"""
This method is called and the returned object is pickled
as the contents for the instance, instead of the contents of
the instance’s dictionary
Returns:
bytes: The packed structure
"""
return self.pack()
def __setstate__(self, state: bytes) -> bool:
"""
This method it is called with the unpickled state
Args:
state: bytes to be unpacked
"""
return self.unpack(state)
|