@@ -20,6 +20,8 @@ is generated. The class offers the method "unpack" for deserializing
20
20
an array of bytes into a Python object and the method "pack" for
21
21
serializing the values into an array of bytes.
22
22
23
+ [ Api Documentation] ( https://readthedocs.org/projects/python-cstruct/badge/?version=latest )
24
+
23
25
Example
24
26
-------
25
27
@@ -31,53 +33,63 @@ import cstruct
31
33
32
34
class Position (cstruct .MemCStruct ):
33
35
__byte_order__ = cstruct.LITTLE_ENDIAN
34
- __def__ = """
35
- struct {
36
- unsigned char head;
37
- unsigned char sector;
38
- unsigned char cyl;
39
- }
36
+ __struct__ = """
37
+ unsigned char head;
38
+ unsigned char sector;
39
+ unsigned char cyl;
40
40
"""
41
41
42
+
42
43
class Partition (cstruct .MemCStruct ):
43
44
__byte_order__ = cstruct.LITTLE_ENDIAN
44
- __def__ = """
45
- struct {
46
- unsigned char status; /* 0x80 - active */
47
- struct Position start;
48
- unsigned char partition_type ;
49
- struct Position end ;
50
- unsigned int start_sect; /* starting sector counting from 0 */
51
- unsigned int sectors ; /* nr of sectors in partition */
52
- }
45
+ __struct__ = """
46
+ #define ACTIVE_FLAG 0x80
47
+
48
+ unsigned char status; /* 0x80 - active */
49
+ struct Position start ;
50
+ unsigned char partition_type ;
51
+ struct Position end;
52
+ unsigned int start_sect ; /* starting sector counting from 0 */
53
+ unsigned int sectors; /* nr of sectors in partition */
53
54
"""
54
55
55
56
def print_info (self ):
56
- print (" bootable: %s " % ((self .status & 0x 80 ) and " Y" or " N" ))
57
- print (" partition_type: %02X " % self .partition_type)
58
- print (" start: head: %X sectory: %X cyl: %X " % (self .start.head, self .start.sector, self .start.cyl))
59
- print (" end: head: %X sectory: %X cyl: %X " % (self .end.head, self .end.sector, self .end.cyl))
60
- print (" starting sector: %08X " % self .start_sect)
61
- print (" size MB: %s " % (self .sectors / 2 / 1024 ))
57
+ print (f " bootable: { ' Y' if self .status & cstruct.getdef(' ACTIVE_FLAG' ) else ' N' } " )
58
+ print (f " partition_type: { self .partition_type:02X } " )
59
+ print (f " start: head: { self .start.head:X } sectory: { self .start.sector:X } cyl: { self .start.cyl:X } " )
60
+ print (f " end: head: { self .end.head:X } sectory: { self .end.sector:X } cyl: { self .end.cyl:X } " )
61
+ print (f " starting sector: { self .start_sect:08x } " )
62
+ print (f " size MB: { self .sectors / 2 / 1024 } " )
63
+
62
64
63
65
class MBR (cstruct .MemCStruct ):
64
66
__byte_order__ = cstruct.LITTLE_ENDIAN
65
- __def__ = """
66
- struct {
67
- char unused[440];
68
- unsigned char disk_signature[4];
69
- unsigned char usualy_nulls[2];
70
- struct Partition partitions[4];
71
- char signature[2];
72
- }
67
+ __struct__ = """
68
+ #define MBR_SIZE 512
69
+ #define MBR_DISK_SIGNATURE_SIZE 4
70
+ #define MBR_USUALY_NULLS_SIZE 2
71
+ #define MBR_SIGNATURE_SIZE 2
72
+ #define MBR_BOOT_SIGNATURE 0xaa55
73
+ #define MBR_PARTITIONS_NUM 4
74
+ #define MBR_PARTITIONS_SIZE (sizeof(Partition) * MBR_PARTITIONS_NUM)
75
+ #define MBR_UNUSED_SIZE (MBR_SIZE - MBR_DISK_SIGNATURE_SIZE - MBR_USUALY_NULLS_SIZE - MBR_PARTITIONS_SIZE - MBR_SIGNATURE_SIZE)
76
+
77
+ char unused[MBR_UNUSED_SIZE];
78
+ unsigned char disk_signature[MBR_DISK_SIGNATURE_SIZE];
79
+ unsigned char usualy_nulls[MBR_USUALY_NULLS_SIZE];
80
+ struct Partition partitions[MBR_PARTITIONS_NUM];
81
+ uint16 signature;
73
82
"""
74
83
84
+ @ property
85
+ def disk_signature_str (self ):
86
+ return " " .join(reversed ([f " { x:02x } " for x in self .disk_signature]))
87
+
75
88
def print_info (self ):
76
- print (" disk signature: %s " % " " .join([" %02X " % x for x in self .disk_signature]))
77
- print (" usualy nulls: %s " % " " .join([" %02X " % x for x in self .usualy_nulls]))
89
+ print (f " disk signature: { self .disk_signature_str} " )
78
90
for i, partition in enumerate (self .partitions):
79
91
print (" " )
80
- print (" partition: %s " % i )
92
+ print (f " partition: { i } " )
81
93
partition.print_info()
82
94
83
95
disk = " mbr"
0 commit comments