2 Copyright (c) 2013 Gildas Lormeau. All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 1. Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in
12 the documentation and/or other materials provided with the distribution.
14 3. The names of the authors may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
18 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
20 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * This program is based on JZlib 1.0.2 ymnk, JCraft,Inc.
31 * JZlib is based on zlib-1.1.3, so all credit should go authors
32 * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
33 * and contributors of zlib.
44 var Z_STREAM_ERROR
= -2;
45 var Z_DATA_ERROR
= -3;
49 var inflate_mask
= [ 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff,
50 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff ];
54 // JZlib version : "1.0.2"
62 var fixed_tl
= [ 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0,
63 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40,
64 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13,
65 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60,
66 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7,
67 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8,
68 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80,
69 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0,
70 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0,
71 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97,
72 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210,
73 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117,
74 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154,
75 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83,
76 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230,
77 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139,
78 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174,
79 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111,
80 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9,
81 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8,
82 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8,
83 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8,
84 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9,
85 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8,
86 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9,
87 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8,
88 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9,
89 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0,
90 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9,
91 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8,
92 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9,
93 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8,
94 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9,
95 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8,
96 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9,
97 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8,
98 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255 ];
99 var fixed_td
= [ 80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5,
100 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5,
101 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577 ];
103 // Tables for deflate from PKZIP's appnote.txt.
104 var cplens
= [ // Copy lengths for literal codes 257..285
105 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 ];
107 // see note #13 above about 258
108 var cplext
= [ // Extra bits for literal codes 257..285
109 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, 112, 112 // 112==invalid
112 var cpdist
= [ // Copy offsets for distance codes 0..29
113 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 ];
115 var cpdext
= [ // Extra bits for distance codes
116 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 ];
118 // If BMAX needs to be larger than 16, then h and x[] should be uLong.
119 var BMAX
= 15; // maximum bit length of any code
124 var hn
; // hufts used in space
125 var v
; // work area for huft_build
126 var c
; // bit length count table
127 var r
; // table entry for structure assignment
128 var u
; // table stack
129 var x
; // bit offsets, then code stack
131 function huft_build(b
, // code lengths in bits (all assumed <=
133 bindex
, n
, // number of codes (assumed <= 288)
134 s
, // number of simple-valued codes (0..s-1)
135 d
, // list of base values for non-simple codes
136 e
, // list of extra bits for non-simple codes
137 t
, // result: starting table
138 m
, // maximum lookup bits, returns actual
139 hp
,// space for trees
140 hn
,// hufts used in space
141 v
// working area: values in order of bit length
143 // Given a list of code lengths and a maximum table size, make a set of
144 // tables to decode that set of codes. Return Z_OK on success,
146 // if the given code set is incomplete (the tables are still built in
148 // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set
150 // lengths), or Z_MEM_ERROR if not enough memory.
152 var a
; // counter for codes of length k
153 var f
; // i repeats in table every f entries
154 var g
; // maximum code length
155 var h
; // table level
156 var i
; // counter, current code
158 var k
; // number of bits in current code
159 var l
; // bits per table (returned in m)
160 var mask
; // (1 << w) - 1, to avoid cc -O bug on HP
161 var p
; // pointer into c[], b[], or v[]
162 var q
; // points to current table
163 var w
; // bits before this table == (l * h)
164 var xp
; // pointer into x
165 var y
; // number of dummy codes added
166 var z
; // number of entries in current table
168 // Generate counts for each bit length
175 i
--; // assume all entries <= BMAX
178 if (c
[0] == n
) { // null input--all zero length codes
184 // Find minimum and maximum length, bound *m by those
186 for (j
= 1; j
<= BMAX
; j
++)
189 k
= j
; // minimum code length
193 for (i
= BMAX
; i
!== 0; i
--) {
197 g
= i
; // maximum code length
203 // Adjust last length count to fill out codes, if needed
204 for (y
= 1 << j
; j
< i
; j
++, y
<<= 1) {
205 if ((y
-= c
[j
]) < 0) {
209 if ((y
-= c
[i
]) < 0) {
214 // Generate starting offsets into the value table for each length
218 while (--i
!== 0) { // note that i == g from above
224 // Make a table of values in order of bit lengths
228 if ((j
= b
[bindex
+ p
]) !== 0) {
233 n
= x
[g
]; // set n to length of v
235 // Generate the Huffman codes and for each, make the table entries
236 x
[0] = i
= 0; // first Huffman code is zero
237 p
= 0; // grab values in bit order
238 h
= -1; // no tables yet--level -1
239 w
= -l
; // bits decoded == (l * h)
240 u
[0] = 0; // just to keep compilers happy
244 // go through the bit lengths (k already is bits in shortest code)
245 for (; k
<= g
; k
++) {
248 // here i is the Huffman code of length k bits for value *p
249 // make tables up to required level
252 w
+= l
; // previous table always l bits
253 // compute minimum size table less than or equal to l bits
255 z
= (z
> l
) ? l
: z
; // table size upper limit
256 if ((f
= 1 << (j
= k
- w
)) > a
+ 1) { // try a k-w bit table
259 f
-= a
+ 1; // deduct codes from patterns left
262 while (++j
< z
) { // try smaller tables up to z bits
263 if ((f
<<= 1) <= c
[++xp
])
264 break; // enough codes to use up j bits
265 f
-= c
[xp
]; // else deduct codes from patterns
269 z
= 1 << j
; // table entries for j-bit table
271 // allocate new table
272 if (hn
[0] + z
> MANY
) { // (note: doesn't matter for fixed)
273 return Z_DATA_ERROR
; // overflow of MANY
275 u
[h
] = q
= /* hp+ */hn
[0]; // DEBUG
278 // connect to last table, if there is one
280 x
[h
] = i
; // save pattern for backing up
281 r
[0] = /* (byte) */j
; // bits in this table
282 r
[1] = /* (byte) */l
; // bits to dump before this table
284 r
[2] = /* (int) */(q
- u
[h
- 1] - j
); // offset to this table
285 hp
.set(r
, (u
[h
- 1] + j
) * 3);
290 t
[0] = q
; // first table is returned result
294 // set up table entry in r
295 r
[1] = /* (byte) */(k
- w
);
297 r
[0] = 128 + 64; // out of values--invalid code
298 } else if (v
[p
] < s
) {
299 r
[0] = /* (byte) */(v
[p
] < 256 ? 0 : 32 + 64); // 256 is
301 r
[2] = v
[p
++]; // simple code is just the value
303 r
[0] = /* (byte) */(e
[v
[p
] - s
] + 16 + 64); // non-simple--look
305 r
[2] = d
[v
[p
++] - s
];
308 // fill code-like entries with r
310 for (j
= i
>>> w
; j
< z
; j
+= f
) {
311 hp
.set(r
, (q
+ j
) * 3);
314 // backwards increment the k-bit code i
315 for (j
= 1 << (k
- 1); (i
& j
) !== 0; j
>>>= 1) {
320 // backup over finished tables
321 mask
= (1 << w
) - 1; // needed on HP, cc -O bug
322 while ((i
& mask
) != x
[h
]) {
323 h
--; // don't need to update q
329 // Return Z_BUF_ERROR if we were given an incomplete table
330 return y
!== 0 && g
!= 1 ? Z_BUF_ERROR
: Z_OK
;
333 function initWorkArea(vsize
) {
336 hn
= []; // []; //new Array(1);
337 v
= []; // new Array(vsize);
338 c
= new Int32Array(BMAX
+ 1); // new Array(BMAX + 1);
339 r
= []; // new Array(3);
340 u
= new Int32Array(BMAX
); // new Array(BMAX);
341 x
= new Int32Array(BMAX
+ 1); // new Array(BMAX + 1);
343 if (v
.length
< vsize
) {
344 v
= []; // new Array(vsize);
346 for (i
= 0; i
< vsize
; i
++) {
349 for (i
= 0; i
< BMAX
+ 1; i
++) {
352 for (i
= 0; i
< 3; i
++) {
355 // for(int i=0; i<BMAX; i++){u[i]=0;}
356 u
.set(c
.subarray(0, BMAX
), 0);
357 // for(int i=0; i<BMAX+1; i++){x[i]=0;}
358 x
.set(c
.subarray(0, BMAX
+ 1), 0);
361 that
.inflate_trees_bits = function(c
, // 19 code lengths
362 bb
, // bits tree desired/actual depth
363 tb
, // bits tree result
364 hp
, // space for trees
370 result
= huft_build(c
, 0, 19, 19, null, null, tb
, bb
, hp
, hn
, v
);
372 if (result
== Z_DATA_ERROR
) {
373 z
.msg
= "oversubscribed dynamic bit lengths tree";
374 } else if (result
== Z_BUF_ERROR
|| bb
[0] === 0) {
375 z
.msg
= "incomplete dynamic bit lengths tree";
376 result
= Z_DATA_ERROR
;
381 that
.inflate_trees_dynamic = function(nl
, // number of literal/length codes
382 nd
, // number of distance codes
383 c
, // that many (total) code lengths
384 bl
, // literal desired/actual bit depth
385 bd
, // distance desired/actual bit depth
386 tl
, // literal/length tree result
387 td
, // distance tree result
388 hp
, // space for trees
393 // build literal/length tree
396 result
= huft_build(c
, 0, nl
, 257, cplens
, cplext
, tl
, bl
, hp
, hn
, v
);
397 if (result
!= Z_OK
|| bl
[0] === 0) {
398 if (result
== Z_DATA_ERROR
) {
399 z
.msg
= "oversubscribed literal/length tree";
400 } else if (result
!= Z_MEM_ERROR
) {
401 z
.msg
= "incomplete literal/length tree";
402 result
= Z_DATA_ERROR
;
407 // build distance tree
409 result
= huft_build(c
, nl
, nd
, 0, cpdist
, cpdext
, td
, bd
, hp
, hn
, v
);
411 if (result
!= Z_OK
|| (bd
[0] === 0 && nl
> 257)) {
412 if (result
== Z_DATA_ERROR
) {
413 z
.msg
= "oversubscribed distance tree";
414 } else if (result
== Z_BUF_ERROR
) {
415 z
.msg
= "incomplete distance tree";
416 result
= Z_DATA_ERROR
;
417 } else if (result
!= Z_MEM_ERROR
) {
418 z
.msg
= "empty distance tree with lengths";
419 result
= Z_DATA_ERROR
;
429 InfTree
.inflate_trees_fixed = function(bl
, // literal desired/actual bit depth
430 bd
, // distance desired/actual bit depth
431 tl
,// literal/length tree result
432 td
// distance tree result
443 // waiting for "i:"=input,
446 var START
= 0; // x: set up for LEN
447 var LEN
= 1; // i: get length/literal/eob next
448 var LENEXT
= 2; // i: getting length extra (have base)
449 var DIST
= 3; // i: get distance next
450 var DISTEXT
= 4;// i: getting distance extra
451 var COPY
= 5; // o: copying bytes in window, waiting
453 var LIT
= 6; // o: got literal, waiting for output
455 var WASH
= 7; // o: got eob, possibly still output
457 var END
= 8; // x: got eob and all data flushed
458 var BADCODE
= 9;// x: got error
460 function InfCodes() {
463 var mode
; // current inflate_codes mode
465 // mode dependent information
468 var tree
; // pointer into tree
470 var need
= 0; // bits needed
474 // if EXT or COPY, where and how much
475 var get = 0; // bits to get for extra
476 var dist
= 0; // distance back to copy from
478 var lbits
= 0; // ltree bits decoded per branch
479 var dbits
= 0; // dtree bits decoder per branch
480 var ltree
; // literal/length/eob tree
481 var ltree_index
= 0; // literal/length/eob tree
482 var dtree
; // distance tree
483 var dtree_index
= 0; // distance tree
485 // Called with number of bytes left to write in window at least 258
486 // (the maximum string length) and number of input bytes available
487 // at least ten. The ten bytes are six bytes for the longest length/
488 // distance pair plus four bytes for overloading the bit buffer.
490 function inflate_fast(bl
, bd
, tl
, tl_index
, td
, td_index
, s
, z
) {
491 var t
; // temporary pointer
492 var tp
; // temporary pointer
493 var tp_index
; // temporary pointer
494 var e
; // extra bits or operation
496 var k
; // bits in bit buffer
497 var p
; // input data pointer
498 var n
; // bytes available there
499 var q
; // output window write pointer
500 var m
; // bytes to end of window or read pointer
501 var ml
; // mask for literal/length tree
502 var md
; // mask for distance tree
503 var c
; // bytes to copy
504 var d
; // distance back to copy from
505 var r
; // copy source pointer
507 var tp_index_t_3
; // (tp_index+t)*3
509 // load input, output, bit values
515 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
518 ml
= inflate_mask
[bl
];
519 md
= inflate_mask
[bd
];
521 // do until not enough input or output space for fast loop
522 do { // assume called with m >= 258 && n >= 10
523 // get literal/length code
524 while (k
< (20)) { // max bits for literal/length code
526 b
|= (z
.read_byte(p
++) & 0xff) << k
;
533 tp_index_t_3
= (tp_index
+ t
) * 3;
534 if ((e
= tp
[tp_index_t_3
]) === 0) {
535 b
>>= (tp
[tp_index_t_3
+ 1]);
536 k
-= (tp
[tp_index_t_3
+ 1]);
538 s
.window
[q
++] = /* (byte) */tp
[tp_index_t_3
+ 2];
544 b
>>= (tp
[tp_index_t_3
+ 1]);
545 k
-= (tp
[tp_index_t_3
+ 1]);
547 if ((e
& 16) !== 0) {
549 c
= tp
[tp_index_t_3
+ 2] + (/* (int) */b
& inflate_mask
[e
]);
554 // decode distance base of block to copy
555 while (k
< (15)) { // max bits for distance code
557 b
|= (z
.read_byte(p
++) & 0xff) << k
;
564 tp_index_t_3
= (tp_index
+ t
) * 3;
565 e
= tp
[tp_index_t_3
];
569 b
>>= (tp
[tp_index_t_3
+ 1]);
570 k
-= (tp
[tp_index_t_3
+ 1]);
572 if ((e
& 16) !== 0) {
573 // get extra bits to add to distance base
575 while (k
< (e
)) { // get extra bits (up to 13)
577 b
|= (z
.read_byte(p
++) & 0xff) << k
;
581 d
= tp
[tp_index_t_3
+ 2] + (b
& inflate_mask
[e
]);
588 if (q
>= d
) { // offset before dest
591 if (q
- r
> 0 && 2 > (q
- r
)) {
592 s
.window
[q
++] = s
.window
[r
++]; // minimum
595 s
.window
[q
++] = s
.window
[r
++]; // so unroll
600 s
.window
.set(s
.window
.subarray(r
, r
+ 2), q
);
605 } else { // else offset after destination
608 r
+= s
.end
; // force pointer in window
609 } while (r
< 0); // covers invalid distances
611 if (c
> e
) { // if source crosses,
612 c
-= e
; // wrapped copy
613 if (q
- r
> 0 && e
> (q
- r
)) {
615 s
.window
[q
++] = s
.window
[r
++];
618 s
.window
.set(s
.window
.subarray(r
, r
+ e
), q
);
623 r
= 0; // copy rest from start of window
628 // copy all or what's left
629 if (q
- r
> 0 && c
> (q
- r
)) {
631 s
.window
[q
++] = s
.window
[r
++];
634 s
.window
.set(s
.window
.subarray(r
, r
+ c
), q
);
640 } else if ((e
& 64) === 0) {
641 t
+= tp
[tp_index_t_3
+ 2];
642 t
+= (b
& inflate_mask
[e
]);
643 tp_index_t_3
= (tp_index
+ t
) * 3;
644 e
= tp
[tp_index_t_3
];
646 z
.msg
= "invalid distance code";
649 c
= (k
>> 3) < c
? k
>> 3 : c
;
657 z
.total_in
+= p
- z
.next_in_index
;
667 if ((e
& 64) === 0) {
668 t
+= tp
[tp_index_t_3
+ 2];
669 t
+= (b
& inflate_mask
[e
]);
670 tp_index_t_3
= (tp_index
+ t
) * 3;
671 if ((e
= tp
[tp_index_t_3
]) === 0) {
673 b
>>= (tp
[tp_index_t_3
+ 1]);
674 k
-= (tp
[tp_index_t_3
+ 1]);
676 s
.window
[q
++] = /* (byte) */tp
[tp_index_t_3
+ 2];
680 } else if ((e
& 32) !== 0) {
683 c
= (k
>> 3) < c
? k
>> 3 : c
;
691 z
.total_in
+= p
- z
.next_in_index
;
697 z
.msg
= "invalid literal/length code";
700 c
= (k
>> 3) < c
? k
>> 3 : c
;
708 z
.total_in
+= p
- z
.next_in_index
;
715 } while (m
>= 258 && n
>= 10);
717 // not enough input or output--restore pointers and return
719 c
= (k
>> 3) < c
? k
>> 3 : c
;
727 z
.total_in
+= p
- z
.next_in_index
;
734 that
.init = function(bl
, bd
, tl
, tl_index
, td
, td_index
) {
736 lbits
= /* (byte) */bl
;
737 dbits
= /* (byte) */bd
;
739 ltree_index
= tl_index
;
741 dtree_index
= td_index
;
745 that
.proc = function(s
, z
, r
) {
746 var j
; // temporary storage
747 var tindex
; // temporary pointer
748 var e
; // extra bits or operation
749 var b
= 0; // bit buffer
750 var k
= 0; // bits in bit buffer
751 var p
= 0; // input data pointer
752 var n
; // bytes available there
753 var q
; // output window write pointer
754 var m
; // bytes to end of window or read pointer
755 var f
; // pointer to copy strings from
757 // copy input/output information to locals (UPDATE macro restores)
763 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
765 // process input and output based on current state
768 // waiting for "i:"=input, "o:"=output, "x:"=nothing
769 case START
: // x: set up for LEN
770 if (m
>= 258 && n
>= 10) {
775 z
.total_in
+= p
- z
.next_in_index
;
778 r
= inflate_fast(lbits
, dbits
, ltree
, ltree_index
, dtree
, dtree_index
, s
, z
);
785 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
788 mode
= r
== Z_STREAM_END
? WASH
: BADCODE
;
794 tree_index
= ltree_index
;
797 case LEN
: // i: get length/literal/eob next
808 z
.total_in
+= p
- z
.next_in_index
;
811 return s
.inflate_flush(z
, r
);
814 b
|= (z
.read_byte(p
++) & 0xff) << k
;
818 tindex
= (tree_index
+ (b
& inflate_mask
[j
])) * 3;
820 b
>>>= (tree
[tindex
+ 1]);
821 k
-= (tree
[tindex
+ 1]);
825 if (e
=== 0) { // literal
826 lit
= tree
[tindex
+ 2];
830 if ((e
& 16) !== 0) { // length
832 len
= tree
[tindex
+ 2];
836 if ((e
& 64) === 0) { // next table
838 tree_index
= tindex
/ 3 + tree
[tindex
+ 2];
841 if ((e
& 32) !== 0) { // end of block
845 mode
= BADCODE
; // invalid code
846 z
.msg
= "invalid literal/length code";
852 z
.total_in
+= p
- z
.next_in_index
;
855 return s
.inflate_flush(z
, r
);
857 case LENEXT
: // i: getting length extra (have base)
868 z
.total_in
+= p
- z
.next_in_index
;
871 return s
.inflate_flush(z
, r
);
874 b
|= (z
.read_byte(p
++) & 0xff) << k
;
878 len
+= (b
& inflate_mask
[j
]);
885 tree_index
= dtree_index
;
887 case DIST
: // i: get distance next
898 z
.total_in
+= p
- z
.next_in_index
;
901 return s
.inflate_flush(z
, r
);
904 b
|= (z
.read_byte(p
++) & 0xff) << k
;
908 tindex
= (tree_index
+ (b
& inflate_mask
[j
])) * 3;
910 b
>>= tree
[tindex
+ 1];
911 k
-= tree
[tindex
+ 1];
914 if ((e
& 16) !== 0) { // distance
916 dist
= tree
[tindex
+ 2];
920 if ((e
& 64) === 0) { // next table
922 tree_index
= tindex
/ 3 + tree
[tindex
+ 2];
925 mode
= BADCODE
; // invalid code
926 z
.msg
= "invalid distance code";
932 z
.total_in
+= p
- z
.next_in_index
;
935 return s
.inflate_flush(z
, r
);
937 case DISTEXT
: // i: getting distance extra
948 z
.total_in
+= p
- z
.next_in_index
;
951 return s
.inflate_flush(z
, r
);
954 b
|= (z
.read_byte(p
++) & 0xff) << k
;
958 dist
+= (b
& inflate_mask
[j
]);
964 case COPY
: // o: copying bytes in window, waiting for space
966 while (f
< 0) { // modulo window size-"while" instead
967 f
+= s
.end
; // of "if" handles invalid distances
972 if (q
== s
.end
&& s
.read
!== 0) {
974 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
978 r
= s
.inflate_flush(z
, r
);
980 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
982 if (q
== s
.end
&& s
.read
!== 0) {
984 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
991 z
.total_in
+= p
- z
.next_in_index
;
994 return s
.inflate_flush(z
, r
);
999 s
.window
[q
++] = s
.window
[f
++];
1008 case LIT
: // o: got literal, waiting for output space
1010 if (q
== s
.end
&& s
.read
!== 0) {
1012 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
1016 r
= s
.inflate_flush(z
, r
);
1018 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
1020 if (q
== s
.end
&& s
.read
!== 0) {
1022 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
1028 z
.total_in
+= p
- z
.next_in_index
;
1029 z
.next_in_index
= p
;
1031 return s
.inflate_flush(z
, r
);
1037 s
.window
[q
++] = /* (byte) */lit
;
1042 case WASH
: // o: got eob, possibly more output
1043 if (k
> 7) { // return unused byte, if any
1046 p
--; // can always return one
1050 r
= s
.inflate_flush(z
, r
);
1052 m
= q
< s
.read
? s
.read
- q
- 1 : s
.end
- q
;
1054 if (s
.read
!= s
.write
) {
1058 z
.total_in
+= p
- z
.next_in_index
;
1059 z
.next_in_index
= p
;
1061 return s
.inflate_flush(z
, r
);
1069 z
.total_in
+= p
- z
.next_in_index
;
1070 z
.next_in_index
= p
;
1072 return s
.inflate_flush(z
, r
);
1074 case BADCODE
: // x: got error
1081 z
.total_in
+= p
- z
.next_in_index
;
1082 z
.next_in_index
= p
;
1084 return s
.inflate_flush(z
, r
);
1092 z
.total_in
+= p
- z
.next_in_index
;
1093 z
.next_in_index
= p
;
1095 return s
.inflate_flush(z
, r
);
1100 that
.free = function() {
1108 // Table for deflate from PKZIP's appnote.txt.
1109 var border
= [ // Order of the bit length code lengths
1110 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
1112 var TYPE
= 0; // get type bits (3, including end bit)
1113 var LENS
= 1; // get lengths for stored
1114 var STORED
= 2;// processing stored block
1115 var TABLE
= 3; // get table lengths
1116 var BTREE
= 4; // get bit lengths tree for a dynamic
1118 var DTREE
= 5; // get length, distance trees for a
1120 var CODES
= 6; // processing fixed or dynamic block
1121 var DRY
= 7; // output remaining window bytes
1122 var DONELOCKS
= 8; // finished last block, done
1123 var BADBLOCKS
= 9; // ot a data error--stuck here
1125 function InfBlocks(z
, w
) {
1128 var mode
= TYPE
; // current inflate_block mode
1130 var left
= 0; // if STORED, bytes left to copy
1132 var table
= 0; // table lengths (14 bits)
1133 var index
= 0; // index into blens (or border)
1134 var blens
; // bit lengths of codes
1135 var bb
= [ 0 ]; // bit length tree depth
1136 var tb
= [ 0 ]; // bit length decoding tree
1138 var codes
= new InfCodes(); // if CODES, current state
1140 var last
= 0; // true if this block is the last block
1142 var hufts
= new Int32Array(MANY
* 3); // single malloc for tree space
1143 var check
= 0; // check on output
1144 var inftree
= new InfTree();
1146 that
.bitk
= 0; // bits in bit buffer
1147 that
.bitb
= 0; // bit buffer
1148 that
.window
= new Uint8Array(w
); // sliding window
1149 that
.end
= w
; // one byte after sliding window
1150 that
.read
= 0; // window read pointer
1151 that
.write
= 0; // window write pointer
1153 that
.reset = function(z
, c
) {
1156 // if (mode == BTREE || mode == DTREE) {
1158 if (mode
== CODES
) {
1164 that
.read
= that
.write
= 0;
1167 that
.reset(z
, null);
1169 // copy as much as possible from the sliding window to the output area
1170 that
.inflate_flush = function(z
, r
) {
1175 // local copies of source and destination pointers
1176 p
= z
.next_out_index
;
1179 // compute number of bytes to copy as far as end of window
1180 n
= /* (int) */((q
<= that
.write
? that
.write
: that
.end
) - q
);
1181 if (n
> z
.avail_out
)
1183 if (n
!== 0 && r
== Z_BUF_ERROR
)
1190 // copy as far as end of window
1191 z
.next_out
.set(that
.window
.subarray(q
, q
+ n
), p
);
1195 // see if more to copy at beginning of window
1196 if (q
== that
.end
) {
1199 if (that
.write
== that
.end
)
1202 // compute bytes to copy
1204 if (n
> z
.avail_out
)
1206 if (n
!== 0 && r
== Z_BUF_ERROR
)
1214 z
.next_out
.set(that
.window
.subarray(q
, q
+ n
), p
);
1220 z
.next_out_index
= p
;
1227 that
.proc = function(z
, r
) {
1228 var t
; // temporary storage
1229 var b
; // bit buffer
1230 var k
; // bits in bit buffer
1231 var p
; // input data pointer
1232 var n
; // bytes available there
1233 var q
; // output window write pointer
1234 var m
; // bytes to end of window or read pointer
1238 // copy input/output information to locals (UPDATE macro restores)
1240 p
= z
.next_in_index
;
1247 m
= /* (int) */(q
< that
.read
? that
.read
- q
- 1 : that
.end
- q
);
1250 // process input based on current state
1263 z
.total_in
+= p
- z
.next_in_index
;
1264 z
.next_in_index
= p
;
1266 return that
.inflate_flush(z
, r
);
1269 b
|= (z
.read_byte(p
++) & 0xff) << k
;
1272 t
= /* (int) */(b
& 7);
1281 t
= k
& 7; // go to byte boundary
1287 mode
= LENS
; // get length of stored block
1291 var bl
= []; // new Array(1);
1292 var bd
= []; // new Array(1);
1293 var tl
= [ [] ]; // new Array(1);
1294 var td
= [ [] ]; // new Array(1);
1296 InfTree
.inflate_trees_fixed(bl
, bd
, tl
, td
);
1297 codes
.init(bl
[0], bd
[0], tl
[0], 0, td
[0], 0);
1323 z
.msg
= "invalid block type";
1329 z
.total_in
+= p
- z
.next_in_index
;
1330 z
.next_in_index
= p
;
1332 return that
.inflate_flush(z
, r
);
1344 z
.total_in
+= p
- z
.next_in_index
;
1345 z
.next_in_index
= p
;
1347 return that
.inflate_flush(z
, r
);
1350 b
|= (z
.read_byte(p
++) & 0xff) << k
;
1354 if ((((~b
) >>> 16) & 0xffff) != (b
& 0xffff)) {
1356 z
.msg
= "invalid stored block lengths";
1362 z
.total_in
+= p
- z
.next_in_index
;
1363 z
.next_in_index
= p
;
1365 return that
.inflate_flush(z
, r
);
1367 left
= (b
& 0xffff);
1368 b
= k
= 0; // dump bits
1369 mode
= left
!== 0 ? STORED
: (last
!== 0 ? DRY
: TYPE
);
1376 z
.total_in
+= p
- z
.next_in_index
;
1377 z
.next_in_index
= p
;
1379 return that
.inflate_flush(z
, r
);
1383 if (q
== that
.end
&& that
.read
!== 0) {
1385 m
= /* (int) */(q
< that
.read
? that
.read
- q
- 1 : that
.end
- q
);
1389 r
= that
.inflate_flush(z
, r
);
1391 m
= /* (int) */(q
< that
.read
? that
.read
- q
- 1 : that
.end
- q
);
1392 if (q
== that
.end
&& that
.read
!== 0) {
1394 m
= /* (int) */(q
< that
.read
? that
.read
- q
- 1 : that
.end
- q
);
1400 z
.total_in
+= p
- z
.next_in_index
;
1401 z
.next_in_index
= p
;
1403 return that
.inflate_flush(z
, r
);
1414 that
.window
.set(z
.read_buf(p
, t
), q
);
1419 if ((left
-= t
) !== 0)
1421 mode
= last
!== 0 ? DRY
: TYPE
;
1432 z
.total_in
+= p
- z
.next_in_index
;
1433 z
.next_in_index
= p
;
1435 return that
.inflate_flush(z
, r
);
1439 b
|= (z
.read_byte(p
++) & 0xff) << k
;
1443 table
= t
= (b
& 0x3fff);
1444 if ((t
& 0x1f) > 29 || ((t
>> 5) & 0x1f) > 29) {
1446 z
.msg
= "too many length or distance symbols";
1452 z
.total_in
+= p
- z
.next_in_index
;
1453 z
.next_in_index
= p
;
1455 return that
.inflate_flush(z
, r
);
1457 t
= 258 + (t
& 0x1f) + ((t
>> 5) & 0x1f);
1458 if (!blens
|| blens
.length
< t
) {
1459 blens
= []; // new Array(t);
1461 for (i
= 0; i
< t
; i
++) {
1474 while (index
< 4 + (table
>>> 10)) {
1482 z
.total_in
+= p
- z
.next_in_index
;
1483 z
.next_in_index
= p
;
1485 return that
.inflate_flush(z
, r
);
1488 b
|= (z
.read_byte(p
++) & 0xff) << k
;
1492 blens
[border
[index
++]] = b
& 7;
1500 while (index
< 19) {
1501 blens
[border
[index
++]] = 0;
1505 t
= inftree
.inflate_trees_bits(blens
, bb
, tb
, hufts
, z
);
1508 if (r
== Z_DATA_ERROR
) {
1516 z
.total_in
+= p
- z
.next_in_index
;
1517 z
.next_in_index
= p
;
1519 return that
.inflate_flush(z
, r
);
1527 if (!(index
< 258 + (t
& 0x1f) + ((t
>> 5) & 0x1f))) {
1542 z
.total_in
+= p
- z
.next_in_index
;
1543 z
.next_in_index
= p
;
1545 return that
.inflate_flush(z
, r
);
1548 b
|= (z
.read_byte(p
++) & 0xff) << k
;
1552 // if (tb[0] == -1) {
1553 // System.err.println("null...");
1556 t
= hufts
[(tb
[0] + (b
& inflate_mask
[t
])) * 3 + 1];
1557 c
= hufts
[(tb
[0] + (b
& inflate_mask
[t
])) * 3 + 2];
1563 } else { // c == 16..18
1564 i
= c
== 18 ? 7 : c
- 14;
1565 j
= c
== 18 ? 11 : 3;
1567 while (k
< (t
+ i
)) {
1574 z
.total_in
+= p
- z
.next_in_index
;
1575 z
.next_in_index
= p
;
1577 return that
.inflate_flush(z
, r
);
1580 b
|= (z
.read_byte(p
++) & 0xff) << k
;
1587 j
+= (b
& inflate_mask
[i
]);
1594 if (i
+ j
> 258 + (t
& 0x1f) + ((t
>> 5) & 0x1f) || (c
== 16 && i
< 1)) {
1597 z
.msg
= "invalid bit length repeat";
1603 z
.total_in
+= p
- z
.next_in_index
;
1604 z
.next_in_index
= p
;
1606 return that
.inflate_flush(z
, r
);
1609 c
= c
== 16 ? blens
[i
- 1] : 0;
1612 } while (--j
!== 0);
1619 var bl_
= []; // new Array(1);
1620 var bd_
= []; // new Array(1);
1621 var tl_
= []; // new Array(1);
1622 var td_
= []; // new Array(1);
1623 bl_
[0] = 9; // must be <= 9 for lookahead assumptions
1624 bd_
[0] = 6; // must be <= 9 for lookahead assumptions
1627 t
= inftree
.inflate_trees_dynamic(257 + (t
& 0x1f), 1 + ((t
>> 5) & 0x1f), blens
, bl_
, bd_
, tl_
, td_
, hufts
, z
);
1630 if (t
== Z_DATA_ERROR
) {
1639 z
.total_in
+= p
- z
.next_in_index
;
1640 z
.next_in_index
= p
;
1642 return that
.inflate_flush(z
, r
);
1644 codes
.init(bl_
[0], bd_
[0], hufts
, tl_
[0], hufts
, td_
[0]);
1651 z
.total_in
+= p
- z
.next_in_index
;
1652 z
.next_in_index
= p
;
1655 if ((r
= codes
.proc(that
, z
, r
)) != Z_STREAM_END
) {
1656 return that
.inflate_flush(z
, r
);
1661 p
= z
.next_in_index
;
1666 m
= /* (int) */(q
< that
.read
? that
.read
- q
- 1 : that
.end
- q
);
1675 r
= that
.inflate_flush(z
, r
);
1677 m
= /* (int) */(q
< that
.read
? that
.read
- q
- 1 : that
.end
- q
);
1678 if (that
.read
!= that
.write
) {
1682 z
.total_in
+= p
- z
.next_in_index
;
1683 z
.next_in_index
= p
;
1685 return that
.inflate_flush(z
, r
);
1694 z
.total_in
+= p
- z
.next_in_index
;
1695 z
.next_in_index
= p
;
1697 return that
.inflate_flush(z
, r
);
1704 z
.total_in
+= p
- z
.next_in_index
;
1705 z
.next_in_index
= p
;
1707 return that
.inflate_flush(z
, r
);
1715 z
.total_in
+= p
- z
.next_in_index
;
1716 z
.next_in_index
= p
;
1718 return that
.inflate_flush(z
, r
);
1723 that
.free = function(z
) {
1724 that
.reset(z
, null);
1730 that
.set_dictionary = function(d
, start
, n
) {
1731 that
.window
.set(d
.subarray(start
, start
+ n
), 0);
1732 that
.read
= that
.write
= n
;
1735 // Returns true if inflate is currently at the end of a block generated
1736 // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
1737 that
.sync_point = function() {
1738 return mode
== LENS
? 1 : 0;
1745 // preset dictionary flag in zlib header
1746 var PRESET_DICT
= 0x20;
1750 var METHOD
= 0; // waiting for method byte
1751 var FLAG
= 1; // waiting for flag byte
1752 var DICT4
= 2; // four dictionary check bytes to go
1753 var DICT3
= 3; // three dictionary check bytes to go
1754 var DICT2
= 4; // two dictionary check bytes to go
1755 var DICT1
= 5; // one dictionary check byte to go
1756 var DICT0
= 6; // waiting for inflateSetDictionary
1757 var BLOCKS
= 7; // decompressing blocks
1758 var DONE
= 12; // finished check, done
1759 var BAD
= 13; // got an error--stay here
1761 var mark
= [ 0, 0, 0xff, 0xff ];
1763 function Inflate() {
1766 that
.mode
= 0; // current inflate mode
1768 // mode dependent information
1769 that
.method
= 0; // if FLAGS, method byte
1771 // if CHECK, check values to compare
1772 that
.was
= [ 0 ]; // new Array(1); // computed check value
1773 that
.need
= 0; // stream check value
1775 // if BAD, inflateSync's marker bytes count
1778 // mode independent information
1779 that
.wbits
= 0; // log2(window size) (8..15, defaults to 15)
1781 // this.blocks; // current inflate_blocks state
1783 function inflateReset(z
) {
1784 if (!z
|| !z
.istate
)
1785 return Z_STREAM_ERROR
;
1787 z
.total_in
= z
.total_out
= 0;
1789 z
.istate
.mode
= BLOCKS
;
1790 z
.istate
.blocks
.reset(z
, null);
1794 that
.inflateEnd = function(z
) {
1796 that
.blocks
.free(z
);
1798 // ZFREE(z, z->state);
1802 that
.inflateInit = function(z
, w
) {
1807 if (w
< 8 || w
> 15) {
1809 return Z_STREAM_ERROR
;
1813 z
.istate
.blocks
= new InfBlocks(z
, 1 << w
);
1820 that
.inflate = function(z
, f
) {
1824 if (!z
|| !z
.istate
|| !z
.next_in
)
1825 return Z_STREAM_ERROR
;
1826 f
= f
== Z_FINISH
? Z_BUF_ERROR
: Z_OK
;
1829 // System.out.println("mode: "+z.istate.mode);
1830 switch (z
.istate
.mode
) {
1833 if (z
.avail_in
=== 0)
1839 if (((z
.istate
.method
= z
.read_byte(z
.next_in_index
++)) & 0xf) != Z_DEFLATED
) {
1840 z
.istate
.mode
= BAD
;
1841 z
.msg
= "unknown compression method";
1842 z
.istate
.marker
= 5; // can't try inflateSync
1845 if ((z
.istate
.method
>> 4) + 8 > z
.istate
.wbits
) {
1846 z
.istate
.mode
= BAD
;
1847 z
.msg
= "invalid window size";
1848 z
.istate
.marker
= 5; // can't try inflateSync
1851 z
.istate
.mode
= FLAG
;
1854 if (z
.avail_in
=== 0)
1860 b
= (z
.read_byte(z
.next_in_index
++)) & 0xff;
1862 if ((((z
.istate
.method
<< 8) + b
) % 31) !== 0) {
1863 z
.istate
.mode
= BAD
;
1864 z
.msg
= "incorrect header check";
1865 z
.istate
.marker
= 5; // can't try inflateSync
1869 if ((b
& PRESET_DICT
) === 0) {
1870 z
.istate
.mode
= BLOCKS
;
1873 z
.istate
.mode
= DICT4
;
1876 if (z
.avail_in
=== 0)
1882 z
.istate
.need
= ((z
.read_byte(z
.next_in_index
++) & 0xff) << 24) & 0xff000000;
1883 z
.istate
.mode
= DICT3
;
1886 if (z
.avail_in
=== 0)
1892 z
.istate
.need
+= ((z
.read_byte(z
.next_in_index
++) & 0xff) << 16) & 0xff0000;
1893 z
.istate
.mode
= DICT2
;
1896 if (z
.avail_in
=== 0)
1902 z
.istate
.need
+= ((z
.read_byte(z
.next_in_index
++) & 0xff) << 8) & 0xff00;
1903 z
.istate
.mode
= DICT1
;
1906 if (z
.avail_in
=== 0)
1912 z
.istate
.need
+= (z
.read_byte(z
.next_in_index
++) & 0xff);
1913 z
.istate
.mode
= DICT0
;
1916 z
.istate
.mode
= BAD
;
1917 z
.msg
= "need dictionary";
1918 z
.istate
.marker
= 0; // can try inflateSync
1919 return Z_STREAM_ERROR
;
1922 r
= z
.istate
.blocks
.proc(z
, r
);
1923 if (r
== Z_DATA_ERROR
) {
1924 z
.istate
.mode
= BAD
;
1925 z
.istate
.marker
= 0; // can try inflateSync
1931 if (r
!= Z_STREAM_END
) {
1935 z
.istate
.blocks
.reset(z
, z
.istate
.was
);
1936 z
.istate
.mode
= DONE
;
1938 return Z_STREAM_END
;
1940 return Z_DATA_ERROR
;
1942 return Z_STREAM_ERROR
;
1947 that
.inflateSetDictionary = function(z
, dictionary
, dictLength
) {
1949 var length
= dictLength
;
1950 if (!z
|| !z
.istate
|| z
.istate
.mode
!= DICT0
)
1951 return Z_STREAM_ERROR
;
1953 if (length
>= (1 << z
.istate
.wbits
)) {
1954 length
= (1 << z
.istate
.wbits
) - 1;
1955 index
= dictLength
- length
;
1957 z
.istate
.blocks
.set_dictionary(dictionary
, index
, length
);
1958 z
.istate
.mode
= BLOCKS
;
1962 that
.inflateSync = function(z
) {
1963 var n
; // number of bytes to look at
1964 var p
; // pointer to bytes
1965 var m
; // number of marker bytes found in a row
1966 var r
, w
; // temporaries to save total_in and total_out
1969 if (!z
|| !z
.istate
)
1970 return Z_STREAM_ERROR
;
1971 if (z
.istate
.mode
!= BAD
) {
1972 z
.istate
.mode
= BAD
;
1973 z
.istate
.marker
= 0;
1975 if ((n
= z
.avail_in
) === 0)
1977 p
= z
.next_in_index
;
1978 m
= z
.istate
.marker
;
1981 while (n
!== 0 && m
< 4) {
1982 if (z
.read_byte(p
) == mark
[m
]) {
1984 } else if (z
.read_byte(p
) !== 0) {
1994 z
.total_in
+= p
- z
.next_in_index
;
1995 z
.next_in_index
= p
;
1997 z
.istate
.marker
= m
;
1999 // return no joy or set up to restart on a new block
2001 return Z_DATA_ERROR
;
2008 z
.istate
.mode
= BLOCKS
;
2012 // Returns true if inflate is currently at the end of a block generated
2013 // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
2014 // implementation to provide an additional safety check. PPP uses
2016 // but removes the length bytes of the resulting empty stored block. When
2017 // decompressing, PPP checks that at the end of input packet, inflate is
2018 // waiting for these length bytes.
2019 that
.inflateSyncPoint = function(z
) {
2020 if (!z
|| !z
.istate
|| !z
.istate
.blocks
)
2021 return Z_STREAM_ERROR
;
2022 return z
.istate
.blocks
.sync_point();
2028 function ZStream() {
2031 ZStream
.prototype = {
2032 inflateInit : function(bits
) {
2034 that
.istate
= new Inflate();
2037 return that
.istate
.inflateInit(that
, bits
);
2040 inflate : function(f
) {
2043 return Z_STREAM_ERROR
;
2044 return that
.istate
.inflate(that
, f
);
2047 inflateEnd : function() {
2050 return Z_STREAM_ERROR
;
2051 var ret
= that
.istate
.inflateEnd(that
);
2056 inflateSync : function() {
2059 return Z_STREAM_ERROR
;
2060 return that
.istate
.inflateSync(that
);
2062 inflateSetDictionary : function(dictionary
, dictLength
) {
2065 return Z_STREAM_ERROR
;
2066 return that
.istate
.inflateSetDictionary(that
, dictionary
, dictLength
);
2068 read_byte : function(start
) {
2070 return that
.next_in
.subarray(start
, start
+ 1)[0];
2072 read_buf : function(start
, size
) {
2074 return that
.next_in
.subarray(start
, start
+ size
);
2080 function Inflater() {
2082 var z
= new ZStream();
2084 var flush
= Z_NO_FLUSH
;
2085 var buf
= new Uint8Array(bufsize
);
2086 var nomoreinput
= false;
2091 that
.append = function(data
, onprogress
) {
2092 var err
, buffers
= [], lastIndex
= 0, bufferIndex
= 0, bufferSize
= 0, array
;
2093 if (data
.length
=== 0)
2095 z
.next_in_index
= 0;
2097 z
.avail_in
= data
.length
;
2099 z
.next_out_index
= 0;
2100 z
.avail_out
= bufsize
;
2101 if ((z
.avail_in
=== 0) && (!nomoreinput
)) { // if buffer is empty and more input is available, refill it
2102 z
.next_in_index
= 0;
2105 err
= z
.inflate(flush
);
2106 if (nomoreinput
&& (err
== Z_BUF_ERROR
))
2108 if (err
!= Z_OK
&& err
!= Z_STREAM_END
)
2109 throw "inflating: " + z
.msg
;
2110 if ((nomoreinput
|| err
== Z_STREAM_END
) && (z
.avail_in
== data
.length
))
2112 if (z
.next_out_index
)
2113 if (z
.next_out_index
== bufsize
)
2114 buffers
.push(new Uint8Array(buf
));
2116 buffers
.push(new Uint8Array(buf
.subarray(0, z
.next_out_index
)));
2117 bufferSize
+= z
.next_out_index
;
2118 if (onprogress
&& z
.next_in_index
> 0 && z
.next_in_index
!= lastIndex
) {
2119 onprogress(z
.next_in_index
);
2120 lastIndex
= z
.next_in_index
;
2122 } while (z
.avail_in
> 0 || z
.avail_out
=== 0);
2123 array
= new Uint8Array(bufferSize
);
2124 buffers
.forEach(function(chunk
) {
2125 array
.set(chunk
, bufferIndex
);
2126 bufferIndex
+= chunk
.length
;
2130 that
.flush = function() {
2138 obj
.zip
.Inflater
= Inflater
;
2140 inflater
= new Inflater();
2141 obj
.addEventListener("message", function(event
) {
2142 var message
= event
.data
;
2147 data
: inflater
.append(message
.data
, function(current
) {
2154 if (message
.flush
) {