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.
Each of the structures are 32-bits in length, and each structure can be identified by it’s binary signature.
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
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.
So as an example in pseudo-code:
if (binaryPattern = 000010xxxxxxxxx0) {
do something with it;
}
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
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.
For example, to check if your data matches the 0000 10xx 10xx xxx0 signature:
To illustrate with some sample data:
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.