flate_stream.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.FlateStream = void 0;
  27. var _decode_stream = require("./decode_stream.js");
  28. var _util = require("../shared/util.js");
  29. const codeLenCodeMap = new Int32Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
  30. const lengthDecode = new Int32Array([0x00003, 0x00004, 0x00005, 0x00006, 0x00007, 0x00008, 0x00009, 0x0000a, 0x1000b, 0x1000d, 0x1000f, 0x10011, 0x20013, 0x20017, 0x2001b, 0x2001f, 0x30023, 0x3002b, 0x30033, 0x3003b, 0x40043, 0x40053, 0x40063, 0x40073, 0x50083, 0x500a3, 0x500c3, 0x500e3, 0x00102, 0x00102, 0x00102]);
  31. const distDecode = new Int32Array([0x00001, 0x00002, 0x00003, 0x00004, 0x10005, 0x10007, 0x20009, 0x2000d, 0x30011, 0x30019, 0x40021, 0x40031, 0x50041, 0x50061, 0x60081, 0x600c1, 0x70101, 0x70181, 0x80201, 0x80301, 0x90401, 0x90601, 0xa0801, 0xa0c01, 0xb1001, 0xb1801, 0xc2001, 0xc3001, 0xd4001, 0xd6001]);
  32. const fixedLitCodeTab = [new Int32Array([0x70100, 0x80050, 0x80010, 0x80118, 0x70110, 0x80070, 0x80030, 0x900c0, 0x70108, 0x80060, 0x80020, 0x900a0, 0x80000, 0x80080, 0x80040, 0x900e0, 0x70104, 0x80058, 0x80018, 0x90090, 0x70114, 0x80078, 0x80038, 0x900d0, 0x7010c, 0x80068, 0x80028, 0x900b0, 0x80008, 0x80088, 0x80048, 0x900f0, 0x70102, 0x80054, 0x80014, 0x8011c, 0x70112, 0x80074, 0x80034, 0x900c8, 0x7010a, 0x80064, 0x80024, 0x900a8, 0x80004, 0x80084, 0x80044, 0x900e8, 0x70106, 0x8005c, 0x8001c, 0x90098, 0x70116, 0x8007c, 0x8003c, 0x900d8, 0x7010e, 0x8006c, 0x8002c, 0x900b8, 0x8000c, 0x8008c, 0x8004c, 0x900f8, 0x70101, 0x80052, 0x80012, 0x8011a, 0x70111, 0x80072, 0x80032, 0x900c4, 0x70109, 0x80062, 0x80022, 0x900a4, 0x80002, 0x80082, 0x80042, 0x900e4, 0x70105, 0x8005a, 0x8001a, 0x90094, 0x70115, 0x8007a, 0x8003a, 0x900d4, 0x7010d, 0x8006a, 0x8002a, 0x900b4, 0x8000a, 0x8008a, 0x8004a, 0x900f4, 0x70103, 0x80056, 0x80016, 0x8011e, 0x70113, 0x80076, 0x80036, 0x900cc, 0x7010b, 0x80066, 0x80026, 0x900ac, 0x80006, 0x80086, 0x80046, 0x900ec, 0x70107, 0x8005e, 0x8001e, 0x9009c, 0x70117, 0x8007e, 0x8003e, 0x900dc, 0x7010f, 0x8006e, 0x8002e, 0x900bc, 0x8000e, 0x8008e, 0x8004e, 0x900fc, 0x70100, 0x80051, 0x80011, 0x80119, 0x70110, 0x80071, 0x80031, 0x900c2, 0x70108, 0x80061, 0x80021, 0x900a2, 0x80001, 0x80081, 0x80041, 0x900e2, 0x70104, 0x80059, 0x80019, 0x90092, 0x70114, 0x80079, 0x80039, 0x900d2, 0x7010c, 0x80069, 0x80029, 0x900b2, 0x80009, 0x80089, 0x80049, 0x900f2, 0x70102, 0x80055, 0x80015, 0x8011d, 0x70112, 0x80075, 0x80035, 0x900ca, 0x7010a, 0x80065, 0x80025, 0x900aa, 0x80005, 0x80085, 0x80045, 0x900ea, 0x70106, 0x8005d, 0x8001d, 0x9009a, 0x70116, 0x8007d, 0x8003d, 0x900da, 0x7010e, 0x8006d, 0x8002d, 0x900ba, 0x8000d, 0x8008d, 0x8004d, 0x900fa, 0x70101, 0x80053, 0x80013, 0x8011b, 0x70111, 0x80073, 0x80033, 0x900c6, 0x70109, 0x80063, 0x80023, 0x900a6, 0x80003, 0x80083, 0x80043, 0x900e6, 0x70105, 0x8005b, 0x8001b, 0x90096, 0x70115, 0x8007b, 0x8003b, 0x900d6, 0x7010d, 0x8006b, 0x8002b, 0x900b6, 0x8000b, 0x8008b, 0x8004b, 0x900f6, 0x70103, 0x80057, 0x80017, 0x8011f, 0x70113, 0x80077, 0x80037, 0x900ce, 0x7010b, 0x80067, 0x80027, 0x900ae, 0x80007, 0x80087, 0x80047, 0x900ee, 0x70107, 0x8005f, 0x8001f, 0x9009e, 0x70117, 0x8007f, 0x8003f, 0x900de, 0x7010f, 0x8006f, 0x8002f, 0x900be, 0x8000f, 0x8008f, 0x8004f, 0x900fe, 0x70100, 0x80050, 0x80010, 0x80118, 0x70110, 0x80070, 0x80030, 0x900c1, 0x70108, 0x80060, 0x80020, 0x900a1, 0x80000, 0x80080, 0x80040, 0x900e1, 0x70104, 0x80058, 0x80018, 0x90091, 0x70114, 0x80078, 0x80038, 0x900d1, 0x7010c, 0x80068, 0x80028, 0x900b1, 0x80008, 0x80088, 0x80048, 0x900f1, 0x70102, 0x80054, 0x80014, 0x8011c, 0x70112, 0x80074, 0x80034, 0x900c9, 0x7010a, 0x80064, 0x80024, 0x900a9, 0x80004, 0x80084, 0x80044, 0x900e9, 0x70106, 0x8005c, 0x8001c, 0x90099, 0x70116, 0x8007c, 0x8003c, 0x900d9, 0x7010e, 0x8006c, 0x8002c, 0x900b9, 0x8000c, 0x8008c, 0x8004c, 0x900f9, 0x70101, 0x80052, 0x80012, 0x8011a, 0x70111, 0x80072, 0x80032, 0x900c5, 0x70109, 0x80062, 0x80022, 0x900a5, 0x80002, 0x80082, 0x80042, 0x900e5, 0x70105, 0x8005a, 0x8001a, 0x90095, 0x70115, 0x8007a, 0x8003a, 0x900d5, 0x7010d, 0x8006a, 0x8002a, 0x900b5, 0x8000a, 0x8008a, 0x8004a, 0x900f5, 0x70103, 0x80056, 0x80016, 0x8011e, 0x70113, 0x80076, 0x80036, 0x900cd, 0x7010b, 0x80066, 0x80026, 0x900ad, 0x80006, 0x80086, 0x80046, 0x900ed, 0x70107, 0x8005e, 0x8001e, 0x9009d, 0x70117, 0x8007e, 0x8003e, 0x900dd, 0x7010f, 0x8006e, 0x8002e, 0x900bd, 0x8000e, 0x8008e, 0x8004e, 0x900fd, 0x70100, 0x80051, 0x80011, 0x80119, 0x70110, 0x80071, 0x80031, 0x900c3, 0x70108, 0x80061, 0x80021, 0x900a3, 0x80001, 0x80081, 0x80041, 0x900e3, 0x70104, 0x80059, 0x80019, 0x90093, 0x70114, 0x80079, 0x80039, 0x900d3, 0x7010c, 0x80069, 0x80029, 0x900b3, 0x80009, 0x80089, 0x80049, 0x900f3, 0x70102, 0x80055, 0x80015, 0x8011d, 0x70112, 0x80075, 0x80035, 0x900cb, 0x7010a, 0x80065, 0x80025, 0x900ab, 0x80005, 0x80085, 0x80045, 0x900eb, 0x70106, 0x8005d, 0x8001d, 0x9009b, 0x70116, 0x8007d, 0x8003d, 0x900db, 0x7010e, 0x8006d, 0x8002d, 0x900bb, 0x8000d, 0x8008d, 0x8004d, 0x900fb, 0x70101, 0x80053, 0x80013, 0x8011b, 0x70111, 0x80073, 0x80033, 0x900c7, 0x70109, 0x80063, 0x80023, 0x900a7, 0x80003, 0x80083, 0x80043, 0x900e7, 0x70105, 0x8005b, 0x8001b, 0x90097, 0x70115, 0x8007b, 0x8003b, 0x900d7, 0x7010d, 0x8006b, 0x8002b, 0x900b7, 0x8000b, 0x8008b, 0x8004b, 0x900f7, 0x70103, 0x80057, 0x80017, 0x8011f, 0x70113, 0x80077, 0x80037, 0x900cf, 0x7010b, 0x80067, 0x80027, 0x900af, 0x80007, 0x80087, 0x80047, 0x900ef, 0x70107, 0x8005f, 0x8001f, 0x9009f, 0x70117, 0x8007f, 0x8003f, 0x900df, 0x7010f, 0x8006f, 0x8002f, 0x900bf, 0x8000f, 0x8008f, 0x8004f, 0x900ff]), 9];
  33. const fixedDistCodeTab = [new Int32Array([0x50000, 0x50010, 0x50008, 0x50018, 0x50004, 0x50014, 0x5000c, 0x5001c, 0x50002, 0x50012, 0x5000a, 0x5001a, 0x50006, 0x50016, 0x5000e, 0x00000, 0x50001, 0x50011, 0x50009, 0x50019, 0x50005, 0x50015, 0x5000d, 0x5001d, 0x50003, 0x50013, 0x5000b, 0x5001b, 0x50007, 0x50017, 0x5000f, 0x00000]), 5];
  34. class FlateStream extends _decode_stream.DecodeStream {
  35. constructor(str, maybeLength) {
  36. super(maybeLength);
  37. this.str = str;
  38. this.dict = str.dict;
  39. const cmf = str.getByte();
  40. const flg = str.getByte();
  41. if (cmf === -1 || flg === -1) {
  42. throw new _util.FormatError(`Invalid header in flate stream: ${cmf}, ${flg}`);
  43. }
  44. if ((cmf & 0x0f) !== 0x08) {
  45. throw new _util.FormatError(`Unknown compression method in flate stream: ${cmf}, ${flg}`);
  46. }
  47. if (((cmf << 8) + flg) % 31 !== 0) {
  48. throw new _util.FormatError(`Bad FCHECK in flate stream: ${cmf}, ${flg}`);
  49. }
  50. if (flg & 0x20) {
  51. throw new _util.FormatError(`FDICT bit set in flate stream: ${cmf}, ${flg}`);
  52. }
  53. this.codeSize = 0;
  54. this.codeBuf = 0;
  55. }
  56. getBits(bits) {
  57. const str = this.str;
  58. let codeSize = this.codeSize;
  59. let codeBuf = this.codeBuf;
  60. let b;
  61. while (codeSize < bits) {
  62. if ((b = str.getByte()) === -1) {
  63. throw new _util.FormatError("Bad encoding in flate stream");
  64. }
  65. codeBuf |= b << codeSize;
  66. codeSize += 8;
  67. }
  68. b = codeBuf & (1 << bits) - 1;
  69. this.codeBuf = codeBuf >> bits;
  70. this.codeSize = codeSize -= bits;
  71. return b;
  72. }
  73. getCode(table) {
  74. const str = this.str;
  75. const codes = table[0];
  76. const maxLen = table[1];
  77. let codeSize = this.codeSize;
  78. let codeBuf = this.codeBuf;
  79. let b;
  80. while (codeSize < maxLen) {
  81. if ((b = str.getByte()) === -1) {
  82. break;
  83. }
  84. codeBuf |= b << codeSize;
  85. codeSize += 8;
  86. }
  87. const code = codes[codeBuf & (1 << maxLen) - 1];
  88. const codeLen = code >> 16;
  89. const codeVal = code & 0xffff;
  90. if (codeLen < 1 || codeSize < codeLen) {
  91. throw new _util.FormatError("Bad encoding in flate stream");
  92. }
  93. this.codeBuf = codeBuf >> codeLen;
  94. this.codeSize = codeSize - codeLen;
  95. return codeVal;
  96. }
  97. generateHuffmanTable(lengths) {
  98. const n = lengths.length;
  99. let maxLen = 0;
  100. let i;
  101. for (i = 0; i < n; ++i) {
  102. if (lengths[i] > maxLen) {
  103. maxLen = lengths[i];
  104. }
  105. }
  106. const size = 1 << maxLen;
  107. const codes = new Int32Array(size);
  108. for (let len = 1, code = 0, skip = 2; len <= maxLen; ++len, code <<= 1, skip <<= 1) {
  109. for (let val = 0; val < n; ++val) {
  110. if (lengths[val] === len) {
  111. let code2 = 0;
  112. let t = code;
  113. for (i = 0; i < len; ++i) {
  114. code2 = code2 << 1 | t & 1;
  115. t >>= 1;
  116. }
  117. for (i = code2; i < size; i += skip) {
  118. codes[i] = len << 16 | val;
  119. }
  120. ++code;
  121. }
  122. }
  123. }
  124. return [codes, maxLen];
  125. }
  126. readBlock() {
  127. let buffer, len;
  128. const str = this.str;
  129. let hdr = this.getBits(3);
  130. if (hdr & 1) {
  131. this.eof = true;
  132. }
  133. hdr >>= 1;
  134. if (hdr === 0) {
  135. let b;
  136. if ((b = str.getByte()) === -1) {
  137. throw new _util.FormatError("Bad block header in flate stream");
  138. }
  139. let blockLen = b;
  140. if ((b = str.getByte()) === -1) {
  141. throw new _util.FormatError("Bad block header in flate stream");
  142. }
  143. blockLen |= b << 8;
  144. if ((b = str.getByte()) === -1) {
  145. throw new _util.FormatError("Bad block header in flate stream");
  146. }
  147. let check = b;
  148. if ((b = str.getByte()) === -1) {
  149. throw new _util.FormatError("Bad block header in flate stream");
  150. }
  151. check |= b << 8;
  152. if (check !== (~blockLen & 0xffff) && (blockLen !== 0 || check !== 0)) {
  153. throw new _util.FormatError("Bad uncompressed block length in flate stream");
  154. }
  155. this.codeBuf = 0;
  156. this.codeSize = 0;
  157. const bufferLength = this.bufferLength,
  158. end = bufferLength + blockLen;
  159. buffer = this.ensureBuffer(end);
  160. this.bufferLength = end;
  161. if (blockLen === 0) {
  162. if (str.peekByte() === -1) {
  163. this.eof = true;
  164. }
  165. } else {
  166. const block = str.getBytes(blockLen);
  167. buffer.set(block, bufferLength);
  168. if (block.length < blockLen) {
  169. this.eof = true;
  170. }
  171. }
  172. return;
  173. }
  174. let litCodeTable;
  175. let distCodeTable;
  176. if (hdr === 1) {
  177. litCodeTable = fixedLitCodeTab;
  178. distCodeTable = fixedDistCodeTab;
  179. } else if (hdr === 2) {
  180. const numLitCodes = this.getBits(5) + 257;
  181. const numDistCodes = this.getBits(5) + 1;
  182. const numCodeLenCodes = this.getBits(4) + 4;
  183. const codeLenCodeLengths = new Uint8Array(codeLenCodeMap.length);
  184. let i;
  185. for (i = 0; i < numCodeLenCodes; ++i) {
  186. codeLenCodeLengths[codeLenCodeMap[i]] = this.getBits(3);
  187. }
  188. const codeLenCodeTab = this.generateHuffmanTable(codeLenCodeLengths);
  189. len = 0;
  190. i = 0;
  191. const codes = numLitCodes + numDistCodes;
  192. const codeLengths = new Uint8Array(codes);
  193. let bitsLength, bitsOffset, what;
  194. while (i < codes) {
  195. const code = this.getCode(codeLenCodeTab);
  196. if (code === 16) {
  197. bitsLength = 2;
  198. bitsOffset = 3;
  199. what = len;
  200. } else if (code === 17) {
  201. bitsLength = 3;
  202. bitsOffset = 3;
  203. what = len = 0;
  204. } else if (code === 18) {
  205. bitsLength = 7;
  206. bitsOffset = 11;
  207. what = len = 0;
  208. } else {
  209. codeLengths[i++] = len = code;
  210. continue;
  211. }
  212. let repeatLength = this.getBits(bitsLength) + bitsOffset;
  213. while (repeatLength-- > 0) {
  214. codeLengths[i++] = what;
  215. }
  216. }
  217. litCodeTable = this.generateHuffmanTable(codeLengths.subarray(0, numLitCodes));
  218. distCodeTable = this.generateHuffmanTable(codeLengths.subarray(numLitCodes, codes));
  219. } else {
  220. throw new _util.FormatError("Unknown block type in flate stream");
  221. }
  222. buffer = this.buffer;
  223. let limit = buffer ? buffer.length : 0;
  224. let pos = this.bufferLength;
  225. while (true) {
  226. let code1 = this.getCode(litCodeTable);
  227. if (code1 < 256) {
  228. if (pos + 1 >= limit) {
  229. buffer = this.ensureBuffer(pos + 1);
  230. limit = buffer.length;
  231. }
  232. buffer[pos++] = code1;
  233. continue;
  234. }
  235. if (code1 === 256) {
  236. this.bufferLength = pos;
  237. return;
  238. }
  239. code1 -= 257;
  240. code1 = lengthDecode[code1];
  241. let code2 = code1 >> 16;
  242. if (code2 > 0) {
  243. code2 = this.getBits(code2);
  244. }
  245. len = (code1 & 0xffff) + code2;
  246. code1 = this.getCode(distCodeTable);
  247. code1 = distDecode[code1];
  248. code2 = code1 >> 16;
  249. if (code2 > 0) {
  250. code2 = this.getBits(code2);
  251. }
  252. const dist = (code1 & 0xffff) + code2;
  253. if (pos + len >= limit) {
  254. buffer = this.ensureBuffer(pos + len);
  255. limit = buffer.length;
  256. }
  257. for (let k = 0; k < len; ++k, ++pos) {
  258. buffer[pos] = buffer[pos - dist];
  259. }
  260. }
  261. }
  262. }
  263. exports.FlateStream = FlateStream;