(whoops... the text should have read "Len is a big-endian length designator")
On Thu, Dec 16, 2010 at 9:24 AM, Joseph Stewart <[email protected]>wrote: > Adrian, > > Do you have any tips or small examples for dealing with lengths? While I > have something that works, it doesn't feel right and I don't have the > experience with Ragel to know if I'm doing this right. > > Below is a shell I've started to parse/validate binary chunks out of > XML-like tags (BTW, I did not create the format... I'm just the victim). > Eventually I need to parse data inside the Bin chunks. > > The graphviz output for this is so gigantic that I think I'm doing > something fundamentally wrong. > > Suggestions are greatly appreciated! > > -joe > > --- code follows --- > > #include <stdio.h> > > /* > > chunks in file look like > > <XRNDATA08192020001>LenData</XRNDATA> > where "08192" is an ASCII type designator (can expect other types) > where "020001" is an ASCII information field > where Len is a 4-byte little endian length designator > where Data is binary data of Len length > > */ > > #define XRN 'X', 'R', 'N', 'D', 'A', 'T', 'A' > #define T1 '0', '8', '1', '9', '2' > #define T2 '0', '0', '0', '1', '6' > #define T3 '0', '0', '0', '0', '8' > #define T4 '0', '0', '0', '0', '1' > #define I1 '0', '2', '0', '0', '0', '1' > > %%{ > machine tds; > write data; > }%% > > int main() { > int len = 0; > unsigned char buf[] = { > '<', XRN, T1, I1, '>', 0, 0, 0, 5, 48,49,50,51,52, '<', '/', XRN, '>', > '<', XRN, T2, I1, '>', 0, 0, 0, 1, 10, '<', '/', XRN, '>', > '<', XRN, T3, I1, '>', 0, 0, 0, 1, 10, '<', '/', XRN, '>', > '<', XRN, T4, I1, '>', 0, 0, 0, 1, 10, '<', '/', XRN, '>', > '<', XRN, T1, I1, '>', 0, 0, 0, 0, '<', '/', XRN, '>', > }; > int cs, r = 0; > unsigned char *p = (unsigned char *)&buf; > unsigned char *pe = p + sizeof(buf); > printf("len=%d\n", len); > %%{ > t1 = '08192' @{ printf("t1\n"); }; > t2 = '00016' @{ printf("t2\n"); }; > t3 = '00008' @{ printf("t3\n"); }; > t4 = '00001' @{ printf("t4\n"); }; > type = (t1 | t2 | t3 | t4); > info = '020001'; > open = '<XRNDATA' type info '>' @{ printf("begin XRNDATA\n"); len = 0; }; > len = extend{4} @{ printf("len = %d\n", (int)*p); len = (len*256) + (*p); > }; > buffer = (extend when {len--})* @{ printf("[%d]:%d\n", len, *p); }; > close = '</XRNDATA>' @{ printf("end XRNDATA\n"); }; > expr = open len buffer close; > main := expr*; > write init; > write exec; > }%% > return 0; > } > > > > On Mon, Dec 6, 2010 at 1:16 PM, Adrian Thurston < > [email protected]> wrote: > >> I use ragel to parse binary protocols at work. Often, the most difficult >> part is dealing with lengths, or counts of things. A good example is the DNS >> protocol. There are several sections of N blocks of questions and resource >> records items. Inside the blocks you must also deal with lengths in names. >> >> Unfortunately, there are not many open examples of parsing this way. Start >> by looking in the manual. I would like to improve Ragel's support for this >> kind of parsing. >> >> -Adrian >> >> >> On 10-12-03 02:53 AM, Vitaly V. Ch wrote: >> >>> Hi! >>> >>> I'm interested in any tips or examples of using ragel on binary >>> datagrams. >>> >>> \\wbr Vitaly Chernooky >>> >>> _______________________________________________ >>> ragel-users mailing list >>> [email protected] >>> http://www.complang.org/mailman/listinfo/ragel-users >>> >>> >> _______________________________________________ >> ragel-users mailing list >> [email protected] >> http://www.complang.org/mailman/listinfo/ragel-users >> > >
_______________________________________________ ragel-users mailing list [email protected] http://www.complang.org/mailman/listinfo/ragel-users
