stream.js 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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 {
  564. var compArray = new Uint8Array(colors + 1);
  565. var bitMask = (1 << bits) - 1;
  566. var j = 0,
  567. k = bufferLength;
  568. var columns = this.columns;
  569. for (i = 0; i < columns; ++i) {
  570. for (var kk = 0; kk < colors; ++kk) {
  571. if (inbits < bits) {
  572. inbuf = inbuf << 8 | rawBytes[j++] & 0xFF;
  573. inbits += 8;
  574. }
  575. compArray[kk] = compArray[kk] + (inbuf >> inbits - bits) & bitMask;
  576. inbits -= bits;
  577. outbuf = outbuf << bits | compArray[kk];
  578. outbits += bits;
  579. if (outbits >= 8) {
  580. buffer[k++] = outbuf >> outbits - 8 & 0xFF;
  581. outbits -= 8;
  582. }
  583. }
  584. }
  585. if (outbits > 0) {
  586. buffer[k++] = (outbuf << 8 - outbits) + (inbuf & (1 << 8 - outbits) - 1);
  587. }
  588. }
  589. this.bufferLength += rowBytes;
  590. };
  591. PredictorStream.prototype.readBlockPng = function predictorStreamReadBlockPng() {
  592. var rowBytes = this.rowBytes;
  593. var pixBytes = this.pixBytes;
  594. var predictor = this.str.getByte();
  595. var rawBytes = this.str.getBytes(rowBytes);
  596. this.eof = !rawBytes.length;
  597. if (this.eof) {
  598. return;
  599. }
  600. var bufferLength = this.bufferLength;
  601. var buffer = this.ensureBuffer(bufferLength + rowBytes);
  602. var prevRow = buffer.subarray(bufferLength - rowBytes, bufferLength);
  603. if (prevRow.length === 0) {
  604. prevRow = new Uint8Array(rowBytes);
  605. }
  606. var i,
  607. j = bufferLength,
  608. up,
  609. c;
  610. switch (predictor) {
  611. case 0:
  612. for (i = 0; i < rowBytes; ++i) {
  613. buffer[j++] = rawBytes[i];
  614. }
  615. break;
  616. case 1:
  617. for (i = 0; i < pixBytes; ++i) {
  618. buffer[j++] = rawBytes[i];
  619. }
  620. for (; i < rowBytes; ++i) {
  621. buffer[j] = buffer[j - pixBytes] + rawBytes[i] & 0xFF;
  622. j++;
  623. }
  624. break;
  625. case 2:
  626. for (i = 0; i < rowBytes; ++i) {
  627. buffer[j++] = prevRow[i] + rawBytes[i] & 0xFF;
  628. }
  629. break;
  630. case 3:
  631. for (i = 0; i < pixBytes; ++i) {
  632. buffer[j++] = (prevRow[i] >> 1) + rawBytes[i];
  633. }
  634. for (; i < rowBytes; ++i) {
  635. buffer[j] = (prevRow[i] + buffer[j - pixBytes] >> 1) + rawBytes[i] & 0xFF;
  636. j++;
  637. }
  638. break;
  639. case 4:
  640. for (i = 0; i < pixBytes; ++i) {
  641. up = prevRow[i];
  642. c = rawBytes[i];
  643. buffer[j++] = up + c;
  644. }
  645. for (; i < rowBytes; ++i) {
  646. up = prevRow[i];
  647. var upLeft = prevRow[i - pixBytes];
  648. var left = buffer[j - pixBytes];
  649. var p = left + up - upLeft;
  650. var pa = p - left;
  651. if (pa < 0) {
  652. pa = -pa;
  653. }
  654. var pb = p - up;
  655. if (pb < 0) {
  656. pb = -pb;
  657. }
  658. var pc = p - upLeft;
  659. if (pc < 0) {
  660. pc = -pc;
  661. }
  662. c = rawBytes[i];
  663. if (pa <= pb && pa <= pc) {
  664. buffer[j++] = left + c;
  665. } else if (pb <= pc) {
  666. buffer[j++] = up + c;
  667. } else {
  668. buffer[j++] = upLeft + c;
  669. }
  670. }
  671. break;
  672. default:
  673. throw new _util.FormatError('Unsupported predictor: ' + predictor);
  674. }
  675. this.bufferLength += rowBytes;
  676. };
  677. return PredictorStream;
  678. }();
  679. var JpegStream = function JpegStreamClosure() {
  680. function JpegStream(stream, maybeLength, dict, params) {
  681. var ch;
  682. while ((ch = stream.getByte()) !== -1) {
  683. if (ch === 0xFF) {
  684. stream.skip(-1);
  685. break;
  686. }
  687. }
  688. this.stream = stream;
  689. this.maybeLength = maybeLength;
  690. this.dict = dict;
  691. this.params = params;
  692. DecodeStream.call(this, maybeLength);
  693. }
  694. JpegStream.prototype = Object.create(DecodeStream.prototype);
  695. Object.defineProperty(JpegStream.prototype, 'bytes', {
  696. get: function JpegStream_bytes() {
  697. return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
  698. },
  699. configurable: true
  700. });
  701. JpegStream.prototype.ensureBuffer = function JpegStream_ensureBuffer(req) {
  702. if (this.bufferLength) {
  703. return;
  704. }
  705. var jpegImage = new _jpg.JpegImage();
  706. var decodeArr = this.dict.getArray('Decode', 'D');
  707. if (this.forceRGB && Array.isArray(decodeArr)) {
  708. var bitsPerComponent = this.dict.get('BitsPerComponent') || 8;
  709. var decodeArrLength = decodeArr.length;
  710. var transform = new Int32Array(decodeArrLength);
  711. var transformNeeded = false;
  712. var maxValue = (1 << bitsPerComponent) - 1;
  713. for (var i = 0; i < decodeArrLength; i += 2) {
  714. transform[i] = (decodeArr[i + 1] - decodeArr[i]) * 256 | 0;
  715. transform[i + 1] = decodeArr[i] * maxValue | 0;
  716. if (transform[i] !== 256 || transform[i + 1] !== 0) {
  717. transformNeeded = true;
  718. }
  719. }
  720. if (transformNeeded) {
  721. jpegImage.decodeTransform = transform;
  722. }
  723. }
  724. if ((0, _primitives.isDict)(this.params)) {
  725. var colorTransform = this.params.get('ColorTransform');
  726. if (Number.isInteger(colorTransform)) {
  727. jpegImage.colorTransform = colorTransform;
  728. }
  729. }
  730. jpegImage.parse(this.bytes);
  731. var data = jpegImage.getData(this.drawWidth, this.drawHeight, this.forceRGB);
  732. this.buffer = data;
  733. this.bufferLength = data.length;
  734. this.eof = true;
  735. };
  736. JpegStream.prototype.getBytes = function JpegStream_getBytes(length) {
  737. this.ensureBuffer();
  738. return this.buffer;
  739. };
  740. JpegStream.prototype.getIR = function JpegStream_getIR(forceDataSchema) {
  741. return (0, _util.createObjectURL)(this.bytes, 'image/jpeg', forceDataSchema);
  742. };
  743. return JpegStream;
  744. }();
  745. var JpxStream = function JpxStreamClosure() {
  746. function JpxStream(stream, maybeLength, dict, params) {
  747. this.stream = stream;
  748. this.maybeLength = maybeLength;
  749. this.dict = dict;
  750. this.params = params;
  751. DecodeStream.call(this, maybeLength);
  752. }
  753. JpxStream.prototype = Object.create(DecodeStream.prototype);
  754. Object.defineProperty(JpxStream.prototype, 'bytes', {
  755. get: function JpxStream_bytes() {
  756. return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
  757. },
  758. configurable: true
  759. });
  760. JpxStream.prototype.ensureBuffer = function JpxStream_ensureBuffer(req) {
  761. if (this.bufferLength) {
  762. return;
  763. }
  764. var jpxImage = new _jpx.JpxImage();
  765. jpxImage.parse(this.bytes);
  766. var width = jpxImage.width;
  767. var height = jpxImage.height;
  768. var componentsCount = jpxImage.componentsCount;
  769. var tileCount = jpxImage.tiles.length;
  770. if (tileCount === 1) {
  771. this.buffer = jpxImage.tiles[0].items;
  772. } else {
  773. var data = new Uint8ClampedArray(width * height * componentsCount);
  774. for (var k = 0; k < tileCount; k++) {
  775. var tileComponents = jpxImage.tiles[k];
  776. var tileWidth = tileComponents.width;
  777. var tileHeight = tileComponents.height;
  778. var tileLeft = tileComponents.left;
  779. var tileTop = tileComponents.top;
  780. var src = tileComponents.items;
  781. var srcPosition = 0;
  782. var dataPosition = (width * tileTop + tileLeft) * componentsCount;
  783. var imgRowSize = width * componentsCount;
  784. var tileRowSize = tileWidth * componentsCount;
  785. for (var j = 0; j < tileHeight; j++) {
  786. var rowBytes = src.subarray(srcPosition, srcPosition + tileRowSize);
  787. data.set(rowBytes, dataPosition);
  788. srcPosition += tileRowSize;
  789. dataPosition += imgRowSize;
  790. }
  791. }
  792. this.buffer = data;
  793. }
  794. this.bufferLength = this.buffer.length;
  795. this.eof = true;
  796. };
  797. return JpxStream;
  798. }();
  799. var Jbig2Stream = function Jbig2StreamClosure() {
  800. function Jbig2Stream(stream, maybeLength, dict, params) {
  801. this.stream = stream;
  802. this.maybeLength = maybeLength;
  803. this.dict = dict;
  804. this.params = params;
  805. DecodeStream.call(this, maybeLength);
  806. }
  807. Jbig2Stream.prototype = Object.create(DecodeStream.prototype);
  808. Object.defineProperty(Jbig2Stream.prototype, 'bytes', {
  809. get: function Jbig2Stream_bytes() {
  810. return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
  811. },
  812. configurable: true
  813. });
  814. Jbig2Stream.prototype.ensureBuffer = function Jbig2Stream_ensureBuffer(req) {
  815. if (this.bufferLength) {
  816. return;
  817. }
  818. var jbig2Image = new _jbig.Jbig2Image();
  819. var chunks = [];
  820. if ((0, _primitives.isDict)(this.params)) {
  821. var globalsStream = this.params.get('JBIG2Globals');
  822. if ((0, _primitives.isStream)(globalsStream)) {
  823. var globals = globalsStream.getBytes();
  824. chunks.push({
  825. data: globals,
  826. start: 0,
  827. end: globals.length
  828. });
  829. }
  830. }
  831. chunks.push({
  832. data: this.bytes,
  833. start: 0,
  834. end: this.bytes.length
  835. });
  836. var data = jbig2Image.parseChunks(chunks);
  837. var dataLength = data.length;
  838. for (var i = 0; i < dataLength; i++) {
  839. data[i] ^= 0xFF;
  840. }
  841. this.buffer = data;
  842. this.bufferLength = dataLength;
  843. this.eof = true;
  844. };
  845. return Jbig2Stream;
  846. }();
  847. var DecryptStream = function DecryptStreamClosure() {
  848. function DecryptStream(str, maybeLength, decrypt) {
  849. this.str = str;
  850. this.dict = str.dict;
  851. this.decrypt = decrypt;
  852. this.nextChunk = null;
  853. this.initialized = false;
  854. DecodeStream.call(this, maybeLength);
  855. }
  856. var chunkSize = 512;
  857. DecryptStream.prototype = Object.create(DecodeStream.prototype);
  858. DecryptStream.prototype.readBlock = function DecryptStream_readBlock() {
  859. var chunk;
  860. if (this.initialized) {
  861. chunk = this.nextChunk;
  862. } else {
  863. chunk = this.str.getBytes(chunkSize);
  864. this.initialized = true;
  865. }
  866. if (!chunk || chunk.length === 0) {
  867. this.eof = true;
  868. return;
  869. }
  870. this.nextChunk = this.str.getBytes(chunkSize);
  871. var hasMoreData = this.nextChunk && this.nextChunk.length > 0;
  872. var decrypt = this.decrypt;
  873. chunk = decrypt(chunk, !hasMoreData);
  874. var bufferLength = this.bufferLength;
  875. var i,
  876. n = chunk.length;
  877. var buffer = this.ensureBuffer(bufferLength + n);
  878. for (i = 0; i < n; i++) {
  879. buffer[bufferLength++] = chunk[i];
  880. }
  881. this.bufferLength = bufferLength;
  882. };
  883. return DecryptStream;
  884. }();
  885. var Ascii85Stream = function Ascii85StreamClosure() {
  886. function Ascii85Stream(str, maybeLength) {
  887. this.str = str;
  888. this.dict = str.dict;
  889. this.input = new Uint8Array(5);
  890. if (maybeLength) {
  891. maybeLength = 0.8 * maybeLength;
  892. }
  893. DecodeStream.call(this, maybeLength);
  894. }
  895. Ascii85Stream.prototype = Object.create(DecodeStream.prototype);
  896. Ascii85Stream.prototype.readBlock = function Ascii85Stream_readBlock() {
  897. var TILDA_CHAR = 0x7E;
  898. var Z_LOWER_CHAR = 0x7A;
  899. var EOF = -1;
  900. var str = this.str;
  901. var c = str.getByte();
  902. while ((0, _util.isSpace)(c)) {
  903. c = str.getByte();
  904. }
  905. if (c === EOF || c === TILDA_CHAR) {
  906. this.eof = true;
  907. return;
  908. }
  909. var bufferLength = this.bufferLength,
  910. buffer;
  911. var i;
  912. if (c === Z_LOWER_CHAR) {
  913. buffer = this.ensureBuffer(bufferLength + 4);
  914. for (i = 0; i < 4; ++i) {
  915. buffer[bufferLength + i] = 0;
  916. }
  917. this.bufferLength += 4;
  918. } else {
  919. var input = this.input;
  920. input[0] = c;
  921. for (i = 1; i < 5; ++i) {
  922. c = str.getByte();
  923. while ((0, _util.isSpace)(c)) {
  924. c = str.getByte();
  925. }
  926. input[i] = c;
  927. if (c === EOF || c === TILDA_CHAR) {
  928. break;
  929. }
  930. }
  931. buffer = this.ensureBuffer(bufferLength + i - 1);
  932. this.bufferLength += i - 1;
  933. if (i < 5) {
  934. for (; i < 5; ++i) {
  935. input[i] = 0x21 + 84;
  936. }
  937. this.eof = true;
  938. }
  939. var t = 0;
  940. for (i = 0; i < 5; ++i) {
  941. t = t * 85 + (input[i] - 0x21);
  942. }
  943. for (i = 3; i >= 0; --i) {
  944. buffer[bufferLength + i] = t & 0xFF;
  945. t >>= 8;
  946. }
  947. }
  948. };
  949. return Ascii85Stream;
  950. }();
  951. var AsciiHexStream = function AsciiHexStreamClosure() {
  952. function AsciiHexStream(str, maybeLength) {
  953. this.str = str;
  954. this.dict = str.dict;
  955. this.firstDigit = -1;
  956. if (maybeLength) {
  957. maybeLength = 0.5 * maybeLength;
  958. }
  959. DecodeStream.call(this, maybeLength);
  960. }
  961. AsciiHexStream.prototype = Object.create(DecodeStream.prototype);
  962. AsciiHexStream.prototype.readBlock = function AsciiHexStream_readBlock() {
  963. var UPSTREAM_BLOCK_SIZE = 8000;
  964. var bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE);
  965. if (!bytes.length) {
  966. this.eof = true;
  967. return;
  968. }
  969. var maxDecodeLength = bytes.length + 1 >> 1;
  970. var buffer = this.ensureBuffer(this.bufferLength + maxDecodeLength);
  971. var bufferLength = this.bufferLength;
  972. var firstDigit = this.firstDigit;
  973. for (var i = 0, ii = bytes.length; i < ii; i++) {
  974. var ch = bytes[i],
  975. digit;
  976. if (ch >= 0x30 && ch <= 0x39) {
  977. digit = ch & 0x0F;
  978. } else if (ch >= 0x41 && ch <= 0x46 || ch >= 0x61 && ch <= 0x66) {
  979. digit = (ch & 0x0F) + 9;
  980. } else if (ch === 0x3E) {
  981. this.eof = true;
  982. break;
  983. } else {
  984. continue;
  985. }
  986. if (firstDigit < 0) {
  987. firstDigit = digit;
  988. } else {
  989. buffer[bufferLength++] = firstDigit << 4 | digit;
  990. firstDigit = -1;
  991. }
  992. }
  993. if (firstDigit >= 0 && this.eof) {
  994. buffer[bufferLength++] = firstDigit << 4;
  995. firstDigit = -1;
  996. }
  997. this.firstDigit = firstDigit;
  998. this.bufferLength = bufferLength;
  999. };
  1000. return AsciiHexStream;
  1001. }();
  1002. var RunLengthStream = function RunLengthStreamClosure() {
  1003. function RunLengthStream(str, maybeLength) {
  1004. this.str = str;
  1005. this.dict = str.dict;
  1006. DecodeStream.call(this, maybeLength);
  1007. }
  1008. RunLengthStream.prototype = Object.create(DecodeStream.prototype);
  1009. RunLengthStream.prototype.readBlock = function RunLengthStream_readBlock() {
  1010. var repeatHeader = this.str.getBytes(2);
  1011. if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) {
  1012. this.eof = true;
  1013. return;
  1014. }
  1015. var buffer;
  1016. var bufferLength = this.bufferLength;
  1017. var n = repeatHeader[0];
  1018. if (n < 128) {
  1019. buffer = this.ensureBuffer(bufferLength + n + 1);
  1020. buffer[bufferLength++] = repeatHeader[1];
  1021. if (n > 0) {
  1022. var source = this.str.getBytes(n);
  1023. buffer.set(source, bufferLength);
  1024. bufferLength += n;
  1025. }
  1026. } else {
  1027. n = 257 - n;
  1028. var b = repeatHeader[1];
  1029. buffer = this.ensureBuffer(bufferLength + n + 1);
  1030. for (var i = 0; i < n; i++) {
  1031. buffer[bufferLength++] = b;
  1032. }
  1033. }
  1034. this.bufferLength = bufferLength;
  1035. };
  1036. return RunLengthStream;
  1037. }();
  1038. var CCITTFaxStream = function CCITTFaxStreamClosure() {
  1039. var ccittEOL = -2;
  1040. var ccittEOF = -1;
  1041. var twoDimPass = 0;
  1042. var twoDimHoriz = 1;
  1043. var twoDimVert0 = 2;
  1044. var twoDimVertR1 = 3;
  1045. var twoDimVertL1 = 4;
  1046. var twoDimVertR2 = 5;
  1047. var twoDimVertL2 = 6;
  1048. var twoDimVertR3 = 7;
  1049. var twoDimVertL3 = 8;
  1050. 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]];
  1051. 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]];
  1052. 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]];
  1053. 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]];
  1054. 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]];
  1055. 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]];
  1056. function CCITTFaxStream(str, maybeLength, params) {
  1057. this.str = str;
  1058. this.dict = str.dict;
  1059. if (!(0, _primitives.isDict)(params)) {
  1060. params = _primitives.Dict.empty;
  1061. }
  1062. this.encoding = params.get('K') || 0;
  1063. this.eoline = params.get('EndOfLine') || false;
  1064. this.byteAlign = params.get('EncodedByteAlign') || false;
  1065. this.columns = params.get('Columns') || 1728;
  1066. this.rows = params.get('Rows') || 0;
  1067. var eoblock = params.get('EndOfBlock');
  1068. if (eoblock === null || eoblock === undefined) {
  1069. eoblock = true;
  1070. }
  1071. this.eoblock = eoblock;
  1072. this.black = params.get('BlackIs1') || false;
  1073. this.codingLine = new Uint32Array(this.columns + 1);
  1074. this.refLine = new Uint32Array(this.columns + 2);
  1075. this.codingLine[0] = this.columns;
  1076. this.codingPos = 0;
  1077. this.row = 0;
  1078. this.nextLine2D = this.encoding < 0;
  1079. this.inputBits = 0;
  1080. this.inputBuf = 0;
  1081. this.outputBits = 0;
  1082. var code1;
  1083. while ((code1 = this.lookBits(12)) === 0) {
  1084. this.eatBits(1);
  1085. }
  1086. if (code1 === 1) {
  1087. this.eatBits(12);
  1088. }
  1089. if (this.encoding > 0) {
  1090. this.nextLine2D = !this.lookBits(1);
  1091. this.eatBits(1);
  1092. }
  1093. DecodeStream.call(this, maybeLength);
  1094. }
  1095. CCITTFaxStream.prototype = Object.create(DecodeStream.prototype);
  1096. CCITTFaxStream.prototype.readBlock = function CCITTFaxStream_readBlock() {
  1097. while (!this.eof) {
  1098. var c = this.lookChar();
  1099. this.ensureBuffer(this.bufferLength + 1);
  1100. this.buffer[this.bufferLength++] = c;
  1101. }
  1102. };
  1103. CCITTFaxStream.prototype.addPixels = function ccittFaxStreamAddPixels(a1, blackPixels) {
  1104. var codingLine = this.codingLine;
  1105. var codingPos = this.codingPos;
  1106. if (a1 > codingLine[codingPos]) {
  1107. if (a1 > this.columns) {
  1108. (0, _util.info)('row is wrong length');
  1109. this.err = true;
  1110. a1 = this.columns;
  1111. }
  1112. if (codingPos & 1 ^ blackPixels) {
  1113. ++codingPos;
  1114. }
  1115. codingLine[codingPos] = a1;
  1116. }
  1117. this.codingPos = codingPos;
  1118. };
  1119. CCITTFaxStream.prototype.addPixelsNeg = function ccittFaxStreamAddPixelsNeg(a1, blackPixels) {
  1120. var codingLine = this.codingLine;
  1121. var codingPos = this.codingPos;
  1122. if (a1 > codingLine[codingPos]) {
  1123. if (a1 > this.columns) {
  1124. (0, _util.info)('row is wrong length');
  1125. this.err = true;
  1126. a1 = this.columns;
  1127. }
  1128. if (codingPos & 1 ^ blackPixels) {
  1129. ++codingPos;
  1130. }
  1131. codingLine[codingPos] = a1;
  1132. } else if (a1 < codingLine[codingPos]) {
  1133. if (a1 < 0) {
  1134. (0, _util.info)('invalid code');
  1135. this.err = true;
  1136. a1 = 0;
  1137. }
  1138. while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
  1139. --codingPos;
  1140. }
  1141. codingLine[codingPos] = a1;
  1142. }
  1143. this.codingPos = codingPos;
  1144. };
  1145. CCITTFaxStream.prototype.lookChar = function CCITTFaxStream_lookChar() {
  1146. var refLine = this.refLine;
  1147. var codingLine = this.codingLine;
  1148. var columns = this.columns;
  1149. var refPos, blackPixels, bits, i;
  1150. if (this.outputBits === 0) {
  1151. if (this.eof) {
  1152. return null;
  1153. }
  1154. this.err = false;
  1155. var code1, code2, code3;
  1156. if (this.nextLine2D) {
  1157. for (i = 0; codingLine[i] < columns; ++i) {
  1158. refLine[i] = codingLine[i];
  1159. }
  1160. refLine[i++] = columns;
  1161. refLine[i] = columns;
  1162. codingLine[0] = 0;
  1163. this.codingPos = 0;
  1164. refPos = 0;
  1165. blackPixels = 0;
  1166. while (codingLine[this.codingPos] < columns) {
  1167. code1 = this.getTwoDimCode();
  1168. switch (code1) {
  1169. case twoDimPass:
  1170. this.addPixels(refLine[refPos + 1], blackPixels);
  1171. if (refLine[refPos + 1] < columns) {
  1172. refPos += 2;
  1173. }
  1174. break;
  1175. case twoDimHoriz:
  1176. code1 = code2 = 0;
  1177. if (blackPixels) {
  1178. do {
  1179. code1 += code3 = this.getBlackCode();
  1180. } while (code3 >= 64);
  1181. do {
  1182. code2 += code3 = this.getWhiteCode();
  1183. } while (code3 >= 64);
  1184. } else {
  1185. do {
  1186. code1 += code3 = this.getWhiteCode();
  1187. } while (code3 >= 64);
  1188. do {
  1189. code2 += code3 = this.getBlackCode();
  1190. } while (code3 >= 64);
  1191. }
  1192. this.addPixels(codingLine[this.codingPos] + code1, blackPixels);
  1193. if (codingLine[this.codingPos] < columns) {
  1194. this.addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
  1195. }
  1196. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1197. refPos += 2;
  1198. }
  1199. break;
  1200. case twoDimVertR3:
  1201. this.addPixels(refLine[refPos] + 3, blackPixels);
  1202. blackPixels ^= 1;
  1203. if (codingLine[this.codingPos] < columns) {
  1204. ++refPos;
  1205. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1206. refPos += 2;
  1207. }
  1208. }
  1209. break;
  1210. case twoDimVertR2:
  1211. this.addPixels(refLine[refPos] + 2, blackPixels);
  1212. blackPixels ^= 1;
  1213. if (codingLine[this.codingPos] < columns) {
  1214. ++refPos;
  1215. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1216. refPos += 2;
  1217. }
  1218. }
  1219. break;
  1220. case twoDimVertR1:
  1221. this.addPixels(refLine[refPos] + 1, blackPixels);
  1222. blackPixels ^= 1;
  1223. if (codingLine[this.codingPos] < columns) {
  1224. ++refPos;
  1225. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1226. refPos += 2;
  1227. }
  1228. }
  1229. break;
  1230. case twoDimVert0:
  1231. this.addPixels(refLine[refPos], blackPixels);
  1232. blackPixels ^= 1;
  1233. if (codingLine[this.codingPos] < columns) {
  1234. ++refPos;
  1235. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1236. refPos += 2;
  1237. }
  1238. }
  1239. break;
  1240. case twoDimVertL3:
  1241. this.addPixelsNeg(refLine[refPos] - 3, blackPixels);
  1242. blackPixels ^= 1;
  1243. if (codingLine[this.codingPos] < columns) {
  1244. if (refPos > 0) {
  1245. --refPos;
  1246. } else {
  1247. ++refPos;
  1248. }
  1249. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1250. refPos += 2;
  1251. }
  1252. }
  1253. break;
  1254. case twoDimVertL2:
  1255. this.addPixelsNeg(refLine[refPos] - 2, 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 twoDimVertL1:
  1269. this.addPixelsNeg(refLine[refPos] - 1, 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 ccittEOF:
  1283. this.addPixels(columns, 0);
  1284. this.eof = true;
  1285. break;
  1286. default:
  1287. (0, _util.info)('bad 2d code');
  1288. this.addPixels(columns, 0);
  1289. this.err = true;
  1290. }
  1291. }
  1292. } else {
  1293. codingLine[0] = 0;
  1294. this.codingPos = 0;
  1295. blackPixels = 0;
  1296. while (codingLine[this.codingPos] < columns) {
  1297. code1 = 0;
  1298. if (blackPixels) {
  1299. do {
  1300. code1 += code3 = this.getBlackCode();
  1301. } while (code3 >= 64);
  1302. } else {
  1303. do {
  1304. code1 += code3 = this.getWhiteCode();
  1305. } while (code3 >= 64);
  1306. }
  1307. this.addPixels(codingLine[this.codingPos] + code1, blackPixels);
  1308. blackPixels ^= 1;
  1309. }
  1310. }
  1311. var gotEOL = false;
  1312. if (this.byteAlign) {
  1313. this.inputBits &= ~7;
  1314. }
  1315. if (!this.eoblock && this.row === this.rows - 1) {
  1316. this.eof = true;
  1317. } else {
  1318. code1 = this.lookBits(12);
  1319. if (this.eoline) {
  1320. while (code1 !== ccittEOF && code1 !== 1) {
  1321. this.eatBits(1);
  1322. code1 = this.lookBits(12);
  1323. }
  1324. } else {
  1325. while (code1 === 0) {
  1326. this.eatBits(1);
  1327. code1 = this.lookBits(12);
  1328. }
  1329. }
  1330. if (code1 === 1) {
  1331. this.eatBits(12);
  1332. gotEOL = true;
  1333. } else if (code1 === ccittEOF) {
  1334. this.eof = true;
  1335. }
  1336. }
  1337. if (!this.eof && this.encoding > 0) {
  1338. this.nextLine2D = !this.lookBits(1);
  1339. this.eatBits(1);
  1340. }
  1341. if (this.eoblock && gotEOL && this.byteAlign) {
  1342. code1 = this.lookBits(12);
  1343. if (code1 === 1) {
  1344. this.eatBits(12);
  1345. if (this.encoding > 0) {
  1346. this.lookBits(1);
  1347. this.eatBits(1);
  1348. }
  1349. if (this.encoding >= 0) {
  1350. for (i = 0; i < 4; ++i) {
  1351. code1 = this.lookBits(12);
  1352. if (code1 !== 1) {
  1353. (0, _util.info)('bad rtc code: ' + code1);
  1354. }
  1355. this.eatBits(12);
  1356. if (this.encoding > 0) {
  1357. this.lookBits(1);
  1358. this.eatBits(1);
  1359. }
  1360. }
  1361. }
  1362. this.eof = true;
  1363. }
  1364. } else if (this.err && this.eoline) {
  1365. while (true) {
  1366. code1 = this.lookBits(13);
  1367. if (code1 === ccittEOF) {
  1368. this.eof = true;
  1369. return null;
  1370. }
  1371. if (code1 >> 1 === 1) {
  1372. break;
  1373. }
  1374. this.eatBits(1);
  1375. }
  1376. this.eatBits(12);
  1377. if (this.encoding > 0) {
  1378. this.eatBits(1);
  1379. this.nextLine2D = !(code1 & 1);
  1380. }
  1381. }
  1382. if (codingLine[0] > 0) {
  1383. this.outputBits = codingLine[this.codingPos = 0];
  1384. } else {
  1385. this.outputBits = codingLine[this.codingPos = 1];
  1386. }
  1387. this.row++;
  1388. }
  1389. var c;
  1390. if (this.outputBits >= 8) {
  1391. c = this.codingPos & 1 ? 0 : 0xFF;
  1392. this.outputBits -= 8;
  1393. if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
  1394. this.codingPos++;
  1395. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  1396. }
  1397. } else {
  1398. bits = 8;
  1399. c = 0;
  1400. do {
  1401. if (this.outputBits > bits) {
  1402. c <<= bits;
  1403. if (!(this.codingPos & 1)) {
  1404. c |= 0xFF >> 8 - bits;
  1405. }
  1406. this.outputBits -= bits;
  1407. bits = 0;
  1408. } else {
  1409. c <<= this.outputBits;
  1410. if (!(this.codingPos & 1)) {
  1411. c |= 0xFF >> 8 - this.outputBits;
  1412. }
  1413. bits -= this.outputBits;
  1414. this.outputBits = 0;
  1415. if (codingLine[this.codingPos] < columns) {
  1416. this.codingPos++;
  1417. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  1418. } else if (bits > 0) {
  1419. c <<= bits;
  1420. bits = 0;
  1421. }
  1422. }
  1423. } while (bits);
  1424. }
  1425. if (this.black) {
  1426. c ^= 0xFF;
  1427. }
  1428. return c;
  1429. };
  1430. CCITTFaxStream.prototype.findTableCode = function ccittFaxStreamFindTableCode(start, end, table, limit) {
  1431. var limitValue = limit || 0;
  1432. for (var i = start; i <= end; ++i) {
  1433. var code = this.lookBits(i);
  1434. if (code === ccittEOF) {
  1435. return [true, 1, false];
  1436. }
  1437. if (i < end) {
  1438. code <<= end - i;
  1439. }
  1440. if (!limitValue || code >= limitValue) {
  1441. var p = table[code - limitValue];
  1442. if (p[0] === i) {
  1443. this.eatBits(i);
  1444. return [true, p[1], true];
  1445. }
  1446. }
  1447. }
  1448. return [false, 0, false];
  1449. };
  1450. CCITTFaxStream.prototype.getTwoDimCode = function ccittFaxStreamGetTwoDimCode() {
  1451. var code = 0;
  1452. var p;
  1453. if (this.eoblock) {
  1454. code = this.lookBits(7);
  1455. p = twoDimTable[code];
  1456. if (p && p[0] > 0) {
  1457. this.eatBits(p[0]);
  1458. return p[1];
  1459. }
  1460. } else {
  1461. var result = this.findTableCode(1, 7, twoDimTable);
  1462. if (result[0] && result[2]) {
  1463. return result[1];
  1464. }
  1465. }
  1466. (0, _util.info)('Bad two dim code');
  1467. return ccittEOF;
  1468. };
  1469. CCITTFaxStream.prototype.getWhiteCode = function ccittFaxStreamGetWhiteCode() {
  1470. var code = 0;
  1471. var p;
  1472. if (this.eoblock) {
  1473. code = this.lookBits(12);
  1474. if (code === ccittEOF) {
  1475. return 1;
  1476. }
  1477. if (code >> 5 === 0) {
  1478. p = whiteTable1[code];
  1479. } else {
  1480. p = whiteTable2[code >> 3];
  1481. }
  1482. if (p[0] > 0) {
  1483. this.eatBits(p[0]);
  1484. return p[1];
  1485. }
  1486. } else {
  1487. var result = this.findTableCode(1, 9, whiteTable2);
  1488. if (result[0]) {
  1489. return result[1];
  1490. }
  1491. result = this.findTableCode(11, 12, whiteTable1);
  1492. if (result[0]) {
  1493. return result[1];
  1494. }
  1495. }
  1496. (0, _util.info)('bad white code');
  1497. this.eatBits(1);
  1498. return 1;
  1499. };
  1500. CCITTFaxStream.prototype.getBlackCode = function ccittFaxStreamGetBlackCode() {
  1501. var code, p;
  1502. if (this.eoblock) {
  1503. code = this.lookBits(13);
  1504. if (code === ccittEOF) {
  1505. return 1;
  1506. }
  1507. if (code >> 7 === 0) {
  1508. p = blackTable1[code];
  1509. } else if (code >> 9 === 0 && code >> 7 !== 0) {
  1510. p = blackTable2[(code >> 1) - 64];
  1511. } else {
  1512. p = blackTable3[code >> 7];
  1513. }
  1514. if (p[0] > 0) {
  1515. this.eatBits(p[0]);
  1516. return p[1];
  1517. }
  1518. } else {
  1519. var result = this.findTableCode(2, 6, blackTable3);
  1520. if (result[0]) {
  1521. return result[1];
  1522. }
  1523. result = this.findTableCode(7, 12, blackTable2, 64);
  1524. if (result[0]) {
  1525. return result[1];
  1526. }
  1527. result = this.findTableCode(10, 13, blackTable1);
  1528. if (result[0]) {
  1529. return result[1];
  1530. }
  1531. }
  1532. (0, _util.info)('bad black code');
  1533. this.eatBits(1);
  1534. return 1;
  1535. };
  1536. CCITTFaxStream.prototype.lookBits = function CCITTFaxStream_lookBits(n) {
  1537. var c;
  1538. while (this.inputBits < n) {
  1539. if ((c = this.str.getByte()) === -1) {
  1540. if (this.inputBits === 0) {
  1541. return ccittEOF;
  1542. }
  1543. return this.inputBuf << n - this.inputBits & 0xFFFF >> 16 - n;
  1544. }
  1545. this.inputBuf = this.inputBuf << 8 | c;
  1546. this.inputBits += 8;
  1547. }
  1548. return this.inputBuf >> this.inputBits - n & 0xFFFF >> 16 - n;
  1549. };
  1550. CCITTFaxStream.prototype.eatBits = function CCITTFaxStream_eatBits(n) {
  1551. if ((this.inputBits -= n) < 0) {
  1552. this.inputBits = 0;
  1553. }
  1554. };
  1555. return CCITTFaxStream;
  1556. }();
  1557. var LZWStream = function LZWStreamClosure() {
  1558. function LZWStream(str, maybeLength, earlyChange) {
  1559. this.str = str;
  1560. this.dict = str.dict;
  1561. this.cachedData = 0;
  1562. this.bitsCached = 0;
  1563. var maxLzwDictionarySize = 4096;
  1564. var lzwState = {
  1565. earlyChange: earlyChange,
  1566. codeLength: 9,
  1567. nextCode: 258,
  1568. dictionaryValues: new Uint8Array(maxLzwDictionarySize),
  1569. dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
  1570. dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
  1571. currentSequence: new Uint8Array(maxLzwDictionarySize),
  1572. currentSequenceLength: 0
  1573. };
  1574. for (var i = 0; i < 256; ++i) {
  1575. lzwState.dictionaryValues[i] = i;
  1576. lzwState.dictionaryLengths[i] = 1;
  1577. }
  1578. this.lzwState = lzwState;
  1579. DecodeStream.call(this, maybeLength);
  1580. }
  1581. LZWStream.prototype = Object.create(DecodeStream.prototype);
  1582. LZWStream.prototype.readBits = function LZWStream_readBits(n) {
  1583. var bitsCached = this.bitsCached;
  1584. var cachedData = this.cachedData;
  1585. while (bitsCached < n) {
  1586. var c = this.str.getByte();
  1587. if (c === -1) {
  1588. this.eof = true;
  1589. return null;
  1590. }
  1591. cachedData = cachedData << 8 | c;
  1592. bitsCached += 8;
  1593. }
  1594. this.bitsCached = bitsCached -= n;
  1595. this.cachedData = cachedData;
  1596. this.lastCode = null;
  1597. return cachedData >>> bitsCached & (1 << n) - 1;
  1598. };
  1599. LZWStream.prototype.readBlock = function LZWStream_readBlock() {
  1600. var blockSize = 512;
  1601. var estimatedDecodedSize = blockSize * 2,
  1602. decodedSizeDelta = blockSize;
  1603. var i, j, q;
  1604. var lzwState = this.lzwState;
  1605. if (!lzwState) {
  1606. return;
  1607. }
  1608. var earlyChange = lzwState.earlyChange;
  1609. var nextCode = lzwState.nextCode;
  1610. var dictionaryValues = lzwState.dictionaryValues;
  1611. var dictionaryLengths = lzwState.dictionaryLengths;
  1612. var dictionaryPrevCodes = lzwState.dictionaryPrevCodes;
  1613. var codeLength = lzwState.codeLength;
  1614. var prevCode = lzwState.prevCode;
  1615. var currentSequence = lzwState.currentSequence;
  1616. var currentSequenceLength = lzwState.currentSequenceLength;
  1617. var decodedLength = 0;
  1618. var currentBufferLength = this.bufferLength;
  1619. var buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1620. for (i = 0; i < blockSize; i++) {
  1621. var code = this.readBits(codeLength);
  1622. var hasPrev = currentSequenceLength > 0;
  1623. if (code < 256) {
  1624. currentSequence[0] = code;
  1625. currentSequenceLength = 1;
  1626. } else if (code >= 258) {
  1627. if (code < nextCode) {
  1628. currentSequenceLength = dictionaryLengths[code];
  1629. for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
  1630. currentSequence[j] = dictionaryValues[q];
  1631. q = dictionaryPrevCodes[q];
  1632. }
  1633. } else {
  1634. currentSequence[currentSequenceLength++] = currentSequence[0];
  1635. }
  1636. } else if (code === 256) {
  1637. codeLength = 9;
  1638. nextCode = 258;
  1639. currentSequenceLength = 0;
  1640. continue;
  1641. } else {
  1642. this.eof = true;
  1643. delete this.lzwState;
  1644. break;
  1645. }
  1646. if (hasPrev) {
  1647. dictionaryPrevCodes[nextCode] = prevCode;
  1648. dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
  1649. dictionaryValues[nextCode] = currentSequence[0];
  1650. nextCode++;
  1651. codeLength = nextCode + earlyChange & nextCode + earlyChange - 1 ? codeLength : Math.min(Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1, 12) | 0;
  1652. }
  1653. prevCode = code;
  1654. decodedLength += currentSequenceLength;
  1655. if (estimatedDecodedSize < decodedLength) {
  1656. do {
  1657. estimatedDecodedSize += decodedSizeDelta;
  1658. } while (estimatedDecodedSize < decodedLength);
  1659. buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1660. }
  1661. for (j = 0; j < currentSequenceLength; j++) {
  1662. buffer[currentBufferLength++] = currentSequence[j];
  1663. }
  1664. }
  1665. lzwState.nextCode = nextCode;
  1666. lzwState.codeLength = codeLength;
  1667. lzwState.prevCode = prevCode;
  1668. lzwState.currentSequenceLength = currentSequenceLength;
  1669. this.bufferLength = currentBufferLength;
  1670. };
  1671. return LZWStream;
  1672. }();
  1673. var NullStream = function NullStreamClosure() {
  1674. function NullStream() {
  1675. Stream.call(this, new Uint8Array(0));
  1676. }
  1677. NullStream.prototype = Stream.prototype;
  1678. return NullStream;
  1679. }();
  1680. exports.Ascii85Stream = Ascii85Stream;
  1681. exports.AsciiHexStream = AsciiHexStream;
  1682. exports.CCITTFaxStream = CCITTFaxStream;
  1683. exports.DecryptStream = DecryptStream;
  1684. exports.DecodeStream = DecodeStream;
  1685. exports.FlateStream = FlateStream;
  1686. exports.Jbig2Stream = Jbig2Stream;
  1687. exports.JpegStream = JpegStream;
  1688. exports.JpxStream = JpxStream;
  1689. exports.NullStream = NullStream;
  1690. exports.PredictorStream = PredictorStream;
  1691. exports.RunLengthStream = RunLengthStream;
  1692. exports.Stream = Stream;
  1693. exports.StreamsSequenceStream = StreamsSequenceStream;
  1694. exports.StringStream = StringStream;
  1695. exports.LZWStream = LZWStream;