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