命名我为二进制解析器.二进制数据解析器 [英] Name me a Binary Parser. A parser for binary data

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

问题描述

所以,我正在获取这些数据.从网络套接字,或者从一个文件中.我正在拼凑可以解释数据的代码.读取一些字节,检查一些标志,一些字节表示后面有多少数据.读入那么多数据,冲洗,重复.

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.

这个任务让我想起了解析源代码.我对 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) 最近获得了 二进制解析工具,字节序感知native混合 解析器

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))

更新:我发现了类似的问题,Joel de Guzman 在他的回答中确认了二进制解析器的可用性:Boost Spirit可以解析字节流数据吗?

UPDATE: I found similar question, where Joel de Guzman confirms in his answer availability of binary parsers: Can Boost Spirit be used to parse byte stream data?

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

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