Skip to content

Add endian keyword argument to type reads#144

Open
Schamper wants to merge 1 commit intomainfrom
add-endian-kwarg
Open

Add endian keyword argument to type reads#144
Schamper wants to merge 1 commit intomainfrom
add-endian-kwarg

Conversation

@Schamper
Copy link
Member

Partially solves #143, but I'd still like the context manager approach to be in there too. But that depends on #140.

Adds the ability to override the endianness on single type reads. So for example:

In [1]: from dissect.cstruct import cstruct

In [2]: cs = cstruct(endian="<")

In [3]: hex(cs.uint32(b"\x01\x02\x03\x04"))
Out[3]: '0x4030201'

In [4]: hex(cs.uint32(b"\x01\x02\x03\x04", endian=">"))
Out[4]: '0x1020304'

This should work on any type, including structs, unions and pointers.

Because the function signature for type reading and writing changes because of this, I also added a check when adding a custom type to ensure it's signature is correct. If it's not, it will either throw a (descriptive) error or issue a warning. I also intentionally added a **kwargs to the function signature to allow for future expansion without breaking backwards compatibility again.

I'm not 100% if the behavior of pointers is as expected. I implemented it so that:

  • If you don't override endianness, it'll take it from the cstruct instance (like everything else)
  • If you override endianness, it'll be used for both the parsing of the pointer value and the dereferencing
  • You can additionally override on .dereference(endian=)
  • As with everything else, calling .dumps() will fall back to the cstruct instance endianness, unless overridden

@codecov
Copy link

codecov bot commented Feb 12, 2026

Codecov Report

❌ Patch coverage is 96.13527% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.83%. Comparing base (194b1b5) to head (8a6b8b6).

Files with missing lines Patch % Lines
dissect/cstruct/types/base.py 92.30% 3 Missing ⚠️
dissect/cstruct/cstruct.py 89.47% 2 Missing ⚠️
dissect/cstruct/utils.py 88.23% 2 Missing ⚠️
dissect/cstruct/types/pointer.py 97.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #144      +/-   ##
==========================================
- Coverage   93.01%   92.83%   -0.18%     
==========================================
  Files          21       21              
  Lines        2435     2472      +37     
==========================================
+ Hits         2265     2295      +30     
- Misses        170      177       +7     
Flag Coverage Δ
unittests 92.83% <96.13%> (-0.18%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Schamper Schamper force-pushed the add-endian-kwarg branch 2 times, most recently from 2f7314c to 15ab019 Compare February 13, 2026 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant