@@ -13,11 +13,13 @@ C-style structs for Python
1313
1414Convert C struct/union definitions into Python classes with methods for
1515serializing/deserializing.
16+
1617The usage is very simple: create a class subclassing cstruct.MemCStruct
17- and add a C struct/union definition as a string in the __ struct__ field.
18+ and add a C struct/union definition as a string in the ` __def__ ` field.
19+
1820The C struct/union definition is parsed at runtime and the struct format string
19- is generated. The class offers the method " unpack" for deserializing
20- an array of bytes into a Python object and the method " pack" for
21+ is generated. The class offers the method ` unpack ` for deserializing
22+ an array of bytes into a Python object and the method ` pack ` for
2123serializing the values into an array of bytes.
2224
2325[ Api Documentation] ( https://python-cstruct.readthedocs.io/en/latest/ )
@@ -272,7 +274,7 @@ C expressions are automatically evaluated during structure definitions:
272274``` python
273275class MBR (cstruct .MemCStruct ):
274276 __byte_order__ = cstruct.LITTLE_ENDIAN
275- __struct__ = """
277+ __def__ = """
276278 #define MBR_SIZE 512
277279 #define MBR_DISK_SIGNATURE_SIZE 4
278280 #define MBR_USUALY_NULLS_SIZE 2
@@ -282,11 +284,13 @@ class MBR(cstruct.MemCStruct):
282284 #define MBR_PARTITIONS_SIZE (sizeof(Partition) * MBR_PARTITIONS_NUM)
283285 #define MBR_UNUSED_SIZE (MBR_SIZE - MBR_DISK_SIGNATURE_SIZE - MBR_USUALY_NULLS_SIZE - MBR_PARTITIONS_SIZE - MBR_SIGNATURE_SIZE)
284286
285- char unused[MBR_UNUSED_SIZE];
286- unsigned char disk_signature[MBR_DISK_SIGNATURE_SIZE];
287- unsigned char usualy_nulls[MBR_USUALY_NULLS_SIZE];
288- struct Partition partitions[MBR_PARTITIONS_NUM];
289- uint16 signature;
287+ struct {
288+ char unused[MBR_UNUSED_SIZE];
289+ unsigned char disk_signature[MBR_DISK_SIGNATURE_SIZE];
290+ unsigned char usualy_nulls[MBR_USUALY_NULLS_SIZE];
291+ struct Partition partitions[MBR_PARTITIONS_NUM];
292+ uint16 signature;
293+ }
290294 """
291295```
292296
@@ -301,24 +305,30 @@ import cstruct
301305
302306class Position (cstruct .MemCStruct ):
303307 __byte_order__ = cstruct.LITTLE_ENDIAN
304- __struct__ = """
305- unsigned char head;
306- unsigned char sector;
307- unsigned char cyl;
308+ __def__ = """
309+ struct {
310+ unsigned char head;
311+ unsigned char sector;
312+ unsigned char cyl;
313+ }
308314 """
309315
310316
311317class Partition (cstruct .MemCStruct ):
312318 __byte_order__ = cstruct.LITTLE_ENDIAN
313- __struct__ = """
319+ __def__ = """
314320 #define ACTIVE_FLAG 0x80
315321
316- unsigned char status; /* 0x80 - active */
317- struct Position start;
318- unsigned char partition_type;
319- struct Position end;
320- unsigned int start_sect; /* starting sector counting from 0 */
321- unsigned int sectors; /* nr of sectors in partition */
322+ typedef struct Position Position;
323+
324+ struct {
325+ unsigned char status; /* 0x80 - active */
326+ Position start;
327+ unsigned char partition_type;
328+ Position end;
329+ unsigned int start_sect; /* starting sector counting from 0 */
330+ unsigned int sectors; /* nr of sectors in partition */
331+ }
322332 """
323333
324334 def print_info (self ):
@@ -332,7 +342,7 @@ class Partition(cstruct.MemCStruct):
332342
333343class MBR (cstruct .MemCStruct ):
334344 __byte_order__ = cstruct.LITTLE_ENDIAN
335- __struct__ = """
345+ __def__ = """
336346 #define MBR_SIZE 512
337347 #define MBR_DISK_SIGNATURE_SIZE 4
338348 #define MBR_USUALY_NULLS_SIZE 2
@@ -342,11 +352,15 @@ class MBR(cstruct.MemCStruct):
342352 #define MBR_PARTITIONS_SIZE (sizeof(Partition) * MBR_PARTITIONS_NUM)
343353 #define MBR_UNUSED_SIZE (MBR_SIZE - MBR_DISK_SIGNATURE_SIZE - MBR_USUALY_NULLS_SIZE - MBR_PARTITIONS_SIZE - MBR_SIGNATURE_SIZE)
344354
345- char unused[MBR_UNUSED_SIZE];
346- unsigned char disk_signature[MBR_DISK_SIGNATURE_SIZE];
347- unsigned char usualy_nulls[MBR_USUALY_NULLS_SIZE];
348- struct Partition partitions[MBR_PARTITIONS_NUM];
349- uint16 signature;
355+ typedef struct Partition Partition;
356+
357+ struct {
358+ char unused[MBR_UNUSED_SIZE];
359+ unsigned char disk_signature[MBR_DISK_SIGNATURE_SIZE];
360+ unsigned char usualy_nulls[MBR_USUALY_NULLS_SIZE];
361+ Partition partitions[MBR_PARTITIONS_NUM];
362+ uint16 signature;
363+ }
350364 """
351365
352366 @ property
0 commit comments