jam1garner / binrw

A Rust crate for helping parse and rebuild binary data using ✨macro magic✨.
https://binrw.rs
MIT License
585 stars 35 forks source link

Need way for things using helpers::count to know their index #190

Closed Gorialis closed 1 year ago

Gorialis commented 1 year ago

I have a format that looks basically like this:

#[binrw]
#[br(import(item_size: u32))]
struct Block {
   ... // some fields
   example: u8,
   #[br(parse_with=count(item_size as usize))]
   content: Vec<u8>,
}

#[binrw]
struct Something {
    item_count: u32,
    #[br(parse_with=count(item_count as usize))]
    sizes: Vec<u32>,
    // problem is here
    #[br(parse_with=count(item_count as usize))]
    blocks: Vec<Block>,
}

Each Block is of variable size, their sizes determined by their fields + a Vec<u8> block whose size is determined by the item in the previous sizes Vec. (that is, blocks[3] has a content of size sizes[3]).

While I can get args to Vec, or get args to be passed to all elements (using VecArgs::inner), I specifically need Block to be able to either:

I can't really use seek tricks because the blocks are end-to-end and thus their offsets become cumulative (the offset of blocks[5] would require me to sum the previous sizes), and because some of the fields of Block may be optionally not there (so the size of the other fields may also vary if a field is omitted).

At the moment the only real thing I can think of is writing a custom parser, but it feels like there should be an easier way to deal with transposed tables like this.

Gorialis commented 1 year ago

Woops, fat fingered enter and submitted this before typing up my content, give me a moment to fill this out Done writing now, thanks GitHub.