clj-commons / gloss

speaks in bytes, so you don't have to
Eclipse Public License 1.0
483 stars 57 forks source link

Non byte-aligned codecs #39

Closed hadronzoo closed 8 years ago

hadronzoo commented 8 years ago

Is there a way to handle codecs which are not byte-aligned? I'm trying to read a format which represents a vector using a 1-bit header before each value. The header determines whether there is another value to read.

ztellman commented 8 years ago

Gloss, in its current form, isn't too great at these, but you can do it by defining a byte header, examining the bit, and defining the body according to what the bit tells you.

hadronzoo commented 8 years ago

Wouldn't the header consume the entire byte? Unfortunately, nothing is aligned to byte boundaries, so one would need to specify codecs lengths in bits rather than bytes. It makes sense that gloss wouldn't handle these non-byte aligned formats, since they're rare and seem to be inefficient to parse.

ztellman commented 8 years ago

The byte formats I'm familiar with that resemble this are variable-length integers, which use a bit in the current byte to indicate whether the next byte should be read in. That would work okay with the approach I suggest, something where a bit indicates that the byte that contains it shouldn't be read in would be difficult to handle.