Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update descriptions and examples #94

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 52 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ refer to [the development guide](https://docs.dissect.tools/en/latest/contributi
All you need to do is instantiate a new cstruct instance and load some structure definitions in there. After that you can start using them from your Python code.

```python
from dissect import cstruct
from dissect.cstruct import cstruct

# Default endianness is LE, but can be configured using a kwarg or setting the 'endian' attribute
# e.g. cstruct.cstruct(endian='>') or cparser.endian = '>'
cparser = cstruct.cstruct()
cparser.load("""
# e.g. cstruct(endian='>') or c_parser.endian = '>'
parser_def = """
#define SOME_CONSTANT 5

enum Example : uint16 {
Expand All @@ -74,31 +73,35 @@ enum Example : uint16 {
struct some_struct {
uint8 field_1;
char field_2[SOME_CONSTANT];
char field_3[field_1 & 1 * 5]; // Some random expression to calculate array length
char field_3[(field_1 & 1) * 5]; // Some random expression to calculate array length
Example field_4[2];
};
""")
"""
c_parser = cstruct().load(parser_def)

data = b'\x01helloworld\x00\x00\x06\x00'
result = cparser.some_struct(data) # Also accepts file-like objects
data = b"\x01helloworld\x00\x00\x06\x00"
result = c_parser.some_struct(data) # Also accepts file-like objects
assert result.field_1 == 0x01
assert result.field_2 == b'hello'
assert result.field_3 == b'world'
assert result.field_4 == [cparser.Example.A, cparser.Example.C]
assert result.field_2 == b"hello"
assert result.field_3 == b"world"
assert result.field_4 == [c_parser.Example.A, c_parser.Example.C]

assert cparser.Example.A == 0
assert cparser.Example.C == 6
assert cparser.Example(5) == cparser.Example.B
assert c_parser.Example.A == 0
assert c_parser.Example.C == 6
assert c_parser.Example(5) == c_parser.Example.B

assert result.dumps() == data

# You can also instantiate structures from Python by using kwargs
# Note that array sizes are not enforced
instance = cparser.some_struct(field_1=5, field_2='lorem', field_3='ipsum', field_4=[cparser.Example.B, cparser.Example.A])
assert instance.dumps() == b'\x05loremipsum\x05\x00\x00\x00'
instance = c_parser.some_struct(
field_1=5, field_2="lorem", field_3="ipsum", field_4=[c_parser.Example.B, c_parser.Example.A]
)
assert instance.dumps() == b"\x05loremipsum\x05\x00\x00\x00"

```

By default, all structures are compiled into classes that provide optimised performance. You can disable this by passing a `compiled=False` keyword argument to the `.load()` call. You can also inspect the resulting source code by accessing the source attribute of the structure: `print(cparser.some_struct.source)`.
By default, all structures are compiled into classes that provide optimised performance. You can disable this by passing a `compiled=False` keyword argument to the `.load()` call. You can also inspect the resulting source code by accessing the source attribute of the structure: `print(c_parser.some_struct.source)`.

More examples can be found in the `examples` directory.

Expand All @@ -110,20 +113,23 @@ Write simple C-like structures and use them to parse binary data, as can be seen
Aside from loading structure definitions, any of the supported types can be used individually for parsing data. For example, the following is all supported:

```python
from dissect import cstruct
cs = cstruct.cstruct()
from dissect.cstruct import cstruct

cs = cstruct()
# Default endianness is LE, but can be configured using a kwarg or setting the attribute
# e.g. cstruct.cstruct(endian='>') or cs.endian = '>'
assert cs.uint32(b'\x05\x00\x00\x00') == 5
assert cs.uint24[2](b'\x01\x00\x00\x02\x00\x00') == [1, 2] # You can also parse arrays using list indexing
assert cs.char[None](b'hello world!\x00') == b'hello world!' # A list index of None means null terminated
# e.g. cstruct(endian='>') or cs.endian = '>'
assert cs.uint32(b"\x05\x00\x00\x00") == 5
assert cs.uint24[2](b"\x01\x00\x00\x02\x00\x00") == [1, 2] # You can also parse arrays using list indexing
assert cs.char[None](b"hello world!\x00") == b"hello world!" # A list index of None means null terminated
```

### Unions and nested structures
Unions and nested structures are supported, both anonymous and named.

```python
cdef = """
from dissect.cstruct import cstruct

c_def = """
struct test_union {
char magic[4];
union {
Expand All @@ -148,58 +154,61 @@ struct test_anonymous {
};
};
"""
c = cstruct.cstruct()
c.load(cdef)
# Default endianness is LE, but can be configured using a kwarg or setting the attribute
# e.g. cstruct(endian='>') or cs.endian = '>'
c = cstruct().load(c_def)

assert len(c.test_union) == 12

a = c.test_union(b'ohaideadbeef')
assert a.magic == b'ohai'
a = c.test_union(b"ohaideadbeef")
assert a.magic == b"ohai"
assert a.c.a.a == 0x64616564
assert a.c.a.b == 0x66656562
assert a.c.b.b == b'deadbeef'
assert a.c.b.b == b"deadbeef"

assert a.dumps() == b'ohaideadbeef'
assert a.dumps() == b"ohaideadbeef"

b = c.test_anonymous(b'ohai\x39\x05\x00\x00\x28\x23\x00\x00deadbeef')
assert b.magic == b'ohai'
b = c.test_anonymous(b"ohai\x39\x05\x00\x00\x28\x23\x00\x00deadbeef")
assert b.magic == b"ohai"
assert b.a == 1337
assert b.b == 9000
assert b.c == b'deadbeef'
assert b.c == b"deadbeef"

```

### Parse bit fields
Bit fields are supported as part of structures. They are properly aligned to their boundaries.

```python
bitdef = """
from dissect.cstruct import cstruct

bit_def = """
struct test {
uint16 a:1;
uint16 b:1; # Read 2 bits from an uint16
uint32 c; # The next field is properly aligned
uint16 b:1; // Read 2 bits from an uint16
uint32 c; // The next field is properly aligned
uint16 d:2;
uint16 e:3;
};
"""
bitfields = cstruct.cstruct()
bitfields.load(bitdef)
bitfields = cstruct().load(bit_def)

d = b'\x03\x00\xff\x00\x00\x00\x1f\x00'
d = b"\x03\x00\xff\x00\x00\x00\x1f\x00"
a = bitfields.test(d)

assert a.a == 0b1
assert a.b == 0b1
assert a.c == 0xff
assert a.c == 0xFF
assert a.d == 0b11
assert a.e == 0b111
assert a.dumps() == d
```

### Enums
The API to access enum members and their values is similar to that of the native Enum type in Python 3. Functionally, it's best comparable to the IntEnum type.
### Enums and Flags
The API to access enum and flag members and their values in the same way as the native Enum types in Python 3. Functionally, it's best comparable to the IntEnum or IntFlag type.

### Custom types
You can implement your own types by subclassing `BaseType` or `RawType`, and adding them to your cstruct instance with `add_type(name, type)`
You can implement your own types by subclassing `BaseType`, and adding them to your cstruct instance with `add_custom_type(name, type, size, alignment, ...)`

### Custom definition parsers
Don't like the C-like definition syntax? Write your own syntax parser!
Expand Down