Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary streams parsing library for C [closed]

Tags:

c

parsing

Could you advise a proven binary stream parsing library for C? It will be nice if it will be as declarative as C allows.

I need it for binary protocol parsing and describing the quite complicated protocol by it's formal specification, that's why I want it as much declarative as possible. Something like Haskell's Data.Binary.Get will be great.

I've found binpac, but it seems heavyweight and oriented for C++ , but the project is pure C.

Example describing what I mean:

parser_t parsers[] = {
  { get_bit,      out_field1, &pkt.field1 }
 ,{ get_bit7,     0,           0          } // skip 7 bits
 ,{ get_word16be, out_field2, &pkt.field2 }
 ,{ 0,            0,           0          } // end
};

// skip
// char *data, char *data_end, 
map_parsers(data, data_end, parsers); // parse stream

UPD. It's fine to have just a fast library for sequential reading bits, words, aligned, unaligned, etc from memory. I may write declarative frontend or even DSL on my own.

like image 983
voidlizard Avatar asked Feb 04 '26 08:02

voidlizard


1 Answers

There's PADS from the nice people at AT&T. There's also RAGEL which can be used for the same thing. I've not used either myself, but found hints of their existence on this page.

PADS looks like a similar idea to the ASN1 compiler from Objective Systems and Google Protocol buffers; you write a schema file, then compile that to the source code language of your choice. The difference of course is that those two work for strictly defined binary encoding schemes whereas PADS seemingly works for arbitrary data streams.

Good luck!

like image 119
bazza Avatar answered Feb 06 '26 20:02

bazza