|
| 1 | +# llms.txt - Guidance for Large Language Models |
| 2 | +# Project: Python-CStruct |
| 3 | + |
| 4 | +python-cstruct is a Python module for defining, parsing, and serializing |
| 5 | +C-style binary structures using C syntax (`struct`, `union`, `enum`). |
| 6 | +It allows users to describe structured binary layouts and convert |
| 7 | +between raw bytes and Python objects. |
| 8 | + |
| 9 | +python-cstruct is a Python library for defining and parsing binary data using |
| 10 | +C-style syntax (`struct`, `union`, `enum`). It converts C definitions into |
| 11 | +Python classes that can pack and unpack raw bytes deterministically. |
| 12 | + |
| 13 | +## What This Project Is |
| 14 | +- A binary parsing and serialization library |
| 15 | +- Based on C syntax embedded as strings |
| 16 | +- Produces Python objects with `.pack()` / `.unpack()` |
| 17 | +- Suitable for files, protocols, and low-level data formats |
| 18 | + |
| 19 | +## What This Project Is NOT |
| 20 | +- NOT a general Python–C FFI or ABI binding |
| 21 | +- NOT a replacement for `ctypes.Structure` |
| 22 | +- NOT an automatic structure inference tool |
| 23 | + |
| 24 | +## Core APIs & Concepts |
| 25 | +- `MemCStruct`: base class for C `struct` / `union` definitions |
| 26 | +- `CEnum`: base class for C `enum` definitions |
| 27 | +- `cstruct.parse()`: parse C definitions at runtime |
| 28 | +- Fields are accessed as Python attributes |
| 29 | +- Layout is deterministic and explicit |
| 30 | + |
| 31 | +## Endianness |
| 32 | +- Explicitly controlled via: |
| 33 | + - `LITTLE_ENDIAN` |
| 34 | + - `BIG_ENDIAN` |
| 35 | + - `NATIVE_ORDER` |
| 36 | +- Do NOT assume platform defaults |
| 37 | + |
| 38 | +## Typical Usage |
| 39 | +1. Define a C struct/union/enum as a string |
| 40 | +2. Bind it to a Python class or parse it dynamically |
| 41 | +3. Pack Python objects into bytes or unpack bytes into objects |
| 42 | +4. Access fields as attributes |
| 43 | + |
| 44 | +## Examples (Authoritative) |
| 45 | +- MBR partition parsing: |
| 46 | + https://python-cstruct.readthedocs.io/en/latest/examples/fdisk/ |
| 47 | +- Logged users (`who`) example: |
| 48 | + https://python-cstruct.readthedocs.io/en/latest/examples/who/ |
| 49 | +- Flexible Array Member (FAM): |
| 50 | + https://python-cstruct.readthedocs.io/en/latest/examples/flexible_array/ |
| 51 | +- libc integration (ctypes): |
| 52 | + https://python-cstruct.readthedocs.io/en/latest/examples/dir/ |
| 53 | + |
| 54 | +## API Reference (Source of Truth) |
| 55 | +https://python-cstruct.readthedocs.io/en/latest/api/module/ |
| 56 | + |
| 57 | +## Guidance for Code Generation |
| 58 | +- Use real API names (`MemCStruct`, `CEnum`, `cstruct.parse`) |
| 59 | +- Always specify byte order when relevant |
| 60 | +- Follow the official documentation if uncertain |
| 61 | + |
| 62 | +If there is any conflict between assumptions and the documentation or source |
| 63 | +code, the documentation and source code take precedence. |
0 commit comments