[erlang-questions] problem of ei_decode_string

Michael Santos michael.santos@REDACTED
Mon Oct 25 12:40:59 CEST 2010


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