Kaitai Struct主动近来出现正好解决了这一任务:以产生从规范二进制解析器。您可以在一个基于JSON的YAML /格式一样,任意数据结构的序列化提供了一个方案:
meta:
id: my_struct
endian: le
seq:
- id: some_int
type: u4
- id: some_string
type: str
encoding: UTF-8
size: some_int + 4
- id: another_int
type: u4
编译使用ksc
(他们提供了一个参考编译器实现),以及,瞧,你有在任何支持的编程语言解析器,例如,在C++:
my_struct_t::my_struct_t(kaitai::kstream *p_io, kaitai::kstruct *p_parent, my_struct_t *p_root) : kaitai::kstruct(p_io) {
m__parent = p_parent;
m__root = this;
m_some_int = m__io->read_u4le();
m_some_string = m__io->read_str_byte_limit((some_int() + 4), "UTF-8");
m_another_int = m__io->read_u4le();
}
或Java中:
private void _parse() throws IOException {
this.someInt = this._io.readU4le();
this.someString = this._io.readStrByteLimit((someInt() + 4), "UTF-8");
this.anotherInt = this._io.readU4le();
}
还说到您的项目,它提供了后一个很直观的API一样,(在Java中的例子,但他们支持更多的语言):
// given file.dat contains 01 00 00 00|41 42 43 44|07 01 00 00
MyStruct s = MyStruct.fromFile("path/to/file.dat");
s.someString() // => "ABCD"
s.anotherInt() // => 263 = 0x107
它支持不同字节,有条件的结构,子等,以及更多。相当复杂的数据结构,如PNG image file format或PE executable可以被解析。
看起来很有希望。谢谢! – doppelfish