base.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /* jshint esversion: 6 */
  2. var base = base || {};
  3. base.Int64 = class Int64 {
  4. constructor(low, high) {
  5. this.low = low | 0;
  6. this.high = high | 0;
  7. }
  8. static create(value) {
  9. if (isNaN(value)) {
  10. return base.Int64.zero;
  11. }
  12. if (value <= -9223372036854776000) {
  13. return base.Int64.min;
  14. }
  15. if (value + 1 >= 9223372036854776000) {
  16. return base.Int64.max;
  17. }
  18. if (value < 0) {
  19. return base.Int64.create(-value).negate();
  20. }
  21. return new base.Int64((value % 4294967296) | 0, (value / 4294967296));
  22. }
  23. get isZero() {
  24. return this.low === 0 && this.high === 0;
  25. }
  26. get isNegative() {
  27. return this.high < 0;
  28. }
  29. negate() {
  30. if (this.equals(base.Int64.min)) {
  31. return base.Int64.min;
  32. }
  33. return this.not().add(base.Int64.one);
  34. }
  35. not() {
  36. return new Int64(~this.low, ~this.high);
  37. }
  38. equals(other) {
  39. if (!(other instanceof base.Int64) && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) {
  40. return false;
  41. }
  42. return this.high === other.high && this.low === other.low;
  43. }
  44. compare(other) {
  45. if (this.equals(other)) {
  46. return 0;
  47. }
  48. const thisNeg = this.isNegative;
  49. const otherNeg = other.isNegative;
  50. if (thisNeg && !otherNeg) {
  51. return -1;
  52. }
  53. if (!thisNeg && otherNeg) {
  54. return 1;
  55. }
  56. return this.subtract(other).isNegative ? -1 : 1;
  57. }
  58. add(other) {
  59. return base.Utility.add(this, other, false);
  60. }
  61. subtract(other) {
  62. return base.Utility.subtract(this, other, false);
  63. }
  64. multiply(other) {
  65. return base.Utility.multiply(this, other, false);
  66. }
  67. divide(other) {
  68. return base.Utility.divide(this, other, false);
  69. }
  70. toInteger() {
  71. return this.low;
  72. }
  73. toNumber() {
  74. if (this.high === 0) {
  75. return this.low >>> 0;
  76. }
  77. if (this.high === -1) {
  78. return this.low;
  79. }
  80. return (this.high * 4294967296) + (this.low >>> 0);
  81. }
  82. toString(radix) {
  83. radix = radix || 10;
  84. if (radix < 2 || radix > 16) {
  85. throw RangeError('radix');
  86. }
  87. if (this.isZero) {
  88. return '0';
  89. }
  90. if (this.high < 0) {
  91. if (this.equals(base.Int64.min)) {
  92. const r = new Int64(radix, 0);
  93. const div = this.divide(r);
  94. const remainder = div.multiply(r).subtract(this);
  95. return div.toString(radix) + (remainder.low >>> 0).toString(radix);
  96. }
  97. return '-' + this.negate().toString(radix);
  98. }
  99. return base.Utility.text(this, false, radix);
  100. }
  101. };
  102. base.Int64.min = new base.Int64(0, -2147483648);
  103. base.Int64.zero = new base.Int64(0, 0);
  104. base.Int64.one = new base.Int64(1, 0);
  105. base.Int64.power24 = new base.Int64(1 << 24, 0);
  106. base.Int64.max = new base.Int64(0, 2147483647);
  107. base.Uint64 = class Uint64 {
  108. constructor(low, high) {
  109. this.low = low | 0;
  110. this.high = high | 0;
  111. }
  112. static create(value) {
  113. if (isNaN(value)) {
  114. return base.Uint64.zero;
  115. }
  116. if (value < 0) {
  117. return base.Uint64.zero;
  118. }
  119. if (value >= 18446744073709552000) {
  120. return base.Uint64.max;
  121. }
  122. if (value < 0) {
  123. return base.Uint64.create(-value).negate();
  124. }
  125. return new base.Uint64((value % 4294967296) | 0, (value / 4294967296));
  126. }
  127. get isZero() {
  128. return this.low === 0 && this.high === 0;
  129. }
  130. get isNegative() {
  131. return false;
  132. }
  133. negate() {
  134. return this.not().add(base.Int64.one);
  135. }
  136. not() {
  137. return new base.Uint64(~this.low, ~this.high);
  138. }
  139. equals(other) {
  140. if (!(other instanceof base.Uint64) && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) {
  141. return false;
  142. }
  143. return this.high === other.high && this.low === other.low;
  144. }
  145. compare(other) {
  146. if (this.equals(other)) {
  147. return 0;
  148. }
  149. const thisNeg = this.isNegative;
  150. const otherNeg = other.isNegative;
  151. if (thisNeg && !otherNeg) {
  152. return -1;
  153. }
  154. if (!thisNeg && otherNeg) {
  155. return 1;
  156. }
  157. return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1;
  158. }
  159. add(other) {
  160. return base.Utility.add(this, other, true);
  161. }
  162. subtract(other) {
  163. return base.Utility.subtract(this, other, true);
  164. }
  165. multiply(other) {
  166. return base.Utility.multiply(this, other, true);
  167. }
  168. divide(other) {
  169. return base.Utility.divide(this, other, true);
  170. }
  171. toInteger() {
  172. return this.low >>> 0;
  173. }
  174. toNumber() {
  175. if (this.high === 0) {
  176. return this.low >>> 0;
  177. }
  178. return ((this.high >>> 0) * 4294967296) + (this.low >>> 0);
  179. }
  180. toString(radix) {
  181. radix = radix || 10;
  182. if (radix < 2 || 36 < radix) {
  183. throw RangeError('radix');
  184. }
  185. if (this.isZero) {
  186. return '0';
  187. }
  188. return base.Utility.text(this, true, radix);
  189. }
  190. };
  191. base.Utility = class {
  192. static add(a, b, unsigned) {
  193. const a48 = a.high >>> 16;
  194. const a32 = a.high & 0xFFFF;
  195. const a16 = a.low >>> 16;
  196. const a00 = a.low & 0xFFFF;
  197. const b48 = b.high >>> 16;
  198. const b32 = b.high & 0xFFFF;
  199. const b16 = b.low >>> 16;
  200. const b00 = b.low & 0xFFFF;
  201. let c48 = 0;
  202. let c32 = 0;
  203. let c16 = 0;
  204. let c00 = 0;
  205. c00 += a00 + b00;
  206. c16 += c00 >>> 16;
  207. c00 &= 0xFFFF;
  208. c16 += a16 + b16;
  209. c32 += c16 >>> 16;
  210. c16 &= 0xFFFF;
  211. c32 += a32 + b32;
  212. c48 += c32 >>> 16;
  213. c32 &= 0xFFFF;
  214. c48 += a48 + b48;
  215. c48 &= 0xFFFF;
  216. return base.Utility._create((c16 << 16) | c00, (c48 << 16) | c32, unsigned);
  217. }
  218. static subtract(a, b, unsigned) {
  219. return base.Utility.add(a, b.negate(), unsigned);
  220. }
  221. static multiply(a, b, unsigned) {
  222. if (a.isZero) {
  223. return base.Int64.zero;
  224. }
  225. if (b.isZero) {
  226. return base.Int64.zero;
  227. }
  228. if (a.equals(base.Int64.min)) {
  229. return b.isOdd() ? base.Int64.min : base.Int64.zero;
  230. }
  231. if (b.equals(base.Int64.min)) {
  232. return b.isOdd() ? base.Int64.min : base.Int64.zero;
  233. }
  234. if (a.isNegative) {
  235. if (b.isNegative) {
  236. return this.negate().multiply(b.negate());
  237. }
  238. else {
  239. return this.negate().multiply(b).negate();
  240. }
  241. }
  242. else if (b.isNegative) {
  243. return this.multiply(b.negate()).negate();
  244. }
  245. if (a.compare(base.Int64.power24) < 0 && b.compare(base.Int64.power24) < 0) {
  246. return unsigned ? base.Uint64.create(a.toNumber() * b.toNumber()) : base.Int64.create(a.toNumber() * b.toNumber());
  247. }
  248. const a48 = a.high >>> 16;
  249. const a32 = a.high & 0xFFFF;
  250. const a16 = a.low >>> 16;
  251. const a00 = a.low & 0xFFFF;
  252. const b48 = b.high >>> 16;
  253. const b32 = b.high & 0xFFFF;
  254. const b16 = b.low >>> 16;
  255. const b00 = b.low & 0xFFFF;
  256. let c48 = 0;
  257. let c32 = 0;
  258. let c16 = 0;
  259. let c00 = 0;
  260. c00 += a00 * b00;
  261. c16 += c00 >>> 16;
  262. c00 &= 0xFFFF;
  263. c16 += a16 * b00;
  264. c32 += c16 >>> 16;
  265. c16 &= 0xFFFF;
  266. c16 += a00 * b16;
  267. c32 += c16 >>> 16;
  268. c16 &= 0xFFFF;
  269. c32 += a32 * b00;
  270. c48 += c32 >>> 16;
  271. c32 &= 0xFFFF;
  272. c32 += a16 * b16;
  273. c48 += c32 >>> 16;
  274. c32 &= 0xFFFF;
  275. c32 += a00 * b32;
  276. c48 += c32 >>> 16;
  277. c32 &= 0xFFFF;
  278. c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
  279. c48 &= 0xFFFF;
  280. return base.Utility._create((c16 << 16) | c00, (c48 << 16) | c32, unsigned);
  281. }
  282. static divide(a, b, unsigned) {
  283. if (b.isZero) {
  284. throw Error('Division by zero.');
  285. }
  286. if (a.isZero) {
  287. return unsigned ? base.Uint64.zero : base.Int64.zero;
  288. }
  289. let approx;
  290. let remainder;
  291. let result;
  292. if (!unsigned) {
  293. if (a.equals(base.Int64.min)) {
  294. if (b.equals(base.Int64.one) || b.equals(base.Int64.negativeOne)) {
  295. return base.Int64.min;
  296. }
  297. else if (b.equals(base.Int64.min)) {
  298. return base.Int64.one;
  299. }
  300. else {
  301. const half = base.Utility._shiftRight(a, unsigned, 1);
  302. const halfDivide = half.divide(b);
  303. approx = base.Utility._shiftLeft(halfDivide, halfDivide instanceof base.Uint64, 1);
  304. if (approx.eq(base.Int64.zero)) {
  305. return b.isNegative ? base.Int64.one : base.Int64.negativeOne;
  306. }
  307. else {
  308. remainder = a.subtract(b.multiply(approx));
  309. result = approx.add(remainder.divide(b));
  310. return result;
  311. }
  312. }
  313. }
  314. else if (b.equals(base.Int64.min)) {
  315. return unsigned ? base.Uint64.zero : base.Int64.zero;
  316. }
  317. if (a.isNegative) {
  318. if (b.isNegative) {
  319. return this.negate().divide(b.negate());
  320. }
  321. return a.negate().divide(b).negate();
  322. }
  323. else if (b.isNegative) {
  324. return a.divide(b.negate()).negate();
  325. }
  326. result = base.Int64.zero;
  327. }
  328. else {
  329. if (!(b instanceof base.Uint64)) {
  330. b = new base.Uint64(b.low, b.high);
  331. }
  332. if (b.compare(a) > 0) {
  333. return base.Int64.zero;
  334. }
  335. if (b.compare(base.Utility._shiftRight(a, unsigned, 1)) > 0) {
  336. return base.Uint64.one;
  337. }
  338. result = base.Uint64.zero;
  339. }
  340. remainder = a;
  341. while (remainder.compare(b) >= 0) {
  342. let approx = Math.max(1, Math.floor(remainder.toNumber() / b.toNumber()));
  343. const log2 = Math.ceil(Math.log(approx) / Math.LN2);
  344. const delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48);
  345. let approxResult = base.Int64.create(approx);
  346. let approxRemainder = approxResult.multiply(b);
  347. while (approxRemainder.isNegative || approxRemainder.compare(remainder) > 0) {
  348. approx -= delta;
  349. approxResult = unsigned ? base.Uint64.create(approx) : base.Int64.create(approx);
  350. approxRemainder = approxResult.multiply(b);
  351. }
  352. if (approxResult.isZero) {
  353. approxResult = base.Int64.one;
  354. }
  355. result = result.add(approxResult);
  356. remainder = remainder.subtract(approxRemainder);
  357. }
  358. return result;
  359. }
  360. static text(value, unsigned, radix) {
  361. const power = unsigned ? base.Uint64.create(Math.pow(radix, 6)) : base.Int64.create(Math.pow(radix, 6));
  362. let remainder = value;
  363. let result = '';
  364. for (;;) {
  365. const remainderDiv = remainder.divide(power);
  366. const intval = remainder.subtract(remainderDiv.multiply(power)).toInteger() >>> 0;
  367. let digits = intval.toString(radix);
  368. remainder = remainderDiv;
  369. if (remainder.low === 0 && remainder.high === 0) {
  370. return digits + result;
  371. }
  372. while (digits.length < 6) {
  373. digits = '0' + digits;
  374. }
  375. result = '' + digits + result;
  376. }
  377. }
  378. static _shiftLeft(value, unsigned, shift) {
  379. return base.Utility._create(value.low << shift, (value.high << shift) | (value.low >>> (32 - shift)), unsigned);
  380. }
  381. static _shiftRight(value, unsigned, shift) {
  382. return base.Utility._create((value.low >>> shift) | (value.high << (32 - shift)), value.high >> shift, unsigned);
  383. }
  384. static _create(low, high, unsigned) {
  385. return unsigned ? new base.Uint64(low, high) : new base.Int64(low, high);
  386. }
  387. };
  388. base.Uint64.zero = new base.Uint64(0, 0);
  389. base.Uint64.one = new base.Uint64(1, 0);
  390. base.Uint64.max = new base.Uint64(-1, -1);
  391. if (!DataView.prototype.getFloat16) {
  392. DataView.prototype.getFloat16 = function(byteOffset, littleEndian) {
  393. const value = this.getUint16(byteOffset, littleEndian);
  394. const e = (value & 0x7C00) >> 10;
  395. let f = value & 0x03FF;
  396. if (e == 0) {
  397. f = 0.00006103515625 * (f / 1024);
  398. }
  399. else if (e == 0x1F) {
  400. f = f ? NaN : Infinity;
  401. }
  402. else {
  403. f = DataView.__float16_pow[e] * (1 + (f / 1024));
  404. }
  405. return value & 0x8000 ? -f : f;
  406. };
  407. DataView.__float16_pow = {
  408. 1: 1/16384, 2: 1/8192, 3: 1/4096, 4: 1/2048, 5: 1/1024, 6: 1/512, 7: 1/256, 8: 1/128,
  409. 9: 1/64, 10: 1/32, 11: 1/16, 12: 1/8, 13: 1/4, 14: 1/2, 15: 1, 16: 2,
  410. 17: 4, 18: 8, 19: 16, 20: 32, 21: 64, 22: 128, 23: 256, 24: 512,
  411. 25: 1024, 26: 2048, 27: 4096, 28: 8192, 29: 16384, 30: 32768, 31: 65536
  412. };
  413. }
  414. if (!DataView.prototype.setFloat16) {
  415. DataView.prototype.setFloat16 = function(byteOffset, value, littleEndian) {
  416. DataView.__float16_float[0] = value;
  417. value = DataView.__float16_int[0];
  418. const s = (value >>> 16) & 0x8000;
  419. const e = (value >>> 23) & 0xff;
  420. const f = value & 0x7fffff;
  421. const v = s | DataView.__float16_base[e] | (f >> DataView.__float16_shift[e]);
  422. this.setUint16(byteOffset, v, littleEndian);
  423. };
  424. DataView.__float16_float = new Float32Array(1);
  425. DataView.__float16_int = new Uint32Array(DataView.__float16_float.buffer, 0, DataView.__float16_float.length);
  426. DataView.__float16_base = new Uint32Array(256);
  427. DataView.__float16_shift = new Uint32Array(256);
  428. for (let i = 0; i < 256; ++i) {
  429. const e = i - 127;
  430. if (e < -27) {
  431. DataView.__float16_base[i] = 0x0000;
  432. DataView.__float16_shift[i] = 24;
  433. }
  434. else if (e < -14) {
  435. DataView.__float16_base[i] = 0x0400 >> -e - 14;
  436. DataView.__float16_shift[i] = -e - 1;
  437. }
  438. else if (e <= 15) {
  439. DataView.__float16_base[i] = e + 15 << 10;
  440. DataView.__float16_shift[i] = 13;
  441. }
  442. else if (e < 128) {
  443. DataView.__float16_base[i] = 0x7c00;
  444. DataView.__float16_shift[i] = 24;
  445. }
  446. else {
  447. DataView.__float16_base[i] = 0x7c00;
  448. DataView.__float16_shift[i] = 13;
  449. }
  450. }
  451. }
  452. DataView.prototype.getInt64 = DataView.prototype.getInt64 || function(byteOffset, littleEndian) {
  453. return littleEndian ?
  454. new base.Int64(this.getUint32(byteOffset, true), this.getUint32(byteOffset + 4, true)) :
  455. new base.Int64(this.getUint32(byteOffset + 4, true), this.getUint32(byteOffset, true));
  456. };
  457. DataView.prototype.setInt64 = DataView.prototype.setInt64 || function(byteOffset, value, littleEndian) {
  458. if (littleEndian) {
  459. this.setUint32(byteOffset, value.low, true);
  460. this.setUint32(byteOffset + 4, value.high, true);
  461. }
  462. else {
  463. this.setUint32(byteOffset + 4, value.low, false);
  464. this.setUint32(byteOffset, value.high, false);
  465. }
  466. };
  467. DataView.prototype.getUint64 = DataView.prototype.getUint64 || function(byteOffset, littleEndian) {
  468. return littleEndian ?
  469. new base.Uint64(this.getUint32(byteOffset, true), this.getUint32(byteOffset + 4, true)) :
  470. new base.Uint64(this.getUint32(byteOffset + 4, true), this.getUint32(byteOffset, true));
  471. };
  472. DataView.prototype.setUint64 = DataView.prototype.setUint64 || function(byteOffset, value, littleEndian) {
  473. if (littleEndian) {
  474. this.setUInt32(byteOffset, value.low, true);
  475. this.setUInt32(byteOffset + 4, value.high, true);
  476. }
  477. else {
  478. this.setUInt32(byteOffset + 4, value.low, false);
  479. this.setUInt32(byteOffset, value.high, false);
  480. }
  481. };
  482. DataView.prototype.getBits = DataView.prototype.getBits || function(offset, bits /*, signed */) {
  483. offset = offset * bits;
  484. const available = (this.byteLength << 3) - offset;
  485. if (bits > available) {
  486. throw new RangeError();
  487. }
  488. let value = 0;
  489. let index = 0;
  490. while (index < bits) {
  491. const remainder = offset & 7;
  492. const size = Math.min(bits - index, 8 - remainder);
  493. value <<= size;
  494. value |= (this.getUint8(offset >> 3) >> (8 - size - remainder)) & ~(0xff << size);
  495. offset += size;
  496. index += size;
  497. }
  498. return value;
  499. };
  500. base.TextDecoder = class {
  501. static open(data) {
  502. if (typeof data === 'string') {
  503. return new base.TextDecoder.String(data);
  504. }
  505. const buffer = data instanceof Uint8Array ? data : data.peek();
  506. const length = buffer.length;
  507. if (length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
  508. return new base.TextDecoder.Utf8(buffer, 3);
  509. }
  510. if (length >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) {
  511. return new base.TextDecoder.Utf16LE(buffer, 2);
  512. }
  513. if (length >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) {
  514. return new base.TextDecoder.Utf16BE(buffer, 2);
  515. }
  516. if (length >= 4 && buffer[0] === 0x00 && buffer[1] === 0x00 && buffer[2] === 0xfe && buffer[3] === 0xff) {
  517. throw new Error("Unsupported UTF-32 big-endian encoding.");
  518. }
  519. if (length >= 4 && buffer[0] === 0xff && buffer[1] === 0xfe && buffer[2] === 0x00 && buffer[3] === 0x00) {
  520. throw new Error("Unsupported UTF-32 little-endian encoding.");
  521. }
  522. if (length >= 5 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && buffer[3] === 0x38 && buffer[4] === 0x2D) {
  523. throw new Error("Unsupported UTF-7 encoding.");
  524. }
  525. if (length >= 4 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && (buffer[3] === 0x38 || buffer[3] === 0x39 || buffer[3] === 0x2B || buffer[3] === 0x2F)) {
  526. throw new Error("Unsupported UTF-7 encoding.");
  527. }
  528. if (length >= 4 && buffer[0] === 0x84 && buffer[1] === 0x31 && buffer[2] === 0x95 && buffer[3] === 0x33) {
  529. throw new Error("Unsupported GB-18030 encoding.");
  530. }
  531. if (length > 4 && (length % 2) == 0 && (buffer[0] === 0x00 || buffer[1] === 0x00 || buffer[2] === 0x00 || buffer[3] === 0x00)) {
  532. const lo = new Uint32Array(256);
  533. const hi = new Uint32Array(256);
  534. for (let i = 0; i < length; i += 2) {
  535. lo[buffer[i]]++;
  536. hi[buffer[i + 1]]++;
  537. }
  538. if (lo[0x00] === 0 && (hi[0x00] / (length >> 1)) > 0.5) {
  539. return new base.TextDecoder.Utf16LE(buffer, 0);
  540. }
  541. if (hi[0x00] === 0 && (lo[0x00] / (length >> 1)) > 0.5) {
  542. return new base.TextDecoder.Utf16BE(buffer, 0);
  543. }
  544. }
  545. return new base.TextDecoder.Utf8(buffer, 0);
  546. }
  547. };
  548. base.TextDecoder.String = class {
  549. constructor(buffer) {
  550. this.buffer = buffer;
  551. this.position = 0;
  552. this.length = buffer.length;
  553. }
  554. decode() {
  555. if (this.position < this.length) {
  556. return this.buffer[this.position++];
  557. }
  558. return undefined;
  559. }
  560. };
  561. base.TextDecoder.Utf8 = class {
  562. constructor(buffer, position) {
  563. this.position = position || 0;
  564. this.buffer = buffer;
  565. }
  566. decode() {
  567. const c = this.buffer[this.position];
  568. if (c === undefined) {
  569. return c;
  570. }
  571. this.position++;
  572. if (c < 0x80) {
  573. return String.fromCodePoint(c);
  574. }
  575. if (c >= 0xC2 && c <= 0xDF) {
  576. if (this.buffer[this.position] !== undefined) {
  577. const c2 = this.buffer[this.position];
  578. this.position++;
  579. return String.fromCharCode(((c & 0x1F) << 6) | (c2 & 0x3F));
  580. }
  581. }
  582. if (c >= 0xE0 && c <= 0xEF) {
  583. if (this.buffer[this.position + 1] !== undefined) {
  584. const c2 = this.buffer[this.position];
  585. if ((c !== 0xE0 || c2 >= 0xA0) && (c !== 0xED || c2 <= 0x9f)) {
  586. const c3 = this.buffer[this.position + 1];
  587. if (c3 >= 0x80 && c3 < 0xFB) {
  588. this.position += 2;
  589. return String.fromCharCode(((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0));
  590. }
  591. }
  592. }
  593. }
  594. if (c >= 0xF0 && c <= 0xF4) {
  595. if (this.buffer[this.position + 2] !== undefined) {
  596. const c2 = this.buffer[this.position];
  597. if ((c !== 0xF0 || c2 >= 0x90) && (c !== 0xF4 || c2 <= 0x8f)) {
  598. const c3 = this.buffer[this.position + 1];
  599. if (c3 >= 0x80 && c3 < 0xFB) {
  600. const c4 = this.buffer[this.position + 2];
  601. this.position += 3;
  602. return String.fromCodePoint(((c & 0x07) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
  603. }
  604. }
  605. }
  606. }
  607. return String.fromCharCode(0xfffd);
  608. }
  609. };
  610. base.TextDecoder.Utf16LE = class {
  611. constructor(buffer, position) {
  612. this.buffer = buffer;
  613. this.position = position || 0;
  614. this.length = buffer.length;
  615. }
  616. decode() {
  617. if (this.position + 1 < this.length) {
  618. const c = this.buffer[this.position++] | (this.buffer[this.position++] << 8);
  619. if (c < 0xD800 || c >= 0xDFFF) {
  620. return String.fromCharCode(c);
  621. }
  622. if (c >= 0xD800 && c < 0xDBFF) {
  623. if (this._position + 1 < this._length) {
  624. const c2 = this._buffer[this._position++] | (this._buffer[this._position++] << 8);
  625. if (c >= 0xDC00 || c < 0xDFFF) {
  626. return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff));
  627. }
  628. }
  629. }
  630. return String.fromCharCode(0xfffd);
  631. }
  632. return undefined;
  633. }
  634. };
  635. base.TextDecoder.Utf16BE = class {
  636. constructor(buffer, position) {
  637. this.buffer = buffer;
  638. this.position = position || 0;
  639. this.length = buffer.length;
  640. }
  641. decode() {
  642. if (this.position + 1 < this.length) {
  643. const c = (this.buffer[this.position++] << 8) | this.buffer[this.position++];
  644. if (c < 0xD800 || c >= 0xDFFF) {
  645. return String.fromCharCode(c);
  646. }
  647. if (c >= 0xD800 && c < 0xDBFF) {
  648. if (this._position + 1 < this._length) {
  649. const c2 = (this._buffer[this._position++] << 8) | this._buffer[this._position++];
  650. if (c >= 0xDC00 || c < 0xDFFF) {
  651. return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff));
  652. }
  653. }
  654. }
  655. return String.fromCharCode(0xfffd);
  656. }
  657. return undefined;
  658. }
  659. };
  660. base.TextReader = class {
  661. constructor(data, length) {
  662. this._decoder = base.TextDecoder.open(data);
  663. this._position = 0;
  664. this._length = length || Number.MAX_SAFE_INTEGER;
  665. }
  666. static open(data, length) {
  667. return new base.TextReader(data, length);
  668. }
  669. read() {
  670. if (this._position >= this._length) {
  671. return undefined;
  672. }
  673. let line = '';
  674. let buffer = null;
  675. for (;;) {
  676. const c = this._decoder.decode();
  677. if (c === undefined) {
  678. this._length = this._position;
  679. break;
  680. }
  681. this._position++;
  682. if (this._position > this._length) {
  683. break;
  684. }
  685. if (c === '\n') {
  686. break;
  687. }
  688. line += c;
  689. if (line.length >= 32) {
  690. buffer = buffer || [];
  691. buffer.push(line);
  692. line = '';
  693. }
  694. }
  695. if (buffer) {
  696. buffer.push(line);
  697. return buffer.join('');
  698. }
  699. return line;
  700. }
  701. };
  702. if (typeof window !== 'undefined' && typeof window.Long != 'undefined') {
  703. window.long = { Long: window.Long };
  704. window.Int64 = base.Int64;
  705. window.Uint64 = base.Uint64;
  706. }
  707. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  708. module.exports.Int64 = base.Int64;
  709. module.exports.Uint64 = base.Uint64;
  710. module.exports.TextDecoder = base.TextDecoder;
  711. module.exports.TextReader = base.TextReader;
  712. }