2
0

stream.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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.LZWStream = exports.StringStream = exports.StreamsSequenceStream = exports.Stream = exports.RunLengthStream = exports.PredictorStream = exports.NullStream = exports.FlateStream = exports.DecodeStream = exports.DecryptStream = exports.AsciiHexStream = exports.Ascii85Stream = void 0;
  27. var _util = require("../shared/util");
  28. var _primitives = require("./primitives");
  29. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
  30. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
  31. function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
  32. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
  33. var Stream = function StreamClosure() {
  34. function Stream(arrayBuffer, start, length, dict) {
  35. this.bytes = arrayBuffer instanceof Uint8Array ? arrayBuffer : new Uint8Array(arrayBuffer);
  36. this.start = start || 0;
  37. this.pos = this.start;
  38. this.end = start + length || this.bytes.length;
  39. this.dict = dict;
  40. }
  41. Stream.prototype = {
  42. get length() {
  43. return this.end - this.start;
  44. },
  45. get isEmpty() {
  46. return this.length === 0;
  47. },
  48. getByte: function Stream_getByte() {
  49. if (this.pos >= this.end) {
  50. return -1;
  51. }
  52. return this.bytes[this.pos++];
  53. },
  54. getUint16: function Stream_getUint16() {
  55. var b0 = this.getByte();
  56. var b1 = this.getByte();
  57. if (b0 === -1 || b1 === -1) {
  58. return -1;
  59. }
  60. return (b0 << 8) + b1;
  61. },
  62. getInt32: function Stream_getInt32() {
  63. var b0 = this.getByte();
  64. var b1 = this.getByte();
  65. var b2 = this.getByte();
  66. var b3 = this.getByte();
  67. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  68. },
  69. getBytes: function getBytes(length) {
  70. var forceClamped = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  71. var bytes = this.bytes;
  72. var pos = this.pos;
  73. var strEnd = this.end;
  74. if (!length) {
  75. var _subarray = bytes.subarray(pos, strEnd);
  76. return forceClamped ? new Uint8ClampedArray(_subarray) : _subarray;
  77. }
  78. var end = pos + length;
  79. if (end > strEnd) {
  80. end = strEnd;
  81. }
  82. this.pos = end;
  83. var subarray = bytes.subarray(pos, end);
  84. return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
  85. },
  86. peekByte: function Stream_peekByte() {
  87. var peekedByte = this.getByte();
  88. this.pos--;
  89. return peekedByte;
  90. },
  91. peekBytes: function peekBytes(length) {
  92. var forceClamped = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  93. var bytes = this.getBytes(length, forceClamped);
  94. this.pos -= bytes.length;
  95. return bytes;
  96. },
  97. skip: function Stream_skip(n) {
  98. if (!n) {
  99. n = 1;
  100. }
  101. this.pos += n;
  102. },
  103. reset: function Stream_reset() {
  104. this.pos = this.start;
  105. },
  106. moveStart: function Stream_moveStart() {
  107. this.start = this.pos;
  108. },
  109. makeSubStream: function Stream_makeSubStream(start, length, dict) {
  110. return new Stream(this.bytes.buffer, start, length, dict);
  111. }
  112. };
  113. return Stream;
  114. }();
  115. exports.Stream = Stream;
  116. var StringStream = function StringStreamClosure() {
  117. function StringStream(str) {
  118. var bytes = (0, _util.stringToBytes)(str);
  119. Stream.call(this, bytes);
  120. }
  121. StringStream.prototype = Stream.prototype;
  122. return StringStream;
  123. }();
  124. exports.StringStream = StringStream;
  125. var DecodeStream = function DecodeStreamClosure() {
  126. var emptyBuffer = new Uint8Array(0);
  127. function DecodeStream(maybeMinBufferLength) {
  128. this._rawMinBufferLength = maybeMinBufferLength || 0;
  129. this.pos = 0;
  130. this.bufferLength = 0;
  131. this.eof = false;
  132. this.buffer = emptyBuffer;
  133. this.minBufferLength = 512;
  134. if (maybeMinBufferLength) {
  135. while (this.minBufferLength < maybeMinBufferLength) {
  136. this.minBufferLength *= 2;
  137. }
  138. }
  139. }
  140. DecodeStream.prototype = {
  141. get isEmpty() {
  142. while (!this.eof && this.bufferLength === 0) {
  143. this.readBlock();
  144. }
  145. return this.bufferLength === 0;
  146. },
  147. ensureBuffer: function DecodeStream_ensureBuffer(requested) {
  148. var buffer = this.buffer;
  149. if (requested <= buffer.byteLength) {
  150. return buffer;
  151. }
  152. var size = this.minBufferLength;
  153. while (size < requested) {
  154. size *= 2;
  155. }
  156. var buffer2 = new Uint8Array(size);
  157. buffer2.set(buffer);
  158. return this.buffer = buffer2;
  159. },
  160. getByte: function DecodeStream_getByte() {
  161. var pos = this.pos;
  162. while (this.bufferLength <= pos) {
  163. if (this.eof) {
  164. return -1;
  165. }
  166. this.readBlock();
  167. }
  168. return this.buffer[this.pos++];
  169. },
  170. getUint16: function DecodeStream_getUint16() {
  171. var b0 = this.getByte();
  172. var b1 = this.getByte();
  173. if (b0 === -1 || b1 === -1) {
  174. return -1;
  175. }
  176. return (b0 << 8) + b1;
  177. },
  178. getInt32: function DecodeStream_getInt32() {
  179. var b0 = this.getByte();
  180. var b1 = this.getByte();
  181. var b2 = this.getByte();
  182. var b3 = this.getByte();
  183. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  184. },
  185. getBytes: function getBytes(length) {
  186. var forceClamped = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  187. var end,
  188. pos = this.pos;
  189. if (length) {
  190. this.ensureBuffer(pos + length);
  191. end = pos + length;
  192. while (!this.eof && this.bufferLength < end) {
  193. this.readBlock();
  194. }
  195. var bufEnd = this.bufferLength;
  196. if (end > bufEnd) {
  197. end = bufEnd;
  198. }
  199. } else {
  200. while (!this.eof) {
  201. this.readBlock();
  202. }
  203. end = this.bufferLength;
  204. }
  205. this.pos = end;
  206. var subarray = this.buffer.subarray(pos, end);
  207. return forceClamped && !(subarray instanceof Uint8ClampedArray) ? new Uint8ClampedArray(subarray) : subarray;
  208. },
  209. peekByte: function DecodeStream_peekByte() {
  210. var peekedByte = this.getByte();
  211. this.pos--;
  212. return peekedByte;
  213. },
  214. peekBytes: function peekBytes(length) {
  215. var forceClamped = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  216. var bytes = this.getBytes(length, forceClamped);
  217. this.pos -= bytes.length;
  218. return bytes;
  219. },
  220. makeSubStream: function DecodeStream_makeSubStream(start, length, dict) {
  221. var end = start + length;
  222. while (this.bufferLength <= end && !this.eof) {
  223. this.readBlock();
  224. }
  225. return new Stream(this.buffer, start, length, dict);
  226. },
  227. skip: function DecodeStream_skip(n) {
  228. if (!n) {
  229. n = 1;
  230. }
  231. this.pos += n;
  232. },
  233. reset: function DecodeStream_reset() {
  234. this.pos = 0;
  235. },
  236. getBaseStreams: function DecodeStream_getBaseStreams() {
  237. if (this.str && this.str.getBaseStreams) {
  238. return this.str.getBaseStreams();
  239. }
  240. return [];
  241. }
  242. };
  243. return DecodeStream;
  244. }();
  245. exports.DecodeStream = DecodeStream;
  246. var StreamsSequenceStream = function StreamsSequenceStreamClosure() {
  247. function StreamsSequenceStream(streams) {
  248. this.streams = streams;
  249. var maybeLength = 0;
  250. for (var i = 0, ii = streams.length; i < ii; i++) {
  251. var stream = streams[i];
  252. if (stream instanceof DecodeStream) {
  253. maybeLength += stream._rawMinBufferLength;
  254. } else {
  255. maybeLength += stream.length;
  256. }
  257. }
  258. DecodeStream.call(this, maybeLength);
  259. }
  260. StreamsSequenceStream.prototype = Object.create(DecodeStream.prototype);
  261. StreamsSequenceStream.prototype.readBlock = function streamSequenceStreamReadBlock() {
  262. var streams = this.streams;
  263. if (streams.length === 0) {
  264. this.eof = true;
  265. return;
  266. }
  267. var stream = streams.shift();
  268. var chunk = stream.getBytes();
  269. var bufferLength = this.bufferLength;
  270. var newLength = bufferLength + chunk.length;
  271. var buffer = this.ensureBuffer(newLength);
  272. buffer.set(chunk, bufferLength);
  273. this.bufferLength = newLength;
  274. };
  275. StreamsSequenceStream.prototype.getBaseStreams = function StreamsSequenceStream_getBaseStreams() {
  276. var baseStreams = [];
  277. for (var i = 0, ii = this.streams.length; i < ii; i++) {
  278. var stream = this.streams[i];
  279. if (stream.getBaseStreams) {
  280. baseStreams.push.apply(baseStreams, _toConsumableArray(stream.getBaseStreams()));
  281. }
  282. }
  283. return baseStreams;
  284. };
  285. return StreamsSequenceStream;
  286. }();
  287. exports.StreamsSequenceStream = StreamsSequenceStream;
  288. var FlateStream = function FlateStreamClosure() {
  289. var codeLenCodeMap = new Int32Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
  290. var 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]);
  291. var 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]);
  292. var 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];
  293. var 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];
  294. function FlateStream(str, maybeLength) {
  295. this.str = str;
  296. this.dict = str.dict;
  297. var cmf = str.getByte();
  298. var flg = str.getByte();
  299. if (cmf === -1 || flg === -1) {
  300. throw new _util.FormatError("Invalid header in flate stream: ".concat(cmf, ", ").concat(flg));
  301. }
  302. if ((cmf & 0x0f) !== 0x08) {
  303. throw new _util.FormatError("Unknown compression method in flate stream: ".concat(cmf, ", ").concat(flg));
  304. }
  305. if (((cmf << 8) + flg) % 31 !== 0) {
  306. throw new _util.FormatError("Bad FCHECK in flate stream: ".concat(cmf, ", ").concat(flg));
  307. }
  308. if (flg & 0x20) {
  309. throw new _util.FormatError("FDICT bit set in flate stream: ".concat(cmf, ", ").concat(flg));
  310. }
  311. this.codeSize = 0;
  312. this.codeBuf = 0;
  313. DecodeStream.call(this, maybeLength);
  314. }
  315. FlateStream.prototype = Object.create(DecodeStream.prototype);
  316. FlateStream.prototype.getBits = function FlateStream_getBits(bits) {
  317. var str = this.str;
  318. var codeSize = this.codeSize;
  319. var codeBuf = this.codeBuf;
  320. var b;
  321. while (codeSize < bits) {
  322. if ((b = str.getByte()) === -1) {
  323. throw new _util.FormatError('Bad encoding in flate stream');
  324. }
  325. codeBuf |= b << codeSize;
  326. codeSize += 8;
  327. }
  328. b = codeBuf & (1 << bits) - 1;
  329. this.codeBuf = codeBuf >> bits;
  330. this.codeSize = codeSize -= bits;
  331. return b;
  332. };
  333. FlateStream.prototype.getCode = function FlateStream_getCode(table) {
  334. var str = this.str;
  335. var codes = table[0];
  336. var maxLen = table[1];
  337. var codeSize = this.codeSize;
  338. var codeBuf = this.codeBuf;
  339. var b;
  340. while (codeSize < maxLen) {
  341. if ((b = str.getByte()) === -1) {
  342. break;
  343. }
  344. codeBuf |= b << codeSize;
  345. codeSize += 8;
  346. }
  347. var code = codes[codeBuf & (1 << maxLen) - 1];
  348. var codeLen = code >> 16;
  349. var codeVal = code & 0xffff;
  350. if (codeLen < 1 || codeSize < codeLen) {
  351. throw new _util.FormatError('Bad encoding in flate stream');
  352. }
  353. this.codeBuf = codeBuf >> codeLen;
  354. this.codeSize = codeSize - codeLen;
  355. return codeVal;
  356. };
  357. FlateStream.prototype.generateHuffmanTable = function flateStreamGenerateHuffmanTable(lengths) {
  358. var n = lengths.length;
  359. var maxLen = 0;
  360. var i;
  361. for (i = 0; i < n; ++i) {
  362. if (lengths[i] > maxLen) {
  363. maxLen = lengths[i];
  364. }
  365. }
  366. var size = 1 << maxLen;
  367. var codes = new Int32Array(size);
  368. for (var len = 1, code = 0, skip = 2; len <= maxLen; ++len, code <<= 1, skip <<= 1) {
  369. for (var val = 0; val < n; ++val) {
  370. if (lengths[val] === len) {
  371. var code2 = 0;
  372. var t = code;
  373. for (i = 0; i < len; ++i) {
  374. code2 = code2 << 1 | t & 1;
  375. t >>= 1;
  376. }
  377. for (i = code2; i < size; i += skip) {
  378. codes[i] = len << 16 | val;
  379. }
  380. ++code;
  381. }
  382. }
  383. }
  384. return [codes, maxLen];
  385. };
  386. FlateStream.prototype.readBlock = function FlateStream_readBlock() {
  387. var buffer, len;
  388. var str = this.str;
  389. var hdr = this.getBits(3);
  390. if (hdr & 1) {
  391. this.eof = true;
  392. }
  393. hdr >>= 1;
  394. if (hdr === 0) {
  395. var b;
  396. if ((b = str.getByte()) === -1) {
  397. throw new _util.FormatError('Bad block header in flate stream');
  398. }
  399. var blockLen = b;
  400. if ((b = str.getByte()) === -1) {
  401. throw new _util.FormatError('Bad block header in flate stream');
  402. }
  403. blockLen |= b << 8;
  404. if ((b = str.getByte()) === -1) {
  405. throw new _util.FormatError('Bad block header in flate stream');
  406. }
  407. var check = b;
  408. if ((b = str.getByte()) === -1) {
  409. throw new _util.FormatError('Bad block header in flate stream');
  410. }
  411. check |= b << 8;
  412. if (check !== (~blockLen & 0xffff) && (blockLen !== 0 || check !== 0)) {
  413. throw new _util.FormatError('Bad uncompressed block length in flate stream');
  414. }
  415. this.codeBuf = 0;
  416. this.codeSize = 0;
  417. var bufferLength = this.bufferLength;
  418. buffer = this.ensureBuffer(bufferLength + blockLen);
  419. var end = bufferLength + blockLen;
  420. this.bufferLength = end;
  421. if (blockLen === 0) {
  422. if (str.peekByte() === -1) {
  423. this.eof = true;
  424. }
  425. } else {
  426. for (var n = bufferLength; n < end; ++n) {
  427. if ((b = str.getByte()) === -1) {
  428. this.eof = true;
  429. break;
  430. }
  431. buffer[n] = b;
  432. }
  433. }
  434. return;
  435. }
  436. var litCodeTable;
  437. var distCodeTable;
  438. if (hdr === 1) {
  439. litCodeTable = fixedLitCodeTab;
  440. distCodeTable = fixedDistCodeTab;
  441. } else if (hdr === 2) {
  442. var numLitCodes = this.getBits(5) + 257;
  443. var numDistCodes = this.getBits(5) + 1;
  444. var numCodeLenCodes = this.getBits(4) + 4;
  445. var codeLenCodeLengths = new Uint8Array(codeLenCodeMap.length);
  446. var i;
  447. for (i = 0; i < numCodeLenCodes; ++i) {
  448. codeLenCodeLengths[codeLenCodeMap[i]] = this.getBits(3);
  449. }
  450. var codeLenCodeTab = this.generateHuffmanTable(codeLenCodeLengths);
  451. len = 0;
  452. i = 0;
  453. var codes = numLitCodes + numDistCodes;
  454. var codeLengths = new Uint8Array(codes);
  455. var bitsLength, bitsOffset, what;
  456. while (i < codes) {
  457. var code = this.getCode(codeLenCodeTab);
  458. if (code === 16) {
  459. bitsLength = 2;
  460. bitsOffset = 3;
  461. what = len;
  462. } else if (code === 17) {
  463. bitsLength = 3;
  464. bitsOffset = 3;
  465. what = len = 0;
  466. } else if (code === 18) {
  467. bitsLength = 7;
  468. bitsOffset = 11;
  469. what = len = 0;
  470. } else {
  471. codeLengths[i++] = len = code;
  472. continue;
  473. }
  474. var repeatLength = this.getBits(bitsLength) + bitsOffset;
  475. while (repeatLength-- > 0) {
  476. codeLengths[i++] = what;
  477. }
  478. }
  479. litCodeTable = this.generateHuffmanTable(codeLengths.subarray(0, numLitCodes));
  480. distCodeTable = this.generateHuffmanTable(codeLengths.subarray(numLitCodes, codes));
  481. } else {
  482. throw new _util.FormatError('Unknown block type in flate stream');
  483. }
  484. buffer = this.buffer;
  485. var limit = buffer ? buffer.length : 0;
  486. var pos = this.bufferLength;
  487. while (true) {
  488. var code1 = this.getCode(litCodeTable);
  489. if (code1 < 256) {
  490. if (pos + 1 >= limit) {
  491. buffer = this.ensureBuffer(pos + 1);
  492. limit = buffer.length;
  493. }
  494. buffer[pos++] = code1;
  495. continue;
  496. }
  497. if (code1 === 256) {
  498. this.bufferLength = pos;
  499. return;
  500. }
  501. code1 -= 257;
  502. code1 = lengthDecode[code1];
  503. var code2 = code1 >> 16;
  504. if (code2 > 0) {
  505. code2 = this.getBits(code2);
  506. }
  507. len = (code1 & 0xffff) + code2;
  508. code1 = this.getCode(distCodeTable);
  509. code1 = distDecode[code1];
  510. code2 = code1 >> 16;
  511. if (code2 > 0) {
  512. code2 = this.getBits(code2);
  513. }
  514. var dist = (code1 & 0xffff) + code2;
  515. if (pos + len >= limit) {
  516. buffer = this.ensureBuffer(pos + len);
  517. limit = buffer.length;
  518. }
  519. for (var k = 0; k < len; ++k, ++pos) {
  520. buffer[pos] = buffer[pos - dist];
  521. }
  522. }
  523. };
  524. return FlateStream;
  525. }();
  526. exports.FlateStream = FlateStream;
  527. var PredictorStream = function PredictorStreamClosure() {
  528. function PredictorStream(str, maybeLength, params) {
  529. if (!(0, _primitives.isDict)(params)) {
  530. return str;
  531. }
  532. var predictor = this.predictor = params.get('Predictor') || 1;
  533. if (predictor <= 1) {
  534. return str;
  535. }
  536. if (predictor !== 2 && (predictor < 10 || predictor > 15)) {
  537. throw new _util.FormatError("Unsupported predictor: ".concat(predictor));
  538. }
  539. if (predictor === 2) {
  540. this.readBlock = this.readBlockTiff;
  541. } else {
  542. this.readBlock = this.readBlockPng;
  543. }
  544. this.str = str;
  545. this.dict = str.dict;
  546. var colors = this.colors = params.get('Colors') || 1;
  547. var bits = this.bits = params.get('BitsPerComponent') || 8;
  548. var columns = this.columns = params.get('Columns') || 1;
  549. this.pixBytes = colors * bits + 7 >> 3;
  550. this.rowBytes = columns * colors * bits + 7 >> 3;
  551. DecodeStream.call(this, maybeLength);
  552. return this;
  553. }
  554. PredictorStream.prototype = Object.create(DecodeStream.prototype);
  555. PredictorStream.prototype.readBlockTiff = function predictorStreamReadBlockTiff() {
  556. var rowBytes = this.rowBytes;
  557. var bufferLength = this.bufferLength;
  558. var buffer = this.ensureBuffer(bufferLength + rowBytes);
  559. var bits = this.bits;
  560. var colors = this.colors;
  561. var rawBytes = this.str.getBytes(rowBytes);
  562. this.eof = !rawBytes.length;
  563. if (this.eof) {
  564. return;
  565. }
  566. var inbuf = 0,
  567. outbuf = 0;
  568. var inbits = 0,
  569. outbits = 0;
  570. var pos = bufferLength;
  571. var i;
  572. if (bits === 1 && colors === 1) {
  573. for (i = 0; i < rowBytes; ++i) {
  574. var c = rawBytes[i] ^ inbuf;
  575. c ^= c >> 1;
  576. c ^= c >> 2;
  577. c ^= c >> 4;
  578. inbuf = (c & 1) << 7;
  579. buffer[pos++] = c;
  580. }
  581. } else if (bits === 8) {
  582. for (i = 0; i < colors; ++i) {
  583. buffer[pos++] = rawBytes[i];
  584. }
  585. for (; i < rowBytes; ++i) {
  586. buffer[pos] = buffer[pos - colors] + rawBytes[i];
  587. pos++;
  588. }
  589. } else if (bits === 16) {
  590. var bytesPerPixel = colors * 2;
  591. for (i = 0; i < bytesPerPixel; ++i) {
  592. buffer[pos++] = rawBytes[i];
  593. }
  594. for (; i < rowBytes; i += 2) {
  595. var sum = ((rawBytes[i] & 0xFF) << 8) + (rawBytes[i + 1] & 0xFF) + ((buffer[pos - bytesPerPixel] & 0xFF) << 8) + (buffer[pos - bytesPerPixel + 1] & 0xFF);
  596. buffer[pos++] = sum >> 8 & 0xFF;
  597. buffer[pos++] = sum & 0xFF;
  598. }
  599. } else {
  600. var compArray = new Uint8Array(colors + 1);
  601. var bitMask = (1 << bits) - 1;
  602. var j = 0,
  603. k = bufferLength;
  604. var columns = this.columns;
  605. for (i = 0; i < columns; ++i) {
  606. for (var kk = 0; kk < colors; ++kk) {
  607. if (inbits < bits) {
  608. inbuf = inbuf << 8 | rawBytes[j++] & 0xFF;
  609. inbits += 8;
  610. }
  611. compArray[kk] = compArray[kk] + (inbuf >> inbits - bits) & bitMask;
  612. inbits -= bits;
  613. outbuf = outbuf << bits | compArray[kk];
  614. outbits += bits;
  615. if (outbits >= 8) {
  616. buffer[k++] = outbuf >> outbits - 8 & 0xFF;
  617. outbits -= 8;
  618. }
  619. }
  620. }
  621. if (outbits > 0) {
  622. buffer[k++] = (outbuf << 8 - outbits) + (inbuf & (1 << 8 - outbits) - 1);
  623. }
  624. }
  625. this.bufferLength += rowBytes;
  626. };
  627. PredictorStream.prototype.readBlockPng = function predictorStreamReadBlockPng() {
  628. var rowBytes = this.rowBytes;
  629. var pixBytes = this.pixBytes;
  630. var predictor = this.str.getByte();
  631. var rawBytes = this.str.getBytes(rowBytes);
  632. this.eof = !rawBytes.length;
  633. if (this.eof) {
  634. return;
  635. }
  636. var bufferLength = this.bufferLength;
  637. var buffer = this.ensureBuffer(bufferLength + rowBytes);
  638. var prevRow = buffer.subarray(bufferLength - rowBytes, bufferLength);
  639. if (prevRow.length === 0) {
  640. prevRow = new Uint8Array(rowBytes);
  641. }
  642. var i,
  643. j = bufferLength,
  644. up,
  645. c;
  646. switch (predictor) {
  647. case 0:
  648. for (i = 0; i < rowBytes; ++i) {
  649. buffer[j++] = rawBytes[i];
  650. }
  651. break;
  652. case 1:
  653. for (i = 0; i < pixBytes; ++i) {
  654. buffer[j++] = rawBytes[i];
  655. }
  656. for (; i < rowBytes; ++i) {
  657. buffer[j] = buffer[j - pixBytes] + rawBytes[i] & 0xFF;
  658. j++;
  659. }
  660. break;
  661. case 2:
  662. for (i = 0; i < rowBytes; ++i) {
  663. buffer[j++] = prevRow[i] + rawBytes[i] & 0xFF;
  664. }
  665. break;
  666. case 3:
  667. for (i = 0; i < pixBytes; ++i) {
  668. buffer[j++] = (prevRow[i] >> 1) + rawBytes[i];
  669. }
  670. for (; i < rowBytes; ++i) {
  671. buffer[j] = (prevRow[i] + buffer[j - pixBytes] >> 1) + rawBytes[i] & 0xFF;
  672. j++;
  673. }
  674. break;
  675. case 4:
  676. for (i = 0; i < pixBytes; ++i) {
  677. up = prevRow[i];
  678. c = rawBytes[i];
  679. buffer[j++] = up + c;
  680. }
  681. for (; i < rowBytes; ++i) {
  682. up = prevRow[i];
  683. var upLeft = prevRow[i - pixBytes];
  684. var left = buffer[j - pixBytes];
  685. var p = left + up - upLeft;
  686. var pa = p - left;
  687. if (pa < 0) {
  688. pa = -pa;
  689. }
  690. var pb = p - up;
  691. if (pb < 0) {
  692. pb = -pb;
  693. }
  694. var pc = p - upLeft;
  695. if (pc < 0) {
  696. pc = -pc;
  697. }
  698. c = rawBytes[i];
  699. if (pa <= pb && pa <= pc) {
  700. buffer[j++] = left + c;
  701. } else if (pb <= pc) {
  702. buffer[j++] = up + c;
  703. } else {
  704. buffer[j++] = upLeft + c;
  705. }
  706. }
  707. break;
  708. default:
  709. throw new _util.FormatError("Unsupported predictor: ".concat(predictor));
  710. }
  711. this.bufferLength += rowBytes;
  712. };
  713. return PredictorStream;
  714. }();
  715. exports.PredictorStream = PredictorStream;
  716. var DecryptStream = function DecryptStreamClosure() {
  717. function DecryptStream(str, maybeLength, decrypt) {
  718. this.str = str;
  719. this.dict = str.dict;
  720. this.decrypt = decrypt;
  721. this.nextChunk = null;
  722. this.initialized = false;
  723. DecodeStream.call(this, maybeLength);
  724. }
  725. var chunkSize = 512;
  726. DecryptStream.prototype = Object.create(DecodeStream.prototype);
  727. DecryptStream.prototype.readBlock = function DecryptStream_readBlock() {
  728. var chunk;
  729. if (this.initialized) {
  730. chunk = this.nextChunk;
  731. } else {
  732. chunk = this.str.getBytes(chunkSize);
  733. this.initialized = true;
  734. }
  735. if (!chunk || chunk.length === 0) {
  736. this.eof = true;
  737. return;
  738. }
  739. this.nextChunk = this.str.getBytes(chunkSize);
  740. var hasMoreData = this.nextChunk && this.nextChunk.length > 0;
  741. var decrypt = this.decrypt;
  742. chunk = decrypt(chunk, !hasMoreData);
  743. var bufferLength = this.bufferLength;
  744. var i,
  745. n = chunk.length;
  746. var buffer = this.ensureBuffer(bufferLength + n);
  747. for (i = 0; i < n; i++) {
  748. buffer[bufferLength++] = chunk[i];
  749. }
  750. this.bufferLength = bufferLength;
  751. };
  752. return DecryptStream;
  753. }();
  754. exports.DecryptStream = DecryptStream;
  755. var Ascii85Stream = function Ascii85StreamClosure() {
  756. function Ascii85Stream(str, maybeLength) {
  757. this.str = str;
  758. this.dict = str.dict;
  759. this.input = new Uint8Array(5);
  760. if (maybeLength) {
  761. maybeLength = 0.8 * maybeLength;
  762. }
  763. DecodeStream.call(this, maybeLength);
  764. }
  765. Ascii85Stream.prototype = Object.create(DecodeStream.prototype);
  766. Ascii85Stream.prototype.readBlock = function Ascii85Stream_readBlock() {
  767. var TILDA_CHAR = 0x7E;
  768. var Z_LOWER_CHAR = 0x7A;
  769. var EOF = -1;
  770. var str = this.str;
  771. var c = str.getByte();
  772. while ((0, _util.isSpace)(c)) {
  773. c = str.getByte();
  774. }
  775. if (c === EOF || c === TILDA_CHAR) {
  776. this.eof = true;
  777. return;
  778. }
  779. var bufferLength = this.bufferLength,
  780. buffer;
  781. var i;
  782. if (c === Z_LOWER_CHAR) {
  783. buffer = this.ensureBuffer(bufferLength + 4);
  784. for (i = 0; i < 4; ++i) {
  785. buffer[bufferLength + i] = 0;
  786. }
  787. this.bufferLength += 4;
  788. } else {
  789. var input = this.input;
  790. input[0] = c;
  791. for (i = 1; i < 5; ++i) {
  792. c = str.getByte();
  793. while ((0, _util.isSpace)(c)) {
  794. c = str.getByte();
  795. }
  796. input[i] = c;
  797. if (c === EOF || c === TILDA_CHAR) {
  798. break;
  799. }
  800. }
  801. buffer = this.ensureBuffer(bufferLength + i - 1);
  802. this.bufferLength += i - 1;
  803. if (i < 5) {
  804. for (; i < 5; ++i) {
  805. input[i] = 0x21 + 84;
  806. }
  807. this.eof = true;
  808. }
  809. var t = 0;
  810. for (i = 0; i < 5; ++i) {
  811. t = t * 85 + (input[i] - 0x21);
  812. }
  813. for (i = 3; i >= 0; --i) {
  814. buffer[bufferLength + i] = t & 0xFF;
  815. t >>= 8;
  816. }
  817. }
  818. };
  819. return Ascii85Stream;
  820. }();
  821. exports.Ascii85Stream = Ascii85Stream;
  822. var AsciiHexStream = function AsciiHexStreamClosure() {
  823. function AsciiHexStream(str, maybeLength) {
  824. this.str = str;
  825. this.dict = str.dict;
  826. this.firstDigit = -1;
  827. if (maybeLength) {
  828. maybeLength = 0.5 * maybeLength;
  829. }
  830. DecodeStream.call(this, maybeLength);
  831. }
  832. AsciiHexStream.prototype = Object.create(DecodeStream.prototype);
  833. AsciiHexStream.prototype.readBlock = function AsciiHexStream_readBlock() {
  834. var UPSTREAM_BLOCK_SIZE = 8000;
  835. var bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE);
  836. if (!bytes.length) {
  837. this.eof = true;
  838. return;
  839. }
  840. var maxDecodeLength = bytes.length + 1 >> 1;
  841. var buffer = this.ensureBuffer(this.bufferLength + maxDecodeLength);
  842. var bufferLength = this.bufferLength;
  843. var firstDigit = this.firstDigit;
  844. for (var i = 0, ii = bytes.length; i < ii; i++) {
  845. var ch = bytes[i],
  846. digit;
  847. if (ch >= 0x30 && ch <= 0x39) {
  848. digit = ch & 0x0F;
  849. } else if (ch >= 0x41 && ch <= 0x46 || ch >= 0x61 && ch <= 0x66) {
  850. digit = (ch & 0x0F) + 9;
  851. } else if (ch === 0x3E) {
  852. this.eof = true;
  853. break;
  854. } else {
  855. continue;
  856. }
  857. if (firstDigit < 0) {
  858. firstDigit = digit;
  859. } else {
  860. buffer[bufferLength++] = firstDigit << 4 | digit;
  861. firstDigit = -1;
  862. }
  863. }
  864. if (firstDigit >= 0 && this.eof) {
  865. buffer[bufferLength++] = firstDigit << 4;
  866. firstDigit = -1;
  867. }
  868. this.firstDigit = firstDigit;
  869. this.bufferLength = bufferLength;
  870. };
  871. return AsciiHexStream;
  872. }();
  873. exports.AsciiHexStream = AsciiHexStream;
  874. var RunLengthStream = function RunLengthStreamClosure() {
  875. function RunLengthStream(str, maybeLength) {
  876. this.str = str;
  877. this.dict = str.dict;
  878. DecodeStream.call(this, maybeLength);
  879. }
  880. RunLengthStream.prototype = Object.create(DecodeStream.prototype);
  881. RunLengthStream.prototype.readBlock = function RunLengthStream_readBlock() {
  882. var repeatHeader = this.str.getBytes(2);
  883. if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) {
  884. this.eof = true;
  885. return;
  886. }
  887. var buffer;
  888. var bufferLength = this.bufferLength;
  889. var n = repeatHeader[0];
  890. if (n < 128) {
  891. buffer = this.ensureBuffer(bufferLength + n + 1);
  892. buffer[bufferLength++] = repeatHeader[1];
  893. if (n > 0) {
  894. var source = this.str.getBytes(n);
  895. buffer.set(source, bufferLength);
  896. bufferLength += n;
  897. }
  898. } else {
  899. n = 257 - n;
  900. var b = repeatHeader[1];
  901. buffer = this.ensureBuffer(bufferLength + n + 1);
  902. for (var i = 0; i < n; i++) {
  903. buffer[bufferLength++] = b;
  904. }
  905. }
  906. this.bufferLength = bufferLength;
  907. };
  908. return RunLengthStream;
  909. }();
  910. exports.RunLengthStream = RunLengthStream;
  911. var LZWStream = function LZWStreamClosure() {
  912. function LZWStream(str, maybeLength, earlyChange) {
  913. this.str = str;
  914. this.dict = str.dict;
  915. this.cachedData = 0;
  916. this.bitsCached = 0;
  917. var maxLzwDictionarySize = 4096;
  918. var lzwState = {
  919. earlyChange: earlyChange,
  920. codeLength: 9,
  921. nextCode: 258,
  922. dictionaryValues: new Uint8Array(maxLzwDictionarySize),
  923. dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
  924. dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
  925. currentSequence: new Uint8Array(maxLzwDictionarySize),
  926. currentSequenceLength: 0
  927. };
  928. for (var i = 0; i < 256; ++i) {
  929. lzwState.dictionaryValues[i] = i;
  930. lzwState.dictionaryLengths[i] = 1;
  931. }
  932. this.lzwState = lzwState;
  933. DecodeStream.call(this, maybeLength);
  934. }
  935. LZWStream.prototype = Object.create(DecodeStream.prototype);
  936. LZWStream.prototype.readBits = function LZWStream_readBits(n) {
  937. var bitsCached = this.bitsCached;
  938. var cachedData = this.cachedData;
  939. while (bitsCached < n) {
  940. var c = this.str.getByte();
  941. if (c === -1) {
  942. this.eof = true;
  943. return null;
  944. }
  945. cachedData = cachedData << 8 | c;
  946. bitsCached += 8;
  947. }
  948. this.bitsCached = bitsCached -= n;
  949. this.cachedData = cachedData;
  950. this.lastCode = null;
  951. return cachedData >>> bitsCached & (1 << n) - 1;
  952. };
  953. LZWStream.prototype.readBlock = function LZWStream_readBlock() {
  954. var blockSize = 512;
  955. var estimatedDecodedSize = blockSize * 2,
  956. decodedSizeDelta = blockSize;
  957. var i, j, q;
  958. var lzwState = this.lzwState;
  959. if (!lzwState) {
  960. return;
  961. }
  962. var earlyChange = lzwState.earlyChange;
  963. var nextCode = lzwState.nextCode;
  964. var dictionaryValues = lzwState.dictionaryValues;
  965. var dictionaryLengths = lzwState.dictionaryLengths;
  966. var dictionaryPrevCodes = lzwState.dictionaryPrevCodes;
  967. var codeLength = lzwState.codeLength;
  968. var prevCode = lzwState.prevCode;
  969. var currentSequence = lzwState.currentSequence;
  970. var currentSequenceLength = lzwState.currentSequenceLength;
  971. var decodedLength = 0;
  972. var currentBufferLength = this.bufferLength;
  973. var buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  974. for (i = 0; i < blockSize; i++) {
  975. var code = this.readBits(codeLength);
  976. var hasPrev = currentSequenceLength > 0;
  977. if (code < 256) {
  978. currentSequence[0] = code;
  979. currentSequenceLength = 1;
  980. } else if (code >= 258) {
  981. if (code < nextCode) {
  982. currentSequenceLength = dictionaryLengths[code];
  983. for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
  984. currentSequence[j] = dictionaryValues[q];
  985. q = dictionaryPrevCodes[q];
  986. }
  987. } else {
  988. currentSequence[currentSequenceLength++] = currentSequence[0];
  989. }
  990. } else if (code === 256) {
  991. codeLength = 9;
  992. nextCode = 258;
  993. currentSequenceLength = 0;
  994. continue;
  995. } else {
  996. this.eof = true;
  997. delete this.lzwState;
  998. break;
  999. }
  1000. if (hasPrev) {
  1001. dictionaryPrevCodes[nextCode] = prevCode;
  1002. dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
  1003. dictionaryValues[nextCode] = currentSequence[0];
  1004. nextCode++;
  1005. codeLength = nextCode + earlyChange & nextCode + earlyChange - 1 ? codeLength : Math.min(Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1, 12) | 0;
  1006. }
  1007. prevCode = code;
  1008. decodedLength += currentSequenceLength;
  1009. if (estimatedDecodedSize < decodedLength) {
  1010. do {
  1011. estimatedDecodedSize += decodedSizeDelta;
  1012. } while (estimatedDecodedSize < decodedLength);
  1013. buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1014. }
  1015. for (j = 0; j < currentSequenceLength; j++) {
  1016. buffer[currentBufferLength++] = currentSequence[j];
  1017. }
  1018. }
  1019. lzwState.nextCode = nextCode;
  1020. lzwState.codeLength = codeLength;
  1021. lzwState.prevCode = prevCode;
  1022. lzwState.currentSequenceLength = currentSequenceLength;
  1023. this.bufferLength = currentBufferLength;
  1024. };
  1025. return LZWStream;
  1026. }();
  1027. exports.LZWStream = LZWStream;
  1028. var NullStream = function NullStreamClosure() {
  1029. function NullStream() {
  1030. Stream.call(this, new Uint8Array(0));
  1031. }
  1032. NullStream.prototype = Stream.prototype;
  1033. return NullStream;
  1034. }();
  1035. exports.NullStream = NullStream;