匹配用C二进制模式 [英] Matching binary patterns in C

查看:362
本文介绍了匹配用C二进制模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发需要解析某些定制的数据结构,C程序,幸运的是我知道他们是如何构建的,但是我不知道如何实现我用C解析器。

I'm currently developing a C program that needs to parse some bespoke data structures, fortunately I know how they are structured, however I'm not sure of how to implement my parser in C.

每个结构体在长度32位,并且每个结构可以被识别由它的二进制签名

Each of the structures are 32-bits in length, and each structure can be identified by it's binary signature.

作为一个例子,有我感兴趣的两个特定的结构,并且它们具有以下二进制模式(x表示0或1)

As an example, there are two particular structures that I'm interested in, and they have the following binary patterns (x means either 0 or 1)

 0000-00xx-xxxx-xxx0
 0000-10xx-10xx-xxx0

在这些结构中的x位包含实际的数据,我需要,所以基本上我需要识别基于所述位如何每个结构中写入的每个结构的一种方式。

Within these structures the 'x' bits contain the actual data I need, so essentially I need a way of identifying each structure based on how the bits are written within each structure.

,以便在伪code的例子:

So as an example in pseudo-code:

if (binaryPattern = 000010xxxxxxxxx0) {
do something with it;
}

我猜读他们为整数,然后执行某种bitmasking将是要走的路,但我的C知识不是很大,也许一个简单的逻辑或将做到这一点,但我只是这样做,我才开始想一些建议。

I'm guessing that reading them as ints, and then performing some kind of bitmasking would be the way to go, but my knowledge of C isn't great, and maybe a simple logical OR operation would do it, but I just wanted some advice on doing this before I start.

感谢

非常感谢大家,已经回答了,非常有帮助!

Thanks very much to everyone that has answered, very helpful!!

推荐答案

要检查你的数据的一个特定的二进制模式匹配,可以先屏蔽掉非签名位,那么它比较的一个特征模板。

To check if your data matches a specific binary pattern, you can first mask out the non-signature bits, then compare it against a signature template.

例如,要检查如果你的数据在0000 10XX 10XX XXX0签名匹配:

For example, to check if your data matches the 0000 10xx 10xx xxx0 signature:


  1. 并与1111 1100 1100 0001输入数据(面具)

  2. 检查输出等于0000 1000 1000 0000(模板)

要说明一些示例数据:

DATA_1   0010 1011 1101 1100                DATA_2   0000 1011 1010 1100
  MASK   1111 1100 1100 0001  &               MASK   1111 1100 1100 0001  &
        --------------------                        --------------------
         0010 1000 1100 0000 (NO_MATCH)              0000 1000 1000 0000 (MATCH)
        --------------------                        --------------------

你每一项规则因此可以重新通过掩模模板对psented $ P $和所有你需要的是一个功能/操作适用上面的操作你的数据,以检查它是否匹配。

Each of you rules could therefore be represented by a mask-template pair and all you need is a function/operation that applies the above operation to your data to check if it is a match.

这篇关于匹配用C二进制模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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