stream.js 40 KB

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