(whoops... the text should have read "Len is a big-endian length designator")<br><br><div class="gmail_quote">On Thu, Dec 16, 2010 at 9:24 AM, Joseph Stewart <span dir="ltr"><<a href="mailto:joseph.stewart@gmail.com">joseph.stewart@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Adrian,<div><br></div><div>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.</div>
<div><br></div><div>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.</div>
<div><br></div><div>The graphviz output for this is so gigantic that I think I'm doing something fundamentally wrong.</div><div><br></div><div>Suggestions are greatly appreciated!</div><div><br></div><div>-joe</div><div>
<br></div><div>--- code follows ---</div><div><br></div><div><div>#include <stdio.h></div><div><br></div><div>/*</div><div><br></div><div>chunks in file look like</div><div><br></div><div><XRNDATA08192020001>LenData</XRNDATA></div>
<div>where "08192" is an ASCII type designator (can expect other types)</div><div>where "020001" is an ASCII information field</div><div>where Len is a 4-byte little endian length designator</div><div>
where Data is binary data of Len length</div><div><br></div><div>*/</div><div><br></div><div>#define XRN 'X', 'R', 'N', 'D', 'A', 'T', 'A'</div><div>#define T1 '0', '8', '1', '9', '2'</div>
<div>#define T2 '0', '0', '0', '1', '6'</div><div>#define T3 '0', '0', '0', '0', '8'</div><div>#define T4 '0', '0', '0', '0', '1'</div>
<div>#define I1 '0', '2', '0', '0', '0', '1'</div><div><br></div><div>%%{</div><div><span style="white-space:pre-wrap"> </span>machine tds;</div><div><span style="white-space:pre-wrap"> </span>write data;</div>
<div>}%%</div><div><br></div><div>int main() {</div><div><span style="white-space:pre-wrap"> </span>int len = 0;</div><div><span style="white-space:pre-wrap"> </span>unsigned char buf[] = {</div>
<div><span style="white-space:pre-wrap"> </span>'<', XRN, T1, I1, '>', 0, 0, 0, 5, 48,49,50,51,52, '<', '/', XRN, '>',</div><div><span style="white-space:pre-wrap"> </span>'<', XRN, T2, I1, '>', 0, 0, 0, 1, 10, '<', '/', XRN, '>',</div>
<div><span style="white-space:pre-wrap"> </span>'<', XRN, T3, I1, '>', 0, 0, 0, 1, 10, '<', '/', XRN, '>',</div><div><span style="white-space:pre-wrap"> </span>'<', XRN, T4, I1, '>', 0, 0, 0, 1, 10, '<', '/', XRN, '>',</div>
<div><span style="white-space:pre-wrap"> </span>'<', XRN, T1, I1, '>', 0, 0, 0, 0, '<', '/', XRN, '>',</div><div><span style="white-space:pre-wrap"> </span>};</div>
<div><span style="white-space:pre-wrap"> </span>int cs, r = 0;</div><div><span style="white-space:pre-wrap"> </span>unsigned char *p = (unsigned char *)&buf;</div><div><span style="white-space:pre-wrap"> </span>unsigned char *pe = p + sizeof(buf);</div>
<div><span style="white-space:pre-wrap"> </span>printf("len=%d\n", len);</div><div><span style="white-space:pre-wrap"> </span>%%{</div><div><span style="white-space:pre-wrap"> </span>t1 = '08192' @{ printf("t1\n"); };</div>
<div><span style="white-space:pre-wrap"> </span>t2 = '00016' @{ printf("t2\n"); };</div><div><span style="white-space:pre-wrap"> </span>t3 = '00008' @{ printf("t3\n"); };</div>
<div><span style="white-space:pre-wrap"> </span>t4 = '00001' @{ printf("t4\n"); };</div><div><span style="white-space:pre-wrap"> </span>type = (t1 | t2 | t3 | t4);</div>
<div><span style="white-space:pre-wrap"> </span>info = '020001';</div><div><span style="white-space:pre-wrap"> </span>open = '<XRNDATA' type info '>' @{ printf("begin XRNDATA\n"); len = 0; };</div>
<div><span style="white-space:pre-wrap"> </span>len = extend{4} @{ printf("len = %d\n", (int)*p); len = (len*256) + (*p); };</div><div><span style="white-space:pre-wrap"> </span>buffer = (extend when {len--})* @{ printf("[%d]:%d\n", len, *p); };</div>
<div><span style="white-space:pre-wrap"> </span>close = '</XRNDATA>' @{ printf("end XRNDATA\n"); };</div><div><span style="white-space:pre-wrap"> </span>expr = open len buffer close;</div>
<div><span style="white-space:pre-wrap"> </span>main := expr*;</div><div><span style="white-space:pre-wrap"> </span>write init;</div><div><span style="white-space:pre-wrap"> </span>write exec;</div>
<div><span style="white-space:pre-wrap"> </span>}%%</div><div><span style="white-space:pre-wrap"> </span>return 0;</div><div>}</div><div><span style="white-space:pre-wrap"> </span></div>
</div><div><div></div><div class="h5"><div><span style="white-space:pre-wrap"><br></span></div><div><br><br><div class="gmail_quote">On Mon, Dec 6, 2010 at 1:16 PM, Adrian Thurston <span dir="ltr"><<a href="mailto:adrian.thurston@esentire.com" target="_blank">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">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.<br>
<br>
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.<br><font color="#888888">
<br>
-Adrian</font><div><div></div><div><br>
<br>
On 10-12-03 02:53 AM, Vitaly V. Ch wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi!<br>
<br>
I'm interested in any tips or examples of using ragel on binary datagrams.<br>
<br>
\\wbr Vitaly Chernooky<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>
<br>
</blockquote>
<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>
</div></div></blockquote></div><br>