questions about binpac


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 {
    field1 : uint8;
    field2 : case field1 of{
         0x10 -> value : B;

type B = record {
     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 (, the algorithm is :

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

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

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 !