jam1garner/binrw

Bit-level support?

DerZade opened this issue · 3 comments

I am looking for a library to read / write data with many fields that are only a few bits (not bytes) long. For example, I have 12-bit wide integer fields and therefore many of the fields are not even byte-aligned.

As far as I can tell that is not really possible with binrw atm. Am I correct in this assumption?

Will that be something that will be supported in the (near) future?

The modular-bitfield crate sounds like it might satisfy what you need here, and it can be integrated with binrw, there's an example of this in the documentation.

Thought that only really works with byte aligned bit-fields 🤔 (see here), but I will try 🙈

PS: The issue with Deku for me is that it does not support Read / Write.

This is an old issue but figured I would mention some thoughts for anyone else in future.

bitstreams really are not the best fit for binrw but you can probably make it work with a stream wrapper.

If the objects are always fixed-size but are just not byte-aligned, you could write a generic stream wrapper that tracks the position and injects extra bits at the end of each object to fake alignment to a byte boundary.

If the objects are not fixed-size, you could signal when an object has finished parsing with a marker field that sends some signal back to the stream wrapper to inject the extra bits.

Neither of these are likely to be particularly fast but maybe that is OK for your use case. Otherwise you would need to write a stream buffer that understands the context of what is currently being parsed, at which point you are just hand-writing a parser anyway and binrw isn’t useful.

If someone does decide to make a generic stream solution for this, submitting it as a PR would be cool.