-
Notifications
You must be signed in to change notification settings - Fork 201
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
repeat-expr can't use fields defined in instances #298
Comments
Thus, stuff like that instances:
- id: num_floats
type: u4 does not make much sense anyway. It does not need order, it does not need instances:
num_floats:
pos: 0x100
type: u4 If you'll take a closer look at the error message, it actually tells you exactly that: |
I see. Thanks! What I was originally looking for is a "lazy" sequence. One which it's values are only retrieved and parsed when accessed. I assumed 'instances' is the closest thing I can get. So, is it possible to make a 'seq' lazy? Thanks for your help. |
There is an issue for lazy sequence attributes — kaitai-io/kaitai_struct_compiler#133 — but don't hold your breath for it. Conceptually, it's somewhat hard, as there are many things to consider. For example, consider this: seq:
- id: a
type: user_type_a
- id: b
type: user_type_b
lazy: true
- id: c
type: user_type_c It is probably impossible to implement, as we don't know size of "user_type_b" before parsing just to skip that amount of bytes to eagerly parse only There are a couple of hacks to achieve that with instances, but note that they're all really hacks :( For example, you can actually exploit the way that instances are called and do something like that: seq:
- id: num_floats
type: u4
- id: placeholder
size: 8 * num_floats
if: save_pos1 >= 0
instances:
save_pos1:
value: _io.pos
floats:
pos: save_pos1
repeat: expr
repeat-expr: num_floats
type: u8 What happens here is that before parsing |
No, but here is a feature request about this: #133
Not in a supported way, but you can still use Try this: meta:
id: lazy_instance
seq:
- id: field1
type: u2le
instances:
field2:
type: u2le
pos: _io.pos Of course if you read the fields in the wrong order, or you want to use more than 1 instance field then the situation gets more complex. (You can use your 1 instance option to read not just a
We usually recommend to split your type which contains the lazy field into multiple subtypes and control the parsing logic from your program code. This way you have to write more code manually, but Kaitai still helps you to parse the binary structures, so you can make your code more flexible at the cost of a little more hand-written code. |
The default position of instance's field is meaningless as of now (I mean, it just reads wherever the stream is). Perhaps it would be wise to print an error or throw an exception or something. (The way I wish it would to be is right after the previous field i.e. previous field position + previous field size which can be either resolved in compile time if possible or deferred to runtime. But I can see it's a very complex feature to implement.) Just to complete the picture: I am parsing a remote resource thus read of every byte is an expensive operation (I'm using an underlying IO implementation for caching). I can't seem to find a nice way to do it with any existing solution :/ Thanks for your help guys, you are running a really awesome project. |
Extracted as separate issue #544 Closing this one, as discussion seems to have reached its point. |
Moving issue from #1204, orignally by @yannayl
Trying to compile this .ksy file:
yields the following error:
This file is a copy-paste from the examples replacing a
seq
withinstances
.I'm not sure if it is related to issue #512 or not.
The text was updated successfully, but these errors were encountered: