[erlang-questions] Binary Parsing

Kaiduan Xie kaiduanx@REDACTED
Sat Mar 21 23:55:05 CET 2009

Here Length is an un-bound variable, you can not use un-bound variable in
Att:Length-2/binary, try to get Length first, then do another matching.

parseAttributes(Bin, Atts) ->
      %% You get the Length first, then do another match
     <<Type:?BYTE, Length:?BYTE, Rest/binary>> = Bin,
     Len2 = Length -2,
     <<Type:?BYTE, Length:?BYTE, Att:Len2/binary, Rest2/binary>> = Bin,

On Sat, Mar 21, 2009 at 3:52 PM, Pete Stapley <pstapley@REDACTED> wrote:

> I am attempting to parse some Radius attributes with Erlang and I have
> run into a bit of a problem. I pass some binary data to parseAttributes
> and I want to start peeling off the attributes one by one. I first peel
> off Type which is one byte, then Length which is also one byte. Next I
> want to peel off the Attribute. The Length variable is the length of the
> attribute plus the Type and Length byte, so I have to subtract 2 bytes
> from the Length variable before peeling off the attribute. I have tried
> the method below without any luck. Is there a way to do this?
> Thanks,
> -define(BYTE, 8/unsigned-big-integer).
> parseAttributes(Bin) ->
>     parseAttributes(Bin, []).
> parseAttributes(<<>>, Atts) ->
>     {ok, Atts};
> %%problem is here, Length - 2 does not seem to work
> parseAttributes( <<Type:?BYTE, Length:?BYTE, Att:Length - 2/binary,
> Rest/binary>>, Atts) ->
>     io:format("Type: ~p Length: ~p Att:~p Length: ~p~n", [Type, Length,
> Att, size(Rest)]),
>     parseAttributes(Rest, Atts).
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090321/34ea2586/attachment.htm>

More information about the erlang-questions mailing list