stream.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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 && (0, _util.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 ((0, _util.isInt)(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. params = params || _primitives.Dict.empty;
  1060. this.encoding = params.get('K') || 0;
  1061. this.eoline = params.get('EndOfLine') || false;
  1062. this.byteAlign = params.get('EncodedByteAlign') || false;
  1063. this.columns = params.get('Columns') || 1728;
  1064. this.rows = params.get('Rows') || 0;
  1065. var eoblock = params.get('EndOfBlock');
  1066. if (eoblock === null || eoblock === undefined) {
  1067. eoblock = true;
  1068. }
  1069. this.eoblock = eoblock;
  1070. this.black = params.get('BlackIs1') || false;
  1071. this.codingLine = new Uint32Array(this.columns + 1);
  1072. this.refLine = new Uint32Array(this.columns + 2);
  1073. this.codingLine[0] = this.columns;
  1074. this.codingPos = 0;
  1075. this.row = 0;
  1076. this.nextLine2D = this.encoding < 0;
  1077. this.inputBits = 0;
  1078. this.inputBuf = 0;
  1079. this.outputBits = 0;
  1080. var code1;
  1081. while ((code1 = this.lookBits(12)) === 0) {
  1082. this.eatBits(1);
  1083. }
  1084. if (code1 === 1) {
  1085. this.eatBits(12);
  1086. }
  1087. if (this.encoding > 0) {
  1088. this.nextLine2D = !this.lookBits(1);
  1089. this.eatBits(1);
  1090. }
  1091. DecodeStream.call(this, maybeLength);
  1092. }
  1093. CCITTFaxStream.prototype = Object.create(DecodeStream.prototype);
  1094. CCITTFaxStream.prototype.readBlock = function CCITTFaxStream_readBlock() {
  1095. while (!this.eof) {
  1096. var c = this.lookChar();
  1097. this.ensureBuffer(this.bufferLength + 1);
  1098. this.buffer[this.bufferLength++] = c;
  1099. }
  1100. };
  1101. CCITTFaxStream.prototype.addPixels = function ccittFaxStreamAddPixels(a1, blackPixels) {
  1102. var codingLine = this.codingLine;
  1103. var codingPos = this.codingPos;
  1104. if (a1 > codingLine[codingPos]) {
  1105. if (a1 > this.columns) {
  1106. (0, _util.info)('row is wrong length');
  1107. this.err = true;
  1108. a1 = this.columns;
  1109. }
  1110. if (codingPos & 1 ^ blackPixels) {
  1111. ++codingPos;
  1112. }
  1113. codingLine[codingPos] = a1;
  1114. }
  1115. this.codingPos = codingPos;
  1116. };
  1117. CCITTFaxStream.prototype.addPixelsNeg = function ccittFaxStreamAddPixelsNeg(a1, blackPixels) {
  1118. var codingLine = this.codingLine;
  1119. var codingPos = this.codingPos;
  1120. if (a1 > codingLine[codingPos]) {
  1121. if (a1 > this.columns) {
  1122. (0, _util.info)('row is wrong length');
  1123. this.err = true;
  1124. a1 = this.columns;
  1125. }
  1126. if (codingPos & 1 ^ blackPixels) {
  1127. ++codingPos;
  1128. }
  1129. codingLine[codingPos] = a1;
  1130. } else if (a1 < codingLine[codingPos]) {
  1131. if (a1 < 0) {
  1132. (0, _util.info)('invalid code');
  1133. this.err = true;
  1134. a1 = 0;
  1135. }
  1136. while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
  1137. --codingPos;
  1138. }
  1139. codingLine[codingPos] = a1;
  1140. }
  1141. this.codingPos = codingPos;
  1142. };
  1143. CCITTFaxStream.prototype.lookChar = function CCITTFaxStream_lookChar() {
  1144. var refLine = this.refLine;
  1145. var codingLine = this.codingLine;
  1146. var columns = this.columns;
  1147. var refPos, blackPixels, bits, i;
  1148. if (this.outputBits === 0) {
  1149. if (this.eof) {
  1150. return null;
  1151. }
  1152. this.err = false;
  1153. var code1, code2, code3;
  1154. if (this.nextLine2D) {
  1155. for (i = 0; codingLine[i] < columns; ++i) {
  1156. refLine[i] = codingLine[i];
  1157. }
  1158. refLine[i++] = columns;
  1159. refLine[i] = columns;
  1160. codingLine[0] = 0;
  1161. this.codingPos = 0;
  1162. refPos = 0;
  1163. blackPixels = 0;
  1164. while (codingLine[this.codingPos] < columns) {
  1165. code1 = this.getTwoDimCode();
  1166. switch (code1) {
  1167. case twoDimPass:
  1168. this.addPixels(refLine[refPos + 1], blackPixels);
  1169. if (refLine[refPos + 1] < columns) {
  1170. refPos += 2;
  1171. }
  1172. break;
  1173. case twoDimHoriz:
  1174. code1 = code2 = 0;
  1175. if (blackPixels) {
  1176. do {
  1177. code1 += code3 = this.getBlackCode();
  1178. } while (code3 >= 64);
  1179. do {
  1180. code2 += code3 = this.getWhiteCode();
  1181. } while (code3 >= 64);
  1182. } else {
  1183. do {
  1184. code1 += code3 = this.getWhiteCode();
  1185. } while (code3 >= 64);
  1186. do {
  1187. code2 += code3 = this.getBlackCode();
  1188. } while (code3 >= 64);
  1189. }
  1190. this.addPixels(codingLine[this.codingPos] + code1, blackPixels);
  1191. if (codingLine[this.codingPos] < columns) {
  1192. this.addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
  1193. }
  1194. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1195. refPos += 2;
  1196. }
  1197. break;
  1198. case twoDimVertR3:
  1199. this.addPixels(refLine[refPos] + 3, blackPixels);
  1200. blackPixels ^= 1;
  1201. if (codingLine[this.codingPos] < columns) {
  1202. ++refPos;
  1203. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1204. refPos += 2;
  1205. }
  1206. }
  1207. break;
  1208. case twoDimVertR2:
  1209. this.addPixels(refLine[refPos] + 2, 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 twoDimVertR1:
  1219. this.addPixels(refLine[refPos] + 1, 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 twoDimVert0:
  1229. this.addPixels(refLine[refPos], 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 twoDimVertL3:
  1239. this.addPixelsNeg(refLine[refPos] - 3, blackPixels);
  1240. blackPixels ^= 1;
  1241. if (codingLine[this.codingPos] < columns) {
  1242. if (refPos > 0) {
  1243. --refPos;
  1244. } else {
  1245. ++refPos;
  1246. }
  1247. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1248. refPos += 2;
  1249. }
  1250. }
  1251. break;
  1252. case twoDimVertL2:
  1253. this.addPixelsNeg(refLine[refPos] - 2, blackPixels);
  1254. blackPixels ^= 1;
  1255. if (codingLine[this.codingPos] < columns) {
  1256. if (refPos > 0) {
  1257. --refPos;
  1258. } else {
  1259. ++refPos;
  1260. }
  1261. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1262. refPos += 2;
  1263. }
  1264. }
  1265. break;
  1266. case twoDimVertL1:
  1267. this.addPixelsNeg(refLine[refPos] - 1, blackPixels);
  1268. blackPixels ^= 1;
  1269. if (codingLine[this.codingPos] < columns) {
  1270. if (refPos > 0) {
  1271. --refPos;
  1272. } else {
  1273. ++refPos;
  1274. }
  1275. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  1276. refPos += 2;
  1277. }
  1278. }
  1279. break;
  1280. case ccittEOF:
  1281. this.addPixels(columns, 0);
  1282. this.eof = true;
  1283. break;
  1284. default:
  1285. (0, _util.info)('bad 2d code');
  1286. this.addPixels(columns, 0);
  1287. this.err = true;
  1288. }
  1289. }
  1290. } else {
  1291. codingLine[0] = 0;
  1292. this.codingPos = 0;
  1293. blackPixels = 0;
  1294. while (codingLine[this.codingPos] < columns) {
  1295. code1 = 0;
  1296. if (blackPixels) {
  1297. do {
  1298. code1 += code3 = this.getBlackCode();
  1299. } while (code3 >= 64);
  1300. } else {
  1301. do {
  1302. code1 += code3 = this.getWhiteCode();
  1303. } while (code3 >= 64);
  1304. }
  1305. this.addPixels(codingLine[this.codingPos] + code1, blackPixels);
  1306. blackPixels ^= 1;
  1307. }
  1308. }
  1309. var gotEOL = false;
  1310. if (this.byteAlign) {
  1311. this.inputBits &= ~7;
  1312. }
  1313. if (!this.eoblock && this.row === this.rows - 1) {
  1314. this.eof = true;
  1315. } else {
  1316. code1 = this.lookBits(12);
  1317. if (this.eoline) {
  1318. while (code1 !== ccittEOF && code1 !== 1) {
  1319. this.eatBits(1);
  1320. code1 = this.lookBits(12);
  1321. }
  1322. } else {
  1323. while (code1 === 0) {
  1324. this.eatBits(1);
  1325. code1 = this.lookBits(12);
  1326. }
  1327. }
  1328. if (code1 === 1) {
  1329. this.eatBits(12);
  1330. gotEOL = true;
  1331. } else if (code1 === ccittEOF) {
  1332. this.eof = true;
  1333. }
  1334. }
  1335. if (!this.eof && this.encoding > 0) {
  1336. this.nextLine2D = !this.lookBits(1);
  1337. this.eatBits(1);
  1338. }
  1339. if (this.eoblock && gotEOL && this.byteAlign) {
  1340. code1 = this.lookBits(12);
  1341. if (code1 === 1) {
  1342. this.eatBits(12);
  1343. if (this.encoding > 0) {
  1344. this.lookBits(1);
  1345. this.eatBits(1);
  1346. }
  1347. if (this.encoding >= 0) {
  1348. for (i = 0; i < 4; ++i) {
  1349. code1 = this.lookBits(12);
  1350. if (code1 !== 1) {
  1351. (0, _util.info)('bad rtc code: ' + code1);
  1352. }
  1353. this.eatBits(12);
  1354. if (this.encoding > 0) {
  1355. this.lookBits(1);
  1356. this.eatBits(1);
  1357. }
  1358. }
  1359. }
  1360. this.eof = true;
  1361. }
  1362. } else if (this.err && this.eoline) {
  1363. while (true) {
  1364. code1 = this.lookBits(13);
  1365. if (code1 === ccittEOF) {
  1366. this.eof = true;
  1367. return null;
  1368. }
  1369. if (code1 >> 1 === 1) {
  1370. break;
  1371. }
  1372. this.eatBits(1);
  1373. }
  1374. this.eatBits(12);
  1375. if (this.encoding > 0) {
  1376. this.eatBits(1);
  1377. this.nextLine2D = !(code1 & 1);
  1378. }
  1379. }
  1380. if (codingLine[0] > 0) {
  1381. this.outputBits = codingLine[this.codingPos = 0];
  1382. } else {
  1383. this.outputBits = codingLine[this.codingPos = 1];
  1384. }
  1385. this.row++;
  1386. }
  1387. var c;
  1388. if (this.outputBits >= 8) {
  1389. c = this.codingPos & 1 ? 0 : 0xFF;
  1390. this.outputBits -= 8;
  1391. if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
  1392. this.codingPos++;
  1393. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  1394. }
  1395. } else {
  1396. bits = 8;
  1397. c = 0;
  1398. do {
  1399. if (this.outputBits > bits) {
  1400. c <<= bits;
  1401. if (!(this.codingPos & 1)) {
  1402. c |= 0xFF >> 8 - bits;
  1403. }
  1404. this.outputBits -= bits;
  1405. bits = 0;
  1406. } else {
  1407. c <<= this.outputBits;
  1408. if (!(this.codingPos & 1)) {
  1409. c |= 0xFF >> 8 - this.outputBits;
  1410. }
  1411. bits -= this.outputBits;
  1412. this.outputBits = 0;
  1413. if (codingLine[this.codingPos] < columns) {
  1414. this.codingPos++;
  1415. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  1416. } else if (bits > 0) {
  1417. c <<= bits;
  1418. bits = 0;
  1419. }
  1420. }
  1421. } while (bits);
  1422. }
  1423. if (this.black) {
  1424. c ^= 0xFF;
  1425. }
  1426. return c;
  1427. };
  1428. CCITTFaxStream.prototype.findTableCode = function ccittFaxStreamFindTableCode(start, end, table, limit) {
  1429. var limitValue = limit || 0;
  1430. for (var i = start; i <= end; ++i) {
  1431. var code = this.lookBits(i);
  1432. if (code === ccittEOF) {
  1433. return [true, 1, false];
  1434. }
  1435. if (i < end) {
  1436. code <<= end - i;
  1437. }
  1438. if (!limitValue || code >= limitValue) {
  1439. var p = table[code - limitValue];
  1440. if (p[0] === i) {
  1441. this.eatBits(i);
  1442. return [true, p[1], true];
  1443. }
  1444. }
  1445. }
  1446. return [false, 0, false];
  1447. };
  1448. CCITTFaxStream.prototype.getTwoDimCode = function ccittFaxStreamGetTwoDimCode() {
  1449. var code = 0;
  1450. var p;
  1451. if (this.eoblock) {
  1452. code = this.lookBits(7);
  1453. p = twoDimTable[code];
  1454. if (p && p[0] > 0) {
  1455. this.eatBits(p[0]);
  1456. return p[1];
  1457. }
  1458. } else {
  1459. var result = this.findTableCode(1, 7, twoDimTable);
  1460. if (result[0] && result[2]) {
  1461. return result[1];
  1462. }
  1463. }
  1464. (0, _util.info)('Bad two dim code');
  1465. return ccittEOF;
  1466. };
  1467. CCITTFaxStream.prototype.getWhiteCode = function ccittFaxStreamGetWhiteCode() {
  1468. var code = 0;
  1469. var p;
  1470. if (this.eoblock) {
  1471. code = this.lookBits(12);
  1472. if (code === ccittEOF) {
  1473. return 1;
  1474. }
  1475. if (code >> 5 === 0) {
  1476. p = whiteTable1[code];
  1477. } else {
  1478. p = whiteTable2[code >> 3];
  1479. }
  1480. if (p[0] > 0) {
  1481. this.eatBits(p[0]);
  1482. return p[1];
  1483. }
  1484. } else {
  1485. var result = this.findTableCode(1, 9, whiteTable2);
  1486. if (result[0]) {
  1487. return result[1];
  1488. }
  1489. result = this.findTableCode(11, 12, whiteTable1);
  1490. if (result[0]) {
  1491. return result[1];
  1492. }
  1493. }
  1494. (0, _util.info)('bad white code');
  1495. this.eatBits(1);
  1496. return 1;
  1497. };
  1498. CCITTFaxStream.prototype.getBlackCode = function ccittFaxStreamGetBlackCode() {
  1499. var code, p;
  1500. if (this.eoblock) {
  1501. code = this.lookBits(13);
  1502. if (code === ccittEOF) {
  1503. return 1;
  1504. }
  1505. if (code >> 7 === 0) {
  1506. p = blackTable1[code];
  1507. } else if (code >> 9 === 0 && code >> 7 !== 0) {
  1508. p = blackTable2[(code >> 1) - 64];
  1509. } else {
  1510. p = blackTable3[code >> 7];
  1511. }
  1512. if (p[0] > 0) {
  1513. this.eatBits(p[0]);
  1514. return p[1];
  1515. }
  1516. } else {
  1517. var result = this.findTableCode(2, 6, blackTable3);
  1518. if (result[0]) {
  1519. return result[1];
  1520. }
  1521. result = this.findTableCode(7, 12, blackTable2, 64);
  1522. if (result[0]) {
  1523. return result[1];
  1524. }
  1525. result = this.findTableCode(10, 13, blackTable1);
  1526. if (result[0]) {
  1527. return result[1];
  1528. }
  1529. }
  1530. (0, _util.info)('bad black code');
  1531. this.eatBits(1);
  1532. return 1;
  1533. };
  1534. CCITTFaxStream.prototype.lookBits = function CCITTFaxStream_lookBits(n) {
  1535. var c;
  1536. while (this.inputBits < n) {
  1537. if ((c = this.str.getByte()) === -1) {
  1538. if (this.inputBits === 0) {
  1539. return ccittEOF;
  1540. }
  1541. return this.inputBuf << n - this.inputBits & 0xFFFF >> 16 - n;
  1542. }
  1543. this.inputBuf = this.inputBuf << 8 | c;
  1544. this.inputBits += 8;
  1545. }
  1546. return this.inputBuf >> this.inputBits - n & 0xFFFF >> 16 - n;
  1547. };
  1548. CCITTFaxStream.prototype.eatBits = function CCITTFaxStream_eatBits(n) {
  1549. if ((this.inputBits -= n) < 0) {
  1550. this.inputBits = 0;
  1551. }
  1552. };
  1553. return CCITTFaxStream;
  1554. }();
  1555. var LZWStream = function LZWStreamClosure() {
  1556. function LZWStream(str, maybeLength, earlyChange) {
  1557. this.str = str;
  1558. this.dict = str.dict;
  1559. this.cachedData = 0;
  1560. this.bitsCached = 0;
  1561. var maxLzwDictionarySize = 4096;
  1562. var lzwState = {
  1563. earlyChange: earlyChange,
  1564. codeLength: 9,
  1565. nextCode: 258,
  1566. dictionaryValues: new Uint8Array(maxLzwDictionarySize),
  1567. dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
  1568. dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
  1569. currentSequence: new Uint8Array(maxLzwDictionarySize),
  1570. currentSequenceLength: 0
  1571. };
  1572. for (var i = 0; i < 256; ++i) {
  1573. lzwState.dictionaryValues[i] = i;
  1574. lzwState.dictionaryLengths[i] = 1;
  1575. }
  1576. this.lzwState = lzwState;
  1577. DecodeStream.call(this, maybeLength);
  1578. }
  1579. LZWStream.prototype = Object.create(DecodeStream.prototype);
  1580. LZWStream.prototype.readBits = function LZWStream_readBits(n) {
  1581. var bitsCached = this.bitsCached;
  1582. var cachedData = this.cachedData;
  1583. while (bitsCached < n) {
  1584. var c = this.str.getByte();
  1585. if (c === -1) {
  1586. this.eof = true;
  1587. return null;
  1588. }
  1589. cachedData = cachedData << 8 | c;
  1590. bitsCached += 8;
  1591. }
  1592. this.bitsCached = bitsCached -= n;
  1593. this.cachedData = cachedData;
  1594. this.lastCode = null;
  1595. return cachedData >>> bitsCached & (1 << n) - 1;
  1596. };
  1597. LZWStream.prototype.readBlock = function LZWStream_readBlock() {
  1598. var blockSize = 512;
  1599. var estimatedDecodedSize = blockSize * 2,
  1600. decodedSizeDelta = blockSize;
  1601. var i, j, q;
  1602. var lzwState = this.lzwState;
  1603. if (!lzwState) {
  1604. return;
  1605. }
  1606. var earlyChange = lzwState.earlyChange;
  1607. var nextCode = lzwState.nextCode;
  1608. var dictionaryValues = lzwState.dictionaryValues;
  1609. var dictionaryLengths = lzwState.dictionaryLengths;
  1610. var dictionaryPrevCodes = lzwState.dictionaryPrevCodes;
  1611. var codeLength = lzwState.codeLength;
  1612. var prevCode = lzwState.prevCode;
  1613. var currentSequence = lzwState.currentSequence;
  1614. var currentSequenceLength = lzwState.currentSequenceLength;
  1615. var decodedLength = 0;
  1616. var currentBufferLength = this.bufferLength;
  1617. var buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1618. for (i = 0; i < blockSize; i++) {
  1619. var code = this.readBits(codeLength);
  1620. var hasPrev = currentSequenceLength > 0;
  1621. if (code < 256) {
  1622. currentSequence[0] = code;
  1623. currentSequenceLength = 1;
  1624. } else if (code >= 258) {
  1625. if (code < nextCode) {
  1626. currentSequenceLength = dictionaryLengths[code];
  1627. for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
  1628. currentSequence[j] = dictionaryValues[q];
  1629. q = dictionaryPrevCodes[q];
  1630. }
  1631. } else {
  1632. currentSequence[currentSequenceLength++] = currentSequence[0];
  1633. }
  1634. } else if (code === 256) {
  1635. codeLength = 9;
  1636. nextCode = 258;
  1637. currentSequenceLength = 0;
  1638. continue;
  1639. } else {
  1640. this.eof = true;
  1641. delete this.lzwState;
  1642. break;
  1643. }
  1644. if (hasPrev) {
  1645. dictionaryPrevCodes[nextCode] = prevCode;
  1646. dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
  1647. dictionaryValues[nextCode] = currentSequence[0];
  1648. nextCode++;
  1649. codeLength = nextCode + earlyChange & nextCode + earlyChange - 1 ? codeLength : Math.min(Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1, 12) | 0;
  1650. }
  1651. prevCode = code;
  1652. decodedLength += currentSequenceLength;
  1653. if (estimatedDecodedSize < decodedLength) {
  1654. do {
  1655. estimatedDecodedSize += decodedSizeDelta;
  1656. } while (estimatedDecodedSize < decodedLength);
  1657. buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  1658. }
  1659. for (j = 0; j < currentSequenceLength; j++) {
  1660. buffer[currentBufferLength++] = currentSequence[j];
  1661. }
  1662. }
  1663. lzwState.nextCode = nextCode;
  1664. lzwState.codeLength = codeLength;
  1665. lzwState.prevCode = prevCode;
  1666. lzwState.currentSequenceLength = currentSequenceLength;
  1667. this.bufferLength = currentBufferLength;
  1668. };
  1669. return LZWStream;
  1670. }();
  1671. var NullStream = function NullStreamClosure() {
  1672. function NullStream() {
  1673. Stream.call(this, new Uint8Array(0));
  1674. }
  1675. NullStream.prototype = Stream.prototype;
  1676. return NullStream;
  1677. }();
  1678. exports.Ascii85Stream = Ascii85Stream;
  1679. exports.AsciiHexStream = AsciiHexStream;
  1680. exports.CCITTFaxStream = CCITTFaxStream;
  1681. exports.DecryptStream = DecryptStream;
  1682. exports.DecodeStream = DecodeStream;
  1683. exports.FlateStream = FlateStream;
  1684. exports.Jbig2Stream = Jbig2Stream;
  1685. exports.JpegStream = JpegStream;
  1686. exports.JpxStream = JpxStream;
  1687. exports.NullStream = NullStream;
  1688. exports.PredictorStream = PredictorStream;
  1689. exports.RunLengthStream = RunLengthStream;
  1690. exports.Stream = Stream;
  1691. exports.StreamsSequenceStream = StreamsSequenceStream;
  1692. exports.StringStream = StringStream;
  1693. exports.LZWStream = LZWStream;