stream.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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 { FormatError, stringToBytes, unreachable } from "./util.js";
  22. import { isDict } from "./primitives.js";
  23. import { isWhiteSpace } from "./core_utils.js";
  24. var Stream = (function StreamClosure() {
  25. // eslint-disable-next-line no-shadow
  26. function Stream(arrayBuffer, start, length, dict) {
  27. this.bytes =
  28. arrayBuffer instanceof Uint8Array
  29. ? arrayBuffer
  30. : new Uint8Array(arrayBuffer);
  31. this.start = start || 0;
  32. this.pos = this.start;
  33. this.end = start + length || this.bytes.length;
  34. this.dict = dict;
  35. }
  36. // required methods for a stream. if a particular stream does not
  37. // implement these, an error should be thrown
  38. Stream.prototype = {
  39. get length() {
  40. return this.end - this.start;
  41. },
  42. get isEmpty() {
  43. return this.length === 0;
  44. },
  45. getByte: function Stream_getByte() {
  46. if (this.pos >= this.end) {
  47. return -1;
  48. }
  49. return this.bytes[this.pos++];
  50. },
  51. getUint16: function Stream_getUint16() {
  52. var b0 = this.getByte();
  53. var b1 = this.getByte();
  54. if (b0 === -1 || b1 === -1) {
  55. return -1;
  56. }
  57. return (b0 << 8) + b1;
  58. },
  59. getInt32: function Stream_getInt32() {
  60. var b0 = this.getByte();
  61. var b1 = this.getByte();
  62. var b2 = this.getByte();
  63. var b3 = this.getByte();
  64. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  65. },
  66. // Returns subarray of original buffer, should only be read.
  67. getBytes(length, forceClamped = false) {
  68. var bytes = this.bytes;
  69. var pos = this.pos;
  70. var strEnd = this.end;
  71. if (!length) {
  72. const subarray = bytes.subarray(pos, strEnd);
  73. // `this.bytes` is always a `Uint8Array` here.
  74. return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
  75. }
  76. var end = pos + length;
  77. if (end > strEnd) {
  78. end = strEnd;
  79. }
  80. this.pos = end;
  81. const subarray = bytes.subarray(pos, end);
  82. // `this.bytes` is always a `Uint8Array` here.
  83. return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
  84. },
  85. peekByte: function Stream_peekByte() {
  86. var peekedByte = this.getByte();
  87. if (peekedByte !== -1) {
  88. this.pos--;
  89. }
  90. return peekedByte;
  91. },
  92. peekBytes(length, forceClamped = false) {
  93. var bytes = this.getBytes(length, forceClamped);
  94. this.pos -= bytes.length;
  95. return bytes;
  96. },
  97. getByteRange(begin, end) {
  98. if (begin < 0) {
  99. begin = 0;
  100. }
  101. if (end > this.end) {
  102. end = this.end;
  103. }
  104. return this.bytes.subarray(begin, end);
  105. },
  106. skip: function Stream_skip(n) {
  107. if (!n) {
  108. n = 1;
  109. }
  110. this.pos += n;
  111. },
  112. reset: function Stream_reset() {
  113. this.pos = this.start;
  114. },
  115. moveStart: function Stream_moveStart() {
  116. this.start = this.pos;
  117. },
  118. makeSubStream: function Stream_makeSubStream(start, length, dict) {
  119. return new Stream(this.bytes.buffer, start, length, dict);
  120. },
  121. };
  122. return Stream;
  123. })();
  124. var StringStream = (function StringStreamClosure() {
  125. // eslint-disable-next-line no-shadow
  126. function StringStream(str) {
  127. const bytes = stringToBytes(str);
  128. Stream.call(this, bytes);
  129. }
  130. StringStream.prototype = Stream.prototype;
  131. return StringStream;
  132. })();
  133. // super class for the decoding streams
  134. var DecodeStream = (function DecodeStreamClosure() {
  135. // Lots of DecodeStreams are created whose buffers are never used. For these
  136. // we share a single empty buffer. This is (a) space-efficient and (b) avoids
  137. // having special cases that would be required if we used |null| for an empty
  138. // buffer.
  139. var emptyBuffer = new Uint8Array(0);
  140. // eslint-disable-next-line no-shadow
  141. function DecodeStream(maybeMinBufferLength) {
  142. this._rawMinBufferLength = maybeMinBufferLength || 0;
  143. this.pos = 0;
  144. this.bufferLength = 0;
  145. this.eof = false;
  146. this.buffer = emptyBuffer;
  147. this.minBufferLength = 512;
  148. if (maybeMinBufferLength) {
  149. // Compute the first power of two that is as big as maybeMinBufferLength.
  150. while (this.minBufferLength < maybeMinBufferLength) {
  151. this.minBufferLength *= 2;
  152. }
  153. }
  154. }
  155. DecodeStream.prototype = {
  156. get isEmpty() {
  157. while (!this.eof && this.bufferLength === 0) {
  158. this.readBlock();
  159. }
  160. return this.bufferLength === 0;
  161. },
  162. ensureBuffer: function DecodeStream_ensureBuffer(requested) {
  163. var buffer = this.buffer;
  164. if (requested <= buffer.byteLength) {
  165. return buffer;
  166. }
  167. var size = this.minBufferLength;
  168. while (size < requested) {
  169. size *= 2;
  170. }
  171. var buffer2 = new Uint8Array(size);
  172. buffer2.set(buffer);
  173. return (this.buffer = buffer2);
  174. },
  175. getByte: function DecodeStream_getByte() {
  176. var pos = this.pos;
  177. while (this.bufferLength <= pos) {
  178. if (this.eof) {
  179. return -1;
  180. }
  181. this.readBlock();
  182. }
  183. return this.buffer[this.pos++];
  184. },
  185. getUint16: function DecodeStream_getUint16() {
  186. var b0 = this.getByte();
  187. var b1 = this.getByte();
  188. if (b0 === -1 || b1 === -1) {
  189. return -1;
  190. }
  191. return (b0 << 8) + b1;
  192. },
  193. getInt32: function DecodeStream_getInt32() {
  194. var b0 = this.getByte();
  195. var b1 = this.getByte();
  196. var b2 = this.getByte();
  197. var b3 = this.getByte();
  198. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  199. },
  200. getBytes(length, forceClamped = false) {
  201. var end,
  202. pos = this.pos;
  203. if (length) {
  204. this.ensureBuffer(pos + length);
  205. end = pos + length;
  206. while (!this.eof && this.bufferLength < end) {
  207. this.readBlock();
  208. }
  209. var bufEnd = this.bufferLength;
  210. if (end > bufEnd) {
  211. end = bufEnd;
  212. }
  213. } else {
  214. while (!this.eof) {
  215. this.readBlock();
  216. }
  217. end = this.bufferLength;
  218. }
  219. this.pos = end;
  220. const subarray = this.buffer.subarray(pos, end);
  221. // `this.buffer` is either a `Uint8Array` or `Uint8ClampedArray` here.
  222. return forceClamped && !(subarray instanceof Uint8ClampedArray)
  223. ? new Uint8ClampedArray(subarray)
  224. : subarray;
  225. },
  226. peekByte: function DecodeStream_peekByte() {
  227. var peekedByte = this.getByte();
  228. if (peekedByte !== -1) {
  229. this.pos--;
  230. }
  231. return peekedByte;
  232. },
  233. peekBytes(length, forceClamped = false) {
  234. var bytes = this.getBytes(length, forceClamped);
  235. this.pos -= bytes.length;
  236. return bytes;
  237. },
  238. makeSubStream: function DecodeStream_makeSubStream(start, length, dict) {
  239. var end = start + length;
  240. while (this.bufferLength <= end && !this.eof) {
  241. this.readBlock();
  242. }
  243. return new Stream(this.buffer, start, length, dict);
  244. },
  245. getByteRange(begin, end) {
  246. unreachable("Should not call DecodeStream.getByteRange");
  247. },
  248. skip: function DecodeStream_skip(n) {
  249. if (!n) {
  250. n = 1;
  251. }
  252. this.pos += n;
  253. },
  254. reset: function DecodeStream_reset() {
  255. this.pos = 0;
  256. },
  257. getBaseStreams: function DecodeStream_getBaseStreams() {
  258. if (this.str && this.str.getBaseStreams) {
  259. return this.str.getBaseStreams();
  260. }
  261. return [];
  262. },
  263. };
  264. return DecodeStream;
  265. })();
  266. var StreamsSequenceStream = (function StreamsSequenceStreamClosure() {
  267. // eslint-disable-next-line no-shadow
  268. function StreamsSequenceStream(streams) {
  269. this.streams = streams;
  270. let maybeLength = 0;
  271. for (let i = 0, ii = streams.length; i < ii; i++) {
  272. const stream = streams[i];
  273. if (stream instanceof DecodeStream) {
  274. maybeLength += stream._rawMinBufferLength;
  275. } else {
  276. maybeLength += stream.length;
  277. }
  278. }
  279. DecodeStream.call(this, maybeLength);
  280. }
  281. StreamsSequenceStream.prototype = Object.create(DecodeStream.prototype);
  282. StreamsSequenceStream.prototype.readBlock = function streamSequenceStreamReadBlock() {
  283. var streams = this.streams;
  284. if (streams.length === 0) {
  285. this.eof = true;
  286. return;
  287. }
  288. var stream = streams.shift();
  289. var chunk = stream.getBytes();
  290. var bufferLength = this.bufferLength;
  291. var newLength = bufferLength + chunk.length;
  292. var buffer = this.ensureBuffer(newLength);
  293. buffer.set(chunk, bufferLength);
  294. this.bufferLength = newLength;
  295. };
  296. StreamsSequenceStream.prototype.getBaseStreams = function StreamsSequenceStream_getBaseStreams() {
  297. var baseStreams = [];
  298. for (var i = 0, ii = this.streams.length; i < ii; i++) {
  299. var stream = this.streams[i];
  300. if (stream.getBaseStreams) {
  301. baseStreams.push(...stream.getBaseStreams());
  302. }
  303. }
  304. return baseStreams;
  305. };
  306. return StreamsSequenceStream;
  307. })();
  308. var FlateStream = (function FlateStreamClosure() {
  309. // prettier-ignore
  310. var codeLenCodeMap = new Int32Array([
  311. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  312. ]);
  313. // prettier-ignore
  314. var lengthDecode = new Int32Array([
  315. 0x00003, 0x00004, 0x00005, 0x00006, 0x00007, 0x00008, 0x00009, 0x0000a,
  316. 0x1000b, 0x1000d, 0x1000f, 0x10011, 0x20013, 0x20017, 0x2001b, 0x2001f,
  317. 0x30023, 0x3002b, 0x30033, 0x3003b, 0x40043, 0x40053, 0x40063, 0x40073,
  318. 0x50083, 0x500a3, 0x500c3, 0x500e3, 0x00102, 0x00102, 0x00102
  319. ]);
  320. // prettier-ignore
  321. var distDecode = new Int32Array([
  322. 0x00001, 0x00002, 0x00003, 0x00004, 0x10005, 0x10007, 0x20009, 0x2000d,
  323. 0x30011, 0x30019, 0x40021, 0x40031, 0x50041, 0x50061, 0x60081, 0x600c1,
  324. 0x70101, 0x70181, 0x80201, 0x80301, 0x90401, 0x90601, 0xa0801, 0xa0c01,
  325. 0xb1001, 0xb1801, 0xc2001, 0xc3001, 0xd4001, 0xd6001
  326. ]);
  327. // prettier-ignore
  328. var fixedLitCodeTab = [new Int32Array([
  329. 0x70100, 0x80050, 0x80010, 0x80118, 0x70110, 0x80070, 0x80030, 0x900c0,
  330. 0x70108, 0x80060, 0x80020, 0x900a0, 0x80000, 0x80080, 0x80040, 0x900e0,
  331. 0x70104, 0x80058, 0x80018, 0x90090, 0x70114, 0x80078, 0x80038, 0x900d0,
  332. 0x7010c, 0x80068, 0x80028, 0x900b0, 0x80008, 0x80088, 0x80048, 0x900f0,
  333. 0x70102, 0x80054, 0x80014, 0x8011c, 0x70112, 0x80074, 0x80034, 0x900c8,
  334. 0x7010a, 0x80064, 0x80024, 0x900a8, 0x80004, 0x80084, 0x80044, 0x900e8,
  335. 0x70106, 0x8005c, 0x8001c, 0x90098, 0x70116, 0x8007c, 0x8003c, 0x900d8,
  336. 0x7010e, 0x8006c, 0x8002c, 0x900b8, 0x8000c, 0x8008c, 0x8004c, 0x900f8,
  337. 0x70101, 0x80052, 0x80012, 0x8011a, 0x70111, 0x80072, 0x80032, 0x900c4,
  338. 0x70109, 0x80062, 0x80022, 0x900a4, 0x80002, 0x80082, 0x80042, 0x900e4,
  339. 0x70105, 0x8005a, 0x8001a, 0x90094, 0x70115, 0x8007a, 0x8003a, 0x900d4,
  340. 0x7010d, 0x8006a, 0x8002a, 0x900b4, 0x8000a, 0x8008a, 0x8004a, 0x900f4,
  341. 0x70103, 0x80056, 0x80016, 0x8011e, 0x70113, 0x80076, 0x80036, 0x900cc,
  342. 0x7010b, 0x80066, 0x80026, 0x900ac, 0x80006, 0x80086, 0x80046, 0x900ec,
  343. 0x70107, 0x8005e, 0x8001e, 0x9009c, 0x70117, 0x8007e, 0x8003e, 0x900dc,
  344. 0x7010f, 0x8006e, 0x8002e, 0x900bc, 0x8000e, 0x8008e, 0x8004e, 0x900fc,
  345. 0x70100, 0x80051, 0x80011, 0x80119, 0x70110, 0x80071, 0x80031, 0x900c2,
  346. 0x70108, 0x80061, 0x80021, 0x900a2, 0x80001, 0x80081, 0x80041, 0x900e2,
  347. 0x70104, 0x80059, 0x80019, 0x90092, 0x70114, 0x80079, 0x80039, 0x900d2,
  348. 0x7010c, 0x80069, 0x80029, 0x900b2, 0x80009, 0x80089, 0x80049, 0x900f2,
  349. 0x70102, 0x80055, 0x80015, 0x8011d, 0x70112, 0x80075, 0x80035, 0x900ca,
  350. 0x7010a, 0x80065, 0x80025, 0x900aa, 0x80005, 0x80085, 0x80045, 0x900ea,
  351. 0x70106, 0x8005d, 0x8001d, 0x9009a, 0x70116, 0x8007d, 0x8003d, 0x900da,
  352. 0x7010e, 0x8006d, 0x8002d, 0x900ba, 0x8000d, 0x8008d, 0x8004d, 0x900fa,
  353. 0x70101, 0x80053, 0x80013, 0x8011b, 0x70111, 0x80073, 0x80033, 0x900c6,
  354. 0x70109, 0x80063, 0x80023, 0x900a6, 0x80003, 0x80083, 0x80043, 0x900e6,
  355. 0x70105, 0x8005b, 0x8001b, 0x90096, 0x70115, 0x8007b, 0x8003b, 0x900d6,
  356. 0x7010d, 0x8006b, 0x8002b, 0x900b6, 0x8000b, 0x8008b, 0x8004b, 0x900f6,
  357. 0x70103, 0x80057, 0x80017, 0x8011f, 0x70113, 0x80077, 0x80037, 0x900ce,
  358. 0x7010b, 0x80067, 0x80027, 0x900ae, 0x80007, 0x80087, 0x80047, 0x900ee,
  359. 0x70107, 0x8005f, 0x8001f, 0x9009e, 0x70117, 0x8007f, 0x8003f, 0x900de,
  360. 0x7010f, 0x8006f, 0x8002f, 0x900be, 0x8000f, 0x8008f, 0x8004f, 0x900fe,
  361. 0x70100, 0x80050, 0x80010, 0x80118, 0x70110, 0x80070, 0x80030, 0x900c1,
  362. 0x70108, 0x80060, 0x80020, 0x900a1, 0x80000, 0x80080, 0x80040, 0x900e1,
  363. 0x70104, 0x80058, 0x80018, 0x90091, 0x70114, 0x80078, 0x80038, 0x900d1,
  364. 0x7010c, 0x80068, 0x80028, 0x900b1, 0x80008, 0x80088, 0x80048, 0x900f1,
  365. 0x70102, 0x80054, 0x80014, 0x8011c, 0x70112, 0x80074, 0x80034, 0x900c9,
  366. 0x7010a, 0x80064, 0x80024, 0x900a9, 0x80004, 0x80084, 0x80044, 0x900e9,
  367. 0x70106, 0x8005c, 0x8001c, 0x90099, 0x70116, 0x8007c, 0x8003c, 0x900d9,
  368. 0x7010e, 0x8006c, 0x8002c, 0x900b9, 0x8000c, 0x8008c, 0x8004c, 0x900f9,
  369. 0x70101, 0x80052, 0x80012, 0x8011a, 0x70111, 0x80072, 0x80032, 0x900c5,
  370. 0x70109, 0x80062, 0x80022, 0x900a5, 0x80002, 0x80082, 0x80042, 0x900e5,
  371. 0x70105, 0x8005a, 0x8001a, 0x90095, 0x70115, 0x8007a, 0x8003a, 0x900d5,
  372. 0x7010d, 0x8006a, 0x8002a, 0x900b5, 0x8000a, 0x8008a, 0x8004a, 0x900f5,
  373. 0x70103, 0x80056, 0x80016, 0x8011e, 0x70113, 0x80076, 0x80036, 0x900cd,
  374. 0x7010b, 0x80066, 0x80026, 0x900ad, 0x80006, 0x80086, 0x80046, 0x900ed,
  375. 0x70107, 0x8005e, 0x8001e, 0x9009d, 0x70117, 0x8007e, 0x8003e, 0x900dd,
  376. 0x7010f, 0x8006e, 0x8002e, 0x900bd, 0x8000e, 0x8008e, 0x8004e, 0x900fd,
  377. 0x70100, 0x80051, 0x80011, 0x80119, 0x70110, 0x80071, 0x80031, 0x900c3,
  378. 0x70108, 0x80061, 0x80021, 0x900a3, 0x80001, 0x80081, 0x80041, 0x900e3,
  379. 0x70104, 0x80059, 0x80019, 0x90093, 0x70114, 0x80079, 0x80039, 0x900d3,
  380. 0x7010c, 0x80069, 0x80029, 0x900b3, 0x80009, 0x80089, 0x80049, 0x900f3,
  381. 0x70102, 0x80055, 0x80015, 0x8011d, 0x70112, 0x80075, 0x80035, 0x900cb,
  382. 0x7010a, 0x80065, 0x80025, 0x900ab, 0x80005, 0x80085, 0x80045, 0x900eb,
  383. 0x70106, 0x8005d, 0x8001d, 0x9009b, 0x70116, 0x8007d, 0x8003d, 0x900db,
  384. 0x7010e, 0x8006d, 0x8002d, 0x900bb, 0x8000d, 0x8008d, 0x8004d, 0x900fb,
  385. 0x70101, 0x80053, 0x80013, 0x8011b, 0x70111, 0x80073, 0x80033, 0x900c7,
  386. 0x70109, 0x80063, 0x80023, 0x900a7, 0x80003, 0x80083, 0x80043, 0x900e7,
  387. 0x70105, 0x8005b, 0x8001b, 0x90097, 0x70115, 0x8007b, 0x8003b, 0x900d7,
  388. 0x7010d, 0x8006b, 0x8002b, 0x900b7, 0x8000b, 0x8008b, 0x8004b, 0x900f7,
  389. 0x70103, 0x80057, 0x80017, 0x8011f, 0x70113, 0x80077, 0x80037, 0x900cf,
  390. 0x7010b, 0x80067, 0x80027, 0x900af, 0x80007, 0x80087, 0x80047, 0x900ef,
  391. 0x70107, 0x8005f, 0x8001f, 0x9009f, 0x70117, 0x8007f, 0x8003f, 0x900df,
  392. 0x7010f, 0x8006f, 0x8002f, 0x900bf, 0x8000f, 0x8008f, 0x8004f, 0x900ff
  393. ]), 9];
  394. // prettier-ignore
  395. var fixedDistCodeTab = [new Int32Array([
  396. 0x50000, 0x50010, 0x50008, 0x50018, 0x50004, 0x50014, 0x5000c, 0x5001c,
  397. 0x50002, 0x50012, 0x5000a, 0x5001a, 0x50006, 0x50016, 0x5000e, 0x00000,
  398. 0x50001, 0x50011, 0x50009, 0x50019, 0x50005, 0x50015, 0x5000d, 0x5001d,
  399. 0x50003, 0x50013, 0x5000b, 0x5001b, 0x50007, 0x50017, 0x5000f, 0x00000
  400. ]), 5];
  401. // eslint-disable-next-line no-shadow
  402. function FlateStream(str, maybeLength) {
  403. this.str = str;
  404. this.dict = str.dict;
  405. var cmf = str.getByte();
  406. var flg = str.getByte();
  407. if (cmf === -1 || flg === -1) {
  408. throw new FormatError(`Invalid header in flate stream: ${cmf}, ${flg}`);
  409. }
  410. if ((cmf & 0x0f) !== 0x08) {
  411. throw new FormatError(
  412. `Unknown compression method in flate stream: ${cmf}, ${flg}`
  413. );
  414. }
  415. if (((cmf << 8) + flg) % 31 !== 0) {
  416. throw new FormatError(`Bad FCHECK in flate stream: ${cmf}, ${flg}`);
  417. }
  418. if (flg & 0x20) {
  419. throw new FormatError(`FDICT bit set in flate stream: ${cmf}, ${flg}`);
  420. }
  421. this.codeSize = 0;
  422. this.codeBuf = 0;
  423. DecodeStream.call(this, maybeLength);
  424. }
  425. FlateStream.prototype = Object.create(DecodeStream.prototype);
  426. FlateStream.prototype.getBits = function FlateStream_getBits(bits) {
  427. var str = this.str;
  428. var codeSize = this.codeSize;
  429. var codeBuf = this.codeBuf;
  430. var b;
  431. while (codeSize < bits) {
  432. if ((b = str.getByte()) === -1) {
  433. throw new FormatError("Bad encoding in flate stream");
  434. }
  435. codeBuf |= b << codeSize;
  436. codeSize += 8;
  437. }
  438. b = codeBuf & ((1 << bits) - 1);
  439. this.codeBuf = codeBuf >> bits;
  440. this.codeSize = codeSize -= bits;
  441. return b;
  442. };
  443. FlateStream.prototype.getCode = function FlateStream_getCode(table) {
  444. var str = this.str;
  445. var codes = table[0];
  446. var maxLen = table[1];
  447. var codeSize = this.codeSize;
  448. var codeBuf = this.codeBuf;
  449. var b;
  450. while (codeSize < maxLen) {
  451. if ((b = str.getByte()) === -1) {
  452. // premature end of stream. code might however still be valid.
  453. // codeSize < codeLen check below guards against incomplete codeVal.
  454. break;
  455. }
  456. codeBuf |= b << codeSize;
  457. codeSize += 8;
  458. }
  459. var code = codes[codeBuf & ((1 << maxLen) - 1)];
  460. var codeLen = code >> 16;
  461. var codeVal = code & 0xffff;
  462. if (codeLen < 1 || codeSize < codeLen) {
  463. throw new FormatError("Bad encoding in flate stream");
  464. }
  465. this.codeBuf = codeBuf >> codeLen;
  466. this.codeSize = codeSize - codeLen;
  467. return codeVal;
  468. };
  469. FlateStream.prototype.generateHuffmanTable = function flateStreamGenerateHuffmanTable(
  470. lengths
  471. ) {
  472. var n = lengths.length;
  473. // find max code length
  474. var maxLen = 0;
  475. var i;
  476. for (i = 0; i < n; ++i) {
  477. if (lengths[i] > maxLen) {
  478. maxLen = lengths[i];
  479. }
  480. }
  481. // build the table
  482. var size = 1 << maxLen;
  483. var codes = new Int32Array(size);
  484. for (
  485. var len = 1, code = 0, skip = 2;
  486. len <= maxLen;
  487. ++len, code <<= 1, skip <<= 1
  488. ) {
  489. for (var val = 0; val < n; ++val) {
  490. if (lengths[val] === len) {
  491. // bit-reverse the code
  492. var code2 = 0;
  493. var t = code;
  494. for (i = 0; i < len; ++i) {
  495. code2 = (code2 << 1) | (t & 1);
  496. t >>= 1;
  497. }
  498. // fill the table entries
  499. for (i = code2; i < size; i += skip) {
  500. codes[i] = (len << 16) | val;
  501. }
  502. ++code;
  503. }
  504. }
  505. }
  506. return [codes, maxLen];
  507. };
  508. FlateStream.prototype.readBlock = function FlateStream_readBlock() {
  509. var buffer, len;
  510. var str = this.str;
  511. // read block header
  512. var hdr = this.getBits(3);
  513. if (hdr & 1) {
  514. this.eof = true;
  515. }
  516. hdr >>= 1;
  517. if (hdr === 0) {
  518. // uncompressed block
  519. var b;
  520. if ((b = str.getByte()) === -1) {
  521. throw new FormatError("Bad block header in flate stream");
  522. }
  523. var blockLen = b;
  524. if ((b = str.getByte()) === -1) {
  525. throw new FormatError("Bad block header in flate stream");
  526. }
  527. blockLen |= b << 8;
  528. if ((b = str.getByte()) === -1) {
  529. throw new FormatError("Bad block header in flate stream");
  530. }
  531. var check = b;
  532. if ((b = str.getByte()) === -1) {
  533. throw new FormatError("Bad block header in flate stream");
  534. }
  535. check |= b << 8;
  536. if (check !== (~blockLen & 0xffff) && (blockLen !== 0 || check !== 0)) {
  537. // Ignoring error for bad "empty" block (see issue 1277)
  538. throw new FormatError("Bad uncompressed block length in flate stream");
  539. }
  540. this.codeBuf = 0;
  541. this.codeSize = 0;
  542. const bufferLength = this.bufferLength,
  543. end = bufferLength + blockLen;
  544. buffer = this.ensureBuffer(end);
  545. this.bufferLength = end;
  546. if (blockLen === 0) {
  547. if (str.peekByte() === -1) {
  548. this.eof = true;
  549. }
  550. } else {
  551. const block = str.getBytes(blockLen);
  552. buffer.set(block, bufferLength);
  553. if (block.length < blockLen) {
  554. this.eof = true;
  555. }
  556. }
  557. return;
  558. }
  559. var litCodeTable;
  560. var distCodeTable;
  561. if (hdr === 1) {
  562. // compressed block, fixed codes
  563. litCodeTable = fixedLitCodeTab;
  564. distCodeTable = fixedDistCodeTab;
  565. } else if (hdr === 2) {
  566. // compressed block, dynamic codes
  567. var numLitCodes = this.getBits(5) + 257;
  568. var numDistCodes = this.getBits(5) + 1;
  569. var numCodeLenCodes = this.getBits(4) + 4;
  570. // build the code lengths code table
  571. var codeLenCodeLengths = new Uint8Array(codeLenCodeMap.length);
  572. var i;
  573. for (i = 0; i < numCodeLenCodes; ++i) {
  574. codeLenCodeLengths[codeLenCodeMap[i]] = this.getBits(3);
  575. }
  576. var codeLenCodeTab = this.generateHuffmanTable(codeLenCodeLengths);
  577. // build the literal and distance code tables
  578. len = 0;
  579. i = 0;
  580. var codes = numLitCodes + numDistCodes;
  581. var codeLengths = new Uint8Array(codes);
  582. var bitsLength, bitsOffset, what;
  583. while (i < codes) {
  584. var code = this.getCode(codeLenCodeTab);
  585. if (code === 16) {
  586. bitsLength = 2;
  587. bitsOffset = 3;
  588. what = len;
  589. } else if (code === 17) {
  590. bitsLength = 3;
  591. bitsOffset = 3;
  592. what = len = 0;
  593. } else if (code === 18) {
  594. bitsLength = 7;
  595. bitsOffset = 11;
  596. what = len = 0;
  597. } else {
  598. codeLengths[i++] = len = code;
  599. continue;
  600. }
  601. var repeatLength = this.getBits(bitsLength) + bitsOffset;
  602. while (repeatLength-- > 0) {
  603. codeLengths[i++] = what;
  604. }
  605. }
  606. litCodeTable = this.generateHuffmanTable(
  607. codeLengths.subarray(0, numLitCodes)
  608. );
  609. distCodeTable = this.generateHuffmanTable(
  610. codeLengths.subarray(numLitCodes, codes)
  611. );
  612. } else {
  613. throw new FormatError("Unknown block type in flate stream");
  614. }
  615. buffer = this.buffer;
  616. var limit = buffer ? buffer.length : 0;
  617. var pos = this.bufferLength;
  618. while (true) {
  619. var code1 = this.getCode(litCodeTable);
  620. if (code1 < 256) {
  621. if (pos + 1 >= limit) {
  622. buffer = this.ensureBuffer(pos + 1);
  623. limit = buffer.length;
  624. }
  625. buffer[pos++] = code1;
  626. continue;
  627. }
  628. if (code1 === 256) {
  629. this.bufferLength = pos;
  630. return;
  631. }
  632. code1 -= 257;
  633. code1 = lengthDecode[code1];
  634. var code2 = code1 >> 16;
  635. if (code2 > 0) {
  636. code2 = this.getBits(code2);
  637. }
  638. len = (code1 & 0xffff) + code2;
  639. code1 = this.getCode(distCodeTable);
  640. code1 = distDecode[code1];
  641. code2 = code1 >> 16;
  642. if (code2 > 0) {
  643. code2 = this.getBits(code2);
  644. }
  645. var dist = (code1 & 0xffff) + code2;
  646. if (pos + len >= limit) {
  647. buffer = this.ensureBuffer(pos + len);
  648. limit = buffer.length;
  649. }
  650. for (var k = 0; k < len; ++k, ++pos) {
  651. buffer[pos] = buffer[pos - dist];
  652. }
  653. }
  654. };
  655. return FlateStream;
  656. })();
  657. var PredictorStream = (function PredictorStreamClosure() {
  658. // eslint-disable-next-line no-shadow
  659. function PredictorStream(str, maybeLength, params) {
  660. if (!isDict(params)) {
  661. return str; // no prediction
  662. }
  663. var predictor = (this.predictor = params.get("Predictor") || 1);
  664. if (predictor <= 1) {
  665. return str; // no prediction
  666. }
  667. if (predictor !== 2 && (predictor < 10 || predictor > 15)) {
  668. throw new FormatError(`Unsupported predictor: ${predictor}`);
  669. }
  670. if (predictor === 2) {
  671. this.readBlock = this.readBlockTiff;
  672. } else {
  673. this.readBlock = this.readBlockPng;
  674. }
  675. this.str = str;
  676. this.dict = str.dict;
  677. var colors = (this.colors = params.get("Colors") || 1);
  678. var bits = (this.bits = params.get("BitsPerComponent") || 8);
  679. var columns = (this.columns = params.get("Columns") || 1);
  680. this.pixBytes = (colors * bits + 7) >> 3;
  681. this.rowBytes = (columns * colors * bits + 7) >> 3;
  682. DecodeStream.call(this, maybeLength);
  683. return this;
  684. }
  685. PredictorStream.prototype = Object.create(DecodeStream.prototype);
  686. PredictorStream.prototype.readBlockTiff = function predictorStreamReadBlockTiff() {
  687. var rowBytes = this.rowBytes;
  688. var bufferLength = this.bufferLength;
  689. var buffer = this.ensureBuffer(bufferLength + rowBytes);
  690. var bits = this.bits;
  691. var colors = this.colors;
  692. var rawBytes = this.str.getBytes(rowBytes);
  693. this.eof = !rawBytes.length;
  694. if (this.eof) {
  695. return;
  696. }
  697. var inbuf = 0,
  698. outbuf = 0;
  699. var inbits = 0,
  700. outbits = 0;
  701. var pos = bufferLength;
  702. var i;
  703. if (bits === 1 && colors === 1) {
  704. // Optimized version of the loop in the "else"-branch
  705. // for 1 bit-per-component and 1 color TIFF images.
  706. for (i = 0; i < rowBytes; ++i) {
  707. var c = rawBytes[i] ^ inbuf;
  708. c ^= c >> 1;
  709. c ^= c >> 2;
  710. c ^= c >> 4;
  711. inbuf = (c & 1) << 7;
  712. buffer[pos++] = c;
  713. }
  714. } else if (bits === 8) {
  715. for (i = 0; i < colors; ++i) {
  716. buffer[pos++] = rawBytes[i];
  717. }
  718. for (; i < rowBytes; ++i) {
  719. buffer[pos] = buffer[pos - colors] + rawBytes[i];
  720. pos++;
  721. }
  722. } else if (bits === 16) {
  723. var bytesPerPixel = colors * 2;
  724. for (i = 0; i < bytesPerPixel; ++i) {
  725. buffer[pos++] = rawBytes[i];
  726. }
  727. for (; i < rowBytes; i += 2) {
  728. var sum =
  729. ((rawBytes[i] & 0xff) << 8) +
  730. (rawBytes[i + 1] & 0xff) +
  731. ((buffer[pos - bytesPerPixel] & 0xff) << 8) +
  732. (buffer[pos - bytesPerPixel + 1] & 0xff);
  733. buffer[pos++] = (sum >> 8) & 0xff;
  734. buffer[pos++] = sum & 0xff;
  735. }
  736. } else {
  737. var compArray = new Uint8Array(colors + 1);
  738. var bitMask = (1 << bits) - 1;
  739. var j = 0,
  740. k = bufferLength;
  741. var columns = this.columns;
  742. for (i = 0; i < columns; ++i) {
  743. for (var kk = 0; kk < colors; ++kk) {
  744. if (inbits < bits) {
  745. inbuf = (inbuf << 8) | (rawBytes[j++] & 0xff);
  746. inbits += 8;
  747. }
  748. compArray[kk] =
  749. (compArray[kk] + (inbuf >> (inbits - bits))) & bitMask;
  750. inbits -= bits;
  751. outbuf = (outbuf << bits) | compArray[kk];
  752. outbits += bits;
  753. if (outbits >= 8) {
  754. buffer[k++] = (outbuf >> (outbits - 8)) & 0xff;
  755. outbits -= 8;
  756. }
  757. }
  758. }
  759. if (outbits > 0) {
  760. buffer[k++] =
  761. (outbuf << (8 - outbits)) + (inbuf & ((1 << (8 - outbits)) - 1));
  762. }
  763. }
  764. this.bufferLength += rowBytes;
  765. };
  766. PredictorStream.prototype.readBlockPng = function predictorStreamReadBlockPng() {
  767. var rowBytes = this.rowBytes;
  768. var pixBytes = this.pixBytes;
  769. var predictor = this.str.getByte();
  770. var rawBytes = this.str.getBytes(rowBytes);
  771. this.eof = !rawBytes.length;
  772. if (this.eof) {
  773. return;
  774. }
  775. var bufferLength = this.bufferLength;
  776. var buffer = this.ensureBuffer(bufferLength + rowBytes);
  777. var prevRow = buffer.subarray(bufferLength - rowBytes, bufferLength);
  778. if (prevRow.length === 0) {
  779. prevRow = new Uint8Array(rowBytes);
  780. }
  781. var i,
  782. j = bufferLength,
  783. up,
  784. c;
  785. switch (predictor) {
  786. case 0:
  787. for (i = 0; i < rowBytes; ++i) {
  788. buffer[j++] = rawBytes[i];
  789. }
  790. break;
  791. case 1:
  792. for (i = 0; i < pixBytes; ++i) {
  793. buffer[j++] = rawBytes[i];
  794. }
  795. for (; i < rowBytes; ++i) {
  796. buffer[j] = (buffer[j - pixBytes] + rawBytes[i]) & 0xff;
  797. j++;
  798. }
  799. break;
  800. case 2:
  801. for (i = 0; i < rowBytes; ++i) {
  802. buffer[j++] = (prevRow[i] + rawBytes[i]) & 0xff;
  803. }
  804. break;
  805. case 3:
  806. for (i = 0; i < pixBytes; ++i) {
  807. buffer[j++] = (prevRow[i] >> 1) + rawBytes[i];
  808. }
  809. for (; i < rowBytes; ++i) {
  810. buffer[j] =
  811. (((prevRow[i] + buffer[j - pixBytes]) >> 1) + rawBytes[i]) & 0xff;
  812. j++;
  813. }
  814. break;
  815. case 4:
  816. // we need to save the up left pixels values. the simplest way
  817. // is to create a new buffer
  818. for (i = 0; i < pixBytes; ++i) {
  819. up = prevRow[i];
  820. c = rawBytes[i];
  821. buffer[j++] = up + c;
  822. }
  823. for (; i < rowBytes; ++i) {
  824. up = prevRow[i];
  825. var upLeft = prevRow[i - pixBytes];
  826. var left = buffer[j - pixBytes];
  827. var p = left + up - upLeft;
  828. var pa = p - left;
  829. if (pa < 0) {
  830. pa = -pa;
  831. }
  832. var pb = p - up;
  833. if (pb < 0) {
  834. pb = -pb;
  835. }
  836. var pc = p - upLeft;
  837. if (pc < 0) {
  838. pc = -pc;
  839. }
  840. c = rawBytes[i];
  841. if (pa <= pb && pa <= pc) {
  842. buffer[j++] = left + c;
  843. } else if (pb <= pc) {
  844. buffer[j++] = up + c;
  845. } else {
  846. buffer[j++] = upLeft + c;
  847. }
  848. }
  849. break;
  850. default:
  851. throw new FormatError(`Unsupported predictor: ${predictor}`);
  852. }
  853. this.bufferLength += rowBytes;
  854. };
  855. return PredictorStream;
  856. })();
  857. var DecryptStream = (function DecryptStreamClosure() {
  858. // eslint-disable-next-line no-shadow
  859. function DecryptStream(str, maybeLength, decrypt) {
  860. this.str = str;
  861. this.dict = str.dict;
  862. this.decrypt = decrypt;
  863. this.nextChunk = null;
  864. this.initialized = false;
  865. DecodeStream.call(this, maybeLength);
  866. }
  867. var chunkSize = 512;
  868. DecryptStream.prototype = Object.create(DecodeStream.prototype);
  869. DecryptStream.prototype.readBlock = function DecryptStream_readBlock() {
  870. var chunk;
  871. if (this.initialized) {
  872. chunk = this.nextChunk;
  873. } else {
  874. chunk = this.str.getBytes(chunkSize);
  875. this.initialized = true;
  876. }
  877. if (!chunk || chunk.length === 0) {
  878. this.eof = true;
  879. return;
  880. }
  881. this.nextChunk = this.str.getBytes(chunkSize);
  882. var hasMoreData = this.nextChunk && this.nextChunk.length > 0;
  883. var decrypt = this.decrypt;
  884. chunk = decrypt(chunk, !hasMoreData);
  885. var bufferLength = this.bufferLength;
  886. var i,
  887. n = chunk.length;
  888. var buffer = this.ensureBuffer(bufferLength + n);
  889. for (i = 0; i < n; i++) {
  890. buffer[bufferLength++] = chunk[i];
  891. }
  892. this.bufferLength = bufferLength;
  893. };
  894. return DecryptStream;
  895. })();
  896. var Ascii85Stream = (function Ascii85StreamClosure() {
  897. // eslint-disable-next-line no-shadow
  898. function Ascii85Stream(str, maybeLength) {
  899. this.str = str;
  900. this.dict = str.dict;
  901. this.input = new Uint8Array(5);
  902. // Most streams increase in size when decoded, but Ascii85 streams
  903. // typically shrink by ~20%.
  904. if (maybeLength) {
  905. maybeLength = 0.8 * maybeLength;
  906. }
  907. DecodeStream.call(this, maybeLength);
  908. }
  909. Ascii85Stream.prototype = Object.create(DecodeStream.prototype);
  910. Ascii85Stream.prototype.readBlock = function Ascii85Stream_readBlock() {
  911. var TILDA_CHAR = 0x7e; // '~'
  912. var Z_LOWER_CHAR = 0x7a; // 'z'
  913. var EOF = -1;
  914. var str = this.str;
  915. var c = str.getByte();
  916. while (isWhiteSpace(c)) {
  917. c = str.getByte();
  918. }
  919. if (c === EOF || c === TILDA_CHAR) {
  920. this.eof = true;
  921. return;
  922. }
  923. var bufferLength = this.bufferLength,
  924. buffer;
  925. var i;
  926. // special code for z
  927. if (c === Z_LOWER_CHAR) {
  928. buffer = this.ensureBuffer(bufferLength + 4);
  929. for (i = 0; i < 4; ++i) {
  930. buffer[bufferLength + i] = 0;
  931. }
  932. this.bufferLength += 4;
  933. } else {
  934. var input = this.input;
  935. input[0] = c;
  936. for (i = 1; i < 5; ++i) {
  937. c = str.getByte();
  938. while (isWhiteSpace(c)) {
  939. c = str.getByte();
  940. }
  941. input[i] = c;
  942. if (c === EOF || c === TILDA_CHAR) {
  943. break;
  944. }
  945. }
  946. buffer = this.ensureBuffer(bufferLength + i - 1);
  947. this.bufferLength += i - 1;
  948. // partial ending;
  949. if (i < 5) {
  950. for (; i < 5; ++i) {
  951. input[i] = 0x21 + 84;
  952. }
  953. this.eof = true;
  954. }
  955. var t = 0;
  956. for (i = 0; i < 5; ++i) {
  957. t = t * 85 + (input[i] - 0x21);
  958. }
  959. for (i = 3; i >= 0; --i) {
  960. buffer[bufferLength + i] = t & 0xff;
  961. t >>= 8;
  962. }
  963. }
  964. };
  965. return Ascii85Stream;
  966. })();
  967. var AsciiHexStream = (function AsciiHexStreamClosure() {
  968. // eslint-disable-next-line no-shadow
  969. function AsciiHexStream(str, maybeLength) {
  970. this.str = str;
  971. this.dict = str.dict;
  972. this.firstDigit = -1;
  973. // Most streams increase in size when decoded, but AsciiHex streams shrink
  974. // by 50%.
  975. if (maybeLength) {
  976. maybeLength = 0.5 * maybeLength;
  977. }
  978. DecodeStream.call(this, maybeLength);
  979. }
  980. AsciiHexStream.prototype = Object.create(DecodeStream.prototype);
  981. AsciiHexStream.prototype.readBlock = function AsciiHexStream_readBlock() {
  982. var UPSTREAM_BLOCK_SIZE = 8000;
  983. var bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE);
  984. if (!bytes.length) {
  985. this.eof = true;
  986. return;
  987. }
  988. var maxDecodeLength = (bytes.length + 1) >> 1;
  989. var buffer = this.ensureBuffer(this.bufferLength + maxDecodeLength);
  990. var bufferLength = this.bufferLength;
  991. var firstDigit = this.firstDigit;
  992. for (var i = 0, ii = bytes.length; i < ii; i++) {
  993. var ch = bytes[i],
  994. digit;
  995. if (ch >= /* '0' = */ 0x30 && ch <= /* '9' = */ 0x39) {
  996. digit = ch & 0x0f;
  997. } else if (
  998. (ch >= /* 'A' = */ 0x41 && ch <= /* 'Z' = */ 0x46) ||
  999. (ch >= /* 'a' = */ 0x61 && ch <= /* 'z' = */ 0x66)
  1000. ) {
  1001. digit = (ch & 0x0f) + 9;
  1002. } else if (ch === /* '>' = */ 0x3e) {
  1003. this.eof = true;
  1004. break;
  1005. } else {
  1006. // Probably whitespace, ignoring.
  1007. continue;
  1008. }
  1009. if (firstDigit < 0) {
  1010. firstDigit = digit;
  1011. } else {
  1012. buffer[bufferLength++] = (firstDigit << 4) | digit;
  1013. firstDigit = -1;
  1014. }
  1015. }
  1016. if (firstDigit >= 0 && this.eof) {
  1017. // incomplete byte
  1018. buffer[bufferLength++] = firstDigit << 4;
  1019. firstDigit = -1;
  1020. }
  1021. this.firstDigit = firstDigit;
  1022. this.bufferLength = bufferLength;
  1023. };
  1024. return AsciiHexStream;
  1025. })();
  1026. var RunLengthStream = (function RunLengthStreamClosure() {
  1027. // eslint-disable-next-line no-shadow
  1028. function RunLengthStream(str, maybeLength) {
  1029. this.str = str;
  1030. this.dict = str.dict;
  1031. DecodeStream.call(this, maybeLength);
  1032. }
  1033. RunLengthStream.prototype = Object.create(DecodeStream.prototype);
  1034. RunLengthStream.prototype.readBlock = function RunLengthStream_readBlock() {
  1035. // The repeatHeader has following format. The first byte defines type of run
  1036. // and amount of bytes to repeat/copy: n = 0 through 127 - copy next n bytes
  1037. // (in addition to the second byte from the header), n = 129 through 255 -
  1038. // duplicate the second byte from the header (257 - n) times, n = 128 - end.
  1039. var repeatHeader = this.str.getBytes(2);
  1040. if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) {
  1041. this.eof = true;
  1042. return;
  1043. }
  1044. var buffer;
  1045. var bufferLength = this.bufferLength;
  1046. var n = repeatHeader[0];
  1047. if (n < 128) {
  1048. // copy n bytes
  1049. buffer = this.ensureBuffer(bufferLength + n + 1);
  1050. buffer[bufferLength++] = repeatHeader[1];
  1051. if (n > 0) {
  1052. var source = this.str.getBytes(n);
  1053. buffer.set(source, bufferLength);
  1054. bufferLength += n;
  1055. }
  1056. } else {
  1057. n = 257 - n;
  1058. var b = repeatHeader[1];
  1059. buffer = this.ensureBuffer(bufferLength + n + 1);
  1060. for (var i = 0; i < n; i++) {
  1061. buffer[bufferLength++] = b;
  1062. }
  1063. }
  1064. this.bufferLength = bufferLength;
  1065. };
  1066. return RunLengthStream;
  1067. })();
  1068. var LZWStream = (function LZWStreamClosure() {
  1069. // eslint-disable-next-line no-shadow
  1070. function LZWStream(str, maybeLength, earlyChange) {
  1071. this.str = str;
  1072. this.dict = str.dict;
  1073. this.cachedData = 0;
  1074. this.bitsCached = 0;
  1075. var maxLzwDictionarySize = 4096;
  1076. var lzwState = {
  1077. earlyChange,
  1078. codeLength: 9,
  1079. nextCode: 258,
  1080. dictionaryValues: new Uint8Array(maxLzwDictionarySize),
  1081. dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
  1082. dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
  1083. currentSequence: new Uint8Array(maxLzwDictionarySize),
  1084. currentSequenceLength: 0,
  1085. };
  1086. for (var i = 0; i < 256; ++i) {
  1087. lzwState.dictionaryValues[i] = i;
  1088. lzwState.dictionaryLengths[i] = 1;
  1089. }
  1090. this.lzwState = lzwState;
  1091. DecodeStream.call(this, maybeLength);
  1092. }
  1093. LZWStream.prototype = Object.create(DecodeStream.prototype);
  1094. LZWStream.prototype.readBits = function LZWStream_readBits(n) {
  1095. var bitsCached = this.bitsCached;
  1096. var cachedData = this.cachedData;
  1097. while (bitsCached < n) {
  1098. var c = this.str.getByte();
  1099. if (c === -1) {
  1100. this.eof = true;
  1101. return null;
  1102. }
  1103. cachedData = (cachedData << 8) | c;
  1104. bitsCached += 8;
  1105. }
  1106. this.bitsCached = bitsCached -= n;
  1107. this.cachedData = cachedData;
  1108. this.lastCode = null;
  1109. return (cachedData >>> bitsCached) & ((1 << n) - 1);
  1110. };
  1111. LZWStream.prototype.readBlock = function LZWStream_readBlock() {
  1112. var blockSize = 512;
  1113. var estimatedDecodedSize = blockSize * 2,
  1114. decodedSizeDelta = blockSize;
  1115. var i, j, q;
  1116. var lzwState = this.lzwState;
  1117. if (!lzwState) {
  1118. return; // eof was found
  1119. }
  1120. var earlyChange = lzwState.earlyChange;
  1121. var nextCode = lzwState.nextCode;
  1122. var dictionaryValues = lzwState.dictionaryValues;
  1123. var dictionaryLengths = lzwState.dictionaryLengths;
  1124. var dictionaryPrevCodes = lzwState.dictionaryPrevCodes;
  1125. var codeLength = lzwState.codeLength;
  1126. var prevCode = lzwState.prevCode;
  1127. var currentSequence = lzwState.currentSequence;
  1128. var currentSequenceLength = lzwState.currentSequenceLength;
  1129. var decodedLength = 0;
  1130. var currentBufferLength = this.bufferLength;
  1131. var buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1132. for (i = 0; i < blockSize; i++) {
  1133. var code = this.readBits(codeLength);
  1134. var hasPrev = currentSequenceLength > 0;
  1135. if (code < 256) {
  1136. currentSequence[0] = code;
  1137. currentSequenceLength = 1;
  1138. } else if (code >= 258) {
  1139. if (code < nextCode) {
  1140. currentSequenceLength = dictionaryLengths[code];
  1141. for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
  1142. currentSequence[j] = dictionaryValues[q];
  1143. q = dictionaryPrevCodes[q];
  1144. }
  1145. } else {
  1146. currentSequence[currentSequenceLength++] = currentSequence[0];
  1147. }
  1148. } else if (code === 256) {
  1149. codeLength = 9;
  1150. nextCode = 258;
  1151. currentSequenceLength = 0;
  1152. continue;
  1153. } else {
  1154. this.eof = true;
  1155. delete this.lzwState;
  1156. break;
  1157. }
  1158. if (hasPrev) {
  1159. dictionaryPrevCodes[nextCode] = prevCode;
  1160. dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
  1161. dictionaryValues[nextCode] = currentSequence[0];
  1162. nextCode++;
  1163. codeLength =
  1164. (nextCode + earlyChange) & (nextCode + earlyChange - 1)
  1165. ? codeLength
  1166. : Math.min(
  1167. Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1,
  1168. 12
  1169. ) | 0;
  1170. }
  1171. prevCode = code;
  1172. decodedLength += currentSequenceLength;
  1173. if (estimatedDecodedSize < decodedLength) {
  1174. do {
  1175. estimatedDecodedSize += decodedSizeDelta;
  1176. } while (estimatedDecodedSize < decodedLength);
  1177. buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1178. }
  1179. for (j = 0; j < currentSequenceLength; j++) {
  1180. buffer[currentBufferLength++] = currentSequence[j];
  1181. }
  1182. }
  1183. lzwState.nextCode = nextCode;
  1184. lzwState.codeLength = codeLength;
  1185. lzwState.prevCode = prevCode;
  1186. lzwState.currentSequenceLength = currentSequenceLength;
  1187. this.bufferLength = currentBufferLength;
  1188. };
  1189. return LZWStream;
  1190. })();
  1191. var NullStream = (function NullStreamClosure() {
  1192. // eslint-disable-next-line no-shadow
  1193. function NullStream() {
  1194. Stream.call(this, new Uint8Array(0));
  1195. }
  1196. NullStream.prototype = Stream.prototype;
  1197. return NullStream;
  1198. })();
  1199. export {
  1200. Ascii85Stream,
  1201. AsciiHexStream,
  1202. DecryptStream,
  1203. DecodeStream,
  1204. FlateStream,
  1205. NullStream,
  1206. PredictorStream,
  1207. RunLengthStream,
  1208. Stream,
  1209. StreamsSequenceStream,
  1210. StringStream,
  1211. LZWStream,
  1212. };