MsgPackDecoder.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MsgPackDecoder = void 0;
  4. const _1 = require(".");
  5. const MsgPackDecoderFast_1 = require("./MsgPackDecoderFast");
  6. class MsgPackDecoder extends MsgPackDecoderFast_1.MsgPackDecoderFast {
  7. skipAny() {
  8. const byte = this.reader.u8();
  9. if (byte >= 0xe0)
  10. return 1;
  11. if (byte <= 0xbf) {
  12. if (byte < 0x90) {
  13. if (byte <= 0b1111111)
  14. return 1;
  15. return 1 + this.skipObj(byte & 0b1111);
  16. }
  17. else {
  18. if (byte < 0xa0)
  19. return 1 + this.skipArr(byte & 0b1111);
  20. else
  21. return 1 + this.skip(byte & 0b11111);
  22. }
  23. }
  24. if (byte <= 0xd0) {
  25. if (byte <= 0xc8) {
  26. if (byte <= 0xc4) {
  27. if (byte <= 0xc2)
  28. return byte === 0xc2 ? 1 : 1;
  29. else
  30. return byte === 0xc4 ? 2 + this.skip(this.reader.u8()) : 1;
  31. }
  32. else {
  33. if (byte <= 0xc6)
  34. return byte === 0xc6 ? 5 + this.skip(this.reader.u32()) : 3 + this.skip(this.reader.u16());
  35. else
  36. return byte === 0xc8 ? 4 + this.skip(this.reader.u16()) : 3 + this.skip(this.reader.u8());
  37. }
  38. }
  39. else {
  40. return byte <= 0xcc
  41. ? byte <= 0xca
  42. ? byte === 0xca
  43. ? 1 + this.skip(4)
  44. : 1 + 1 + 4 + this.skip(this.reader.u32())
  45. : byte === 0xcc
  46. ? 1 + this.skip(1)
  47. : 1 + this.skip(8)
  48. : byte <= 0xce
  49. ? byte === 0xce
  50. ? 1 + this.skip(4)
  51. : 1 + this.skip(2)
  52. : byte === 0xd0
  53. ? 1 + this.skip(1)
  54. : 1 + this.skip(8);
  55. }
  56. }
  57. else if (byte <= 0xd8) {
  58. return byte <= 0xd4
  59. ? byte <= 0xd2
  60. ? byte === 0xd2
  61. ? 1 + this.skip(4)
  62. : 1 + this.skip(2)
  63. : byte === 0xd4
  64. ? 1 + this.skip(2)
  65. : 1 + this.skip(8)
  66. : byte <= 0xd6
  67. ? byte === 0xd6
  68. ? 1 + this.skip(5)
  69. : 1 + this.skip(3)
  70. : byte === 0xd8
  71. ? 1 + this.skip(17)
  72. : 1 + this.skip(9);
  73. }
  74. else {
  75. switch (byte) {
  76. case 0xd9:
  77. return 2 + this.skip(this.reader.u8());
  78. case 0xda:
  79. return 3 + this.skip(this.reader.u16());
  80. case 0xdb:
  81. return 5 + this.skip(this.reader.u32());
  82. case 0xdc:
  83. return 3 + this.skipArr(this.reader.u16());
  84. case 0xdd:
  85. return 5 + this.skipArr(this.reader.u32());
  86. case 0xde:
  87. return 3 + this.skipObj(this.reader.u16());
  88. case 0xdf:
  89. return 5 + this.skipObj(this.reader.u32());
  90. }
  91. }
  92. return 1;
  93. }
  94. skipArr(size) {
  95. let length = 0;
  96. for (let i = 0; i < size; i++)
  97. length += this.skipAny();
  98. return length;
  99. }
  100. skipObj(size) {
  101. let length = 0;
  102. for (let i = 0; i < size; i++) {
  103. length += this.skipAny() + this.skipAny();
  104. }
  105. return length;
  106. }
  107. readLevel(uint8) {
  108. this.reader.reset(uint8);
  109. return this.valOneLevel();
  110. }
  111. valOneLevel() {
  112. const byte = this.reader.view.getUint8(this.reader.x);
  113. const isMap = byte === 0xde || byte === 0xdf || byte >> 4 === 0b1000;
  114. if (isMap) {
  115. this.reader.x++;
  116. const size = byte === 0xde ? this.reader.u16() : byte === 0xdf ? this.reader.u32() : byte & 0b1111;
  117. const obj = {};
  118. for (let i = 0; i < size; i++) {
  119. const key = this.key();
  120. obj[key] = this.primitive();
  121. }
  122. return obj;
  123. }
  124. const isArray = byte === 0xdc || byte === 0xdd || byte >> 4 === 0b1001;
  125. if (isArray) {
  126. this.reader.x++;
  127. const size = byte === 0xdc ? this.reader.u16() : byte === 0xdd ? this.reader.u32() : byte & 0b1111;
  128. const arr = [];
  129. for (let i = 0; i < size; i++)
  130. arr.push(this.primitive());
  131. return arr;
  132. }
  133. return this.val();
  134. }
  135. primitive() {
  136. const reader = this.reader;
  137. const byte = reader.view.getUint8(reader.x);
  138. const isMapOrArray = byte === 0xde || byte === 0xdf || byte === 0xdc || byte === 0xdd || byte >> 5 === 0b100;
  139. if (isMapOrArray) {
  140. const length = this.skipAny();
  141. reader.x -= length;
  142. const buf = reader.buf(length);
  143. return new _1.JsonPackValue(buf);
  144. }
  145. return this.val();
  146. }
  147. skip(length) {
  148. this.reader.x += length;
  149. return length;
  150. }
  151. validate(value, offset = 0, size = value.length) {
  152. this.reader.reset(value);
  153. this.reader.x = offset;
  154. const start = offset;
  155. this.skipAny();
  156. const end = this.reader.x;
  157. if (end - start !== size)
  158. throw new Error('INVALID_SIZE');
  159. }
  160. readObjHdr() {
  161. const reader = this.reader;
  162. const byte = reader.u8();
  163. const isFixMap = byte >> 4 === 0b1000;
  164. if (isFixMap)
  165. return byte & 0b1111;
  166. switch (byte) {
  167. case 0xde:
  168. return reader.u16();
  169. case 0xdf:
  170. return reader.u32();
  171. }
  172. throw new Error('NOT_OBJ');
  173. }
  174. readStrHdr() {
  175. const reader = this.reader;
  176. const byte = reader.u8();
  177. if (byte >> 5 === 0b101)
  178. return byte & 0b11111;
  179. switch (byte) {
  180. case 0xd9:
  181. return reader.u8();
  182. case 0xda:
  183. return reader.u16();
  184. case 0xdb:
  185. return reader.u32();
  186. }
  187. throw new Error('NOT_STR');
  188. }
  189. findKey(key) {
  190. const size = this.readObjHdr();
  191. for (let i = 0; i < size; i++) {
  192. const k = this.key();
  193. if (k === key)
  194. return this;
  195. this.skipAny();
  196. }
  197. throw new Error('KEY_NOT_FOUND');
  198. }
  199. readArrHdr() {
  200. const reader = this.reader;
  201. const byte = reader.u8();
  202. const isFixArr = byte >> 4 === 0b1001;
  203. if (isFixArr)
  204. return byte & 0b1111;
  205. switch (byte) {
  206. case 0xdc:
  207. return this.reader.u16();
  208. case 0xdd:
  209. return this.reader.u32();
  210. }
  211. throw new Error('NOT_ARR');
  212. }
  213. findIndex(index) {
  214. const size = this.readArrHdr();
  215. if (index >= size)
  216. throw new Error('INDEX_OUT_OF_BOUNDS');
  217. for (let i = 0; i < index; i++)
  218. this.skipAny();
  219. return this;
  220. }
  221. find(path) {
  222. for (let i = 0; i < path.length; i++) {
  223. const segment = path[i];
  224. if (typeof segment === 'string')
  225. this.findKey(segment);
  226. else
  227. this.findIndex(segment);
  228. }
  229. return this;
  230. }
  231. }
  232. exports.MsgPackDecoder = MsgPackDecoder;
  233. //# sourceMappingURL=MsgPackDecoder.js.map