Debian Codegraph @

wasm/browser/src/zlib/rawinflate.js

raw
1 /* eslint-disable unicorn/numeric-separators-style,camelcase,no-unused-expressions */
2 goog.provide("Zlib.RawInflate");
3
4 goog.require("Zlib.Huffman");
5
6 // -----------------------------------------------------------------------------
7
8 /** @define {number} buffer block size. */
9 const ZLIB_RAW_INFLATE_BUFFER_SIZE = 0x8000; // [ 0x8000 >= ZLIB_BUFFER_BLOCK_SIZE ]
10
11 // -----------------------------------------------------------------------------
12
13 goog.scope(function () {
14 const buildHuffmanTable = Zlib.Huffman.buildHuffmanTable;
15
16 /**
17 * @constructor
18 * @param {!(Uint8Array)} input
19 * @param {Object} [opt_parameters]
20 *
21 * opt_params は以下のプロパティを指定する事ができます。
22 * - index: input buffer の deflate コンテナの開始位置.
23 * - blockSize: バッファのブロックサイズ.
24 * - bufferType: Zlib.RawInflate.BufferType の値によってバッファの管理方法を指定する.
25 * - resize: 確保したバッファが実際の大きさより大きかった場合に切り詰める.
26 */
27 Zlib.RawInflate = function (input, opt_parameters) {
28 /** @type {!(Uint8Array)} inflated buffer */
29 this.buffer;
30 /** @type {!Array.<(Uint8Array)>} */
31 this.blocks = [];
32 /** @type {number} block size. */
33 this.bufferSize = ZLIB_RAW_INFLATE_BUFFER_SIZE;
34 /** @type {!number} total output buffer pointer. */
35 this.totalpos = 0;
36 /** @type {!number} input buffer pointer. */
37 this.ip = 0;
38 /** @type {!number} bit stream reader buffer. */
39 this.bitsbuf = 0;
40 /** @type {!number} bit stream reader buffer size. */
41 this.bitsbuflen = 0;
42 /** @type {!(Uint8Array)} input buffer. */
43 this.input = new Uint8Array(input);
44 /** @type {!(Uint8Array)} output buffer. */
45 this.output;
46 /** @type {!number} output buffer pointer. */
47 this.op;
48 /** @type {boolean} is final block flag. */
49 this.bfinal = false;
50 /** @type {Zlib.RawInflate.BufferType} buffer management. */
51 this.bufferType = Zlib.RawInflate.BufferType.ADAPTIVE;
52 /** @type {boolean} resize flag for memory size optimization. */
53 this.resize = false;
54
55 // option parameters
56 if (opt_parameters || !(opt_parameters = {})) {
57 if (opt_parameters.index) {
58 this.ip = opt_parameters.index;
59 }
60 if (opt_parameters.bufferSize) {
61 this.bufferSize = opt_parameters.bufferSize;
62 }
63 if (opt_parameters.bufferType) {
64 this.bufferType = opt_parameters.bufferType;
65 }
66 if (opt_parameters.resize) {
67 this.resize = opt_parameters.resize;
68 }
69 }
70
71 // initialize
72 switch (this.bufferType) {
73 case Zlib.RawInflate.BufferType.BLOCK:
74 this.op = Zlib.RawInflate.MaxBackwardLength;
75 this.output = new Uint8Array(
76 Zlib.RawInflate.MaxBackwardLength + this.bufferSize + Zlib.RawInflate.MaxCopyLength,
77 );
78 break;
79 case Zlib.RawInflate.BufferType.ADAPTIVE:
80 this.op = 0;
81 this.output = new Uint8Array(this.bufferSize);
82 break;
83 default:
84 throw new Error("invalid inflate mode");
85 }
86 };
87
88 /**
89 * @enum {number}
90 */
91 Zlib.RawInflate.BufferType = {
92 BLOCK: 0,
93 ADAPTIVE: 1,
94 };
95
96 /**
97 * decompress.
98 * @return {!(Uint8Array)} inflated buffer.
99 */
100 Zlib.RawInflate.prototype.decompress = function () {
101 while (!this.bfinal) {
102 this.parseBlock();
103 }
104
105 switch (this.bufferType) {
106 case Zlib.RawInflate.BufferType.BLOCK:
107 return this.concatBufferBlock();
108 case Zlib.RawInflate.BufferType.ADAPTIVE:
109 return this.concatBufferDynamic();
110 default:
111 throw new Error("invalid inflate mode");
112 }
113 };
114
115 /**
116 * @const
117 * @type {number} max backward length for LZ77.
118 */
119 Zlib.RawInflate.MaxBackwardLength = 32768;
120
121 /**
122 * @const
123 * @type {number} max copy length for LZ77.
124 */
125 Zlib.RawInflate.MaxCopyLength = 258;
126
127 /**
128 * huffman order
129 * @const
130 * @type {!(Uint8Array)}
131 */
132 Zlib.RawInflate.Order = (function (table) {
133 return new Uint16Array(table);
134 })([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
135
136 /**
137 * huffman length code table.
138 * @const
139 * @type {!(Uint16Array)}
140 */
141 Zlib.RawInflate.LengthCodeTable = (function (table) {
142 return new Uint16Array(table);
143 })([
144 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000d, 0x000f, 0x0011,
145 0x0013, 0x0017, 0x001b, 0x001f, 0x0023, 0x002b, 0x0033, 0x003b, 0x0043, 0x0053, 0x0063, 0x0073,
146 0x0083, 0x00a3, 0x00c3, 0x00e3, 0x0102, 0x0102, 0x0102,
147 ]);
148
149 /**
150 * huffman length extra-bits table.
151 * @const
152 * @type {!(Uint8Array)}
153 */
154 Zlib.RawInflate.LengthExtraTable = (function (table) {
155 return new Uint8Array(table);
156 })([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0]);
157
158 /**
159 * huffman dist code table.
160 * @const
161 * @type {!(Uint16Array)}
162 */
163 Zlib.RawInflate.DistCodeTable = (function (table) {
164 return new Uint16Array(table);
165 })([
166 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0007, 0x0009, 0x000d, 0x0011, 0x0019, 0x0021, 0x0031,
167 0x0041, 0x0061, 0x0081, 0x00c1, 0x0101, 0x0181, 0x0201, 0x0301, 0x0401, 0x0601, 0x0801, 0x0c01,
168 0x1001, 0x1801, 0x2001, 0x3001, 0x4001, 0x6001,
169 ]);
170
171 /**
172 * huffman dist extra-bits table.
173 * @const
174 * @type {!(Uint8Array)}
175 */
176 Zlib.RawInflate.DistExtraTable = (function (table) {
177 return new Uint8Array(table);
178 })([
179 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13,
180 13,
181 ]);
182
183 /**
184 * fixed huffman length code table
185 * @const
186 * @type {!Array}
187 */
188 Zlib.RawInflate.FixedLiteralLengthTable = (function (table) {
189 return table;
190 })(
191 (function () {
192 const lengths = new Uint8Array(288);
193 let index, il;
194
195 for (index = 0, il = lengths.length; index < il; ++index) {
196 lengths[index] = index <= 143 ? 8 : index <= 255 ? 9 : index <= 279 ? 7 : 8;
197 }
198
199 return buildHuffmanTable(lengths);
200 })(),
201 );
202
203 /**
204 * fixed huffman distance code table
205 * @const
206 * @type {!Array}
207 */
208 Zlib.RawInflate.FixedDistanceTable = (function (table) {
209 return table;
210 })(
211 (function () {
212 const lengths = new Uint8Array(30);
213 let index, il;
214
215 for (index = 0, il = lengths.length; index < il; ++index) {
216 lengths[index] = 5;
217 }
218
219 return buildHuffmanTable(lengths);
220 })(),
221 );
222
223 /**
224 * parse deflated block.
225 */
226 Zlib.RawInflate.prototype.parseBlock = function () {
227 /** @type {number} header */
228 let hdr = this.readBits(3);
229
230 // BFINAL
231 if (hdr & 0x1) {
232 this.bfinal = true;
233 }
234
235 // BTYPE
236 hdr >>>= 1;
237 switch (hdr) {
238 // uncompressed
239 case 0:
240 this.parseUncompressedBlock();
241 break;
242 // fixed huffman
243 case 1:
244 this.parseFixedHuffmanBlock();
245 break;
246 // dynamic huffman
247 case 2:
248 this.parseDynamicHuffmanBlock();
249 break;
250 // reserved or other
251 default:
252 throw new Error("unknown BTYPE: " + hdr);
253 }
254 };
255
256 /**
257 * read inflate bits
258 * @param {number} length bits length.
259 * @return {number} read bits.
260 */
261 Zlib.RawInflate.prototype.readBits = function (length) {
262 let bitsbuf = this.bitsbuf;
263 let bitsbuflen = this.bitsbuflen;
264 const input = this.input;
265 let ip = this.ip;
266
267 /** @type {number} */
268 const inputLength = input.length;
269
270 if (ip + ((length - bitsbuflen + 7) >> 3) >= inputLength) {
271 throw new Error("input buffer is broken");
272 }
273
274 // not enough buffer
275 while (bitsbuflen < length) {
276 bitsbuf |= input[ip++] << bitsbuflen;
277 bitsbuflen += 8;
278 }
279
280 /** @type {number} input and output byte. */
281 const octet = bitsbuf & /* MASK */ ((1 << length) - 1);
282 bitsbuf >>>= length;
283 bitsbuflen -= length;
284
285 this.bitsbuf = bitsbuf;
286 this.bitsbuflen = bitsbuflen;
287 this.ip = ip;
288
289 return octet;
290 };
291
292 /**
293 * read huffman code using table
294 * @param {Array.<number>|Uint8Array|Uint16Array|null} table huffman code table.
295 * @return {number} huffman code.
296 */
297 Zlib.RawInflate.prototype.readCodeByTable = function (table) {
298 let bitsbuf = this.bitsbuf;
299 let bitsbuflen = this.bitsbuflen;
300 const input = this.input;
301 let ip = this.ip;
302
303 /** @type {number} */
304 const inputLength = input.length;
305 /** @type {!(Uint8Array)} huffman code table */
306 const codeTable = table[0];
307 /** @type {number} */
308 const maxCodeLength = table[1];
309
310 // not enough buffer
311 while (bitsbuflen < maxCodeLength) {
312 if (ip >= inputLength) {
313 break;
314 }
315 bitsbuf |= input[ip++] << bitsbuflen;
316 bitsbuflen += 8;
317 }
318
319 // read max length
320 /** @type {number} code length & code (16bit, 16bit) */
321 const codeWithLength = codeTable[bitsbuf & ((1 << maxCodeLength) - 1)];
322 /** @type {number} code bits length */
323 const codeLength = codeWithLength >>> 16;
324
325 if (codeLength > bitsbuflen) {
326 throw new Error("invalid code length: " + codeLength);
327 }
328
329 this.bitsbuf = bitsbuf >> codeLength;
330 this.bitsbuflen = bitsbuflen - codeLength;
331 this.ip = ip;
332
333 return codeWithLength & 0xffff;
334 };
335
336 /**
337 * parse uncompressed block.
338 */
339 Zlib.RawInflate.prototype.parseUncompressedBlock = function () {
340 const input = this.input;
341 let ip = this.ip;
342 let output = this.output;
343 let op = this.op;
344
345 /** @type {number} */
346 const inputLength = input.length;
347 /** @type {number} block length */
348 let length_;
349 /** @type {number} output buffer length */
350 const olength = output.length;
351 /** @type {number} copy counter */
352 let preCopy;
353
354 // skip buffered header bits
355 this.bitsbuf = 0;
356 this.bitsbuflen = 0;
357
358 // len
359 if (ip + 1 >= inputLength) {
360 throw new Error("invalid uncompressed block header: LEN");
361 }
362 length_ = input[ip++] | (input[ip++] << 8);
363
364 // nlen
365 if (ip + 1 >= inputLength) {
366 throw new Error("invalid uncompressed block header: NLEN");
367 }
368
369 /** @type {number} number for check block length */
370 const nlen = input[ip++] | (input[ip++] << 8);
371
372 // check len & nlen
373 if (length_ === ~nlen) {
374 throw new Error("invalid uncompressed block header: length verify");
375 }
376
377 // check size
378 if (ip + length_ > input.length) {
379 throw new Error("input buffer is broken");
380 }
381
382 // expand buffer
383 switch (this.bufferType) {
384 case Zlib.RawInflate.BufferType.BLOCK:
385 // pre copy
386 while (op + length_ > output.length) {
387 preCopy = olength - op;
388 length_ -= preCopy;
389 output.set(input.subarray(ip, ip + preCopy), op);
390 op += preCopy;
391 ip += preCopy;
392
393 this.op = op;
394 output = this.expandBufferBlock();
395 op = this.op;
396 }
397 break;
398 case Zlib.RawInflate.BufferType.ADAPTIVE:
399 while (op + length_ > output.length) {
400 output = this.expandBufferAdaptive({ fixRatio: 2 });
401 }
402 break;
403 default:
404 throw new Error("invalid inflate mode");
405 }
406
407 // copy
408 output.set(input.subarray(ip, ip + length_), op);
409 op += length_;
410 ip += length_;
411
412 this.ip = ip;
413 this.op = op;
414 this.output = output;
415 };
416
417 /**
418 * parse fixed huffman block.
419 */
420 Zlib.RawInflate.prototype.parseFixedHuffmanBlock = function () {
421 switch (this.bufferType) {
422 case Zlib.RawInflate.BufferType.ADAPTIVE:
423 this.decodeHuffmanAdaptive(
424 Zlib.RawInflate.FixedLiteralLengthTable,
425 Zlib.RawInflate.FixedDistanceTable,
426 );
427 break;
428 case Zlib.RawInflate.BufferType.BLOCK:
429 this.decodeHuffmanBlock(
430 Zlib.RawInflate.FixedLiteralLengthTable,
431 Zlib.RawInflate.FixedDistanceTable,
432 );
433 break;
434 default:
435 throw new Error("invalid inflate mode");
436 }
437 };
438
439 /**
440 * parse dynamic huffman block.
441 */
442 Zlib.RawInflate.prototype.parseDynamicHuffmanBlock = function () {
443 /** @type {number} number of literal and length codes. */
444 const hlit = this.readBits(5) + 257;
445 /** @type {number} number of distance codes. */
446 const hdist = this.readBits(5) + 1;
447 /** @type {number} number of code lengths. */
448 const hclen = this.readBits(4) + 4;
449 /** @type {Uint8Array} code lengths. */
450 const codeLengths = new Uint8Array(Zlib.RawInflate.Order.length);
451 /** @type {number} */
452 let code;
453 /** @type {number} */
454 let previous;
455 /** @type {number} */
456 let repeat;
457 /** @type {number} loop counter. */
458 let index;
459 /** @type {number} loop limit. */
460 let il;
461
462 // decode code lengths
463 for (index = 0; index < hclen; ++index) {
464 codeLengths[Zlib.RawInflate.Order[index]] = this.readBits(3);
465 }
466
467 // decode length table
468 /** @type {Array.<number,number,number>} code lengths table. */
469 const codeLengthsTable = buildHuffmanTable(codeLengths);
470 /** @type {Uint8Array} code length table. */
471 const lengthTable = new Uint8Array(hlit + hdist);
472 for (index = 0, il = hlit + hdist; index < il; ) {
473 code = this.readCodeByTable(codeLengthsTable);
474 switch (code) {
475 case 16:
476 repeat = 3 + this.readBits(2);
477 while (repeat--) {
478 lengthTable[index++] = previous;
479 }
480 break;
481 case 17:
482 repeat = 3 + this.readBits(3);
483 while (repeat--) {
484 lengthTable[index++] = 0;
485 }
486 previous = 0;
487 break;
488 case 18:
489 repeat = 11 + this.readBits(7);
490 while (repeat--) {
491 lengthTable[index++] = 0;
492 }
493 previous = 0;
494 break;
495 default:
496 lengthTable[index++] = code;
497 previous = code;
498 break;
499 }
500 }
501
502 /** @type {Array.<number>|Uint8Array|null} literal and length code table. */
503 const litlenTable = buildHuffmanTable(lengthTable.subarray(0, hlit));
504 /** @type {Array.<number>|Uint8Array} distance code table. */
505 const distTable = buildHuffmanTable(lengthTable.subarray(hlit));
506
507 switch (this.bufferType) {
508 case Zlib.RawInflate.BufferType.ADAPTIVE:
509 this.decodeHuffmanAdaptive(litlenTable, distTable);
510 break;
511 case Zlib.RawInflate.BufferType.BLOCK:
512 this.decodeHuffmanBlock(litlenTable, distTable);
513 break;
514 default:
515 throw new Error("invalid inflate mode");
516 }
517 };
518
519 /**
520 * decode huffman code
521 * @param {Array.<number>|Uint8Array|Uint16Array} litlen literal and length code table.
522 * @param {Array.<number>|Uint8Array} dist distination code table.
523 */
524 Zlib.RawInflate.prototype.decodeHuffmanBlock = function (litlen, dist) {
525 let output = this.output;
526 let op = this.op;
527
528 this.currentLitlenTable = litlen;
529
530 /** @type {number} output position limit. */
531 const olength = output.length - Zlib.RawInflate.MaxCopyLength;
532 /** @type {number} huffman code. */
533 let code;
534 /** @type {number} table index. */
535 let ti;
536 /** @type {number} huffman code distination. */
537 let codeDist;
538 /** @type {number} huffman code length. */
539 let codeLength;
540
541 const lengthCodeTable = Zlib.RawInflate.LengthCodeTable;
542 const lengthExtraTable = Zlib.RawInflate.LengthExtraTable;
543 const distCodeTable = Zlib.RawInflate.DistCodeTable;
544 const distExtraTable = Zlib.RawInflate.DistExtraTable;
545
546 while ((code = this.readCodeByTable(litlen)) !== 256) {
547 // literal
548 if (code < 256) {
549 if (op >= olength) {
550 this.op = op;
551 output = this.expandBufferBlock();
552 op = this.op;
553 }
554 output[op++] = code;
555
556 continue;
557 }
558
559 // length code
560 ti = code - 257;
561 codeLength = lengthCodeTable[ti];
562 if (lengthExtraTable[ti] > 0) {
563 codeLength += this.readBits(lengthExtraTable[ti]);
564 }
565
566 // dist code
567 code = this.readCodeByTable(dist);
568 codeDist = distCodeTable[code];
569 if (distExtraTable[code] > 0) {
570 codeDist += this.readBits(distExtraTable[code]);
571 }
572
573 // lz77 decode
574 if (op >= olength) {
575 this.op = op;
576 output = this.expandBufferBlock();
577 op = this.op;
578 }
579 while (codeLength--) {
580 output[op] = output[op++ - codeDist];
581 }
582 }
583
584 while (this.bitsbuflen >= 8) {
585 this.bitsbuflen -= 8;
586 this.ip--;
587 }
588 this.op = op;
589 };
590
591 /**
592 * decode huffman code (adaptive)
593 * @param {Array.<number>|Uint8Array|Uint16Array} litlen literal and length code table.
594 * @param {Array.<number>|Uint8Array} dist distination code table.
595 */
596 Zlib.RawInflate.prototype.decodeHuffmanAdaptive = function (litlen, dist) {
597 let output = this.output;
598 let op = this.op;
599
600 this.currentLitlenTable = litlen;
601
602 /** @type {number} output position limit. */
603 let olength = output.length;
604 /** @type {number} huffman code. */
605 let code;
606 /** @type {number} table index. */
607 let ti;
608 /** @type {number} huffman code distination. */
609 let codeDist;
610 /** @type {number} huffman code length. */
611 let codeLength;
612
613 const lengthCodeTable = Zlib.RawInflate.LengthCodeTable;
614 const lengthExtraTable = Zlib.RawInflate.LengthExtraTable;
615 const distCodeTable = Zlib.RawInflate.DistCodeTable;
616 const distExtraTable = Zlib.RawInflate.DistExtraTable;
617
618 while ((code = this.readCodeByTable(litlen)) !== 256) {
619 // literal
620 if (code < 256) {
621 if (op >= olength) {
622 output = this.expandBufferAdaptive();
623 olength = output.length;
624 }
625 output[op++] = code;
626
627 continue;
628 }
629
630 // length code
631 ti = code - 257;
632 codeLength = lengthCodeTable[ti];
633 if (lengthExtraTable[ti] > 0) {
634 codeLength += this.readBits(lengthExtraTable[ti]);
635 }
636
637 // dist code
638 code = this.readCodeByTable(dist);
639 codeDist = distCodeTable[code];
640 if (distExtraTable[code] > 0) {
641 codeDist += this.readBits(distExtraTable[code]);
642 }
643
644 // lz77 decode
645 if (op + codeLength > olength) {
646 output = this.expandBufferAdaptive();
647 olength = output.length;
648 }
649 while (codeLength--) {
650 output[op] = output[op++ - codeDist];
651 }
652 }
653
654 while (this.bitsbuflen >= 8) {
655 this.bitsbuflen -= 8;
656 this.ip--;
657 }
658 this.op = op;
659 };
660
661 /**
662 * expand output buffer.
663 * @param {Object} [opt_parameter]
664 * @return {!(Uint8Array)} output buffer.
665 */
666 Zlib.RawInflate.prototype.expandBufferBlock = function (opt_parameter) {
667 /** @type {!(Uint8Array)} store buffer. */
668 const buffer = new Uint8Array(this.op - Zlib.RawInflate.MaxBackwardLength);
669 /** @type {number} backward base point */
670 const backward = this.op - Zlib.RawInflate.MaxBackwardLength;
671
672 const output = this.output;
673
674 // copy to output buffer
675 buffer.set(output.subarray(Zlib.RawInflate.MaxBackwardLength, buffer.length));
676
677 this.blocks.push(buffer);
678 this.totalpos += buffer.length;
679
680 // copy to backward buffer
681 output.set(output.subarray(backward, backward + Zlib.RawInflate.MaxBackwardLength));
682
683 this.op = Zlib.RawInflate.MaxBackwardLength;
684
685 return output;
686 };
687
688 /**
689 * expand output buffer. (adaptive)
690 * @param {Object=} opt_parameter
691 * @return {!(Uint8Array)} output buffer pointer.
692 */
693 Zlib.RawInflate.prototype.expandBufferAdaptive = function (opt_parameter) {
694 /** @type {number} expantion ratio. */
695 let ratio = Math.trunc(this.input.length / this.ip + 1);
696 /** @type {number} maximum number of huffman code. */
697 let maxHuffCode;
698 /** @type {number} new output buffer size. */
699 let newSize;
700 /** @type {number} max inflate size. */
701 let maxInflateSize;
702
703 const input = this.input;
704 const output = this.output;
705
706 if (opt_parameter) {
707 if (typeof opt_parameter.fixRatio === "number") {
708 ratio = opt_parameter.fixRatio;
709 }
710 if (typeof opt_parameter.addRatio === "number") {
711 ratio += opt_parameter.addRatio;
712 }
713 }
714
715 // calculate new buffer size
716 if (ratio < 2) {
717 maxHuffCode = (input.length - this.ip) / this.currentLitlenTable[2];
718 maxInflateSize = Math.trunc((maxHuffCode / 2) * 258);
719 newSize =
720 maxInflateSize < output.length ? output.length + maxInflateSize : output.length << 1;
721 } else {
722 newSize = output.length * ratio;
723 }
724
725 // buffer expantion
726 /** @type {!(Uint8Array)} store buffer. */
727 const buffer = new Uint8Array(newSize);
728 buffer.set(output);
729
730 this.output = buffer;
731
732 return this.output;
733 };
734
735 /**
736 * concat output buffer.
737 * @return {!(Uint8Array)} output buffer.
738 */
739 Zlib.RawInflate.prototype.concatBufferBlock = function () {
740 /** @type {number} buffer pointer. */
741 let pos = 0;
742 /** @type {number} buffer pointer. */
743 const limit = this.totalpos + (this.op - Zlib.RawInflate.MaxBackwardLength);
744 /** @type {!(Uint8Array)} output block array. */
745 const output = this.output;
746 /** @type {!Array} blocks array. */
747 const blocks = this.blocks;
748 /** @type {!(Uint8Array)} output block array. */
749 let block;
750 /** @type {!(Uint8Array)} output buffer. */
751 const buffer = new Uint8Array(limit);
752 /** @type {number} loop counter. */
753 let index;
754 /** @type {number} loop limiter. */
755 let il;
756 /** @type {number} loop counter. */
757 let index_;
758 /** @type {number} loop limiter. */
759 let jl;
760
761 // single buffer
762 if (blocks.length === 0) {
763 return this.output.subarray(Zlib.RawInflate.MaxBackwardLength, this.op);
764 }
765
766 // copy to buffer
767 for (index = 0, il = blocks.length; index < il; ++index) {
768 block = blocks[index];
769 for (index_ = 0, jl = block.length; index_ < jl; ++index_) {
770 buffer[pos++] = block[index_];
771 }
772 }
773
774 // current buffer
775 for (index = Zlib.RawInflate.MaxBackwardLength, il = this.op; index < il; ++index) {
776 buffer[pos++] = output[index];
777 }
778
779 this.blocks = [];
780 this.buffer = buffer;
781
782 return this.buffer;
783 };
784
785 /**
786 * concat output buffer. (dynamic)
787 * @return {!(Uint8Array)} output buffer.
788 */
789 Zlib.RawInflate.prototype.concatBufferDynamic = function () {
790 /** @type {Uint8Array} output buffer. */
791 let buffer;
792 const op = this.op;
793 if (this.resize) {
794 buffer = new Uint8Array(op);
795 buffer.set(this.output.subarray(0, op));
796 } else {
797 buffer = this.output.subarray(0, op);
798 }
799
800 this.buffer = buffer;
801
802 return this.buffer;
803 };
804
805 // end of scope
806 });