|
1 |
| -python-cstruct |
| 1 | +Python-CStruct |
2 | 2 | ==============
|
3 | 3 |
|
4 | 4 | C-style structs for Python
|
| 5 | + |
| 6 | +Convert C struct definitions into Python classes with methods for |
| 7 | +serializing/deserializing. |
| 8 | +The usage is very simple: create a class subclassing cstruct.CStruct |
| 9 | +and add a C struct definition as a string in the __struct__ field. |
| 10 | +The C struct definition is parsed at runtime and the struct format string |
| 11 | +is generated. The class offers the method "unpack" for deserializing |
| 12 | +a string of bytes into a Python object and the method "pack" for |
| 13 | +serializing the values into a string. |
| 14 | + |
| 15 | +Example |
| 16 | +------- |
| 17 | + |
| 18 | +The following program reads the DOS partition information from a disk. |
| 19 | + |
| 20 | +```python |
| 21 | +#!/usr/bin/python |
| 22 | +import cstruct |
| 23 | + |
| 24 | +class Position(cstruct.CStruct): |
| 25 | + __byte_order__ = cstruct.LITTLE_ENDIAN |
| 26 | + __struct__ = """ |
| 27 | + unsigned char head; |
| 28 | + unsigned char sector; |
| 29 | + unsigned char cyl; |
| 30 | + """ |
| 31 | + |
| 32 | +class Partition(cstruct.CStruct): |
| 33 | + __byte_order__ = cstruct.LITTLE_ENDIAN |
| 34 | + __struct__ = """ |
| 35 | + unsigned char status; /* 0x80 - active */ |
| 36 | + struct Position start; |
| 37 | + unsigned char partition_type; |
| 38 | + struct Position end; |
| 39 | + unsigned int start_sect; /* starting sector counting from 0 */ |
| 40 | + unsigned int sectors; /* nr of sectors in partition */ |
| 41 | + """ |
| 42 | + |
| 43 | + def print_info(self): |
| 44 | + print("bootable: %s" % ((self.status & 0x80) and "Y" or "N")) |
| 45 | + print("partition_type: %02X" % self.partition_type) |
| 46 | + print("start: head: %X sectory: %X cyl: %X" % (self.start.head, self.start.sector, self.start.cyl)) |
| 47 | + print("end: head: %X sectory: %X cyl: %X" % (self.end.head, self.end.sector, self.end.cyl)) |
| 48 | + print("starting sector: %08X" % self.start_sect) |
| 49 | + print("size MB: %s" % (self.sectors / 2 / 1024)) |
| 50 | + |
| 51 | +class MBR(cstruct.CStruct): |
| 52 | + __byte_order__ = cstruct.LITTLE_ENDIAN |
| 53 | + __struct__ = """ |
| 54 | + char unused[440]; |
| 55 | + unsigned char disk_signature[4]; |
| 56 | + unsigned char usualy_nulls[2]; |
| 57 | + struct Partition partitions[4]; |
| 58 | + char signature[2]; |
| 59 | + """ |
| 60 | + |
| 61 | + def print_info(self): |
| 62 | + print("disk signature: %s" % "".join(["%02X" % x for x in self.disk_signature])) |
| 63 | + print("usualy nulls: %s" % "".join(["%02X" % x for x in self.usualy_nulls])) |
| 64 | + for i, partition in enumerate(self.partitions): |
| 65 | + print("") |
| 66 | + print("partition: %s" % i) |
| 67 | + partition.print_info() |
| 68 | + |
| 69 | +disk = "mbr" |
| 70 | +f = open(disk, "rb") |
| 71 | +mbr = MBR() |
| 72 | +data = f.read(len(mbr)) |
| 73 | +mbr.unpack(data) |
| 74 | +mbr.print_info() |
| 75 | +f.close() |
| 76 | +``` |
| 77 | + |
0 commit comments