2
0

stream.js 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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. var sharedUtil = require('../shared/util.js');
  17. var corePrimitives = require('./primitives.js');
  18. var coreJbig2 = require('./jbig2.js');
  19. var coreJpg = require('./jpg.js');
  20. var coreJpx = require('./jpx.js');
  21. var Util = sharedUtil.Util;
  22. var error = sharedUtil.error;
  23. var info = sharedUtil.info;
  24. var isInt = sharedUtil.isInt;
  25. var isArray = sharedUtil.isArray;
  26. var createObjectURL = sharedUtil.createObjectURL;
  27. var shadow = sharedUtil.shadow;
  28. var isSpace = sharedUtil.isSpace;
  29. var Dict = corePrimitives.Dict;
  30. var isDict = corePrimitives.isDict;
  31. var isStream = corePrimitives.isStream;
  32. var Jbig2Image = coreJbig2.Jbig2Image;
  33. var JpegImage = coreJpg.JpegImage;
  34. var JpxImage = coreJpx.JpxImage;
  35. var Stream = function StreamClosure() {
  36. function Stream(arrayBuffer, start, length, dict) {
  37. this.bytes = arrayBuffer instanceof Uint8Array ? arrayBuffer : new Uint8Array(arrayBuffer);
  38. this.start = start || 0;
  39. this.pos = this.start;
  40. this.end = start + length || this.bytes.length;
  41. this.dict = dict;
  42. }
  43. Stream.prototype = {
  44. get length() {
  45. return this.end - this.start;
  46. },
  47. get isEmpty() {
  48. return this.length === 0;
  49. },
  50. getByte: function Stream_getByte() {
  51. if (this.pos >= this.end) {
  52. return -1;
  53. }
  54. return this.bytes[this.pos++];
  55. },
  56. getUint16: function Stream_getUint16() {
  57. var b0 = this.getByte();
  58. var b1 = this.getByte();
  59. if (b0 === -1 || b1 === -1) {
  60. return -1;
  61. }
  62. return (b0 << 8) + b1;
  63. },
  64. getInt32: function Stream_getInt32() {
  65. var b0 = this.getByte();
  66. var b1 = this.getByte();
  67. var b2 = this.getByte();
  68. var b3 = this.getByte();
  69. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  70. },
  71. getBytes: function Stream_getBytes(length) {
  72. var bytes = this.bytes;
  73. var pos = this.pos;
  74. var strEnd = this.end;
  75. if (!length) {
  76. return bytes.subarray(pos, strEnd);
  77. }
  78. var end = pos + length;
  79. if (end > strEnd) {
  80. end = strEnd;
  81. }
  82. this.pos = end;
  83. return bytes.subarray(pos, end);
  84. },
  85. peekByte: function Stream_peekByte() {
  86. var peekedByte = this.getByte();
  87. this.pos--;
  88. return peekedByte;
  89. },
  90. peekBytes: function Stream_peekBytes(length) {
  91. var bytes = this.getBytes(length);
  92. this.pos -= bytes.length;
  93. return bytes;
  94. },
  95. skip: function Stream_skip(n) {
  96. if (!n) {
  97. n = 1;
  98. }
  99. this.pos += n;
  100. },
  101. reset: function Stream_reset() {
  102. this.pos = this.start;
  103. },
  104. moveStart: function Stream_moveStart() {
  105. this.start = this.pos;
  106. },
  107. makeSubStream: function Stream_makeSubStream(start, length, dict) {
  108. return new Stream(this.bytes.buffer, start, length, dict);
  109. }
  110. };
  111. return Stream;
  112. }();
  113. var StringStream = function StringStreamClosure() {
  114. function StringStream(str) {
  115. var length = str.length;
  116. var bytes = new Uint8Array(length);
  117. for (var n = 0; n < length; ++n) {
  118. bytes[n] = str.charCodeAt(n);
  119. }
  120. Stream.call(this, bytes);
  121. }
  122. StringStream.prototype = Stream.prototype;
  123. return StringStream;
  124. }();
  125. var DecodeStream = function DecodeStreamClosure() {
  126. var emptyBuffer = new Uint8Array(0);
  127. function DecodeStream(maybeMinBufferLength) {
  128. this.pos = 0;
  129. this.bufferLength = 0;
  130. this.eof = false;
  131. this.buffer = emptyBuffer;
  132. this.minBufferLength = 512;
  133. if (maybeMinBufferLength) {
  134. while (this.minBufferLength < maybeMinBufferLength) {
  135. this.minBufferLength *= 2;
  136. }
  137. }
  138. }
  139. DecodeStream.prototype = {
  140. get isEmpty() {
  141. while (!this.eof && this.bufferLength === 0) {
  142. this.readBlock();
  143. }
  144. return this.bufferLength === 0;
  145. },
  146. ensureBuffer: function DecodeStream_ensureBuffer(requested) {
  147. var buffer = this.buffer;
  148. if (requested <= buffer.byteLength) {
  149. return buffer;
  150. }
  151. var size = this.minBufferLength;
  152. while (size < requested) {
  153. size *= 2;
  154. }
  155. var buffer2 = new Uint8Array(size);
  156. buffer2.set(buffer);
  157. return this.buffer = buffer2;
  158. },
  159. getByte: function DecodeStream_getByte() {
  160. var pos = this.pos;
  161. while (this.bufferLength <= pos) {
  162. if (this.eof) {
  163. return -1;
  164. }
  165. this.readBlock();
  166. }
  167. return this.buffer[this.pos++];
  168. },
  169. getUint16: function DecodeStream_getUint16() {
  170. var b0 = this.getByte();
  171. var b1 = this.getByte();
  172. if (b0 === -1 || b1 === -1) {
  173. return -1;
  174. }
  175. return (b0 << 8) + b1;
  176. },
  177. getInt32: function DecodeStream_getInt32() {
  178. var b0 = this.getByte();
  179. var b1 = this.getByte();
  180. var b2 = this.getByte();
  181. var b3 = this.getByte();
  182. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  183. },
  184. getBytes: function DecodeStream_getBytes(length) {
  185. var end,
  186. pos = this.pos;
  187. if (length) {
  188. this.ensureBuffer(pos + length);
  189. end = pos + length;
  190. while (!this.eof && this.bufferLength < end) {
  191. this.readBlock();
  192. }
  193. var bufEnd = this.bufferLength;
  194. if (end > bufEnd) {
  195. end = bufEnd;
  196. }
  197. } else {
  198. while (!this.eof) {
  199. this.readBlock();
  200. }
  201. end = this.bufferLength;
  202. }
  203. this.pos = end;
  204. return this.buffer.subarray(pos, end);
  205. },
  206. peekByte: function DecodeStream_peekByte() {
  207. var peekedByte = this.getByte();
  208. this.pos--;
  209. return peekedByte;
  210. },
  211. peekBytes: function DecodeStream_peekBytes(length) {
  212. var bytes = this.getBytes(length);
  213. this.pos -= bytes.length;
  214. return bytes;
  215. },
  216. makeSubStream: function DecodeStream_makeSubStream(start, length, dict) {
  217. var end = start + length;
  218. while (this.bufferLength <= end && !this.eof) {
  219. this.readBlock();
  220. }
  221. return new Stream(this.buffer, start, length, dict);
  222. },
  223. skip: function DecodeStream_skip(n) {
  224. if (!n) {
  225. n = 1;
  226. }
  227. this.pos += n;
  228. },
  229. reset: function DecodeStream_reset() {
  230. this.pos = 0;
  231. },
  232. getBaseStreams: function DecodeStream_getBaseStreams() {
  233. if (this.str && this.str.getBaseStreams) {
  234. return this.str.getBaseStreams();
  235. }
  236. return [];
  237. }
  238. };
  239. return DecodeStream;
  240. }();
  241. var StreamsSequenceStream = function StreamsSequenceStreamClosure() {
  242. function StreamsSequenceStream(streams) {
  243. this.streams = streams;
  244. DecodeStream.call(this, null);
  245. }
  246. StreamsSequenceStream.prototype = Object.create(DecodeStream.prototype);
  247. StreamsSequenceStream.prototype.readBlock = function streamSequenceStreamReadBlock() {
  248. var streams = this.streams;
  249. if (streams.length === 0) {
  250. this.eof = true;
  251. return;
  252. }
  253. var stream = streams.shift();
  254. var chunk = stream.getBytes();
  255. var bufferLength = this.bufferLength;
  256. var newLength = bufferLength + chunk.length;
  257. var buffer = this.ensureBuffer(newLength);
  258. buffer.set(chunk, bufferLength);
  259. this.bufferLength = newLength;
  260. };
  261. StreamsSequenceStream.prototype.getBaseStreams = function StreamsSequenceStream_getBaseStreams() {
  262. var baseStreams = [];
  263. for (var i = 0, ii = this.streams.length; i < ii; i++) {
  264. var stream = this.streams[i];
  265. if (stream.getBaseStreams) {
  266. Util.appendToArray(baseStreams, stream.getBaseStreams());
  267. }
  268. }
  269. return baseStreams;
  270. };
  271. return StreamsSequenceStream;
  272. }();
  273. var FlateStream = function FlateStreamClosure() {
  274. var codeLenCodeMap = new Int32Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
  275. 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]);
  276. 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]);
  277. 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];
  278. 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];
  279. function FlateStream(str, maybeLength) {
  280. this.str = str;
  281. this.dict = str.dict;
  282. var cmf = str.getByte();
  283. var flg = str.getByte();
  284. if (cmf === -1 || flg === -1) {
  285. error('Invalid header in flate stream: ' + cmf + ', ' + flg);
  286. }
  287. if ((cmf & 0x0f) !== 0x08) {
  288. error('Unknown compression method in flate stream: ' + cmf + ', ' + flg);
  289. }
  290. if (((cmf << 8) + flg) % 31 !== 0) {
  291. error('Bad FCHECK in flate stream: ' + cmf + ', ' + flg);
  292. }
  293. if (flg & 0x20) {
  294. error('FDICT bit set in flate stream: ' + cmf + ', ' + flg);
  295. }
  296. this.codeSize = 0;
  297. this.codeBuf = 0;
  298. DecodeStream.call(this, maybeLength);
  299. }
  300. FlateStream.prototype = Object.create(DecodeStream.prototype);
  301. FlateStream.prototype.getBits = function FlateStream_getBits(bits) {
  302. var str = this.str;
  303. var codeSize = this.codeSize;
  304. var codeBuf = this.codeBuf;
  305. var b;
  306. while (codeSize < bits) {
  307. if ((b = str.getByte()) === -1) {
  308. error('Bad encoding in flate stream');
  309. }
  310. codeBuf |= b << codeSize;
  311. codeSize += 8;
  312. }
  313. b = codeBuf & (1 << bits) - 1;
  314. this.codeBuf = codeBuf >> bits;
  315. this.codeSize = codeSize -= bits;
  316. return b;
  317. };
  318. FlateStream.prototype.getCode = function FlateStream_getCode(table) {
  319. var str = this.str;
  320. var codes = table[0];
  321. var maxLen = table[1];
  322. var codeSize = this.codeSize;
  323. var codeBuf = this.codeBuf;
  324. var b;
  325. while (codeSize < maxLen) {
  326. if ((b = str.getByte()) === -1) {
  327. break;
  328. }
  329. codeBuf |= b << codeSize;
  330. codeSize += 8;
  331. }
  332. var code = codes[codeBuf & (1 << maxLen) - 1];
  333. var codeLen = code >> 16;
  334. var codeVal = code & 0xffff;
  335. if (codeLen < 1 || codeSize < codeLen) {
  336. error('Bad encoding in flate stream');
  337. }
  338. this.codeBuf = codeBuf >> codeLen;
  339. this.codeSize = codeSize - codeLen;
  340. return codeVal;
  341. };
  342. FlateStream.prototype.generateHuffmanTable = function flateStreamGenerateHuffmanTable(lengths) {
  343. var n = lengths.length;
  344. var maxLen = 0;
  345. var i;
  346. for (i = 0; i < n; ++i) {
  347. if (lengths[i] > maxLen) {
  348. maxLen = lengths[i];
  349. }
  350. }
  351. var size = 1 << maxLen;
  352. var codes = new Int32Array(size);
  353. for (var len = 1, code = 0, skip = 2; len <= maxLen; ++len, code <<= 1, skip <<= 1) {
  354. for (var val = 0; val < n; ++val) {
  355. if (lengths[val] === len) {
  356. var code2 = 0;
  357. var t = code;
  358. for (i = 0; i < len; ++i) {
  359. code2 = code2 << 1 | t & 1;
  360. t >>= 1;
  361. }
  362. for (i = code2; i < size; i += skip) {
  363. codes[i] = len << 16 | val;
  364. }
  365. ++code;
  366. }
  367. }
  368. }
  369. return [codes, maxLen];
  370. };
  371. FlateStream.prototype.readBlock = function FlateStream_readBlock() {
  372. var buffer, len;
  373. var str = this.str;
  374. var hdr = this.getBits(3);
  375. if (hdr & 1) {
  376. this.eof = true;
  377. }
  378. hdr >>= 1;
  379. if (hdr === 0) {
  380. var b;
  381. if ((b = str.getByte()) === -1) {
  382. error('Bad block header in flate stream');
  383. }
  384. var blockLen = b;
  385. if ((b = str.getByte()) === -1) {
  386. error('Bad block header in flate stream');
  387. }
  388. blockLen |= b << 8;
  389. if ((b = str.getByte()) === -1) {
  390. error('Bad block header in flate stream');
  391. }
  392. var check = b;
  393. if ((b = str.getByte()) === -1) {
  394. error('Bad block header in flate stream');
  395. }
  396. check |= b << 8;
  397. if (check !== (~blockLen & 0xffff) && (blockLen !== 0 || check !== 0)) {
  398. error('Bad uncompressed block length in flate stream');
  399. }
  400. this.codeBuf = 0;
  401. this.codeSize = 0;
  402. var bufferLength = this.bufferLength;
  403. buffer = this.ensureBuffer(bufferLength + blockLen);
  404. var end = bufferLength + blockLen;
  405. this.bufferLength = end;
  406. if (blockLen === 0) {
  407. if (str.peekByte() === -1) {
  408. this.eof = true;
  409. }
  410. } else {
  411. for (var n = bufferLength; n < end; ++n) {
  412. if ((b = str.getByte()) === -1) {
  413. this.eof = true;
  414. break;
  415. }
  416. buffer[n] = b;
  417. }
  418. }
  419. return;
  420. }
  421. var litCodeTable;
  422. var distCodeTable;
  423. if (hdr === 1) {
  424. litCodeTable = fixedLitCodeTab;
  425. distCodeTable = fixedDistCodeTab;
  426. } else if (hdr === 2) {
  427. var numLitCodes = this.getBits(5) + 257;
  428. var numDistCodes = this.getBits(5) + 1;
  429. var numCodeLenCodes = this.getBits(4) + 4;
  430. var codeLenCodeLengths = new Uint8Array(codeLenCodeMap.length);
  431. var i;
  432. for (i = 0; i < numCodeLenCodes; ++i) {
  433. codeLenCodeLengths[codeLenCodeMap[i]] = this.getBits(3);
  434. }
  435. var codeLenCodeTab = this.generateHuffmanTable(codeLenCodeLengths);
  436. len = 0;
  437. i = 0;
  438. var codes = numLitCodes + numDistCodes;
  439. var codeLengths = new Uint8Array(codes);
  440. var bitsLength, bitsOffset, what;
  441. while (i < codes) {
  442. var code = this.getCode(codeLenCodeTab);
  443. if (code === 16) {
  444. bitsLength = 2;
  445. bitsOffset = 3;
  446. what = len;
  447. } else if (code === 17) {
  448. bitsLength = 3;
  449. bitsOffset = 3;
  450. what = len = 0;
  451. } else if (code === 18) {
  452. bitsLength = 7;
  453. bitsOffset = 11;
  454. what = len = 0;
  455. } else {
  456. codeLengths[i++] = len = code;
  457. continue;
  458. }
  459. var repeatLength = this.getBits(bitsLength) + bitsOffset;
  460. while (repeatLength-- > 0) {
  461. codeLengths[i++] = what;
  462. }
  463. }
  464. litCodeTable = this.generateHuffmanTable(codeLengths.subarray(0, numLitCodes));
  465. distCodeTable = this.generateHuffmanTable(codeLengths.subarray(numLitCodes, codes));
  466. } else {
  467. error('Unknown block type in flate stream');
  468. }
  469. buffer = this.buffer;
  470. var limit = buffer ? buffer.length : 0;
  471. var pos = this.bufferLength;
  472. while (true) {
  473. var code1 = this.getCode(litCodeTable);
  474. if (code1 < 256) {
  475. if (pos + 1 >= limit) {
  476. buffer = this.ensureBuffer(pos + 1);
  477. limit = buffer.length;
  478. }
  479. buffer[pos++] = code1;
  480. continue;
  481. }
  482. if (code1 === 256) {
  483. this.bufferLength = pos;
  484. return;
  485. }
  486. code1 -= 257;
  487. code1 = lengthDecode[code1];
  488. var code2 = code1 >> 16;
  489. if (code2 > 0) {
  490. code2 = this.getBits(code2);
  491. }
  492. len = (code1 & 0xffff) + code2;
  493. code1 = this.getCode(distCodeTable);
  494. code1 = distDecode[code1];
  495. code2 = code1 >> 16;
  496. if (code2 > 0) {
  497. code2 = this.getBits(code2);
  498. }
  499. var dist = (code1 & 0xffff) + code2;
  500. if (pos + len >= limit) {
  501. buffer = this.ensureBuffer(pos + len);
  502. limit = buffer.length;
  503. }
  504. for (var k = 0; k < len; ++k, ++pos) {
  505. buffer[pos] = buffer[pos - dist];
  506. }
  507. }
  508. };
  509. return FlateStream;
  510. }();
  511. var PredictorStream = function PredictorStreamClosure() {
  512. function PredictorStream(str, maybeLength, params) {
  513. if (!isDict(params)) {
  514. return str;
  515. }
  516. var predictor = this.predictor = params.get('Predictor') || 1;
  517. if (predictor <= 1) {
  518. return str;
  519. }
  520. if (predictor !== 2 && (predictor < 10 || predictor > 15)) {
  521. error('Unsupported predictor: ' + predictor);
  522. }
  523. if (predictor === 2) {
  524. this.readBlock = this.readBlockTiff;
  525. } else {
  526. this.readBlock = this.readBlockPng;
  527. }
  528. this.str = str;
  529. this.dict = str.dict;
  530. var colors = this.colors = params.get('Colors') || 1;
  531. var bits = this.bits = params.get('BitsPerComponent') || 8;
  532. var columns = this.columns = params.get('Columns') || 1;
  533. this.pixBytes = colors * bits + 7 >> 3;
  534. this.rowBytes = columns * colors * bits + 7 >> 3;
  535. DecodeStream.call(this, maybeLength);
  536. return this;
  537. }
  538. PredictorStream.prototype = Object.create(DecodeStream.prototype);
  539. PredictorStream.prototype.readBlockTiff = function predictorStreamReadBlockTiff() {
  540. var rowBytes = this.rowBytes;
  541. var bufferLength = this.bufferLength;
  542. var buffer = this.ensureBuffer(bufferLength + rowBytes);
  543. var bits = this.bits;
  544. var colors = this.colors;
  545. var rawBytes = this.str.getBytes(rowBytes);
  546. this.eof = !rawBytes.length;
  547. if (this.eof) {
  548. return;
  549. }
  550. var inbuf = 0,
  551. outbuf = 0;
  552. var inbits = 0,
  553. outbits = 0;
  554. var pos = bufferLength;
  555. var i;
  556. if (bits === 1 && colors === 1) {
  557. for (i = 0; i < rowBytes; ++i) {
  558. var c = rawBytes[i] ^ inbuf;
  559. c ^= c >> 1;
  560. c ^= c >> 2;
  561. c ^= c >> 4;
  562. inbuf = (c & 1) << 7;
  563. buffer[pos++] = c;
  564. }
  565. } else if (bits === 8) {
  566. for (i = 0; i < colors; ++i) {
  567. buffer[pos++] = rawBytes[i];
  568. }
  569. for (; i < rowBytes; ++i) {
  570. buffer[pos] = buffer[pos - colors] + rawBytes[i];
  571. pos++;
  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. error('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 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 JpegImage();
  716. var decodeArr = this.dict.getArray('Decode', 'D');
  717. if (this.forceRGB && 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 (isDict(this.params)) {
  735. var colorTransform = this.params.get('ColorTransform');
  736. if (isInt(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 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 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 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 Uint8Array(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 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 Jbig2Image();
  829. var chunks = [];
  830. if (isDict(this.params)) {
  831. var globalsStream = this.params.get('JBIG2Globals');
  832. if (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 (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 (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. params = params || Dict.empty;
  1070. this.encoding = params.get('K') || 0;
  1071. this.eoline = params.get('EndOfLine') || false;
  1072. this.byteAlign = params.get('EncodedByteAlign') || false;
  1073. this.columns = params.get('Columns') || 1728;
  1074. this.rows = params.get('Rows') || 0;
  1075. var eoblock = params.get('EndOfBlock');
  1076. if (eoblock === null || eoblock === undefined) {
  1077. eoblock = true;
  1078. }
  1079. this.eoblock = eoblock;
  1080. this.black = params.get('BlackIs1') || false;
  1081. this.codingLine = new Uint32Array(this.columns + 1);
  1082. this.refLine = new Uint32Array(this.columns + 2);
  1083. this.codingLine[0] = this.columns;
  1084. this.codingPos = 0;
  1085. this.row = 0;
  1086. this.nextLine2D = this.encoding < 0;
  1087. this.inputBits = 0;
  1088. this.inputBuf = 0;
  1089. this.outputBits = 0;
  1090. var code1;
  1091. while ((code1 = this.lookBits(12)) === 0) {
  1092. this.eatBits(1);
  1093. }
  1094. if (code1 === 1) {
  1095. this.eatBits(12);
  1096. }
  1097. if (this.encoding > 0) {
  1098. this.nextLine2D = !this.lookBits(1);
  1099. this.eatBits(1);
  1100. }
  1101. DecodeStream.call(this, maybeLength);
  1102. }
  1103. CCITTFaxStream.prototype = Object.create(DecodeStream.prototype);
  1104. CCITTFaxStream.prototype.readBlock = function CCITTFaxStream_readBlock() {
  1105. while (!this.eof) {
  1106. var c = this.lookChar();
  1107. this.ensureBuffer(this.bufferLength + 1);
  1108. this.buffer[this.bufferLength++] = c;
  1109. }
  1110. };
  1111. CCITTFaxStream.prototype.addPixels = function ccittFaxStreamAddPixels(a1, blackPixels) {
  1112. var codingLine = this.codingLine;
  1113. var codingPos = this.codingPos;
  1114. if (a1 > codingLine[codingPos]) {
  1115. if (a1 > this.columns) {
  1116. info('row is wrong length');
  1117. this.err = true;
  1118. a1 = this.columns;
  1119. }
  1120. if (codingPos & 1 ^ blackPixels) {
  1121. ++codingPos;
  1122. }
  1123. codingLine[codingPos] = a1;
  1124. }
  1125. this.codingPos = codingPos;
  1126. };
  1127. CCITTFaxStream.prototype.addPixelsNeg = function ccittFaxStreamAddPixelsNeg(a1, blackPixels) {
  1128. var codingLine = this.codingLine;
  1129. var codingPos = this.codingPos;
  1130. if (a1 > codingLine[codingPos]) {
  1131. if (a1 > this.columns) {
  1132. info('row is wrong length');
  1133. this.err = true;
  1134. a1 = this.columns;
  1135. }
  1136. if (codingPos & 1 ^ blackPixels) {
  1137. ++codingPos;
  1138. }
  1139. codingLine[codingPos] = a1;
  1140. } else if (a1 < codingLine[codingPos]) {
  1141. if (a1 < 0) {
  1142. info('invalid code');
  1143. this.err = true;
  1144. a1 = 0;
  1145. }
  1146. while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
  1147. --codingPos;
  1148. }
  1149. codingLine[codingPos] = a1;
  1150. }
  1151. this.codingPos = codingPos;
  1152. };
  1153. CCITTFaxStream.prototype.lookChar = function CCITTFaxStream_lookChar() {
  1154. var refLine = this.refLine;
  1155. var codingLine = this.codingLine;
  1156. var columns = this.columns;
  1157. var refPos, blackPixels, bits, i;
  1158. if (this.outputBits === 0) {
  1159. if (this.eof) {
  1160. return null;
  1161. }
  1162. this.err = false;
  1163. var code1, code2, code3;
  1164. if (this.nextLine2D) {
  1165. for (i = 0; codingLine[i] < columns; ++i) {
  1166. refLine[i] = codingLine[i];
  1167. }
  1168. refLine[i++] = columns;
  1169. refLine[i] = columns;
  1170. codingLine[0] = 0;
  1171. this.codingPos = 0;
  1172. refPos = 0;
  1173. blackPixels = 0;
  1174. while (codingLine[this.codingPos] < columns) {
  1175. code1 = this.getTwoDimCode();
  1176. switch (code1) {
  1177. case twoDimPass:
  1178. this.addPixels(refLine[refPos + 1], blackPixels);
  1179. if (refLine[refPos + 1] < columns) {
  1180. refPos += 2;
  1181. }
  1182. break;
  1183. case twoDimHoriz:
  1184. code1 = code2 = 0;
  1185. if (blackPixels) {
  1186. do {
  1187. code1 += code3 = this.getBlackCode();
  1188. } while (code3 >= 64);
  1189. do {
  1190. code2 += code3 = this.getWhiteCode();
  1191. } while (code3 >= 64);
  1192. } else {
  1193. do {
  1194. code1 += code3 = this.getWhiteCode();
  1195. } while (code3 >= 64);
  1196. do {
  1197. code2 += code3 = this.getBlackCode();
  1198. } while (code3 >= 64);
  1199. }
  1200. this.addPixels(codingLine[this.codingPos] + code1, blackPixels);
  1201. if (codingLine[this.codingPos] < columns) {
  1202. this.addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
  1203. }
  1204. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1205. refPos += 2;
  1206. }
  1207. break;
  1208. case twoDimVertR3:
  1209. this.addPixels(refLine[refPos] + 3, blackPixels);
  1210. blackPixels ^= 1;
  1211. if (codingLine[this.codingPos] < columns) {
  1212. ++refPos;
  1213. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1214. refPos += 2;
  1215. }
  1216. }
  1217. break;
  1218. case twoDimVertR2:
  1219. this.addPixels(refLine[refPos] + 2, blackPixels);
  1220. blackPixels ^= 1;
  1221. if (codingLine[this.codingPos] < columns) {
  1222. ++refPos;
  1223. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1224. refPos += 2;
  1225. }
  1226. }
  1227. break;
  1228. case twoDimVertR1:
  1229. this.addPixels(refLine[refPos] + 1, blackPixels);
  1230. blackPixels ^= 1;
  1231. if (codingLine[this.codingPos] < columns) {
  1232. ++refPos;
  1233. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1234. refPos += 2;
  1235. }
  1236. }
  1237. break;
  1238. case twoDimVert0:
  1239. this.addPixels(refLine[refPos], blackPixels);
  1240. blackPixels ^= 1;
  1241. if (codingLine[this.codingPos] < columns) {
  1242. ++refPos;
  1243. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1244. refPos += 2;
  1245. }
  1246. }
  1247. break;
  1248. case twoDimVertL3:
  1249. this.addPixelsNeg(refLine[refPos] - 3, blackPixels);
  1250. blackPixels ^= 1;
  1251. if (codingLine[this.codingPos] < columns) {
  1252. if (refPos > 0) {
  1253. --refPos;
  1254. } else {
  1255. ++refPos;
  1256. }
  1257. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1258. refPos += 2;
  1259. }
  1260. }
  1261. break;
  1262. case twoDimVertL2:
  1263. this.addPixelsNeg(refLine[refPos] - 2, blackPixels);
  1264. blackPixels ^= 1;
  1265. if (codingLine[this.codingPos] < columns) {
  1266. if (refPos > 0) {
  1267. --refPos;
  1268. } else {
  1269. ++refPos;
  1270. }
  1271. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1272. refPos += 2;
  1273. }
  1274. }
  1275. break;
  1276. case twoDimVertL1:
  1277. this.addPixelsNeg(refLine[refPos] - 1, blackPixels);
  1278. blackPixels ^= 1;
  1279. if (codingLine[this.codingPos] < columns) {
  1280. if (refPos > 0) {
  1281. --refPos;
  1282. } else {
  1283. ++refPos;
  1284. }
  1285. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1286. refPos += 2;
  1287. }
  1288. }
  1289. break;
  1290. case ccittEOF:
  1291. this.addPixels(columns, 0);
  1292. this.eof = true;
  1293. break;
  1294. default:
  1295. info('bad 2d code');
  1296. this.addPixels(columns, 0);
  1297. this.err = true;
  1298. }
  1299. }
  1300. } else {
  1301. codingLine[0] = 0;
  1302. this.codingPos = 0;
  1303. blackPixels = 0;
  1304. while (codingLine[this.codingPos] < columns) {
  1305. code1 = 0;
  1306. if (blackPixels) {
  1307. do {
  1308. code1 += code3 = this.getBlackCode();
  1309. } while (code3 >= 64);
  1310. } else {
  1311. do {
  1312. code1 += code3 = this.getWhiteCode();
  1313. } while (code3 >= 64);
  1314. }
  1315. this.addPixels(codingLine[this.codingPos] + code1, blackPixels);
  1316. blackPixels ^= 1;
  1317. }
  1318. }
  1319. var gotEOL = false;
  1320. if (this.byteAlign) {
  1321. this.inputBits &= ~7;
  1322. }
  1323. if (!this.eoblock && this.row === this.rows - 1) {
  1324. this.eof = true;
  1325. } else {
  1326. code1 = this.lookBits(12);
  1327. if (this.eoline) {
  1328. while (code1 !== ccittEOF && code1 !== 1) {
  1329. this.eatBits(1);
  1330. code1 = this.lookBits(12);
  1331. }
  1332. } else {
  1333. while (code1 === 0) {
  1334. this.eatBits(1);
  1335. code1 = this.lookBits(12);
  1336. }
  1337. }
  1338. if (code1 === 1) {
  1339. this.eatBits(12);
  1340. gotEOL = true;
  1341. } else if (code1 === ccittEOF) {
  1342. this.eof = true;
  1343. }
  1344. }
  1345. if (!this.eof && this.encoding > 0) {
  1346. this.nextLine2D = !this.lookBits(1);
  1347. this.eatBits(1);
  1348. }
  1349. if (this.eoblock && gotEOL && this.byteAlign) {
  1350. code1 = this.lookBits(12);
  1351. if (code1 === 1) {
  1352. this.eatBits(12);
  1353. if (this.encoding > 0) {
  1354. this.lookBits(1);
  1355. this.eatBits(1);
  1356. }
  1357. if (this.encoding >= 0) {
  1358. for (i = 0; i < 4; ++i) {
  1359. code1 = this.lookBits(12);
  1360. if (code1 !== 1) {
  1361. info('bad rtc code: ' + code1);
  1362. }
  1363. this.eatBits(12);
  1364. if (this.encoding > 0) {
  1365. this.lookBits(1);
  1366. this.eatBits(1);
  1367. }
  1368. }
  1369. }
  1370. this.eof = true;
  1371. }
  1372. } else if (this.err && this.eoline) {
  1373. while (true) {
  1374. code1 = this.lookBits(13);
  1375. if (code1 === ccittEOF) {
  1376. this.eof = true;
  1377. return null;
  1378. }
  1379. if (code1 >> 1 === 1) {
  1380. break;
  1381. }
  1382. this.eatBits(1);
  1383. }
  1384. this.eatBits(12);
  1385. if (this.encoding > 0) {
  1386. this.eatBits(1);
  1387. this.nextLine2D = !(code1 & 1);
  1388. }
  1389. }
  1390. if (codingLine[0] > 0) {
  1391. this.outputBits = codingLine[this.codingPos = 0];
  1392. } else {
  1393. this.outputBits = codingLine[this.codingPos = 1];
  1394. }
  1395. this.row++;
  1396. }
  1397. var c;
  1398. if (this.outputBits >= 8) {
  1399. c = this.codingPos & 1 ? 0 : 0xFF;
  1400. this.outputBits -= 8;
  1401. if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
  1402. this.codingPos++;
  1403. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  1404. }
  1405. } else {
  1406. bits = 8;
  1407. c = 0;
  1408. do {
  1409. if (this.outputBits > bits) {
  1410. c <<= bits;
  1411. if (!(this.codingPos & 1)) {
  1412. c |= 0xFF >> 8 - bits;
  1413. }
  1414. this.outputBits -= bits;
  1415. bits = 0;
  1416. } else {
  1417. c <<= this.outputBits;
  1418. if (!(this.codingPos & 1)) {
  1419. c |= 0xFF >> 8 - this.outputBits;
  1420. }
  1421. bits -= this.outputBits;
  1422. this.outputBits = 0;
  1423. if (codingLine[this.codingPos] < columns) {
  1424. this.codingPos++;
  1425. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  1426. } else if (bits > 0) {
  1427. c <<= bits;
  1428. bits = 0;
  1429. }
  1430. }
  1431. } while (bits);
  1432. }
  1433. if (this.black) {
  1434. c ^= 0xFF;
  1435. }
  1436. return c;
  1437. };
  1438. CCITTFaxStream.prototype.findTableCode = function ccittFaxStreamFindTableCode(start, end, table, limit) {
  1439. var limitValue = limit || 0;
  1440. for (var i = start; i <= end; ++i) {
  1441. var code = this.lookBits(i);
  1442. if (code === ccittEOF) {
  1443. return [true, 1, false];
  1444. }
  1445. if (i < end) {
  1446. code <<= end - i;
  1447. }
  1448. if (!limitValue || code >= limitValue) {
  1449. var p = table[code - limitValue];
  1450. if (p[0] === i) {
  1451. this.eatBits(i);
  1452. return [true, p[1], true];
  1453. }
  1454. }
  1455. }
  1456. return [false, 0, false];
  1457. };
  1458. CCITTFaxStream.prototype.getTwoDimCode = function ccittFaxStreamGetTwoDimCode() {
  1459. var code = 0;
  1460. var p;
  1461. if (this.eoblock) {
  1462. code = this.lookBits(7);
  1463. p = twoDimTable[code];
  1464. if (p && p[0] > 0) {
  1465. this.eatBits(p[0]);
  1466. return p[1];
  1467. }
  1468. } else {
  1469. var result = this.findTableCode(1, 7, twoDimTable);
  1470. if (result[0] && result[2]) {
  1471. return result[1];
  1472. }
  1473. }
  1474. info('Bad two dim code');
  1475. return ccittEOF;
  1476. };
  1477. CCITTFaxStream.prototype.getWhiteCode = function ccittFaxStreamGetWhiteCode() {
  1478. var code = 0;
  1479. var p;
  1480. if (this.eoblock) {
  1481. code = this.lookBits(12);
  1482. if (code === ccittEOF) {
  1483. return 1;
  1484. }
  1485. if (code >> 5 === 0) {
  1486. p = whiteTable1[code];
  1487. } else {
  1488. p = whiteTable2[code >> 3];
  1489. }
  1490. if (p[0] > 0) {
  1491. this.eatBits(p[0]);
  1492. return p[1];
  1493. }
  1494. } else {
  1495. var result = this.findTableCode(1, 9, whiteTable2);
  1496. if (result[0]) {
  1497. return result[1];
  1498. }
  1499. result = this.findTableCode(11, 12, whiteTable1);
  1500. if (result[0]) {
  1501. return result[1];
  1502. }
  1503. }
  1504. info('bad white code');
  1505. this.eatBits(1);
  1506. return 1;
  1507. };
  1508. CCITTFaxStream.prototype.getBlackCode = function ccittFaxStreamGetBlackCode() {
  1509. var code, p;
  1510. if (this.eoblock) {
  1511. code = this.lookBits(13);
  1512. if (code === ccittEOF) {
  1513. return 1;
  1514. }
  1515. if (code >> 7 === 0) {
  1516. p = blackTable1[code];
  1517. } else if (code >> 9 === 0 && code >> 7 !== 0) {
  1518. p = blackTable2[(code >> 1) - 64];
  1519. } else {
  1520. p = blackTable3[code >> 7];
  1521. }
  1522. if (p[0] > 0) {
  1523. this.eatBits(p[0]);
  1524. return p[1];
  1525. }
  1526. } else {
  1527. var result = this.findTableCode(2, 6, blackTable3);
  1528. if (result[0]) {
  1529. return result[1];
  1530. }
  1531. result = this.findTableCode(7, 12, blackTable2, 64);
  1532. if (result[0]) {
  1533. return result[1];
  1534. }
  1535. result = this.findTableCode(10, 13, blackTable1);
  1536. if (result[0]) {
  1537. return result[1];
  1538. }
  1539. }
  1540. info('bad black code');
  1541. this.eatBits(1);
  1542. return 1;
  1543. };
  1544. CCITTFaxStream.prototype.lookBits = function CCITTFaxStream_lookBits(n) {
  1545. var c;
  1546. while (this.inputBits < n) {
  1547. if ((c = this.str.getByte()) === -1) {
  1548. if (this.inputBits === 0) {
  1549. return ccittEOF;
  1550. }
  1551. return this.inputBuf << n - this.inputBits & 0xFFFF >> 16 - n;
  1552. }
  1553. this.inputBuf = this.inputBuf << 8 | c;
  1554. this.inputBits += 8;
  1555. }
  1556. return this.inputBuf >> this.inputBits - n & 0xFFFF >> 16 - n;
  1557. };
  1558. CCITTFaxStream.prototype.eatBits = function CCITTFaxStream_eatBits(n) {
  1559. if ((this.inputBits -= n) < 0) {
  1560. this.inputBits = 0;
  1561. }
  1562. };
  1563. return CCITTFaxStream;
  1564. }();
  1565. var LZWStream = function LZWStreamClosure() {
  1566. function LZWStream(str, maybeLength, earlyChange) {
  1567. this.str = str;
  1568. this.dict = str.dict;
  1569. this.cachedData = 0;
  1570. this.bitsCached = 0;
  1571. var maxLzwDictionarySize = 4096;
  1572. var lzwState = {
  1573. earlyChange: earlyChange,
  1574. codeLength: 9,
  1575. nextCode: 258,
  1576. dictionaryValues: new Uint8Array(maxLzwDictionarySize),
  1577. dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
  1578. dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
  1579. currentSequence: new Uint8Array(maxLzwDictionarySize),
  1580. currentSequenceLength: 0
  1581. };
  1582. for (var i = 0; i < 256; ++i) {
  1583. lzwState.dictionaryValues[i] = i;
  1584. lzwState.dictionaryLengths[i] = 1;
  1585. }
  1586. this.lzwState = lzwState;
  1587. DecodeStream.call(this, maybeLength);
  1588. }
  1589. LZWStream.prototype = Object.create(DecodeStream.prototype);
  1590. LZWStream.prototype.readBits = function LZWStream_readBits(n) {
  1591. var bitsCached = this.bitsCached;
  1592. var cachedData = this.cachedData;
  1593. while (bitsCached < n) {
  1594. var c = this.str.getByte();
  1595. if (c === -1) {
  1596. this.eof = true;
  1597. return null;
  1598. }
  1599. cachedData = cachedData << 8 | c;
  1600. bitsCached += 8;
  1601. }
  1602. this.bitsCached = bitsCached -= n;
  1603. this.cachedData = cachedData;
  1604. this.lastCode = null;
  1605. return cachedData >>> bitsCached & (1 << n) - 1;
  1606. };
  1607. LZWStream.prototype.readBlock = function LZWStream_readBlock() {
  1608. var blockSize = 512;
  1609. var estimatedDecodedSize = blockSize * 2,
  1610. decodedSizeDelta = blockSize;
  1611. var i, j, q;
  1612. var lzwState = this.lzwState;
  1613. if (!lzwState) {
  1614. return;
  1615. }
  1616. var earlyChange = lzwState.earlyChange;
  1617. var nextCode = lzwState.nextCode;
  1618. var dictionaryValues = lzwState.dictionaryValues;
  1619. var dictionaryLengths = lzwState.dictionaryLengths;
  1620. var dictionaryPrevCodes = lzwState.dictionaryPrevCodes;
  1621. var codeLength = lzwState.codeLength;
  1622. var prevCode = lzwState.prevCode;
  1623. var currentSequence = lzwState.currentSequence;
  1624. var currentSequenceLength = lzwState.currentSequenceLength;
  1625. var decodedLength = 0;
  1626. var currentBufferLength = this.bufferLength;
  1627. var buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1628. for (i = 0; i < blockSize; i++) {
  1629. var code = this.readBits(codeLength);
  1630. var hasPrev = currentSequenceLength > 0;
  1631. if (code < 256) {
  1632. currentSequence[0] = code;
  1633. currentSequenceLength = 1;
  1634. } else if (code >= 258) {
  1635. if (code < nextCode) {
  1636. currentSequenceLength = dictionaryLengths[code];
  1637. for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
  1638. currentSequence[j] = dictionaryValues[q];
  1639. q = dictionaryPrevCodes[q];
  1640. }
  1641. } else {
  1642. currentSequence[currentSequenceLength++] = currentSequence[0];
  1643. }
  1644. } else if (code === 256) {
  1645. codeLength = 9;
  1646. nextCode = 258;
  1647. currentSequenceLength = 0;
  1648. continue;
  1649. } else {
  1650. this.eof = true;
  1651. delete this.lzwState;
  1652. break;
  1653. }
  1654. if (hasPrev) {
  1655. dictionaryPrevCodes[nextCode] = prevCode;
  1656. dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
  1657. dictionaryValues[nextCode] = currentSequence[0];
  1658. nextCode++;
  1659. codeLength = nextCode + earlyChange & nextCode + earlyChange - 1 ? codeLength : Math.min(Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1, 12) | 0;
  1660. }
  1661. prevCode = code;
  1662. decodedLength += currentSequenceLength;
  1663. if (estimatedDecodedSize < decodedLength) {
  1664. do {
  1665. estimatedDecodedSize += decodedSizeDelta;
  1666. } while (estimatedDecodedSize < decodedLength);
  1667. buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1668. }
  1669. for (j = 0; j < currentSequenceLength; j++) {
  1670. buffer[currentBufferLength++] = currentSequence[j];
  1671. }
  1672. }
  1673. lzwState.nextCode = nextCode;
  1674. lzwState.codeLength = codeLength;
  1675. lzwState.prevCode = prevCode;
  1676. lzwState.currentSequenceLength = currentSequenceLength;
  1677. this.bufferLength = currentBufferLength;
  1678. };
  1679. return LZWStream;
  1680. }();
  1681. var NullStream = function NullStreamClosure() {
  1682. function NullStream() {
  1683. Stream.call(this, new Uint8Array(0));
  1684. }
  1685. NullStream.prototype = Stream.prototype;
  1686. return NullStream;
  1687. }();
  1688. exports.Ascii85Stream = Ascii85Stream;
  1689. exports.AsciiHexStream = AsciiHexStream;
  1690. exports.CCITTFaxStream = CCITTFaxStream;
  1691. exports.DecryptStream = DecryptStream;
  1692. exports.DecodeStream = DecodeStream;
  1693. exports.FlateStream = FlateStream;
  1694. exports.Jbig2Stream = Jbig2Stream;
  1695. exports.JpegStream = JpegStream;
  1696. exports.JpxStream = JpxStream;
  1697. exports.NullStream = NullStream;
  1698. exports.PredictorStream = PredictorStream;
  1699. exports.RunLengthStream = RunLengthStream;
  1700. exports.Stream = Stream;
  1701. exports.StreamsSequenceStream = StreamsSequenceStream;
  1702. exports.StringStream = StringStream;
  1703. exports.LZWStream = LZWStream;