Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use instances: in a loop, where each iteration has a different size? #1079

Open
jeansanch opened this issue Jan 12, 2024 · 1 comment
Labels

Comments

@jeansanch
Copy link

jeansanch commented Jan 12, 2024

How can I get the pos of an item inside a loop of blocks that have different sizes?

I've tried doing

seq:
  - id: skip_init
    size: 280060
  - id: run_data
    type: data
    repeat: expr 
    repeat-expr: 2 # just to test
instances:
  names:
    io: run_data[_index]._io
    pos: _io.pos
    size: run_data[_index].name_size

I have tried to add instances with a similar logic inside of the run_data block, but it would always point to the same block of data.

I'm doing this because I've a structure that looks something like this.

name[name_size]
zeroes[2] (that will only appear if (name_size / 2) % 2 == 1) + data[8]
name2[name_size]
zeroes[Value also based on the string size]
data[400]
name_size[2]
data[16]

so I have to pass through the "name" variable first, then I'm able to get its value (name_size).

Still I've managed to parse each block using another logic, that doesn't allow me to filter the "name" , that's why I was using instances for "post-processing"

What I would like to do is something like this.

seq:
  - id: skip_init
    size: 280060
  - id: run_data
    type: data
    repeat: expr 
    repeat-expr: 2 # just to test
instances:
  names:
    pos: Start of the block at each index
    size: name_size variable of each index

Am I taking the best approach for this? I've read the documentation, but I was not able to get an answer for this specific case.

@jeansanch jeansanch changed the title How to use instances: in a loop, where each iteration has a dynamic size? How to use instances: in a loop, where each iteration has a different size? Jan 12, 2024
@jeansanch
Copy link
Author

jeansanch commented Jan 24, 2024

I would like to add that the code referenced at http://doc.kaitai.io/user_guide.html#_relative_positioning is not possible to do here, because I don't have a fixed size as in "some_block.size" in the code on the example "Good (w/substream)" and I'm only able to get the block size, at the end of the block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants