zip-js 0.1.0 → 0.2.0

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