Skip to content

Commit 6236ff6

Browse files
committed
Update README.md
1 parent 26c15be commit 6236ff6

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

README.md

+44-32
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ is generated. The class offers the method "unpack" for deserializing
2020
an array of bytes into a Python object and the method "pack" for
2121
serializing the values into an array of bytes.
2222

23+
[Api Documentation](https://readthedocs.org/projects/python-cstruct/badge/?version=latest)
24+
2325
Example
2426
-------
2527

@@ -31,53 +33,63 @@ import cstruct
3133

3234
class Position(cstruct.MemCStruct):
3335
__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;
4040
"""
4141

42+
4243
class Partition(cstruct.MemCStruct):
4344
__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 */
5354
"""
5455

5556
def print_info(self):
56-
print("bootable: %s" % ((self.status & 0x80) 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+
6264

6365
class MBR(cstruct.MemCStruct):
6466
__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;
7382
"""
7483

84+
@property
85+
def disk_signature_str(self):
86+
return "".join(reversed([f"{x:02x}" for x in self.disk_signature]))
87+
7588
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}")
7890
for i, partition in enumerate(self.partitions):
7991
print("")
80-
print("partition: %s" % i)
92+
print(f"partition: {i}")
8193
partition.print_info()
8294

8395
disk = "mbr"

0 commit comments

Comments
 (0)