erlAarango 二进制序列化库
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1557 行
51 KiB

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