erlang自定义二进制协议
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

762 lines
28 KiB

  1. -module(protoMsg).
  2. -compile([nowarn_unused_vars]).
  3. -export([encode/1, decode/1, encodeRec/1, decodeBin/2]).
  4. -define(min8, -128).
  5. -define(max8, 127).
  6. -define(min16, -32768).
  7. -define(max16, 32767).
  8. -define(min32, -2147483648).
  9. -define(max32, 2147483647).
  10. -define(min64, -9223372036854775808).
  11. -define(max64, 9223372036854775807).
  12. -define(minF32, -3.4E+38).
  13. -define(maxF32, 3.4E+38).
  14. -define(minF64, -1.797E-308).
  15. -define(maxF64, 1.797E+308).
  16. -define(int8(V), <<V:8>>).
  17. -define(uint8(V), <<V:8>>).
  18. -define(int16(V), <<V:16/big>>).
  19. -define(uint16(V), <<V:16/big>>).
  20. -define(int32(V), <<V:32/big>>).
  21. -define(uint32(V), <<V:32/big>>).
  22. -define(int64(V), <<V:64/big>>).
  23. -define(uint64(V), <<V:64/big>>).
  24. -define(integer(V), (integer(V))).
  25. -define(number(V), (number(V))).
  26. -define(string(V), (string(V))).
  27. -define(float(V), <<V:32/big-float>>).
  28. -define(double(V), <<V:64/big-float>>).
  29. -define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)).
  30. -define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)).
  31. -define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]).
  32. -define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]).
  33. -define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]).
  34. -define(list_int16(List), [<<(length(List)):16/big>>, [?int16(V) || V <- List]]).
  35. -define(list_uint16(List), [<<(length(List)):16/big>>, [?uint16(V) || V <- List]]).
  36. -define(list_int32(List), [<<(length(List)):16/big>>, [?int32(V) || V <- List]]).
  37. -define(list_uint32(List), [<<(length(List)):16/big>>, [?uint32(V) || V <- List]]).
  38. -define(list_int64(List), [<<(length(List)):16/big>>, [?int64(V) || V <- List]]).
  39. -define(list_uint64(List), [<<(length(List)):16/big>>, [?uint64(V) || V <- List]]).
  40. -define(list_float(List), [<<(length(List)):16/big>>, [?float(V) || V <- List]]).
  41. -define(list_double(List), [<<(length(List)):16/big>>, [?double(V) || V <- List]]).
  42. -define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]).
  43. -define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]).
  44. -define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]).
  45. -define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]).
  46. integer(V) ->
  47. if
  48. V >= ?min8 andalso V =< ?max8 ->
  49. <<8:8, <<V:8>>/binary>>;
  50. V >= ?min16 andalso V =< ?max16 ->
  51. <<16:8, <<V:16/big>>/binary>>;
  52. V >= ?min32 andalso V =< ?max32 ->
  53. <<32:8, <<V:32/big>>/binary>>;
  54. V >= ?min64 andalso V =< ?max64 ->
  55. <<64:8, <<V:64/big>>/binary>>;
  56. true ->
  57. throw(exceeded_the_integer)
  58. end.
  59. number(V) ->
  60. if
  61. erlang:is_integer(V) ->
  62. if
  63. V >= ?min8 andalso V =< ?max8 ->
  64. <<8:8, <<V:8>>/binary>>;
  65. V >= ?min16 andalso V =< ?max16 ->
  66. <<16:8, <<V:16/big>>/binary>>;
  67. V >= ?min32 andalso V =< ?max32 ->
  68. <<32:8, <<V:32/big>>/binary>>;
  69. V >= ?min64 andalso V =< ?max64 ->
  70. <<64:8, <<V:64/big>>/binary>>;
  71. true ->
  72. throw(exceeded_the_integer)
  73. end;
  74. erlang:is_float(V) ->
  75. if
  76. V >= ?minF32 andalso V =< ?maxF32 ->
  77. <<33:8, <<V:32/big-float>>/binary>>;
  78. V >= ?minF64 andalso V =< ?maxF64 ->
  79. <<65:8, <<V:64/big-float>>/binary>>;
  80. true ->
  81. throw(exceeded_the_float)
  82. end;
  83. true ->
  84. throw(is_not_number)
  85. end.
  86. string(Str) when is_binary(Str) ->
  87. [<<(byte_size(Str)):16/big>>, Str];
  88. string(Str) ->
  89. Str2 = unicode:characters_to_binary(Str, utf8),
  90. [<<(byte_size(Str2)):16/big>>, Str2].
  91. decode(Bin) ->
  92. <<MsgId:16/big, MsgBin/binary>> = Bin,
  93. decodeBin(MsgId, MsgBin).
  94. deBoolList(0, MsgBin, RetList) ->
  95. {lists:reverse(RetList), MsgBin};
  96. deBoolList(N, MsgBin, RetList) ->
  97. <<Bool:8, LeftBin/binary>> = MsgBin,
  98. case Bool =:= 1 of
  99. true ->
  100. deBoolList(N - 1, LeftBin, [true | RetList]);
  101. _ ->
  102. deBoolList(N - 1, LeftBin, [false | RetList])
  103. end.
  104. deInt8List(0, MsgBin, RetList) ->
  105. {RetList, MsgBin};
  106. deInt8List(N, MsgBin, RetList) ->
  107. <<Int:8/big-signed, LeftBin/binary>> = MsgBin,
  108. deInt8List(N - 1, LeftBin, [Int | RetList]).
  109. deUint8List(0, MsgBin, RetList) ->
  110. {lists:reverse(RetList), MsgBin};
  111. deUint8List(N, MsgBin, RetList) ->
  112. <<Int:8/big-unsigned, LeftBin/binary>> = MsgBin,
  113. deUint8List(N - 1, LeftBin, [Int | RetList]).
  114. deInt16List(0, MsgBin, RetList) ->
  115. {lists:reverse(RetList), MsgBin};
  116. deInt16List(N, MsgBin, RetList) ->
  117. <<Int:16/big-signed, LeftBin/binary>> = MsgBin,
  118. deInt16List(N - 1, LeftBin, [Int | RetList]).
  119. deUint16List(0, MsgBin, RetList) ->
  120. {lists:reverse(RetList), MsgBin};
  121. deUint16List(N, MsgBin, RetList) ->
  122. <<Int:16/big-unsigned, LeftBin/binary>> = MsgBin,
  123. deUint16List(N - 1, LeftBin, [Int | RetList]).
  124. deInt32List(0, MsgBin, RetList) ->
  125. {lists:reverse(RetList), MsgBin};
  126. deInt32List(N, MsgBin, RetList) ->
  127. <<Int:32/big-signed, LeftBin/binary>> = MsgBin,
  128. deInt32List(N - 1, LeftBin, [Int | RetList]).
  129. deUint32List(0, MsgBin, RetList) ->
  130. {lists:reverse(RetList), MsgBin};
  131. deUint32List(N, MsgBin, RetList) ->
  132. <<Int:32/big-unsigned, LeftBin/binary>> = MsgBin,
  133. deUint32List(N - 1, LeftBin, [Int | RetList]).
  134. deInt64List(0, MsgBin, RetList) ->
  135. {lists:reverse(RetList), MsgBin};
  136. deInt64List(N, MsgBin, RetList) ->
  137. <<Int:64/big-signed, LeftBin/binary>> = MsgBin,
  138. deInt64List(N - 1, LeftBin, [Int | RetList]).
  139. deUint64List(0, MsgBin, RetList) ->
  140. {lists:reverse(RetList), MsgBin};
  141. deUint64List(N, MsgBin, RetList) ->
  142. <<Int:64/big-unsigned, LeftBin/binary>> = MsgBin,
  143. deUint64List(N - 1, LeftBin, [Int | RetList]).
  144. deIntegerList(0, MsgBin, RetList) ->
  145. {lists:reverse(RetList), MsgBin};
  146. deIntegerList(N, MsgBin, RetList) ->
  147. <<IntBits:8, Int:IntBits/big-signed, LeftBin/binary>> = MsgBin,
  148. deIntegerList(N - 1, LeftBin, [Int | RetList]).
  149. deNumberList(0, MsgBin, RetList) ->
  150. {lists:reverse(RetList), MsgBin};
  151. deNumberList(N, MsgBin, RetList) ->
  152. <<NumBits:8, NumBin/binary>> = MsgBin,
  153. case NumBits of
  154. 33 ->
  155. <<Float:32/big-float, LeftBin/binary>> = NumBin,
  156. deNumberList(N - 1, LeftBin, [Float | RetList]);
  157. 65 ->
  158. <<Float:64/big-float, LeftBin/binary>> = NumBin,
  159. deNumberList(N - 1, LeftBin, [Float | RetList]);
  160. _ ->
  161. <<Int:NumBits/big-signed, LeftBin/binary>> = NumBin,
  162. deNumberList(N - 1, LeftBin, [Int | RetList])
  163. end.
  164. deFloatList(0, MsgBin, RetList) ->
  165. {lists:reverse(RetList), MsgBin};
  166. deFloatList(N, MsgBin, RetList) ->
  167. <<Float:32/big-float, LeftBin/binary>> = MsgBin,
  168. deFloatList(N - 1, LeftBin, [Float | RetList]).
  169. deDoubleList(0, MsgBin, RetList) ->
  170. {lists:reverse(RetList), MsgBin};
  171. deDoubleList(N, MsgBin, RetList) ->
  172. <<Float:64/big-float, LeftBin/binary>> = MsgBin,
  173. deDoubleList(N - 1, LeftBin, [Float | RetList]).
  174. deStringList(0, MsgBin, RetList) ->
  175. {lists:reverse(RetList), MsgBin};
  176. deStringList(N, MsgBin, RetList) ->
  177. <<Len:16/big, StrBin:Len/binary-unit:8, LeftBin/binary>> = MsgBin,
  178. deStringList(N - 1, LeftBin, [StrBin | RetList]).
  179. deRecordList(0, _MsgId, MsgBin, RetList) ->
  180. {lists:reverse(RetList), MsgBin};
  181. deRecordList(N, MsgId, MsgBin, RetList) ->
  182. {Tuple, LeftBin} = decodeRec(MsgId, MsgBin),
  183. deRecordList(N - 1, MsgId, LeftBin, [Tuple | RetList]).
  184. getMsgId(test)-> 1;
  185. getMsgId(phoneNumber)-> 2;
  186. getMsgId(person)-> 3;
  187. getMsgId(addressBook)-> 4;
  188. getMsgId(union)-> 5;
  189. getMsgId(tbool)-> 6;
  190. getMsgId(tint8)-> 7;
  191. getMsgId(tuint8)-> 8;
  192. getMsgId(tint16)-> 9;
  193. getMsgId(tuint16)-> 10;
  194. getMsgId(tint32)-> 11;
  195. getMsgId(tuint32)-> 12;
  196. getMsgId(tint64)-> 13;
  197. getMsgId(tuint64)-> 14;
  198. getMsgId(tinteger)-> 15;
  199. getMsgId(tnumber)-> 16;
  200. getMsgId(tfloat)-> 17;
  201. getMsgId(tdouble)-> 18;
  202. getMsgId(tstring)-> 19;
  203. getMsgId(tlistbool)-> 20;
  204. getMsgId(tlistint8)-> 21;
  205. getMsgId(tlistuint8)-> 22;
  206. getMsgId(tlistint16)-> 23;
  207. getMsgId(tlistuint16)-> 24;
  208. getMsgId(tlistint32)-> 25;
  209. getMsgId(tlistuint32)-> 26;
  210. getMsgId(tlistint64)-> 27;
  211. getMsgId(tlistuint64)-> 28;
  212. getMsgId(tlistinteger)-> 29;
  213. getMsgId(tlistnumber)-> 30;
  214. getMsgId(tlistfloat)-> 31;
  215. getMsgId(tlistdouble)-> 32;
  216. getMsgId(tliststring)-> 33;
  217. getMsgId(tlistunion)-> 34;
  218. getMsgId(allType)-> 35;
  219. getMsgId(person1)-> 1001;
  220. getMsgId(_) -> 0.
  221. encodeRec({test, V1}) ->
  222. [?string(V1)];
  223. encodeRec({phoneNumber, V1, V2}) ->
  224. [?record(V1), ?int32(V2)];
  225. encodeRec({person, V1, V2, V3, V4}) ->
  226. [?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)];
  227. encodeRec({union, V1, V2}) ->
  228. [?string(V1), ?int32(V2)];
  229. encodeRec(_) ->
  230. [].
  231. encode({test, V1}) ->
  232. [<<1:16/big-unsigned>>, ?string(V1)];
  233. encode({phoneNumber, V1, V2}) ->
  234. [<<2:16/big-unsigned>>, ?record(V1), ?int32(V2)];
  235. encode({person, V1, V2, V3, V4}) ->
  236. [<<3:16/big-unsigned>>, ?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)];
  237. encode({addressBook, V1, V2}) ->
  238. [<<4:16/big-unsigned>>, ?list_record(V1), ?list_record(V2)];
  239. encode({union, V1, V2}) ->
  240. [<<5:16/big-unsigned>>, ?string(V1), ?int32(V2)];
  241. encode({tbool, V1}) ->
  242. [<<6:16/big-unsigned>>, ?bool(V1)];
  243. encode({tint8, V1, V2}) ->
  244. [<<7:16/big-unsigned>>, ?int8(V1), ?int8(V2)];
  245. encode({tuint8, V1, V2}) ->
  246. [<<8:16/big-unsigned>>, ?uint8(V1), ?uint8(V2)];
  247. encode({tint16, V1, V2}) ->
  248. [<<9:16/big-unsigned>>, ?int16(V1), ?int16(V2)];
  249. encode({tuint16, V1, V2}) ->
  250. [<<10:16/big-unsigned>>, ?uint16(V1), ?uint16(V2)];
  251. encode({tint32, V1, V2}) ->
  252. [<<11:16/big-unsigned>>, ?int32(V1), ?int32(V2)];
  253. encode({tuint32, V1, V2}) ->
  254. [<<12:16/big-unsigned>>, ?uint32(V1), ?uint32(V2)];
  255. encode({tint64, V1, V2}) ->
  256. [<<13:16/big-unsigned>>, ?int64(V1), ?int64(V2)];
  257. encode({tuint64, V1, V2}) ->
  258. [<<14:16/big-unsigned>>, ?uint64(V1), ?uint64(V2)];
  259. encode({tinteger, V1, V2, V3, V4, V5, V6, V7, V8}) ->
  260. [<<15:16/big-unsigned>>, ?integer(V1), ?integer(V2), ?integer(V3), ?integer(V4), ?integer(V5), ?integer(V6), ?integer(V7), ?integer(V8)];
  261. encode({tnumber, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) ->
  262. [<<16:16/big-unsigned>>, ?number(V1), ?number(V2), ?number(V3), ?number(V4), ?number(V5), ?number(V6), ?number(V7), ?number(V8), ?number(V9), ?number(V10)];
  263. encode({tfloat, V1, V2}) ->
  264. [<<17:16/big-unsigned>>, ?float(V1), ?float(V2)];
  265. encode({tdouble, V1, V2}) ->
  266. [<<18:16/big-unsigned>>, ?double(V1), ?double(V2)];
  267. encode({tstring, V1, V2}) ->
  268. [<<19:16/big-unsigned>>, ?string(V1), ?string(V2)];
  269. encode({tlistbool, V1}) ->
  270. [<<20:16/big-unsigned>>, ?list_bool(V1)];
  271. encode({tlistint8, V1}) ->
  272. [<<21:16/big-unsigned>>, ?list_int8(V1)];
  273. encode({tlistuint8, V1}) ->
  274. [<<22:16/big-unsigned>>, ?list_uint8(V1)];
  275. encode({tlistint16, V1}) ->
  276. [<<23:16/big-unsigned>>, ?list_int16(V1)];
  277. encode({tlistuint16, V1}) ->
  278. [<<24:16/big-unsigned>>, ?list_uint16(V1)];
  279. encode({tlistint32, V1}) ->
  280. [<<25:16/big-unsigned>>, ?list_int32(V1)];
  281. encode({tlistuint32, V1}) ->
  282. [<<26:16/big-unsigned>>, ?list_uint32(V1)];
  283. encode({tlistint64, V1}) ->
  284. [<<27:16/big-unsigned>>, ?list_int64(V1)];
  285. encode({tlistuint64, V1}) ->
  286. [<<28:16/big-unsigned>>, ?list_uint64(V1)];
  287. encode({tlistinteger, V1}) ->
  288. [<<29:16/big-unsigned>>, ?list_integer(V1)];
  289. encode({tlistnumber, V1}) ->
  290. [<<30:16/big-unsigned>>, ?list_number(V1)];
  291. encode({tlistfloat, V1}) ->
  292. [<<31:16/big-unsigned>>, ?list_float(V1)];
  293. encode({tlistdouble, V1}) ->
  294. [<<32:16/big-unsigned>>, ?list_double(V1)];
  295. encode({tliststring, V1}) ->
  296. [<<33:16/big-unsigned>>, ?list_string(V1)];
  297. encode({tlistunion, V1}) ->
  298. [<<34:16/big-unsigned>>, ?list_record(V1)];
  299. encode({allType, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}) ->
  300. [<<35:16/big-unsigned>>, ?bool(V1), ?int8(V2), ?uint8(V3), ?int16(V4), ?uint16(V5), ?int32(V6), ?uint32(V7), ?int64(V8), ?uint64(V9), ?integer(V10), ?integer(V11), ?integer(V12), ?integer(V13), ?integer(V14), ?integer(V15), ?integer(V16), ?integer(V17), ?number(V18), ?number(V19), ?number(V20), ?number(V21), ?number(V22), ?number(V23), ?number(V24), ?number(V25), ?number(V26), ?number(V27), ?float(V28), ?double(V29), ?string(V30), ?string(V31), ?record(V32), ?list_bool(V33), ?list_int8(V34), ?list_uint8(V35), ?list_int16(V36), ?list_uint16(V37), ?list_int32(V38), ?list_uint32(V39), ?list_int64(V40), ?list_uint64(V41), ?list_integer(V42), ?list_integer(V43), ?list_integer(V44), ?list_integer(V45), ?list_number(V46), ?list_number(V47), ?list_number(V48), ?list_number(V49), ?list_number(V50), ?list_number(V51), ?list_float(V52), ?list_double(V53), ?list_string(V54), ?list_record(V55)];
  301. encode({person1, V1, V2, V3, V4}) ->
  302. [<<1001:16/big-unsigned>>, ?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)];
  303. encode(_) ->
  304. [].
  305. decodeRec(1, LeftBin0) ->
  306. <<Len1:16/big-unsigned, V1:Len1/binary, LeftBin1/binary>> = LeftBin0,
  307. MsgRec = {test, V1},
  308. {MsgRec, LeftBin1};
  309. decodeRec(2, LeftBin0) ->
  310. <<IsUndef1:8/big-unsigned, LeftBin1/binary>> = LeftBin0,
  311. case IsUndef1 of
  312. 0 ->
  313. V1 = undefined,
  314. LeftBin2 = LeftBin1 ;
  315. _ ->
  316. {V1, LeftBin2} = decodeRec(1, LeftBin1)
  317. end,
  318. <<V2:32/big-signed, LeftBin3/binary>> = LeftBin2,
  319. MsgRec = {phoneNumber, V1, V2},
  320. {MsgRec, LeftBin3};
  321. decodeRec(3, LeftBin0) ->
  322. <<Len1:16/big-unsigned, V1:Len1/binary, LeftBin1/binary>> = LeftBin0,
  323. <<V2:32/big-signed, LeftBin2/binary>> = LeftBin1,
  324. <<Len2:16/big-unsigned, V3:Len2/binary, LeftBin3/binary>> = LeftBin2,
  325. <<Len3:16/big-unsigned, LeftBin4/binary>> = LeftBin3,
  326. {V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []),
  327. MsgRec = {person, V1, V2, V3, V4},
  328. {MsgRec, LeftBin5};
  329. decodeRec(5, LeftBin0) ->
  330. <<Len1:16/big-unsigned, V1:Len1/binary, LeftBin1/binary>> = LeftBin0,
  331. <<V2:32/big-signed, LeftBin2/binary>> = LeftBin1,
  332. MsgRec = {union, V1, V2},
  333. {MsgRec, LeftBin2};
  334. decodeRec(_, _) ->
  335. {{}, <<>>}.
  336. decodeBin(1, LeftBin0) ->
  337. <<Len1:16/big-unsigned, V1:Len1/binary, LeftBin1/binary>> = LeftBin0,
  338. {test, V1};
  339. decodeBin(2, LeftBin0) ->
  340. <<IsUndef1:8/big-unsigned, LeftBin1/binary>> = LeftBin0,
  341. case IsUndef1 of
  342. 0 ->
  343. V1 = undefined,
  344. LeftBin2 = LeftBin1 ;
  345. _ ->
  346. {V1, LeftBin2} = decodeRec(1, LeftBin1)
  347. end,
  348. <<V2:32/big-signed, LeftBin3/binary>> = LeftBin2,
  349. {phoneNumber, V1, V2};
  350. decodeBin(3, LeftBin0) ->
  351. <<Len1:16/big-unsigned, V1:Len1/binary, LeftBin1/binary>> = LeftBin0,
  352. <<V2:32/big-signed, LeftBin2/binary>> = LeftBin1,
  353. <<Len2:16/big-unsigned, V3:Len2/binary, LeftBin3/binary>> = LeftBin2,
  354. <<Len3:16/big-unsigned, LeftBin4/binary>> = LeftBin3,
  355. {V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []),
  356. {person, V1, V2, V3, V4};
  357. decodeBin(4, LeftBin0) ->
  358. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  359. {V1, LeftBin2} = deRecordList(Len1, 3, LeftBin1, []),
  360. <<Len2:16/big-unsigned, LeftBin3/binary>> = LeftBin2,
  361. {V2, LeftBin4} = deRecordList(Len2, 3, LeftBin3, []),
  362. {addressBook, V1, V2};
  363. decodeBin(5, LeftBin0) ->
  364. <<Len1:16/big-unsigned, V1:Len1/binary, LeftBin1/binary>> = LeftBin0,
  365. <<V2:32/big-signed, LeftBin2/binary>> = LeftBin1,
  366. {union, V1, V2};
  367. decodeBin(6, LeftBin0) ->
  368. <<Bool1:8/big-unsigned, LeftBin1/binary>> = LeftBin0,
  369. case Bool1 =:= 1 of
  370. true ->
  371. V1 = true;
  372. _ ->
  373. V1 = false
  374. end,
  375. {tbool, V1};
  376. decodeBin(7, LeftBin0) ->
  377. <<V1:8/big-signed, V2:8/big-signed, LeftBin1/binary>> = LeftBin0,
  378. {tint8, V1, V2};
  379. decodeBin(8, LeftBin0) ->
  380. <<V1:8/big-unsigned, V2:8/big-unsigned, LeftBin1/binary>> = LeftBin0,
  381. {tuint8, V1, V2};
  382. decodeBin(9, LeftBin0) ->
  383. <<V1:16/big-signed, V2:16/big-signed, LeftBin1/binary>> = LeftBin0,
  384. {tint16, V1, V2};
  385. decodeBin(10, LeftBin0) ->
  386. <<V1:16/big-unsigned, V2:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  387. {tuint16, V1, V2};
  388. decodeBin(11, LeftBin0) ->
  389. <<V1:32/big-signed, V2:32/big-signed, LeftBin1/binary>> = LeftBin0,
  390. {tint32, V1, V2};
  391. decodeBin(12, LeftBin0) ->
  392. <<V1:32/big-unsigned, V2:32/big-unsigned, LeftBin1/binary>> = LeftBin0,
  393. {tuint32, V1, V2};
  394. decodeBin(13, LeftBin0) ->
  395. <<V1:64/big-signed, V2:64/big-signed, LeftBin1/binary>> = LeftBin0,
  396. {tint64, V1, V2};
  397. decodeBin(14, LeftBin0) ->
  398. <<V1:64/big-unsigned, V2:64/big-unsigned, LeftBin1/binary>> = LeftBin0,
  399. {tuint64, V1, V2};
  400. decodeBin(15, LeftBin0) ->
  401. <<IntBits1:8, V1:IntBits1/big-signed, IntBits2:8, V2:IntBits2/big-signed, IntBits3:8, V3:IntBits3/big-signed, IntBits4:8, V4:IntBits4/big-signed, IntBits5:8, V5:IntBits5/big-signed, IntBits6:8, V6:IntBits6/big-signed, IntBits7:8, V7:IntBits7/big-signed, IntBits8:8, V8:IntBits8/big-signed, LeftBin1/binary>> = LeftBin0,
  402. {tinteger, V1, V2, V3, V4, V5, V6, V7, V8};
  403. decodeBin(16, LeftBin0) ->
  404. <<NumBits1:8, LeftBin1/binary>> = LeftBin0,
  405. case NumBits1 of
  406. 33->
  407. <<V1:32/big-float, LeftBin2/binary>> = LeftBin1;
  408. 65 ->
  409. <<V1:64/big-float, LeftBin2/binary>> = LeftBin1;
  410. _ ->
  411. <<V1:NumBits1/big-signed, LeftBin2/binary>> = LeftBin1
  412. end,
  413. <<NumBits2:8, LeftBin3/binary>> = LeftBin2,
  414. case NumBits2 of
  415. 33->
  416. <<V2:32/big-float, LeftBin4/binary>> = LeftBin3;
  417. 65 ->
  418. <<V2:64/big-float, LeftBin4/binary>> = LeftBin3;
  419. _ ->
  420. <<V2:NumBits2/big-signed, LeftBin4/binary>> = LeftBin3
  421. end,
  422. <<NumBits3:8, LeftBin5/binary>> = LeftBin4,
  423. case NumBits3 of
  424. 33->
  425. <<V3:32/big-float, LeftBin6/binary>> = LeftBin5;
  426. 65 ->
  427. <<V3:64/big-float, LeftBin6/binary>> = LeftBin5;
  428. _ ->
  429. <<V3:NumBits3/big-signed, LeftBin6/binary>> = LeftBin5
  430. end,
  431. <<NumBits4:8, LeftBin7/binary>> = LeftBin6,
  432. case NumBits4 of
  433. 33->
  434. <<V4:32/big-float, LeftBin8/binary>> = LeftBin7;
  435. 65 ->
  436. <<V4:64/big-float, LeftBin8/binary>> = LeftBin7;
  437. _ ->
  438. <<V4:NumBits4/big-signed, LeftBin8/binary>> = LeftBin7
  439. end,
  440. <<NumBits5:8, LeftBin9/binary>> = LeftBin8,
  441. case NumBits5 of
  442. 33->
  443. <<V5:32/big-float, LeftBin10/binary>> = LeftBin9;
  444. 65 ->
  445. <<V5:64/big-float, LeftBin10/binary>> = LeftBin9;
  446. _ ->
  447. <<V5:NumBits5/big-signed, LeftBin10/binary>> = LeftBin9
  448. end,
  449. <<NumBits6:8, LeftBin11/binary>> = LeftBin10,
  450. case NumBits6 of
  451. 33->
  452. <<V6:32/big-float, LeftBin12/binary>> = LeftBin11;
  453. 65 ->
  454. <<V6:64/big-float, LeftBin12/binary>> = LeftBin11;
  455. _ ->
  456. <<V6:NumBits6/big-signed, LeftBin12/binary>> = LeftBin11
  457. end,
  458. <<NumBits7:8, LeftBin13/binary>> = LeftBin12,
  459. case NumBits7 of
  460. 33->
  461. <<V7:32/big-float, LeftBin14/binary>> = LeftBin13;
  462. 65 ->
  463. <<V7:64/big-float, LeftBin14/binary>> = LeftBin13;
  464. _ ->
  465. <<V7:NumBits7/big-signed, LeftBin14/binary>> = LeftBin13
  466. end,
  467. <<NumBits8:8, LeftBin15/binary>> = LeftBin14,
  468. case NumBits8 of
  469. 33->
  470. <<V8:32/big-float, LeftBin16/binary>> = LeftBin15;
  471. 65 ->
  472. <<V8:64/big-float, LeftBin16/binary>> = LeftBin15;
  473. _ ->
  474. <<V8:NumBits8/big-signed, LeftBin16/binary>> = LeftBin15
  475. end,
  476. <<NumBits9:8, LeftBin17/binary>> = LeftBin16,
  477. case NumBits9 of
  478. 33->
  479. <<V9:32/big-float, LeftBin18/binary>> = LeftBin17;
  480. 65 ->
  481. <<V9:64/big-float, LeftBin18/binary>> = LeftBin17;
  482. _ ->
  483. <<V9:NumBits9/big-signed, LeftBin18/binary>> = LeftBin17
  484. end,
  485. <<NumBits10:8, LeftBin19/binary>> = LeftBin18,
  486. case NumBits10 of
  487. 33->
  488. <<V10:32/big-float, LeftBin20/binary>> = LeftBin19;
  489. 65 ->
  490. <<V10:64/big-float, LeftBin20/binary>> = LeftBin19;
  491. _ ->
  492. <<V10:NumBits10/big-signed, LeftBin20/binary>> = LeftBin19
  493. end,
  494. {tnumber, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10};
  495. decodeBin(17, LeftBin0) ->
  496. <<V1:32/big-float, V2:32/big-float, LeftBin1/binary>> = LeftBin0,
  497. {tfloat, V1, V2};
  498. decodeBin(18, LeftBin0) ->
  499. <<V1:64/big-float, V2:64/big-float, LeftBin1/binary>> = LeftBin0,
  500. {tdouble, V1, V2};
  501. decodeBin(19, LeftBin0) ->
  502. <<Len1:16/big-unsigned, V1:Len1/binary, LeftBin1/binary>> = LeftBin0,
  503. <<Len2:16/big-unsigned, V2:Len2/binary, LeftBin2/binary>> = LeftBin1,
  504. {tstring, V1, V2};
  505. decodeBin(20, LeftBin0) ->
  506. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  507. {V1, LeftBin2} = deBoolList(Len1, LeftBin1, []),
  508. {tlistbool, V1};
  509. decodeBin(21, LeftBin0) ->
  510. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  511. {V1, LeftBin2} = deInt8List(Len1, LeftBin1, []),
  512. {tlistint8, V1};
  513. decodeBin(22, LeftBin0) ->
  514. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  515. {V1, LeftBin2} = deUint8List(Len1, LeftBin1, []),
  516. {tlistuint8, V1};
  517. decodeBin(23, LeftBin0) ->
  518. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  519. {V1, LeftBin2} = deInt16List(Len1, LeftBin1, []),
  520. {tlistint16, V1};
  521. decodeBin(24, LeftBin0) ->
  522. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  523. {V1, LeftBin2} = deUint16List(Len1, LeftBin1, []),
  524. {tlistuint16, V1};
  525. decodeBin(25, LeftBin0) ->
  526. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  527. {V1, LeftBin2} = deInt32List(Len1, LeftBin1, []),
  528. {tlistint32, V1};
  529. decodeBin(26, LeftBin0) ->
  530. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  531. {V1, LeftBin2} = deUint32List(Len1, LeftBin1, []),
  532. {tlistuint32, V1};
  533. decodeBin(27, LeftBin0) ->
  534. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  535. {V1, LeftBin2} = deInt64List(Len1, LeftBin1, []),
  536. {tlistint64, V1};
  537. decodeBin(28, LeftBin0) ->
  538. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  539. {V1, LeftBin2} = deUint64List(Len1, LeftBin1, []),
  540. {tlistuint64, V1};
  541. decodeBin(29, LeftBin0) ->
  542. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  543. {V1, LeftBin2} = deIntegerList(Len1, LeftBin1, []),
  544. {tlistinteger, V1};
  545. decodeBin(30, LeftBin0) ->
  546. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  547. {V1, LeftBin2} = deNumberList(Len1, LeftBin1, []),
  548. {tlistnumber, V1};
  549. decodeBin(31, LeftBin0) ->
  550. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  551. {V1, LeftBin2} = deFloatList(Len1, LeftBin1, []),
  552. {tlistfloat, V1};
  553. decodeBin(32, LeftBin0) ->
  554. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  555. {V1, LeftBin2} = deDoubleList(Len1, LeftBin1, []),
  556. {tlistdouble, V1};
  557. decodeBin(33, LeftBin0) ->
  558. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  559. {V1, LeftBin2} = deStringList(Len1, LeftBin1, []),
  560. {tliststring, V1};
  561. decodeBin(34, LeftBin0) ->
  562. <<Len1:16/big-unsigned, LeftBin1/binary>> = LeftBin0,
  563. {V1, LeftBin2} = deRecordList(Len1, 5, LeftBin1, []),
  564. {tlistunion, V1};
  565. decodeBin(35, LeftBin0) ->
  566. <<Bool1:8/big-unsigned, LeftBin1/binary>> = LeftBin0,
  567. case Bool1 =:= 1 of
  568. true ->
  569. V1 = true;
  570. _ ->
  571. V1 = false
  572. end,
  573. <<V2:8/big-signed, V3:8/big-unsigned, V4:16/big-signed, V5:16/big-unsigned, V6:32/big-signed, V7:32/big-unsigned, V8:64/big-signed, V9:64/big-unsigned, IntBits1:8, V10:IntBits1/big-signed, IntBits2:8, V11:IntBits2/big-signed, IntBits3:8, V12:IntBits3/big-signed, IntBits4:8, V13:IntBits4/big-signed, IntBits5:8, V14:IntBits5/big-signed, IntBits6:8, V15:IntBits6/big-signed, IntBits7:8, V16:IntBits7/big-signed, IntBits8:8, V17:IntBits8/big-signed, LeftBin2/binary>> = LeftBin1,
  574. <<NumBits1:8, LeftBin3/binary>> = LeftBin2,
  575. case NumBits1 of
  576. 33->
  577. <<V18:32/big-float, LeftBin4/binary>> = LeftBin3;
  578. 65 ->
  579. <<V18:64/big-float, LeftBin4/binary>> = LeftBin3;
  580. _ ->
  581. <<V18:NumBits1/big-signed, LeftBin4/binary>> = LeftBin3
  582. end,
  583. <<NumBits2:8, LeftBin5/binary>> = LeftBin4,
  584. case NumBits2 of
  585. 33->
  586. <<V19:32/big-float, LeftBin6/binary>> = LeftBin5;
  587. 65 ->
  588. <<V19:64/big-float, LeftBin6/binary>> = LeftBin5;
  589. _ ->
  590. <<V19:NumBits2/big-signed, LeftBin6/binary>> = LeftBin5
  591. end,
  592. <<NumBits3:8, LeftBin7/binary>> = LeftBin6,
  593. case NumBits3 of
  594. 33->
  595. <<V20:32/big-float, LeftBin8/binary>> = LeftBin7;
  596. 65 ->
  597. <<V20:64/big-float, LeftBin8/binary>> = LeftBin7;
  598. _ ->
  599. <<V20:NumBits3/big-signed, LeftBin8/binary>> = LeftBin7
  600. end,
  601. <<NumBits4:8, LeftBin9/binary>> = LeftBin8,
  602. case NumBits4 of
  603. 33->
  604. <<V21:32/big-float, LeftBin10/binary>> = LeftBin9;
  605. 65 ->
  606. <<V21:64/big-float, LeftBin10/binary>> = LeftBin9;
  607. _ ->
  608. <<V21:NumBits4/big-signed, LeftBin10/binary>> = LeftBin9
  609. end,
  610. <<NumBits5:8, LeftBin11/binary>> = LeftBin10,
  611. case NumBits5 of
  612. 33->
  613. <<V22:32/big-float, LeftBin12/binary>> = LeftBin11;
  614. 65 ->
  615. <<V22:64/big-float, LeftBin12/binary>> = LeftBin11;
  616. _ ->
  617. <<V22:NumBits5/big-signed, LeftBin12/binary>> = LeftBin11
  618. end,
  619. <<NumBits6:8, LeftBin13/binary>> = LeftBin12,
  620. case NumBits6 of
  621. 33->
  622. <<V23:32/big-float, LeftBin14/binary>> = LeftBin13;
  623. 65 ->
  624. <<V23:64/big-float, LeftBin14/binary>> = LeftBin13;
  625. _ ->
  626. <<V23:NumBits6/big-signed, LeftBin14/binary>> = LeftBin13
  627. end,
  628. <<NumBits7:8, LeftBin15/binary>> = LeftBin14,
  629. case NumBits7 of
  630. 33->
  631. <<V24:32/big-float, LeftBin16/binary>> = LeftBin15;
  632. 65 ->
  633. <<V24:64/big-float, LeftBin16/binary>> = LeftBin15;
  634. _ ->
  635. <<V24:NumBits7/big-signed, LeftBin16/binary>> = LeftBin15
  636. end,
  637. <<NumBits8:8, LeftBin17/binary>> = LeftBin16,
  638. case NumBits8 of
  639. 33->
  640. <<V25:32/big-float, LeftBin18/binary>> = LeftBin17;
  641. 65 ->
  642. <<V25:64/big-float, LeftBin18/binary>> = LeftBin17;
  643. _ ->
  644. <<V25:NumBits8/big-signed, LeftBin18/binary>> = LeftBin17
  645. end,
  646. <<NumBits9:8, LeftBin19/binary>> = LeftBin18,
  647. case NumBits9 of
  648. 33->
  649. <<V26:32/big-float, LeftBin20/binary>> = LeftBin19;
  650. 65 ->
  651. <<V26:64/big-float, LeftBin20/binary>> = LeftBin19;
  652. _ ->
  653. <<V26:NumBits9/big-signed, LeftBin20/binary>> = LeftBin19
  654. end,
  655. <<NumBits10:8, LeftBin21/binary>> = LeftBin20,
  656. case NumBits10 of
  657. 33->
  658. <<V27:32/big-float, LeftBin22/binary>> = LeftBin21;
  659. 65 ->
  660. <<V27:64/big-float, LeftBin22/binary>> = LeftBin21;
  661. _ ->
  662. <<V27:NumBits10/big-signed, LeftBin22/binary>> = LeftBin21
  663. end,
  664. <<V28:32/big-float, V29:64/big-float, LeftBin23/binary>> = LeftBin22,
  665. <<Len1:16/big-unsigned, V30:Len1/binary, LeftBin24/binary>> = LeftBin23,
  666. <<Len2:16/big-unsigned, V31:Len2/binary, LeftBin25/binary>> = LeftBin24,
  667. <<IsUndef1:8/big-unsigned, LeftBin26/binary>> = LeftBin25,
  668. case IsUndef1 of
  669. 0 ->
  670. V32 = undefined,
  671. LeftBin27 = LeftBin26 ;
  672. _ ->
  673. {V32, LeftBin27} = decodeRec(5, LeftBin26)
  674. end,
  675. <<Len3:16/big-unsigned, LeftBin28/binary>> = LeftBin27,
  676. {V33, LeftBin29} = deBoolList(Len3, LeftBin28, []),
  677. <<Len4:16/big-unsigned, LeftBin30/binary>> = LeftBin29,
  678. {V34, LeftBin31} = deInt8List(Len4, LeftBin30, []),
  679. <<Len5:16/big-unsigned, LeftBin32/binary>> = LeftBin31,
  680. {V35, LeftBin33} = deUint8List(Len5, LeftBin32, []),
  681. <<Len6:16/big-unsigned, LeftBin34/binary>> = LeftBin33,
  682. {V36, LeftBin35} = deInt16List(Len6, LeftBin34, []),
  683. <<Len7:16/big-unsigned, LeftBin36/binary>> = LeftBin35,
  684. {V37, LeftBin37} = deUint16List(Len7, LeftBin36, []),
  685. <<Len8:16/big-unsigned, LeftBin38/binary>> = LeftBin37,
  686. {V38, LeftBin39} = deInt32List(Len8, LeftBin38, []),
  687. <<Len9:16/big-unsigned, LeftBin40/binary>> = LeftBin39,
  688. {V39, LeftBin41} = deUint32List(Len9, LeftBin40, []),
  689. <<Len10:16/big-unsigned, LeftBin42/binary>> = LeftBin41,
  690. {V40, LeftBin43} = deInt64List(Len10, LeftBin42, []),
  691. <<Len11:16/big-unsigned, LeftBin44/binary>> = LeftBin43,
  692. {V41, LeftBin45} = deUint64List(Len11, LeftBin44, []),
  693. <<Len12:16/big-unsigned, LeftBin46/binary>> = LeftBin45,
  694. {V42, LeftBin47} = deIntegerList(Len12, LeftBin46, []),
  695. <<Len13:16/big-unsigned, LeftBin48/binary>> = LeftBin47,
  696. {V43, LeftBin49} = deIntegerList(Len13, LeftBin48, []),
  697. <<Len14:16/big-unsigned, LeftBin50/binary>> = LeftBin49,
  698. {V44, LeftBin51} = deIntegerList(Len14, LeftBin50, []),
  699. <<Len15:16/big-unsigned, LeftBin52/binary>> = LeftBin51,
  700. {V45, LeftBin53} = deIntegerList(Len15, LeftBin52, []),
  701. <<Len16:16/big-unsigned, LeftBin54/binary>> = LeftBin53,
  702. {V46, LeftBin55} = deNumberList(Len16, LeftBin54, []),
  703. <<Len17:16/big-unsigned, LeftBin56/binary>> = LeftBin55,
  704. {V47, LeftBin57} = deNumberList(Len17, LeftBin56, []),
  705. <<Len18:16/big-unsigned, LeftBin58/binary>> = LeftBin57,
  706. {V48, LeftBin59} = deNumberList(Len18, LeftBin58, []),
  707. <<Len19:16/big-unsigned, LeftBin60/binary>> = LeftBin59,
  708. {V49, LeftBin61} = deNumberList(Len19, LeftBin60, []),
  709. <<Len20:16/big-unsigned, LeftBin62/binary>> = LeftBin61,
  710. {V50, LeftBin63} = deNumberList(Len20, LeftBin62, []),
  711. <<Len21:16/big-unsigned, LeftBin64/binary>> = LeftBin63,
  712. {V51, LeftBin65} = deNumberList(Len21, LeftBin64, []),
  713. <<Len22:16/big-unsigned, LeftBin66/binary>> = LeftBin65,
  714. {V52, LeftBin67} = deFloatList(Len22, LeftBin66, []),
  715. <<Len23:16/big-unsigned, LeftBin68/binary>> = LeftBin67,
  716. {V53, LeftBin69} = deDoubleList(Len23, LeftBin68, []),
  717. <<Len24:16/big-unsigned, LeftBin70/binary>> = LeftBin69,
  718. {V54, LeftBin71} = deStringList(Len24, LeftBin70, []),
  719. <<Len25:16/big-unsigned, LeftBin72/binary>> = LeftBin71,
  720. {V55, LeftBin73} = deRecordList(Len25, 5, LeftBin72, []),
  721. {allType, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55};
  722. decodeBin(1001, LeftBin0) ->
  723. <<Len1:16/big-unsigned, V1:Len1/binary, LeftBin1/binary>> = LeftBin0,
  724. <<V2:32/big-signed, LeftBin2/binary>> = LeftBin1,
  725. <<Len2:16/big-unsigned, V3:Len2/binary, LeftBin3/binary>> = LeftBin2,
  726. <<Len3:16/big-unsigned, LeftBin4/binary>> = LeftBin3,
  727. {V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []),
  728. {person1, V1, V2, V3, V4};
  729. decodeBin(_, _) ->
  730. {{}, <<>>}.