anchor.diff:      p6uuid:parse(UUID);
   encode_type(secid,SecID) when is_binary(SecID) ->
       SecID;
  -encode_type(bytes,Bytes) when is_binary(Bytes)->
  -    [encode_varint(size(Bytes)),Bytes];
   encode_type(string,String) when is_tuple(String) ->
       encode_type(string,p6str:mkstr("~p",String));
   encode_type(string,String) when is_list(String) ->
  
positive.code: encode_type(string,String) when is_binary(String)->
      [encode_varint(size(String)),String].
  
  encode_array(Data) ->
      encode_array(Data,0,[]).
  
  encode_array([],Sz,Acc) ->
      [encode_type(varint32,Sz),lists:reverse(Acc)];
  encode_array([Item|Items],Sz,Acc) ->
      encode_array(Items,Sz+1,[encode_obj(Item)|Acc]).
  
  encode_map(Data) ->
      encode_map(Data,0,[]).
  
  encode_map([],Sz,Acc) ->
      [encode_varint(Sz),Acc];
  encode_map([{K,V}|Tail],Sz,Acc) ->
      encode_map(Tail,Sz+1,[encode_obj(K),encode_obj(V)|Acc]).
  
  encode_uuid(Bin) when is_binary(Bin) ->
      Bin;
  
negative.code: encode_channel_type(Other) -> Other.
  
  %% Tagged types
  encode_obj(?raw(Data)) -> Data;
  encode_obj(Data) -> encode_obj(v1_1, Data).
  
  encode_obj(v1_1, Obj) when is_binary(Obj) ->
      [?FAST_STRING, encode_obj(size(Obj)), Obj];
  encode_obj(v1_1, 0) -> ?INT0;
  encode_obj(v1_1, Obj) when is_integer(Obj) ->
      case Obj of
          O when O >= -128 andalso O < 128 -> [?INT1, <<O:8/signed>>];
          O when O > 0 andalso O < 256 -> [?UINT1, <<O:8>>];
          O when O >= -32768 andalso O < 32768 -> [?INT2, <<O:16/signed>>];
          O when O > 0 andalso O < 65536 -> [?UINT2, <<O:16>>];
          O when O >= -2147483648 andalso O < 2147483648 ->
              [?INT4, <<O:32/signed>>];
          O when O > 0 andalso O < 4294967296 -> [?UINT4, <<O:32>>];
          O when O >= -9223372036854775808 andalso
                 O < 9223372036854775808 ->
              [?INT8, <<O:64/signed>>];
          O when O > 0 andalso O < 18446744073709551616 -> [?UINT8, <<O:64>>];
          O -> ?lerr("Int too big to mmd encode: ~p", [O]), O
      end;
  encode_obj(_, Obj) when is_float(Obj) ->
      [?DOUBLE, encode_type(double, Obj)];
  encode_obj(Vsn, Obj) when is_atom(Obj) ->
      encode_obj(Vsn, atom_to_binary(Obj, utf8));
  
  encode_obj(_, true) -> [?TRUE];
  encode_obj(_, false) -> [?FALSE];
  encode_obj(_, undefined) -> [?NULL];
  encode_obj(_, null) -> [?NULL];
  encode_obj(_, nil) -> [?NULL];
  
  encode_obj(Vsn, ?map(Data)) ->
      {Sz, Body} = encode_map(Vsn, Data),
      case Vsn of
          v1_1 -> [?FAST_MAP, encode_obj(Sz), Body];
          v1_0 -> [?VARINT_MAP, encode_varint(Sz), Body]
      end;
  encode_obj(Vsn, ?array(Data)) ->
      {Sz, Body} = encode_array(Vsn, Data),
      case Vsn of
          v1_1 -> [?FAST_ARRAY, encode_obj(Sz), Body];
          v1_0 -> [?VARINT_ARRAY, encode_type(varint32, Sz), Body]
      end;
  encode_obj(v1_1, ?error(Code, Msg)) ->
      [?VARINT_ERROR, encode_obj(Code), encode_obj(v1_1, Msg)];
  encode_obj(v1_1, ?bytes(Data)) -> [?FAST_BYTES, encode_obj(size(Data)), Data];
  encode_obj(_, ?byte(Byte)) ->
      [?BYTE, Byte];
  encode_obj(_, ?uuid(Data)) -> [?UUID, encode_type(uuid, Data)];
  encode_obj(_, ?secid(Data)) -> [?SECID, encode_type(secid, Data)];
  encode_obj(v1_1,  ?time(Data)) -> [?FAST_TIME, <<Data:64>>];
  encode_obj(Vsn, Obj) when is_list(Obj) -> encode_obj(Vsn, ?array(Obj));
  encode_obj(Vsn, Obj) when is_tuple(Obj) ->
      encode_obj(Vsn, ?array(tuple_to_list(Obj)));
  
  encode_obj(v1_0, Obj) when is_binary(Obj) ->
      [?VARINT_STRING, encode_varint(size(Obj)), Obj];
  
  encode_obj(v1_0, ?error(Code, Msg)) ->
      [?VARINT_ERROR, encode_type(svarint32, Code), encode_obj(v1_0, Msg)];
  encode_obj(v1_0, ?bytes(Data)) -> [?VARINT_BYTES, encode_varint(Data), Data];
  encode_obj(v1_0,  ?time(Data)) -> [?VARINT_TIME, encode_svarint(Data)];
  encode_obj(v1_0, Obj) when is_integer(Obj), Obj < 0 ->
      [?SVARINT64, encode_type(svarint64, Obj)];
  encode_obj(v1_0, Obj) when is_integer(Obj) ->
      [?VARINT64, encode_type(varint64, Obj)].
  
  
  %% Encodes a given value as the specified type
  %% Note: This does NOT prefix with a type specifier, it's up to the caller
  