stream.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  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.Jbig2Stream = exports.FlateStream = exports.DecodeStream = exports.DecryptStream = exports.CCITTFaxStream = exports.AsciiHexStream = exports.Ascii85Stream = undefined;
  20. var _util = require('../shared/util');
  21. var _primitives = require('./primitives');
  22. var _jbig = require('./jbig2');
  23. var _jpg = require('./jpg');
  24. var _jpx = require('./jpx');
  25. var Stream = function StreamClosure() {
  26. function Stream(arrayBuffer, start, length, dict) {
  27. this.bytes = arrayBuffer instanceof Uint8Array ? arrayBuffer : new Uint8Array(arrayBuffer);
  28. this.start = start || 0;
  29. this.pos = this.start;
  30. this.end = start + length || this.bytes.length;
  31. this.dict = dict;
  32. }
  33. Stream.prototype = {
  34. get length() {
  35. return this.end - this.start;
  36. },
  37. get isEmpty() {
  38. return this.length === 0;
  39. },
  40. getByte: function Stream_getByte() {
  41. if (this.pos >= this.end) {
  42. return -1;
  43. }
  44. return this.bytes[this.pos++];
  45. },
  46. getUint16: function Stream_getUint16() {
  47. var b0 = this.getByte();
  48. var b1 = this.getByte();
  49. if (b0 === -1 || b1 === -1) {
  50. return -1;
  51. }
  52. return (b0 << 8) + b1;
  53. },
  54. getInt32: function Stream_getInt32() {
  55. var b0 = this.getByte();
  56. var b1 = this.getByte();
  57. var b2 = this.getByte();
  58. var b3 = this.getByte();
  59. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  60. },
  61. getBytes: function Stream_getBytes(length) {
  62. var bytes = this.bytes;
  63. var pos = this.pos;
  64. var strEnd = this.end;
  65. if (!length) {
  66. return bytes.subarray(pos, strEnd);
  67. }
  68. var end = pos + length;
  69. if (end > strEnd) {
  70. end = strEnd;
  71. }
  72. this.pos = end;
  73. return bytes.subarray(pos, end);
  74. },
  75. peekByte: function Stream_peekByte() {
  76. var peekedByte = this.getByte();
  77. this.pos--;
  78. return peekedByte;
  79. },
  80. peekBytes: function Stream_peekBytes(length) {
  81. var bytes = this.getBytes(length);
  82. this.pos -= bytes.length;
  83. return bytes;
  84. },
  85. skip: function Stream_skip(n) {
  86. if (!n) {
  87. n = 1;
  88. }
  89. this.pos += n;
  90. },
  91. reset: function Stream_reset() {
  92. this.pos = this.start;
  93. },
  94. moveStart: function Stream_moveStart() {
  95. this.start = this.pos;
  96. },
  97. makeSubStream: function Stream_makeSubStream(start, length, dict) {
  98. return new Stream(this.bytes.buffer, start, length, dict);
  99. }
  100. };
  101. return Stream;
  102. }();
  103. var StringStream = function StringStreamClosure() {
  104. function StringStream(str) {
  105. var length = str.length;
  106. var bytes = new Uint8Array(length);
  107. for (var n = 0; n < length; ++n) {
  108. bytes[n] = str.charCodeAt(n);
  109. }
  110. Stream.call(this, bytes);
  111. }
  112. StringStream.prototype = Stream.prototype;
  113. return StringStream;
  114. }();
  115. var DecodeStream = function DecodeStreamClosure() {
  116. var emptyBuffer = new Uint8Array(0);
  117. function DecodeStream(maybeMinBufferLength) {
  118. this.pos = 0;
  119. this.bufferLength = 0;
  120. this.eof = false;
  121. this.buffer = emptyBuffer;
  122. this.minBufferLength = 512;
  123. if (maybeMinBufferLength) {
  124. while (this.minBufferLength < maybeMinBufferLength) {
  125. this.minBufferLength *= 2;
  126. }
  127. }
  128. }
  129. DecodeStream.prototype = {
  130. get isEmpty() {
  131. while (!this.eof && this.bufferLength === 0) {
  132. this.readBlock();
  133. }
  134. return this.bufferLength === 0;
  135. },
  136. ensureBuffer: function DecodeStream_ensureBuffer(requested) {
  137. var buffer = this.buffer;
  138. if (requested <= buffer.byteLength) {
  139. return buffer;
  140. }
  141. var size = this.minBufferLength;
  142. while (size < requested) {
  143. size *= 2;
  144. }
  145. var buffer2 = new Uint8Array(size);
  146. buffer2.set(buffer);
  147. return this.buffer = buffer2;
  148. },
  149. getByte: function DecodeStream_getByte() {
  150. var pos = this.pos;
  151. while (this.bufferLength <= pos) {
  152. if (this.eof) {
  153. return -1;
  154. }
  155. this.readBlock();
  156. }
  157. return this.buffer[this.pos++];
  158. },
  159. getUint16: function DecodeStream_getUint16() {
  160. var b0 = this.getByte();
  161. var b1 = this.getByte();
  162. if (b0 === -1 || b1 === -1) {
  163. return -1;
  164. }
  165. return (b0 << 8) + b1;
  166. },
  167. getInt32: function DecodeStream_getInt32() {
  168. var b0 = this.getByte();
  169. var b1 = this.getByte();
  170. var b2 = this.getByte();
  171. var b3 = this.getByte();
  172. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  173. },
  174. getBytes: function DecodeStream_getBytes(length) {
  175. var end,
  176. pos = this.pos;
  177. if (length) {
  178. this.ensureBuffer(pos + length);
  179. end = pos + length;
  180. while (!this.eof && this.bufferLength < end) {
  181. this.readBlock();
  182. }
  183. var bufEnd = this.bufferLength;
  184. if (end > bufEnd) {
  185. end = bufEnd;
  186. }
  187. } else {
  188. while (!this.eof) {
  189. this.readBlock();
  190. }
  191. end = this.bufferLength;
  192. }
  193. this.pos = end;
  194. return this.buffer.subarray(pos, end);
  195. },
  196. peekByte: function DecodeStream_peekByte() {
  197. var peekedByte = this.getByte();
  198. this.pos--;
  199. return peekedByte;
  200. },
  201. peekBytes: function DecodeStream_peekBytes(length) {
  202. var bytes = this.getBytes(length);
  203. this.pos -= bytes.length;
  204. return bytes;
  205. },
  206. makeSubStream: function DecodeStream_makeSubStream(start, length, dict) {
  207. var end = start + length;
  208. while (this.bufferLength <= end && !this.eof) {
  209. this.readBlock();
  210. }
  211. return new Stream(this.buffer, start, length, dict);
  212. },
  213. skip: function DecodeStream_skip(n) {
  214. if (!n) {
  215. n = 1;
  216. }
  217. this.pos += n;
  218. },
  219. reset: function DecodeStream_reset() {
  220. this.pos = 0;
  221. },
  222. getBaseStreams: function DecodeStream_getBaseStreams() {
  223. if (this.str && this.str.getBaseStreams) {
  224. return this.str.getBaseStreams();
  225. }
  226. return [];
  227. }
  228. };
  229. return DecodeStream;
  230. }();
  231. var StreamsSequenceStream = function StreamsSequenceStreamClosure() {
  232. function StreamsSequenceStream(streams) {
  233. this.streams = streams;
  234. DecodeStream.call(this, null);
  235. }
  236. StreamsSequenceStream.prototype = Object.create(DecodeStream.prototype);
  237. StreamsSequenceStream.prototype.readBlock = function streamSequenceStreamReadBlock() {
  238. var streams = this.streams;
  239. if (streams.length === 0) {
  240. this.eof = true;
  241. return;
  242. }
  243. var stream = streams.shift();
  244. var chunk = stream.getBytes();
  245. var bufferLength = this.bufferLength;
  246. var newLength = bufferLength + chunk.length;
  247. var buffer = this.ensureBuffer(newLength);
  248. buffer.set(chunk, bufferLength);
  249. this.bufferLength = newLength;
  250. };
  251. StreamsSequenceStream.prototype.getBaseStreams = function StreamsSequenceStream_getBaseStreams() {
  252. var baseStreams = [];
  253. for (var i = 0, ii = this.streams.length; i < ii; i++) {
  254. var stream = this.streams[i];
  255. if (stream.getBaseStreams) {
  256. _util.Util.appendToArray(baseStreams, stream.getBaseStreams());
  257. }
  258. }
  259. return baseStreams;
  260. };
  261. return StreamsSequenceStream;
  262. }();
  263. var FlateStream = function FlateStreamClosure() {
  264. var codeLenCodeMap = new Int32Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
  265. 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]);
  266. 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]);
  267. 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];
  268. 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];
  269. function FlateStream(str, maybeLength) {
  270. this.str = str;
  271. this.dict = str.dict;
  272. var cmf = str.getByte();
  273. var flg = str.getByte();
  274. if (cmf === -1 || flg === -1) {
  275. throw new _util.FormatError('Invalid header in flate stream: ' + cmf + ', ' + flg);
  276. }
  277. if ((cmf & 0x0f) !== 0x08) {
  278. throw new _util.FormatError('Unknown compression method in flate stream: ' + cmf + ', ' + flg);
  279. }
  280. if (((cmf << 8) + flg) % 31 !== 0) {
  281. throw new _util.FormatError('Bad FCHECK in flate stream: ' + cmf + ', ' + flg);
  282. }
  283. if (flg & 0x20) {
  284. throw new _util.FormatError('FDICT bit set in flate stream: ' + cmf + ', ' + flg);
  285. }
  286. this.codeSize = 0;
  287. this.codeBuf = 0;
  288. DecodeStream.call(this, maybeLength);
  289. }
  290. FlateStream.prototype = Object.create(DecodeStream.prototype);
  291. FlateStream.prototype.getBits = function FlateStream_getBits(bits) {
  292. var str = this.str;
  293. var codeSize = this.codeSize;
  294. var codeBuf = this.codeBuf;
  295. var b;
  296. while (codeSize < bits) {
  297. if ((b = str.getByte()) === -1) {
  298. throw new _util.FormatError('Bad encoding in flate stream');
  299. }
  300. codeBuf |= b << codeSize;
  301. codeSize += 8;
  302. }
  303. b = codeBuf & (1 << bits) - 1;
  304. this.codeBuf = codeBuf >> bits;
  305. this.codeSize = codeSize -= bits;
  306. return b;
  307. };
  308. FlateStream.prototype.getCode = function FlateStream_getCode(table) {
  309. var str = this.str;
  310. var codes = table[0];
  311. var maxLen = table[1];
  312. var codeSize = this.codeSize;
  313. var codeBuf = this.codeBuf;
  314. var b;
  315. while (codeSize < maxLen) {
  316. if ((b = str.getByte()) === -1) {
  317. break;
  318. }
  319. codeBuf |= b << codeSize;
  320. codeSize += 8;
  321. }
  322. var code = codes[codeBuf & (1 << maxLen) - 1];
  323. var codeLen = code >> 16;
  324. var codeVal = code & 0xffff;
  325. if (codeLen < 1 || codeSize < codeLen) {
  326. throw new _util.FormatError('Bad encoding in flate stream');
  327. }
  328. this.codeBuf = codeBuf >> codeLen;
  329. this.codeSize = codeSize - codeLen;
  330. return codeVal;
  331. };
  332. FlateStream.prototype.generateHuffmanTable = function flateStreamGenerateHuffmanTable(lengths) {
  333. var n = lengths.length;
  334. var maxLen = 0;
  335. var i;
  336. for (i = 0; i < n; ++i) {
  337. if (lengths[i] > maxLen) {
  338. maxLen = lengths[i];
  339. }
  340. }
  341. var size = 1 << maxLen;
  342. var codes = new Int32Array(size);
  343. for (var len = 1, code = 0, skip = 2; len <= maxLen; ++len, code <<= 1, skip <<= 1) {
  344. for (var val = 0; val < n; ++val) {
  345. if (lengths[val] === len) {
  346. var code2 = 0;
  347. var t = code;
  348. for (i = 0; i < len; ++i) {
  349. code2 = code2 << 1 | t & 1;
  350. t >>= 1;
  351. }
  352. for (i = code2; i < size; i += skip) {
  353. codes[i] = len << 16 | val;
  354. }
  355. ++code;
  356. }
  357. }
  358. }
  359. return [codes, maxLen];
  360. };
  361. FlateStream.prototype.readBlock = function FlateStream_readBlock() {
  362. var buffer, len;
  363. var str = this.str;
  364. var hdr = this.getBits(3);
  365. if (hdr & 1) {
  366. this.eof = true;
  367. }
  368. hdr >>= 1;
  369. if (hdr === 0) {
  370. var b;
  371. if ((b = str.getByte()) === -1) {
  372. throw new _util.FormatError('Bad block header in flate stream');
  373. }
  374. var blockLen = b;
  375. if ((b = str.getByte()) === -1) {
  376. throw new _util.FormatError('Bad block header in flate stream');
  377. }
  378. blockLen |= b << 8;
  379. if ((b = str.getByte()) === -1) {
  380. throw new _util.FormatError('Bad block header in flate stream');
  381. }
  382. var check = b;
  383. if ((b = str.getByte()) === -1) {
  384. throw new _util.FormatError('Bad block header in flate stream');
  385. }
  386. check |= b << 8;
  387. if (check !== (~blockLen & 0xffff) && (blockLen !== 0 || check !== 0)) {
  388. throw new _util.FormatError('Bad uncompressed block length in flate stream');
  389. }
  390. this.codeBuf = 0;
  391. this.codeSize = 0;
  392. var bufferLength = this.bufferLength;
  393. buffer = this.ensureBuffer(bufferLength + blockLen);
  394. var end = bufferLength + blockLen;
  395. this.bufferLength = end;
  396. if (blockLen === 0) {
  397. if (str.peekByte() === -1) {
  398. this.eof = true;
  399. }
  400. } else {
  401. for (var n = bufferLength; n < end; ++n) {
  402. if ((b = str.getByte()) === -1) {
  403. this.eof = true;
  404. break;
  405. }
  406. buffer[n] = b;
  407. }
  408. }
  409. return;
  410. }
  411. var litCodeTable;
  412. var distCodeTable;
  413. if (hdr === 1) {
  414. litCodeTable = fixedLitCodeTab;
  415. distCodeTable = fixedDistCodeTab;
  416. } else if (hdr === 2) {
  417. var numLitCodes = this.getBits(5) + 257;
  418. var numDistCodes = this.getBits(5) + 1;
  419. var numCodeLenCodes = this.getBits(4) + 4;
  420. var codeLenCodeLengths = new Uint8Array(codeLenCodeMap.length);
  421. var i;
  422. for (i = 0; i < numCodeLenCodes; ++i) {
  423. codeLenCodeLengths[codeLenCodeMap[i]] = this.getBits(3);
  424. }
  425. var codeLenCodeTab = this.generateHuffmanTable(codeLenCodeLengths);
  426. len = 0;
  427. i = 0;
  428. var codes = numLitCodes + numDistCodes;
  429. var codeLengths = new Uint8Array(codes);
  430. var bitsLength, bitsOffset, what;
  431. while (i < codes) {
  432. var code = this.getCode(codeLenCodeTab);
  433. if (code === 16) {
  434. bitsLength = 2;
  435. bitsOffset = 3;
  436. what = len;
  437. } else if (code === 17) {
  438. bitsLength = 3;
  439. bitsOffset = 3;
  440. what = len = 0;
  441. } else if (code === 18) {
  442. bitsLength = 7;
  443. bitsOffset = 11;
  444. what = len = 0;
  445. } else {
  446. codeLengths[i++] = len = code;
  447. continue;
  448. }
  449. var repeatLength = this.getBits(bitsLength) + bitsOffset;
  450. while (repeatLength-- > 0) {
  451. codeLengths[i++] = what;
  452. }
  453. }
  454. litCodeTable = this.generateHuffmanTable(codeLengths.subarray(0, numLitCodes));
  455. distCodeTable = this.generateHuffmanTable(codeLengths.subarray(numLitCodes, codes));
  456. } else {
  457. throw new _util.FormatError('Unknown block type in flate stream');
  458. }
  459. buffer = this.buffer;
  460. var limit = buffer ? buffer.length : 0;
  461. var pos = this.bufferLength;
  462. while (true) {
  463. var code1 = this.getCode(litCodeTable);
  464. if (code1 < 256) {
  465. if (pos + 1 >= limit) {
  466. buffer = this.ensureBuffer(pos + 1);
  467. limit = buffer.length;
  468. }
  469. buffer[pos++] = code1;
  470. continue;
  471. }
  472. if (code1 === 256) {
  473. this.bufferLength = pos;
  474. return;
  475. }
  476. code1 -= 257;
  477. code1 = lengthDecode[code1];
  478. var code2 = code1 >> 16;
  479. if (code2 > 0) {
  480. code2 = this.getBits(code2);
  481. }
  482. len = (code1 & 0xffff) + code2;
  483. code1 = this.getCode(distCodeTable);
  484. code1 = distDecode[code1];
  485. code2 = code1 >> 16;
  486. if (code2 > 0) {
  487. code2 = this.getBits(code2);
  488. }
  489. var dist = (code1 & 0xffff) + code2;
  490. if (pos + len >= limit) {
  491. buffer = this.ensureBuffer(pos + len);
  492. limit = buffer.length;
  493. }
  494. for (var k = 0; k < len; ++k, ++pos) {
  495. buffer[pos] = buffer[pos - dist];
  496. }
  497. }
  498. };
  499. return FlateStream;
  500. }();
  501. var PredictorStream = function PredictorStreamClosure() {
  502. function PredictorStream(str, maybeLength, params) {
  503. if (!(0, _primitives.isDict)(params)) {
  504. return str;
  505. }
  506. var predictor = this.predictor = params.get('Predictor') || 1;
  507. if (predictor <= 1) {
  508. return str;
  509. }
  510. if (predictor !== 2 && (predictor < 10 || predictor > 15)) {
  511. throw new _util.FormatError('Unsupported predictor: ' + predictor);
  512. }
  513. if (predictor === 2) {
  514. this.readBlock = this.readBlockTiff;
  515. } else {
  516. this.readBlock = this.readBlockPng;
  517. }
  518. this.str = str;
  519. this.dict = str.dict;
  520. var colors = this.colors = params.get('Colors') || 1;
  521. var bits = this.bits = params.get('BitsPerComponent') || 8;
  522. var columns = this.columns = params.get('Columns') || 1;
  523. this.pixBytes = colors * bits + 7 >> 3;
  524. this.rowBytes = columns * colors * bits + 7 >> 3;
  525. DecodeStream.call(this, maybeLength);
  526. return this;
  527. }
  528. PredictorStream.prototype = Object.create(DecodeStream.prototype);
  529. PredictorStream.prototype.readBlockTiff = function predictorStreamReadBlockTiff() {
  530. var rowBytes = this.rowBytes;
  531. var bufferLength = this.bufferLength;
  532. var buffer = this.ensureBuffer(bufferLength + rowBytes);
  533. var bits = this.bits;
  534. var colors = this.colors;
  535. var rawBytes = this.str.getBytes(rowBytes);
  536. this.eof = !rawBytes.length;
  537. if (this.eof) {
  538. return;
  539. }
  540. var inbuf = 0,
  541. outbuf = 0;
  542. var inbits = 0,
  543. outbits = 0;
  544. var pos = bufferLength;
  545. var i;
  546. if (bits === 1 && colors === 1) {
  547. for (i = 0; i < rowBytes; ++i) {
  548. var c = rawBytes[i] ^ inbuf;
  549. c ^= c >> 1;
  550. c ^= c >> 2;
  551. c ^= c >> 4;
  552. inbuf = (c & 1) << 7;
  553. buffer[pos++] = c;
  554. }
  555. } else if (bits === 8) {
  556. for (i = 0; i < colors; ++i) {
  557. buffer[pos++] = rawBytes[i];
  558. }
  559. for (; i < rowBytes; ++i) {
  560. buffer[pos] = buffer[pos - colors] + rawBytes[i];
  561. pos++;
  562. }
  563. } else if (bits === 16) {
  564. var bytesPerPixel = colors * 2;
  565. for (i = 0; i < bytesPerPixel; ++i) {
  566. buffer[pos++] = rawBytes[i];
  567. }
  568. for (; i < rowBytes; i += 2) {
  569. var sum = ((rawBytes[i] & 0xFF) << 8) + (rawBytes[i + 1] & 0xFF) + ((buffer[pos - bytesPerPixel] & 0xFF) << 8) + (buffer[pos - bytesPerPixel + 1] & 0xFF);
  570. buffer[pos++] = sum >> 8 & 0xFF;
  571. buffer[pos++] = sum & 0xFF;
  572. }
  573. } else {
  574. var compArray = new Uint8Array(colors + 1);
  575. var bitMask = (1 << bits) - 1;
  576. var j = 0,
  577. k = bufferLength;
  578. var columns = this.columns;
  579. for (i = 0; i < columns; ++i) {
  580. for (var kk = 0; kk < colors; ++kk) {
  581. if (inbits < bits) {
  582. inbuf = inbuf << 8 | rawBytes[j++] & 0xFF;
  583. inbits += 8;
  584. }
  585. compArray[kk] = compArray[kk] + (inbuf >> inbits - bits) & bitMask;
  586. inbits -= bits;
  587. outbuf = outbuf << bits | compArray[kk];
  588. outbits += bits;
  589. if (outbits >= 8) {
  590. buffer[k++] = outbuf >> outbits - 8 & 0xFF;
  591. outbits -= 8;
  592. }
  593. }
  594. }
  595. if (outbits > 0) {
  596. buffer[k++] = (outbuf << 8 - outbits) + (inbuf & (1 << 8 - outbits) - 1);
  597. }
  598. }
  599. this.bufferLength += rowBytes;
  600. };
  601. PredictorStream.prototype.readBlockPng = function predictorStreamReadBlockPng() {
  602. var rowBytes = this.rowBytes;
  603. var pixBytes = this.pixBytes;
  604. var predictor = this.str.getByte();
  605. var rawBytes = this.str.getBytes(rowBytes);
  606. this.eof = !rawBytes.length;
  607. if (this.eof) {
  608. return;
  609. }
  610. var bufferLength = this.bufferLength;
  611. var buffer = this.ensureBuffer(bufferLength + rowBytes);
  612. var prevRow = buffer.subarray(bufferLength - rowBytes, bufferLength);
  613. if (prevRow.length === 0) {
  614. prevRow = new Uint8Array(rowBytes);
  615. }
  616. var i,
  617. j = bufferLength,
  618. up,
  619. c;
  620. switch (predictor) {
  621. case 0:
  622. for (i = 0; i < rowBytes; ++i) {
  623. buffer[j++] = rawBytes[i];
  624. }
  625. break;
  626. case 1:
  627. for (i = 0; i < pixBytes; ++i) {
  628. buffer[j++] = rawBytes[i];
  629. }
  630. for (; i < rowBytes; ++i) {
  631. buffer[j] = buffer[j - pixBytes] + rawBytes[i] & 0xFF;
  632. j++;
  633. }
  634. break;
  635. case 2:
  636. for (i = 0; i < rowBytes; ++i) {
  637. buffer[j++] = prevRow[i] + rawBytes[i] & 0xFF;
  638. }
  639. break;
  640. case 3:
  641. for (i = 0; i < pixBytes; ++i) {
  642. buffer[j++] = (prevRow[i] >> 1) + rawBytes[i];
  643. }
  644. for (; i < rowBytes; ++i) {
  645. buffer[j] = (prevRow[i] + buffer[j - pixBytes] >> 1) + rawBytes[i] & 0xFF;
  646. j++;
  647. }
  648. break;
  649. case 4:
  650. for (i = 0; i < pixBytes; ++i) {
  651. up = prevRow[i];
  652. c = rawBytes[i];
  653. buffer[j++] = up + c;
  654. }
  655. for (; i < rowBytes; ++i) {
  656. up = prevRow[i];
  657. var upLeft = prevRow[i - pixBytes];
  658. var left = buffer[j - pixBytes];
  659. var p = left + up - upLeft;
  660. var pa = p - left;
  661. if (pa < 0) {
  662. pa = -pa;
  663. }
  664. var pb = p - up;
  665. if (pb < 0) {
  666. pb = -pb;
  667. }
  668. var pc = p - upLeft;
  669. if (pc < 0) {
  670. pc = -pc;
  671. }
  672. c = rawBytes[i];
  673. if (pa <= pb && pa <= pc) {
  674. buffer[j++] = left + c;
  675. } else if (pb <= pc) {
  676. buffer[j++] = up + c;
  677. } else {
  678. buffer[j++] = upLeft + c;
  679. }
  680. }
  681. break;
  682. default:
  683. throw new _util.FormatError('Unsupported predictor: ' + predictor);
  684. }
  685. this.bufferLength += rowBytes;
  686. };
  687. return PredictorStream;
  688. }();
  689. var JpegStream = function JpegStreamClosure() {
  690. function JpegStream(stream, maybeLength, dict, params) {
  691. var ch;
  692. while ((ch = stream.getByte()) !== -1) {
  693. if (ch === 0xFF) {
  694. stream.skip(-1);
  695. break;
  696. }
  697. }
  698. this.stream = stream;
  699. this.maybeLength = maybeLength;
  700. this.dict = dict;
  701. this.params = params;
  702. DecodeStream.call(this, maybeLength);
  703. }
  704. JpegStream.prototype = Object.create(DecodeStream.prototype);
  705. Object.defineProperty(JpegStream.prototype, 'bytes', {
  706. get: function JpegStream_bytes() {
  707. return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
  708. },
  709. configurable: true
  710. });
  711. JpegStream.prototype.ensureBuffer = function JpegStream_ensureBuffer(req) {
  712. if (this.bufferLength) {
  713. return;
  714. }
  715. var jpegImage = new _jpg.JpegImage();
  716. var decodeArr = this.dict.getArray('Decode', 'D');
  717. if (this.forceRGB && Array.isArray(decodeArr)) {
  718. var bitsPerComponent = this.dict.get('BitsPerComponent') || 8;
  719. var decodeArrLength = decodeArr.length;
  720. var transform = new Int32Array(decodeArrLength);
  721. var transformNeeded = false;
  722. var maxValue = (1 << bitsPerComponent) - 1;
  723. for (var i = 0; i < decodeArrLength; i += 2) {
  724. transform[i] = (decodeArr[i + 1] - decodeArr[i]) * 256 | 0;
  725. transform[i + 1] = decodeArr[i] * maxValue | 0;
  726. if (transform[i] !== 256 || transform[i + 1] !== 0) {
  727. transformNeeded = true;
  728. }
  729. }
  730. if (transformNeeded) {
  731. jpegImage.decodeTransform = transform;
  732. }
  733. }
  734. if ((0, _primitives.isDict)(this.params)) {
  735. var colorTransform = this.params.get('ColorTransform');
  736. if (Number.isInteger(colorTransform)) {
  737. jpegImage.colorTransform = colorTransform;
  738. }
  739. }
  740. jpegImage.parse(this.bytes);
  741. var data = jpegImage.getData(this.drawWidth, this.drawHeight, this.forceRGB);
  742. this.buffer = data;
  743. this.bufferLength = data.length;
  744. this.eof = true;
  745. };
  746. JpegStream.prototype.getBytes = function JpegStream_getBytes(length) {
  747. this.ensureBuffer();
  748. return this.buffer;
  749. };
  750. JpegStream.prototype.getIR = function JpegStream_getIR(forceDataSchema) {
  751. return (0, _util.createObjectURL)(this.bytes, 'image/jpeg', forceDataSchema);
  752. };
  753. return JpegStream;
  754. }();
  755. var JpxStream = function JpxStreamClosure() {
  756. function JpxStream(stream, maybeLength, dict, params) {
  757. this.stream = stream;
  758. this.maybeLength = maybeLength;
  759. this.dict = dict;
  760. this.params = params;
  761. DecodeStream.call(this, maybeLength);
  762. }
  763. JpxStream.prototype = Object.create(DecodeStream.prototype);
  764. Object.defineProperty(JpxStream.prototype, 'bytes', {
  765. get: function JpxStream_bytes() {
  766. return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
  767. },
  768. configurable: true
  769. });
  770. JpxStream.prototype.ensureBuffer = function JpxStream_ensureBuffer(req) {
  771. if (this.bufferLength) {
  772. return;
  773. }
  774. var jpxImage = new _jpx.JpxImage();
  775. jpxImage.parse(this.bytes);
  776. var width = jpxImage.width;
  777. var height = jpxImage.height;
  778. var componentsCount = jpxImage.componentsCount;
  779. var tileCount = jpxImage.tiles.length;
  780. if (tileCount === 1) {
  781. this.buffer = jpxImage.tiles[0].items;
  782. } else {
  783. var data = new Uint8ClampedArray(width * height * componentsCount);
  784. for (var k = 0; k < tileCount; k++) {
  785. var tileComponents = jpxImage.tiles[k];
  786. var tileWidth = tileComponents.width;
  787. var tileHeight = tileComponents.height;
  788. var tileLeft = tileComponents.left;
  789. var tileTop = tileComponents.top;
  790. var src = tileComponents.items;
  791. var srcPosition = 0;
  792. var dataPosition = (width * tileTop + tileLeft) * componentsCount;
  793. var imgRowSize = width * componentsCount;
  794. var tileRowSize = tileWidth * componentsCount;
  795. for (var j = 0; j < tileHeight; j++) {
  796. var rowBytes = src.subarray(srcPosition, srcPosition + tileRowSize);
  797. data.set(rowBytes, dataPosition);
  798. srcPosition += tileRowSize;
  799. dataPosition += imgRowSize;
  800. }
  801. }
  802. this.buffer = data;
  803. }
  804. this.bufferLength = this.buffer.length;
  805. this.eof = true;
  806. };
  807. return JpxStream;
  808. }();
  809. var Jbig2Stream = function Jbig2StreamClosure() {
  810. function Jbig2Stream(stream, maybeLength, dict, params) {
  811. this.stream = stream;
  812. this.maybeLength = maybeLength;
  813. this.dict = dict;
  814. this.params = params;
  815. DecodeStream.call(this, maybeLength);
  816. }
  817. Jbig2Stream.prototype = Object.create(DecodeStream.prototype);
  818. Object.defineProperty(Jbig2Stream.prototype, 'bytes', {
  819. get: function Jbig2Stream_bytes() {
  820. return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
  821. },
  822. configurable: true
  823. });
  824. Jbig2Stream.prototype.ensureBuffer = function Jbig2Stream_ensureBuffer(req) {
  825. if (this.bufferLength) {
  826. return;
  827. }
  828. var jbig2Image = new _jbig.Jbig2Image();
  829. var chunks = [];
  830. if ((0, _primitives.isDict)(this.params)) {
  831. var globalsStream = this.params.get('JBIG2Globals');
  832. if ((0, _primitives.isStream)(globalsStream)) {
  833. var globals = globalsStream.getBytes();
  834. chunks.push({
  835. data: globals,
  836. start: 0,
  837. end: globals.length
  838. });
  839. }
  840. }
  841. chunks.push({
  842. data: this.bytes,
  843. start: 0,
  844. end: this.bytes.length
  845. });
  846. var data = jbig2Image.parseChunks(chunks);
  847. var dataLength = data.length;
  848. for (var i = 0; i < dataLength; i++) {
  849. data[i] ^= 0xFF;
  850. }
  851. this.buffer = data;
  852. this.bufferLength = dataLength;
  853. this.eof = true;
  854. };
  855. return Jbig2Stream;
  856. }();
  857. var DecryptStream = function DecryptStreamClosure() {
  858. function DecryptStream(str, maybeLength, decrypt) {
  859. this.str = str;
  860. this.dict = str.dict;
  861. this.decrypt = decrypt;
  862. this.nextChunk = null;
  863. this.initialized = false;
  864. DecodeStream.call(this, maybeLength);
  865. }
  866. var chunkSize = 512;
  867. DecryptStream.prototype = Object.create(DecodeStream.prototype);
  868. DecryptStream.prototype.readBlock = function DecryptStream_readBlock() {
  869. var chunk;
  870. if (this.initialized) {
  871. chunk = this.nextChunk;
  872. } else {
  873. chunk = this.str.getBytes(chunkSize);
  874. this.initialized = true;
  875. }
  876. if (!chunk || chunk.length === 0) {
  877. this.eof = true;
  878. return;
  879. }
  880. this.nextChunk = this.str.getBytes(chunkSize);
  881. var hasMoreData = this.nextChunk && this.nextChunk.length > 0;
  882. var decrypt = this.decrypt;
  883. chunk = decrypt(chunk, !hasMoreData);
  884. var bufferLength = this.bufferLength;
  885. var i,
  886. n = chunk.length;
  887. var buffer = this.ensureBuffer(bufferLength + n);
  888. for (i = 0; i < n; i++) {
  889. buffer[bufferLength++] = chunk[i];
  890. }
  891. this.bufferLength = bufferLength;
  892. };
  893. return DecryptStream;
  894. }();
  895. var Ascii85Stream = function Ascii85StreamClosure() {
  896. function Ascii85Stream(str, maybeLength) {
  897. this.str = str;
  898. this.dict = str.dict;
  899. this.input = new Uint8Array(5);
  900. if (maybeLength) {
  901. maybeLength = 0.8 * maybeLength;
  902. }
  903. DecodeStream.call(this, maybeLength);
  904. }
  905. Ascii85Stream.prototype = Object.create(DecodeStream.prototype);
  906. Ascii85Stream.prototype.readBlock = function Ascii85Stream_readBlock() {
  907. var TILDA_CHAR = 0x7E;
  908. var Z_LOWER_CHAR = 0x7A;
  909. var EOF = -1;
  910. var str = this.str;
  911. var c = str.getByte();
  912. while ((0, _util.isSpace)(c)) {
  913. c = str.getByte();
  914. }
  915. if (c === EOF || c === TILDA_CHAR) {
  916. this.eof = true;
  917. return;
  918. }
  919. var bufferLength = this.bufferLength,
  920. buffer;
  921. var i;
  922. if (c === Z_LOWER_CHAR) {
  923. buffer = this.ensureBuffer(bufferLength + 4);
  924. for (i = 0; i < 4; ++i) {
  925. buffer[bufferLength + i] = 0;
  926. }
  927. this.bufferLength += 4;
  928. } else {
  929. var input = this.input;
  930. input[0] = c;
  931. for (i = 1; i < 5; ++i) {
  932. c = str.getByte();
  933. while ((0, _util.isSpace)(c)) {
  934. c = str.getByte();
  935. }
  936. input[i] = c;
  937. if (c === EOF || c === TILDA_CHAR) {
  938. break;
  939. }
  940. }
  941. buffer = this.ensureBuffer(bufferLength + i - 1);
  942. this.bufferLength += i - 1;
  943. if (i < 5) {
  944. for (; i < 5; ++i) {
  945. input[i] = 0x21 + 84;
  946. }
  947. this.eof = true;
  948. }
  949. var t = 0;
  950. for (i = 0; i < 5; ++i) {
  951. t = t * 85 + (input[i] - 0x21);
  952. }
  953. for (i = 3; i >= 0; --i) {
  954. buffer[bufferLength + i] = t & 0xFF;
  955. t >>= 8;
  956. }
  957. }
  958. };
  959. return Ascii85Stream;
  960. }();
  961. var AsciiHexStream = function AsciiHexStreamClosure() {
  962. function AsciiHexStream(str, maybeLength) {
  963. this.str = str;
  964. this.dict = str.dict;
  965. this.firstDigit = -1;
  966. if (maybeLength) {
  967. maybeLength = 0.5 * maybeLength;
  968. }
  969. DecodeStream.call(this, maybeLength);
  970. }
  971. AsciiHexStream.prototype = Object.create(DecodeStream.prototype);
  972. AsciiHexStream.prototype.readBlock = function AsciiHexStream_readBlock() {
  973. var UPSTREAM_BLOCK_SIZE = 8000;
  974. var bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE);
  975. if (!bytes.length) {
  976. this.eof = true;
  977. return;
  978. }
  979. var maxDecodeLength = bytes.length + 1 >> 1;
  980. var buffer = this.ensureBuffer(this.bufferLength + maxDecodeLength);
  981. var bufferLength = this.bufferLength;
  982. var firstDigit = this.firstDigit;
  983. for (var i = 0, ii = bytes.length; i < ii; i++) {
  984. var ch = bytes[i],
  985. digit;
  986. if (ch >= 0x30 && ch <= 0x39) {
  987. digit = ch & 0x0F;
  988. } else if (ch >= 0x41 && ch <= 0x46 || ch >= 0x61 && ch <= 0x66) {
  989. digit = (ch & 0x0F) + 9;
  990. } else if (ch === 0x3E) {
  991. this.eof = true;
  992. break;
  993. } else {
  994. continue;
  995. }
  996. if (firstDigit < 0) {
  997. firstDigit = digit;
  998. } else {
  999. buffer[bufferLength++] = firstDigit << 4 | digit;
  1000. firstDigit = -1;
  1001. }
  1002. }
  1003. if (firstDigit >= 0 && this.eof) {
  1004. buffer[bufferLength++] = firstDigit << 4;
  1005. firstDigit = -1;
  1006. }
  1007. this.firstDigit = firstDigit;
  1008. this.bufferLength = bufferLength;
  1009. };
  1010. return AsciiHexStream;
  1011. }();
  1012. var RunLengthStream = function RunLengthStreamClosure() {
  1013. function RunLengthStream(str, maybeLength) {
  1014. this.str = str;
  1015. this.dict = str.dict;
  1016. DecodeStream.call(this, maybeLength);
  1017. }
  1018. RunLengthStream.prototype = Object.create(DecodeStream.prototype);
  1019. RunLengthStream.prototype.readBlock = function RunLengthStream_readBlock() {
  1020. var repeatHeader = this.str.getBytes(2);
  1021. if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) {
  1022. this.eof = true;
  1023. return;
  1024. }
  1025. var buffer;
  1026. var bufferLength = this.bufferLength;
  1027. var n = repeatHeader[0];
  1028. if (n < 128) {
  1029. buffer = this.ensureBuffer(bufferLength + n + 1);
  1030. buffer[bufferLength++] = repeatHeader[1];
  1031. if (n > 0) {
  1032. var source = this.str.getBytes(n);
  1033. buffer.set(source, bufferLength);
  1034. bufferLength += n;
  1035. }
  1036. } else {
  1037. n = 257 - n;
  1038. var b = repeatHeader[1];
  1039. buffer = this.ensureBuffer(bufferLength + n + 1);
  1040. for (var i = 0; i < n; i++) {
  1041. buffer[bufferLength++] = b;
  1042. }
  1043. }
  1044. this.bufferLength = bufferLength;
  1045. };
  1046. return RunLengthStream;
  1047. }();
  1048. var CCITTFaxStream = function CCITTFaxStreamClosure() {
  1049. var ccittEOL = -2;
  1050. var ccittEOF = -1;
  1051. var twoDimPass = 0;
  1052. var twoDimHoriz = 1;
  1053. var twoDimVert0 = 2;
  1054. var twoDimVertR1 = 3;
  1055. var twoDimVertL1 = 4;
  1056. var twoDimVertR2 = 5;
  1057. var twoDimVertL2 = 6;
  1058. var twoDimVertR3 = 7;
  1059. var twoDimVertL3 = 8;
  1060. var twoDimTable = [[-1, -1], [-1, -1], [7, twoDimVertL3], [7, twoDimVertR3], [6, twoDimVertL2], [6, twoDimVertL2], [6, twoDimVertR2], [6, twoDimVertR2], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0]];
  1061. var whiteTable1 = [[-1, -1], [12, ccittEOL], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [11, 1792], [11, 1792], [12, 1984], [12, 2048], [12, 2112], [12, 2176], [12, 2240], [12, 2304], [11, 1856], [11, 1856], [11, 1920], [11, 1920], [12, 2368], [12, 2432], [12, 2496], [12, 2560]];
  1062. var whiteTable2 = [[-1, -1], [-1, -1], [-1, -1], [-1, -1], [8, 29], [8, 29], [8, 30], [8, 30], [8, 45], [8, 45], [8, 46], [8, 46], [7, 22], [7, 22], [7, 22], [7, 22], [7, 23], [7, 23], [7, 23], [7, 23], [8, 47], [8, 47], [8, 48], [8, 48], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [7, 20], [7, 20], [7, 20], [7, 20], [8, 33], [8, 33], [8, 34], [8, 34], [8, 35], [8, 35], [8, 36], [8, 36], [8, 37], [8, 37], [8, 38], [8, 38], [7, 19], [7, 19], [7, 19], [7, 19], [8, 31], [8, 31], [8, 32], [8, 32], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [8, 53], [8, 53], [8, 54], [8, 54], [7, 26], [7, 26], [7, 26], [7, 26], [8, 39], [8, 39], [8, 40], [8, 40], [8, 41], [8, 41], [8, 42], [8, 42], [8, 43], [8, 43], [8, 44], [8, 44], [7, 21], [7, 21], [7, 21], [7, 21], [7, 28], [7, 28], [7, 28], [7, 28], [8, 61], [8, 61], [8, 62], [8, 62], [8, 63], [8, 63], [8, 0], [8, 0], [8, 320], [8, 320], [8, 384], [8, 384], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [7, 27], [7, 27], [7, 27], [7, 27], [8, 59], [8, 59], [8, 60], [8, 60], [9, 1472], [9, 1536], [9, 1600], [9, 1728], [7, 18], [7, 18], [7, 18], [7, 18], [7, 24], [7, 24], [7, 24], [7, 24], [8, 49], [8, 49], [8, 50], [8, 50], [8, 51], [8, 51], [8, 52], [8, 52], [7, 25], [7, 25], [7, 25], [7, 25], [8, 55], [8, 55], [8, 56], [8, 56], [8, 57], [8, 57], [8, 58], [8, 58], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [8, 448], [8, 448], [8, 512], [8, 512], [9, 704], [9, 768], [8, 640], [8, 640], [8, 576], [8, 576], [9, 832], [9, 896], [9, 960], [9, 1024], [9, 1088], [9, 1152], [9, 1216], [9, 1280], [9, 1344], [9, 1408], [7, 256], [7, 256], [7, 256], [7, 256], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7]];
  1063. var blackTable1 = [[-1, -1], [-1, -1], [12, ccittEOL], [12, ccittEOL], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [11, 1792], [11, 1792], [11, 1792], [11, 1792], [12, 1984], [12, 1984], [12, 2048], [12, 2048], [12, 2112], [12, 2112], [12, 2176], [12, 2176], [12, 2240], [12, 2240], [12, 2304], [12, 2304], [11, 1856], [11, 1856], [11, 1856], [11, 1856], [11, 1920], [11, 1920], [11, 1920], [11, 1920], [12, 2368], [12, 2368], [12, 2432], [12, 2432], [12, 2496], [12, 2496], [12, 2560], [12, 2560], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [12, 52], [12, 52], [13, 640], [13, 704], [13, 768], [13, 832], [12, 55], [12, 55], [12, 56], [12, 56], [13, 1280], [13, 1344], [13, 1408], [13, 1472], [12, 59], [12, 59], [12, 60], [12, 60], [13, 1536], [13, 1600], [11, 24], [11, 24], [11, 24], [11, 24], [11, 25], [11, 25], [11, 25], [11, 25], [13, 1664], [13, 1728], [12, 320], [12, 320], [12, 384], [12, 384], [12, 448], [12, 448], [13, 512], [13, 576], [12, 53], [12, 53], [12, 54], [12, 54], [13, 896], [13, 960], [13, 1024], [13, 1088], [13, 1152], [13, 1216], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64]];
  1064. var blackTable2 = [[8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [11, 23], [11, 23], [12, 50], [12, 51], [12, 44], [12, 45], [12, 46], [12, 47], [12, 57], [12, 58], [12, 61], [12, 256], [10, 16], [10, 16], [10, 16], [10, 16], [10, 17], [10, 17], [10, 17], [10, 17], [12, 48], [12, 49], [12, 62], [12, 63], [12, 30], [12, 31], [12, 32], [12, 33], [12, 40], [12, 41], [11, 22], [11, 22], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [12, 128], [12, 192], [12, 26], [12, 27], [12, 28], [12, 29], [11, 19], [11, 19], [11, 20], [11, 20], [12, 34], [12, 35], [12, 36], [12, 37], [12, 38], [12, 39], [11, 21], [11, 21], [12, 42], [12, 43], [10, 0], [10, 0], [10, 0], [10, 0], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12]];
  1065. var blackTable3 = [[-1, -1], [-1, -1], [-1, -1], [-1, -1], [6, 9], [6, 8], [5, 7], [5, 7], [4, 6], [4, 6], [4, 6], [4, 6], [4, 5], [4, 5], [4, 5], [4, 5], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2]];
  1066. function CCITTFaxStream(str, maybeLength, params) {
  1067. this.str = str;
  1068. this.dict = str.dict;
  1069. if (!(0, _primitives.isDict)(params)) {
  1070. params = _primitives.Dict.empty;
  1071. }
  1072. this.encoding = params.get('K') || 0;
  1073. this.eoline = params.get('EndOfLine') || false;
  1074. this.byteAlign = params.get('EncodedByteAlign') || false;
  1075. this.columns = params.get('Columns') || 1728;
  1076. this.rows = params.get('Rows') || 0;
  1077. var eoblock = params.get('EndOfBlock');
  1078. if (eoblock === null || eoblock === undefined) {
  1079. eoblock = true;
  1080. }
  1081. this.eoblock = eoblock;
  1082. this.black = params.get('BlackIs1') || false;
  1083. this.codingLine = new Uint32Array(this.columns + 1);
  1084. this.refLine = new Uint32Array(this.columns + 2);
  1085. this.codingLine[0] = this.columns;
  1086. this.codingPos = 0;
  1087. this.row = 0;
  1088. this.nextLine2D = this.encoding < 0;
  1089. this.inputBits = 0;
  1090. this.inputBuf = 0;
  1091. this.outputBits = 0;
  1092. this.rowsDone = false;
  1093. var code1;
  1094. while ((code1 = this.lookBits(12)) === 0) {
  1095. this.eatBits(1);
  1096. }
  1097. if (code1 === 1) {
  1098. this.eatBits(12);
  1099. }
  1100. if (this.encoding > 0) {
  1101. this.nextLine2D = !this.lookBits(1);
  1102. this.eatBits(1);
  1103. }
  1104. DecodeStream.call(this, maybeLength);
  1105. }
  1106. CCITTFaxStream.prototype = Object.create(DecodeStream.prototype);
  1107. CCITTFaxStream.prototype.readBlock = function CCITTFaxStream_readBlock() {
  1108. while (!this.eof) {
  1109. var c = this.lookChar();
  1110. this.ensureBuffer(this.bufferLength + 1);
  1111. this.buffer[this.bufferLength++] = c;
  1112. }
  1113. };
  1114. CCITTFaxStream.prototype.addPixels = function ccittFaxStreamAddPixels(a1, blackPixels) {
  1115. var codingLine = this.codingLine;
  1116. var codingPos = this.codingPos;
  1117. if (a1 > codingLine[codingPos]) {
  1118. if (a1 > this.columns) {
  1119. (0, _util.info)('row is wrong length');
  1120. this.err = true;
  1121. a1 = this.columns;
  1122. }
  1123. if (codingPos & 1 ^ blackPixels) {
  1124. ++codingPos;
  1125. }
  1126. codingLine[codingPos] = a1;
  1127. }
  1128. this.codingPos = codingPos;
  1129. };
  1130. CCITTFaxStream.prototype.addPixelsNeg = function ccittFaxStreamAddPixelsNeg(a1, blackPixels) {
  1131. var codingLine = this.codingLine;
  1132. var codingPos = this.codingPos;
  1133. if (a1 > codingLine[codingPos]) {
  1134. if (a1 > this.columns) {
  1135. (0, _util.info)('row is wrong length');
  1136. this.err = true;
  1137. a1 = this.columns;
  1138. }
  1139. if (codingPos & 1 ^ blackPixels) {
  1140. ++codingPos;
  1141. }
  1142. codingLine[codingPos] = a1;
  1143. } else if (a1 < codingLine[codingPos]) {
  1144. if (a1 < 0) {
  1145. (0, _util.info)('invalid code');
  1146. this.err = true;
  1147. a1 = 0;
  1148. }
  1149. while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
  1150. --codingPos;
  1151. }
  1152. codingLine[codingPos] = a1;
  1153. }
  1154. this.codingPos = codingPos;
  1155. };
  1156. CCITTFaxStream.prototype.lookChar = function CCITTFaxStream_lookChar() {
  1157. var refLine = this.refLine;
  1158. var codingLine = this.codingLine;
  1159. var columns = this.columns;
  1160. var refPos, blackPixels, bits, i;
  1161. if (this.outputBits === 0) {
  1162. if (this.rowsDone) {
  1163. this.eof = true;
  1164. }
  1165. if (this.eof) {
  1166. return null;
  1167. }
  1168. this.err = false;
  1169. var code1, code2, code3;
  1170. if (this.nextLine2D) {
  1171. for (i = 0; codingLine[i] < columns; ++i) {
  1172. refLine[i] = codingLine[i];
  1173. }
  1174. refLine[i++] = columns;
  1175. refLine[i] = columns;
  1176. codingLine[0] = 0;
  1177. this.codingPos = 0;
  1178. refPos = 0;
  1179. blackPixels = 0;
  1180. while (codingLine[this.codingPos] < columns) {
  1181. code1 = this.getTwoDimCode();
  1182. switch (code1) {
  1183. case twoDimPass:
  1184. this.addPixels(refLine[refPos + 1], blackPixels);
  1185. if (refLine[refPos + 1] < columns) {
  1186. refPos += 2;
  1187. }
  1188. break;
  1189. case twoDimHoriz:
  1190. code1 = code2 = 0;
  1191. if (blackPixels) {
  1192. do {
  1193. code1 += code3 = this.getBlackCode();
  1194. } while (code3 >= 64);
  1195. do {
  1196. code2 += code3 = this.getWhiteCode();
  1197. } while (code3 >= 64);
  1198. } else {
  1199. do {
  1200. code1 += code3 = this.getWhiteCode();
  1201. } while (code3 >= 64);
  1202. do {
  1203. code2 += code3 = this.getBlackCode();
  1204. } while (code3 >= 64);
  1205. }
  1206. this.addPixels(codingLine[this.codingPos] + code1, blackPixels);
  1207. if (codingLine[this.codingPos] < columns) {
  1208. this.addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
  1209. }
  1210. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1211. refPos += 2;
  1212. }
  1213. break;
  1214. case twoDimVertR3:
  1215. this.addPixels(refLine[refPos] + 3, blackPixels);
  1216. blackPixels ^= 1;
  1217. if (codingLine[this.codingPos] < columns) {
  1218. ++refPos;
  1219. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1220. refPos += 2;
  1221. }
  1222. }
  1223. break;
  1224. case twoDimVertR2:
  1225. this.addPixels(refLine[refPos] + 2, blackPixels);
  1226. blackPixels ^= 1;
  1227. if (codingLine[this.codingPos] < columns) {
  1228. ++refPos;
  1229. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1230. refPos += 2;
  1231. }
  1232. }
  1233. break;
  1234. case twoDimVertR1:
  1235. this.addPixels(refLine[refPos] + 1, blackPixels);
  1236. blackPixels ^= 1;
  1237. if (codingLine[this.codingPos] < columns) {
  1238. ++refPos;
  1239. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1240. refPos += 2;
  1241. }
  1242. }
  1243. break;
  1244. case twoDimVert0:
  1245. this.addPixels(refLine[refPos], blackPixels);
  1246. blackPixels ^= 1;
  1247. if (codingLine[this.codingPos] < columns) {
  1248. ++refPos;
  1249. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1250. refPos += 2;
  1251. }
  1252. }
  1253. break;
  1254. case twoDimVertL3:
  1255. this.addPixelsNeg(refLine[refPos] - 3, blackPixels);
  1256. blackPixels ^= 1;
  1257. if (codingLine[this.codingPos] < columns) {
  1258. if (refPos > 0) {
  1259. --refPos;
  1260. } else {
  1261. ++refPos;
  1262. }
  1263. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1264. refPos += 2;
  1265. }
  1266. }
  1267. break;
  1268. case twoDimVertL2:
  1269. this.addPixelsNeg(refLine[refPos] - 2, blackPixels);
  1270. blackPixels ^= 1;
  1271. if (codingLine[this.codingPos] < columns) {
  1272. if (refPos > 0) {
  1273. --refPos;
  1274. } else {
  1275. ++refPos;
  1276. }
  1277. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1278. refPos += 2;
  1279. }
  1280. }
  1281. break;
  1282. case twoDimVertL1:
  1283. this.addPixelsNeg(refLine[refPos] - 1, blackPixels);
  1284. blackPixels ^= 1;
  1285. if (codingLine[this.codingPos] < columns) {
  1286. if (refPos > 0) {
  1287. --refPos;
  1288. } else {
  1289. ++refPos;
  1290. }
  1291. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1292. refPos += 2;
  1293. }
  1294. }
  1295. break;
  1296. case ccittEOF:
  1297. this.addPixels(columns, 0);
  1298. this.eof = true;
  1299. break;
  1300. default:
  1301. (0, _util.info)('bad 2d code');
  1302. this.addPixels(columns, 0);
  1303. this.err = true;
  1304. }
  1305. }
  1306. } else {
  1307. codingLine[0] = 0;
  1308. this.codingPos = 0;
  1309. blackPixels = 0;
  1310. while (codingLine[this.codingPos] < columns) {
  1311. code1 = 0;
  1312. if (blackPixels) {
  1313. do {
  1314. code1 += code3 = this.getBlackCode();
  1315. } while (code3 >= 64);
  1316. } else {
  1317. do {
  1318. code1 += code3 = this.getWhiteCode();
  1319. } while (code3 >= 64);
  1320. }
  1321. this.addPixels(codingLine[this.codingPos] + code1, blackPixels);
  1322. blackPixels ^= 1;
  1323. }
  1324. }
  1325. var gotEOL = false;
  1326. if (this.byteAlign) {
  1327. this.inputBits &= ~7;
  1328. }
  1329. if (!this.eoblock && this.row === this.rows - 1) {
  1330. this.rowsDone = true;
  1331. } else {
  1332. code1 = this.lookBits(12);
  1333. if (this.eoline) {
  1334. while (code1 !== ccittEOF && code1 !== 1) {
  1335. this.eatBits(1);
  1336. code1 = this.lookBits(12);
  1337. }
  1338. } else {
  1339. while (code1 === 0) {
  1340. this.eatBits(1);
  1341. code1 = this.lookBits(12);
  1342. }
  1343. }
  1344. if (code1 === 1) {
  1345. this.eatBits(12);
  1346. gotEOL = true;
  1347. } else if (code1 === ccittEOF) {
  1348. this.eof = true;
  1349. }
  1350. }
  1351. if (!this.eof && this.encoding > 0 && !this.rowsDone) {
  1352. this.nextLine2D = !this.lookBits(1);
  1353. this.eatBits(1);
  1354. }
  1355. if (this.eoblock && gotEOL && this.byteAlign) {
  1356. code1 = this.lookBits(12);
  1357. if (code1 === 1) {
  1358. this.eatBits(12);
  1359. if (this.encoding > 0) {
  1360. this.lookBits(1);
  1361. this.eatBits(1);
  1362. }
  1363. if (this.encoding >= 0) {
  1364. for (i = 0; i < 4; ++i) {
  1365. code1 = this.lookBits(12);
  1366. if (code1 !== 1) {
  1367. (0, _util.info)('bad rtc code: ' + code1);
  1368. }
  1369. this.eatBits(12);
  1370. if (this.encoding > 0) {
  1371. this.lookBits(1);
  1372. this.eatBits(1);
  1373. }
  1374. }
  1375. }
  1376. this.eof = true;
  1377. }
  1378. } else if (this.err && this.eoline) {
  1379. while (true) {
  1380. code1 = this.lookBits(13);
  1381. if (code1 === ccittEOF) {
  1382. this.eof = true;
  1383. return null;
  1384. }
  1385. if (code1 >> 1 === 1) {
  1386. break;
  1387. }
  1388. this.eatBits(1);
  1389. }
  1390. this.eatBits(12);
  1391. if (this.encoding > 0) {
  1392. this.eatBits(1);
  1393. this.nextLine2D = !(code1 & 1);
  1394. }
  1395. }
  1396. if (codingLine[0] > 0) {
  1397. this.outputBits = codingLine[this.codingPos = 0];
  1398. } else {
  1399. this.outputBits = codingLine[this.codingPos = 1];
  1400. }
  1401. this.row++;
  1402. }
  1403. var c;
  1404. if (this.outputBits >= 8) {
  1405. c = this.codingPos & 1 ? 0 : 0xFF;
  1406. this.outputBits -= 8;
  1407. if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
  1408. this.codingPos++;
  1409. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  1410. }
  1411. } else {
  1412. bits = 8;
  1413. c = 0;
  1414. do {
  1415. if (this.outputBits > bits) {
  1416. c <<= bits;
  1417. if (!(this.codingPos & 1)) {
  1418. c |= 0xFF >> 8 - bits;
  1419. }
  1420. this.outputBits -= bits;
  1421. bits = 0;
  1422. } else {
  1423. c <<= this.outputBits;
  1424. if (!(this.codingPos & 1)) {
  1425. c |= 0xFF >> 8 - this.outputBits;
  1426. }
  1427. bits -= this.outputBits;
  1428. this.outputBits = 0;
  1429. if (codingLine[this.codingPos] < columns) {
  1430. this.codingPos++;
  1431. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  1432. } else if (bits > 0) {
  1433. c <<= bits;
  1434. bits = 0;
  1435. }
  1436. }
  1437. } while (bits);
  1438. }
  1439. if (this.black) {
  1440. c ^= 0xFF;
  1441. }
  1442. return c;
  1443. };
  1444. CCITTFaxStream.prototype.findTableCode = function ccittFaxStreamFindTableCode(start, end, table, limit) {
  1445. var limitValue = limit || 0;
  1446. for (var i = start; i <= end; ++i) {
  1447. var code = this.lookBits(i);
  1448. if (code === ccittEOF) {
  1449. return [true, 1, false];
  1450. }
  1451. if (i < end) {
  1452. code <<= end - i;
  1453. }
  1454. if (!limitValue || code >= limitValue) {
  1455. var p = table[code - limitValue];
  1456. if (p[0] === i) {
  1457. this.eatBits(i);
  1458. return [true, p[1], true];
  1459. }
  1460. }
  1461. }
  1462. return [false, 0, false];
  1463. };
  1464. CCITTFaxStream.prototype.getTwoDimCode = function ccittFaxStreamGetTwoDimCode() {
  1465. var code = 0;
  1466. var p;
  1467. if (this.eoblock) {
  1468. code = this.lookBits(7);
  1469. p = twoDimTable[code];
  1470. if (p && p[0] > 0) {
  1471. this.eatBits(p[0]);
  1472. return p[1];
  1473. }
  1474. } else {
  1475. var result = this.findTableCode(1, 7, twoDimTable);
  1476. if (result[0] && result[2]) {
  1477. return result[1];
  1478. }
  1479. }
  1480. (0, _util.info)('Bad two dim code');
  1481. return ccittEOF;
  1482. };
  1483. CCITTFaxStream.prototype.getWhiteCode = function ccittFaxStreamGetWhiteCode() {
  1484. var code = 0;
  1485. var p;
  1486. if (this.eoblock) {
  1487. code = this.lookBits(12);
  1488. if (code === ccittEOF) {
  1489. return 1;
  1490. }
  1491. if (code >> 5 === 0) {
  1492. p = whiteTable1[code];
  1493. } else {
  1494. p = whiteTable2[code >> 3];
  1495. }
  1496. if (p[0] > 0) {
  1497. this.eatBits(p[0]);
  1498. return p[1];
  1499. }
  1500. } else {
  1501. var result = this.findTableCode(1, 9, whiteTable2);
  1502. if (result[0]) {
  1503. return result[1];
  1504. }
  1505. result = this.findTableCode(11, 12, whiteTable1);
  1506. if (result[0]) {
  1507. return result[1];
  1508. }
  1509. }
  1510. (0, _util.info)('bad white code');
  1511. this.eatBits(1);
  1512. return 1;
  1513. };
  1514. CCITTFaxStream.prototype.getBlackCode = function ccittFaxStreamGetBlackCode() {
  1515. var code, p;
  1516. if (this.eoblock) {
  1517. code = this.lookBits(13);
  1518. if (code === ccittEOF) {
  1519. return 1;
  1520. }
  1521. if (code >> 7 === 0) {
  1522. p = blackTable1[code];
  1523. } else if (code >> 9 === 0 && code >> 7 !== 0) {
  1524. p = blackTable2[(code >> 1) - 64];
  1525. } else {
  1526. p = blackTable3[code >> 7];
  1527. }
  1528. if (p[0] > 0) {
  1529. this.eatBits(p[0]);
  1530. return p[1];
  1531. }
  1532. } else {
  1533. var result = this.findTableCode(2, 6, blackTable3);
  1534. if (result[0]) {
  1535. return result[1];
  1536. }
  1537. result = this.findTableCode(7, 12, blackTable2, 64);
  1538. if (result[0]) {
  1539. return result[1];
  1540. }
  1541. result = this.findTableCode(10, 13, blackTable1);
  1542. if (result[0]) {
  1543. return result[1];
  1544. }
  1545. }
  1546. (0, _util.info)('bad black code');
  1547. this.eatBits(1);
  1548. return 1;
  1549. };
  1550. CCITTFaxStream.prototype.lookBits = function CCITTFaxStream_lookBits(n) {
  1551. var c;
  1552. while (this.inputBits < n) {
  1553. if ((c = this.str.getByte()) === -1) {
  1554. if (this.inputBits === 0) {
  1555. return ccittEOF;
  1556. }
  1557. return this.inputBuf << n - this.inputBits & 0xFFFF >> 16 - n;
  1558. }
  1559. this.inputBuf = this.inputBuf << 8 | c;
  1560. this.inputBits += 8;
  1561. }
  1562. return this.inputBuf >> this.inputBits - n & 0xFFFF >> 16 - n;
  1563. };
  1564. CCITTFaxStream.prototype.eatBits = function CCITTFaxStream_eatBits(n) {
  1565. if ((this.inputBits -= n) < 0) {
  1566. this.inputBits = 0;
  1567. }
  1568. };
  1569. return CCITTFaxStream;
  1570. }();
  1571. var LZWStream = function LZWStreamClosure() {
  1572. function LZWStream(str, maybeLength, earlyChange) {
  1573. this.str = str;
  1574. this.dict = str.dict;
  1575. this.cachedData = 0;
  1576. this.bitsCached = 0;
  1577. var maxLzwDictionarySize = 4096;
  1578. var lzwState = {
  1579. earlyChange: earlyChange,
  1580. codeLength: 9,
  1581. nextCode: 258,
  1582. dictionaryValues: new Uint8Array(maxLzwDictionarySize),
  1583. dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
  1584. dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
  1585. currentSequence: new Uint8Array(maxLzwDictionarySize),
  1586. currentSequenceLength: 0
  1587. };
  1588. for (var i = 0; i < 256; ++i) {
  1589. lzwState.dictionaryValues[i] = i;
  1590. lzwState.dictionaryLengths[i] = 1;
  1591. }
  1592. this.lzwState = lzwState;
  1593. DecodeStream.call(this, maybeLength);
  1594. }
  1595. LZWStream.prototype = Object.create(DecodeStream.prototype);
  1596. LZWStream.prototype.readBits = function LZWStream_readBits(n) {
  1597. var bitsCached = this.bitsCached;
  1598. var cachedData = this.cachedData;
  1599. while (bitsCached < n) {
  1600. var c = this.str.getByte();
  1601. if (c === -1) {
  1602. this.eof = true;
  1603. return null;
  1604. }
  1605. cachedData = cachedData << 8 | c;
  1606. bitsCached += 8;
  1607. }
  1608. this.bitsCached = bitsCached -= n;
  1609. this.cachedData = cachedData;
  1610. this.lastCode = null;
  1611. return cachedData >>> bitsCached & (1 << n) - 1;
  1612. };
  1613. LZWStream.prototype.readBlock = function LZWStream_readBlock() {
  1614. var blockSize = 512;
  1615. var estimatedDecodedSize = blockSize * 2,
  1616. decodedSizeDelta = blockSize;
  1617. var i, j, q;
  1618. var lzwState = this.lzwState;
  1619. if (!lzwState) {
  1620. return;
  1621. }
  1622. var earlyChange = lzwState.earlyChange;
  1623. var nextCode = lzwState.nextCode;
  1624. var dictionaryValues = lzwState.dictionaryValues;
  1625. var dictionaryLengths = lzwState.dictionaryLengths;
  1626. var dictionaryPrevCodes = lzwState.dictionaryPrevCodes;
  1627. var codeLength = lzwState.codeLength;
  1628. var prevCode = lzwState.prevCode;
  1629. var currentSequence = lzwState.currentSequence;
  1630. var currentSequenceLength = lzwState.currentSequenceLength;
  1631. var decodedLength = 0;
  1632. var currentBufferLength = this.bufferLength;
  1633. var buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1634. for (i = 0; i < blockSize; i++) {
  1635. var code = this.readBits(codeLength);
  1636. var hasPrev = currentSequenceLength > 0;
  1637. if (code < 256) {
  1638. currentSequence[0] = code;
  1639. currentSequenceLength = 1;
  1640. } else if (code >= 258) {
  1641. if (code < nextCode) {
  1642. currentSequenceLength = dictionaryLengths[code];
  1643. for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
  1644. currentSequence[j] = dictionaryValues[q];
  1645. q = dictionaryPrevCodes[q];
  1646. }
  1647. } else {
  1648. currentSequence[currentSequenceLength++] = currentSequence[0];
  1649. }
  1650. } else if (code === 256) {
  1651. codeLength = 9;
  1652. nextCode = 258;
  1653. currentSequenceLength = 0;
  1654. continue;
  1655. } else {
  1656. this.eof = true;
  1657. delete this.lzwState;
  1658. break;
  1659. }
  1660. if (hasPrev) {
  1661. dictionaryPrevCodes[nextCode] = prevCode;
  1662. dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
  1663. dictionaryValues[nextCode] = currentSequence[0];
  1664. nextCode++;
  1665. codeLength = nextCode + earlyChange & nextCode + earlyChange - 1 ? codeLength : Math.min(Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1, 12) | 0;
  1666. }
  1667. prevCode = code;
  1668. decodedLength += currentSequenceLength;
  1669. if (estimatedDecodedSize < decodedLength) {
  1670. do {
  1671. estimatedDecodedSize += decodedSizeDelta;
  1672. } while (estimatedDecodedSize < decodedLength);
  1673. buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1674. }
  1675. for (j = 0; j < currentSequenceLength; j++) {
  1676. buffer[currentBufferLength++] = currentSequence[j];
  1677. }
  1678. }
  1679. lzwState.nextCode = nextCode;
  1680. lzwState.codeLength = codeLength;
  1681. lzwState.prevCode = prevCode;
  1682. lzwState.currentSequenceLength = currentSequenceLength;
  1683. this.bufferLength = currentBufferLength;
  1684. };
  1685. return LZWStream;
  1686. }();
  1687. var NullStream = function NullStreamClosure() {
  1688. function NullStream() {
  1689. Stream.call(this, new Uint8Array(0));
  1690. }
  1691. NullStream.prototype = Stream.prototype;
  1692. return NullStream;
  1693. }();
  1694. exports.Ascii85Stream = Ascii85Stream;
  1695. exports.AsciiHexStream = AsciiHexStream;
  1696. exports.CCITTFaxStream = CCITTFaxStream;
  1697. exports.DecryptStream = DecryptStream;
  1698. exports.DecodeStream = DecodeStream;
  1699. exports.FlateStream = FlateStream;
  1700. exports.Jbig2Stream = Jbig2Stream;
  1701. exports.JpegStream = JpegStream;
  1702. exports.JpxStream = JpxStream;
  1703. exports.NullStream = NullStream;
  1704. exports.PredictorStream = PredictorStream;
  1705. exports.RunLengthStream = RunLengthStream;
  1706. exports.Stream = Stream;
  1707. exports.StreamsSequenceStream = StreamsSequenceStream;
  1708. exports.StringStream = StringStream;
  1709. exports.LZWStream = LZWStream;