StreamingOctetReader.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StreamingOctetReader = void 0;
  4. const fromCharCode = String.fromCharCode;
  5. class StreamingOctetReader {
  6. constructor() {
  7. this.chunks = [];
  8. this.chunkSize = 0;
  9. this.x = 0;
  10. }
  11. size() {
  12. return this.chunkSize - this.x;
  13. }
  14. push(chunk) {
  15. this.chunks.push(chunk);
  16. this.chunkSize += chunk.length;
  17. }
  18. assertSize(size) {
  19. if (size > this.size())
  20. throw new RangeError('OUT_OF_BOUNDS');
  21. }
  22. u8() {
  23. this.assertSize(1);
  24. const chunk = this.chunks[0];
  25. let x = this.x;
  26. const octet = chunk[x++];
  27. if (x === chunk.length) {
  28. this.chunks.shift();
  29. this.chunkSize -= chunk.length;
  30. x = 0;
  31. }
  32. this.x = x;
  33. return octet;
  34. }
  35. u32() {
  36. const octet0 = this.u8();
  37. const octet1 = this.u8();
  38. const octet2 = this.u8();
  39. const octet3 = this.u8();
  40. return (octet0 * 0x1000000 + (octet1 << 16) + (octet2 << 8)) | octet3;
  41. }
  42. copy(size, dst, pos) {
  43. if (!size)
  44. return;
  45. this.assertSize(size);
  46. const chunk0 = this.chunks[0];
  47. const size0 = Math.min(chunk0.length - this.x, size);
  48. dst.set(chunk0.subarray(this.x, this.x + size0), pos);
  49. size -= size0;
  50. if (size <= 0) {
  51. this.skipUnsafe(size0);
  52. return;
  53. }
  54. let chunkIndex = 1;
  55. while (size > 0) {
  56. const chunk1 = this.chunks[chunkIndex];
  57. const size1 = Math.min(chunk1.length, size);
  58. dst.set(chunk1.subarray(0, size1), pos + size0);
  59. size -= size1;
  60. chunkIndex++;
  61. }
  62. this.skipUnsafe(size);
  63. }
  64. copyXor(size, dst, pos, mask, maskIndex) {
  65. if (!size)
  66. return;
  67. this.assertSize(size);
  68. const chunk0 = this.chunks[0];
  69. let x = this.x;
  70. const size0 = Math.min(chunk0.length - x, size);
  71. const end = x + size0;
  72. for (; x < end;)
  73. dst[pos++] = chunk0[x++] ^ mask[maskIndex++ % 4];
  74. size -= size0;
  75. if (size <= 0) {
  76. this.skipUnsafe(size0);
  77. return;
  78. }
  79. let chunkIndex = 1;
  80. while (size > 0) {
  81. const chunk1 = this.chunks[chunkIndex++];
  82. const size1 = Math.min(chunk1.length, size);
  83. for (let x = 0; x < size1;)
  84. dst[pos++] = chunk1[x++] ^ mask[maskIndex++ % 4];
  85. size -= size1;
  86. }
  87. this.skipUnsafe(size);
  88. }
  89. buf(size) {
  90. this.assertSize(size);
  91. const buf = new Uint8Array(size);
  92. this.copy(size, buf, 0);
  93. return buf;
  94. }
  95. bufXor(size, mask, maskIndex) {
  96. this.assertSize(size);
  97. const buf = new Uint8Array(size);
  98. this.copyXor(size, buf, 0, mask, maskIndex);
  99. return buf;
  100. }
  101. skipUnsafe(n) {
  102. if (!n)
  103. return;
  104. const chunk = this.chunks[0];
  105. const chunkLength = chunk.length;
  106. const remaining = chunkLength - this.x;
  107. if (remaining > n) {
  108. this.x = this.x + n;
  109. return;
  110. }
  111. this.x = 0;
  112. this.chunks.shift();
  113. this.chunkSize -= chunkLength;
  114. n -= remaining;
  115. this.skipUnsafe(n);
  116. }
  117. skip(n) {
  118. this.assertSize(n);
  119. this.skipUnsafe(n);
  120. }
  121. peak() {
  122. this.assertSize(1);
  123. return this.chunks[0][this.x];
  124. }
  125. utf8(length, mask, maskIndex) {
  126. this.assertSize(length);
  127. let i = 0;
  128. const points = [];
  129. while (i < length) {
  130. let code = this.u8() ^ mask[maskIndex++ % 4];
  131. i++;
  132. if ((code & 0x80) !== 0) {
  133. const octet2 = (this.u8() ^ mask[maskIndex++ % 4]) & 0x3f;
  134. i++;
  135. if ((code & 0xe0) === 0xc0) {
  136. code = ((code & 0x1f) << 6) | octet2;
  137. }
  138. else {
  139. const octet3 = (this.u8() ^ mask[maskIndex++ % 4]) & 0x3f;
  140. i++;
  141. if ((code & 0xf0) === 0xe0) {
  142. code = ((code & 0x1f) << 12) | (octet2 << 6) | octet3;
  143. }
  144. else {
  145. if ((code & 0xf8) === 0xf0) {
  146. const octet4 = (this.u8() ^ mask[maskIndex++ % 4]) & 0x3f;
  147. i++;
  148. let unit = ((code & 0x07) << 0x12) | (octet2 << 0x0c) | (octet3 << 0x06) | octet4;
  149. if (unit > 0xffff) {
  150. unit -= 0x10000;
  151. const unit0 = ((unit >>> 10) & 0x3ff) | 0xd800;
  152. code = 0xdc00 | (unit & 0x3ff);
  153. points.push(unit0);
  154. }
  155. else {
  156. code = unit;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. points.push(code);
  163. }
  164. return fromCharCode.apply(String, points);
  165. }
  166. }
  167. exports.StreamingOctetReader = StreamingOctetReader;
  168. //# sourceMappingURL=StreamingOctetReader.js.map