gpt4 book ai didi

python - 开泰结构 : calculated instances with a condition

转载 作者:太空狗 更新时间:2023-10-30 01:27:34 28 4
gpt4 key购买 nike

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

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

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] 的范围。

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

关于python - 开泰结构 : calculated instances with a condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38334665/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com