anchor.diff:  % erl> c(msgpack).
   % erl> S = <some term>.
   % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
  --type reason() ::  enomem | badarg | no_code_matches.
  --type msgpack_term() :: [msgpack_term()] | {[{msgpack_term(),msgpack_term()}]} | integer() | float().
  +-type reason() ::  enomem | {badarg, term()}.
  +-type msgpack_term() :: [msgpack_term()] | {[{msgpack_term(),msgpack_term()}]} | integer() | float() | binary().
   
   % ===== external APIs ===== %
  --spec pack(Term::msgpack_term()) -> binary().
  +
  +% @doc Pack one erlang term into an msgpack message.
  +-spec pack(Term::msgpack_term()) -> binary() | reason().
   pack(O) when is_integer(O) andalso O < 0 ->
       pack_int_(O);
   pack(O) when is_integer(O) ->
  
positive.code: 	    << 16#DB:8, Len:32/big-unsigned-integer-unit:1, Bin/binary >>
      end.
  
  % list / tuple
  pack_array(L) ->
      case length(L) of
   	Len when Len < 16 ->
  
negative.code:     pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
  
  % FIXME: write test for unpack_map/1
  -spec unpack_map_(binary(), non_neg_integer(), [{msgpack_term(), msgpack_term()}]) -> {more, undefined} | {any(), binary()} | {error, reason()}.
  unpack_map_(Bin,  0,  Acc) -> {{lists:reverse(Acc)}, Bin};
  unpack_map_(Bin, Len, Acc) ->
      case unpack(Bin) of
  	{more, undefined} -> {more, undefined};
  	{Key, Rest} ->
  	    case unpack(Rest) of
  		{more, undefined} -> {more, undefined};
  		{Value, Rest2} ->
  		    unpack_map_(Rest2,Len-1,[{Key,Value}|Acc])
  	    end
      end.
  
  
  -spec unpack_(Payload::binary()) -> {more, undefined} | {msgpack_term(), binary()} | {error, reason()}.
  % Atoms
  unpack_(<<16#C0, Rest/binary>>) -> {nil, Rest};
  unpack_(<<16#C2, Rest/binary>>) -> {false, Rest};
  unpack_(<<16#C3, Rest/binary>>) -> {true, Rest};
  
  % Floats
  unpack_(<<16#CA, Val:32/float-unit:1, Rest/binary>>) -> {Val, Rest};
  unpack_(<<16#CB, Val:64/float-unit:1, Rest/binary>>) -> {Val, Rest};
  
  % Unsigned integers
  unpack_(<<16#CC, Val:8/unsigned-integer, Rest/binary>>) ->             {Val, Rest};
  unpack_(<<16#CD, Val:16/big-unsigned-integer-unit:1, Rest/binary>>) -> {Val, Rest};
  unpack_(<<16#CE, Val:32/big-unsigned-integer-unit:1, Rest/binary>>) -> {Val, Rest};
  unpack_(<<16#CF, Val:64/big-unsigned-integer-unit:1, Rest/binary>>) -> {Val, Rest};
  
  % Signed integers
  unpack_(<<16#D0, Val:8/signed-integer, Rest/binary>>) ->             {Val, Rest};
  unpack_(<<16#D1, Val:16/big-signed-integer-unit:1, Rest/binary>>) -> {Val, Rest};
  unpack_(<<16#D2, Val:32/big-signed-integer-unit:1, Rest/binary>>) -> {Val, Rest};
  unpack_(<<16#D3, Val:64/big-signed-integer-unit:1, Rest/binary>>) -> {Val, Rest};
  
  % Raw bytes
  unpack_(<<16#DA, Len:16/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>>) -> {Val, Rest};
  unpack_(<<16#DB, Len:32/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>>) -> {Val, Rest};
  
  % Arrays
  unpack_(<<16#DC, Len:16/big-unsigned-integer-unit:1, Rest/binary>>) -> unpack_array_(Rest, Len, []);
  unpack_(<<16#DD, Len:32/big-unsigned-integer-unit:1, Rest/binary>>) -> unpack_array_(Rest, Len, []);
  
  % Maps
  unpack_(<<16#DE, Len:16/big-unsigned-integer-unit:1, Rest/binary>>) -> unpack_map_(Rest, Len, []);
  unpack_(<<16#DF, Len:32/big-unsigned-integer-unit:1, Rest/binary>>) -> unpack_map_(Rest, Len, []);
  
  % Tag-encoded lengths (kept last, for speed)
  unpack_(<<0:1, Val:7, Rest/binary>>) ->                      {Val, Rest};                  % pos fixnum
  unpack_(<<2#111:3, Val:5, Rest/binary>>) ->                  {Val - 2#100000, Rest};       % neg fixnum
  unpack_(<<2#101:3, Len:5, Val:Len/binary, Rest/binary>>) ->  {Val, Rest};                  % fixraw
  unpack_(<<2#1001:4, Len:4, Rest/binary>>) ->                 unpack_array_(Rest, Len, []); % fixarray
  unpack_(<<2#1000:4, Len:4, Rest/binary>>) ->                 unpack_map_(Rest, Len, []);   % fixmap
  
  % Incomplete / invalid data
  unpack_(<<F:8, Rest/binary>>) when F==16#C1; F==16#C4; F==16#C5; F==16#C6; F==16#C7; F==16#C8; F==16#C9; F==16#D5; F==16#D6; F==16#D7; F==16#D8; F==16#D9->
      {error, {badarg, <<F, Rest/binary>>}};
  unpack_(_Bin) ->
      {more, undefined}.
  
  
  % ===== test codes ===== %
  