我的名字二元分析器。一个二进制数据分析器 [英] Name me a Binary Parser. A parser for binary data

查看:97
本文介绍了我的名字二元分析器。一个二进制数据分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我得到这个数据。从网络套接字或出文件。我拼凑code将inter preT的数据。阅读一些字节,检查一些标志,有的字节指示数据多少如下。阅读这么多数据,漂洗,重复。

So, I'm getting this data. From the network socket, or out of a file. I'm cobbling together code that will interpret the data. Read some bytes, check some flags, and some bytes indicate how much data follows. Read in that much data, rinse, repeat.

这个任务让我想起太多解析源$ C ​​$ C。我与舒适的lex / YACC和ANTLR,但他们达不到这个任务。您不能指定位和原始字节作为标记(好吧,也许你可以,但我不知道怎么做),你无法将它们哄读两个字节,使他们成为一个无符号整数16位,叫它的 N 的,然后读取的 N 的字节数。

This task reminds me much to parsing source code. I'm comfy with lex/yacc and antlr, but they're not up to this task. You can't specify bits and raw bytes as tokens (well, maybe you could, but I wouldn't know how), and you can't coax them into "read two bytes, make them into an unsigned 16bit integer, call it n, and then read n bytes.".

再然后,当协议/数据格式的规范以系统的方式(不是其中全部都是)的定义,应该有在根据所述协议格式的数据读取的系统方法。对?

Then again, when the spec of the protocol/data format is defined in a systematic manner (not all of them are), there should be a systematic way to read in data that is formatted according to the protocol. Right?

有得是一个工具,做到这一点。

There's gotta be a tool that does that.

推荐答案

您可以尝试采用 Boost.Spirit (V2),其中有最近得到<一个href=\"http://www.boost.org/doc/libs/release/libs/spirit/doc/html/spirit/qi/reference/binary.html\">binary解析工具的,字节序感知<一个href=\"http://www.boost.org/doc/libs/release/libs/spirit/doc/html/spirit/qi/reference/binary/binary_native.html\">native和<一个href=\"http://www.boost.org/doc/libs/release/libs/spirit/doc/html/spirit/qi/quick_reference/qi_parsers/binary.html\">mixed <一href=\"http://www.boost.org/doc/libs/release/libs/spirit/doc/html/spirit/qi/reference/binary/binary_native.html\">parsers

You may try to employ Boost.Spirit (v2) which has recently got binary parsing tools, endianness-aware native and mixed parsers

// This is not a complete and useful example, but just illustration that parsing
// of raw binary to real data components is possible
typedef boost::uint8_t byte_t;
byte_t raw[16] = { 0 };
char const* hex = "01010000005839B4C876BEF33F83C0CA";
my_custom_hex_to_bytes(hex, raw, 16);

// parse raw binary stream bytes to 4 separate words
boost::uint32_t word(0);
byte_t* beg = raw;
boost::spirit::qi::parse(beg, beg + 16, boost::spirit::qi::dword, word))

更新:我发现了类似的问题,其中乔尔·古斯曼证实了他的二进制解析器的答案可用性:<一href=\"http://stackoverflow.com/questions/285200/can-boost-spirit-be-used-to-parse-byte-stream-data\">Can提振精神用来解析字节流的数据?

这篇关于我的名字二元分析器。一个二进制数据分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆