Kaitai Struct: design instances with the condition

I am trying to get Kaitai Struct to reverse engineer the binary structure. The seq fields work as intended, but the instances don't seem to work the way I want them to.

My binary format includes a header with a list of constants, which I parse as header with a subfield of the consts array:

 types: header: seq: # ... - id: consts type: u8 repeat: expr repeat-expr: 0x10 

However, when I try to use the following declaration:

 instances: index_const: value: '_root.header.consts[idx - 0x40]' if: idx >= 0x40 and idx <= 0x4f 

This method is designed to calculate the value of index_const by searching the header.consts array if and only if idx is in the range from [0x40..0x4f].

I use Python as my target language, and I assume that it should generate code, for example:

  @property def index_const(self): if hasattr(self, '_m_index_const'): return self._m_index_const if self.idx >= 64 and self.idx <= 79: self._m_index_const = self._root.header.consts[(self.idx - 64)]; return self._m_index_const 

However, I get:

  @property def index_const(self): if hasattr(self, '_m_index_const'): return self._m_index_const self._m_index_const = self._root.header.consts[(self.idx - 64)]; return self._m_index_const 

Is it just me, will I miss something obvious or is it a mistake in Kaitai Struct?

+5
source share
1 answer

Yes, I think this should be considered a mistake. At the very least, the compiler must either allow the use of if in value instances, or handle it properly, or disable if and issue an error message.

Thinking about this, I see no reason why if allowed for regular instances , but is handled this way for value instances .

Thanks for reporting, I submitted a problem .

+2
source

All Articles