All Projects → andreax79 → python-cstruct

andreax79 / python-cstruct

Licence: MIT license
C-style structs for Python

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to python-cstruct

fastproto
FastProto is a binary data processing tool written in Java.
Stars: ✭ 65 (+71.05%)
Mutual labels:  binary, pack
cstruct-go
a fast c-style struct packer & unpacker for golang
Stars: ✭ 28 (-26.32%)
Mutual labels:  binary, struct
mdict-utils
MDict pack/unpack/list/info tool
Stars: ✭ 105 (+176.32%)
Mutual labels:  pack, unpack
binstruct
Golang binary decoder for mapping data into the structure
Stars: ✭ 67 (+76.32%)
Mutual labels:  binary, struct
dmc unrar
A dependency-free, single-file FLOSS unrar library
Stars: ✭ 47 (+23.68%)
Mutual labels:  unpack
nason
🗜 Ultra tiny serializer / encoder with plugin-support. Useful to build binary files containing images, strings, numbers and more!
Stars: ✭ 30 (-21.05%)
Mutual labels:  binary
ronin-support
A support library for Ronin. Like activesupport, but for hacking!
Stars: ✭ 23 (-39.47%)
Mutual labels:  binary
parco
🏇🏻 generalist, fast and tiny binary parser and compiler generator, powered by Go 1.18+ Generics
Stars: ✭ 57 (+50%)
Mutual labels:  binary
libdemangle
A simple library focusing on demangling symbols for different programing languages
Stars: ✭ 34 (-10.53%)
Mutual labels:  binary
IMCtermite
Enables extraction of measurement data from binary files with extension 'raw' used by proprietary software imcFAMOS/imcSTUDIO and facilitates its storage in open source file formats
Stars: ✭ 20 (-47.37%)
Mutual labels:  binary
FortniteReplayDecompressor
Read Fortnite replay files
Stars: ✭ 68 (+78.95%)
Mutual labels:  binary
Apex.Serialization
High performance contract-less binary serializer for .NET
Stars: ✭ 82 (+115.79%)
Mutual labels:  binary
win-wallpaper
Manage the desktop wallpaper on Windows
Stars: ✭ 62 (+63.16%)
Mutual labels:  binary
rectangle-pack
A general purpose, deterministic bin packer designed to conform to any two or three dimensional use case.
Stars: ✭ 60 (+57.89%)
Mutual labels:  pack
table
Produces a string that represents slice data in a text table, inspired by gajus/table.
Stars: ✭ 130 (+242.11%)
Mutual labels:  struct
faker
Random fake data and struct generator for Go.
Stars: ✭ 67 (+76.32%)
Mutual labels:  struct
xbedump
Tool to dump header information or sign original Xbox executables
Stars: ✭ 18 (-52.63%)
Mutual labels:  binary
Binary-Learning
二进制安全相关的学习笔记,感谢滴水逆向的所有老师辛苦教学。
Stars: ✭ 886 (+2231.58%)
Mutual labels:  binary
BinaryStream
BinaryStream - a writer and reader for binary data. Best replacement for pack()/unpack().
Stars: ✭ 44 (+15.79%)
Mutual labels:  binary
GroBuf
Fast binary serializer
Stars: ✭ 56 (+47.37%)
Mutual labels:  binary

Python-CStruct

C-style structs for Python

Build Status PyPI version PyPI Downloads Code style: black Known Vulnerabilities Documentation

Convert C struct/union definitions into Python classes with methods for serializing/deserializing. The usage is very simple: create a class subclassing cstruct.MemCStruct and add a C struct/union definition as a string in the struct field. The C struct/union definition is parsed at runtime and the struct format string is generated. The class offers the method "unpack" for deserializing an array of bytes into a Python object and the method "pack" for serializing the values into an array of bytes.

Example

The following program reads the DOS partition information from a disk.

#!/usr/bin/env python
import cstruct

class Position(cstruct.MemCStruct):
    __byte_order__ = cstruct.LITTLE_ENDIAN
    __def__ = """
        struct {
            unsigned char head;
            unsigned char sector;
            unsigned char cyl;
        }
    """

class Partition(cstruct.MemCStruct):
    __byte_order__ = cstruct.LITTLE_ENDIAN
    __def__ = """
        struct {
            unsigned char status;       /* 0x80 - active */
            struct Position start;
            unsigned char partition_type;
            struct Position end;
            unsigned int start_sect;    /* starting sector counting from 0 */
            unsigned int sectors;       /* nr of sectors in partition */
        }
    """

    def print_info(self):
        print("bootable: %s" % ((self.status & 0x80) and "Y" or "N"))
        print("partition_type: %02X" % self.partition_type)
        print("start: head: %X sectory: %X cyl: %X" % (self.start.head, self.start.sector, self.start.cyl))
        print("end: head: %X sectory: %X cyl: %X" % (self.end.head, self.end.sector, self.end.cyl))
        print("starting sector: %08X" % self.start_sect)
        print("size MB: %s" % (self.sectors / 2 / 1024))

class MBR(cstruct.MemCStruct):
    __byte_order__ = cstruct.LITTLE_ENDIAN
    __def__ = """
        struct {
            char unused[440];
            unsigned char disk_signature[4];
            unsigned char usualy_nulls[2];
            struct Partition partitions[4];
            char signature[2];
        }
    """

    def print_info(self):
        print("disk signature: %s" % "".join(["%02X" % x for x in self.disk_signature]))
        print("usualy nulls: %s" % "".join(["%02X" % x for x in self.usualy_nulls]))
        for i, partition in enumerate(self.partitions):
            print("")
            print("partition: %s" % i)
            partition.print_info()

disk = "mbr"
with open(disk, "rb") as f:
    mbr = MBR()
    mbr.unpack(f)
    mbr.print_info()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].