我试图让Kaitai Struct对二进制结构进行逆向工程.seq字段按预期工作,但instances似乎不像我希望的那样工作.
我的二进制格式包括一个标题,其中包含一些常量列表,我将其解析为header带有consts数组子字段的字段:
types:
header:
seq:
# ...
- id: consts
type: u8
repeat: expr
repeat-expr: 0x10
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用以下声明时:
instances:
index_const:
value: '_root.header.consts[idx - 0x40]'
if: idx >= 0x40 and idx <= 0x4f
Run Code Online (Sandbox Code Playgroud)
这个用于index_const通过查找header.constsif和仅idx在[0x40..0x4f]范围内的数组来计算值.
我使用Python作为我的目标语言,我认为它应该生成如下代码:
@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
Run Code Online (Sandbox Code Playgroud)
但是,我得到的是:
@property
def index_const(self):
if hasattr(self, '_m_index_const'):
return self._m_index_const …Run Code Online (Sandbox Code Playgroud)