# questions about binpac

**URL:** <https://community.zeek.org/t/questions-about-binpac/2746>\
**Category:** Zeek\
**Created:** [July 24, 2013, 8:59am UTC](https://community.zeek.org/t/questions-about-binpac/2746 "2013-07-24T08:59:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolas.retrain](https://avatars.discourse-cdn.com/v4/letter/n/3bc359/32.png) [@nicolas.retrain](https://community.zeek.org/u/nicolas.retrain)\
**Post date:** [July 24, 2013, 8:59am UTC](https://community.zeek.org/t/questions-about-binpac/2746/1 "2013-07-24T08:59:00Z")

</div>

Hi,

I am trying parse BER with binpac, but I have some difficulties, and I wonder if there is a clean way to implement the protocol.

-First, is it possible to use recursive code in binpac?

type A = record {  
&nbsp;&nbsp;&nbsp;&nbsp;field1 : uint8;  
&nbsp;&nbsp;&nbsp;&nbsp;field2 : case field1 of{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x10 -\> value : B;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};  
};

type B = record {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field1 : A;  
}

This kind of dependency give me "error 139" at compilation time.  
(Sorry for writing code in a email..)

-Secondly, I need to do some computations on the current byte to know how to parse the next ones.  
I can sum up the problem, for instance, with the length field. Due to the X.690 documentation ([https://www.itu.int/rec/T-REC-X.690-200811-I](https://www.itu.int/rec/T-REC-X.690-200811-I)), the algorithm is :

-if the lead bit of the first byte is 0, then the byte is the packet length.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex : 0x20 0x02 0x01 0x..  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^ | ---- -- -- -- ... |other fields  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> length

-else , the 7 other bits of the first byte give then number of byte to parse in order to retrieve the packet length.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex : 0x84 0x00 0x00 0x00 0x41 0x..  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^ ^ | -- ... |other fields  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>-- -- -- -- -- -- -- --|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;length

In my binpac I have tried something with &let and &if, but it was not working. Is there a way to do it in full binpac code? (otherwise I will write it in C++).

Thanks for your help !

Nicolas

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/zeek/original/1X/f09d732bc2cc7c7cc7e35db67cf4e1d5233ce7a7.png) [@system](https://community.zeek.org/u/system)\
**Post date:** [May 6, 2022, 3:41pm UTC](https://community.zeek.org/t/questions-about-binpac/2746/2 "2022-05-06T15:41:06Z")

</div>


