-
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
Question on switch functionality #207
Comments
It's not really related to switch, rather than it is an issue of alignment (see #12). Right now we've implemented a very simple alignment scheme, that is everything is byte-aligned, except for sequences of types that are known to require bit-level alignment (that is basically seq:
- id: one
type: b1
- id: two
type: b7 that compiles into m_one = m__io->read_bits_int(1);
m_two = m__io->read_bits_int(7); but as soon as you'll interject these seq:
- id: one
type: b1
- id: cccombo_breaker
type: u1
- id: two
type: b7 compiles into: m_one = m__io->read_bits_int(1);
m__io->align_to_byte(); // <================ this one!
m_cccombo_breaker = m__io->read_u1();
m_two = m__io->read_bits_int(7); As you might have guessed, both "switch types" and "user types" fall into "other stuff" category, i.e. they would invoke byte alignment statement. So, we need some sort of flexible alignment specification system that would at least say which types / attributes require which alignment statements before / after them. So, I suggest to continue this discussion in #12, if it's relevant. |
Happy to discuss elsewhere, but that discussion mainly relates to specifying ways to force certain types off byte alignment, while I am having a bit-alignment issue. Even if the If the idea is to -in the end - replace the current 'automatic' alignment by 'user-defined' alignment and supporting a 'no-alignment' case would become possible that would be great. While these issues are not orthogonal, I am not convinced that the existing discussion is the right place to continue. Thoughts? |
Could you show me how would you parse those 160 bits? As 20 bytes? So basically it's a data format which stores every byte shifted by 4 bits?
|
Yeah, you're completely right, it's not a very trivial issue. My idea is actually this and that issue have a lot in common. Basically, the difference in generated code should be insertion/abscence of certain
That, and, of course, bit-level reading primitives. So my thought was to make it look the same for end-user. |
Thanks for that - indeed, I agree that this approach addresses both issues. @tamas: the 160 bits contain a variety of bit-fields. Essentially, the data structure in my example is 172 bits of which the first 12 bits contain the ID which determines which of a number of message formats to select. This requires me to treat the 12 bits ID separately as I can't simply parse them as part of each of the messages (hence my reference to the |
I asked this because the byte arrays are usually byte-aligned, so maybe we don't need a EditSo if we look at this example: meta:
id: testalign
seq:
- id: preamble
type: b12
- id: data
type:
switch-on: preamble
cases:
0x504: type1
types:
type1:
seq:
- id: field1
type: b4
- id: field2
type: b8 Currently this generates the following code ( If I comment the If my input is |
Thanks Tamas, I understand what you are aiming at and thanks for showing me how to achieve it. |
@JaapAap There is an initiative to migrate from these intermediate byte arrays to "asking for substreams" directly in #44. But this still needs that bit-level support anyway. Even if instead of: m__raw_foo = m__io->read_bytes(4);
m__io__raw_foo = new kaitai::kstream(m__raw_foo);
m_foo = new foo_t(m__io__raw_foo, this, m__root); we'll be doing something like: m__io__raw_foo = m__io->read_substream(4);
m_foo = new foo_t(m__io__raw_foo, this, m__root); ... that |
Yes, currently the intermediate byte array is not just can be avoided, but must be avoided AFAIK as you cannot create a reader stream from a byte array (without additional non-ksy code) - related issue: #44. Of course you could parse everything as a byte array and extract various bits via expressions and instances, but that way you would lose the declarative capabilities of Kaitai, so I would not recommend that method. |
So, generally, it's not a question of "shall we do unaligned reads or not" — the question is a priority of that task. And, yeah, unfortunately, given that 99% of users probably can live without it, it's somewhat around mid-low. |
I am just saying that I have a feeling that the current issue can be solved without unaligned reads (except the My biggest fear is that we want to do something which can be architecturally bad. So before going that way I want to be sure I understand the problem correctly. For example let's say we need an aligned sub-stream: that implementation probably shouldn't copy the input byte array by shifting every byte or something like that. It just 'simply' have to clone the current bit alignment (eg. I never met a file format which used eg. bit-aligned byte arrays. For bit-aligned integers, the current |
I'll close this issue as is seems to be resolved or rejected. Please reopen if you feel this is wrong. |
Suppose I have a set of messages that contain a 12 bit preamble with the message ID followed by a message body of 160 bits, with different contents for different messages. Can the current switch functionality support such a case, in which the message body does not start on a byte-boundary?
The text was updated successfully, but these errors were encountered: