flate_stream.js 14 KB

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