2
0

stream.js 39 KB

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