sharksforarms / deku

Declarative binary reading and writing: bit-level, symmetric, serialization/deserialization
Apache License 2.0
1.14k stars 55 forks source link

Seek bits #482

Open vhdirk opened 1 month ago

vhdirk commented 1 month ago

Given the recent addition of seek_from_*, I was wondering if there are plans to add seek_bits_from_*, too.?

I have this weird protocol that looks somewhat like this:

struct Operation {
    header: Header, #2 bits

    #[deku(bits="6")]
    opCode: u8,

    operand: Operand # any number of bytes
}

As you would assume, opCode + operand could be just an enum. However, for some if not most operands, the header isn't so much a header but rather an integral part of the operand itself. I know, it's a weird format.

So, it'd be very cool if there was a way to define that the enum id location is input[2..8], and the operand is actually ( input[0..2], 6 unused bits, input[9...] )

Do you have any ideas on how this could be implemented?

wcampbell0x2a commented 1 month ago

I've solved that before by sending in an already read header into the operand, but it sounds like that's not really a solution since it's doesn't interpret the header the second time as just being two bits.

  1. We don't have bit_order support, yet. But that first u8 looks like Lsb Order . https://github.com/sharksforarms/deku/pull/468. I've written other instructions set decoders that I had to do this for, wouldn't surprise me.
  2. I'd have to play around with the idea of seek_bits! I think it's possible as long as it's no bigger then the last read.
vhdirk commented 1 month ago

Ah, I indeed thought I had brought this up before, but it's been a while and I couldn't find it anymore :) I'll search some more