CborDecoderBase.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CborDecoderBase = void 0;
  4. const tslib_1 = require("tslib");
  5. const f16_1 = require("@jsonjoy.com/buffers/lib/f16");
  6. const JsonPackExtension_1 = require("../JsonPackExtension");
  7. const JsonPackValue_1 = require("../JsonPackValue");
  8. const Reader_1 = require("@jsonjoy.com/buffers/lib/Reader");
  9. const sharedCachedUtf8Decoder_1 = tslib_1.__importDefault(require("@jsonjoy.com/buffers/lib/utf8/sharedCachedUtf8Decoder"));
  10. class CborDecoderBase {
  11. constructor(reader = new Reader_1.Reader(), keyDecoder = sharedCachedUtf8Decoder_1.default) {
  12. this.reader = reader;
  13. this.keyDecoder = keyDecoder;
  14. }
  15. read(uint8) {
  16. this.reader.reset(uint8);
  17. return this.readAny();
  18. }
  19. decode(uint8) {
  20. this.reader.reset(uint8);
  21. return this.readAny();
  22. }
  23. val() {
  24. return this.readAny();
  25. }
  26. readAny() {
  27. const reader = this.reader;
  28. const octet = reader.u8();
  29. const major = octet >> 5;
  30. const minor = octet & 31;
  31. if (major < 4) {
  32. if (major < 2)
  33. return major === 0 ? this.readUint(minor) : this.readNint(minor);
  34. else
  35. return major === 2 ? this.readBin(minor) : this.readStr(minor);
  36. }
  37. else {
  38. if (major < 6)
  39. return major === 4 ? this.readArr(minor) : this.readObj(minor);
  40. else
  41. return major === 6 ? this.readTag(minor) : this.readTkn(minor);
  42. }
  43. }
  44. readAnyRaw(octet) {
  45. const major = octet >> 5;
  46. const minor = octet & 31;
  47. if (major < 4) {
  48. if (major < 2)
  49. return major === 0 ? this.readUint(minor) : this.readNint(minor);
  50. else
  51. return major === 2 ? this.readBin(minor) : this.readStr(minor);
  52. }
  53. else {
  54. if (major < 6)
  55. return major === 4 ? this.readArr(minor) : this.readObj(minor);
  56. else
  57. return major === 6 ? this.readTag(minor) : this.readTkn(minor);
  58. }
  59. }
  60. readMinorLen(minor) {
  61. if (minor < 24)
  62. return minor;
  63. switch (minor) {
  64. case 24:
  65. return this.reader.u8();
  66. case 25:
  67. return this.reader.u16();
  68. case 26:
  69. return this.reader.u32();
  70. case 27:
  71. return Number(this.reader.u64());
  72. case 31:
  73. return -1;
  74. default:
  75. throw 1;
  76. }
  77. }
  78. readUint(minor) {
  79. if (minor < 25) {
  80. return minor === 24 ? this.reader.u8() : minor;
  81. }
  82. else {
  83. if (minor < 27) {
  84. return minor === 25 ? this.reader.u16() : this.reader.u32();
  85. }
  86. else {
  87. const num = this.reader.u64();
  88. return num > 9007199254740991 ? num : Number(num);
  89. }
  90. }
  91. }
  92. readNint(minor) {
  93. if (minor < 25) {
  94. return minor === 24 ? -this.reader.u8() - 1 : -minor - 1;
  95. }
  96. else {
  97. if (minor < 27) {
  98. return minor === 25 ? -this.reader.u16() - 1 : -this.reader.u32() - 1;
  99. }
  100. else {
  101. const num = this.reader.u64();
  102. return num > 9007199254740991 - 1 ? -num - BigInt(1) : -Number(num) - 1;
  103. }
  104. }
  105. }
  106. readBin(minor) {
  107. const reader = this.reader;
  108. if (minor <= 23)
  109. return reader.buf(minor);
  110. switch (minor) {
  111. case 24:
  112. return reader.buf(reader.u8());
  113. case 25:
  114. return reader.buf(reader.u16());
  115. case 26:
  116. return reader.buf(reader.u32());
  117. case 27:
  118. return reader.buf(Number(reader.u64()));
  119. case 31: {
  120. let size = 0;
  121. const list = [];
  122. while (this.reader.peak() !== 255) {
  123. const uint8 = this.readBinChunk();
  124. size += uint8.length;
  125. list.push(uint8);
  126. }
  127. this.reader.x++;
  128. const res = new Uint8Array(size);
  129. let offset = 0;
  130. const length = list.length;
  131. for (let i = 0; i < length; i++) {
  132. const arr = list[i];
  133. res.set(arr, offset);
  134. offset += arr.length;
  135. }
  136. return res;
  137. }
  138. default:
  139. throw 1;
  140. }
  141. }
  142. readBinChunk() {
  143. const octet = this.reader.u8();
  144. const major = octet >> 5;
  145. const minor = octet & 31;
  146. if (major !== 2)
  147. throw 2;
  148. if (minor > 27)
  149. throw 3;
  150. return this.readBin(minor);
  151. }
  152. readAsStr() {
  153. const reader = this.reader;
  154. const octet = reader.u8();
  155. const major = octet >> 5;
  156. const minor = octet & 31;
  157. if (major !== 3)
  158. throw 11;
  159. return this.readStr(minor);
  160. }
  161. readStr(minor) {
  162. const reader = this.reader;
  163. if (minor <= 23)
  164. return reader.utf8(minor);
  165. switch (minor) {
  166. case 24:
  167. return reader.utf8(reader.u8());
  168. case 25:
  169. return reader.utf8(reader.u16());
  170. case 26:
  171. return reader.utf8(reader.u32());
  172. case 27:
  173. return reader.utf8(Number(reader.u64()));
  174. case 31: {
  175. let str = '';
  176. while (reader.peak() !== 255)
  177. str += this.readStrChunk();
  178. this.reader.x++;
  179. return str;
  180. }
  181. default:
  182. throw 1;
  183. }
  184. }
  185. readStrLen(minor) {
  186. if (minor <= 23)
  187. return minor;
  188. switch (minor) {
  189. case 24:
  190. return this.reader.u8();
  191. case 25:
  192. return this.reader.u16();
  193. case 26:
  194. return this.reader.u32();
  195. case 27:
  196. return Number(this.reader.u64());
  197. default:
  198. throw 1;
  199. }
  200. }
  201. readStrChunk() {
  202. const octet = this.reader.u8();
  203. const major = octet >> 5;
  204. const minor = octet & 31;
  205. if (major !== 3)
  206. throw 4;
  207. if (minor > 27)
  208. throw 5;
  209. return this.readStr(minor);
  210. }
  211. readArr(minor) {
  212. const length = this.readMinorLen(minor);
  213. if (length >= 0)
  214. return this.readArrRaw(length);
  215. return this.readArrIndef();
  216. }
  217. readArrRaw(length) {
  218. const arr = [];
  219. for (let i = 0; i < length; i++)
  220. arr.push(this.readAny());
  221. return arr;
  222. }
  223. readArrIndef() {
  224. const arr = [];
  225. while (this.reader.peak() !== 255)
  226. arr.push(this.readAny());
  227. this.reader.x++;
  228. return arr;
  229. }
  230. readObj(minor) {
  231. if (minor < 28) {
  232. let length = minor;
  233. switch (minor) {
  234. case 24:
  235. length = this.reader.u8();
  236. break;
  237. case 25:
  238. length = this.reader.u16();
  239. break;
  240. case 26:
  241. length = this.reader.u32();
  242. break;
  243. case 27:
  244. length = Number(this.reader.u64());
  245. break;
  246. }
  247. const obj = {};
  248. for (let i = 0; i < length; i++) {
  249. const key = this.key();
  250. if (key === '__proto__')
  251. throw 6;
  252. const value = this.readAny();
  253. obj[key] = value;
  254. }
  255. return obj;
  256. }
  257. else if (minor === 31)
  258. return this.readObjIndef();
  259. else
  260. throw 1;
  261. }
  262. readObjRaw(length) {
  263. const obj = {};
  264. for (let i = 0; i < length; i++) {
  265. const key = this.key();
  266. const value = this.readAny();
  267. obj[key] = value;
  268. }
  269. return obj;
  270. }
  271. readObjIndef() {
  272. const obj = {};
  273. while (this.reader.peak() !== 255) {
  274. const key = this.key();
  275. if (this.reader.peak() === 255)
  276. throw 7;
  277. const value = this.readAny();
  278. obj[key] = value;
  279. }
  280. this.reader.x++;
  281. return obj;
  282. }
  283. key() {
  284. const octet = this.reader.u8();
  285. const major = octet >> 5;
  286. const minor = octet & 31;
  287. if (major !== 3)
  288. return String(this.readAnyRaw(octet));
  289. const length = this.readStrLen(minor);
  290. if (length > 31)
  291. return this.reader.utf8(length);
  292. const key = this.keyDecoder.decode(this.reader.uint8, this.reader.x, length);
  293. this.reader.skip(length);
  294. return key;
  295. }
  296. readTag(minor) {
  297. if (minor <= 23)
  298. return this.readTagRaw(minor);
  299. switch (minor) {
  300. case 24:
  301. return this.readTagRaw(this.reader.u8());
  302. case 25:
  303. return this.readTagRaw(this.reader.u16());
  304. case 26:
  305. return this.readTagRaw(this.reader.u32());
  306. case 27:
  307. return this.readTagRaw(Number(this.reader.u64()));
  308. default:
  309. throw 1;
  310. }
  311. }
  312. readTagRaw(tag) {
  313. return new JsonPackExtension_1.JsonPackExtension(tag, this.readAny());
  314. }
  315. readTkn(minor) {
  316. switch (minor) {
  317. case 0xf4 & 31:
  318. return false;
  319. case 0xf5 & 31:
  320. return true;
  321. case 0xf6 & 31:
  322. return null;
  323. case 0xf7 & 31:
  324. return undefined;
  325. case 0xf8 & 31:
  326. return new JsonPackValue_1.JsonPackValue(this.reader.u8());
  327. case 0xf9 & 31:
  328. return this.f16();
  329. case 0xfa & 31:
  330. return this.reader.f32();
  331. case 0xfb & 31:
  332. return this.reader.f64();
  333. }
  334. if (minor <= 23)
  335. return new JsonPackValue_1.JsonPackValue(minor);
  336. throw 1;
  337. }
  338. f16() {
  339. return (0, f16_1.decodeF16)(this.reader.u16());
  340. }
  341. }
  342. exports.CborDecoderBase = CborDecoderBase;
  343. //# sourceMappingURL=CborDecoderBase.js.map