Kaitai Struct:带条件的计算实例

Kaitai Struct: calculated instances with a condition

我正在尝试让 Kaitai Struct 对二进制结构进行逆向工程。 seq 字段按预期工作,但 instances 似乎不像我希望的那样工作。

我的二进制格式包括一个 header 和一个常量列表,我将其解析为 header 字段和 consts 数组子字段:

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

但是,当我尝试使用以下声明时:

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

当且仅当 idx 在 [0x40..0x4f] 的范围内时,此方法旨在通过查找 header.consts 的数组来计算 index_const 的值。

我使用 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

然而,我得到的是:

    @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

只有我一个人,是我遗漏了一些明显的东西,还是 Kaitai Struct 中的错误?

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

考虑到这一点,我看不出为什么 if 可以用于常规 instances,但可以这样对待 value instances

感谢您的报告,我已提交 an issue


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

if_instances test now tests that. ... Closing this one as solved.