[erlang-questions] problem of ei_decode_string

caox caox@REDACTED
Mon Oct 25 14:09:02 CEST 2010


thanks a lot. I know where the problem is.

The return type of ei_get_type() will be ERL_LIST_EXT rather than ERL_STRING_EXT when string length is beyond 65535. So I should take care of this.
在 2010-10-25,下午6:40, Michael Santos 写道:

> On Mon, Oct 25, 2010 at 05:02:39PM +0800, caox wrote:
>> hi
>> 
>> 	I find there will be problem with this api when the length of list to be decoded is larger than 65535. Is this the limit of decoded string, or a bug?
> 
> A string in the external term format has a 2 byte length header. If the
> string length can't be represented in 2 bytes, it will be encoded as a
> list of small integers:
> 
> 1> term_to_binary(lists:flatten(lists:duplicate(16#ffff-1, "n"))).
> <<131,107,255,254,110,110,110,110,110,110,110,110,110,110,
>  110,110,110,110,110,110,110,110,110,110,110,110,110,110,
>  110,...>>
> 
> 131 = Erlang magic version number
> 107 = type is string
> 255,254 = length is 65534 bytes
> 110,... = "nnnn..."
> 
> 2> term_to_binary(lists:flatten(lists:duplicate(16#ffff, "n"))).
> <<131,108,0,0,255,255,97,110,97,110,97,110,97,110,97,110,
>  97,110,97,110,97,110,97,110,97,110,97,110,97,...>>
> 
> 131 = Erlang magic version number
> 108 = type is list
> 0,0,255,255 = length is 65535 bytes
> 97 = type is small integer
> 110 = "n"
> ...
> 
> ei_decode_string() works with both formats.
> 
> 



More information about the erlang-questions mailing list