SECURITY: resources: Patch jQuery 3.3.1 for CVE-2019-11358
[lhc/web/wiklou.git] / resources / lib / easy-deflate / inflate.js
1 /*
2 Copyright (c) 2013 Gildas Lormeau. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 1. Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9
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.
13
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.
16
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.
27 */
28
29 /*
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.
34 */
35
36 (function(obj) {
37
38 // Global
39 var MAX_BITS = 15;
40
41 var Z_OK = 0;
42 var Z_STREAM_END = 1;
43 var Z_NEED_DICT = 2;
44 var Z_STREAM_ERROR = -2;
45 var Z_DATA_ERROR = -3;
46 var Z_MEM_ERROR = -4;
47 var Z_BUF_ERROR = -5;
48
49 var inflate_mask = [ 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff,
50 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff ];
51
52 var MANY = 1440;
53
54 // JZlib version : "1.0.2"
55 var Z_NO_FLUSH = 0;
56 var Z_FINISH = 4;
57
58 // InfTree
59 var fixed_bl = 9;
60 var fixed_bd = 5;
61
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 ];
102
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 ];
106
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
110 ];
111
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 ];
114
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 ];
117
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
120
121 function InfTree() {
122 var that = this;
123
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
130
131 function huft_build(b, // code lengths in bits (all assumed <=
132 // BMAX)
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
142 ) {
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,
145 // Z_BUF_ERROR
146 // if the given code set is incomplete (the tables are still built in
147 // this
148 // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set
149 // of
150 // lengths), or Z_MEM_ERROR if not enough memory.
151
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
157 var j; // counter
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
167
168 // Generate counts for each bit length
169
170 p = 0;
171 i = n;
172 do {
173 c[b[bindex + p]]++;
174 p++;
175 i--; // assume all entries <= BMAX
176 } while (i !== 0);
177
178 if (c[0] == n) { // null input--all zero length codes
179 t[0] = -1;
180 m[0] = 0;
181 return Z_OK;
182 }
183
184 // Find minimum and maximum length, bound *m by those
185 l = m[0];
186 for (j = 1; j <= BMAX; j++)
187 if (c[j] !== 0)
188 break;
189 k = j; // minimum code length
190 if (l < j) {
191 l = j;
192 }
193 for (i = BMAX; i !== 0; i--) {
194 if (c[i] !== 0)
195 break;
196 }
197 g = i; // maximum code length
198 if (l > i) {
199 l = i;
200 }
201 m[0] = l;
202
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) {
206 return Z_DATA_ERROR;
207 }
208 }
209 if ((y -= c[i]) < 0) {
210 return Z_DATA_ERROR;
211 }
212 c[i] += y;
213
214 // Generate starting offsets into the value table for each length
215 x[1] = j = 0;
216 p = 1;
217 xp = 2;
218 while (--i !== 0) { // note that i == g from above
219 x[xp] = (j += c[p]);
220 xp++;
221 p++;
222 }
223
224 // Make a table of values in order of bit lengths
225 i = 0;
226 p = 0;
227 do {
228 if ((j = b[bindex + p]) !== 0) {
229 v[x[j]++] = i;
230 }
231 p++;
232 } while (++i < n);
233 n = x[g]; // set n to length of v
234
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
241 q = 0; // ditto
242 z = 0; // ditto
243
244 // go through the bit lengths (k already is bits in shortest code)
245 for (; k <= g; k++) {
246 a = c[k];
247 while (a-- !== 0) {
248 // here i is the Huffman code of length k bits for value *p
249 // make tables up to required level
250 while (k > w + l) {
251 h++;
252 w += l; // previous table always l bits
253 // compute minimum size table less than or equal to l bits
254 z = g - w;
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
257 // too few codes for
258 // k-w bit table
259 f -= a + 1; // deduct codes from patterns left
260 xp = k;
261 if (j < z) {
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
266 }
267 }
268 }
269 z = 1 << j; // table entries for j-bit table
270
271 // allocate new table
272 if (hn[0] + z > MANY) { // (note: doesn't matter for fixed)
273 return Z_DATA_ERROR; // overflow of MANY
274 }
275 u[h] = q = /* hp+ */hn[0]; // DEBUG
276 hn[0] += z;
277
278 // connect to last table, if there is one
279 if (h !== 0) {
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
283 j = i >>> (w - l);
284 r[2] = /* (int) */(q - u[h - 1] - j); // offset to this table
285 hp.set(r, (u[h - 1] + j) * 3);
286 // to
287 // last
288 // table
289 } else {
290 t[0] = q; // first table is returned result
291 }
292 }
293
294 // set up table entry in r
295 r[1] = /* (byte) */(k - w);
296 if (p >= n) {
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
300 // end-of-block
301 r[2] = v[p++]; // simple code is just the value
302 } else {
303 r[0] = /* (byte) */(e[v[p] - s] + 16 + 64); // non-simple--look
304 // up in lists
305 r[2] = d[v[p++] - s];
306 }
307
308 // fill code-like entries with r
309 f = 1 << (k - w);
310 for (j = i >>> w; j < z; j += f) {
311 hp.set(r, (q + j) * 3);
312 }
313
314 // backwards increment the k-bit code i
315 for (j = 1 << (k - 1); (i & j) !== 0; j >>>= 1) {
316 i ^= j;
317 }
318 i ^= j;
319
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
324 w -= l;
325 mask = (1 << w) - 1;
326 }
327 }
328 }
329 // Return Z_BUF_ERROR if we were given an incomplete table
330 return y !== 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
331 }
332
333 function initWorkArea(vsize) {
334 var i;
335 if (!hn) {
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);
342 }
343 if (v.length < vsize) {
344 v = []; // new Array(vsize);
345 }
346 for (i = 0; i < vsize; i++) {
347 v[i] = 0;
348 }
349 for (i = 0; i < BMAX + 1; i++) {
350 c[i] = 0;
351 }
352 for (i = 0; i < 3; i++) {
353 r[i] = 0;
354 }
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);
359 }
360
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
365 z // for messages
366 ) {
367 var result;
368 initWorkArea(19);
369 hn[0] = 0;
370 result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
371
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;
377 }
378 return result;
379 };
380
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
389 z // for messages
390 ) {
391 var result;
392
393 // build literal/length tree
394 initWorkArea(288);
395 hn[0] = 0;
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;
403 }
404 return result;
405 }
406
407 // build distance tree
408 initWorkArea(288);
409 result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
410
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;
420 }
421 return result;
422 }
423
424 return Z_OK;
425 };
426
427 }
428
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
433 ) {
434 bl[0] = fixed_bl;
435 bd[0] = fixed_bd;
436 tl[0] = fixed_tl;
437 td[0] = fixed_td;
438 return Z_OK;
439 };
440
441 // InfCodes
442
443 // waiting for "i:"=input,
444 // "o:"=output,
445 // "x:"=nothing
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
452 // for space
453 var LIT = 6; // o: got literal, waiting for output
454 // space
455 var WASH = 7; // o: got eob, possibly still output
456 // waiting
457 var END = 8; // x: got eob and all data flushed
458 var BADCODE = 9;// x: got error
459
460 function InfCodes() {
461 var that = this;
462
463 var mode; // current inflate_codes mode
464
465 // mode dependent information
466 var len = 0;
467
468 var tree; // pointer into tree
469 var tree_index = 0;
470 var need = 0; // bits needed
471
472 var lit = 0;
473
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
477
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
484
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.
489
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
495 var b; // bit buffer
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
506
507 var tp_index_t_3; // (tp_index+t)*3
508
509 // load input, output, bit values
510 p = z.next_in_index;
511 n = z.avail_in;
512 b = s.bitb;
513 k = s.bitk;
514 q = s.write;
515 m = q < s.read ? s.read - q - 1 : s.end - q;
516
517 // initialize masks
518 ml = inflate_mask[bl];
519 md = inflate_mask[bd];
520
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
525 n--;
526 b |= (z.read_byte(p++) & 0xff) << k;
527 k += 8;
528 }
529
530 t = b & ml;
531 tp = tl;
532 tp_index = tl_index;
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]);
537
538 s.window[q++] = /* (byte) */tp[tp_index_t_3 + 2];
539 m--;
540 continue;
541 }
542 do {
543
544 b >>= (tp[tp_index_t_3 + 1]);
545 k -= (tp[tp_index_t_3 + 1]);
546
547 if ((e & 16) !== 0) {
548 e &= 15;
549 c = tp[tp_index_t_3 + 2] + (/* (int) */b & inflate_mask[e]);
550
551 b >>= e;
552 k -= e;
553
554 // decode distance base of block to copy
555 while (k < (15)) { // max bits for distance code
556 n--;
557 b |= (z.read_byte(p++) & 0xff) << k;
558 k += 8;
559 }
560
561 t = b & md;
562 tp = td;
563 tp_index = td_index;
564 tp_index_t_3 = (tp_index + t) * 3;
565 e = tp[tp_index_t_3];
566
567 do {
568
569 b >>= (tp[tp_index_t_3 + 1]);
570 k -= (tp[tp_index_t_3 + 1]);
571
572 if ((e & 16) !== 0) {
573 // get extra bits to add to distance base
574 e &= 15;
575 while (k < (e)) { // get extra bits (up to 13)
576 n--;
577 b |= (z.read_byte(p++) & 0xff) << k;
578 k += 8;
579 }
580
581 d = tp[tp_index_t_3 + 2] + (b & inflate_mask[e]);
582
583 b >>= (e);
584 k -= (e);
585
586 // do the copy
587 m -= c;
588 if (q >= d) { // offset before dest
589 // just copy
590 r = q - d;
591 if (q - r > 0 && 2 > (q - r)) {
592 s.window[q++] = s.window[r++]; // minimum
593 // count is
594 // three,
595 s.window[q++] = s.window[r++]; // so unroll
596 // loop a
597 // little
598 c -= 2;
599 } else {
600 s.window.set(s.window.subarray(r, r + 2), q);
601 q += 2;
602 r += 2;
603 c -= 2;
604 }
605 } else { // else offset after destination
606 r = q - d;
607 do {
608 r += s.end; // force pointer in window
609 } while (r < 0); // covers invalid distances
610 e = s.end - r;
611 if (c > e) { // if source crosses,
612 c -= e; // wrapped copy
613 if (q - r > 0 && e > (q - r)) {
614 do {
615 s.window[q++] = s.window[r++];
616 } while (--e !== 0);
617 } else {
618 s.window.set(s.window.subarray(r, r + e), q);
619 q += e;
620 r += e;
621 e = 0;
622 }
623 r = 0; // copy rest from start of window
624 }
625
626 }
627
628 // copy all or what's left
629 if (q - r > 0 && c > (q - r)) {
630 do {
631 s.window[q++] = s.window[r++];
632 } while (--c !== 0);
633 } else {
634 s.window.set(s.window.subarray(r, r + c), q);
635 q += c;
636 r += c;
637 c = 0;
638 }
639 break;
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];
645 } else {
646 z.msg = "invalid distance code";
647
648 c = z.avail_in - n;
649 c = (k >> 3) < c ? k >> 3 : c;
650 n += c;
651 p -= c;
652 k -= c << 3;
653
654 s.bitb = b;
655 s.bitk = k;
656 z.avail_in = n;
657 z.total_in += p - z.next_in_index;
658 z.next_in_index = p;
659 s.write = q;
660
661 return Z_DATA_ERROR;
662 }
663 } while (true);
664 break;
665 }
666
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) {
672
673 b >>= (tp[tp_index_t_3 + 1]);
674 k -= (tp[tp_index_t_3 + 1]);
675
676 s.window[q++] = /* (byte) */tp[tp_index_t_3 + 2];
677 m--;
678 break;
679 }
680 } else if ((e & 32) !== 0) {
681
682 c = z.avail_in - n;
683 c = (k >> 3) < c ? k >> 3 : c;
684 n += c;
685 p -= c;
686 k -= c << 3;
687
688 s.bitb = b;
689 s.bitk = k;
690 z.avail_in = n;
691 z.total_in += p - z.next_in_index;
692 z.next_in_index = p;
693 s.write = q;
694
695 return Z_STREAM_END;
696 } else {
697 z.msg = "invalid literal/length code";
698
699 c = z.avail_in - n;
700 c = (k >> 3) < c ? k >> 3 : c;
701 n += c;
702 p -= c;
703 k -= c << 3;
704
705 s.bitb = b;
706 s.bitk = k;
707 z.avail_in = n;
708 z.total_in += p - z.next_in_index;
709 z.next_in_index = p;
710 s.write = q;
711
712 return Z_DATA_ERROR;
713 }
714 } while (true);
715 } while (m >= 258 && n >= 10);
716
717 // not enough input or output--restore pointers and return
718 c = z.avail_in - n;
719 c = (k >> 3) < c ? k >> 3 : c;
720 n += c;
721 p -= c;
722 k -= c << 3;
723
724 s.bitb = b;
725 s.bitk = k;
726 z.avail_in = n;
727 z.total_in += p - z.next_in_index;
728 z.next_in_index = p;
729 s.write = q;
730
731 return Z_OK;
732 }
733
734 that.init = function(bl, bd, tl, tl_index, td, td_index) {
735 mode = START;
736 lbits = /* (byte) */bl;
737 dbits = /* (byte) */bd;
738 ltree = tl;
739 ltree_index = tl_index;
740 dtree = td;
741 dtree_index = td_index;
742 tree = null;
743 };
744
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
756
757 // copy input/output information to locals (UPDATE macro restores)
758 p = z.next_in_index;
759 n = z.avail_in;
760 b = s.bitb;
761 k = s.bitk;
762 q = s.write;
763 m = q < s.read ? s.read - q - 1 : s.end - q;
764
765 // process input and output based on current state
766 while (true) {
767 switch (mode) {
768 // waiting for "i:"=input, "o:"=output, "x:"=nothing
769 case START: // x: set up for LEN
770 if (m >= 258 && n >= 10) {
771
772 s.bitb = b;
773 s.bitk = k;
774 z.avail_in = n;
775 z.total_in += p - z.next_in_index;
776 z.next_in_index = p;
777 s.write = q;
778 r = inflate_fast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, s, z);
779
780 p = z.next_in_index;
781 n = z.avail_in;
782 b = s.bitb;
783 k = s.bitk;
784 q = s.write;
785 m = q < s.read ? s.read - q - 1 : s.end - q;
786
787 if (r != Z_OK) {
788 mode = r == Z_STREAM_END ? WASH : BADCODE;
789 break;
790 }
791 }
792 need = lbits;
793 tree = ltree;
794 tree_index = ltree_index;
795
796 mode = LEN;
797 case LEN: // i: get length/literal/eob next
798 j = need;
799
800 while (k < (j)) {
801 if (n !== 0)
802 r = Z_OK;
803 else {
804
805 s.bitb = b;
806 s.bitk = k;
807 z.avail_in = n;
808 z.total_in += p - z.next_in_index;
809 z.next_in_index = p;
810 s.write = q;
811 return s.inflate_flush(z, r);
812 }
813 n--;
814 b |= (z.read_byte(p++) & 0xff) << k;
815 k += 8;
816 }
817
818 tindex = (tree_index + (b & inflate_mask[j])) * 3;
819
820 b >>>= (tree[tindex + 1]);
821 k -= (tree[tindex + 1]);
822
823 e = tree[tindex];
824
825 if (e === 0) { // literal
826 lit = tree[tindex + 2];
827 mode = LIT;
828 break;
829 }
830 if ((e & 16) !== 0) { // length
831 get = e & 15;
832 len = tree[tindex + 2];
833 mode = LENEXT;
834 break;
835 }
836 if ((e & 64) === 0) { // next table
837 need = e;
838 tree_index = tindex / 3 + tree[tindex + 2];
839 break;
840 }
841 if ((e & 32) !== 0) { // end of block
842 mode = WASH;
843 break;
844 }
845 mode = BADCODE; // invalid code
846 z.msg = "invalid literal/length code";
847 r = Z_DATA_ERROR;
848
849 s.bitb = b;
850 s.bitk = k;
851 z.avail_in = n;
852 z.total_in += p - z.next_in_index;
853 z.next_in_index = p;
854 s.write = q;
855 return s.inflate_flush(z, r);
856
857 case LENEXT: // i: getting length extra (have base)
858 j = get;
859
860 while (k < (j)) {
861 if (n !== 0)
862 r = Z_OK;
863 else {
864
865 s.bitb = b;
866 s.bitk = k;
867 z.avail_in = n;
868 z.total_in += p - z.next_in_index;
869 z.next_in_index = p;
870 s.write = q;
871 return s.inflate_flush(z, r);
872 }
873 n--;
874 b |= (z.read_byte(p++) & 0xff) << k;
875 k += 8;
876 }
877
878 len += (b & inflate_mask[j]);
879
880 b >>= j;
881 k -= j;
882
883 need = dbits;
884 tree = dtree;
885 tree_index = dtree_index;
886 mode = DIST;
887 case DIST: // i: get distance next
888 j = need;
889
890 while (k < (j)) {
891 if (n !== 0)
892 r = Z_OK;
893 else {
894
895 s.bitb = b;
896 s.bitk = k;
897 z.avail_in = n;
898 z.total_in += p - z.next_in_index;
899 z.next_in_index = p;
900 s.write = q;
901 return s.inflate_flush(z, r);
902 }
903 n--;
904 b |= (z.read_byte(p++) & 0xff) << k;
905 k += 8;
906 }
907
908 tindex = (tree_index + (b & inflate_mask[j])) * 3;
909
910 b >>= tree[tindex + 1];
911 k -= tree[tindex + 1];
912
913 e = (tree[tindex]);
914 if ((e & 16) !== 0) { // distance
915 get = e & 15;
916 dist = tree[tindex + 2];
917 mode = DISTEXT;
918 break;
919 }
920 if ((e & 64) === 0) { // next table
921 need = e;
922 tree_index = tindex / 3 + tree[tindex + 2];
923 break;
924 }
925 mode = BADCODE; // invalid code
926 z.msg = "invalid distance code";
927 r = Z_DATA_ERROR;
928
929 s.bitb = b;
930 s.bitk = k;
931 z.avail_in = n;
932 z.total_in += p - z.next_in_index;
933 z.next_in_index = p;
934 s.write = q;
935 return s.inflate_flush(z, r);
936
937 case DISTEXT: // i: getting distance extra
938 j = get;
939
940 while (k < (j)) {
941 if (n !== 0)
942 r = Z_OK;
943 else {
944
945 s.bitb = b;
946 s.bitk = k;
947 z.avail_in = n;
948 z.total_in += p - z.next_in_index;
949 z.next_in_index = p;
950 s.write = q;
951 return s.inflate_flush(z, r);
952 }
953 n--;
954 b |= (z.read_byte(p++) & 0xff) << k;
955 k += 8;
956 }
957
958 dist += (b & inflate_mask[j]);
959
960 b >>= j;
961 k -= j;
962
963 mode = COPY;
964 case COPY: // o: copying bytes in window, waiting for space
965 f = q - dist;
966 while (f < 0) { // modulo window size-"while" instead
967 f += s.end; // of "if" handles invalid distances
968 }
969 while (len !== 0) {
970
971 if (m === 0) {
972 if (q == s.end && s.read !== 0) {
973 q = 0;
974 m = q < s.read ? s.read - q - 1 : s.end - q;
975 }
976 if (m === 0) {
977 s.write = q;
978 r = s.inflate_flush(z, r);
979 q = s.write;
980 m = q < s.read ? s.read - q - 1 : s.end - q;
981
982 if (q == s.end && s.read !== 0) {
983 q = 0;
984 m = q < s.read ? s.read - q - 1 : s.end - q;
985 }
986
987 if (m === 0) {
988 s.bitb = b;
989 s.bitk = k;
990 z.avail_in = n;
991 z.total_in += p - z.next_in_index;
992 z.next_in_index = p;
993 s.write = q;
994 return s.inflate_flush(z, r);
995 }
996 }
997 }
998
999 s.window[q++] = s.window[f++];
1000 m--;
1001
1002 if (f == s.end)
1003 f = 0;
1004 len--;
1005 }
1006 mode = START;
1007 break;
1008 case LIT: // o: got literal, waiting for output space
1009 if (m === 0) {
1010 if (q == s.end && s.read !== 0) {
1011 q = 0;
1012 m = q < s.read ? s.read - q - 1 : s.end - q;
1013 }
1014 if (m === 0) {
1015 s.write = q;
1016 r = s.inflate_flush(z, r);
1017 q = s.write;
1018 m = q < s.read ? s.read - q - 1 : s.end - q;
1019
1020 if (q == s.end && s.read !== 0) {
1021 q = 0;
1022 m = q < s.read ? s.read - q - 1 : s.end - q;
1023 }
1024 if (m === 0) {
1025 s.bitb = b;
1026 s.bitk = k;
1027 z.avail_in = n;
1028 z.total_in += p - z.next_in_index;
1029 z.next_in_index = p;
1030 s.write = q;
1031 return s.inflate_flush(z, r);
1032 }
1033 }
1034 }
1035 r = Z_OK;
1036
1037 s.window[q++] = /* (byte) */lit;
1038 m--;
1039
1040 mode = START;
1041 break;
1042 case WASH: // o: got eob, possibly more output
1043 if (k > 7) { // return unused byte, if any
1044 k -= 8;
1045 n++;
1046 p--; // can always return one
1047 }
1048
1049 s.write = q;
1050 r = s.inflate_flush(z, r);
1051 q = s.write;
1052 m = q < s.read ? s.read - q - 1 : s.end - q;
1053
1054 if (s.read != s.write) {
1055 s.bitb = b;
1056 s.bitk = k;
1057 z.avail_in = n;
1058 z.total_in += p - z.next_in_index;
1059 z.next_in_index = p;
1060 s.write = q;
1061 return s.inflate_flush(z, r);
1062 }
1063 mode = END;
1064 case END:
1065 r = Z_STREAM_END;
1066 s.bitb = b;
1067 s.bitk = k;
1068 z.avail_in = n;
1069 z.total_in += p - z.next_in_index;
1070 z.next_in_index = p;
1071 s.write = q;
1072 return s.inflate_flush(z, r);
1073
1074 case BADCODE: // x: got error
1075
1076 r = Z_DATA_ERROR;
1077
1078 s.bitb = b;
1079 s.bitk = k;
1080 z.avail_in = n;
1081 z.total_in += p - z.next_in_index;
1082 z.next_in_index = p;
1083 s.write = q;
1084 return s.inflate_flush(z, r);
1085
1086 default:
1087 r = Z_STREAM_ERROR;
1088
1089 s.bitb = b;
1090 s.bitk = k;
1091 z.avail_in = n;
1092 z.total_in += p - z.next_in_index;
1093 z.next_in_index = p;
1094 s.write = q;
1095 return s.inflate_flush(z, r);
1096 }
1097 }
1098 };
1099
1100 that.free = function() {
1101 // ZFREE(z, c);
1102 };
1103
1104 }
1105
1106 // InfBlocks
1107
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 ];
1111
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
1117 // block
1118 var DTREE = 5; // get length, distance trees for a
1119 // dynamic block
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
1124
1125 function InfBlocks(z, w) {
1126 var that = this;
1127
1128 var mode = TYPE; // current inflate_block mode
1129
1130 var left = 0; // if STORED, bytes left to copy
1131
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
1137
1138 var codes = new InfCodes(); // if CODES, current state
1139
1140 var last = 0; // true if this block is the last block
1141
1142 var hufts = new Int32Array(MANY * 3); // single malloc for tree space
1143 var check = 0; // check on output
1144 var inftree = new InfTree();
1145
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
1152
1153 that.reset = function(z, c) {
1154 if (c)
1155 c[0] = check;
1156 // if (mode == BTREE || mode == DTREE) {
1157 // }
1158 if (mode == CODES) {
1159 codes.free(z);
1160 }
1161 mode = TYPE;
1162 that.bitk = 0;
1163 that.bitb = 0;
1164 that.read = that.write = 0;
1165 };
1166
1167 that.reset(z, null);
1168
1169 // copy as much as possible from the sliding window to the output area
1170 that.inflate_flush = function(z, r) {
1171 var n;
1172 var p;
1173 var q;
1174
1175 // local copies of source and destination pointers
1176 p = z.next_out_index;
1177 q = that.read;
1178
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)
1182 n = z.avail_out;
1183 if (n !== 0 && r == Z_BUF_ERROR)
1184 r = Z_OK;
1185
1186 // update counters
1187 z.avail_out -= n;
1188 z.total_out += n;
1189
1190 // copy as far as end of window
1191 z.next_out.set(that.window.subarray(q, q + n), p);
1192 p += n;
1193 q += n;
1194
1195 // see if more to copy at beginning of window
1196 if (q == that.end) {
1197 // wrap pointers
1198 q = 0;
1199 if (that.write == that.end)
1200 that.write = 0;
1201
1202 // compute bytes to copy
1203 n = that.write - q;
1204 if (n > z.avail_out)
1205 n = z.avail_out;
1206 if (n !== 0 && r == Z_BUF_ERROR)
1207 r = Z_OK;
1208
1209 // update counters
1210 z.avail_out -= n;
1211 z.total_out += n;
1212
1213 // copy
1214 z.next_out.set(that.window.subarray(q, q + n), p);
1215 p += n;
1216 q += n;
1217 }
1218
1219 // update pointers
1220 z.next_out_index = p;
1221 that.read = q;
1222
1223 // done
1224 return r;
1225 };
1226
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
1235
1236 var i;
1237
1238 // copy input/output information to locals (UPDATE macro restores)
1239 // {
1240 p = z.next_in_index;
1241 n = z.avail_in;
1242 b = that.bitb;
1243 k = that.bitk;
1244 // }
1245 // {
1246 q = that.write;
1247 m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
1248 // }
1249
1250 // process input based on current state
1251 // DEBUG dtree
1252 while (true) {
1253 switch (mode) {
1254 case TYPE:
1255
1256 while (k < (3)) {
1257 if (n !== 0) {
1258 r = Z_OK;
1259 } else {
1260 that.bitb = b;
1261 that.bitk = k;
1262 z.avail_in = n;
1263 z.total_in += p - z.next_in_index;
1264 z.next_in_index = p;
1265 that.write = q;
1266 return that.inflate_flush(z, r);
1267 }
1268 n--;
1269 b |= (z.read_byte(p++) & 0xff) << k;
1270 k += 8;
1271 }
1272 t = /* (int) */(b & 7);
1273 last = t & 1;
1274
1275 switch (t >>> 1) {
1276 case 0: // stored
1277 // {
1278 b >>>= (3);
1279 k -= (3);
1280 // }
1281 t = k & 7; // go to byte boundary
1282
1283 // {
1284 b >>>= (t);
1285 k -= (t);
1286 // }
1287 mode = LENS; // get length of stored block
1288 break;
1289 case 1: // fixed
1290 // {
1291 var bl = []; // new Array(1);
1292 var bd = []; // new Array(1);
1293 var tl = [ [] ]; // new Array(1);
1294 var td = [ [] ]; // new Array(1);
1295
1296 InfTree.inflate_trees_fixed(bl, bd, tl, td);
1297 codes.init(bl[0], bd[0], tl[0], 0, td[0], 0);
1298 // }
1299
1300 // {
1301 b >>>= (3);
1302 k -= (3);
1303 // }
1304
1305 mode = CODES;
1306 break;
1307 case 2: // dynamic
1308
1309 // {
1310 b >>>= (3);
1311 k -= (3);
1312 // }
1313
1314 mode = TABLE;
1315 break;
1316 case 3: // illegal
1317
1318 // {
1319 b >>>= (3);
1320 k -= (3);
1321 // }
1322 mode = BADBLOCKS;
1323 z.msg = "invalid block type";
1324 r = Z_DATA_ERROR;
1325
1326 that.bitb = b;
1327 that.bitk = k;
1328 z.avail_in = n;
1329 z.total_in += p - z.next_in_index;
1330 z.next_in_index = p;
1331 that.write = q;
1332 return that.inflate_flush(z, r);
1333 }
1334 break;
1335 case LENS:
1336
1337 while (k < (32)) {
1338 if (n !== 0) {
1339 r = Z_OK;
1340 } else {
1341 that.bitb = b;
1342 that.bitk = k;
1343 z.avail_in = n;
1344 z.total_in += p - z.next_in_index;
1345 z.next_in_index = p;
1346 that.write = q;
1347 return that.inflate_flush(z, r);
1348 }
1349 n--;
1350 b |= (z.read_byte(p++) & 0xff) << k;
1351 k += 8;
1352 }
1353
1354 if ((((~b) >>> 16) & 0xffff) != (b & 0xffff)) {
1355 mode = BADBLOCKS;
1356 z.msg = "invalid stored block lengths";
1357 r = Z_DATA_ERROR;
1358
1359 that.bitb = b;
1360 that.bitk = k;
1361 z.avail_in = n;
1362 z.total_in += p - z.next_in_index;
1363 z.next_in_index = p;
1364 that.write = q;
1365 return that.inflate_flush(z, r);
1366 }
1367 left = (b & 0xffff);
1368 b = k = 0; // dump bits
1369 mode = left !== 0 ? STORED : (last !== 0 ? DRY : TYPE);
1370 break;
1371 case STORED:
1372 if (n === 0) {
1373 that.bitb = b;
1374 that.bitk = k;
1375 z.avail_in = n;
1376 z.total_in += p - z.next_in_index;
1377 z.next_in_index = p;
1378 that.write = q;
1379 return that.inflate_flush(z, r);
1380 }
1381
1382 if (m === 0) {
1383 if (q == that.end && that.read !== 0) {
1384 q = 0;
1385 m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
1386 }
1387 if (m === 0) {
1388 that.write = q;
1389 r = that.inflate_flush(z, r);
1390 q = that.write;
1391 m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
1392 if (q == that.end && that.read !== 0) {
1393 q = 0;
1394 m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
1395 }
1396 if (m === 0) {
1397 that.bitb = b;
1398 that.bitk = k;
1399 z.avail_in = n;
1400 z.total_in += p - z.next_in_index;
1401 z.next_in_index = p;
1402 that.write = q;
1403 return that.inflate_flush(z, r);
1404 }
1405 }
1406 }
1407 r = Z_OK;
1408
1409 t = left;
1410 if (t > n)
1411 t = n;
1412 if (t > m)
1413 t = m;
1414 that.window.set(z.read_buf(p, t), q);
1415 p += t;
1416 n -= t;
1417 q += t;
1418 m -= t;
1419 if ((left -= t) !== 0)
1420 break;
1421 mode = last !== 0 ? DRY : TYPE;
1422 break;
1423 case TABLE:
1424
1425 while (k < (14)) {
1426 if (n !== 0) {
1427 r = Z_OK;
1428 } else {
1429 that.bitb = b;
1430 that.bitk = k;
1431 z.avail_in = n;
1432 z.total_in += p - z.next_in_index;
1433 z.next_in_index = p;
1434 that.write = q;
1435 return that.inflate_flush(z, r);
1436 }
1437
1438 n--;
1439 b |= (z.read_byte(p++) & 0xff) << k;
1440 k += 8;
1441 }
1442
1443 table = t = (b & 0x3fff);
1444 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) {
1445 mode = BADBLOCKS;
1446 z.msg = "too many length or distance symbols";
1447 r = Z_DATA_ERROR;
1448
1449 that.bitb = b;
1450 that.bitk = k;
1451 z.avail_in = n;
1452 z.total_in += p - z.next_in_index;
1453 z.next_in_index = p;
1454 that.write = q;
1455 return that.inflate_flush(z, r);
1456 }
1457 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
1458 if (!blens || blens.length < t) {
1459 blens = []; // new Array(t);
1460 } else {
1461 for (i = 0; i < t; i++) {
1462 blens[i] = 0;
1463 }
1464 }
1465
1466 // {
1467 b >>>= (14);
1468 k -= (14);
1469 // }
1470
1471 index = 0;
1472 mode = BTREE;
1473 case BTREE:
1474 while (index < 4 + (table >>> 10)) {
1475 while (k < (3)) {
1476 if (n !== 0) {
1477 r = Z_OK;
1478 } else {
1479 that.bitb = b;
1480 that.bitk = k;
1481 z.avail_in = n;
1482 z.total_in += p - z.next_in_index;
1483 z.next_in_index = p;
1484 that.write = q;
1485 return that.inflate_flush(z, r);
1486 }
1487 n--;
1488 b |= (z.read_byte(p++) & 0xff) << k;
1489 k += 8;
1490 }
1491
1492 blens[border[index++]] = b & 7;
1493
1494 // {
1495 b >>>= (3);
1496 k -= (3);
1497 // }
1498 }
1499
1500 while (index < 19) {
1501 blens[border[index++]] = 0;
1502 }
1503
1504 bb[0] = 7;
1505 t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
1506 if (t != Z_OK) {
1507 r = t;
1508 if (r == Z_DATA_ERROR) {
1509 blens = null;
1510 mode = BADBLOCKS;
1511 }
1512
1513 that.bitb = b;
1514 that.bitk = k;
1515 z.avail_in = n;
1516 z.total_in += p - z.next_in_index;
1517 z.next_in_index = p;
1518 that.write = q;
1519 return that.inflate_flush(z, r);
1520 }
1521
1522 index = 0;
1523 mode = DTREE;
1524 case DTREE:
1525 while (true) {
1526 t = table;
1527 if (!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))) {
1528 break;
1529 }
1530
1531 var j, c;
1532
1533 t = bb[0];
1534
1535 while (k < (t)) {
1536 if (n !== 0) {
1537 r = Z_OK;
1538 } else {
1539 that.bitb = b;
1540 that.bitk = k;
1541 z.avail_in = n;
1542 z.total_in += p - z.next_in_index;
1543 z.next_in_index = p;
1544 that.write = q;
1545 return that.inflate_flush(z, r);
1546 }
1547 n--;
1548 b |= (z.read_byte(p++) & 0xff) << k;
1549 k += 8;
1550 }
1551
1552 // if (tb[0] == -1) {
1553 // System.err.println("null...");
1554 // }
1555
1556 t = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 1];
1557 c = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 2];
1558
1559 if (c < 16) {
1560 b >>>= (t);
1561 k -= (t);
1562 blens[index++] = c;
1563 } else { // c == 16..18
1564 i = c == 18 ? 7 : c - 14;
1565 j = c == 18 ? 11 : 3;
1566
1567 while (k < (t + i)) {
1568 if (n !== 0) {
1569 r = Z_OK;
1570 } else {
1571 that.bitb = b;
1572 that.bitk = k;
1573 z.avail_in = n;
1574 z.total_in += p - z.next_in_index;
1575 z.next_in_index = p;
1576 that.write = q;
1577 return that.inflate_flush(z, r);
1578 }
1579 n--;
1580 b |= (z.read_byte(p++) & 0xff) << k;
1581 k += 8;
1582 }
1583
1584 b >>>= (t);
1585 k -= (t);
1586
1587 j += (b & inflate_mask[i]);
1588
1589 b >>>= (i);
1590 k -= (i);
1591
1592 i = index;
1593 t = table;
1594 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || (c == 16 && i < 1)) {
1595 blens = null;
1596 mode = BADBLOCKS;
1597 z.msg = "invalid bit length repeat";
1598 r = Z_DATA_ERROR;
1599
1600 that.bitb = b;
1601 that.bitk = k;
1602 z.avail_in = n;
1603 z.total_in += p - z.next_in_index;
1604 z.next_in_index = p;
1605 that.write = q;
1606 return that.inflate_flush(z, r);
1607 }
1608
1609 c = c == 16 ? blens[i - 1] : 0;
1610 do {
1611 blens[i++] = c;
1612 } while (--j !== 0);
1613 index = i;
1614 }
1615 }
1616
1617 tb[0] = -1;
1618 // {
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
1625
1626 t = table;
1627 t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), blens, bl_, bd_, tl_, td_, hufts, z);
1628
1629 if (t != Z_OK) {
1630 if (t == Z_DATA_ERROR) {
1631 blens = null;
1632 mode = BADBLOCKS;
1633 }
1634 r = t;
1635
1636 that.bitb = b;
1637 that.bitk = k;
1638 z.avail_in = n;
1639 z.total_in += p - z.next_in_index;
1640 z.next_in_index = p;
1641 that.write = q;
1642 return that.inflate_flush(z, r);
1643 }
1644 codes.init(bl_[0], bd_[0], hufts, tl_[0], hufts, td_[0]);
1645 // }
1646 mode = CODES;
1647 case CODES:
1648 that.bitb = b;
1649 that.bitk = k;
1650 z.avail_in = n;
1651 z.total_in += p - z.next_in_index;
1652 z.next_in_index = p;
1653 that.write = q;
1654
1655 if ((r = codes.proc(that, z, r)) != Z_STREAM_END) {
1656 return that.inflate_flush(z, r);
1657 }
1658 r = Z_OK;
1659 codes.free(z);
1660
1661 p = z.next_in_index;
1662 n = z.avail_in;
1663 b = that.bitb;
1664 k = that.bitk;
1665 q = that.write;
1666 m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
1667
1668 if (last === 0) {
1669 mode = TYPE;
1670 break;
1671 }
1672 mode = DRY;
1673 case DRY:
1674 that.write = q;
1675 r = that.inflate_flush(z, r);
1676 q = that.write;
1677 m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
1678 if (that.read != that.write) {
1679 that.bitb = b;
1680 that.bitk = k;
1681 z.avail_in = n;
1682 z.total_in += p - z.next_in_index;
1683 z.next_in_index = p;
1684 that.write = q;
1685 return that.inflate_flush(z, r);
1686 }
1687 mode = DONELOCKS;
1688 case DONELOCKS:
1689 r = Z_STREAM_END;
1690
1691 that.bitb = b;
1692 that.bitk = k;
1693 z.avail_in = n;
1694 z.total_in += p - z.next_in_index;
1695 z.next_in_index = p;
1696 that.write = q;
1697 return that.inflate_flush(z, r);
1698 case BADBLOCKS:
1699 r = Z_DATA_ERROR;
1700
1701 that.bitb = b;
1702 that.bitk = k;
1703 z.avail_in = n;
1704 z.total_in += p - z.next_in_index;
1705 z.next_in_index = p;
1706 that.write = q;
1707 return that.inflate_flush(z, r);
1708
1709 default:
1710 r = Z_STREAM_ERROR;
1711
1712 that.bitb = b;
1713 that.bitk = k;
1714 z.avail_in = n;
1715 z.total_in += p - z.next_in_index;
1716 z.next_in_index = p;
1717 that.write = q;
1718 return that.inflate_flush(z, r);
1719 }
1720 }
1721 };
1722
1723 that.free = function(z) {
1724 that.reset(z, null);
1725 that.window = null;
1726 hufts = null;
1727 // ZFREE(z, s);
1728 };
1729
1730 that.set_dictionary = function(d, start, n) {
1731 that.window.set(d.subarray(start, start + n), 0);
1732 that.read = that.write = n;
1733 };
1734
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;
1739 };
1740
1741 }
1742
1743 // Inflate
1744
1745 // preset dictionary flag in zlib header
1746 var PRESET_DICT = 0x20;
1747
1748 var Z_DEFLATED = 8;
1749
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
1760
1761 var mark = [ 0, 0, 0xff, 0xff ];
1762
1763 function Inflate() {
1764 var that = this;
1765
1766 that.mode = 0; // current inflate mode
1767
1768 // mode dependent information
1769 that.method = 0; // if FLAGS, method byte
1770
1771 // if CHECK, check values to compare
1772 that.was = [ 0 ]; // new Array(1); // computed check value
1773 that.need = 0; // stream check value
1774
1775 // if BAD, inflateSync's marker bytes count
1776 that.marker = 0;
1777
1778 // mode independent information
1779 that.wbits = 0; // log2(window size) (8..15, defaults to 15)
1780
1781 // this.blocks; // current inflate_blocks state
1782
1783 function inflateReset(z) {
1784 if (!z || !z.istate)
1785 return Z_STREAM_ERROR;
1786
1787 z.total_in = z.total_out = 0;
1788 z.msg = null;
1789 z.istate.mode = BLOCKS;
1790 z.istate.blocks.reset(z, null);
1791 return Z_OK;
1792 }
1793
1794 that.inflateEnd = function(z) {
1795 if (that.blocks)
1796 that.blocks.free(z);
1797 that.blocks = null;
1798 // ZFREE(z, z->state);
1799 return Z_OK;
1800 };
1801
1802 that.inflateInit = function(z, w) {
1803 z.msg = null;
1804 that.blocks = null;
1805
1806 // set window size
1807 if (w < 8 || w > 15) {
1808 that.inflateEnd(z);
1809 return Z_STREAM_ERROR;
1810 }
1811 that.wbits = w;
1812
1813 z.istate.blocks = new InfBlocks(z, 1 << w);
1814
1815 // reset state
1816 inflateReset(z);
1817 return Z_OK;
1818 };
1819
1820 that.inflate = function(z, f) {
1821 var r;
1822 var b;
1823
1824 if (!z || !z.istate || !z.next_in)
1825 return Z_STREAM_ERROR;
1826 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
1827 r = Z_BUF_ERROR;
1828 while (true) {
1829 // System.out.println("mode: "+z.istate.mode);
1830 switch (z.istate.mode) {
1831 case METHOD:
1832
1833 if (z.avail_in === 0)
1834 return r;
1835 r = f;
1836
1837 z.avail_in--;
1838 z.total_in++;
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
1843 break;
1844 }
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
1849 break;
1850 }
1851 z.istate.mode = FLAG;
1852 case FLAG:
1853
1854 if (z.avail_in === 0)
1855 return r;
1856 r = f;
1857
1858 z.avail_in--;
1859 z.total_in++;
1860 b = (z.read_byte(z.next_in_index++)) & 0xff;
1861
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
1866 break;
1867 }
1868
1869 if ((b & PRESET_DICT) === 0) {
1870 z.istate.mode = BLOCKS;
1871 break;
1872 }
1873 z.istate.mode = DICT4;
1874 case DICT4:
1875
1876 if (z.avail_in === 0)
1877 return r;
1878 r = f;
1879
1880 z.avail_in--;
1881 z.total_in++;
1882 z.istate.need = ((z.read_byte(z.next_in_index++) & 0xff) << 24) & 0xff000000;
1883 z.istate.mode = DICT3;
1884 case DICT3:
1885
1886 if (z.avail_in === 0)
1887 return r;
1888 r = f;
1889
1890 z.avail_in--;
1891 z.total_in++;
1892 z.istate.need += ((z.read_byte(z.next_in_index++) & 0xff) << 16) & 0xff0000;
1893 z.istate.mode = DICT2;
1894 case DICT2:
1895
1896 if (z.avail_in === 0)
1897 return r;
1898 r = f;
1899
1900 z.avail_in--;
1901 z.total_in++;
1902 z.istate.need += ((z.read_byte(z.next_in_index++) & 0xff) << 8) & 0xff00;
1903 z.istate.mode = DICT1;
1904 case DICT1:
1905
1906 if (z.avail_in === 0)
1907 return r;
1908 r = f;
1909
1910 z.avail_in--;
1911 z.total_in++;
1912 z.istate.need += (z.read_byte(z.next_in_index++) & 0xff);
1913 z.istate.mode = DICT0;
1914 return Z_NEED_DICT;
1915 case DICT0:
1916 z.istate.mode = BAD;
1917 z.msg = "need dictionary";
1918 z.istate.marker = 0; // can try inflateSync
1919 return Z_STREAM_ERROR;
1920 case BLOCKS:
1921
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
1926 break;
1927 }
1928 if (r == Z_OK) {
1929 r = f;
1930 }
1931 if (r != Z_STREAM_END) {
1932 return r;
1933 }
1934 r = f;
1935 z.istate.blocks.reset(z, z.istate.was);
1936 z.istate.mode = DONE;
1937 case DONE:
1938 return Z_STREAM_END;
1939 case BAD:
1940 return Z_DATA_ERROR;
1941 default:
1942 return Z_STREAM_ERROR;
1943 }
1944 }
1945 };
1946
1947 that.inflateSetDictionary = function(z, dictionary, dictLength) {
1948 var index = 0;
1949 var length = dictLength;
1950 if (!z || !z.istate || z.istate.mode != DICT0)
1951 return Z_STREAM_ERROR;
1952
1953 if (length >= (1 << z.istate.wbits)) {
1954 length = (1 << z.istate.wbits) - 1;
1955 index = dictLength - length;
1956 }
1957 z.istate.blocks.set_dictionary(dictionary, index, length);
1958 z.istate.mode = BLOCKS;
1959 return Z_OK;
1960 };
1961
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
1967
1968 // set up
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;
1974 }
1975 if ((n = z.avail_in) === 0)
1976 return Z_BUF_ERROR;
1977 p = z.next_in_index;
1978 m = z.istate.marker;
1979
1980 // search
1981 while (n !== 0 && m < 4) {
1982 if (z.read_byte(p) == mark[m]) {
1983 m++;
1984 } else if (z.read_byte(p) !== 0) {
1985 m = 0;
1986 } else {
1987 m = 4 - m;
1988 }
1989 p++;
1990 n--;
1991 }
1992
1993 // restore
1994 z.total_in += p - z.next_in_index;
1995 z.next_in_index = p;
1996 z.avail_in = n;
1997 z.istate.marker = m;
1998
1999 // return no joy or set up to restart on a new block
2000 if (m != 4) {
2001 return Z_DATA_ERROR;
2002 }
2003 r = z.total_in;
2004 w = z.total_out;
2005 inflateReset(z);
2006 z.total_in = r;
2007 z.total_out = w;
2008 z.istate.mode = BLOCKS;
2009 return Z_OK;
2010 };
2011
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
2015 // Z_SYNC_FLUSH
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();
2023 };
2024 }
2025
2026 // ZStream
2027
2028 function ZStream() {
2029 }
2030
2031 ZStream.prototype = {
2032 inflateInit : function(bits) {
2033 var that = this;
2034 that.istate = new Inflate();
2035 if (!bits)
2036 bits = MAX_BITS;
2037 return that.istate.inflateInit(that, bits);
2038 },
2039
2040 inflate : function(f) {
2041 var that = this;
2042 if (!that.istate)
2043 return Z_STREAM_ERROR;
2044 return that.istate.inflate(that, f);
2045 },
2046
2047 inflateEnd : function() {
2048 var that = this;
2049 if (!that.istate)
2050 return Z_STREAM_ERROR;
2051 var ret = that.istate.inflateEnd(that);
2052 that.istate = null;
2053 return ret;
2054 },
2055
2056 inflateSync : function() {
2057 var that = this;
2058 if (!that.istate)
2059 return Z_STREAM_ERROR;
2060 return that.istate.inflateSync(that);
2061 },
2062 inflateSetDictionary : function(dictionary, dictLength) {
2063 var that = this;
2064 if (!that.istate)
2065 return Z_STREAM_ERROR;
2066 return that.istate.inflateSetDictionary(that, dictionary, dictLength);
2067 },
2068 read_byte : function(start) {
2069 var that = this;
2070 return that.next_in.subarray(start, start + 1)[0];
2071 },
2072 read_buf : function(start, size) {
2073 var that = this;
2074 return that.next_in.subarray(start, start + size);
2075 }
2076 };
2077
2078 // Inflater
2079
2080 function Inflater() {
2081 var that = this;
2082 var z = new ZStream();
2083 var bufsize = 512;
2084 var flush = Z_NO_FLUSH;
2085 var buf = new Uint8Array(bufsize);
2086 var nomoreinput = false;
2087
2088 z.inflateInit();
2089 z.next_out = buf;
2090
2091 that.append = function(data, onprogress) {
2092 var err, buffers = [], lastIndex = 0, bufferIndex = 0, bufferSize = 0, array;
2093 if (data.length === 0)
2094 return;
2095 z.next_in_index = 0;
2096 z.next_in = data;
2097 z.avail_in = data.length;
2098 do {
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;
2103 nomoreinput = true;
2104 }
2105 err = z.inflate(flush);
2106 if (nomoreinput && (err == Z_BUF_ERROR))
2107 return -1;
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))
2111 return -1;
2112 if (z.next_out_index)
2113 if (z.next_out_index == bufsize)
2114 buffers.push(new Uint8Array(buf));
2115 else
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;
2121 }
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;
2127 });
2128 return array;
2129 };
2130 that.flush = function() {
2131 z.inflateEnd();
2132 };
2133 }
2134
2135 var inflater;
2136
2137 if (obj.zip)
2138 obj.zip.Inflater = Inflater;
2139 else {
2140 inflater = new Inflater();
2141 obj.addEventListener("message", function(event) {
2142 var message = event.data;
2143
2144 if (message.append)
2145 obj.postMessage({
2146 onappend : true,
2147 data : inflater.append(message.data, function(current) {
2148 obj.postMessage({
2149 progress : true,
2150 current : current
2151 });
2152 })
2153 });
2154 if (message.flush) {
2155 inflater.flush();
2156 obj.postMessage({
2157 onflush : true
2158 });
2159 }
2160 }, false);
2161 }
2162
2163 })(EasyDeflate);