erlAarango 二进制序列化库
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1549 строки
52 KiB

4 лет назад
  1. -module(eVPack).
  2. -include("eVPack.hrl").
  3. -export([
  4. encode/1
  5. , encode/3
  6. , decode/1
  7. , encodeAtom/1
  8. , encodeFloat/1
  9. , encodeInteger/1
  10. , encodeList/3
  11. , encodeMap/3
  12. , encodeString/1
  13. , encoder/3
  14. , buildIndexTable_1/2
  15. , buildIndexTable_2/2
  16. , buildIndexTable_4/2
  17. , buildIndexTable_8/2
  18. ]).
  19. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 编码 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20. -spec encode(term()) -> {ok, vpack()} | {error, any()}.
  21. encode(Term) -> encode(Term, ?VpDefArrOpt, ?VpDefObjOpt).
  22. -spec encode(term(), vpOpt(), vpOpt()) -> {ok, vpack()} | {error, any()}.
  23. encode(Term, ArrOpt, ObjOpt) ->
  24. try {_v@1, __size@1} = encoder(Term, ArrOpt, ObjOpt),
  25. {ok, _v@1}
  26. catch
  27. throw:Class -> {error, Class}
  28. end.
  29. encoder(Map, ArrOpt, ObjOpt) when erlang:is_map(Map) ->
  30. encodeMap(ObjOpt, Map, ArrOpt);
  31. encoder(Atom, _, _) when erlang:is_atom(Atom) ->
  32. encodeAtom(Atom);
  33. encoder(Binary, _, _) when erlang:is_binary(Binary) ->
  34. encodeString(Binary);
  35. encoder(Integer, _, _) when erlang:is_integer(Integer) ->
  36. encodeInteger(Integer);
  37. encoder(Float, _, _) when erlang:is_float(Float) ->
  38. encodeFloat(Float);
  39. encoder(List, ArrOpt, ObjOpt) when erlang:is_list(List) ->
  40. encodeList(ArrOpt, List, ObjOpt);
  41. encoder(_Value, _, _) ->
  42. {error, dataType(_Value)}.
  43. dataType(Data) when is_list(Data) -> list;
  44. dataType(Data) when is_integer(Data) -> integer;
  45. dataType(Data) when is_binary(Data) -> binary;
  46. dataType(Data) when is_function(Data) -> function;
  47. dataType(Data) when is_tuple(Data) -> tuple;
  48. dataType(Data) when is_atom(Data) -> atom;
  49. dataType(Data) when is_bitstring(Data) -> bitstring;
  50. dataType(Data) when is_boolean(Data) -> boolean;
  51. dataType(Data) when is_float(Data) -> float;
  52. dataType(Data) when is_number(Data) -> number;
  53. dataType(Data) when is_pid(Data) -> pid;
  54. dataType(Data) when is_port(Data) -> port;
  55. dataType(_Data) -> not_know.
  56. encodeAtom(undefined) -> {<<24/integer>>, 1};
  57. encodeAtom(false) -> {<<25/integer>>, 1};
  58. encodeAtom(true) -> {<<26/integer>>, 1};
  59. encodeAtom(minKey) -> {<<30/integer>>, 1};
  60. encodeAtom(maxKey) -> {<<31/integer>>, 1};
  61. encodeAtom(illegal) -> {<<23/integer>>, 1};
  62. encodeAtom(null) -> {<<24/integer>>, 1};
  63. encodeAtom(Atom) ->
  64. encodeString(erlang:atom_to_binary(Atom, utf8)).
  65. encodeInteger(0) ->
  66. {<<48/integer>>, 1};
  67. encodeInteger(1) ->
  68. {<<49/integer>>, 1};
  69. encodeInteger(2) ->
  70. {<<50/integer>>, 1};
  71. encodeInteger(3) ->
  72. {<<51/integer>>, 1};
  73. encodeInteger(4) ->
  74. {<<52/integer>>, 1};
  75. encodeInteger(5) ->
  76. {<<53/integer>>, 1};
  77. encodeInteger(6) ->
  78. {<<54/integer>>, 1};
  79. encodeInteger(7) ->
  80. {<<55/integer>>, 1};
  81. encodeInteger(8) ->
  82. {<<56/integer>>, 1};
  83. encodeInteger(9) ->
  84. {<<57/integer>>, 1};
  85. encodeInteger(-6) ->
  86. {<<58/integer>>, 1};
  87. encodeInteger(-5) ->
  88. {<<59/integer>>, 1};
  89. encodeInteger(-4) ->
  90. {<<60/integer>>, 1};
  91. encodeInteger(-3) ->
  92. {<<61/integer>>, 1};
  93. encodeInteger(-2) ->
  94. {<<62/integer>>, 1};
  95. encodeInteger(-1) ->
  96. {<<63/integer>>, 1};
  97. encodeInteger(Integer) ->
  98. if
  99. Integer < -9223372036854775808 ->
  100. erlang:throw(<<"Cannot encode integers less than -9223372036854775808">>);
  101. Integer < -36028797018963968 ->
  102. {<<39/integer, Integer:64/integer-little-signed>>, 9};
  103. Integer < -140737488355328 ->
  104. {<<38/integer, Integer:56/integer-little-signed>>, 8};
  105. Integer < -549755813888 ->
  106. {<<37/integer, Integer:48/integer-little-signed>>, 7};
  107. Integer < -2147483648 ->
  108. {<<36/integer, Integer:40/integer-little-signed>>, 6};
  109. Integer < -8388608 ->
  110. {<<35/integer, Integer:32/integer-little-signed>>, 5};
  111. Integer < -32768 ->
  112. {<<34/integer, Integer:24/integer-little-signed>>, 4};
  113. Integer < -128 ->
  114. {<<33/integer, Integer:16/integer-little-signed>>, 3};
  115. Integer < 0 ->
  116. {<<32/integer, Integer:8/integer-little-unsigned>>, 2};
  117. Integer < 256 ->
  118. {<<40/integer, Integer:8/integer-little-unsigned>>, 2};
  119. Integer < 65536 ->
  120. {<<41/integer, Integer:16/integer-little-unsigned>>, 3};
  121. Integer < 16777216 ->
  122. {<<42/integer, Integer:24/integer-little-unsigned>>, 4};
  123. Integer < 4294967296 ->
  124. {<<43/integer, Integer:32/integer-little-unsigned>>, 5};
  125. Integer < 1099511627776 ->
  126. {<<44/integer, Integer:40/integer-little-unsigned>>, 6};
  127. Integer < 281474976710656 ->
  128. {<<45/integer, Integer:48/integer-little-unsigned>>, 7};
  129. Integer < 72057594037927936 ->
  130. {<<46/integer, Integer:56/integer-little-unsigned>>, 8};
  131. Integer < 18446744073709551616 ->
  132. {<<47/integer, Integer:64/integer-little-unsigned>>, 9};
  133. true ->
  134. erlang:throw(<<"Cannot encode integers greater than 18446744073709551616">>)
  135. end.
  136. encodeFloat(Float) ->
  137. {<<27/integer, Float:64/float-little>>, 9}.
  138. encodeString(BinStr) ->
  139. StrSize = erlang:byte_size(BinStr),
  140. if
  141. StrSize =< 126 ->
  142. {<<(StrSize + 64)/integer, BinStr/binary>>, StrSize + 1};
  143. StrSize < 256 ->
  144. {<<192/integer, StrSize:8/integer-little-unsigned, BinStr/binary>>, StrSize + 2};
  145. StrSize < 65536 ->
  146. {<<193/integer, StrSize:16/integer-little-unsigned, BinStr/binary>>, StrSize + 3};
  147. StrSize < 16777216 ->
  148. {<<194/integer, StrSize:24/integer-little-unsigned, BinStr/binary>>, StrSize + 4};
  149. StrSize < 4294967296 ->
  150. {<<195/integer, StrSize:32/integer-little-unsigned, BinStr/binary>>, StrSize + 5};
  151. StrSize < 1099511627776 ->
  152. {<<196/integer, StrSize:40/integer-little-unsigned, BinStr/binary>>, StrSize + 6};
  153. StrSize < 281474976710656 ->
  154. {<<197/integer, StrSize:48/integer-little-unsigned, BinStr/binary>>, StrSize + 7};
  155. StrSize < 72057594037927936 ->
  156. {<<198/integer, StrSize:56/integer-little-unsigned, BinStr/binary>>, StrSize + 8};
  157. StrSize < 18446744073709551616 ->
  158. {<<199/integer, StrSize:64/integer-little-unsigned, BinStr/binary>>, StrSize + 9};
  159. true ->
  160. {<<191/integer, StrSize:64/integer-little-unsigned, BinStr/binary>>, StrSize + 9}
  161. end.
  162. doEncodeMap(Iterator, ArrOpt, ObjOpt, AccList, SumSize) ->
  163. case maps:next(Iterator) of
  164. {Key, Value, NextIter} ->
  165. {KeyEn, KeySize} = encodeString(asKey(Key)),
  166. {ValueEn, ValueSize} = encoder(Value, ArrOpt, ObjOpt),
  167. doEncodeMap(NextIter, ArrOpt, ObjOpt, [ValueEn, KeyEn | AccList], SumSize + KeySize + ValueSize);
  168. none ->
  169. {AccList, SumSize}
  170. end.
  171. doEncodeMap(Iterator, ArrOpt, ObjOpt, AccList, Offsets, SumSize) ->
  172. case maps:next(Iterator) of
  173. {Key, Value, NextIter} ->
  174. {KeyEn, KeySize} = encodeString(asKey(Key)),
  175. {ValueEn, ValueSize} = encoder(Value, ArrOpt, ObjOpt),
  176. doEncodeMap(NextIter, ArrOpt, ObjOpt, [ValueEn, KeyEn | AccList], [SumSize | Offsets], SumSize + KeySize + ValueSize);
  177. none ->
  178. {AccList, Offsets, SumSize}
  179. end.
  180. doEncodeMap([], _Map, _ArrOpt, _ObjOpt, AccList, Offsets, SumSize) ->
  181. {AccList, Offsets, SumSize};
  182. doEncodeMap([OneKeys | Left], Map, ArrOpt, ObjOpt, AccList, Offsets, SumSize) ->
  183. case Map of
  184. #{OneKeys := Value} ->
  185. {KeyEn, KeySize} = encodeString(OneKeys),
  186. {ValueEn, ValueSize} = encoder(Value, ArrOpt, ObjOpt),
  187. doEncodeMap(Left, Map, ArrOpt, ObjOpt, [ValueEn, KeyEn | AccList], [SumSize | Offsets], SumSize + KeySize + ValueSize);
  188. _ ->
  189. AtomKey = binary_to_atom(OneKeys, utf8),
  190. case Map of
  191. #{AtomKey := Value} ->
  192. {KeyEn, KeySize} = encodeString(OneKeys),
  193. {ValueEn, ValueSize} = encoder(Value, ArrOpt, ObjOpt),
  194. doEncodeMap(Left, Map, ArrOpt, ObjOpt, [ValueEn, KeyEn | AccList], [SumSize | Offsets], SumSize + KeySize + ValueSize);
  195. _ ->
  196. erlang:throw(<<"doEncodeMap not found the value ", OneKeys/binary>>)
  197. end
  198. end.
  199. encodeMap(?vpObjNcNs, Map, ArrOpt) ->
  200. MapSize = erlang:map_size(Map),
  201. case MapSize == 0 of
  202. true ->
  203. {<<10/integer>>, 1};
  204. _ ->
  205. {AccList, Offsets, SumSize} = doEncodeMap(maps:iterator(Map), ArrOpt, ?vpObjNcNs, [], [], 0),
  206. IoData = lists:reverse(AccList),
  207. case MapSize >= 1000 of
  208. false ->
  209. encodeUnSortMapIndexTable(IoData, MapSize, Offsets, SumSize);
  210. _ ->
  211. encodeUnSortMapIndexTable(erlang:iolist_to_binary(IoData), MapSize, Offsets, SumSize)
  212. end
  213. end;
  214. encodeMap(?vpObjYc, Map, ArrOpt) ->
  215. MapSize = erlang:map_size(Map),
  216. case MapSize == 0 of
  217. true ->
  218. {<<10/integer>>, 1};
  219. _ ->
  220. {AccList, SumSize} = doEncodeMap(maps:iterator(Map), ArrOpt, ?vpObjYc, [], 0),
  221. IoData = lists:reverse(AccList),
  222. case MapSize >= 1000 of
  223. false ->
  224. encodeCompactData(<<20/integer>>, IoData, SumSize, MapSize);
  225. _ ->
  226. encodeCompactData(<<20/integer>>, erlang:iolist_to_binary(IoData), SumSize, MapSize)
  227. end
  228. end;
  229. encodeMap(?vpObjNcYs, Map, ArrOpt) ->
  230. MapSize = erlang:map_size(Map),
  231. case MapSize == 0 of
  232. true ->
  233. {<<10/integer>>, 1};
  234. _ ->
  235. Keys = maps:keys(Map),
  236. StrKeys = [asKey(OneKey) || OneKey <- Keys],
  237. {AccList, Offsets, SumSize} = doEncodeMap(lists:sort(StrKeys), Map, ArrOpt, ?vpObjNcYs, [], [], 0),
  238. IoData = lists:reverse(AccList),
  239. case MapSize >= 1000 of
  240. false ->
  241. encodeSortMapIndexTable(IoData, MapSize, Offsets, SumSize);
  242. _ ->
  243. encodeSortMapIndexTable(erlang:iolist_to_binary(IoData), MapSize, Offsets, SumSize)
  244. end
  245. end.
  246. encodeSortMapIndexTable(IoData, Count, Offsets, SumSize) ->
  247. TemSize = SumSize + Count,
  248. if
  249. TemSize < 253 ->
  250. AllSize = TemSize + 3,
  251. Header = <<11/integer, AllSize:8/integer-unsigned, Count:8/integer-unsigned>>,
  252. {[Header, IoData, buildIndexTable_1(Offsets, 3)], AllSize};
  253. TemSize + Count < 65531 ->
  254. AllSize = TemSize + Count + 5,
  255. Header = <<12/integer, AllSize:16/integer-little-unsigned, Count:16/integer-little-unsigned>>,
  256. {[Header, IoData, buildIndexTable_2(Offsets, 5)], AllSize};
  257. TemSize + Count * 3 < 4294967287 ->
  258. AllSize = TemSize + Count * 3 + 9,
  259. Header = <<13/integer, AllSize:32/integer-little-unsigned, Count:32/integer-little-unsigned>>,
  260. {[Header, IoData, buildIndexTable_4(Offsets, 9)], AllSize};
  261. TemSize + Count * 7 < 18446744073709551599 ->
  262. AllSize = TemSize + Count * 7 + 17,
  263. Header = <<14/integer, AllSize:64/integer-little-unsigned>>,
  264. {[Header, IoData, buildIndexTable_8(Offsets, 17), <<Count:64/integer-little-unsigned>>], AllSize};
  265. true ->
  266. erlang:throw(<<"too much size">>)
  267. end.
  268. encodeUnSortMapIndexTable(IoData, Count, Offsets, SumSize) ->
  269. TemSize = SumSize + Count,
  270. if
  271. TemSize < 253 ->
  272. AllSize = TemSize + 3,
  273. Header = <<15/integer, AllSize:8/integer-unsigned, Count:8/integer-unsigned>>,
  274. {[Header, IoData, buildIndexTable_1(Offsets, 3)], AllSize};
  275. TemSize + Count < 65531 ->
  276. AllSize = TemSize + Count + 5,
  277. Header = <<16/integer, AllSize:16/integer-little-unsigned, Count:16/integer-little-unsigned>>,
  278. {[Header, IoData, buildIndexTable_2(Offsets, 5)], AllSize};
  279. TemSize + Count * 3 < 4294967287 ->
  280. AllSize = TemSize + Count * 3 + 9,
  281. Header = <<17/integer, AllSize:32/integer-little-unsigned, Count:32/integer-little-unsigned>>,
  282. {[Header, IoData, buildIndexTable_4(Offsets, 9)], AllSize};
  283. TemSize + Count * 7 < 18446744073709551599 ->
  284. AllSize = TemSize + Count * 7 + 17,
  285. Header = <<18/integer, AllSize:64/integer-little-unsigned>>,
  286. {[Header, IoData, buildIndexTable_8(Offsets, 17), <<Count:64/integer-little-unsigned>>], AllSize};
  287. true ->
  288. erlang:throw(<<"encode map too much size">>)
  289. end.
  290. buildIndexTable_1(Offsets, StartSize) ->
  291. <<<<(OneOff + StartSize):1/integer-little-unsigned-unit:8>> || OneOff <- lists:reverse(Offsets)>>.
  292. buildIndexTable_2(Offsets, StartSize) ->
  293. <<<<(OneOff + StartSize):2/integer-little-unsigned-unit:8>> || OneOff <- lists:reverse(Offsets)>>.
  294. buildIndexTable_4(Offsets, StartSize) ->
  295. <<<<(OneOff + StartSize):4/integer-little-unsigned-unit:8>> || OneOff <- lists:reverse(Offsets)>>.
  296. buildIndexTable_8(Offsets, StartSize) ->
  297. <<<<(OneOff + StartSize):8/integer-little-unsigned-unit:8>> || OneOff <- lists:reverse(Offsets)>>.
  298. compactIntegerList(Integer, AccList) ->
  299. case Integer < 128 of
  300. true ->
  301. [<<Integer:8/integer-unsigned>> | AccList];
  302. _ ->
  303. TemInteger = Integer band 127 bor 128,
  304. NewInteger = Integer bsr 7,
  305. compactIntegerList(NewInteger, [<<TemInteger:8/integer-unsigned>> | AccList])
  306. end.
  307. compactInteger(Value, Reverse) ->
  308. CompactList = compactIntegerList(Value, []),
  309. case Reverse of
  310. false -> lists:reverse(CompactList);
  311. _ -> CompactList
  312. end.
  313. compactSize(AllSize) ->
  314. TemByte = erlang:ceil(AllSize / 128),
  315. FinalSize = AllSize + TemByte,
  316. LastSize =
  317. case TemByte == erlang:ceil(FinalSize / 128) of
  318. false -> FinalSize + 1;
  319. true -> FinalSize
  320. end,
  321. {compactInteger(LastSize, false), LastSize}.
  322. encodeCompactData(Type, IoData, SumSize, Count) ->
  323. CompactList = compactInteger(Count, true),
  324. AllSize = SumSize + 1 + lists:length(CompactList),
  325. {TotalSize, FinalSize} = compactSize(AllSize),
  326. {[Type, TotalSize, IoData | CompactList], FinalSize}.
  327. asKey(Value) when erlang:is_atom(Value) -> erlang:atom_to_binary(Value, utf8);
  328. asKey(Value) when erlang:is_binary(Value) -> Value;
  329. asKey(_Value) -> erlang:error(<<"Invalid key">>).
  330. doEncodeList([], _ArrOpt, _ObjOpt, AccList, SumSize, Count) ->
  331. {AccList, SumSize, Count};
  332. doEncodeList([One | Left], ArrOpt, ObjOpt, AccList, SumSize, Count) ->
  333. {ValueEn, ValueSize} = encoder(One, ArrOpt, ObjOpt),
  334. doEncodeList(Left, ArrOpt, ObjOpt, [ValueEn | AccList], SumSize + ValueSize, Count + 1).
  335. doEncodeList([], _ArrOpt, _ObjOpt, AccList, Offsets, SumSize, Count, SizeOrIsNot) ->
  336. {AccList, Offsets, SumSize, Count, SizeOrIsNot};
  337. doEncodeList([One | Left], ArrOpt, ObjOpt, AccList, Offsets, SumSize, Count, SizeOrIsNot) ->
  338. {ValueEn, ValueSize} = encoder(One, ArrOpt, ObjOpt),
  339. case SizeOrIsNot of
  340. true ->
  341. doEncodeList(Left, ArrOpt, ObjOpt, [ValueEn | AccList], [SumSize | Offsets], ValueSize + SumSize, Count + 1, SizeOrIsNot);
  342. init ->
  343. doEncodeList(Left, ArrOpt, ObjOpt, [ValueEn | AccList], [SumSize | Offsets], ValueSize + SumSize, Count + 1, ValueSize);
  344. _ ->
  345. doEncodeList(Left, ArrOpt, ObjOpt, [ValueEn | AccList], [SumSize | Offsets], ValueSize + SumSize, Count + 1, ValueSize =/= SizeOrIsNot orelse SizeOrIsNot)
  346. end.
  347. encodeList(?vpArrNc, List, ObjOpt) ->
  348. case List of
  349. [] ->
  350. {<<1/integer>>, 1};
  351. _ ->
  352. {AccList, Offsets, SumSize, Count, IsNotSameSize} = doEncodeList(List, ?vpArrNc, ObjOpt, [], [], 0, 0, init),
  353. IoData = lists:reverse(AccList),
  354. case Count >= 1000 of
  355. false ->
  356. case IsNotSameSize of
  357. true ->
  358. encodeListWithoutIndexTable(IoData, SumSize);
  359. _ ->
  360. encodeListWithIndexTable(IoData, Count, Offsets, SumSize)
  361. end;
  362. _ ->
  363. case IsNotSameSize of
  364. true ->
  365. encodeListWithoutIndexTable(erlang:iolist_to_binary(IoData), SumSize);
  366. _ ->
  367. encodeListWithIndexTable(erlang:iolist_to_binary(IoData), Count, Offsets, SumSize)
  368. end
  369. end
  370. end;
  371. encodeList(?vpArrYc, List, ObjOpt) ->
  372. case List of
  373. [] ->
  374. {<<1/integer>>, 1};
  375. _ ->
  376. {AccList, SumSize, Count} = doEncodeList(List, ?vpArrYc, ObjOpt, [], 0, 0),
  377. IoData = lists:reverse(AccList),
  378. case Count >= 1000 of
  379. false ->
  380. encodeCompactData(<<19/integer>>, IoData, SumSize, Count);
  381. _ ->
  382. encodeCompactData(<<19/integer>>, erlang:iolist_to_binary(IoData), SumSize, Count)
  383. end
  384. end.
  385. encodeListWithoutIndexTable(IoData, SumSize) ->
  386. if
  387. SumSize < 254 ->
  388. AllSize = SumSize + 2,
  389. Header = <<2/integer, AllSize:1/integer-little-unsigned-unit:8>>,
  390. {[Header, IoData], AllSize};
  391. SumSize < 65533 ->
  392. AllSize = SumSize + 3,
  393. Header = <<3/integer, AllSize:2/integer-little-unsigned-unit:8>>,
  394. {[Header, IoData], AllSize};
  395. SumSize < 4294967291 ->
  396. AllSize = SumSize + 5,
  397. Header = <<4/integer, AllSize:4/integer-little-unsigned-unit:8>>,
  398. {[Header, IoData], AllSize};
  399. SumSize < 18446744073709551607 ->
  400. AllSize = SumSize + 9,
  401. Header = <<5/integer, AllSize:8/integer-little-unsigned-unit:8>>,
  402. {[Header, IoData], AllSize};
  403. true ->
  404. erlang:throw(<<"encode map too much size">>)
  405. end.
  406. encodeListWithIndexTable(IoData, Count, Offsets, SumSize) ->
  407. TemSize = SumSize + Count,
  408. if
  409. TemSize < 253 ->
  410. AllSize = TemSize + 3,
  411. Header = <<6/integer, AllSize:8/integer-unsigned, Count:8/integer-unsigned>>,
  412. {[Header, IoData, buildIndexTable_1(Offsets, 3)], AllSize};
  413. TemSize + Count < 65531 ->
  414. AllSize = TemSize + Count + 5,
  415. Header = <<7/integer, AllSize:16/integer-little-unsigned, Count:16/integer-little-unsigned>>,
  416. {[Header, IoData, buildIndexTable_2(Offsets, 5)], AllSize};
  417. TemSize + Count * 3 < 4294967287 ->
  418. AllSize = TemSize + Count * 3 + 9,
  419. Header = <<8/integer, AllSize:32/integer-little-unsigned, Count:32/integer-little-unsigned>>,
  420. {[Header, IoData, buildIndexTable_4(Offsets, 9)], AllSize};
  421. TemSize + Count * 7 < 18446744073709551599 ->
  422. AllSize = TemSize + Count * 7 + 17,
  423. Header = <<9/integer, AllSize:64/integer-little-unsigned>>,
  424. {[Header, IoData, buildIndexTable_8(Offsets, 17), <<Count:64/integer-little-unsigned>>], AllSize};
  425. true ->
  426. erlang:throw(<<"encode map too much size">>)
  427. end.
  428. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% decode %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  429. decode(DataBin) ->
  430. try decoder(DataBin) of
  431. {Value, <<>>} -> {ok, Value};
  432. {Value, Tail} -> {ok, {Value, Tail}}
  433. catch
  434. Class:Err:Strace ->
  435. {error, Class, Err, Strace}
  436. end.
  437. decoder(DataBin) ->
  438. case DataBin of
  439. <<Type/integer, RestBin/bitstring>> ->
  440. decoder(Type, RestBin);
  441. _ ->
  442. erlang:throw(unexpected_end)
  443. end.
  444. decoder(0, RestBin) ->
  445. erlang:throw({unsupported_type, RestBin});
  446. decoder(1, RestBin) ->
  447. {[], RestBin};
  448. decoder(2, RestBin) ->
  449. parseArrayWithoutIndexTable(2, RestBin);
  450. decoder(3, RestBin) ->
  451. parseArrayWithoutIndexTable(3, RestBin);
  452. decoder(4, RestBin) ->
  453. parseArrayWithoutIndexTable(4, RestBin);
  454. decoder(5, RestBin) ->
  455. parseArrayWithoutIndexTable(5, RestBin);
  456. decoder(6, RestBin) ->
  457. parseArrayWithIndexTable(6, RestBin);
  458. decoder(7, RestBin) ->
  459. parseArrayWithIndexTable(7, RestBin);
  460. decoder(8, RestBin) ->
  461. parseArrayWithIndexTable(8, RestBin);
  462. decoder(9, RestBin) ->
  463. parseArrayWithIndexTable(9, RestBin);
  464. decoder(10, RestBin) ->
  465. {#{}, RestBin};
  466. decoder(11, RestBin) ->
  467. parseObject(11, RestBin);
  468. decoder(12, RestBin) ->
  469. parseObject(12, RestBin);
  470. decoder(13, RestBin) ->
  471. parseObject(13, RestBin);
  472. decoder(14, RestBin) ->
  473. parseObject(14, RestBin);
  474. decoder(15, RestBin) ->
  475. parseObject(15, RestBin);
  476. decoder(16, RestBin) ->
  477. parseObject(16, RestBin);
  478. decoder(17, RestBin) ->
  479. parseObject(17, RestBin);
  480. decoder(18, RestBin) ->
  481. parseObject(18, RestBin);
  482. decoder(19, RestBin) ->
  483. parseCompactArray(RestBin);
  484. decoder(20, RestBin) ->
  485. parseCompactObject(RestBin);
  486. decoder(21, _RestBin) ->
  487. erlang:throw({unsupported_type, 21});
  488. decoder(22, _RestBin) ->
  489. erlang:throw({unsupported_type, 22});
  490. decoder(23, RestBin) ->
  491. {illegal, RestBin};
  492. decoder(24, RestBin) -> {undefined, RestBin};
  493. decoder(25, RestBin) -> {false, RestBin};
  494. decoder(26, RestBin) -> {true, RestBin};
  495. decoder(27, RestBin) ->
  496. <<Float:64/float-little, LeftBin/bitstring>> = RestBin,
  497. {Float, LeftBin};
  498. decoder(28, RestBin) ->
  499. <<TimeMs:64/integer-little-unsigned, LeftBin/bitstring>> = RestBin,
  500. {TimeMs, LeftBin};
  501. decoder(29, _RestBin) ->
  502. erlang:throw({unsupported_type, 29});
  503. decoder(30, RestBin) ->
  504. {min_key, RestBin};
  505. decoder(31, RestBin) ->
  506. {max_key, RestBin};
  507. decoder(32, RestBin) ->
  508. parseInt(1, RestBin);
  509. decoder(33, RestBin) ->
  510. parseInt(2, RestBin);
  511. decoder(34, RestBin) ->
  512. parseInt(3, RestBin);
  513. decoder(35, RestBin) ->
  514. parseInt(4, RestBin);
  515. decoder(36, RestBin) ->
  516. parseInt(5, RestBin);
  517. decoder(37, RestBin) ->
  518. parseInt(6, RestBin);
  519. decoder(38, RestBin) ->
  520. parseInt(7, RestBin);
  521. decoder(39, RestBin) ->
  522. parseInt(8, RestBin);
  523. decoder(40, RestBin) ->
  524. parseUint(1, RestBin);
  525. decoder(41, RestBin) ->
  526. parseUint(2, RestBin);
  527. decoder(42, RestBin) ->
  528. parseUint(3, RestBin);
  529. decoder(43, RestBin) ->
  530. parseUint(4, RestBin);
  531. decoder(44, RestBin) ->
  532. parseUint(5, RestBin);
  533. decoder(45, RestBin) ->
  534. parseUint(6, RestBin);
  535. decoder(46, RestBin) ->
  536. parseUint(7, RestBin);
  537. decoder(47, RestBin) ->
  538. parseUint(8, RestBin);
  539. decoder(48, RestBin) ->
  540. {0, RestBin};
  541. decoder(49, RestBin) ->
  542. {1, RestBin};
  543. decoder(50, RestBin) ->
  544. {2, RestBin};
  545. decoder(51, RestBin) ->
  546. {3, RestBin};
  547. decoder(52, RestBin) ->
  548. {4, RestBin};
  549. decoder(53, RestBin) ->
  550. {5, RestBin};
  551. decoder(54, RestBin) ->
  552. {6, RestBin};
  553. decoder(55, RestBin) ->
  554. {7, RestBin};
  555. decoder(56, RestBin) ->
  556. {8, RestBin};
  557. decoder(57, RestBin) ->
  558. {9, RestBin};
  559. decoder(58, RestBin) ->
  560. {-6, RestBin};
  561. decoder(59, RestBin) ->
  562. {-5, RestBin};
  563. decoder(60, RestBin) ->
  564. {-4, RestBin};
  565. decoder(61, RestBin) ->
  566. {-3, RestBin};
  567. decoder(62, RestBin) ->
  568. {-2, RestBin};
  569. decoder(63, RestBin) ->
  570. {-1, RestBin};
  571. decoder(64, RestBin) ->
  572. {<<>>, RestBin};
  573. decoder(65, RestBin) ->
  574. <<BinStr:1/binary, LeftBin/bitstring>> = RestBin,
  575. {BinStr, LeftBin};
  576. decoder(66, RestBin) ->
  577. <<BinStr:2/binary, LeftBin/bitstring>> = RestBin,
  578. {BinStr, LeftBin};
  579. decoder(67, RestBin) ->
  580. <<BinStr:3/binary, LeftBin/bitstring>> = RestBin,
  581. {BinStr, LeftBin};
  582. decoder(68, RestBin) ->
  583. <<BinStr:4/binary, LeftBin/bitstring>> = RestBin,
  584. {BinStr, LeftBin};
  585. decoder(69, RestBin) ->
  586. <<BinStr:5/binary, LeftBin/bitstring>> = RestBin,
  587. {BinStr, LeftBin};
  588. decoder(70, RestBin) ->
  589. <<BinStr:6/binary, LeftBin/bitstring>> = RestBin,
  590. {BinStr, LeftBin};
  591. decoder(71, RestBin) ->
  592. <<BinStr:7/binary, LeftBin/bitstring>> = RestBin,
  593. {BinStr, LeftBin};
  594. decoder(72, RestBin) ->
  595. <<BinStr:8/binary, LeftBin/bitstring>> = RestBin,
  596. {BinStr, LeftBin};
  597. decoder(73, RestBin) ->
  598. <<BinStr:9/binary, LeftBin/bitstring>> = RestBin,
  599. {BinStr, LeftBin};
  600. decoder(74, RestBin) ->
  601. <<BinStr:10/binary, LeftBin/bitstring>> = RestBin,
  602. {BinStr, LeftBin};
  603. decoder(75, RestBin) ->
  604. <<BinStr:11/binary, LeftBin/bitstring>> = RestBin,
  605. {BinStr, LeftBin};
  606. decoder(76, RestBin) ->
  607. <<BinStr:12/binary, LeftBin/bitstring>> = RestBin,
  608. {BinStr, LeftBin};
  609. decoder(77, RestBin) ->
  610. <<BinStr:13/binary, LeftBin/bitstring>> = RestBin,
  611. {BinStr, LeftBin};
  612. decoder(78, RestBin) ->
  613. <<BinStr:14/binary, LeftBin/bitstring>> = RestBin,
  614. {BinStr, LeftBin};
  615. decoder(79, RestBin) ->
  616. <<BinStr:15/binary, LeftBin/bitstring>> = RestBin,
  617. {BinStr, LeftBin};
  618. decoder(80, RestBin) ->
  619. <<BinStr:16/binary, LeftBin/bitstring>> = RestBin,
  620. {BinStr, LeftBin};
  621. decoder(81, RestBin) ->
  622. <<BinStr:17/binary, LeftBin/bitstring>> = RestBin,
  623. {BinStr, LeftBin};
  624. decoder(82, RestBin) ->
  625. <<BinStr:18/binary, LeftBin/bitstring>> = RestBin,
  626. {BinStr, LeftBin};
  627. decoder(83, RestBin) ->
  628. <<BinStr:19/binary, LeftBin/bitstring>> = RestBin,
  629. {BinStr, LeftBin};
  630. decoder(84, RestBin) ->
  631. <<BinStr:20/binary, LeftBin/bitstring>> = RestBin,
  632. {BinStr, LeftBin};
  633. decoder(85, RestBin) ->
  634. <<BinStr:21/binary, LeftBin/bitstring>> = RestBin,
  635. {BinStr, LeftBin};
  636. decoder(86, RestBin) ->
  637. <<BinStr:22/binary, LeftBin/bitstring>> = RestBin,
  638. {BinStr, LeftBin};
  639. decoder(87, RestBin) ->
  640. <<BinStr:23/binary, LeftBin/bitstring>> = RestBin,
  641. {BinStr, LeftBin};
  642. decoder(88, RestBin) ->
  643. <<BinStr:24/binary, LeftBin/bitstring>> = RestBin,
  644. {BinStr, LeftBin};
  645. decoder(89, RestBin) ->
  646. <<BinStr:25/binary, LeftBin/bitstring>> = RestBin,
  647. {BinStr, LeftBin};
  648. decoder(90, RestBin) ->
  649. <<BinStr:26/binary, LeftBin/bitstring>> = RestBin,
  650. {BinStr, LeftBin};
  651. decoder(91, RestBin) ->
  652. <<BinStr:27/binary, LeftBin/bitstring>> = RestBin,
  653. {BinStr, LeftBin};
  654. decoder(92, RestBin) ->
  655. <<BinStr:28/binary, LeftBin/bitstring>> = RestBin,
  656. {BinStr, LeftBin};
  657. decoder(93, RestBin) ->
  658. <<BinStr:29/binary, LeftBin/bitstring>> = RestBin,
  659. {BinStr, LeftBin};
  660. decoder(94, RestBin) ->
  661. <<BinStr:30/binary, LeftBin/bitstring>> = RestBin,
  662. {BinStr, LeftBin};
  663. decoder(95, RestBin) ->
  664. <<BinStr:31/binary, LeftBin/bitstring>> = RestBin,
  665. {BinStr, LeftBin};
  666. decoder(96, RestBin) ->
  667. <<BinStr:32/binary, LeftBin/bitstring>> = RestBin,
  668. {BinStr, LeftBin};
  669. decoder(97, RestBin) ->
  670. <<BinStr:33/binary, LeftBin/bitstring>> = RestBin,
  671. {BinStr, LeftBin};
  672. decoder(98, RestBin) ->
  673. <<BinStr:34/binary, LeftBin/bitstring>> = RestBin,
  674. {BinStr, LeftBin};
  675. decoder(99, RestBin) ->
  676. <<BinStr:35/binary, LeftBin/bitstring>> = RestBin,
  677. {BinStr, LeftBin};
  678. decoder(100, RestBin) ->
  679. <<BinStr:36/binary, LeftBin/bitstring>> = RestBin,
  680. {BinStr, LeftBin};
  681. decoder(101, RestBin) ->
  682. <<BinStr:37/binary, LeftBin/bitstring>> = RestBin,
  683. {BinStr, LeftBin};
  684. decoder(102, RestBin) ->
  685. <<BinStr:38/binary, LeftBin/bitstring>> = RestBin,
  686. {BinStr, LeftBin};
  687. decoder(103, RestBin) ->
  688. <<BinStr:39/binary, LeftBin/bitstring>> = RestBin,
  689. {BinStr, LeftBin};
  690. decoder(104, RestBin) ->
  691. <<BinStr:40/binary, LeftBin/bitstring>> = RestBin,
  692. {BinStr, LeftBin};
  693. decoder(105, RestBin) ->
  694. <<BinStr:41/binary, LeftBin/bitstring>> = RestBin,
  695. {BinStr, LeftBin};
  696. decoder(106, RestBin) ->
  697. <<BinStr:42/binary, LeftBin/bitstring>> = RestBin,
  698. {BinStr, LeftBin};
  699. decoder(107, RestBin) ->
  700. <<BinStr:43/binary, LeftBin/bitstring>> = RestBin,
  701. {BinStr, LeftBin};
  702. decoder(108, RestBin) ->
  703. <<BinStr:44/binary, LeftBin/bitstring>> = RestBin,
  704. {BinStr, LeftBin};
  705. decoder(109, RestBin) ->
  706. <<BinStr:45/binary, LeftBin/bitstring>> = RestBin,
  707. {BinStr, LeftBin};
  708. decoder(110, RestBin) ->
  709. <<BinStr:46/binary, LeftBin/bitstring>> = RestBin,
  710. {BinStr, LeftBin};
  711. decoder(111, RestBin) ->
  712. <<BinStr:47/binary, LeftBin/bitstring>> = RestBin,
  713. {BinStr, LeftBin};
  714. decoder(112, RestBin) ->
  715. <<BinStr:48/binary, LeftBin/bitstring>> = RestBin,
  716. {BinStr, LeftBin};
  717. decoder(113, RestBin) ->
  718. <<BinStr:49/binary, LeftBin/bitstring>> = RestBin,
  719. {BinStr, LeftBin};
  720. decoder(114, RestBin) ->
  721. <<BinStr:50/binary, LeftBin/bitstring>> = RestBin,
  722. {BinStr, LeftBin};
  723. decoder(115, RestBin) ->
  724. <<BinStr:51/binary, LeftBin/bitstring>> = RestBin,
  725. {BinStr, LeftBin};
  726. decoder(116, RestBin) ->
  727. <<BinStr:52/binary, LeftBin/bitstring>> = RestBin,
  728. {BinStr, LeftBin};
  729. decoder(117, RestBin) ->
  730. <<BinStr:53/binary, LeftBin/bitstring>> = RestBin,
  731. {BinStr, LeftBin};
  732. decoder(118, RestBin) ->
  733. <<BinStr:54/binary, LeftBin/bitstring>> = RestBin,
  734. {BinStr, LeftBin};
  735. decoder(119, RestBin) ->
  736. <<BinStr:55/binary, LeftBin/bitstring>> = RestBin,
  737. {BinStr, LeftBin};
  738. decoder(120, RestBin) ->
  739. <<BinStr:56/binary, LeftBin/bitstring>> = RestBin,
  740. {BinStr, LeftBin};
  741. decoder(121, RestBin) ->
  742. <<BinStr:57/binary, LeftBin/bitstring>> = RestBin,
  743. {BinStr, LeftBin};
  744. decoder(122, RestBin) ->
  745. <<BinStr:58/binary, LeftBin/bitstring>> = RestBin,
  746. {BinStr, LeftBin};
  747. decoder(123, RestBin) ->
  748. <<BinStr:59/binary, LeftBin/bitstring>> = RestBin,
  749. {BinStr, LeftBin};
  750. decoder(124, RestBin) ->
  751. <<BinStr:60/binary, LeftBin/bitstring>> = RestBin,
  752. {BinStr, LeftBin};
  753. decoder(125, RestBin) ->
  754. <<BinStr:61/binary, LeftBin/bitstring>> = RestBin,
  755. {BinStr, LeftBin};
  756. decoder(126, RestBin) ->
  757. <<BinStr:62/binary, LeftBin/bitstring>> = RestBin,
  758. {BinStr, LeftBin};
  759. decoder(127, RestBin) ->
  760. <<BinStr:63/binary, LeftBin/bitstring>> = RestBin,
  761. {BinStr, LeftBin};
  762. decoder(128, RestBin) ->
  763. <<BinStr:64/binary, LeftBin/bitstring>> = RestBin,
  764. {BinStr, LeftBin};
  765. decoder(129, RestBin) ->
  766. <<BinStr:65/binary, LeftBin/bitstring>> = RestBin,
  767. RefSize = binary:referenced_byte_size(RestBin),
  768. case RefSize / 65 > ?VpBinaryCopyRatio of
  769. true ->
  770. {binary:copy(BinStr), LeftBin};
  771. _ ->
  772. {BinStr, LeftBin}
  773. end;
  774. decoder(130, RestBin) ->
  775. <<BinStr:66/binary, LeftBin/bitstring>> = RestBin,
  776. RefSize = binary:referenced_byte_size(RestBin),
  777. case RefSize / 66 > ?VpBinaryCopyRatio of
  778. true ->
  779. {binary:copy(BinStr), LeftBin};
  780. _ ->
  781. {BinStr, LeftBin}
  782. end;
  783. decoder(131, RestBin) ->
  784. <<BinStr:67/binary, LeftBin/bitstring>> = RestBin,
  785. RefSize = binary:referenced_byte_size(RestBin),
  786. case RefSize / 67 > ?VpBinaryCopyRatio of
  787. true ->
  788. {binary:copy(BinStr), LeftBin};
  789. _ ->
  790. {BinStr, LeftBin}
  791. end;
  792. decoder(132, RestBin) ->
  793. <<BinStr:68/binary, LeftBin/bitstring>> = RestBin,
  794. RefSize = binary:referenced_byte_size(RestBin),
  795. case RefSize / 68 > ?VpBinaryCopyRatio of
  796. true ->
  797. {binary:copy(BinStr), LeftBin};
  798. _ ->
  799. {BinStr, LeftBin}
  800. end;
  801. decoder(133, RestBin) ->
  802. <<BinStr:69/binary, LeftBin/bitstring>> = RestBin,
  803. RefSize = binary:referenced_byte_size(RestBin),
  804. case RefSize / 69 > ?VpBinaryCopyRatio of
  805. true ->
  806. {binary:copy(BinStr), LeftBin};
  807. _ ->
  808. {BinStr, LeftBin}
  809. end;
  810. decoder(134, RestBin) ->
  811. <<BinStr:70/binary, LeftBin/bitstring>> = RestBin,
  812. RefSize = binary:referenced_byte_size(RestBin),
  813. case RefSize / 70 > ?VpBinaryCopyRatio of
  814. true ->
  815. {binary:copy(BinStr), LeftBin};
  816. _ ->
  817. {BinStr, LeftBin}
  818. end;
  819. decoder(135, RestBin) ->
  820. <<BinStr:71/binary, LeftBin/bitstring>> = RestBin,
  821. RefSize = binary:referenced_byte_size(RestBin),
  822. case RefSize / 71 > ?VpBinaryCopyRatio of
  823. true ->
  824. {binary:copy(BinStr), LeftBin};
  825. _ ->
  826. {BinStr, LeftBin}
  827. end;
  828. decoder(136, RestBin) ->
  829. <<BinStr:72/binary, LeftBin/bitstring>> = RestBin,
  830. RefSize = binary:referenced_byte_size(RestBin),
  831. case RefSize / 72 > ?VpBinaryCopyRatio of
  832. true ->
  833. {binary:copy(BinStr), LeftBin};
  834. _ ->
  835. {BinStr, LeftBin}
  836. end;
  837. decoder(137, RestBin) ->
  838. <<BinStr:73/binary, LeftBin/bitstring>> = RestBin,
  839. RefSize = binary:referenced_byte_size(RestBin),
  840. case RefSize / 73 > ?VpBinaryCopyRatio of
  841. true ->
  842. {binary:copy(BinStr), LeftBin};
  843. _ ->
  844. {BinStr, LeftBin}
  845. end;
  846. decoder(138, RestBin) ->
  847. <<BinStr:74/binary, LeftBin/bitstring>> = RestBin,
  848. RefSize = binary:referenced_byte_size(RestBin),
  849. case RefSize / 74 > ?VpBinaryCopyRatio of
  850. true ->
  851. {binary:copy(BinStr), LeftBin};
  852. _ ->
  853. {BinStr, LeftBin}
  854. end;
  855. decoder(139, RestBin) ->
  856. <<BinStr:75/binary, LeftBin/bitstring>> = RestBin,
  857. RefSize = binary:referenced_byte_size(RestBin),
  858. case RefSize / 75 > ?VpBinaryCopyRatio of
  859. true ->
  860. {binary:copy(BinStr), LeftBin};
  861. _ ->
  862. {BinStr, LeftBin}
  863. end;
  864. decoder(140, RestBin) ->
  865. <<BinStr:76/binary, LeftBin/bitstring>> = RestBin,
  866. RefSize = binary:referenced_byte_size(RestBin),
  867. case RefSize / 76 > ?VpBinaryCopyRatio of
  868. true ->
  869. {binary:copy(BinStr), LeftBin};
  870. _ ->
  871. {BinStr, LeftBin}
  872. end;
  873. decoder(141, RestBin) ->
  874. <<BinStr:77/binary, LeftBin/bitstring>> = RestBin,
  875. RefSize = binary:referenced_byte_size(RestBin),
  876. case RefSize / 77 > ?VpBinaryCopyRatio of
  877. true ->
  878. {binary:copy(BinStr), LeftBin};
  879. _ ->
  880. {BinStr, LeftBin}
  881. end;
  882. decoder(142, RestBin) ->
  883. <<BinStr:78/binary, LeftBin/bitstring>> = RestBin,
  884. RefSize = binary:referenced_byte_size(RestBin),
  885. case RefSize / 78 > ?VpBinaryCopyRatio of
  886. true ->
  887. {binary:copy(BinStr), LeftBin};
  888. _ ->
  889. {BinStr, LeftBin}
  890. end;
  891. decoder(143, RestBin) ->
  892. <<BinStr:79/binary, LeftBin/bitstring>> = RestBin,
  893. RefSize = binary:referenced_byte_size(RestBin),
  894. case RefSize / 79 > ?VpBinaryCopyRatio of
  895. true ->
  896. {binary:copy(BinStr), LeftBin};
  897. _ ->
  898. {BinStr, LeftBin}
  899. end;
  900. decoder(144, RestBin) ->
  901. <<BinStr:80/binary, LeftBin/bitstring>> = RestBin,
  902. RefSize = binary:referenced_byte_size(RestBin),
  903. case RefSize / 80 > ?VpBinaryCopyRatio of
  904. true ->
  905. {binary:copy(BinStr), LeftBin};
  906. _ ->
  907. {BinStr, LeftBin}
  908. end;
  909. decoder(145, RestBin) ->
  910. <<BinStr:81/binary, LeftBin/bitstring>> = RestBin,
  911. RefSize = binary:referenced_byte_size(RestBin),
  912. case RefSize / 81 > ?VpBinaryCopyRatio of
  913. true ->
  914. {binary:copy(BinStr), LeftBin};
  915. _ ->
  916. {BinStr, LeftBin}
  917. end;
  918. decoder(146, RestBin) ->
  919. <<BinStr:82/binary, LeftBin/bitstring>> = RestBin,
  920. RefSize = binary:referenced_byte_size(RestBin),
  921. case RefSize / 82 > ?VpBinaryCopyRatio of
  922. true ->
  923. {binary:copy(BinStr), LeftBin};
  924. _ ->
  925. {BinStr, LeftBin}
  926. end;
  927. decoder(147, RestBin) ->
  928. <<BinStr:83/binary, LeftBin/bitstring>> = RestBin,
  929. RefSize = binary:referenced_byte_size(RestBin),
  930. case RefSize / 83 > ?VpBinaryCopyRatio of
  931. true ->
  932. {binary:copy(BinStr), LeftBin};
  933. _ ->
  934. {BinStr, LeftBin}
  935. end;
  936. decoder(148, RestBin) ->
  937. <<BinStr:84/binary, LeftBin/bitstring>> = RestBin,
  938. RefSize = binary:referenced_byte_size(RestBin),
  939. case RefSize / 84 > ?VpBinaryCopyRatio of
  940. true ->
  941. {binary:copy(BinStr), LeftBin};
  942. _ ->
  943. {BinStr, LeftBin}
  944. end;
  945. decoder(149, RestBin) ->
  946. <<BinStr:85/binary, LeftBin/bitstring>> = RestBin,
  947. RefSize = binary:referenced_byte_size(RestBin),
  948. case RefSize / 85 > ?VpBinaryCopyRatio of
  949. true ->
  950. {binary:copy(BinStr), LeftBin};
  951. _ ->
  952. {BinStr, LeftBin}
  953. end;
  954. decoder(150, RestBin) ->
  955. <<BinStr:86/binary, LeftBin/bitstring>> = RestBin,
  956. RefSize = binary:referenced_byte_size(RestBin),
  957. case RefSize / 86 > ?VpBinaryCopyRatio of
  958. true ->
  959. {binary:copy(BinStr), LeftBin};
  960. _ ->
  961. {BinStr, LeftBin}
  962. end;
  963. decoder(151, RestBin) ->
  964. <<BinStr:87/binary, LeftBin/bitstring>> = RestBin,
  965. RefSize = binary:referenced_byte_size(RestBin),
  966. case RefSize / 87 > ?VpBinaryCopyRatio of
  967. true ->
  968. {binary:copy(BinStr), LeftBin};
  969. _ ->
  970. {BinStr, LeftBin}
  971. end;
  972. decoder(152, RestBin) ->
  973. <<BinStr:88/binary, LeftBin/bitstring>> = RestBin,
  974. RefSize = binary:referenced_byte_size(RestBin),
  975. case RefSize / 88 > ?VpBinaryCopyRatio of
  976. true ->
  977. {binary:copy(BinStr), LeftBin};
  978. _ ->
  979. {BinStr, LeftBin}
  980. end;
  981. decoder(153, RestBin) ->
  982. <<BinStr:89/binary, LeftBin/bitstring>> = RestBin,
  983. RefSize = binary:referenced_byte_size(RestBin),
  984. case RefSize / 89 > ?VpBinaryCopyRatio of
  985. true ->
  986. {binary:copy(BinStr), LeftBin};
  987. _ ->
  988. {BinStr, LeftBin}
  989. end;
  990. decoder(154, RestBin) ->
  991. <<BinStr:90/binary, LeftBin/bitstring>> = RestBin,
  992. RefSize = binary:referenced_byte_size(RestBin),
  993. case RefSize / 90 > ?VpBinaryCopyRatio of
  994. true ->
  995. {binary:copy(BinStr), LeftBin};
  996. _ ->
  997. {BinStr, LeftBin}
  998. end;
  999. decoder(155, RestBin) ->
  1000. <<BinStr:91/binary, LeftBin/bitstring>> = RestBin,
  1001. RefSize = binary:referenced_byte_size(RestBin),
  1002. case RefSize / 91 > ?VpBinaryCopyRatio of
  1003. true ->
  1004. {binary:copy(BinStr), LeftBin};
  1005. _ ->
  1006. {BinStr, LeftBin}
  1007. end;
  1008. decoder(156, RestBin) ->
  1009. <<BinStr:92/binary, LeftBin/bitstring>> = RestBin,
  1010. RefSize = binary:referenced_byte_size(RestBin),
  1011. case RefSize / 92 > ?VpBinaryCopyRatio of
  1012. true ->
  1013. {binary:copy(BinStr), LeftBin};
  1014. _ ->
  1015. {BinStr, LeftBin}
  1016. end;
  1017. decoder(157, RestBin) ->
  1018. <<BinStr:93/binary, LeftBin/bitstring>> = RestBin,
  1019. RefSize = binary:referenced_byte_size(RestBin),
  1020. case RefSize / 93 > ?VpBinaryCopyRatio of
  1021. true ->
  1022. {binary:copy(BinStr), LeftBin};
  1023. _ ->
  1024. {BinStr, LeftBin}
  1025. end;
  1026. decoder(158, RestBin) ->
  1027. <<BinStr:94/binary, LeftBin/bitstring>> = RestBin,
  1028. RefSize = binary:referenced_byte_size(RestBin),
  1029. case RefSize / 94 > ?VpBinaryCopyRatio of
  1030. true ->
  1031. {binary:copy(BinStr), LeftBin};
  1032. _ ->
  1033. {BinStr, LeftBin}
  1034. end;
  1035. decoder(159, RestBin) ->
  1036. <<BinStr:95/binary, LeftBin/bitstring>> = RestBin,
  1037. RefSize = binary:referenced_byte_size(RestBin),
  1038. case RefSize / 95 > ?VpBinaryCopyRatio of
  1039. true ->
  1040. {binary:copy(BinStr), LeftBin};
  1041. _ ->
  1042. {BinStr, LeftBin}
  1043. end;
  1044. decoder(160, RestBin) ->
  1045. <<BinStr:96/binary, LeftBin/bitstring>> = RestBin,
  1046. RefSize = binary:referenced_byte_size(RestBin),
  1047. case RefSize / 96 > ?VpBinaryCopyRatio of
  1048. true ->
  1049. {binary:copy(BinStr), LeftBin};
  1050. _ ->
  1051. {BinStr, LeftBin}
  1052. end;
  1053. decoder(161, RestBin) ->
  1054. <<BinStr:97/binary, LeftBin/bitstring>> = RestBin,
  1055. RefSize = binary:referenced_byte_size(RestBin),
  1056. case RefSize / 97 > ?VpBinaryCopyRatio of
  1057. true ->
  1058. {binary:copy(BinStr), LeftBin};
  1059. _ ->
  1060. {BinStr, LeftBin}
  1061. end;
  1062. decoder(162, RestBin) ->
  1063. <<BinStr:98/binary, LeftBin/bitstring>> = RestBin,
  1064. RefSize = binary:referenced_byte_size(RestBin),
  1065. case RefSize / 98 > ?VpBinaryCopyRatio of
  1066. true ->
  1067. {binary:copy(BinStr), LeftBin};
  1068. _ ->
  1069. {BinStr, LeftBin}
  1070. end;
  1071. decoder(163, RestBin) ->
  1072. <<BinStr:99/binary, LeftBin/bitstring>> = RestBin,
  1073. RefSize = binary:referenced_byte_size(RestBin),
  1074. case RefSize / 99 > ?VpBinaryCopyRatio of
  1075. true ->
  1076. {binary:copy(BinStr), LeftBin};
  1077. _ ->
  1078. {BinStr, LeftBin}
  1079. end;
  1080. decoder(164, RestBin) ->
  1081. <<BinStr:100/binary, LeftBin/bitstring>> = RestBin,
  1082. RefSize = binary:referenced_byte_size(RestBin),
  1083. case RefSize / 100 > ?VpBinaryCopyRatio of
  1084. true ->
  1085. {binary:copy(BinStr), LeftBin};
  1086. _ ->
  1087. {BinStr, LeftBin}
  1088. end;
  1089. decoder(165, RestBin) ->
  1090. <<BinStr:101/binary, LeftBin/bitstring>> = RestBin,
  1091. RefSize = binary:referenced_byte_size(RestBin),
  1092. case RefSize / 101 > ?VpBinaryCopyRatio of
  1093. true ->
  1094. {binary:copy(BinStr), LeftBin};
  1095. _ ->
  1096. {BinStr, LeftBin}
  1097. end;
  1098. decoder(166, RestBin) ->
  1099. <<BinStr:102/binary, LeftBin/bitstring>> = RestBin,
  1100. RefSize = binary:referenced_byte_size(RestBin),
  1101. case RefSize / 102 > ?VpBinaryCopyRatio of
  1102. true ->
  1103. {binary:copy(BinStr), LeftBin};
  1104. _ ->
  1105. {BinStr, LeftBin}
  1106. end;
  1107. decoder(167, RestBin) ->
  1108. <<BinStr:103/binary, LeftBin/bitstring>> = RestBin,
  1109. RefSize = binary:referenced_byte_size(RestBin),
  1110. case RefSize / 103 > ?VpBinaryCopyRatio of
  1111. true ->
  1112. {binary:copy(BinStr), LeftBin};
  1113. _ ->
  1114. {BinStr, LeftBin}
  1115. end;
  1116. decoder(168, RestBin) ->
  1117. <<BinStr:104/binary, LeftBin/bitstring>> = RestBin,
  1118. RefSize = binary:referenced_byte_size(RestBin),
  1119. case RefSize / 104 > ?VpBinaryCopyRatio of
  1120. true ->
  1121. {binary:copy(BinStr), LeftBin};
  1122. _ ->
  1123. {BinStr, LeftBin}
  1124. end;
  1125. decoder(169, RestBin) ->
  1126. <<BinStr:105/binary, LeftBin/bitstring>> = RestBin,
  1127. RefSize = binary:referenced_byte_size(RestBin),
  1128. case RefSize / 105 > ?VpBinaryCopyRatio of
  1129. true ->
  1130. {binary:copy(BinStr), LeftBin};
  1131. _ ->
  1132. {BinStr, LeftBin}
  1133. end;
  1134. decoder(170, RestBin) ->
  1135. <<BinStr:106/binary, LeftBin/bitstring>> = RestBin,
  1136. RefSize = binary:referenced_byte_size(RestBin),
  1137. case RefSize / 106 > ?VpBinaryCopyRatio of
  1138. true ->
  1139. {binary:copy(BinStr), LeftBin};
  1140. _ ->
  1141. {BinStr, LeftBin}
  1142. end;
  1143. decoder(171, RestBin) ->
  1144. <<BinStr:107/binary, LeftBin/bitstring>> = RestBin,
  1145. RefSize = binary:referenced_byte_size(RestBin),
  1146. case RefSize / 107 > ?VpBinaryCopyRatio of
  1147. true ->
  1148. {binary:copy(BinStr), LeftBin};
  1149. _ ->
  1150. {BinStr, LeftBin}
  1151. end;
  1152. decoder(172, RestBin) ->
  1153. <<BinStr:108/binary, LeftBin/bitstring>> = RestBin,
  1154. RefSize = binary:referenced_byte_size(RestBin),
  1155. case RefSize / 108 > ?VpBinaryCopyRatio of
  1156. true ->
  1157. {binary:copy(BinStr), LeftBin};
  1158. _ ->
  1159. {BinStr, LeftBin}
  1160. end;
  1161. decoder(173, RestBin) ->
  1162. <<BinStr:109/binary, LeftBin/bitstring>> = RestBin,
  1163. RefSize = binary:referenced_byte_size(RestBin),
  1164. case RefSize / 109 > ?VpBinaryCopyRatio of
  1165. true ->
  1166. {binary:copy(BinStr), LeftBin};
  1167. _ ->
  1168. {BinStr, LeftBin}
  1169. end;
  1170. decoder(174, RestBin) ->
  1171. <<BinStr:110/binary, LeftBin/bitstring>> = RestBin,
  1172. RefSize = binary:referenced_byte_size(RestBin),
  1173. case RefSize / 110 > ?VpBinaryCopyRatio of
  1174. true ->
  1175. {binary:copy(BinStr), LeftBin};
  1176. _ ->
  1177. {BinStr, LeftBin}
  1178. end;
  1179. decoder(175, RestBin) ->
  1180. <<BinStr:111/binary, LeftBin/bitstring>> = RestBin,
  1181. RefSize = binary:referenced_byte_size(RestBin),
  1182. case RefSize / 111 > ?VpBinaryCopyRatio of
  1183. true ->
  1184. {binary:copy(BinStr), LeftBin};
  1185. _ ->
  1186. {BinStr, LeftBin}
  1187. end;
  1188. decoder(176, RestBin) ->
  1189. <<BinStr:112/binary, LeftBin/bitstring>> = RestBin,
  1190. RefSize = binary:referenced_byte_size(RestBin),
  1191. case RefSize / 112 > ?VpBinaryCopyRatio of
  1192. true ->
  1193. {binary:copy(BinStr), LeftBin};
  1194. _ ->
  1195. {BinStr, LeftBin}
  1196. end;
  1197. decoder(177, RestBin) ->
  1198. <<BinStr:113/binary, LeftBin/bitstring>> = RestBin,
  1199. RefSize = binary:referenced_byte_size(RestBin),
  1200. case RefSize / 113 > ?VpBinaryCopyRatio of
  1201. true ->
  1202. {binary:copy(BinStr), LeftBin};
  1203. _ ->
  1204. {BinStr, LeftBin}
  1205. end;
  1206. decoder(178, RestBin) ->
  1207. <<BinStr:114/binary, LeftBin/bitstring>> = RestBin,
  1208. RefSize = binary:referenced_byte_size(RestBin),
  1209. case RefSize / 114 > ?VpBinaryCopyRatio of
  1210. true ->
  1211. {binary:copy(BinStr), LeftBin};
  1212. _ ->
  1213. {BinStr, LeftBin}
  1214. end;
  1215. decoder(179, RestBin) ->
  1216. <<BinStr:115/binary, LeftBin/bitstring>> = RestBin,
  1217. RefSize = binary:referenced_byte_size(RestBin),
  1218. case RefSize / 115 > ?VpBinaryCopyRatio of
  1219. true ->
  1220. {binary:copy(BinStr), LeftBin};
  1221. _ ->
  1222. {BinStr, LeftBin}
  1223. end;
  1224. decoder(180, RestBin) ->
  1225. <<BinStr:116/binary, LeftBin/bitstring>> = RestBin,
  1226. RefSize = binary:referenced_byte_size(RestBin),
  1227. case RefSize / 116 > ?VpBinaryCopyRatio of
  1228. true ->
  1229. {binary:copy(BinStr), LeftBin};
  1230. _ ->
  1231. {BinStr, LeftBin}
  1232. end;
  1233. decoder(181, RestBin) ->
  1234. <<BinStr:117/binary, LeftBin/bitstring>> = RestBin,
  1235. RefSize = binary:referenced_byte_size(RestBin),
  1236. case RefSize / 117 > ?VpBinaryCopyRatio of
  1237. true ->
  1238. {binary:copy(BinStr), LeftBin};
  1239. _ ->
  1240. {BinStr, LeftBin}
  1241. end;
  1242. decoder(182, RestBin) ->
  1243. <<BinStr:118/binary, LeftBin/bitstring>> = RestBin,
  1244. RefSize = binary:referenced_byte_size(RestBin),
  1245. case RefSize / 118 > ?VpBinaryCopyRatio of
  1246. true ->
  1247. {binary:copy(BinStr), LeftBin};
  1248. _ ->
  1249. {BinStr, LeftBin}
  1250. end;
  1251. decoder(183, RestBin) ->
  1252. <<BinStr:119/binary, LeftBin/bitstring>> = RestBin,
  1253. RefSize = binary:referenced_byte_size(RestBin),
  1254. case RefSize / 119 > ?VpBinaryCopyRatio of
  1255. true ->
  1256. {binary:copy(BinStr), LeftBin};
  1257. _ ->
  1258. {BinStr, LeftBin}
  1259. end;
  1260. decoder(184, RestBin) ->
  1261. <<BinStr:120/binary, LeftBin/bitstring>> = RestBin,
  1262. RefSize = binary:referenced_byte_size(RestBin),
  1263. case RefSize / 120 > ?VpBinaryCopyRatio of
  1264. true ->
  1265. {binary:copy(BinStr), LeftBin};
  1266. _ ->
  1267. {BinStr, LeftBin}
  1268. end;
  1269. decoder(185, RestBin) ->
  1270. <<BinStr:121/binary, LeftBin/bitstring>> = RestBin,
  1271. RefSize = binary:referenced_byte_size(RestBin),
  1272. case RefSize / 121 > ?VpBinaryCopyRatio of
  1273. true ->
  1274. {binary:copy(BinStr), LeftBin};
  1275. _ ->
  1276. {BinStr, LeftBin}
  1277. end;
  1278. decoder(186, RestBin) ->
  1279. <<BinStr:122/binary, LeftBin/bitstring>> = RestBin,
  1280. RefSize = binary:referenced_byte_size(RestBin),
  1281. case RefSize / 122 > ?VpBinaryCopyRatio of
  1282. true ->
  1283. {binary:copy(BinStr), LeftBin};
  1284. _ ->
  1285. {BinStr, LeftBin}
  1286. end;
  1287. decoder(187, RestBin) ->
  1288. <<BinStr:123/binary, LeftBin/bitstring>> = RestBin,
  1289. RefSize = binary:referenced_byte_size(RestBin),
  1290. case RefSize / 123 > ?VpBinaryCopyRatio of
  1291. true ->
  1292. {binary:copy(BinStr), LeftBin};
  1293. _ ->
  1294. {BinStr, LeftBin}
  1295. end;
  1296. decoder(188, RestBin) ->
  1297. <<BinStr:124/binary, LeftBin/bitstring>> = RestBin,
  1298. RefSize = binary:referenced_byte_size(RestBin),
  1299. case RefSize / 124 > ?VpBinaryCopyRatio of
  1300. true ->
  1301. {binary:copy(BinStr), LeftBin};
  1302. _ ->
  1303. {BinStr, LeftBin}
  1304. end;
  1305. decoder(189, RestBin) ->
  1306. <<BinStr:125/binary, LeftBin/bitstring>> = RestBin,
  1307. RefSize = binary:referenced_byte_size(RestBin),
  1308. case RefSize / 125 > ?VpBinaryCopyRatio of
  1309. true ->
  1310. {binary:copy(BinStr), LeftBin};
  1311. _ ->
  1312. {BinStr, LeftBin}
  1313. end;
  1314. decoder(190, RestBin) ->
  1315. <<BinStr:126/binary, LeftBin/bitstring>> = RestBin,
  1316. RefSize = binary:referenced_byte_size(RestBin),
  1317. case RefSize / 126 > ?VpBinaryCopyRatio of
  1318. true ->
  1319. {binary:copy(BinStr), LeftBin};
  1320. _ ->
  1321. {BinStr, LeftBin}
  1322. end;
  1323. decoder(191, RestBin) ->
  1324. <<Length:64/integer-little-unsigned, BinStr:Length/binary, LeftBin/bitstring>> = RestBin,
  1325. RefSize = binary:referenced_byte_size(RestBin),
  1326. case RefSize / Length > ?VpBinaryCopyRatio of
  1327. true ->
  1328. {binary:copy(BinStr), LeftBin};
  1329. _ ->
  1330. {BinStr, LeftBin}
  1331. end;
  1332. decoder(192, RestBin) ->
  1333. <<Length:1/integer-little-unsigned-unit:8, BinStr:Length/binary, LeftBin/bitstring>> = RestBin,
  1334. RefSize = binary:referenced_byte_size(RestBin),
  1335. case RefSize / Length > ?VpBinaryCopyRatio of
  1336. true ->
  1337. {binary:copy(BinStr), LeftBin};
  1338. _ ->
  1339. {BinStr, LeftBin}
  1340. end;
  1341. decoder(193, RestBin) ->
  1342. <<Length:2/integer-little-unsigned-unit:8, BinStr:Length/binary, LeftBin/bitstring>> = RestBin,
  1343. RefSize = binary:referenced_byte_size(RestBin),
  1344. case RefSize / Length > ?VpBinaryCopyRatio of
  1345. true ->
  1346. {binary:copy(BinStr), LeftBin};
  1347. _ ->
  1348. {BinStr, LeftBin}
  1349. end;
  1350. decoder(194, RestBin) ->
  1351. <<Length:3/integer-little-unsigned-unit:8, BinStr:Length/binary, LeftBin/bitstring>> = RestBin,
  1352. RefSize = binary:referenced_byte_size(RestBin),
  1353. case RefSize / Length > ?VpBinaryCopyRatio of
  1354. true ->
  1355. {binary:copy(BinStr), LeftBin};
  1356. _ ->
  1357. {BinStr, LeftBin}
  1358. end;
  1359. decoder(195, RestBin) ->
  1360. <<Length:4/integer-little-unsigned-unit:8, BinStr:Length/binary, LeftBin/bitstring>> = RestBin,
  1361. RefSize = binary:referenced_byte_size(RestBin),
  1362. case RefSize / Length > ?VpBinaryCopyRatio of
  1363. true ->
  1364. {binary:copy(BinStr), LeftBin};
  1365. _ ->
  1366. {BinStr, LeftBin}
  1367. end;
  1368. decoder(196, RestBin) ->
  1369. <<Length:5/integer-little-unsigned-unit:8, BinStr:Length/binary, LeftBin/bitstring>> = RestBin,
  1370. RefSize = binary:referenced_byte_size(RestBin),
  1371. case RefSize / Length > ?VpBinaryCopyRatio of
  1372. true ->
  1373. {binary:copy(BinStr), LeftBin};
  1374. _ ->
  1375. {BinStr, LeftBin}
  1376. end;
  1377. decoder(197, RestBin) ->
  1378. <<Length:6/integer-little-unsigned-unit:8, BinStr:Length/binary, LeftBin/bitstring>> = RestBin,
  1379. RefSize = binary:referenced_byte_size(RestBin),
  1380. case RefSize / Length > ?VpBinaryCopyRatio of
  1381. true ->
  1382. {binary:copy(BinStr), LeftBin};
  1383. _ ->
  1384. {BinStr, LeftBin}
  1385. end;
  1386. decoder(198, RestBin) ->
  1387. <<Length:7/integer-little-unsigned-unit:8, BinStr:Length/binary, LeftBin/bitstring>> = RestBin,
  1388. RefSize = binary:referenced_byte_size(RestBin),
  1389. case RefSize / Length > ?VpBinaryCopyRatio of
  1390. true ->
  1391. {binary:copy(BinStr), LeftBin};
  1392. _ ->
  1393. {BinStr, LeftBin}
  1394. end;
  1395. decoder(199, RestBin) ->
  1396. <<Length:8/integer-little-unsigned-unit:8, BinStr:Length/binary, LeftBin/bitstring>> = RestBin,
  1397. RefSize = binary:referenced_byte_size(RestBin),
  1398. case RefSize / Length > ?VpBinaryCopyRatio of
  1399. true ->
  1400. {binary:copy(BinStr), LeftBin};
  1401. _ ->
  1402. {BinStr, LeftBin}
  1403. end;
  1404. decoder(_, _) ->
  1405. erlang:throw(unexpected_end).
  1406. parseArrayElements(<<>>, AccList) -> lists:reverse(AccList);
  1407. parseArrayElements(DataBin, AccList) ->
  1408. {Elem, Rest} = decoder(DataBin),
  1409. parseArrayElements(Rest, [Elem | AccList]).
  1410. parseArrayWithIndexTable(9, <<SumSize:64/integer-little-unsigned, RestBin/binary>>) ->
  1411. DataSize = SumSize - 1 - 8 - 8,
  1412. <<DataBin:DataSize/binary, Length:64/integer-little-unsigned, LeftBin/binary>> = RestBin,
  1413. IndexSize = Length * 8,
  1414. DataSize = DataSize - IndexSize,
  1415. <<ArrData:DataSize/binary, _Index:IndexSize/binary>> = DataBin,
  1416. ArrList = parseArrayElements(ArrData, []),
  1417. {ArrList, LeftBin};
  1418. parseArrayWithIndexTable(Type, DataBin) ->
  1419. SizeBytes = 1 bsl (Type - 6),
  1420. <<SumSize:SizeBytes/integer-little-unsigned-unit:8, Length:SizeBytes/integer-little-unsigned-unit:8, RestBin/binary>> = DataBin,
  1421. IndexSize = SizeBytes * Length,
  1422. DataSize = erlang:byte_size(RestBin),
  1423. DataLeftBin = skipZeros(RestBin),
  1424. ZerosSize = DataSize - erlang:byte_size(DataLeftBin),
  1425. ArrSize = SumSize - 1 - 2 * SizeBytes - ZerosSize - IndexSize,
  1426. <<ArrData:ArrSize/binary, _Index:IndexSize/binary, LeftBin/binary>> = DataLeftBin,
  1427. ArrList = parseArrayElements(ArrData, []),
  1428. {ArrList, LeftBin}.
  1429. parseArrayWithoutIndexTable(Type, DataBin) ->
  1430. SizeBytes = 1 bsl (Type - 2),
  1431. <<SumSize:SizeBytes/integer-little-unsigned-unit:8, RestBin/binary>> = DataBin,
  1432. DataSize = erlang:byte_size(RestBin),
  1433. DataLeftBin = skipZeros(RestBin),
  1434. ZerosSize = DataSize - erlang:byte_size(DataLeftBin),
  1435. ArrSize = SumSize - SizeBytes - 1 - ZerosSize,
  1436. <<ArrData:ArrSize/binary, LeftBin/binary>> = DataLeftBin,
  1437. ArrList = parseArrayElements(ArrData, []),
  1438. {ArrList, LeftBin}.
  1439. parseCompactArray(DataBin) ->
  1440. {ArrData, _Length, RestBin} = parseCompactHeader(DataBin),
  1441. ArrList = parseArrayElements(ArrData, []),
  1442. {ArrList, RestBin}.
  1443. parseCompactHeader(DataBin) ->
  1444. {Size, RestBin} = parseLength(DataBin, 0, 0, false),
  1445. DataSize = Size - (erlang:byte_size(DataBin) - erlang:byte_size(RestBin)) - 1,
  1446. <<ArrData:DataSize/binary, LeftBin/binary>> = RestBin,
  1447. {Length, HeaderBin} = parseLength(ArrData, 0, 0, true),
  1448. {HeaderBin, Length, LeftBin}.
  1449. parseCompactObject(DataBin) ->
  1450. {ObjData, Length, RestBin} = parseCompactHeader(DataBin),
  1451. {Obj, <<>>} = parseObjectMembers(Length, #{}, ObjData),
  1452. {Obj, RestBin}.
  1453. parseInt(IntegerSize, DataBin) ->
  1454. <<Integer:IntegerSize/integer-little-signed-unit:8, LeftBin/binary>> = DataBin,
  1455. {Integer, LeftBin}.
  1456. parseUint(IntegerSize, DataBin) ->
  1457. <<Integer:IntegerSize/integer-little-unsigned-unit:8, LeftBin/binary>> = DataBin,
  1458. {Integer, LeftBin}.
  1459. parseLength(DataBin, Len, Pt, Reverse) ->
  1460. {LastV, LeftBin} =
  1461. case Reverse of
  1462. false ->
  1463. <<V/integer, RestBin/binary>> = DataBin,
  1464. {V, RestBin};
  1465. _ ->
  1466. Size = erlang:byte_size(DataBin) - 1,
  1467. <<RestBin:Size/binary, V/integer>> = DataBin,
  1468. {V, RestBin}
  1469. end,
  1470. NewLen = Len + (LastV band 127 bsl Pt),
  1471. NewPt = Pt + 7,
  1472. case LastV band 128 /= 0 of
  1473. false -> {NewLen, LeftBin};
  1474. true -> parseLength(LeftBin, NewLen, NewPt, Reverse)
  1475. end.
  1476. parseObject(Type, DataBin) ->
  1477. SizeBytes = 1 bsl (Type - 11),
  1478. <<SumSize:SizeBytes/integer-little-unsigned-unit:8, Length:SizeBytes/integer-little-unsigned-unit:8, ObjBin/binary>> = DataBin,
  1479. DataSize = SumSize - 1 - 2 * SizeBytes,
  1480. <<ZerosBin:DataSize/binary, LeftBin/binary>> = ObjBin,
  1481. IndexTableSize = Length * SizeBytes,
  1482. {Obj, <<_IndexTable:IndexTableSize/binary>>} = parseObjectMembers(Length, #{}, skipZeros(ZerosBin)),
  1483. {Obj, LeftBin}.
  1484. parseObjectMembers(0, Obj, DataBin) ->
  1485. {Obj, DataBin};
  1486. parseObjectMembers(Length, Obj, DataBin) ->
  1487. {Key, ValueDataBin} = decoder(DataBin),
  1488. {Value, LeftBin} = decoder(ValueDataBin),
  1489. NewObj = Obj#{Key => Value},
  1490. parseObjectMembers(Length - 1, NewObj, LeftBin).
  1491. skipZeros(<<0/integer, LeftBin/binary>>) ->
  1492. skipZeros(LeftBin);
  1493. skipZeros(DataBin) -> DataBin.