stream.js 38 KB

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