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