Thanks. I'll be keeping an eye out for this and try to contribute if I come up with any compelling ideas.<div><br></div><div>In the meantime, let me drone about my current state machine... looking at the output from graphviz, it looks like the complexity is greater in the graph output than what I expected (indeed I'm trying to sell my team on the ragel approach, with one metric being that the state machines are understandable via the graphviz output). This makes me think I've specified my machine poorly.</div>
<div><br></div><div>At a high-level my data looks like:</div><div><br></div><div>open xml-like tag with parameters type and info</div><div>length</div><div>binary blog of length bytes</div><div>close xml-like tag<br><br></div>
<div>I'm hoping someone can increase my wisdom on matters of this sort.</div><div><br></div><div>BR,</div><div><br></div><div>-joe</div><div><br><div class="gmail_quote">On Thu, Dec 16, 2010 at 1:34 PM, Adrian Thurston <span dir="ltr"><<a href="mailto:adrian.thurston@esentire.com">adrian.thurston@esentire.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Right now there is no 'right' way. There are actually are many different ways each with their own ups and downs. There is some work I want to do on conditions to ease length processing (comes up at work a lot) and I'm hoping to soon be able to prescribe a recommended way of dealing with lengths that is general enough to apply to the majority of cases that I see.<div>
<div></div><div class="h5"><br>
<br>
On 10-12-16 06:24 AM, Joseph Stewart wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div class="h5">
Adrian,<br>
<br>
Do you have any tips or small examples for dealing with lengths? While I<br>
have something that works, it doesn't feel right and I don't have the<br>
experience with Ragel to know if I'm doing this right.<br>
<br>
Below is a shell I've started to parse/validate binary chunks out of<br>
XML-like tags (BTW, I did not create the format... I'm just the victim).<br>
Eventually I need to parse data inside the Bin chunks.<br>
<br>
The graphviz output for this is so gigantic that I think I'm doing<br>
something fundamentally wrong.<br>
<br>
Suggestions are greatly appreciated!<br>
<br>
-joe<br>
<br>
--- code follows ---<br>
<br>
#include <stdio.h><br>
<br>
/*<br>
<br>
chunks in file look like<br>
<br>
<XRNDATA08192020001>LenData</XRNDATA><br>
where "08192" is an ASCII type designator (can expect other types)<br>
where "020001" is an ASCII information field<br>
where Len is a 4-byte little endian length designator<br>
where Data is binary data of Len length<br>
<br>
*/<br>
<br>
#define XRN 'X', 'R', 'N', 'D', 'A', 'T', 'A'<br>
#define T1 '0', '8', '1', '9', '2'<br>
#define T2 '0', '0', '0', '1', '6'<br>
#define T3 '0', '0', '0', '0', '8'<br>
#define T4 '0', '0', '0', '0', '1'<br>
#define I1 '0', '2', '0', '0', '0', '1'<br>
<br>
%%{<br>
machine tds;<br>
write data;<br>
}%%<br>
<br>
int main() {<br>
int len = 0;<br>
unsigned char buf[] = {<br>
'<', XRN, T1, I1, '>', 0, 0, 0, 5, 48,49,50,51,52, '<', '/', XRN, '>',<br>
'<', XRN, T2, I1, '>', 0, 0, 0, 1, 10, '<', '/', XRN, '>',<br>
'<', XRN, T3, I1, '>', 0, 0, 0, 1, 10, '<', '/', XRN, '>',<br>
'<', XRN, T4, I1, '>', 0, 0, 0, 1, 10, '<', '/', XRN, '>',<br>
'<', XRN, T1, I1, '>', 0, 0, 0, 0, '<', '/', XRN, '>',<br>
};<br>
int cs, r = 0;<br>
unsigned char *p = (unsigned char *)&buf;<br>
unsigned char *pe = p + sizeof(buf);<br>
printf("len=%d\n", len);<br>
%%{<br>
t1 = '08192' @{ printf("t1\n"); };<br>
t2 = '00016' @{ printf("t2\n"); };<br>
t3 = '00008' @{ printf("t3\n"); };<br>
t4 = '00001' @{ printf("t4\n"); };<br>
type = (t1 | t2 | t3 | t4);<br>
info = '020001';<br>
open = '<XRNDATA' type info '>' @{ printf("begin XRNDATA\n"); len = 0; };<br>
len = extend{4} @{ printf("len = %d\n", (int)*p); len = (len*256) + (*p); };<br>
buffer = (extend when {len--})* @{ printf("[%d]:%d\n", len, *p); };<br>
close = '</XRNDATA>' @{ printf("end XRNDATA\n"); };<br>
expr = open len buffer close;<br>
main := expr*;<br>
write init;<br>
write exec;<br>
}%%<br>
return 0;<br>
}<br>
<br>
<br>
<br>
On Mon, Dec 6, 2010 at 1:16 PM, Adrian Thurston<br></div></div><div class="im">
<<a href="mailto:adrian.thurston@esentire.com" target="_blank">adrian.thurston@esentire.com</a> <mailto:<a href="mailto:adrian.thurston@esentire.com" target="_blank">adrian.thurston@esentire.com</a>>> wrote:<br>
<br>
I use ragel to parse binary protocols at work. Often, the most<br>
difficult part is dealing with lengths, or counts of things. A good<br>
example is the DNS protocol. There are several sections of N blocks<br>
of questions and resource records items. Inside the blocks you must<br>
also deal with lengths in names.<br>
<br>
Unfortunately, there are not many open examples of parsing this way.<br>
Start by looking in the manual. I would like to improve Ragel's<br>
support for this kind of parsing.<br>
<br>
-Adrian<br>
<br>
<br>
On 10-12-03 02:53 AM, Vitaly V. Ch wrote:<br>
<br>
Hi!<br>
<br>
I'm interested in any tips or examples of using ragel on binary<br>
datagrams.<br>
<br>
\\wbr Vitaly Chernooky<br>
<br>
_______________________________________________<br>
ragel-users mailing list<br></div>
<a href="mailto:ragel-users@complang.org" target="_blank">ragel-users@complang.org</a> <mailto:<a href="mailto:ragel-users@complang.org" target="_blank">ragel-users@complang.org</a>><div class="im"><br>
<a href="http://www.complang.org/mailman/listinfo/ragel-users" target="_blank">http://www.complang.org/mailman/listinfo/ragel-users</a><br>
<br>
<br>
_______________________________________________<br>
ragel-users mailing list<br></div>
<a href="mailto:ragel-users@complang.org" target="_blank">ragel-users@complang.org</a> <mailto:<a href="mailto:ragel-users@complang.org" target="_blank">ragel-users@complang.org</a>><div class="im"><br>
<a href="http://www.complang.org/mailman/listinfo/ragel-users" target="_blank">http://www.complang.org/mailman/listinfo/ragel-users</a><br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
ragel-users mailing list<br>
<a href="mailto:ragel-users@complang.org" target="_blank">ragel-users@complang.org</a><br>
<a href="http://www.complang.org/mailman/listinfo/ragel-users" target="_blank">http://www.complang.org/mailman/listinfo/ragel-users</a><br>
</div></blockquote><div><div></div><div class="h5">
<br>
_______________________________________________<br>
ragel-users mailing list<br>
<a href="mailto:ragel-users@complang.org" target="_blank">ragel-users@complang.org</a><br>
<a href="http://www.complang.org/mailman/listinfo/ragel-users" target="_blank">http://www.complang.org/mailman/listinfo/ragel-users</a><br>
</div></div></blockquote></div><br></div>