Kaitai Struct:计算具有条件的实例

cho*_*x4f 5 python reverse-engineering data-structures kaitai-struct

我试图让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

        self._m_index_const = self._root.header.consts[(self.idx - 64)];
        return self._m_index_const
Run Code Online (Sandbox Code Playgroud)

它只是我,我错过了一些明显的东西,还是Kaitai Struct中的一个错误?

Gre*_*Cat 2

是的,我想这应该被视为一个错误。至少,编译器应该允许if在值实例中使用并正确处理它,或者禁止if并发出错误消息。

考虑到这一点,我不明白为什么if允许常规instances,但以这种方式对待value instances

感谢您的报告,我已经提交了一个问题


更新:该问题现已标记为已关闭。

if_instances现在测试测试一下。...关闭此问题已解决。