hwp2md 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +7 -0
- package/README.md +174 -0
- package/dist/browser.d.mts +179 -0
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.mjs +27 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.mjs +687 -0
- package/dist/cli/index.mjs.map +1 -0
- package/dist/converter-C0C25ssg.mjs +3 -0
- package/dist/converter-D6LrZNSL.mjs +4804 -0
- package/dist/converter-D6LrZNSL.mjs.map +1 -0
- package/dist/index.cjs +1200 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +454 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +454 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1126 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +92 -0
|
@@ -0,0 +1,4804 @@
|
|
|
1
|
+
import * as CFB from "cfb";
|
|
2
|
+
import { unzipSync } from "fflate";
|
|
3
|
+
import { XMLParser } from "fast-xml-parser";
|
|
4
|
+
|
|
5
|
+
//#region ../../node_modules/.pnpm/pako@2.1.0/node_modules/pako/dist/pako.esm.mjs
|
|
6
|
+
/*! pako 2.1.0 https://github.com/nodeca/pako @license (MIT AND Zlib) */
|
|
7
|
+
const Z_FIXED$1 = 4;
|
|
8
|
+
const Z_BINARY = 0;
|
|
9
|
+
const Z_TEXT = 1;
|
|
10
|
+
const Z_UNKNOWN$1 = 2;
|
|
11
|
+
function zero$1(buf) {
|
|
12
|
+
let len = buf.length;
|
|
13
|
+
while (--len >= 0) buf[len] = 0;
|
|
14
|
+
}
|
|
15
|
+
const STORED_BLOCK = 0;
|
|
16
|
+
const STATIC_TREES = 1;
|
|
17
|
+
const DYN_TREES = 2;
|
|
18
|
+
const MIN_MATCH$1 = 3;
|
|
19
|
+
const MAX_MATCH$1 = 258;
|
|
20
|
+
const LENGTH_CODES$1 = 29;
|
|
21
|
+
const LITERALS$1 = 256;
|
|
22
|
+
const L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1;
|
|
23
|
+
const D_CODES$1 = 30;
|
|
24
|
+
const BL_CODES$1 = 19;
|
|
25
|
+
const HEAP_SIZE$1 = 2 * L_CODES$1 + 1;
|
|
26
|
+
const MAX_BITS$1 = 15;
|
|
27
|
+
const Buf_size = 16;
|
|
28
|
+
const MAX_BL_BITS = 7;
|
|
29
|
+
const END_BLOCK = 256;
|
|
30
|
+
const REP_3_6 = 16;
|
|
31
|
+
const REPZ_3_10 = 17;
|
|
32
|
+
const REPZ_11_138 = 18;
|
|
33
|
+
const extra_lbits = new Uint8Array([
|
|
34
|
+
0,
|
|
35
|
+
0,
|
|
36
|
+
0,
|
|
37
|
+
0,
|
|
38
|
+
0,
|
|
39
|
+
0,
|
|
40
|
+
0,
|
|
41
|
+
0,
|
|
42
|
+
1,
|
|
43
|
+
1,
|
|
44
|
+
1,
|
|
45
|
+
1,
|
|
46
|
+
2,
|
|
47
|
+
2,
|
|
48
|
+
2,
|
|
49
|
+
2,
|
|
50
|
+
3,
|
|
51
|
+
3,
|
|
52
|
+
3,
|
|
53
|
+
3,
|
|
54
|
+
4,
|
|
55
|
+
4,
|
|
56
|
+
4,
|
|
57
|
+
4,
|
|
58
|
+
5,
|
|
59
|
+
5,
|
|
60
|
+
5,
|
|
61
|
+
5,
|
|
62
|
+
0
|
|
63
|
+
]);
|
|
64
|
+
const extra_dbits = new Uint8Array([
|
|
65
|
+
0,
|
|
66
|
+
0,
|
|
67
|
+
0,
|
|
68
|
+
0,
|
|
69
|
+
1,
|
|
70
|
+
1,
|
|
71
|
+
2,
|
|
72
|
+
2,
|
|
73
|
+
3,
|
|
74
|
+
3,
|
|
75
|
+
4,
|
|
76
|
+
4,
|
|
77
|
+
5,
|
|
78
|
+
5,
|
|
79
|
+
6,
|
|
80
|
+
6,
|
|
81
|
+
7,
|
|
82
|
+
7,
|
|
83
|
+
8,
|
|
84
|
+
8,
|
|
85
|
+
9,
|
|
86
|
+
9,
|
|
87
|
+
10,
|
|
88
|
+
10,
|
|
89
|
+
11,
|
|
90
|
+
11,
|
|
91
|
+
12,
|
|
92
|
+
12,
|
|
93
|
+
13,
|
|
94
|
+
13
|
|
95
|
+
]);
|
|
96
|
+
const extra_blbits = new Uint8Array([
|
|
97
|
+
0,
|
|
98
|
+
0,
|
|
99
|
+
0,
|
|
100
|
+
0,
|
|
101
|
+
0,
|
|
102
|
+
0,
|
|
103
|
+
0,
|
|
104
|
+
0,
|
|
105
|
+
0,
|
|
106
|
+
0,
|
|
107
|
+
0,
|
|
108
|
+
0,
|
|
109
|
+
0,
|
|
110
|
+
0,
|
|
111
|
+
0,
|
|
112
|
+
0,
|
|
113
|
+
2,
|
|
114
|
+
3,
|
|
115
|
+
7
|
|
116
|
+
]);
|
|
117
|
+
const bl_order = new Uint8Array([
|
|
118
|
+
16,
|
|
119
|
+
17,
|
|
120
|
+
18,
|
|
121
|
+
0,
|
|
122
|
+
8,
|
|
123
|
+
7,
|
|
124
|
+
9,
|
|
125
|
+
6,
|
|
126
|
+
10,
|
|
127
|
+
5,
|
|
128
|
+
11,
|
|
129
|
+
4,
|
|
130
|
+
12,
|
|
131
|
+
3,
|
|
132
|
+
13,
|
|
133
|
+
2,
|
|
134
|
+
14,
|
|
135
|
+
1,
|
|
136
|
+
15
|
|
137
|
+
]);
|
|
138
|
+
const DIST_CODE_LEN = 512;
|
|
139
|
+
const static_ltree = new Array((L_CODES$1 + 2) * 2);
|
|
140
|
+
zero$1(static_ltree);
|
|
141
|
+
const static_dtree = new Array(D_CODES$1 * 2);
|
|
142
|
+
zero$1(static_dtree);
|
|
143
|
+
const _dist_code = new Array(DIST_CODE_LEN);
|
|
144
|
+
zero$1(_dist_code);
|
|
145
|
+
const _length_code = new Array(MAX_MATCH$1 - MIN_MATCH$1 + 1);
|
|
146
|
+
zero$1(_length_code);
|
|
147
|
+
const base_length = new Array(LENGTH_CODES$1);
|
|
148
|
+
zero$1(base_length);
|
|
149
|
+
const base_dist = new Array(D_CODES$1);
|
|
150
|
+
zero$1(base_dist);
|
|
151
|
+
function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
|
|
152
|
+
this.static_tree = static_tree;
|
|
153
|
+
this.extra_bits = extra_bits;
|
|
154
|
+
this.extra_base = extra_base;
|
|
155
|
+
this.elems = elems;
|
|
156
|
+
this.max_length = max_length;
|
|
157
|
+
this.has_stree = static_tree && static_tree.length;
|
|
158
|
+
}
|
|
159
|
+
let static_l_desc;
|
|
160
|
+
let static_d_desc;
|
|
161
|
+
let static_bl_desc;
|
|
162
|
+
function TreeDesc(dyn_tree, stat_desc) {
|
|
163
|
+
this.dyn_tree = dyn_tree;
|
|
164
|
+
this.max_code = 0;
|
|
165
|
+
this.stat_desc = stat_desc;
|
|
166
|
+
}
|
|
167
|
+
const d_code = (dist) => {
|
|
168
|
+
return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
|
|
169
|
+
};
|
|
170
|
+
const put_short = (s, w) => {
|
|
171
|
+
s.pending_buf[s.pending++] = w & 255;
|
|
172
|
+
s.pending_buf[s.pending++] = w >>> 8 & 255;
|
|
173
|
+
};
|
|
174
|
+
const send_bits = (s, value, length) => {
|
|
175
|
+
if (s.bi_valid > Buf_size - length) {
|
|
176
|
+
s.bi_buf |= value << s.bi_valid & 65535;
|
|
177
|
+
put_short(s, s.bi_buf);
|
|
178
|
+
s.bi_buf = value >> Buf_size - s.bi_valid;
|
|
179
|
+
s.bi_valid += length - Buf_size;
|
|
180
|
+
} else {
|
|
181
|
+
s.bi_buf |= value << s.bi_valid & 65535;
|
|
182
|
+
s.bi_valid += length;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
const send_code = (s, c, tree) => {
|
|
186
|
+
send_bits(s, tree[c * 2], tree[c * 2 + 1]);
|
|
187
|
+
};
|
|
188
|
+
const bi_reverse = (code, len) => {
|
|
189
|
+
let res = 0;
|
|
190
|
+
do {
|
|
191
|
+
res |= code & 1;
|
|
192
|
+
code >>>= 1;
|
|
193
|
+
res <<= 1;
|
|
194
|
+
} while (--len > 0);
|
|
195
|
+
return res >>> 1;
|
|
196
|
+
};
|
|
197
|
+
const bi_flush = (s) => {
|
|
198
|
+
if (s.bi_valid === 16) {
|
|
199
|
+
put_short(s, s.bi_buf);
|
|
200
|
+
s.bi_buf = 0;
|
|
201
|
+
s.bi_valid = 0;
|
|
202
|
+
} else if (s.bi_valid >= 8) {
|
|
203
|
+
s.pending_buf[s.pending++] = s.bi_buf & 255;
|
|
204
|
+
s.bi_buf >>= 8;
|
|
205
|
+
s.bi_valid -= 8;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
const gen_bitlen = (s, desc) => {
|
|
209
|
+
const tree = desc.dyn_tree;
|
|
210
|
+
const max_code = desc.max_code;
|
|
211
|
+
const stree = desc.stat_desc.static_tree;
|
|
212
|
+
const has_stree = desc.stat_desc.has_stree;
|
|
213
|
+
const extra = desc.stat_desc.extra_bits;
|
|
214
|
+
const base = desc.stat_desc.extra_base;
|
|
215
|
+
const max_length = desc.stat_desc.max_length;
|
|
216
|
+
let h;
|
|
217
|
+
let n, m;
|
|
218
|
+
let bits;
|
|
219
|
+
let xbits;
|
|
220
|
+
let f;
|
|
221
|
+
let overflow = 0;
|
|
222
|
+
for (bits = 0; bits <= MAX_BITS$1; bits++) s.bl_count[bits] = 0;
|
|
223
|
+
tree[s.heap[s.heap_max] * 2 + 1] = 0;
|
|
224
|
+
for (h = s.heap_max + 1; h < HEAP_SIZE$1; h++) {
|
|
225
|
+
n = s.heap[h];
|
|
226
|
+
bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
|
|
227
|
+
if (bits > max_length) {
|
|
228
|
+
bits = max_length;
|
|
229
|
+
overflow++;
|
|
230
|
+
}
|
|
231
|
+
tree[n * 2 + 1] = bits;
|
|
232
|
+
if (n > max_code) continue;
|
|
233
|
+
s.bl_count[bits]++;
|
|
234
|
+
xbits = 0;
|
|
235
|
+
if (n >= base) xbits = extra[n - base];
|
|
236
|
+
f = tree[n * 2];
|
|
237
|
+
s.opt_len += f * (bits + xbits);
|
|
238
|
+
if (has_stree) s.static_len += f * (stree[n * 2 + 1] + xbits);
|
|
239
|
+
}
|
|
240
|
+
if (overflow === 0) return;
|
|
241
|
+
do {
|
|
242
|
+
bits = max_length - 1;
|
|
243
|
+
while (s.bl_count[bits] === 0) bits--;
|
|
244
|
+
s.bl_count[bits]--;
|
|
245
|
+
s.bl_count[bits + 1] += 2;
|
|
246
|
+
s.bl_count[max_length]--;
|
|
247
|
+
overflow -= 2;
|
|
248
|
+
} while (overflow > 0);
|
|
249
|
+
for (bits = max_length; bits !== 0; bits--) {
|
|
250
|
+
n = s.bl_count[bits];
|
|
251
|
+
while (n !== 0) {
|
|
252
|
+
m = s.heap[--h];
|
|
253
|
+
if (m > max_code) continue;
|
|
254
|
+
if (tree[m * 2 + 1] !== bits) {
|
|
255
|
+
s.opt_len += (bits - tree[m * 2 + 1]) * tree[m * 2];
|
|
256
|
+
tree[m * 2 + 1] = bits;
|
|
257
|
+
}
|
|
258
|
+
n--;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
const gen_codes = (tree, max_code, bl_count) => {
|
|
263
|
+
const next_code = new Array(MAX_BITS$1 + 1);
|
|
264
|
+
let code = 0;
|
|
265
|
+
let bits;
|
|
266
|
+
let n;
|
|
267
|
+
for (bits = 1; bits <= MAX_BITS$1; bits++) {
|
|
268
|
+
code = code + bl_count[bits - 1] << 1;
|
|
269
|
+
next_code[bits] = code;
|
|
270
|
+
}
|
|
271
|
+
for (n = 0; n <= max_code; n++) {
|
|
272
|
+
let len = tree[n * 2 + 1];
|
|
273
|
+
if (len === 0) continue;
|
|
274
|
+
tree[n * 2] = bi_reverse(next_code[len]++, len);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
const tr_static_init = () => {
|
|
278
|
+
let n;
|
|
279
|
+
let bits;
|
|
280
|
+
let length;
|
|
281
|
+
let code;
|
|
282
|
+
let dist;
|
|
283
|
+
const bl_count = new Array(MAX_BITS$1 + 1);
|
|
284
|
+
length = 0;
|
|
285
|
+
for (code = 0; code < LENGTH_CODES$1 - 1; code++) {
|
|
286
|
+
base_length[code] = length;
|
|
287
|
+
for (n = 0; n < 1 << extra_lbits[code]; n++) _length_code[length++] = code;
|
|
288
|
+
}
|
|
289
|
+
_length_code[length - 1] = code;
|
|
290
|
+
dist = 0;
|
|
291
|
+
for (code = 0; code < 16; code++) {
|
|
292
|
+
base_dist[code] = dist;
|
|
293
|
+
for (n = 0; n < 1 << extra_dbits[code]; n++) _dist_code[dist++] = code;
|
|
294
|
+
}
|
|
295
|
+
dist >>= 7;
|
|
296
|
+
for (; code < D_CODES$1; code++) {
|
|
297
|
+
base_dist[code] = dist << 7;
|
|
298
|
+
for (n = 0; n < 1 << extra_dbits[code] - 7; n++) _dist_code[256 + dist++] = code;
|
|
299
|
+
}
|
|
300
|
+
for (bits = 0; bits <= MAX_BITS$1; bits++) bl_count[bits] = 0;
|
|
301
|
+
n = 0;
|
|
302
|
+
while (n <= 143) {
|
|
303
|
+
static_ltree[n * 2 + 1] = 8;
|
|
304
|
+
n++;
|
|
305
|
+
bl_count[8]++;
|
|
306
|
+
}
|
|
307
|
+
while (n <= 255) {
|
|
308
|
+
static_ltree[n * 2 + 1] = 9;
|
|
309
|
+
n++;
|
|
310
|
+
bl_count[9]++;
|
|
311
|
+
}
|
|
312
|
+
while (n <= 279) {
|
|
313
|
+
static_ltree[n * 2 + 1] = 7;
|
|
314
|
+
n++;
|
|
315
|
+
bl_count[7]++;
|
|
316
|
+
}
|
|
317
|
+
while (n <= 287) {
|
|
318
|
+
static_ltree[n * 2 + 1] = 8;
|
|
319
|
+
n++;
|
|
320
|
+
bl_count[8]++;
|
|
321
|
+
}
|
|
322
|
+
gen_codes(static_ltree, L_CODES$1 + 1, bl_count);
|
|
323
|
+
for (n = 0; n < D_CODES$1; n++) {
|
|
324
|
+
static_dtree[n * 2 + 1] = 5;
|
|
325
|
+
static_dtree[n * 2] = bi_reverse(n, 5);
|
|
326
|
+
}
|
|
327
|
+
static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS$1 + 1, L_CODES$1, MAX_BITS$1);
|
|
328
|
+
static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES$1, MAX_BITS$1);
|
|
329
|
+
static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES$1, MAX_BL_BITS);
|
|
330
|
+
};
|
|
331
|
+
const init_block = (s) => {
|
|
332
|
+
let n;
|
|
333
|
+
for (n = 0; n < L_CODES$1; n++) s.dyn_ltree[n * 2] = 0;
|
|
334
|
+
for (n = 0; n < D_CODES$1; n++) s.dyn_dtree[n * 2] = 0;
|
|
335
|
+
for (n = 0; n < BL_CODES$1; n++) s.bl_tree[n * 2] = 0;
|
|
336
|
+
s.dyn_ltree[END_BLOCK * 2] = 1;
|
|
337
|
+
s.opt_len = s.static_len = 0;
|
|
338
|
+
s.sym_next = s.matches = 0;
|
|
339
|
+
};
|
|
340
|
+
const bi_windup = (s) => {
|
|
341
|
+
if (s.bi_valid > 8) put_short(s, s.bi_buf);
|
|
342
|
+
else if (s.bi_valid > 0) s.pending_buf[s.pending++] = s.bi_buf;
|
|
343
|
+
s.bi_buf = 0;
|
|
344
|
+
s.bi_valid = 0;
|
|
345
|
+
};
|
|
346
|
+
const smaller = (tree, n, m, depth) => {
|
|
347
|
+
const _n2 = n * 2;
|
|
348
|
+
const _m2 = m * 2;
|
|
349
|
+
return tree[_n2] < tree[_m2] || tree[_n2] === tree[_m2] && depth[n] <= depth[m];
|
|
350
|
+
};
|
|
351
|
+
const pqdownheap = (s, tree, k) => {
|
|
352
|
+
const v = s.heap[k];
|
|
353
|
+
let j = k << 1;
|
|
354
|
+
while (j <= s.heap_len) {
|
|
355
|
+
if (j < s.heap_len && smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) j++;
|
|
356
|
+
if (smaller(tree, v, s.heap[j], s.depth)) break;
|
|
357
|
+
s.heap[k] = s.heap[j];
|
|
358
|
+
k = j;
|
|
359
|
+
j <<= 1;
|
|
360
|
+
}
|
|
361
|
+
s.heap[k] = v;
|
|
362
|
+
};
|
|
363
|
+
const compress_block = (s, ltree, dtree) => {
|
|
364
|
+
let dist;
|
|
365
|
+
let lc;
|
|
366
|
+
let sx = 0;
|
|
367
|
+
let code;
|
|
368
|
+
let extra;
|
|
369
|
+
if (s.sym_next !== 0) do {
|
|
370
|
+
dist = s.pending_buf[s.sym_buf + sx++] & 255;
|
|
371
|
+
dist += (s.pending_buf[s.sym_buf + sx++] & 255) << 8;
|
|
372
|
+
lc = s.pending_buf[s.sym_buf + sx++];
|
|
373
|
+
if (dist === 0) send_code(s, lc, ltree);
|
|
374
|
+
else {
|
|
375
|
+
code = _length_code[lc];
|
|
376
|
+
send_code(s, code + LITERALS$1 + 1, ltree);
|
|
377
|
+
extra = extra_lbits[code];
|
|
378
|
+
if (extra !== 0) {
|
|
379
|
+
lc -= base_length[code];
|
|
380
|
+
send_bits(s, lc, extra);
|
|
381
|
+
}
|
|
382
|
+
dist--;
|
|
383
|
+
code = d_code(dist);
|
|
384
|
+
send_code(s, code, dtree);
|
|
385
|
+
extra = extra_dbits[code];
|
|
386
|
+
if (extra !== 0) {
|
|
387
|
+
dist -= base_dist[code];
|
|
388
|
+
send_bits(s, dist, extra);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
} while (sx < s.sym_next);
|
|
392
|
+
send_code(s, END_BLOCK, ltree);
|
|
393
|
+
};
|
|
394
|
+
const build_tree = (s, desc) => {
|
|
395
|
+
const tree = desc.dyn_tree;
|
|
396
|
+
const stree = desc.stat_desc.static_tree;
|
|
397
|
+
const has_stree = desc.stat_desc.has_stree;
|
|
398
|
+
const elems = desc.stat_desc.elems;
|
|
399
|
+
let n, m;
|
|
400
|
+
let max_code = -1;
|
|
401
|
+
let node;
|
|
402
|
+
s.heap_len = 0;
|
|
403
|
+
s.heap_max = HEAP_SIZE$1;
|
|
404
|
+
for (n = 0; n < elems; n++) if (tree[n * 2] !== 0) {
|
|
405
|
+
s.heap[++s.heap_len] = max_code = n;
|
|
406
|
+
s.depth[n] = 0;
|
|
407
|
+
} else tree[n * 2 + 1] = 0;
|
|
408
|
+
while (s.heap_len < 2) {
|
|
409
|
+
node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0;
|
|
410
|
+
tree[node * 2] = 1;
|
|
411
|
+
s.depth[node] = 0;
|
|
412
|
+
s.opt_len--;
|
|
413
|
+
if (has_stree) s.static_len -= stree[node * 2 + 1];
|
|
414
|
+
}
|
|
415
|
+
desc.max_code = max_code;
|
|
416
|
+
for (n = s.heap_len >> 1; n >= 1; n--) pqdownheap(s, tree, n);
|
|
417
|
+
node = elems;
|
|
418
|
+
do {
|
|
419
|
+
/*** pqremove ***/
|
|
420
|
+
n = s.heap[1];
|
|
421
|
+
s.heap[1] = s.heap[s.heap_len--];
|
|
422
|
+
pqdownheap(s, tree, 1);
|
|
423
|
+
m = s.heap[1];
|
|
424
|
+
s.heap[--s.heap_max] = n;
|
|
425
|
+
s.heap[--s.heap_max] = m;
|
|
426
|
+
tree[node * 2] = tree[n * 2] + tree[m * 2];
|
|
427
|
+
s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
|
|
428
|
+
tree[n * 2 + 1] = tree[m * 2 + 1] = node;
|
|
429
|
+
s.heap[1] = node++;
|
|
430
|
+
pqdownheap(s, tree, 1);
|
|
431
|
+
} while (s.heap_len >= 2);
|
|
432
|
+
s.heap[--s.heap_max] = s.heap[1];
|
|
433
|
+
gen_bitlen(s, desc);
|
|
434
|
+
gen_codes(tree, max_code, s.bl_count);
|
|
435
|
+
};
|
|
436
|
+
const scan_tree = (s, tree, max_code) => {
|
|
437
|
+
let n;
|
|
438
|
+
let prevlen = -1;
|
|
439
|
+
let curlen;
|
|
440
|
+
let nextlen = tree[1];
|
|
441
|
+
let count = 0;
|
|
442
|
+
let max_count = 7;
|
|
443
|
+
let min_count = 4;
|
|
444
|
+
if (nextlen === 0) {
|
|
445
|
+
max_count = 138;
|
|
446
|
+
min_count = 3;
|
|
447
|
+
}
|
|
448
|
+
tree[(max_code + 1) * 2 + 1] = 65535;
|
|
449
|
+
for (n = 0; n <= max_code; n++) {
|
|
450
|
+
curlen = nextlen;
|
|
451
|
+
nextlen = tree[(n + 1) * 2 + 1];
|
|
452
|
+
if (++count < max_count && curlen === nextlen) continue;
|
|
453
|
+
else if (count < min_count) s.bl_tree[curlen * 2] += count;
|
|
454
|
+
else if (curlen !== 0) {
|
|
455
|
+
if (curlen !== prevlen) s.bl_tree[curlen * 2]++;
|
|
456
|
+
s.bl_tree[REP_3_6 * 2]++;
|
|
457
|
+
} else if (count <= 10) s.bl_tree[REPZ_3_10 * 2]++;
|
|
458
|
+
else s.bl_tree[REPZ_11_138 * 2]++;
|
|
459
|
+
count = 0;
|
|
460
|
+
prevlen = curlen;
|
|
461
|
+
if (nextlen === 0) {
|
|
462
|
+
max_count = 138;
|
|
463
|
+
min_count = 3;
|
|
464
|
+
} else if (curlen === nextlen) {
|
|
465
|
+
max_count = 6;
|
|
466
|
+
min_count = 3;
|
|
467
|
+
} else {
|
|
468
|
+
max_count = 7;
|
|
469
|
+
min_count = 4;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
const send_tree = (s, tree, max_code) => {
|
|
474
|
+
let n;
|
|
475
|
+
let prevlen = -1;
|
|
476
|
+
let curlen;
|
|
477
|
+
let nextlen = tree[1];
|
|
478
|
+
let count = 0;
|
|
479
|
+
let max_count = 7;
|
|
480
|
+
let min_count = 4;
|
|
481
|
+
if (nextlen === 0) {
|
|
482
|
+
max_count = 138;
|
|
483
|
+
min_count = 3;
|
|
484
|
+
}
|
|
485
|
+
for (n = 0; n <= max_code; n++) {
|
|
486
|
+
curlen = nextlen;
|
|
487
|
+
nextlen = tree[(n + 1) * 2 + 1];
|
|
488
|
+
if (++count < max_count && curlen === nextlen) continue;
|
|
489
|
+
else if (count < min_count) do
|
|
490
|
+
send_code(s, curlen, s.bl_tree);
|
|
491
|
+
while (--count !== 0);
|
|
492
|
+
else if (curlen !== 0) {
|
|
493
|
+
if (curlen !== prevlen) {
|
|
494
|
+
send_code(s, curlen, s.bl_tree);
|
|
495
|
+
count--;
|
|
496
|
+
}
|
|
497
|
+
send_code(s, REP_3_6, s.bl_tree);
|
|
498
|
+
send_bits(s, count - 3, 2);
|
|
499
|
+
} else if (count <= 10) {
|
|
500
|
+
send_code(s, REPZ_3_10, s.bl_tree);
|
|
501
|
+
send_bits(s, count - 3, 3);
|
|
502
|
+
} else {
|
|
503
|
+
send_code(s, REPZ_11_138, s.bl_tree);
|
|
504
|
+
send_bits(s, count - 11, 7);
|
|
505
|
+
}
|
|
506
|
+
count = 0;
|
|
507
|
+
prevlen = curlen;
|
|
508
|
+
if (nextlen === 0) {
|
|
509
|
+
max_count = 138;
|
|
510
|
+
min_count = 3;
|
|
511
|
+
} else if (curlen === nextlen) {
|
|
512
|
+
max_count = 6;
|
|
513
|
+
min_count = 3;
|
|
514
|
+
} else {
|
|
515
|
+
max_count = 7;
|
|
516
|
+
min_count = 4;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
const build_bl_tree = (s) => {
|
|
521
|
+
let max_blindex;
|
|
522
|
+
scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
|
|
523
|
+
scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
|
|
524
|
+
build_tree(s, s.bl_desc);
|
|
525
|
+
for (max_blindex = BL_CODES$1 - 1; max_blindex >= 3; max_blindex--) if (s.bl_tree[bl_order[max_blindex] * 2 + 1] !== 0) break;
|
|
526
|
+
s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
|
|
527
|
+
return max_blindex;
|
|
528
|
+
};
|
|
529
|
+
const send_all_trees = (s, lcodes, dcodes, blcodes) => {
|
|
530
|
+
let rank$1;
|
|
531
|
+
send_bits(s, lcodes - 257, 5);
|
|
532
|
+
send_bits(s, dcodes - 1, 5);
|
|
533
|
+
send_bits(s, blcodes - 4, 4);
|
|
534
|
+
for (rank$1 = 0; rank$1 < blcodes; rank$1++) send_bits(s, s.bl_tree[bl_order[rank$1] * 2 + 1], 3);
|
|
535
|
+
send_tree(s, s.dyn_ltree, lcodes - 1);
|
|
536
|
+
send_tree(s, s.dyn_dtree, dcodes - 1);
|
|
537
|
+
};
|
|
538
|
+
const detect_data_type = (s) => {
|
|
539
|
+
let block_mask = 4093624447;
|
|
540
|
+
let n;
|
|
541
|
+
for (n = 0; n <= 31; n++, block_mask >>>= 1) if (block_mask & 1 && s.dyn_ltree[n * 2] !== 0) return Z_BINARY;
|
|
542
|
+
if (s.dyn_ltree[18] !== 0 || s.dyn_ltree[20] !== 0 || s.dyn_ltree[26] !== 0) return Z_TEXT;
|
|
543
|
+
for (n = 32; n < LITERALS$1; n++) if (s.dyn_ltree[n * 2] !== 0) return Z_TEXT;
|
|
544
|
+
return Z_BINARY;
|
|
545
|
+
};
|
|
546
|
+
let static_init_done = false;
|
|
547
|
+
const _tr_init$1 = (s) => {
|
|
548
|
+
if (!static_init_done) {
|
|
549
|
+
tr_static_init();
|
|
550
|
+
static_init_done = true;
|
|
551
|
+
}
|
|
552
|
+
s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
|
|
553
|
+
s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
|
|
554
|
+
s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
|
|
555
|
+
s.bi_buf = 0;
|
|
556
|
+
s.bi_valid = 0;
|
|
557
|
+
init_block(s);
|
|
558
|
+
};
|
|
559
|
+
const _tr_stored_block$1 = (s, buf, stored_len, last) => {
|
|
560
|
+
send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3);
|
|
561
|
+
bi_windup(s);
|
|
562
|
+
put_short(s, stored_len);
|
|
563
|
+
put_short(s, ~stored_len);
|
|
564
|
+
if (stored_len) s.pending_buf.set(s.window.subarray(buf, buf + stored_len), s.pending);
|
|
565
|
+
s.pending += stored_len;
|
|
566
|
+
};
|
|
567
|
+
const _tr_align$1 = (s) => {
|
|
568
|
+
send_bits(s, STATIC_TREES << 1, 3);
|
|
569
|
+
send_code(s, END_BLOCK, static_ltree);
|
|
570
|
+
bi_flush(s);
|
|
571
|
+
};
|
|
572
|
+
const _tr_flush_block$1 = (s, buf, stored_len, last) => {
|
|
573
|
+
let opt_lenb, static_lenb;
|
|
574
|
+
let max_blindex = 0;
|
|
575
|
+
if (s.level > 0) {
|
|
576
|
+
if (s.strm.data_type === Z_UNKNOWN$1) s.strm.data_type = detect_data_type(s);
|
|
577
|
+
build_tree(s, s.l_desc);
|
|
578
|
+
build_tree(s, s.d_desc);
|
|
579
|
+
max_blindex = build_bl_tree(s);
|
|
580
|
+
opt_lenb = s.opt_len + 3 + 7 >>> 3;
|
|
581
|
+
static_lenb = s.static_len + 3 + 7 >>> 3;
|
|
582
|
+
if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
|
|
583
|
+
} else opt_lenb = static_lenb = stored_len + 5;
|
|
584
|
+
if (stored_len + 4 <= opt_lenb && buf !== -1) _tr_stored_block$1(s, buf, stored_len, last);
|
|
585
|
+
else if (s.strategy === Z_FIXED$1 || static_lenb === opt_lenb) {
|
|
586
|
+
send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
|
|
587
|
+
compress_block(s, static_ltree, static_dtree);
|
|
588
|
+
} else {
|
|
589
|
+
send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
|
|
590
|
+
send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
|
|
591
|
+
compress_block(s, s.dyn_ltree, s.dyn_dtree);
|
|
592
|
+
}
|
|
593
|
+
init_block(s);
|
|
594
|
+
if (last) bi_windup(s);
|
|
595
|
+
};
|
|
596
|
+
const _tr_tally$1 = (s, dist, lc) => {
|
|
597
|
+
s.pending_buf[s.sym_buf + s.sym_next++] = dist;
|
|
598
|
+
s.pending_buf[s.sym_buf + s.sym_next++] = dist >> 8;
|
|
599
|
+
s.pending_buf[s.sym_buf + s.sym_next++] = lc;
|
|
600
|
+
if (dist === 0) s.dyn_ltree[lc * 2]++;
|
|
601
|
+
else {
|
|
602
|
+
s.matches++;
|
|
603
|
+
dist--;
|
|
604
|
+
s.dyn_ltree[(_length_code[lc] + LITERALS$1 + 1) * 2]++;
|
|
605
|
+
s.dyn_dtree[d_code(dist) * 2]++;
|
|
606
|
+
}
|
|
607
|
+
return s.sym_next === s.sym_end;
|
|
608
|
+
};
|
|
609
|
+
var trees = {
|
|
610
|
+
_tr_init: _tr_init$1,
|
|
611
|
+
_tr_stored_block: _tr_stored_block$1,
|
|
612
|
+
_tr_flush_block: _tr_flush_block$1,
|
|
613
|
+
_tr_tally: _tr_tally$1,
|
|
614
|
+
_tr_align: _tr_align$1
|
|
615
|
+
};
|
|
616
|
+
const adler32 = (adler, buf, len, pos) => {
|
|
617
|
+
let s1 = adler & 65535 | 0, s2 = adler >>> 16 & 65535 | 0, n = 0;
|
|
618
|
+
while (len !== 0) {
|
|
619
|
+
n = len > 2e3 ? 2e3 : len;
|
|
620
|
+
len -= n;
|
|
621
|
+
do {
|
|
622
|
+
s1 = s1 + buf[pos++] | 0;
|
|
623
|
+
s2 = s2 + s1 | 0;
|
|
624
|
+
} while (--n);
|
|
625
|
+
s1 %= 65521;
|
|
626
|
+
s2 %= 65521;
|
|
627
|
+
}
|
|
628
|
+
return s1 | s2 << 16 | 0;
|
|
629
|
+
};
|
|
630
|
+
var adler32_1 = adler32;
|
|
631
|
+
const makeTable = () => {
|
|
632
|
+
let c, table = [];
|
|
633
|
+
for (var n = 0; n < 256; n++) {
|
|
634
|
+
c = n;
|
|
635
|
+
for (var k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
636
|
+
table[n] = c;
|
|
637
|
+
}
|
|
638
|
+
return table;
|
|
639
|
+
};
|
|
640
|
+
const crcTable = new Uint32Array(makeTable());
|
|
641
|
+
const crc32 = (crc, buf, len, pos) => {
|
|
642
|
+
const t = crcTable;
|
|
643
|
+
const end = pos + len;
|
|
644
|
+
crc ^= -1;
|
|
645
|
+
for (let i = pos; i < end; i++) crc = crc >>> 8 ^ t[(crc ^ buf[i]) & 255];
|
|
646
|
+
return crc ^ -1;
|
|
647
|
+
};
|
|
648
|
+
var crc32_1 = crc32;
|
|
649
|
+
var messages = {
|
|
650
|
+
2: "need dictionary",
|
|
651
|
+
1: "stream end",
|
|
652
|
+
0: "",
|
|
653
|
+
"-1": "file error",
|
|
654
|
+
"-2": "stream error",
|
|
655
|
+
"-3": "data error",
|
|
656
|
+
"-4": "insufficient memory",
|
|
657
|
+
"-5": "buffer error",
|
|
658
|
+
"-6": "incompatible version"
|
|
659
|
+
};
|
|
660
|
+
var constants$2 = {
|
|
661
|
+
Z_NO_FLUSH: 0,
|
|
662
|
+
Z_PARTIAL_FLUSH: 1,
|
|
663
|
+
Z_SYNC_FLUSH: 2,
|
|
664
|
+
Z_FULL_FLUSH: 3,
|
|
665
|
+
Z_FINISH: 4,
|
|
666
|
+
Z_BLOCK: 5,
|
|
667
|
+
Z_TREES: 6,
|
|
668
|
+
Z_OK: 0,
|
|
669
|
+
Z_STREAM_END: 1,
|
|
670
|
+
Z_NEED_DICT: 2,
|
|
671
|
+
Z_ERRNO: -1,
|
|
672
|
+
Z_STREAM_ERROR: -2,
|
|
673
|
+
Z_DATA_ERROR: -3,
|
|
674
|
+
Z_MEM_ERROR: -4,
|
|
675
|
+
Z_BUF_ERROR: -5,
|
|
676
|
+
Z_NO_COMPRESSION: 0,
|
|
677
|
+
Z_BEST_SPEED: 1,
|
|
678
|
+
Z_BEST_COMPRESSION: 9,
|
|
679
|
+
Z_DEFAULT_COMPRESSION: -1,
|
|
680
|
+
Z_FILTERED: 1,
|
|
681
|
+
Z_HUFFMAN_ONLY: 2,
|
|
682
|
+
Z_RLE: 3,
|
|
683
|
+
Z_FIXED: 4,
|
|
684
|
+
Z_DEFAULT_STRATEGY: 0,
|
|
685
|
+
Z_BINARY: 0,
|
|
686
|
+
Z_TEXT: 1,
|
|
687
|
+
Z_UNKNOWN: 2,
|
|
688
|
+
Z_DEFLATED: 8
|
|
689
|
+
};
|
|
690
|
+
const { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align } = trees;
|
|
691
|
+
const { Z_NO_FLUSH: Z_NO_FLUSH$2, Z_PARTIAL_FLUSH, Z_FULL_FLUSH: Z_FULL_FLUSH$1, Z_FINISH: Z_FINISH$3, Z_BLOCK: Z_BLOCK$1, Z_OK: Z_OK$3, Z_STREAM_END: Z_STREAM_END$3, Z_STREAM_ERROR: Z_STREAM_ERROR$2, Z_DATA_ERROR: Z_DATA_ERROR$2, Z_BUF_ERROR: Z_BUF_ERROR$1, Z_DEFAULT_COMPRESSION: Z_DEFAULT_COMPRESSION$1, Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE, Z_FIXED, Z_DEFAULT_STRATEGY: Z_DEFAULT_STRATEGY$1, Z_UNKNOWN, Z_DEFLATED: Z_DEFLATED$2 } = constants$2;
|
|
692
|
+
const MAX_MEM_LEVEL = 9;
|
|
693
|
+
const MAX_WBITS$1 = 15;
|
|
694
|
+
const DEF_MEM_LEVEL = 8;
|
|
695
|
+
const L_CODES = 286;
|
|
696
|
+
const D_CODES = 30;
|
|
697
|
+
const BL_CODES = 19;
|
|
698
|
+
const HEAP_SIZE = 2 * L_CODES + 1;
|
|
699
|
+
const MAX_BITS = 15;
|
|
700
|
+
const MIN_MATCH = 3;
|
|
701
|
+
const MAX_MATCH = 258;
|
|
702
|
+
const MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1;
|
|
703
|
+
const PRESET_DICT = 32;
|
|
704
|
+
const INIT_STATE = 42;
|
|
705
|
+
const GZIP_STATE = 57;
|
|
706
|
+
const EXTRA_STATE = 69;
|
|
707
|
+
const NAME_STATE = 73;
|
|
708
|
+
const COMMENT_STATE = 91;
|
|
709
|
+
const HCRC_STATE = 103;
|
|
710
|
+
const BUSY_STATE = 113;
|
|
711
|
+
const FINISH_STATE = 666;
|
|
712
|
+
const BS_NEED_MORE = 1;
|
|
713
|
+
const BS_BLOCK_DONE = 2;
|
|
714
|
+
const BS_FINISH_STARTED = 3;
|
|
715
|
+
const BS_FINISH_DONE = 4;
|
|
716
|
+
const OS_CODE = 3;
|
|
717
|
+
const err = (strm, errorCode) => {
|
|
718
|
+
strm.msg = messages[errorCode];
|
|
719
|
+
return errorCode;
|
|
720
|
+
};
|
|
721
|
+
const rank = (f) => {
|
|
722
|
+
return f * 2 - (f > 4 ? 9 : 0);
|
|
723
|
+
};
|
|
724
|
+
const zero = (buf) => {
|
|
725
|
+
let len = buf.length;
|
|
726
|
+
while (--len >= 0) buf[len] = 0;
|
|
727
|
+
};
|
|
728
|
+
const slide_hash = (s) => {
|
|
729
|
+
let n, m;
|
|
730
|
+
let p;
|
|
731
|
+
let wsize = s.w_size;
|
|
732
|
+
n = s.hash_size;
|
|
733
|
+
p = n;
|
|
734
|
+
do {
|
|
735
|
+
m = s.head[--p];
|
|
736
|
+
s.head[p] = m >= wsize ? m - wsize : 0;
|
|
737
|
+
} while (--n);
|
|
738
|
+
n = wsize;
|
|
739
|
+
p = n;
|
|
740
|
+
do {
|
|
741
|
+
m = s.prev[--p];
|
|
742
|
+
s.prev[p] = m >= wsize ? m - wsize : 0;
|
|
743
|
+
} while (--n);
|
|
744
|
+
};
|
|
745
|
+
let HASH_ZLIB = (s, prev, data) => (prev << s.hash_shift ^ data) & s.hash_mask;
|
|
746
|
+
let HASH = HASH_ZLIB;
|
|
747
|
+
const flush_pending = (strm) => {
|
|
748
|
+
const s = strm.state;
|
|
749
|
+
let len = s.pending;
|
|
750
|
+
if (len > strm.avail_out) len = strm.avail_out;
|
|
751
|
+
if (len === 0) return;
|
|
752
|
+
strm.output.set(s.pending_buf.subarray(s.pending_out, s.pending_out + len), strm.next_out);
|
|
753
|
+
strm.next_out += len;
|
|
754
|
+
s.pending_out += len;
|
|
755
|
+
strm.total_out += len;
|
|
756
|
+
strm.avail_out -= len;
|
|
757
|
+
s.pending -= len;
|
|
758
|
+
if (s.pending === 0) s.pending_out = 0;
|
|
759
|
+
};
|
|
760
|
+
const flush_block_only = (s, last) => {
|
|
761
|
+
_tr_flush_block(s, s.block_start >= 0 ? s.block_start : -1, s.strstart - s.block_start, last);
|
|
762
|
+
s.block_start = s.strstart;
|
|
763
|
+
flush_pending(s.strm);
|
|
764
|
+
};
|
|
765
|
+
const put_byte = (s, b) => {
|
|
766
|
+
s.pending_buf[s.pending++] = b;
|
|
767
|
+
};
|
|
768
|
+
const putShortMSB = (s, b) => {
|
|
769
|
+
s.pending_buf[s.pending++] = b >>> 8 & 255;
|
|
770
|
+
s.pending_buf[s.pending++] = b & 255;
|
|
771
|
+
};
|
|
772
|
+
const read_buf = (strm, buf, start, size) => {
|
|
773
|
+
let len = strm.avail_in;
|
|
774
|
+
if (len > size) len = size;
|
|
775
|
+
if (len === 0) return 0;
|
|
776
|
+
strm.avail_in -= len;
|
|
777
|
+
buf.set(strm.input.subarray(strm.next_in, strm.next_in + len), start);
|
|
778
|
+
if (strm.state.wrap === 1) strm.adler = adler32_1(strm.adler, buf, len, start);
|
|
779
|
+
else if (strm.state.wrap === 2) strm.adler = crc32_1(strm.adler, buf, len, start);
|
|
780
|
+
strm.next_in += len;
|
|
781
|
+
strm.total_in += len;
|
|
782
|
+
return len;
|
|
783
|
+
};
|
|
784
|
+
const longest_match = (s, cur_match) => {
|
|
785
|
+
let chain_length = s.max_chain_length;
|
|
786
|
+
let scan = s.strstart;
|
|
787
|
+
let match;
|
|
788
|
+
let len;
|
|
789
|
+
let best_len = s.prev_length;
|
|
790
|
+
let nice_match = s.nice_match;
|
|
791
|
+
const limit = s.strstart > s.w_size - MIN_LOOKAHEAD ? s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0;
|
|
792
|
+
const _win = s.window;
|
|
793
|
+
const wmask = s.w_mask;
|
|
794
|
+
const prev = s.prev;
|
|
795
|
+
const strend = s.strstart + MAX_MATCH;
|
|
796
|
+
let scan_end1 = _win[scan + best_len - 1];
|
|
797
|
+
let scan_end = _win[scan + best_len];
|
|
798
|
+
if (s.prev_length >= s.good_match) chain_length >>= 2;
|
|
799
|
+
if (nice_match > s.lookahead) nice_match = s.lookahead;
|
|
800
|
+
do {
|
|
801
|
+
match = cur_match;
|
|
802
|
+
if (_win[match + best_len] !== scan_end || _win[match + best_len - 1] !== scan_end1 || _win[match] !== _win[scan] || _win[++match] !== _win[scan + 1]) continue;
|
|
803
|
+
scan += 2;
|
|
804
|
+
match++;
|
|
805
|
+
do ;
|
|
806
|
+
while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && scan < strend);
|
|
807
|
+
len = MAX_MATCH - (strend - scan);
|
|
808
|
+
scan = strend - MAX_MATCH;
|
|
809
|
+
if (len > best_len) {
|
|
810
|
+
s.match_start = cur_match;
|
|
811
|
+
best_len = len;
|
|
812
|
+
if (len >= nice_match) break;
|
|
813
|
+
scan_end1 = _win[scan + best_len - 1];
|
|
814
|
+
scan_end = _win[scan + best_len];
|
|
815
|
+
}
|
|
816
|
+
} while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
|
|
817
|
+
if (best_len <= s.lookahead) return best_len;
|
|
818
|
+
return s.lookahead;
|
|
819
|
+
};
|
|
820
|
+
const fill_window = (s) => {
|
|
821
|
+
const _w_size = s.w_size;
|
|
822
|
+
let n, more, str;
|
|
823
|
+
do {
|
|
824
|
+
more = s.window_size - s.lookahead - s.strstart;
|
|
825
|
+
if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
|
|
826
|
+
s.window.set(s.window.subarray(_w_size, _w_size + _w_size - more), 0);
|
|
827
|
+
s.match_start -= _w_size;
|
|
828
|
+
s.strstart -= _w_size;
|
|
829
|
+
s.block_start -= _w_size;
|
|
830
|
+
if (s.insert > s.strstart) s.insert = s.strstart;
|
|
831
|
+
slide_hash(s);
|
|
832
|
+
more += _w_size;
|
|
833
|
+
}
|
|
834
|
+
if (s.strm.avail_in === 0) break;
|
|
835
|
+
n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
|
|
836
|
+
s.lookahead += n;
|
|
837
|
+
if (s.lookahead + s.insert >= MIN_MATCH) {
|
|
838
|
+
str = s.strstart - s.insert;
|
|
839
|
+
s.ins_h = s.window[str];
|
|
840
|
+
s.ins_h = HASH(s, s.ins_h, s.window[str + 1]);
|
|
841
|
+
while (s.insert) {
|
|
842
|
+
s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
|
|
843
|
+
s.prev[str & s.w_mask] = s.head[s.ins_h];
|
|
844
|
+
s.head[s.ins_h] = str;
|
|
845
|
+
str++;
|
|
846
|
+
s.insert--;
|
|
847
|
+
if (s.lookahead + s.insert < MIN_MATCH) break;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
} while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
|
|
851
|
+
};
|
|
852
|
+
const deflate_stored = (s, flush) => {
|
|
853
|
+
let min_block = s.pending_buf_size - 5 > s.w_size ? s.w_size : s.pending_buf_size - 5;
|
|
854
|
+
let len, left, have, last = 0;
|
|
855
|
+
let used = s.strm.avail_in;
|
|
856
|
+
do {
|
|
857
|
+
len = 65535;
|
|
858
|
+
have = s.bi_valid + 42 >> 3;
|
|
859
|
+
if (s.strm.avail_out < have) break;
|
|
860
|
+
have = s.strm.avail_out - have;
|
|
861
|
+
left = s.strstart - s.block_start;
|
|
862
|
+
if (len > left + s.strm.avail_in) len = left + s.strm.avail_in;
|
|
863
|
+
if (len > have) len = have;
|
|
864
|
+
if (len < min_block && (len === 0 && flush !== Z_FINISH$3 || flush === Z_NO_FLUSH$2 || len !== left + s.strm.avail_in)) break;
|
|
865
|
+
last = flush === Z_FINISH$3 && len === left + s.strm.avail_in ? 1 : 0;
|
|
866
|
+
_tr_stored_block(s, 0, 0, last);
|
|
867
|
+
s.pending_buf[s.pending - 4] = len;
|
|
868
|
+
s.pending_buf[s.pending - 3] = len >> 8;
|
|
869
|
+
s.pending_buf[s.pending - 2] = ~len;
|
|
870
|
+
s.pending_buf[s.pending - 1] = ~len >> 8;
|
|
871
|
+
flush_pending(s.strm);
|
|
872
|
+
if (left) {
|
|
873
|
+
if (left > len) left = len;
|
|
874
|
+
s.strm.output.set(s.window.subarray(s.block_start, s.block_start + left), s.strm.next_out);
|
|
875
|
+
s.strm.next_out += left;
|
|
876
|
+
s.strm.avail_out -= left;
|
|
877
|
+
s.strm.total_out += left;
|
|
878
|
+
s.block_start += left;
|
|
879
|
+
len -= left;
|
|
880
|
+
}
|
|
881
|
+
if (len) {
|
|
882
|
+
read_buf(s.strm, s.strm.output, s.strm.next_out, len);
|
|
883
|
+
s.strm.next_out += len;
|
|
884
|
+
s.strm.avail_out -= len;
|
|
885
|
+
s.strm.total_out += len;
|
|
886
|
+
}
|
|
887
|
+
} while (last === 0);
|
|
888
|
+
used -= s.strm.avail_in;
|
|
889
|
+
if (used) {
|
|
890
|
+
if (used >= s.w_size) {
|
|
891
|
+
s.matches = 2;
|
|
892
|
+
s.window.set(s.strm.input.subarray(s.strm.next_in - s.w_size, s.strm.next_in), 0);
|
|
893
|
+
s.strstart = s.w_size;
|
|
894
|
+
s.insert = s.strstart;
|
|
895
|
+
} else {
|
|
896
|
+
if (s.window_size - s.strstart <= used) {
|
|
897
|
+
s.strstart -= s.w_size;
|
|
898
|
+
s.window.set(s.window.subarray(s.w_size, s.w_size + s.strstart), 0);
|
|
899
|
+
if (s.matches < 2) s.matches++;
|
|
900
|
+
if (s.insert > s.strstart) s.insert = s.strstart;
|
|
901
|
+
}
|
|
902
|
+
s.window.set(s.strm.input.subarray(s.strm.next_in - used, s.strm.next_in), s.strstart);
|
|
903
|
+
s.strstart += used;
|
|
904
|
+
s.insert += used > s.w_size - s.insert ? s.w_size - s.insert : used;
|
|
905
|
+
}
|
|
906
|
+
s.block_start = s.strstart;
|
|
907
|
+
}
|
|
908
|
+
if (s.high_water < s.strstart) s.high_water = s.strstart;
|
|
909
|
+
if (last) return BS_FINISH_DONE;
|
|
910
|
+
if (flush !== Z_NO_FLUSH$2 && flush !== Z_FINISH$3 && s.strm.avail_in === 0 && s.strstart === s.block_start) return BS_BLOCK_DONE;
|
|
911
|
+
have = s.window_size - s.strstart;
|
|
912
|
+
if (s.strm.avail_in > have && s.block_start >= s.w_size) {
|
|
913
|
+
s.block_start -= s.w_size;
|
|
914
|
+
s.strstart -= s.w_size;
|
|
915
|
+
s.window.set(s.window.subarray(s.w_size, s.w_size + s.strstart), 0);
|
|
916
|
+
if (s.matches < 2) s.matches++;
|
|
917
|
+
have += s.w_size;
|
|
918
|
+
if (s.insert > s.strstart) s.insert = s.strstart;
|
|
919
|
+
}
|
|
920
|
+
if (have > s.strm.avail_in) have = s.strm.avail_in;
|
|
921
|
+
if (have) {
|
|
922
|
+
read_buf(s.strm, s.window, s.strstart, have);
|
|
923
|
+
s.strstart += have;
|
|
924
|
+
s.insert += have > s.w_size - s.insert ? s.w_size - s.insert : have;
|
|
925
|
+
}
|
|
926
|
+
if (s.high_water < s.strstart) s.high_water = s.strstart;
|
|
927
|
+
have = s.bi_valid + 42 >> 3;
|
|
928
|
+
have = s.pending_buf_size - have > 65535 ? 65535 : s.pending_buf_size - have;
|
|
929
|
+
min_block = have > s.w_size ? s.w_size : have;
|
|
930
|
+
left = s.strstart - s.block_start;
|
|
931
|
+
if (left >= min_block || (left || flush === Z_FINISH$3) && flush !== Z_NO_FLUSH$2 && s.strm.avail_in === 0 && left <= have) {
|
|
932
|
+
len = left > have ? have : left;
|
|
933
|
+
last = flush === Z_FINISH$3 && s.strm.avail_in === 0 && len === left ? 1 : 0;
|
|
934
|
+
_tr_stored_block(s, s.block_start, len, last);
|
|
935
|
+
s.block_start += len;
|
|
936
|
+
flush_pending(s.strm);
|
|
937
|
+
}
|
|
938
|
+
return last ? BS_FINISH_STARTED : BS_NEED_MORE;
|
|
939
|
+
};
|
|
940
|
+
const deflate_fast = (s, flush) => {
|
|
941
|
+
let hash_head;
|
|
942
|
+
let bflush;
|
|
943
|
+
for (;;) {
|
|
944
|
+
if (s.lookahead < MIN_LOOKAHEAD) {
|
|
945
|
+
fill_window(s);
|
|
946
|
+
if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$2) return BS_NEED_MORE;
|
|
947
|
+
if (s.lookahead === 0) break;
|
|
948
|
+
}
|
|
949
|
+
hash_head = 0;
|
|
950
|
+
if (s.lookahead >= MIN_MATCH) {
|
|
951
|
+
/*** INSERT_STRING(s, s.strstart, hash_head); ***/
|
|
952
|
+
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
|
|
953
|
+
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
|
|
954
|
+
s.head[s.ins_h] = s.strstart;
|
|
955
|
+
}
|
|
956
|
+
if (hash_head !== 0 && s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD) s.match_length = longest_match(s, hash_head);
|
|
957
|
+
if (s.match_length >= MIN_MATCH) {
|
|
958
|
+
/*** _tr_tally_dist(s, s.strstart - s.match_start,
|
|
959
|
+
s.match_length - MIN_MATCH, bflush); ***/
|
|
960
|
+
bflush = _tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
|
|
961
|
+
s.lookahead -= s.match_length;
|
|
962
|
+
if (s.match_length <= s.max_lazy_match && s.lookahead >= MIN_MATCH) {
|
|
963
|
+
s.match_length--;
|
|
964
|
+
do {
|
|
965
|
+
s.strstart++;
|
|
966
|
+
/*** INSERT_STRING(s, s.strstart, hash_head); ***/
|
|
967
|
+
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
|
|
968
|
+
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
|
|
969
|
+
s.head[s.ins_h] = s.strstart;
|
|
970
|
+
} while (--s.match_length !== 0);
|
|
971
|
+
s.strstart++;
|
|
972
|
+
} else {
|
|
973
|
+
s.strstart += s.match_length;
|
|
974
|
+
s.match_length = 0;
|
|
975
|
+
s.ins_h = s.window[s.strstart];
|
|
976
|
+
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + 1]);
|
|
977
|
+
}
|
|
978
|
+
} else {
|
|
979
|
+
/*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
|
|
980
|
+
bflush = _tr_tally(s, 0, s.window[s.strstart]);
|
|
981
|
+
s.lookahead--;
|
|
982
|
+
s.strstart++;
|
|
983
|
+
}
|
|
984
|
+
if (bflush) {
|
|
985
|
+
/*** FLUSH_BLOCK(s, 0); ***/
|
|
986
|
+
flush_block_only(s, false);
|
|
987
|
+
if (s.strm.avail_out === 0) return BS_NEED_MORE;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
|
|
991
|
+
if (flush === Z_FINISH$3) {
|
|
992
|
+
/*** FLUSH_BLOCK(s, 1); ***/
|
|
993
|
+
flush_block_only(s, true);
|
|
994
|
+
if (s.strm.avail_out === 0) return BS_FINISH_STARTED;
|
|
995
|
+
return BS_FINISH_DONE;
|
|
996
|
+
}
|
|
997
|
+
if (s.sym_next) {
|
|
998
|
+
/*** FLUSH_BLOCK(s, 0); ***/
|
|
999
|
+
flush_block_only(s, false);
|
|
1000
|
+
if (s.strm.avail_out === 0) return BS_NEED_MORE;
|
|
1001
|
+
}
|
|
1002
|
+
return BS_BLOCK_DONE;
|
|
1003
|
+
};
|
|
1004
|
+
const deflate_slow = (s, flush) => {
|
|
1005
|
+
let hash_head;
|
|
1006
|
+
let bflush;
|
|
1007
|
+
let max_insert;
|
|
1008
|
+
for (;;) {
|
|
1009
|
+
if (s.lookahead < MIN_LOOKAHEAD) {
|
|
1010
|
+
fill_window(s);
|
|
1011
|
+
if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$2) return BS_NEED_MORE;
|
|
1012
|
+
if (s.lookahead === 0) break;
|
|
1013
|
+
}
|
|
1014
|
+
hash_head = 0;
|
|
1015
|
+
if (s.lookahead >= MIN_MATCH) {
|
|
1016
|
+
/*** INSERT_STRING(s, s.strstart, hash_head); ***/
|
|
1017
|
+
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
|
|
1018
|
+
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
|
|
1019
|
+
s.head[s.ins_h] = s.strstart;
|
|
1020
|
+
}
|
|
1021
|
+
s.prev_length = s.match_length;
|
|
1022
|
+
s.prev_match = s.match_start;
|
|
1023
|
+
s.match_length = MIN_MATCH - 1;
|
|
1024
|
+
if (hash_head !== 0 && s.prev_length < s.max_lazy_match && s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD) {
|
|
1025
|
+
s.match_length = longest_match(s, hash_head);
|
|
1026
|
+
if (s.match_length <= 5 && (s.strategy === Z_FILTERED || s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096)) s.match_length = MIN_MATCH - 1;
|
|
1027
|
+
}
|
|
1028
|
+
if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
|
|
1029
|
+
max_insert = s.strstart + s.lookahead - MIN_MATCH;
|
|
1030
|
+
/***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
|
|
1031
|
+
s.prev_length - MIN_MATCH, bflush);***/
|
|
1032
|
+
bflush = _tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
|
|
1033
|
+
s.lookahead -= s.prev_length - 1;
|
|
1034
|
+
s.prev_length -= 2;
|
|
1035
|
+
do
|
|
1036
|
+
if (++s.strstart <= max_insert) {
|
|
1037
|
+
/*** INSERT_STRING(s, s.strstart, hash_head); ***/
|
|
1038
|
+
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
|
|
1039
|
+
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
|
|
1040
|
+
s.head[s.ins_h] = s.strstart;
|
|
1041
|
+
}
|
|
1042
|
+
while (--s.prev_length !== 0);
|
|
1043
|
+
s.match_available = 0;
|
|
1044
|
+
s.match_length = MIN_MATCH - 1;
|
|
1045
|
+
s.strstart++;
|
|
1046
|
+
if (bflush) {
|
|
1047
|
+
/*** FLUSH_BLOCK(s, 0); ***/
|
|
1048
|
+
flush_block_only(s, false);
|
|
1049
|
+
if (s.strm.avail_out === 0) return BS_NEED_MORE;
|
|
1050
|
+
}
|
|
1051
|
+
} else if (s.match_available) {
|
|
1052
|
+
/*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
|
|
1053
|
+
bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
|
|
1054
|
+
if (bflush)
|
|
1055
|
+
/*** FLUSH_BLOCK_ONLY(s, 0) ***/
|
|
1056
|
+
flush_block_only(s, false);
|
|
1057
|
+
s.strstart++;
|
|
1058
|
+
s.lookahead--;
|
|
1059
|
+
if (s.strm.avail_out === 0) return BS_NEED_MORE;
|
|
1060
|
+
} else {
|
|
1061
|
+
s.match_available = 1;
|
|
1062
|
+
s.strstart++;
|
|
1063
|
+
s.lookahead--;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
if (s.match_available) {
|
|
1067
|
+
/*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
|
|
1068
|
+
bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
|
|
1069
|
+
s.match_available = 0;
|
|
1070
|
+
}
|
|
1071
|
+
s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
|
|
1072
|
+
if (flush === Z_FINISH$3) {
|
|
1073
|
+
/*** FLUSH_BLOCK(s, 1); ***/
|
|
1074
|
+
flush_block_only(s, true);
|
|
1075
|
+
if (s.strm.avail_out === 0) return BS_FINISH_STARTED;
|
|
1076
|
+
return BS_FINISH_DONE;
|
|
1077
|
+
}
|
|
1078
|
+
if (s.sym_next) {
|
|
1079
|
+
/*** FLUSH_BLOCK(s, 0); ***/
|
|
1080
|
+
flush_block_only(s, false);
|
|
1081
|
+
if (s.strm.avail_out === 0) return BS_NEED_MORE;
|
|
1082
|
+
}
|
|
1083
|
+
return BS_BLOCK_DONE;
|
|
1084
|
+
};
|
|
1085
|
+
const deflate_rle = (s, flush) => {
|
|
1086
|
+
let bflush;
|
|
1087
|
+
let prev;
|
|
1088
|
+
let scan, strend;
|
|
1089
|
+
const _win = s.window;
|
|
1090
|
+
for (;;) {
|
|
1091
|
+
if (s.lookahead <= MAX_MATCH) {
|
|
1092
|
+
fill_window(s);
|
|
1093
|
+
if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH$2) return BS_NEED_MORE;
|
|
1094
|
+
if (s.lookahead === 0) break;
|
|
1095
|
+
}
|
|
1096
|
+
s.match_length = 0;
|
|
1097
|
+
if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
|
|
1098
|
+
scan = s.strstart - 1;
|
|
1099
|
+
prev = _win[scan];
|
|
1100
|
+
if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
|
|
1101
|
+
strend = s.strstart + MAX_MATCH;
|
|
1102
|
+
do ;
|
|
1103
|
+
while (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && scan < strend);
|
|
1104
|
+
s.match_length = MAX_MATCH - (strend - scan);
|
|
1105
|
+
if (s.match_length > s.lookahead) s.match_length = s.lookahead;
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
if (s.match_length >= MIN_MATCH) {
|
|
1109
|
+
/*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
|
|
1110
|
+
bflush = _tr_tally(s, 1, s.match_length - MIN_MATCH);
|
|
1111
|
+
s.lookahead -= s.match_length;
|
|
1112
|
+
s.strstart += s.match_length;
|
|
1113
|
+
s.match_length = 0;
|
|
1114
|
+
} else {
|
|
1115
|
+
/*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
|
|
1116
|
+
bflush = _tr_tally(s, 0, s.window[s.strstart]);
|
|
1117
|
+
s.lookahead--;
|
|
1118
|
+
s.strstart++;
|
|
1119
|
+
}
|
|
1120
|
+
if (bflush) {
|
|
1121
|
+
/*** FLUSH_BLOCK(s, 0); ***/
|
|
1122
|
+
flush_block_only(s, false);
|
|
1123
|
+
if (s.strm.avail_out === 0) return BS_NEED_MORE;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
s.insert = 0;
|
|
1127
|
+
if (flush === Z_FINISH$3) {
|
|
1128
|
+
/*** FLUSH_BLOCK(s, 1); ***/
|
|
1129
|
+
flush_block_only(s, true);
|
|
1130
|
+
if (s.strm.avail_out === 0) return BS_FINISH_STARTED;
|
|
1131
|
+
return BS_FINISH_DONE;
|
|
1132
|
+
}
|
|
1133
|
+
if (s.sym_next) {
|
|
1134
|
+
/*** FLUSH_BLOCK(s, 0); ***/
|
|
1135
|
+
flush_block_only(s, false);
|
|
1136
|
+
if (s.strm.avail_out === 0) return BS_NEED_MORE;
|
|
1137
|
+
}
|
|
1138
|
+
return BS_BLOCK_DONE;
|
|
1139
|
+
};
|
|
1140
|
+
const deflate_huff = (s, flush) => {
|
|
1141
|
+
let bflush;
|
|
1142
|
+
for (;;) {
|
|
1143
|
+
if (s.lookahead === 0) {
|
|
1144
|
+
fill_window(s);
|
|
1145
|
+
if (s.lookahead === 0) {
|
|
1146
|
+
if (flush === Z_NO_FLUSH$2) return BS_NEED_MORE;
|
|
1147
|
+
break;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
s.match_length = 0;
|
|
1151
|
+
/*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
|
|
1152
|
+
bflush = _tr_tally(s, 0, s.window[s.strstart]);
|
|
1153
|
+
s.lookahead--;
|
|
1154
|
+
s.strstart++;
|
|
1155
|
+
if (bflush) {
|
|
1156
|
+
/*** FLUSH_BLOCK(s, 0); ***/
|
|
1157
|
+
flush_block_only(s, false);
|
|
1158
|
+
if (s.strm.avail_out === 0) return BS_NEED_MORE;
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
s.insert = 0;
|
|
1162
|
+
if (flush === Z_FINISH$3) {
|
|
1163
|
+
/*** FLUSH_BLOCK(s, 1); ***/
|
|
1164
|
+
flush_block_only(s, true);
|
|
1165
|
+
if (s.strm.avail_out === 0) return BS_FINISH_STARTED;
|
|
1166
|
+
return BS_FINISH_DONE;
|
|
1167
|
+
}
|
|
1168
|
+
if (s.sym_next) {
|
|
1169
|
+
/*** FLUSH_BLOCK(s, 0); ***/
|
|
1170
|
+
flush_block_only(s, false);
|
|
1171
|
+
if (s.strm.avail_out === 0) return BS_NEED_MORE;
|
|
1172
|
+
}
|
|
1173
|
+
return BS_BLOCK_DONE;
|
|
1174
|
+
};
|
|
1175
|
+
function Config(good_length, max_lazy, nice_length, max_chain, func) {
|
|
1176
|
+
this.good_length = good_length;
|
|
1177
|
+
this.max_lazy = max_lazy;
|
|
1178
|
+
this.nice_length = nice_length;
|
|
1179
|
+
this.max_chain = max_chain;
|
|
1180
|
+
this.func = func;
|
|
1181
|
+
}
|
|
1182
|
+
const configuration_table = [
|
|
1183
|
+
new Config(0, 0, 0, 0, deflate_stored),
|
|
1184
|
+
new Config(4, 4, 8, 4, deflate_fast),
|
|
1185
|
+
new Config(4, 5, 16, 8, deflate_fast),
|
|
1186
|
+
new Config(4, 6, 32, 32, deflate_fast),
|
|
1187
|
+
new Config(4, 4, 16, 16, deflate_slow),
|
|
1188
|
+
new Config(8, 16, 32, 32, deflate_slow),
|
|
1189
|
+
new Config(8, 16, 128, 128, deflate_slow),
|
|
1190
|
+
new Config(8, 32, 128, 256, deflate_slow),
|
|
1191
|
+
new Config(32, 128, 258, 1024, deflate_slow),
|
|
1192
|
+
new Config(32, 258, 258, 4096, deflate_slow)
|
|
1193
|
+
];
|
|
1194
|
+
const lm_init = (s) => {
|
|
1195
|
+
s.window_size = 2 * s.w_size;
|
|
1196
|
+
/*** CLEAR_HASH(s); ***/
|
|
1197
|
+
zero(s.head);
|
|
1198
|
+
s.max_lazy_match = configuration_table[s.level].max_lazy;
|
|
1199
|
+
s.good_match = configuration_table[s.level].good_length;
|
|
1200
|
+
s.nice_match = configuration_table[s.level].nice_length;
|
|
1201
|
+
s.max_chain_length = configuration_table[s.level].max_chain;
|
|
1202
|
+
s.strstart = 0;
|
|
1203
|
+
s.block_start = 0;
|
|
1204
|
+
s.lookahead = 0;
|
|
1205
|
+
s.insert = 0;
|
|
1206
|
+
s.match_length = s.prev_length = MIN_MATCH - 1;
|
|
1207
|
+
s.match_available = 0;
|
|
1208
|
+
s.ins_h = 0;
|
|
1209
|
+
};
|
|
1210
|
+
function DeflateState() {
|
|
1211
|
+
this.strm = null;
|
|
1212
|
+
this.status = 0;
|
|
1213
|
+
this.pending_buf = null;
|
|
1214
|
+
this.pending_buf_size = 0;
|
|
1215
|
+
this.pending_out = 0;
|
|
1216
|
+
this.pending = 0;
|
|
1217
|
+
this.wrap = 0;
|
|
1218
|
+
this.gzhead = null;
|
|
1219
|
+
this.gzindex = 0;
|
|
1220
|
+
this.method = Z_DEFLATED$2;
|
|
1221
|
+
this.last_flush = -1;
|
|
1222
|
+
this.w_size = 0;
|
|
1223
|
+
this.w_bits = 0;
|
|
1224
|
+
this.w_mask = 0;
|
|
1225
|
+
this.window = null;
|
|
1226
|
+
this.window_size = 0;
|
|
1227
|
+
this.prev = null;
|
|
1228
|
+
this.head = null;
|
|
1229
|
+
this.ins_h = 0;
|
|
1230
|
+
this.hash_size = 0;
|
|
1231
|
+
this.hash_bits = 0;
|
|
1232
|
+
this.hash_mask = 0;
|
|
1233
|
+
this.hash_shift = 0;
|
|
1234
|
+
this.block_start = 0;
|
|
1235
|
+
this.match_length = 0;
|
|
1236
|
+
this.prev_match = 0;
|
|
1237
|
+
this.match_available = 0;
|
|
1238
|
+
this.strstart = 0;
|
|
1239
|
+
this.match_start = 0;
|
|
1240
|
+
this.lookahead = 0;
|
|
1241
|
+
this.prev_length = 0;
|
|
1242
|
+
this.max_chain_length = 0;
|
|
1243
|
+
this.max_lazy_match = 0;
|
|
1244
|
+
this.level = 0;
|
|
1245
|
+
this.strategy = 0;
|
|
1246
|
+
this.good_match = 0;
|
|
1247
|
+
this.nice_match = 0;
|
|
1248
|
+
this.dyn_ltree = new Uint16Array(HEAP_SIZE * 2);
|
|
1249
|
+
this.dyn_dtree = new Uint16Array((2 * D_CODES + 1) * 2);
|
|
1250
|
+
this.bl_tree = new Uint16Array((2 * BL_CODES + 1) * 2);
|
|
1251
|
+
zero(this.dyn_ltree);
|
|
1252
|
+
zero(this.dyn_dtree);
|
|
1253
|
+
zero(this.bl_tree);
|
|
1254
|
+
this.l_desc = null;
|
|
1255
|
+
this.d_desc = null;
|
|
1256
|
+
this.bl_desc = null;
|
|
1257
|
+
this.bl_count = new Uint16Array(MAX_BITS + 1);
|
|
1258
|
+
this.heap = new Uint16Array(2 * L_CODES + 1);
|
|
1259
|
+
zero(this.heap);
|
|
1260
|
+
this.heap_len = 0;
|
|
1261
|
+
this.heap_max = 0;
|
|
1262
|
+
this.depth = new Uint16Array(2 * L_CODES + 1);
|
|
1263
|
+
zero(this.depth);
|
|
1264
|
+
this.sym_buf = 0;
|
|
1265
|
+
this.lit_bufsize = 0;
|
|
1266
|
+
this.sym_next = 0;
|
|
1267
|
+
this.sym_end = 0;
|
|
1268
|
+
this.opt_len = 0;
|
|
1269
|
+
this.static_len = 0;
|
|
1270
|
+
this.matches = 0;
|
|
1271
|
+
this.insert = 0;
|
|
1272
|
+
this.bi_buf = 0;
|
|
1273
|
+
this.bi_valid = 0;
|
|
1274
|
+
}
|
|
1275
|
+
const deflateStateCheck = (strm) => {
|
|
1276
|
+
if (!strm) return 1;
|
|
1277
|
+
const s = strm.state;
|
|
1278
|
+
if (!s || s.strm !== strm || s.status !== INIT_STATE && s.status !== GZIP_STATE && s.status !== EXTRA_STATE && s.status !== NAME_STATE && s.status !== COMMENT_STATE && s.status !== HCRC_STATE && s.status !== BUSY_STATE && s.status !== FINISH_STATE) return 1;
|
|
1279
|
+
return 0;
|
|
1280
|
+
};
|
|
1281
|
+
const deflateResetKeep = (strm) => {
|
|
1282
|
+
if (deflateStateCheck(strm)) return err(strm, Z_STREAM_ERROR$2);
|
|
1283
|
+
strm.total_in = strm.total_out = 0;
|
|
1284
|
+
strm.data_type = Z_UNKNOWN;
|
|
1285
|
+
const s = strm.state;
|
|
1286
|
+
s.pending = 0;
|
|
1287
|
+
s.pending_out = 0;
|
|
1288
|
+
if (s.wrap < 0) s.wrap = -s.wrap;
|
|
1289
|
+
s.status = s.wrap === 2 ? GZIP_STATE : s.wrap ? INIT_STATE : BUSY_STATE;
|
|
1290
|
+
strm.adler = s.wrap === 2 ? 0 : 1;
|
|
1291
|
+
s.last_flush = -2;
|
|
1292
|
+
_tr_init(s);
|
|
1293
|
+
return Z_OK$3;
|
|
1294
|
+
};
|
|
1295
|
+
const deflateReset = (strm) => {
|
|
1296
|
+
const ret = deflateResetKeep(strm);
|
|
1297
|
+
if (ret === Z_OK$3) lm_init(strm.state);
|
|
1298
|
+
return ret;
|
|
1299
|
+
};
|
|
1300
|
+
const deflateSetHeader = (strm, head) => {
|
|
1301
|
+
if (deflateStateCheck(strm) || strm.state.wrap !== 2) return Z_STREAM_ERROR$2;
|
|
1302
|
+
strm.state.gzhead = head;
|
|
1303
|
+
return Z_OK$3;
|
|
1304
|
+
};
|
|
1305
|
+
const deflateInit2 = (strm, level, method, windowBits, memLevel, strategy) => {
|
|
1306
|
+
if (!strm) return Z_STREAM_ERROR$2;
|
|
1307
|
+
let wrap = 1;
|
|
1308
|
+
if (level === Z_DEFAULT_COMPRESSION$1) level = 6;
|
|
1309
|
+
if (windowBits < 0) {
|
|
1310
|
+
wrap = 0;
|
|
1311
|
+
windowBits = -windowBits;
|
|
1312
|
+
} else if (windowBits > 15) {
|
|
1313
|
+
wrap = 2;
|
|
1314
|
+
windowBits -= 16;
|
|
1315
|
+
}
|
|
1316
|
+
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED$2 || windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED || windowBits === 8 && wrap !== 1) return err(strm, Z_STREAM_ERROR$2);
|
|
1317
|
+
if (windowBits === 8) windowBits = 9;
|
|
1318
|
+
const s = new DeflateState();
|
|
1319
|
+
strm.state = s;
|
|
1320
|
+
s.strm = strm;
|
|
1321
|
+
s.status = INIT_STATE;
|
|
1322
|
+
s.wrap = wrap;
|
|
1323
|
+
s.gzhead = null;
|
|
1324
|
+
s.w_bits = windowBits;
|
|
1325
|
+
s.w_size = 1 << s.w_bits;
|
|
1326
|
+
s.w_mask = s.w_size - 1;
|
|
1327
|
+
s.hash_bits = memLevel + 7;
|
|
1328
|
+
s.hash_size = 1 << s.hash_bits;
|
|
1329
|
+
s.hash_mask = s.hash_size - 1;
|
|
1330
|
+
s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
|
|
1331
|
+
s.window = new Uint8Array(s.w_size * 2);
|
|
1332
|
+
s.head = new Uint16Array(s.hash_size);
|
|
1333
|
+
s.prev = new Uint16Array(s.w_size);
|
|
1334
|
+
s.lit_bufsize = 1 << memLevel + 6;
|
|
1335
|
+
s.pending_buf_size = s.lit_bufsize * 4;
|
|
1336
|
+
s.pending_buf = new Uint8Array(s.pending_buf_size);
|
|
1337
|
+
s.sym_buf = s.lit_bufsize;
|
|
1338
|
+
s.sym_end = (s.lit_bufsize - 1) * 3;
|
|
1339
|
+
s.level = level;
|
|
1340
|
+
s.strategy = strategy;
|
|
1341
|
+
s.method = method;
|
|
1342
|
+
return deflateReset(strm);
|
|
1343
|
+
};
|
|
1344
|
+
const deflateInit = (strm, level) => {
|
|
1345
|
+
return deflateInit2(strm, level, Z_DEFLATED$2, MAX_WBITS$1, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY$1);
|
|
1346
|
+
};
|
|
1347
|
+
const deflate$2 = (strm, flush) => {
|
|
1348
|
+
if (deflateStateCheck(strm) || flush > Z_BLOCK$1 || flush < 0) return strm ? err(strm, Z_STREAM_ERROR$2) : Z_STREAM_ERROR$2;
|
|
1349
|
+
const s = strm.state;
|
|
1350
|
+
if (!strm.output || strm.avail_in !== 0 && !strm.input || s.status === FINISH_STATE && flush !== Z_FINISH$3) return err(strm, strm.avail_out === 0 ? Z_BUF_ERROR$1 : Z_STREAM_ERROR$2);
|
|
1351
|
+
const old_flush = s.last_flush;
|
|
1352
|
+
s.last_flush = flush;
|
|
1353
|
+
if (s.pending !== 0) {
|
|
1354
|
+
flush_pending(strm);
|
|
1355
|
+
if (strm.avail_out === 0) {
|
|
1356
|
+
s.last_flush = -1;
|
|
1357
|
+
return Z_OK$3;
|
|
1358
|
+
}
|
|
1359
|
+
} else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) && flush !== Z_FINISH$3) return err(strm, Z_BUF_ERROR$1);
|
|
1360
|
+
if (s.status === FINISH_STATE && strm.avail_in !== 0) return err(strm, Z_BUF_ERROR$1);
|
|
1361
|
+
if (s.status === INIT_STATE && s.wrap === 0) s.status = BUSY_STATE;
|
|
1362
|
+
if (s.status === INIT_STATE) {
|
|
1363
|
+
let header = Z_DEFLATED$2 + (s.w_bits - 8 << 4) << 8;
|
|
1364
|
+
let level_flags = -1;
|
|
1365
|
+
if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) level_flags = 0;
|
|
1366
|
+
else if (s.level < 6) level_flags = 1;
|
|
1367
|
+
else if (s.level === 6) level_flags = 2;
|
|
1368
|
+
else level_flags = 3;
|
|
1369
|
+
header |= level_flags << 6;
|
|
1370
|
+
if (s.strstart !== 0) header |= PRESET_DICT;
|
|
1371
|
+
header += 31 - header % 31;
|
|
1372
|
+
putShortMSB(s, header);
|
|
1373
|
+
if (s.strstart !== 0) {
|
|
1374
|
+
putShortMSB(s, strm.adler >>> 16);
|
|
1375
|
+
putShortMSB(s, strm.adler & 65535);
|
|
1376
|
+
}
|
|
1377
|
+
strm.adler = 1;
|
|
1378
|
+
s.status = BUSY_STATE;
|
|
1379
|
+
flush_pending(strm);
|
|
1380
|
+
if (s.pending !== 0) {
|
|
1381
|
+
s.last_flush = -1;
|
|
1382
|
+
return Z_OK$3;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
if (s.status === GZIP_STATE) {
|
|
1386
|
+
strm.adler = 0;
|
|
1387
|
+
put_byte(s, 31);
|
|
1388
|
+
put_byte(s, 139);
|
|
1389
|
+
put_byte(s, 8);
|
|
1390
|
+
if (!s.gzhead) {
|
|
1391
|
+
put_byte(s, 0);
|
|
1392
|
+
put_byte(s, 0);
|
|
1393
|
+
put_byte(s, 0);
|
|
1394
|
+
put_byte(s, 0);
|
|
1395
|
+
put_byte(s, 0);
|
|
1396
|
+
put_byte(s, s.level === 9 ? 2 : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0);
|
|
1397
|
+
put_byte(s, OS_CODE);
|
|
1398
|
+
s.status = BUSY_STATE;
|
|
1399
|
+
flush_pending(strm);
|
|
1400
|
+
if (s.pending !== 0) {
|
|
1401
|
+
s.last_flush = -1;
|
|
1402
|
+
return Z_OK$3;
|
|
1403
|
+
}
|
|
1404
|
+
} else {
|
|
1405
|
+
put_byte(s, (s.gzhead.text ? 1 : 0) + (s.gzhead.hcrc ? 2 : 0) + (!s.gzhead.extra ? 0 : 4) + (!s.gzhead.name ? 0 : 8) + (!s.gzhead.comment ? 0 : 16));
|
|
1406
|
+
put_byte(s, s.gzhead.time & 255);
|
|
1407
|
+
put_byte(s, s.gzhead.time >> 8 & 255);
|
|
1408
|
+
put_byte(s, s.gzhead.time >> 16 & 255);
|
|
1409
|
+
put_byte(s, s.gzhead.time >> 24 & 255);
|
|
1410
|
+
put_byte(s, s.level === 9 ? 2 : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0);
|
|
1411
|
+
put_byte(s, s.gzhead.os & 255);
|
|
1412
|
+
if (s.gzhead.extra && s.gzhead.extra.length) {
|
|
1413
|
+
put_byte(s, s.gzhead.extra.length & 255);
|
|
1414
|
+
put_byte(s, s.gzhead.extra.length >> 8 & 255);
|
|
1415
|
+
}
|
|
1416
|
+
if (s.gzhead.hcrc) strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending, 0);
|
|
1417
|
+
s.gzindex = 0;
|
|
1418
|
+
s.status = EXTRA_STATE;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
if (s.status === EXTRA_STATE) {
|
|
1422
|
+
if (s.gzhead.extra) {
|
|
1423
|
+
let beg = s.pending;
|
|
1424
|
+
let left = (s.gzhead.extra.length & 65535) - s.gzindex;
|
|
1425
|
+
while (s.pending + left > s.pending_buf_size) {
|
|
1426
|
+
let copy = s.pending_buf_size - s.pending;
|
|
1427
|
+
s.pending_buf.set(s.gzhead.extra.subarray(s.gzindex, s.gzindex + copy), s.pending);
|
|
1428
|
+
s.pending = s.pending_buf_size;
|
|
1429
|
+
if (s.gzhead.hcrc && s.pending > beg) strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
|
|
1430
|
+
s.gzindex += copy;
|
|
1431
|
+
flush_pending(strm);
|
|
1432
|
+
if (s.pending !== 0) {
|
|
1433
|
+
s.last_flush = -1;
|
|
1434
|
+
return Z_OK$3;
|
|
1435
|
+
}
|
|
1436
|
+
beg = 0;
|
|
1437
|
+
left -= copy;
|
|
1438
|
+
}
|
|
1439
|
+
let gzhead_extra = new Uint8Array(s.gzhead.extra);
|
|
1440
|
+
s.pending_buf.set(gzhead_extra.subarray(s.gzindex, s.gzindex + left), s.pending);
|
|
1441
|
+
s.pending += left;
|
|
1442
|
+
if (s.gzhead.hcrc && s.pending > beg) strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
|
|
1443
|
+
s.gzindex = 0;
|
|
1444
|
+
}
|
|
1445
|
+
s.status = NAME_STATE;
|
|
1446
|
+
}
|
|
1447
|
+
if (s.status === NAME_STATE) {
|
|
1448
|
+
if (s.gzhead.name) {
|
|
1449
|
+
let beg = s.pending;
|
|
1450
|
+
let val;
|
|
1451
|
+
do {
|
|
1452
|
+
if (s.pending === s.pending_buf_size) {
|
|
1453
|
+
if (s.gzhead.hcrc && s.pending > beg) strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
|
|
1454
|
+
flush_pending(strm);
|
|
1455
|
+
if (s.pending !== 0) {
|
|
1456
|
+
s.last_flush = -1;
|
|
1457
|
+
return Z_OK$3;
|
|
1458
|
+
}
|
|
1459
|
+
beg = 0;
|
|
1460
|
+
}
|
|
1461
|
+
if (s.gzindex < s.gzhead.name.length) val = s.gzhead.name.charCodeAt(s.gzindex++) & 255;
|
|
1462
|
+
else val = 0;
|
|
1463
|
+
put_byte(s, val);
|
|
1464
|
+
} while (val !== 0);
|
|
1465
|
+
if (s.gzhead.hcrc && s.pending > beg) strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
|
|
1466
|
+
s.gzindex = 0;
|
|
1467
|
+
}
|
|
1468
|
+
s.status = COMMENT_STATE;
|
|
1469
|
+
}
|
|
1470
|
+
if (s.status === COMMENT_STATE) {
|
|
1471
|
+
if (s.gzhead.comment) {
|
|
1472
|
+
let beg = s.pending;
|
|
1473
|
+
let val;
|
|
1474
|
+
do {
|
|
1475
|
+
if (s.pending === s.pending_buf_size) {
|
|
1476
|
+
if (s.gzhead.hcrc && s.pending > beg) strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
|
|
1477
|
+
flush_pending(strm);
|
|
1478
|
+
if (s.pending !== 0) {
|
|
1479
|
+
s.last_flush = -1;
|
|
1480
|
+
return Z_OK$3;
|
|
1481
|
+
}
|
|
1482
|
+
beg = 0;
|
|
1483
|
+
}
|
|
1484
|
+
if (s.gzindex < s.gzhead.comment.length) val = s.gzhead.comment.charCodeAt(s.gzindex++) & 255;
|
|
1485
|
+
else val = 0;
|
|
1486
|
+
put_byte(s, val);
|
|
1487
|
+
} while (val !== 0);
|
|
1488
|
+
if (s.gzhead.hcrc && s.pending > beg) strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
|
|
1489
|
+
}
|
|
1490
|
+
s.status = HCRC_STATE;
|
|
1491
|
+
}
|
|
1492
|
+
if (s.status === HCRC_STATE) {
|
|
1493
|
+
if (s.gzhead.hcrc) {
|
|
1494
|
+
if (s.pending + 2 > s.pending_buf_size) {
|
|
1495
|
+
flush_pending(strm);
|
|
1496
|
+
if (s.pending !== 0) {
|
|
1497
|
+
s.last_flush = -1;
|
|
1498
|
+
return Z_OK$3;
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
put_byte(s, strm.adler & 255);
|
|
1502
|
+
put_byte(s, strm.adler >> 8 & 255);
|
|
1503
|
+
strm.adler = 0;
|
|
1504
|
+
}
|
|
1505
|
+
s.status = BUSY_STATE;
|
|
1506
|
+
flush_pending(strm);
|
|
1507
|
+
if (s.pending !== 0) {
|
|
1508
|
+
s.last_flush = -1;
|
|
1509
|
+
return Z_OK$3;
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
if (strm.avail_in !== 0 || s.lookahead !== 0 || flush !== Z_NO_FLUSH$2 && s.status !== FINISH_STATE) {
|
|
1513
|
+
let bstate = s.level === 0 ? deflate_stored(s, flush) : s.strategy === Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : s.strategy === Z_RLE ? deflate_rle(s, flush) : configuration_table[s.level].func(s, flush);
|
|
1514
|
+
if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) s.status = FINISH_STATE;
|
|
1515
|
+
if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
|
|
1516
|
+
if (strm.avail_out === 0) s.last_flush = -1;
|
|
1517
|
+
return Z_OK$3;
|
|
1518
|
+
}
|
|
1519
|
+
if (bstate === BS_BLOCK_DONE) {
|
|
1520
|
+
if (flush === Z_PARTIAL_FLUSH) _tr_align(s);
|
|
1521
|
+
else if (flush !== Z_BLOCK$1) {
|
|
1522
|
+
_tr_stored_block(s, 0, 0, false);
|
|
1523
|
+
if (flush === Z_FULL_FLUSH$1) {
|
|
1524
|
+
/*** CLEAR_HASH(s); ***/ zero(s.head);
|
|
1525
|
+
if (s.lookahead === 0) {
|
|
1526
|
+
s.strstart = 0;
|
|
1527
|
+
s.block_start = 0;
|
|
1528
|
+
s.insert = 0;
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
flush_pending(strm);
|
|
1533
|
+
if (strm.avail_out === 0) {
|
|
1534
|
+
s.last_flush = -1;
|
|
1535
|
+
return Z_OK$3;
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
if (flush !== Z_FINISH$3) return Z_OK$3;
|
|
1540
|
+
if (s.wrap <= 0) return Z_STREAM_END$3;
|
|
1541
|
+
if (s.wrap === 2) {
|
|
1542
|
+
put_byte(s, strm.adler & 255);
|
|
1543
|
+
put_byte(s, strm.adler >> 8 & 255);
|
|
1544
|
+
put_byte(s, strm.adler >> 16 & 255);
|
|
1545
|
+
put_byte(s, strm.adler >> 24 & 255);
|
|
1546
|
+
put_byte(s, strm.total_in & 255);
|
|
1547
|
+
put_byte(s, strm.total_in >> 8 & 255);
|
|
1548
|
+
put_byte(s, strm.total_in >> 16 & 255);
|
|
1549
|
+
put_byte(s, strm.total_in >> 24 & 255);
|
|
1550
|
+
} else {
|
|
1551
|
+
putShortMSB(s, strm.adler >>> 16);
|
|
1552
|
+
putShortMSB(s, strm.adler & 65535);
|
|
1553
|
+
}
|
|
1554
|
+
flush_pending(strm);
|
|
1555
|
+
if (s.wrap > 0) s.wrap = -s.wrap;
|
|
1556
|
+
return s.pending !== 0 ? Z_OK$3 : Z_STREAM_END$3;
|
|
1557
|
+
};
|
|
1558
|
+
const deflateEnd = (strm) => {
|
|
1559
|
+
if (deflateStateCheck(strm)) return Z_STREAM_ERROR$2;
|
|
1560
|
+
const status = strm.state.status;
|
|
1561
|
+
strm.state = null;
|
|
1562
|
+
return status === BUSY_STATE ? err(strm, Z_DATA_ERROR$2) : Z_OK$3;
|
|
1563
|
+
};
|
|
1564
|
+
const deflateSetDictionary = (strm, dictionary) => {
|
|
1565
|
+
let dictLength = dictionary.length;
|
|
1566
|
+
if (deflateStateCheck(strm)) return Z_STREAM_ERROR$2;
|
|
1567
|
+
const s = strm.state;
|
|
1568
|
+
const wrap = s.wrap;
|
|
1569
|
+
if (wrap === 2 || wrap === 1 && s.status !== INIT_STATE || s.lookahead) return Z_STREAM_ERROR$2;
|
|
1570
|
+
if (wrap === 1) strm.adler = adler32_1(strm.adler, dictionary, dictLength, 0);
|
|
1571
|
+
s.wrap = 0;
|
|
1572
|
+
if (dictLength >= s.w_size) {
|
|
1573
|
+
if (wrap === 0) {
|
|
1574
|
+
/*** CLEAR_HASH(s); ***/
|
|
1575
|
+
zero(s.head);
|
|
1576
|
+
s.strstart = 0;
|
|
1577
|
+
s.block_start = 0;
|
|
1578
|
+
s.insert = 0;
|
|
1579
|
+
}
|
|
1580
|
+
let tmpDict = new Uint8Array(s.w_size);
|
|
1581
|
+
tmpDict.set(dictionary.subarray(dictLength - s.w_size, dictLength), 0);
|
|
1582
|
+
dictionary = tmpDict;
|
|
1583
|
+
dictLength = s.w_size;
|
|
1584
|
+
}
|
|
1585
|
+
const avail = strm.avail_in;
|
|
1586
|
+
const next = strm.next_in;
|
|
1587
|
+
const input = strm.input;
|
|
1588
|
+
strm.avail_in = dictLength;
|
|
1589
|
+
strm.next_in = 0;
|
|
1590
|
+
strm.input = dictionary;
|
|
1591
|
+
fill_window(s);
|
|
1592
|
+
while (s.lookahead >= MIN_MATCH) {
|
|
1593
|
+
let str = s.strstart;
|
|
1594
|
+
let n = s.lookahead - (MIN_MATCH - 1);
|
|
1595
|
+
do {
|
|
1596
|
+
s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
|
|
1597
|
+
s.prev[str & s.w_mask] = s.head[s.ins_h];
|
|
1598
|
+
s.head[s.ins_h] = str;
|
|
1599
|
+
str++;
|
|
1600
|
+
} while (--n);
|
|
1601
|
+
s.strstart = str;
|
|
1602
|
+
s.lookahead = MIN_MATCH - 1;
|
|
1603
|
+
fill_window(s);
|
|
1604
|
+
}
|
|
1605
|
+
s.strstart += s.lookahead;
|
|
1606
|
+
s.block_start = s.strstart;
|
|
1607
|
+
s.insert = s.lookahead;
|
|
1608
|
+
s.lookahead = 0;
|
|
1609
|
+
s.match_length = s.prev_length = MIN_MATCH - 1;
|
|
1610
|
+
s.match_available = 0;
|
|
1611
|
+
strm.next_in = next;
|
|
1612
|
+
strm.input = input;
|
|
1613
|
+
strm.avail_in = avail;
|
|
1614
|
+
s.wrap = wrap;
|
|
1615
|
+
return Z_OK$3;
|
|
1616
|
+
};
|
|
1617
|
+
var deflate_1$2 = {
|
|
1618
|
+
deflateInit,
|
|
1619
|
+
deflateInit2,
|
|
1620
|
+
deflateReset,
|
|
1621
|
+
deflateResetKeep,
|
|
1622
|
+
deflateSetHeader,
|
|
1623
|
+
deflate: deflate$2,
|
|
1624
|
+
deflateEnd,
|
|
1625
|
+
deflateSetDictionary,
|
|
1626
|
+
deflateInfo: "pako deflate (from Nodeca project)"
|
|
1627
|
+
};
|
|
1628
|
+
const _has = (obj, key) => {
|
|
1629
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
1630
|
+
};
|
|
1631
|
+
var assign = function(obj) {
|
|
1632
|
+
const sources = Array.prototype.slice.call(arguments, 1);
|
|
1633
|
+
while (sources.length) {
|
|
1634
|
+
const source = sources.shift();
|
|
1635
|
+
if (!source) continue;
|
|
1636
|
+
if (typeof source !== "object") throw new TypeError(source + "must be non-object");
|
|
1637
|
+
for (const p in source) if (_has(source, p)) obj[p] = source[p];
|
|
1638
|
+
}
|
|
1639
|
+
return obj;
|
|
1640
|
+
};
|
|
1641
|
+
var flattenChunks = (chunks) => {
|
|
1642
|
+
let len = 0;
|
|
1643
|
+
for (let i = 0, l = chunks.length; i < l; i++) len += chunks[i].length;
|
|
1644
|
+
const result = new Uint8Array(len);
|
|
1645
|
+
for (let i = 0, pos = 0, l = chunks.length; i < l; i++) {
|
|
1646
|
+
let chunk = chunks[i];
|
|
1647
|
+
result.set(chunk, pos);
|
|
1648
|
+
pos += chunk.length;
|
|
1649
|
+
}
|
|
1650
|
+
return result;
|
|
1651
|
+
};
|
|
1652
|
+
var common = {
|
|
1653
|
+
assign,
|
|
1654
|
+
flattenChunks
|
|
1655
|
+
};
|
|
1656
|
+
let STR_APPLY_UIA_OK = true;
|
|
1657
|
+
try {
|
|
1658
|
+
String.fromCharCode.apply(null, new Uint8Array(1));
|
|
1659
|
+
} catch (__) {
|
|
1660
|
+
STR_APPLY_UIA_OK = false;
|
|
1661
|
+
}
|
|
1662
|
+
const _utf8len = new Uint8Array(256);
|
|
1663
|
+
for (let q = 0; q < 256; q++) _utf8len[q] = q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1;
|
|
1664
|
+
_utf8len[254] = _utf8len[254] = 1;
|
|
1665
|
+
var string2buf = (str) => {
|
|
1666
|
+
if (typeof TextEncoder === "function" && TextEncoder.prototype.encode) return new TextEncoder().encode(str);
|
|
1667
|
+
let buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
|
|
1668
|
+
for (m_pos = 0; m_pos < str_len; m_pos++) {
|
|
1669
|
+
c = str.charCodeAt(m_pos);
|
|
1670
|
+
if ((c & 64512) === 55296 && m_pos + 1 < str_len) {
|
|
1671
|
+
c2 = str.charCodeAt(m_pos + 1);
|
|
1672
|
+
if ((c2 & 64512) === 56320) {
|
|
1673
|
+
c = 65536 + (c - 55296 << 10) + (c2 - 56320);
|
|
1674
|
+
m_pos++;
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
buf_len += c < 128 ? 1 : c < 2048 ? 2 : c < 65536 ? 3 : 4;
|
|
1678
|
+
}
|
|
1679
|
+
buf = new Uint8Array(buf_len);
|
|
1680
|
+
for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
|
|
1681
|
+
c = str.charCodeAt(m_pos);
|
|
1682
|
+
if ((c & 64512) === 55296 && m_pos + 1 < str_len) {
|
|
1683
|
+
c2 = str.charCodeAt(m_pos + 1);
|
|
1684
|
+
if ((c2 & 64512) === 56320) {
|
|
1685
|
+
c = 65536 + (c - 55296 << 10) + (c2 - 56320);
|
|
1686
|
+
m_pos++;
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
if (c < 128) buf[i++] = c;
|
|
1690
|
+
else if (c < 2048) {
|
|
1691
|
+
buf[i++] = 192 | c >>> 6;
|
|
1692
|
+
buf[i++] = 128 | c & 63;
|
|
1693
|
+
} else if (c < 65536) {
|
|
1694
|
+
buf[i++] = 224 | c >>> 12;
|
|
1695
|
+
buf[i++] = 128 | c >>> 6 & 63;
|
|
1696
|
+
buf[i++] = 128 | c & 63;
|
|
1697
|
+
} else {
|
|
1698
|
+
buf[i++] = 240 | c >>> 18;
|
|
1699
|
+
buf[i++] = 128 | c >>> 12 & 63;
|
|
1700
|
+
buf[i++] = 128 | c >>> 6 & 63;
|
|
1701
|
+
buf[i++] = 128 | c & 63;
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
return buf;
|
|
1705
|
+
};
|
|
1706
|
+
const buf2binstring = (buf, len) => {
|
|
1707
|
+
if (len < 65534) {
|
|
1708
|
+
if (buf.subarray && STR_APPLY_UIA_OK) return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len));
|
|
1709
|
+
}
|
|
1710
|
+
let result = "";
|
|
1711
|
+
for (let i = 0; i < len; i++) result += String.fromCharCode(buf[i]);
|
|
1712
|
+
return result;
|
|
1713
|
+
};
|
|
1714
|
+
var buf2string = (buf, max) => {
|
|
1715
|
+
const len = max || buf.length;
|
|
1716
|
+
if (typeof TextDecoder === "function" && TextDecoder.prototype.decode) return new TextDecoder().decode(buf.subarray(0, max));
|
|
1717
|
+
let i, out;
|
|
1718
|
+
const utf16buf = new Array(len * 2);
|
|
1719
|
+
for (out = 0, i = 0; i < len;) {
|
|
1720
|
+
let c = buf[i++];
|
|
1721
|
+
if (c < 128) {
|
|
1722
|
+
utf16buf[out++] = c;
|
|
1723
|
+
continue;
|
|
1724
|
+
}
|
|
1725
|
+
let c_len = _utf8len[c];
|
|
1726
|
+
if (c_len > 4) {
|
|
1727
|
+
utf16buf[out++] = 65533;
|
|
1728
|
+
i += c_len - 1;
|
|
1729
|
+
continue;
|
|
1730
|
+
}
|
|
1731
|
+
c &= c_len === 2 ? 31 : c_len === 3 ? 15 : 7;
|
|
1732
|
+
while (c_len > 1 && i < len) {
|
|
1733
|
+
c = c << 6 | buf[i++] & 63;
|
|
1734
|
+
c_len--;
|
|
1735
|
+
}
|
|
1736
|
+
if (c_len > 1) {
|
|
1737
|
+
utf16buf[out++] = 65533;
|
|
1738
|
+
continue;
|
|
1739
|
+
}
|
|
1740
|
+
if (c < 65536) utf16buf[out++] = c;
|
|
1741
|
+
else {
|
|
1742
|
+
c -= 65536;
|
|
1743
|
+
utf16buf[out++] = 55296 | c >> 10 & 1023;
|
|
1744
|
+
utf16buf[out++] = 56320 | c & 1023;
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
return buf2binstring(utf16buf, out);
|
|
1748
|
+
};
|
|
1749
|
+
var utf8border = (buf, max) => {
|
|
1750
|
+
max = max || buf.length;
|
|
1751
|
+
if (max > buf.length) max = buf.length;
|
|
1752
|
+
let pos = max - 1;
|
|
1753
|
+
while (pos >= 0 && (buf[pos] & 192) === 128) pos--;
|
|
1754
|
+
if (pos < 0) return max;
|
|
1755
|
+
if (pos === 0) return max;
|
|
1756
|
+
return pos + _utf8len[buf[pos]] > max ? pos : max;
|
|
1757
|
+
};
|
|
1758
|
+
var strings = {
|
|
1759
|
+
string2buf,
|
|
1760
|
+
buf2string,
|
|
1761
|
+
utf8border
|
|
1762
|
+
};
|
|
1763
|
+
function ZStream() {
|
|
1764
|
+
this.input = null;
|
|
1765
|
+
this.next_in = 0;
|
|
1766
|
+
this.avail_in = 0;
|
|
1767
|
+
this.total_in = 0;
|
|
1768
|
+
this.output = null;
|
|
1769
|
+
this.next_out = 0;
|
|
1770
|
+
this.avail_out = 0;
|
|
1771
|
+
this.total_out = 0;
|
|
1772
|
+
this.msg = "";
|
|
1773
|
+
this.state = null;
|
|
1774
|
+
this.data_type = 2;
|
|
1775
|
+
this.adler = 0;
|
|
1776
|
+
}
|
|
1777
|
+
var zstream = ZStream;
|
|
1778
|
+
const toString$1 = Object.prototype.toString;
|
|
1779
|
+
const { Z_NO_FLUSH: Z_NO_FLUSH$1, Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH: Z_FINISH$2, Z_OK: Z_OK$2, Z_STREAM_END: Z_STREAM_END$2, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY, Z_DEFLATED: Z_DEFLATED$1 } = constants$2;
|
|
1780
|
+
/**
|
|
1781
|
+
* class Deflate
|
|
1782
|
+
*
|
|
1783
|
+
* Generic JS-style wrapper for zlib calls. If you don't need
|
|
1784
|
+
* streaming behaviour - use more simple functions: [[deflate]],
|
|
1785
|
+
* [[deflateRaw]] and [[gzip]].
|
|
1786
|
+
**/
|
|
1787
|
+
/**
|
|
1788
|
+
* Deflate.result -> Uint8Array
|
|
1789
|
+
*
|
|
1790
|
+
* Compressed result, generated by default [[Deflate#onData]]
|
|
1791
|
+
* and [[Deflate#onEnd]] handlers. Filled after you push last chunk
|
|
1792
|
+
* (call [[Deflate#push]] with `Z_FINISH` / `true` param).
|
|
1793
|
+
**/
|
|
1794
|
+
/**
|
|
1795
|
+
* Deflate.err -> Number
|
|
1796
|
+
*
|
|
1797
|
+
* Error code after deflate finished. 0 (Z_OK) on success.
|
|
1798
|
+
* You will not need it in real life, because deflate errors
|
|
1799
|
+
* are possible only on wrong options or bad `onData` / `onEnd`
|
|
1800
|
+
* custom handlers.
|
|
1801
|
+
**/
|
|
1802
|
+
/**
|
|
1803
|
+
* Deflate.msg -> String
|
|
1804
|
+
*
|
|
1805
|
+
* Error message, if [[Deflate.err]] != 0
|
|
1806
|
+
**/
|
|
1807
|
+
/**
|
|
1808
|
+
* new Deflate(options)
|
|
1809
|
+
* - options (Object): zlib deflate options.
|
|
1810
|
+
*
|
|
1811
|
+
* Creates new deflator instance with specified params. Throws exception
|
|
1812
|
+
* on bad params. Supported options:
|
|
1813
|
+
*
|
|
1814
|
+
* - `level`
|
|
1815
|
+
* - `windowBits`
|
|
1816
|
+
* - `memLevel`
|
|
1817
|
+
* - `strategy`
|
|
1818
|
+
* - `dictionary`
|
|
1819
|
+
*
|
|
1820
|
+
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
|
|
1821
|
+
* for more information on these.
|
|
1822
|
+
*
|
|
1823
|
+
* Additional options, for internal needs:
|
|
1824
|
+
*
|
|
1825
|
+
* - `chunkSize` - size of generated data chunks (16K by default)
|
|
1826
|
+
* - `raw` (Boolean) - do raw deflate
|
|
1827
|
+
* - `gzip` (Boolean) - create gzip wrapper
|
|
1828
|
+
* - `header` (Object) - custom header for gzip
|
|
1829
|
+
* - `text` (Boolean) - true if compressed data believed to be text
|
|
1830
|
+
* - `time` (Number) - modification time, unix timestamp
|
|
1831
|
+
* - `os` (Number) - operation system code
|
|
1832
|
+
* - `extra` (Array) - array of bytes with extra data (max 65536)
|
|
1833
|
+
* - `name` (String) - file name (binary string)
|
|
1834
|
+
* - `comment` (String) - comment (binary string)
|
|
1835
|
+
* - `hcrc` (Boolean) - true if header crc should be added
|
|
1836
|
+
*
|
|
1837
|
+
* ##### Example:
|
|
1838
|
+
*
|
|
1839
|
+
* ```javascript
|
|
1840
|
+
* const pako = require('pako')
|
|
1841
|
+
* , chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
|
|
1842
|
+
* , chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
|
|
1843
|
+
*
|
|
1844
|
+
* const deflate = new pako.Deflate({ level: 3});
|
|
1845
|
+
*
|
|
1846
|
+
* deflate.push(chunk1, false);
|
|
1847
|
+
* deflate.push(chunk2, true); // true -> last chunk
|
|
1848
|
+
*
|
|
1849
|
+
* if (deflate.err) { throw new Error(deflate.err); }
|
|
1850
|
+
*
|
|
1851
|
+
* console.log(deflate.result);
|
|
1852
|
+
* ```
|
|
1853
|
+
**/
|
|
1854
|
+
function Deflate$1(options) {
|
|
1855
|
+
this.options = common.assign({
|
|
1856
|
+
level: Z_DEFAULT_COMPRESSION,
|
|
1857
|
+
method: Z_DEFLATED$1,
|
|
1858
|
+
chunkSize: 16384,
|
|
1859
|
+
windowBits: 15,
|
|
1860
|
+
memLevel: 8,
|
|
1861
|
+
strategy: Z_DEFAULT_STRATEGY
|
|
1862
|
+
}, options || {});
|
|
1863
|
+
let opt = this.options;
|
|
1864
|
+
if (opt.raw && opt.windowBits > 0) opt.windowBits = -opt.windowBits;
|
|
1865
|
+
else if (opt.gzip && opt.windowBits > 0 && opt.windowBits < 16) opt.windowBits += 16;
|
|
1866
|
+
this.err = 0;
|
|
1867
|
+
this.msg = "";
|
|
1868
|
+
this.ended = false;
|
|
1869
|
+
this.chunks = [];
|
|
1870
|
+
this.strm = new zstream();
|
|
1871
|
+
this.strm.avail_out = 0;
|
|
1872
|
+
let status = deflate_1$2.deflateInit2(this.strm, opt.level, opt.method, opt.windowBits, opt.memLevel, opt.strategy);
|
|
1873
|
+
if (status !== Z_OK$2) throw new Error(messages[status]);
|
|
1874
|
+
if (opt.header) deflate_1$2.deflateSetHeader(this.strm, opt.header);
|
|
1875
|
+
if (opt.dictionary) {
|
|
1876
|
+
let dict;
|
|
1877
|
+
if (typeof opt.dictionary === "string") dict = strings.string2buf(opt.dictionary);
|
|
1878
|
+
else if (toString$1.call(opt.dictionary) === "[object ArrayBuffer]") dict = new Uint8Array(opt.dictionary);
|
|
1879
|
+
else dict = opt.dictionary;
|
|
1880
|
+
status = deflate_1$2.deflateSetDictionary(this.strm, dict);
|
|
1881
|
+
if (status !== Z_OK$2) throw new Error(messages[status]);
|
|
1882
|
+
this._dict_set = true;
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
/**
|
|
1886
|
+
* Deflate#push(data[, flush_mode]) -> Boolean
|
|
1887
|
+
* - data (Uint8Array|ArrayBuffer|String): input data. Strings will be
|
|
1888
|
+
* converted to utf8 byte sequence.
|
|
1889
|
+
* - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
|
|
1890
|
+
* See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
|
|
1891
|
+
*
|
|
1892
|
+
* Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
|
|
1893
|
+
* new compressed chunks. Returns `true` on success. The last data block must
|
|
1894
|
+
* have `flush_mode` Z_FINISH (or `true`). That will flush internal pending
|
|
1895
|
+
* buffers and call [[Deflate#onEnd]].
|
|
1896
|
+
*
|
|
1897
|
+
* On fail call [[Deflate#onEnd]] with error code and return false.
|
|
1898
|
+
*
|
|
1899
|
+
* ##### Example
|
|
1900
|
+
*
|
|
1901
|
+
* ```javascript
|
|
1902
|
+
* push(chunk, false); // push one of data chunks
|
|
1903
|
+
* ...
|
|
1904
|
+
* push(chunk, true); // push last chunk
|
|
1905
|
+
* ```
|
|
1906
|
+
**/
|
|
1907
|
+
Deflate$1.prototype.push = function(data, flush_mode) {
|
|
1908
|
+
const strm = this.strm;
|
|
1909
|
+
const chunkSize = this.options.chunkSize;
|
|
1910
|
+
let status, _flush_mode;
|
|
1911
|
+
if (this.ended) return false;
|
|
1912
|
+
if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;
|
|
1913
|
+
else _flush_mode = flush_mode === true ? Z_FINISH$2 : Z_NO_FLUSH$1;
|
|
1914
|
+
if (typeof data === "string") strm.input = strings.string2buf(data);
|
|
1915
|
+
else if (toString$1.call(data) === "[object ArrayBuffer]") strm.input = new Uint8Array(data);
|
|
1916
|
+
else strm.input = data;
|
|
1917
|
+
strm.next_in = 0;
|
|
1918
|
+
strm.avail_in = strm.input.length;
|
|
1919
|
+
for (;;) {
|
|
1920
|
+
if (strm.avail_out === 0) {
|
|
1921
|
+
strm.output = new Uint8Array(chunkSize);
|
|
1922
|
+
strm.next_out = 0;
|
|
1923
|
+
strm.avail_out = chunkSize;
|
|
1924
|
+
}
|
|
1925
|
+
if ((_flush_mode === Z_SYNC_FLUSH || _flush_mode === Z_FULL_FLUSH) && strm.avail_out <= 6) {
|
|
1926
|
+
this.onData(strm.output.subarray(0, strm.next_out));
|
|
1927
|
+
strm.avail_out = 0;
|
|
1928
|
+
continue;
|
|
1929
|
+
}
|
|
1930
|
+
status = deflate_1$2.deflate(strm, _flush_mode);
|
|
1931
|
+
if (status === Z_STREAM_END$2) {
|
|
1932
|
+
if (strm.next_out > 0) this.onData(strm.output.subarray(0, strm.next_out));
|
|
1933
|
+
status = deflate_1$2.deflateEnd(this.strm);
|
|
1934
|
+
this.onEnd(status);
|
|
1935
|
+
this.ended = true;
|
|
1936
|
+
return status === Z_OK$2;
|
|
1937
|
+
}
|
|
1938
|
+
if (strm.avail_out === 0) {
|
|
1939
|
+
this.onData(strm.output);
|
|
1940
|
+
continue;
|
|
1941
|
+
}
|
|
1942
|
+
if (_flush_mode > 0 && strm.next_out > 0) {
|
|
1943
|
+
this.onData(strm.output.subarray(0, strm.next_out));
|
|
1944
|
+
strm.avail_out = 0;
|
|
1945
|
+
continue;
|
|
1946
|
+
}
|
|
1947
|
+
if (strm.avail_in === 0) break;
|
|
1948
|
+
}
|
|
1949
|
+
return true;
|
|
1950
|
+
};
|
|
1951
|
+
/**
|
|
1952
|
+
* Deflate#onData(chunk) -> Void
|
|
1953
|
+
* - chunk (Uint8Array): output data.
|
|
1954
|
+
*
|
|
1955
|
+
* By default, stores data blocks in `chunks[]` property and glue
|
|
1956
|
+
* those in `onEnd`. Override this handler, if you need another behaviour.
|
|
1957
|
+
**/
|
|
1958
|
+
Deflate$1.prototype.onData = function(chunk) {
|
|
1959
|
+
this.chunks.push(chunk);
|
|
1960
|
+
};
|
|
1961
|
+
/**
|
|
1962
|
+
* Deflate#onEnd(status) -> Void
|
|
1963
|
+
* - status (Number): deflate status. 0 (Z_OK) on success,
|
|
1964
|
+
* other if not.
|
|
1965
|
+
*
|
|
1966
|
+
* Called once after you tell deflate that the input stream is
|
|
1967
|
+
* complete (Z_FINISH). By default - join collected chunks,
|
|
1968
|
+
* free memory and fill `results` / `err` properties.
|
|
1969
|
+
**/
|
|
1970
|
+
Deflate$1.prototype.onEnd = function(status) {
|
|
1971
|
+
if (status === Z_OK$2) this.result = common.flattenChunks(this.chunks);
|
|
1972
|
+
this.chunks = [];
|
|
1973
|
+
this.err = status;
|
|
1974
|
+
this.msg = this.strm.msg;
|
|
1975
|
+
};
|
|
1976
|
+
/**
|
|
1977
|
+
* deflate(data[, options]) -> Uint8Array
|
|
1978
|
+
* - data (Uint8Array|ArrayBuffer|String): input data to compress.
|
|
1979
|
+
* - options (Object): zlib deflate options.
|
|
1980
|
+
*
|
|
1981
|
+
* Compress `data` with deflate algorithm and `options`.
|
|
1982
|
+
*
|
|
1983
|
+
* Supported options are:
|
|
1984
|
+
*
|
|
1985
|
+
* - level
|
|
1986
|
+
* - windowBits
|
|
1987
|
+
* - memLevel
|
|
1988
|
+
* - strategy
|
|
1989
|
+
* - dictionary
|
|
1990
|
+
*
|
|
1991
|
+
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
|
|
1992
|
+
* for more information on these.
|
|
1993
|
+
*
|
|
1994
|
+
* Sugar (options):
|
|
1995
|
+
*
|
|
1996
|
+
* - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
|
|
1997
|
+
* negative windowBits implicitly.
|
|
1998
|
+
*
|
|
1999
|
+
* ##### Example:
|
|
2000
|
+
*
|
|
2001
|
+
* ```javascript
|
|
2002
|
+
* const pako = require('pako')
|
|
2003
|
+
* const data = new Uint8Array([1,2,3,4,5,6,7,8,9]);
|
|
2004
|
+
*
|
|
2005
|
+
* console.log(pako.deflate(data));
|
|
2006
|
+
* ```
|
|
2007
|
+
**/
|
|
2008
|
+
function deflate$1(input, options) {
|
|
2009
|
+
const deflator = new Deflate$1(options);
|
|
2010
|
+
deflator.push(input, true);
|
|
2011
|
+
if (deflator.err) throw deflator.msg || messages[deflator.err];
|
|
2012
|
+
return deflator.result;
|
|
2013
|
+
}
|
|
2014
|
+
/**
|
|
2015
|
+
* deflateRaw(data[, options]) -> Uint8Array
|
|
2016
|
+
* - data (Uint8Array|ArrayBuffer|String): input data to compress.
|
|
2017
|
+
* - options (Object): zlib deflate options.
|
|
2018
|
+
*
|
|
2019
|
+
* The same as [[deflate]], but creates raw data, without wrapper
|
|
2020
|
+
* (header and adler32 crc).
|
|
2021
|
+
**/
|
|
2022
|
+
function deflateRaw$1(input, options) {
|
|
2023
|
+
options = options || {};
|
|
2024
|
+
options.raw = true;
|
|
2025
|
+
return deflate$1(input, options);
|
|
2026
|
+
}
|
|
2027
|
+
/**
|
|
2028
|
+
* gzip(data[, options]) -> Uint8Array
|
|
2029
|
+
* - data (Uint8Array|ArrayBuffer|String): input data to compress.
|
|
2030
|
+
* - options (Object): zlib deflate options.
|
|
2031
|
+
*
|
|
2032
|
+
* The same as [[deflate]], but create gzip wrapper instead of
|
|
2033
|
+
* deflate one.
|
|
2034
|
+
**/
|
|
2035
|
+
function gzip$1(input, options) {
|
|
2036
|
+
options = options || {};
|
|
2037
|
+
options.gzip = true;
|
|
2038
|
+
return deflate$1(input, options);
|
|
2039
|
+
}
|
|
2040
|
+
var deflate_1$1 = {
|
|
2041
|
+
Deflate: Deflate$1,
|
|
2042
|
+
deflate: deflate$1,
|
|
2043
|
+
deflateRaw: deflateRaw$1,
|
|
2044
|
+
gzip: gzip$1,
|
|
2045
|
+
constants: constants$2
|
|
2046
|
+
};
|
|
2047
|
+
const BAD$1 = 16209;
|
|
2048
|
+
const TYPE$1 = 16191;
|
|
2049
|
+
var inffast = function inflate_fast(strm, start) {
|
|
2050
|
+
let _in;
|
|
2051
|
+
let last;
|
|
2052
|
+
let _out;
|
|
2053
|
+
let beg;
|
|
2054
|
+
let end;
|
|
2055
|
+
let dmax;
|
|
2056
|
+
let wsize;
|
|
2057
|
+
let whave;
|
|
2058
|
+
let wnext;
|
|
2059
|
+
let s_window;
|
|
2060
|
+
let hold;
|
|
2061
|
+
let bits;
|
|
2062
|
+
let lcode;
|
|
2063
|
+
let dcode;
|
|
2064
|
+
let lmask;
|
|
2065
|
+
let dmask;
|
|
2066
|
+
let here;
|
|
2067
|
+
let op;
|
|
2068
|
+
let len;
|
|
2069
|
+
let dist;
|
|
2070
|
+
let from;
|
|
2071
|
+
let from_source;
|
|
2072
|
+
let input, output;
|
|
2073
|
+
const state = strm.state;
|
|
2074
|
+
_in = strm.next_in;
|
|
2075
|
+
input = strm.input;
|
|
2076
|
+
last = _in + (strm.avail_in - 5);
|
|
2077
|
+
_out = strm.next_out;
|
|
2078
|
+
output = strm.output;
|
|
2079
|
+
beg = _out - (start - strm.avail_out);
|
|
2080
|
+
end = _out + (strm.avail_out - 257);
|
|
2081
|
+
dmax = state.dmax;
|
|
2082
|
+
wsize = state.wsize;
|
|
2083
|
+
whave = state.whave;
|
|
2084
|
+
wnext = state.wnext;
|
|
2085
|
+
s_window = state.window;
|
|
2086
|
+
hold = state.hold;
|
|
2087
|
+
bits = state.bits;
|
|
2088
|
+
lcode = state.lencode;
|
|
2089
|
+
dcode = state.distcode;
|
|
2090
|
+
lmask = (1 << state.lenbits) - 1;
|
|
2091
|
+
dmask = (1 << state.distbits) - 1;
|
|
2092
|
+
top: do {
|
|
2093
|
+
if (bits < 15) {
|
|
2094
|
+
hold += input[_in++] << bits;
|
|
2095
|
+
bits += 8;
|
|
2096
|
+
hold += input[_in++] << bits;
|
|
2097
|
+
bits += 8;
|
|
2098
|
+
}
|
|
2099
|
+
here = lcode[hold & lmask];
|
|
2100
|
+
dolen: for (;;) {
|
|
2101
|
+
op = here >>> 24;
|
|
2102
|
+
hold >>>= op;
|
|
2103
|
+
bits -= op;
|
|
2104
|
+
op = here >>> 16 & 255;
|
|
2105
|
+
if (op === 0) output[_out++] = here & 65535;
|
|
2106
|
+
else if (op & 16) {
|
|
2107
|
+
len = here & 65535;
|
|
2108
|
+
op &= 15;
|
|
2109
|
+
if (op) {
|
|
2110
|
+
if (bits < op) {
|
|
2111
|
+
hold += input[_in++] << bits;
|
|
2112
|
+
bits += 8;
|
|
2113
|
+
}
|
|
2114
|
+
len += hold & (1 << op) - 1;
|
|
2115
|
+
hold >>>= op;
|
|
2116
|
+
bits -= op;
|
|
2117
|
+
}
|
|
2118
|
+
if (bits < 15) {
|
|
2119
|
+
hold += input[_in++] << bits;
|
|
2120
|
+
bits += 8;
|
|
2121
|
+
hold += input[_in++] << bits;
|
|
2122
|
+
bits += 8;
|
|
2123
|
+
}
|
|
2124
|
+
here = dcode[hold & dmask];
|
|
2125
|
+
dodist: for (;;) {
|
|
2126
|
+
op = here >>> 24;
|
|
2127
|
+
hold >>>= op;
|
|
2128
|
+
bits -= op;
|
|
2129
|
+
op = here >>> 16 & 255;
|
|
2130
|
+
if (op & 16) {
|
|
2131
|
+
dist = here & 65535;
|
|
2132
|
+
op &= 15;
|
|
2133
|
+
if (bits < op) {
|
|
2134
|
+
hold += input[_in++] << bits;
|
|
2135
|
+
bits += 8;
|
|
2136
|
+
if (bits < op) {
|
|
2137
|
+
hold += input[_in++] << bits;
|
|
2138
|
+
bits += 8;
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
dist += hold & (1 << op) - 1;
|
|
2142
|
+
if (dist > dmax) {
|
|
2143
|
+
strm.msg = "invalid distance too far back";
|
|
2144
|
+
state.mode = BAD$1;
|
|
2145
|
+
break top;
|
|
2146
|
+
}
|
|
2147
|
+
hold >>>= op;
|
|
2148
|
+
bits -= op;
|
|
2149
|
+
op = _out - beg;
|
|
2150
|
+
if (dist > op) {
|
|
2151
|
+
op = dist - op;
|
|
2152
|
+
if (op > whave) {
|
|
2153
|
+
if (state.sane) {
|
|
2154
|
+
strm.msg = "invalid distance too far back";
|
|
2155
|
+
state.mode = BAD$1;
|
|
2156
|
+
break top;
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
from = 0;
|
|
2160
|
+
from_source = s_window;
|
|
2161
|
+
if (wnext === 0) {
|
|
2162
|
+
from += wsize - op;
|
|
2163
|
+
if (op < len) {
|
|
2164
|
+
len -= op;
|
|
2165
|
+
do
|
|
2166
|
+
output[_out++] = s_window[from++];
|
|
2167
|
+
while (--op);
|
|
2168
|
+
from = _out - dist;
|
|
2169
|
+
from_source = output;
|
|
2170
|
+
}
|
|
2171
|
+
} else if (wnext < op) {
|
|
2172
|
+
from += wsize + wnext - op;
|
|
2173
|
+
op -= wnext;
|
|
2174
|
+
if (op < len) {
|
|
2175
|
+
len -= op;
|
|
2176
|
+
do
|
|
2177
|
+
output[_out++] = s_window[from++];
|
|
2178
|
+
while (--op);
|
|
2179
|
+
from = 0;
|
|
2180
|
+
if (wnext < len) {
|
|
2181
|
+
op = wnext;
|
|
2182
|
+
len -= op;
|
|
2183
|
+
do
|
|
2184
|
+
output[_out++] = s_window[from++];
|
|
2185
|
+
while (--op);
|
|
2186
|
+
from = _out - dist;
|
|
2187
|
+
from_source = output;
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
} else {
|
|
2191
|
+
from += wnext - op;
|
|
2192
|
+
if (op < len) {
|
|
2193
|
+
len -= op;
|
|
2194
|
+
do
|
|
2195
|
+
output[_out++] = s_window[from++];
|
|
2196
|
+
while (--op);
|
|
2197
|
+
from = _out - dist;
|
|
2198
|
+
from_source = output;
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
while (len > 2) {
|
|
2202
|
+
output[_out++] = from_source[from++];
|
|
2203
|
+
output[_out++] = from_source[from++];
|
|
2204
|
+
output[_out++] = from_source[from++];
|
|
2205
|
+
len -= 3;
|
|
2206
|
+
}
|
|
2207
|
+
if (len) {
|
|
2208
|
+
output[_out++] = from_source[from++];
|
|
2209
|
+
if (len > 1) output[_out++] = from_source[from++];
|
|
2210
|
+
}
|
|
2211
|
+
} else {
|
|
2212
|
+
from = _out - dist;
|
|
2213
|
+
do {
|
|
2214
|
+
output[_out++] = output[from++];
|
|
2215
|
+
output[_out++] = output[from++];
|
|
2216
|
+
output[_out++] = output[from++];
|
|
2217
|
+
len -= 3;
|
|
2218
|
+
} while (len > 2);
|
|
2219
|
+
if (len) {
|
|
2220
|
+
output[_out++] = output[from++];
|
|
2221
|
+
if (len > 1) output[_out++] = output[from++];
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
} else if ((op & 64) === 0) {
|
|
2225
|
+
here = dcode[(here & 65535) + (hold & (1 << op) - 1)];
|
|
2226
|
+
continue dodist;
|
|
2227
|
+
} else {
|
|
2228
|
+
strm.msg = "invalid distance code";
|
|
2229
|
+
state.mode = BAD$1;
|
|
2230
|
+
break top;
|
|
2231
|
+
}
|
|
2232
|
+
break;
|
|
2233
|
+
}
|
|
2234
|
+
} else if ((op & 64) === 0) {
|
|
2235
|
+
here = lcode[(here & 65535) + (hold & (1 << op) - 1)];
|
|
2236
|
+
continue dolen;
|
|
2237
|
+
} else if (op & 32) {
|
|
2238
|
+
state.mode = TYPE$1;
|
|
2239
|
+
break top;
|
|
2240
|
+
} else {
|
|
2241
|
+
strm.msg = "invalid literal/length code";
|
|
2242
|
+
state.mode = BAD$1;
|
|
2243
|
+
break top;
|
|
2244
|
+
}
|
|
2245
|
+
break;
|
|
2246
|
+
}
|
|
2247
|
+
} while (_in < last && _out < end);
|
|
2248
|
+
len = bits >> 3;
|
|
2249
|
+
_in -= len;
|
|
2250
|
+
bits -= len << 3;
|
|
2251
|
+
hold &= (1 << bits) - 1;
|
|
2252
|
+
strm.next_in = _in;
|
|
2253
|
+
strm.next_out = _out;
|
|
2254
|
+
strm.avail_in = _in < last ? 5 + (last - _in) : 5 - (_in - last);
|
|
2255
|
+
strm.avail_out = _out < end ? 257 + (end - _out) : 257 - (_out - end);
|
|
2256
|
+
state.hold = hold;
|
|
2257
|
+
state.bits = bits;
|
|
2258
|
+
};
|
|
2259
|
+
const MAXBITS = 15;
|
|
2260
|
+
const ENOUGH_LENS$1 = 852;
|
|
2261
|
+
const ENOUGH_DISTS$1 = 592;
|
|
2262
|
+
const CODES$1 = 0;
|
|
2263
|
+
const LENS$1 = 1;
|
|
2264
|
+
const DISTS$1 = 2;
|
|
2265
|
+
const lbase = new Uint16Array([
|
|
2266
|
+
3,
|
|
2267
|
+
4,
|
|
2268
|
+
5,
|
|
2269
|
+
6,
|
|
2270
|
+
7,
|
|
2271
|
+
8,
|
|
2272
|
+
9,
|
|
2273
|
+
10,
|
|
2274
|
+
11,
|
|
2275
|
+
13,
|
|
2276
|
+
15,
|
|
2277
|
+
17,
|
|
2278
|
+
19,
|
|
2279
|
+
23,
|
|
2280
|
+
27,
|
|
2281
|
+
31,
|
|
2282
|
+
35,
|
|
2283
|
+
43,
|
|
2284
|
+
51,
|
|
2285
|
+
59,
|
|
2286
|
+
67,
|
|
2287
|
+
83,
|
|
2288
|
+
99,
|
|
2289
|
+
115,
|
|
2290
|
+
131,
|
|
2291
|
+
163,
|
|
2292
|
+
195,
|
|
2293
|
+
227,
|
|
2294
|
+
258,
|
|
2295
|
+
0,
|
|
2296
|
+
0
|
|
2297
|
+
]);
|
|
2298
|
+
const lext = new Uint8Array([
|
|
2299
|
+
16,
|
|
2300
|
+
16,
|
|
2301
|
+
16,
|
|
2302
|
+
16,
|
|
2303
|
+
16,
|
|
2304
|
+
16,
|
|
2305
|
+
16,
|
|
2306
|
+
16,
|
|
2307
|
+
17,
|
|
2308
|
+
17,
|
|
2309
|
+
17,
|
|
2310
|
+
17,
|
|
2311
|
+
18,
|
|
2312
|
+
18,
|
|
2313
|
+
18,
|
|
2314
|
+
18,
|
|
2315
|
+
19,
|
|
2316
|
+
19,
|
|
2317
|
+
19,
|
|
2318
|
+
19,
|
|
2319
|
+
20,
|
|
2320
|
+
20,
|
|
2321
|
+
20,
|
|
2322
|
+
20,
|
|
2323
|
+
21,
|
|
2324
|
+
21,
|
|
2325
|
+
21,
|
|
2326
|
+
21,
|
|
2327
|
+
16,
|
|
2328
|
+
72,
|
|
2329
|
+
78
|
|
2330
|
+
]);
|
|
2331
|
+
const dbase = new Uint16Array([
|
|
2332
|
+
1,
|
|
2333
|
+
2,
|
|
2334
|
+
3,
|
|
2335
|
+
4,
|
|
2336
|
+
5,
|
|
2337
|
+
7,
|
|
2338
|
+
9,
|
|
2339
|
+
13,
|
|
2340
|
+
17,
|
|
2341
|
+
25,
|
|
2342
|
+
33,
|
|
2343
|
+
49,
|
|
2344
|
+
65,
|
|
2345
|
+
97,
|
|
2346
|
+
129,
|
|
2347
|
+
193,
|
|
2348
|
+
257,
|
|
2349
|
+
385,
|
|
2350
|
+
513,
|
|
2351
|
+
769,
|
|
2352
|
+
1025,
|
|
2353
|
+
1537,
|
|
2354
|
+
2049,
|
|
2355
|
+
3073,
|
|
2356
|
+
4097,
|
|
2357
|
+
6145,
|
|
2358
|
+
8193,
|
|
2359
|
+
12289,
|
|
2360
|
+
16385,
|
|
2361
|
+
24577,
|
|
2362
|
+
0,
|
|
2363
|
+
0
|
|
2364
|
+
]);
|
|
2365
|
+
const dext = new Uint8Array([
|
|
2366
|
+
16,
|
|
2367
|
+
16,
|
|
2368
|
+
16,
|
|
2369
|
+
16,
|
|
2370
|
+
17,
|
|
2371
|
+
17,
|
|
2372
|
+
18,
|
|
2373
|
+
18,
|
|
2374
|
+
19,
|
|
2375
|
+
19,
|
|
2376
|
+
20,
|
|
2377
|
+
20,
|
|
2378
|
+
21,
|
|
2379
|
+
21,
|
|
2380
|
+
22,
|
|
2381
|
+
22,
|
|
2382
|
+
23,
|
|
2383
|
+
23,
|
|
2384
|
+
24,
|
|
2385
|
+
24,
|
|
2386
|
+
25,
|
|
2387
|
+
25,
|
|
2388
|
+
26,
|
|
2389
|
+
26,
|
|
2390
|
+
27,
|
|
2391
|
+
27,
|
|
2392
|
+
28,
|
|
2393
|
+
28,
|
|
2394
|
+
29,
|
|
2395
|
+
29,
|
|
2396
|
+
64,
|
|
2397
|
+
64
|
|
2398
|
+
]);
|
|
2399
|
+
const inflate_table = (type, lens, lens_index, codes, table, table_index, work, opts) => {
|
|
2400
|
+
const bits = opts.bits;
|
|
2401
|
+
let len = 0;
|
|
2402
|
+
let sym = 0;
|
|
2403
|
+
let min = 0, max = 0;
|
|
2404
|
+
let root = 0;
|
|
2405
|
+
let curr = 0;
|
|
2406
|
+
let drop = 0;
|
|
2407
|
+
let left = 0;
|
|
2408
|
+
let used = 0;
|
|
2409
|
+
let huff = 0;
|
|
2410
|
+
let incr;
|
|
2411
|
+
let fill;
|
|
2412
|
+
let low;
|
|
2413
|
+
let mask;
|
|
2414
|
+
let next;
|
|
2415
|
+
let base = null;
|
|
2416
|
+
let match;
|
|
2417
|
+
const count = new Uint16Array(MAXBITS + 1);
|
|
2418
|
+
const offs = new Uint16Array(MAXBITS + 1);
|
|
2419
|
+
let extra = null;
|
|
2420
|
+
let here_bits, here_op, here_val;
|
|
2421
|
+
for (len = 0; len <= MAXBITS; len++) count[len] = 0;
|
|
2422
|
+
for (sym = 0; sym < codes; sym++) count[lens[lens_index + sym]]++;
|
|
2423
|
+
root = bits;
|
|
2424
|
+
for (max = MAXBITS; max >= 1; max--) if (count[max] !== 0) break;
|
|
2425
|
+
if (root > max) root = max;
|
|
2426
|
+
if (max === 0) {
|
|
2427
|
+
table[table_index++] = 20971520;
|
|
2428
|
+
table[table_index++] = 20971520;
|
|
2429
|
+
opts.bits = 1;
|
|
2430
|
+
return 0;
|
|
2431
|
+
}
|
|
2432
|
+
for (min = 1; min < max; min++) if (count[min] !== 0) break;
|
|
2433
|
+
if (root < min) root = min;
|
|
2434
|
+
left = 1;
|
|
2435
|
+
for (len = 1; len <= MAXBITS; len++) {
|
|
2436
|
+
left <<= 1;
|
|
2437
|
+
left -= count[len];
|
|
2438
|
+
if (left < 0) return -1;
|
|
2439
|
+
}
|
|
2440
|
+
if (left > 0 && (type === CODES$1 || max !== 1)) return -1;
|
|
2441
|
+
offs[1] = 0;
|
|
2442
|
+
for (len = 1; len < MAXBITS; len++) offs[len + 1] = offs[len] + count[len];
|
|
2443
|
+
for (sym = 0; sym < codes; sym++) if (lens[lens_index + sym] !== 0) work[offs[lens[lens_index + sym]]++] = sym;
|
|
2444
|
+
if (type === CODES$1) {
|
|
2445
|
+
base = extra = work;
|
|
2446
|
+
match = 20;
|
|
2447
|
+
} else if (type === LENS$1) {
|
|
2448
|
+
base = lbase;
|
|
2449
|
+
extra = lext;
|
|
2450
|
+
match = 257;
|
|
2451
|
+
} else {
|
|
2452
|
+
base = dbase;
|
|
2453
|
+
extra = dext;
|
|
2454
|
+
match = 0;
|
|
2455
|
+
}
|
|
2456
|
+
huff = 0;
|
|
2457
|
+
sym = 0;
|
|
2458
|
+
len = min;
|
|
2459
|
+
next = table_index;
|
|
2460
|
+
curr = root;
|
|
2461
|
+
drop = 0;
|
|
2462
|
+
low = -1;
|
|
2463
|
+
used = 1 << root;
|
|
2464
|
+
mask = used - 1;
|
|
2465
|
+
if (type === LENS$1 && used > ENOUGH_LENS$1 || type === DISTS$1 && used > ENOUGH_DISTS$1) return 1;
|
|
2466
|
+
for (;;) {
|
|
2467
|
+
here_bits = len - drop;
|
|
2468
|
+
if (work[sym] + 1 < match) {
|
|
2469
|
+
here_op = 0;
|
|
2470
|
+
here_val = work[sym];
|
|
2471
|
+
} else if (work[sym] >= match) {
|
|
2472
|
+
here_op = extra[work[sym] - match];
|
|
2473
|
+
here_val = base[work[sym] - match];
|
|
2474
|
+
} else {
|
|
2475
|
+
here_op = 96;
|
|
2476
|
+
here_val = 0;
|
|
2477
|
+
}
|
|
2478
|
+
incr = 1 << len - drop;
|
|
2479
|
+
fill = 1 << curr;
|
|
2480
|
+
min = fill;
|
|
2481
|
+
do {
|
|
2482
|
+
fill -= incr;
|
|
2483
|
+
table[next + (huff >> drop) + fill] = here_bits << 24 | here_op << 16 | here_val | 0;
|
|
2484
|
+
} while (fill !== 0);
|
|
2485
|
+
incr = 1 << len - 1;
|
|
2486
|
+
while (huff & incr) incr >>= 1;
|
|
2487
|
+
if (incr !== 0) {
|
|
2488
|
+
huff &= incr - 1;
|
|
2489
|
+
huff += incr;
|
|
2490
|
+
} else huff = 0;
|
|
2491
|
+
sym++;
|
|
2492
|
+
if (--count[len] === 0) {
|
|
2493
|
+
if (len === max) break;
|
|
2494
|
+
len = lens[lens_index + work[sym]];
|
|
2495
|
+
}
|
|
2496
|
+
if (len > root && (huff & mask) !== low) {
|
|
2497
|
+
if (drop === 0) drop = root;
|
|
2498
|
+
next += min;
|
|
2499
|
+
curr = len - drop;
|
|
2500
|
+
left = 1 << curr;
|
|
2501
|
+
while (curr + drop < max) {
|
|
2502
|
+
left -= count[curr + drop];
|
|
2503
|
+
if (left <= 0) break;
|
|
2504
|
+
curr++;
|
|
2505
|
+
left <<= 1;
|
|
2506
|
+
}
|
|
2507
|
+
used += 1 << curr;
|
|
2508
|
+
if (type === LENS$1 && used > ENOUGH_LENS$1 || type === DISTS$1 && used > ENOUGH_DISTS$1) return 1;
|
|
2509
|
+
low = huff & mask;
|
|
2510
|
+
table[low] = root << 24 | curr << 16 | next - table_index | 0;
|
|
2511
|
+
}
|
|
2512
|
+
}
|
|
2513
|
+
if (huff !== 0) table[next + huff] = len - drop << 24 | 4194304;
|
|
2514
|
+
opts.bits = root;
|
|
2515
|
+
return 0;
|
|
2516
|
+
};
|
|
2517
|
+
var inftrees = inflate_table;
|
|
2518
|
+
const CODES = 0;
|
|
2519
|
+
const LENS = 1;
|
|
2520
|
+
const DISTS = 2;
|
|
2521
|
+
const { Z_FINISH: Z_FINISH$1, Z_BLOCK, Z_TREES, Z_OK: Z_OK$1, Z_STREAM_END: Z_STREAM_END$1, Z_NEED_DICT: Z_NEED_DICT$1, Z_STREAM_ERROR: Z_STREAM_ERROR$1, Z_DATA_ERROR: Z_DATA_ERROR$1, Z_MEM_ERROR: Z_MEM_ERROR$1, Z_BUF_ERROR, Z_DEFLATED } = constants$2;
|
|
2522
|
+
const HEAD = 16180;
|
|
2523
|
+
const FLAGS = 16181;
|
|
2524
|
+
const TIME = 16182;
|
|
2525
|
+
const OS = 16183;
|
|
2526
|
+
const EXLEN = 16184;
|
|
2527
|
+
const EXTRA = 16185;
|
|
2528
|
+
const NAME = 16186;
|
|
2529
|
+
const COMMENT = 16187;
|
|
2530
|
+
const HCRC = 16188;
|
|
2531
|
+
const DICTID = 16189;
|
|
2532
|
+
const DICT = 16190;
|
|
2533
|
+
const TYPE = 16191;
|
|
2534
|
+
const TYPEDO = 16192;
|
|
2535
|
+
const STORED = 16193;
|
|
2536
|
+
const COPY_ = 16194;
|
|
2537
|
+
const COPY = 16195;
|
|
2538
|
+
const TABLE = 16196;
|
|
2539
|
+
const LENLENS = 16197;
|
|
2540
|
+
const CODELENS = 16198;
|
|
2541
|
+
const LEN_ = 16199;
|
|
2542
|
+
const LEN = 16200;
|
|
2543
|
+
const LENEXT = 16201;
|
|
2544
|
+
const DIST = 16202;
|
|
2545
|
+
const DISTEXT = 16203;
|
|
2546
|
+
const MATCH = 16204;
|
|
2547
|
+
const LIT = 16205;
|
|
2548
|
+
const CHECK = 16206;
|
|
2549
|
+
const LENGTH = 16207;
|
|
2550
|
+
const DONE = 16208;
|
|
2551
|
+
const BAD = 16209;
|
|
2552
|
+
const MEM = 16210;
|
|
2553
|
+
const SYNC = 16211;
|
|
2554
|
+
const ENOUGH_LENS = 852;
|
|
2555
|
+
const ENOUGH_DISTS = 592;
|
|
2556
|
+
const DEF_WBITS = 15;
|
|
2557
|
+
const zswap32 = (q) => {
|
|
2558
|
+
return (q >>> 24 & 255) + (q >>> 8 & 65280) + ((q & 65280) << 8) + ((q & 255) << 24);
|
|
2559
|
+
};
|
|
2560
|
+
function InflateState() {
|
|
2561
|
+
this.strm = null;
|
|
2562
|
+
this.mode = 0;
|
|
2563
|
+
this.last = false;
|
|
2564
|
+
this.wrap = 0;
|
|
2565
|
+
this.havedict = false;
|
|
2566
|
+
this.flags = 0;
|
|
2567
|
+
this.dmax = 0;
|
|
2568
|
+
this.check = 0;
|
|
2569
|
+
this.total = 0;
|
|
2570
|
+
this.head = null;
|
|
2571
|
+
this.wbits = 0;
|
|
2572
|
+
this.wsize = 0;
|
|
2573
|
+
this.whave = 0;
|
|
2574
|
+
this.wnext = 0;
|
|
2575
|
+
this.window = null;
|
|
2576
|
+
this.hold = 0;
|
|
2577
|
+
this.bits = 0;
|
|
2578
|
+
this.length = 0;
|
|
2579
|
+
this.offset = 0;
|
|
2580
|
+
this.extra = 0;
|
|
2581
|
+
this.lencode = null;
|
|
2582
|
+
this.distcode = null;
|
|
2583
|
+
this.lenbits = 0;
|
|
2584
|
+
this.distbits = 0;
|
|
2585
|
+
this.ncode = 0;
|
|
2586
|
+
this.nlen = 0;
|
|
2587
|
+
this.ndist = 0;
|
|
2588
|
+
this.have = 0;
|
|
2589
|
+
this.next = null;
|
|
2590
|
+
this.lens = new Uint16Array(320);
|
|
2591
|
+
this.work = new Uint16Array(288);
|
|
2592
|
+
this.lendyn = null;
|
|
2593
|
+
this.distdyn = null;
|
|
2594
|
+
this.sane = 0;
|
|
2595
|
+
this.back = 0;
|
|
2596
|
+
this.was = 0;
|
|
2597
|
+
}
|
|
2598
|
+
const inflateStateCheck = (strm) => {
|
|
2599
|
+
if (!strm) return 1;
|
|
2600
|
+
const state = strm.state;
|
|
2601
|
+
if (!state || state.strm !== strm || state.mode < HEAD || state.mode > SYNC) return 1;
|
|
2602
|
+
return 0;
|
|
2603
|
+
};
|
|
2604
|
+
const inflateResetKeep = (strm) => {
|
|
2605
|
+
if (inflateStateCheck(strm)) return Z_STREAM_ERROR$1;
|
|
2606
|
+
const state = strm.state;
|
|
2607
|
+
strm.total_in = strm.total_out = state.total = 0;
|
|
2608
|
+
strm.msg = "";
|
|
2609
|
+
if (state.wrap) strm.adler = state.wrap & 1;
|
|
2610
|
+
state.mode = HEAD;
|
|
2611
|
+
state.last = 0;
|
|
2612
|
+
state.havedict = 0;
|
|
2613
|
+
state.flags = -1;
|
|
2614
|
+
state.dmax = 32768;
|
|
2615
|
+
state.head = null;
|
|
2616
|
+
state.hold = 0;
|
|
2617
|
+
state.bits = 0;
|
|
2618
|
+
state.lencode = state.lendyn = new Int32Array(ENOUGH_LENS);
|
|
2619
|
+
state.distcode = state.distdyn = new Int32Array(ENOUGH_DISTS);
|
|
2620
|
+
state.sane = 1;
|
|
2621
|
+
state.back = -1;
|
|
2622
|
+
return Z_OK$1;
|
|
2623
|
+
};
|
|
2624
|
+
const inflateReset = (strm) => {
|
|
2625
|
+
if (inflateStateCheck(strm)) return Z_STREAM_ERROR$1;
|
|
2626
|
+
const state = strm.state;
|
|
2627
|
+
state.wsize = 0;
|
|
2628
|
+
state.whave = 0;
|
|
2629
|
+
state.wnext = 0;
|
|
2630
|
+
return inflateResetKeep(strm);
|
|
2631
|
+
};
|
|
2632
|
+
const inflateReset2 = (strm, windowBits) => {
|
|
2633
|
+
let wrap;
|
|
2634
|
+
if (inflateStateCheck(strm)) return Z_STREAM_ERROR$1;
|
|
2635
|
+
const state = strm.state;
|
|
2636
|
+
if (windowBits < 0) {
|
|
2637
|
+
wrap = 0;
|
|
2638
|
+
windowBits = -windowBits;
|
|
2639
|
+
} else {
|
|
2640
|
+
wrap = (windowBits >> 4) + 5;
|
|
2641
|
+
if (windowBits < 48) windowBits &= 15;
|
|
2642
|
+
}
|
|
2643
|
+
if (windowBits && (windowBits < 8 || windowBits > 15)) return Z_STREAM_ERROR$1;
|
|
2644
|
+
if (state.window !== null && state.wbits !== windowBits) state.window = null;
|
|
2645
|
+
state.wrap = wrap;
|
|
2646
|
+
state.wbits = windowBits;
|
|
2647
|
+
return inflateReset(strm);
|
|
2648
|
+
};
|
|
2649
|
+
const inflateInit2 = (strm, windowBits) => {
|
|
2650
|
+
if (!strm) return Z_STREAM_ERROR$1;
|
|
2651
|
+
const state = new InflateState();
|
|
2652
|
+
strm.state = state;
|
|
2653
|
+
state.strm = strm;
|
|
2654
|
+
state.window = null;
|
|
2655
|
+
state.mode = HEAD;
|
|
2656
|
+
const ret = inflateReset2(strm, windowBits);
|
|
2657
|
+
if (ret !== Z_OK$1) strm.state = null;
|
|
2658
|
+
return ret;
|
|
2659
|
+
};
|
|
2660
|
+
const inflateInit = (strm) => {
|
|
2661
|
+
return inflateInit2(strm, DEF_WBITS);
|
|
2662
|
+
};
|
|
2663
|
+
let virgin = true;
|
|
2664
|
+
let lenfix, distfix;
|
|
2665
|
+
const fixedtables = (state) => {
|
|
2666
|
+
if (virgin) {
|
|
2667
|
+
lenfix = new Int32Array(512);
|
|
2668
|
+
distfix = new Int32Array(32);
|
|
2669
|
+
let sym = 0;
|
|
2670
|
+
while (sym < 144) state.lens[sym++] = 8;
|
|
2671
|
+
while (sym < 256) state.lens[sym++] = 9;
|
|
2672
|
+
while (sym < 280) state.lens[sym++] = 7;
|
|
2673
|
+
while (sym < 288) state.lens[sym++] = 8;
|
|
2674
|
+
inftrees(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
|
|
2675
|
+
sym = 0;
|
|
2676
|
+
while (sym < 32) state.lens[sym++] = 5;
|
|
2677
|
+
inftrees(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
|
|
2678
|
+
virgin = false;
|
|
2679
|
+
}
|
|
2680
|
+
state.lencode = lenfix;
|
|
2681
|
+
state.lenbits = 9;
|
|
2682
|
+
state.distcode = distfix;
|
|
2683
|
+
state.distbits = 5;
|
|
2684
|
+
};
|
|
2685
|
+
const updatewindow = (strm, src, end, copy) => {
|
|
2686
|
+
let dist;
|
|
2687
|
+
const state = strm.state;
|
|
2688
|
+
if (state.window === null) {
|
|
2689
|
+
state.wsize = 1 << state.wbits;
|
|
2690
|
+
state.wnext = 0;
|
|
2691
|
+
state.whave = 0;
|
|
2692
|
+
state.window = new Uint8Array(state.wsize);
|
|
2693
|
+
}
|
|
2694
|
+
if (copy >= state.wsize) {
|
|
2695
|
+
state.window.set(src.subarray(end - state.wsize, end), 0);
|
|
2696
|
+
state.wnext = 0;
|
|
2697
|
+
state.whave = state.wsize;
|
|
2698
|
+
} else {
|
|
2699
|
+
dist = state.wsize - state.wnext;
|
|
2700
|
+
if (dist > copy) dist = copy;
|
|
2701
|
+
state.window.set(src.subarray(end - copy, end - copy + dist), state.wnext);
|
|
2702
|
+
copy -= dist;
|
|
2703
|
+
if (copy) {
|
|
2704
|
+
state.window.set(src.subarray(end - copy, end), 0);
|
|
2705
|
+
state.wnext = copy;
|
|
2706
|
+
state.whave = state.wsize;
|
|
2707
|
+
} else {
|
|
2708
|
+
state.wnext += dist;
|
|
2709
|
+
if (state.wnext === state.wsize) state.wnext = 0;
|
|
2710
|
+
if (state.whave < state.wsize) state.whave += dist;
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
return 0;
|
|
2714
|
+
};
|
|
2715
|
+
const inflate$2 = (strm, flush) => {
|
|
2716
|
+
let state;
|
|
2717
|
+
let input, output;
|
|
2718
|
+
let next;
|
|
2719
|
+
let put;
|
|
2720
|
+
let have, left;
|
|
2721
|
+
let hold;
|
|
2722
|
+
let bits;
|
|
2723
|
+
let _in, _out;
|
|
2724
|
+
let copy;
|
|
2725
|
+
let from;
|
|
2726
|
+
let from_source;
|
|
2727
|
+
let here = 0;
|
|
2728
|
+
let here_bits, here_op, here_val;
|
|
2729
|
+
let last_bits, last_op, last_val;
|
|
2730
|
+
let len;
|
|
2731
|
+
let ret;
|
|
2732
|
+
const hbuf = new Uint8Array(4);
|
|
2733
|
+
let opts;
|
|
2734
|
+
let n;
|
|
2735
|
+
const order = new Uint8Array([
|
|
2736
|
+
16,
|
|
2737
|
+
17,
|
|
2738
|
+
18,
|
|
2739
|
+
0,
|
|
2740
|
+
8,
|
|
2741
|
+
7,
|
|
2742
|
+
9,
|
|
2743
|
+
6,
|
|
2744
|
+
10,
|
|
2745
|
+
5,
|
|
2746
|
+
11,
|
|
2747
|
+
4,
|
|
2748
|
+
12,
|
|
2749
|
+
3,
|
|
2750
|
+
13,
|
|
2751
|
+
2,
|
|
2752
|
+
14,
|
|
2753
|
+
1,
|
|
2754
|
+
15
|
|
2755
|
+
]);
|
|
2756
|
+
if (inflateStateCheck(strm) || !strm.output || !strm.input && strm.avail_in !== 0) return Z_STREAM_ERROR$1;
|
|
2757
|
+
state = strm.state;
|
|
2758
|
+
if (state.mode === TYPE) state.mode = TYPEDO;
|
|
2759
|
+
put = strm.next_out;
|
|
2760
|
+
output = strm.output;
|
|
2761
|
+
left = strm.avail_out;
|
|
2762
|
+
next = strm.next_in;
|
|
2763
|
+
input = strm.input;
|
|
2764
|
+
have = strm.avail_in;
|
|
2765
|
+
hold = state.hold;
|
|
2766
|
+
bits = state.bits;
|
|
2767
|
+
_in = have;
|
|
2768
|
+
_out = left;
|
|
2769
|
+
ret = Z_OK$1;
|
|
2770
|
+
inf_leave: for (;;) switch (state.mode) {
|
|
2771
|
+
case HEAD:
|
|
2772
|
+
if (state.wrap === 0) {
|
|
2773
|
+
state.mode = TYPEDO;
|
|
2774
|
+
break;
|
|
2775
|
+
}
|
|
2776
|
+
while (bits < 16) {
|
|
2777
|
+
if (have === 0) break inf_leave;
|
|
2778
|
+
have--;
|
|
2779
|
+
hold += input[next++] << bits;
|
|
2780
|
+
bits += 8;
|
|
2781
|
+
}
|
|
2782
|
+
if (state.wrap & 2 && hold === 35615) {
|
|
2783
|
+
if (state.wbits === 0) state.wbits = 15;
|
|
2784
|
+
state.check = 0;
|
|
2785
|
+
hbuf[0] = hold & 255;
|
|
2786
|
+
hbuf[1] = hold >>> 8 & 255;
|
|
2787
|
+
state.check = crc32_1(state.check, hbuf, 2, 0);
|
|
2788
|
+
hold = 0;
|
|
2789
|
+
bits = 0;
|
|
2790
|
+
state.mode = FLAGS;
|
|
2791
|
+
break;
|
|
2792
|
+
}
|
|
2793
|
+
if (state.head) state.head.done = false;
|
|
2794
|
+
if (!(state.wrap & 1) || (((hold & 255) << 8) + (hold >> 8)) % 31) {
|
|
2795
|
+
strm.msg = "incorrect header check";
|
|
2796
|
+
state.mode = BAD;
|
|
2797
|
+
break;
|
|
2798
|
+
}
|
|
2799
|
+
if ((hold & 15) !== Z_DEFLATED) {
|
|
2800
|
+
strm.msg = "unknown compression method";
|
|
2801
|
+
state.mode = BAD;
|
|
2802
|
+
break;
|
|
2803
|
+
}
|
|
2804
|
+
hold >>>= 4;
|
|
2805
|
+
bits -= 4;
|
|
2806
|
+
len = (hold & 15) + 8;
|
|
2807
|
+
if (state.wbits === 0) state.wbits = len;
|
|
2808
|
+
if (len > 15 || len > state.wbits) {
|
|
2809
|
+
strm.msg = "invalid window size";
|
|
2810
|
+
state.mode = BAD;
|
|
2811
|
+
break;
|
|
2812
|
+
}
|
|
2813
|
+
state.dmax = 1 << state.wbits;
|
|
2814
|
+
state.flags = 0;
|
|
2815
|
+
strm.adler = state.check = 1;
|
|
2816
|
+
state.mode = hold & 512 ? DICTID : TYPE;
|
|
2817
|
+
hold = 0;
|
|
2818
|
+
bits = 0;
|
|
2819
|
+
break;
|
|
2820
|
+
case FLAGS:
|
|
2821
|
+
while (bits < 16) {
|
|
2822
|
+
if (have === 0) break inf_leave;
|
|
2823
|
+
have--;
|
|
2824
|
+
hold += input[next++] << bits;
|
|
2825
|
+
bits += 8;
|
|
2826
|
+
}
|
|
2827
|
+
state.flags = hold;
|
|
2828
|
+
if ((state.flags & 255) !== Z_DEFLATED) {
|
|
2829
|
+
strm.msg = "unknown compression method";
|
|
2830
|
+
state.mode = BAD;
|
|
2831
|
+
break;
|
|
2832
|
+
}
|
|
2833
|
+
if (state.flags & 57344) {
|
|
2834
|
+
strm.msg = "unknown header flags set";
|
|
2835
|
+
state.mode = BAD;
|
|
2836
|
+
break;
|
|
2837
|
+
}
|
|
2838
|
+
if (state.head) state.head.text = hold >> 8 & 1;
|
|
2839
|
+
if (state.flags & 512 && state.wrap & 4) {
|
|
2840
|
+
hbuf[0] = hold & 255;
|
|
2841
|
+
hbuf[1] = hold >>> 8 & 255;
|
|
2842
|
+
state.check = crc32_1(state.check, hbuf, 2, 0);
|
|
2843
|
+
}
|
|
2844
|
+
hold = 0;
|
|
2845
|
+
bits = 0;
|
|
2846
|
+
state.mode = TIME;
|
|
2847
|
+
case TIME:
|
|
2848
|
+
while (bits < 32) {
|
|
2849
|
+
if (have === 0) break inf_leave;
|
|
2850
|
+
have--;
|
|
2851
|
+
hold += input[next++] << bits;
|
|
2852
|
+
bits += 8;
|
|
2853
|
+
}
|
|
2854
|
+
if (state.head) state.head.time = hold;
|
|
2855
|
+
if (state.flags & 512 && state.wrap & 4) {
|
|
2856
|
+
hbuf[0] = hold & 255;
|
|
2857
|
+
hbuf[1] = hold >>> 8 & 255;
|
|
2858
|
+
hbuf[2] = hold >>> 16 & 255;
|
|
2859
|
+
hbuf[3] = hold >>> 24 & 255;
|
|
2860
|
+
state.check = crc32_1(state.check, hbuf, 4, 0);
|
|
2861
|
+
}
|
|
2862
|
+
hold = 0;
|
|
2863
|
+
bits = 0;
|
|
2864
|
+
state.mode = OS;
|
|
2865
|
+
case OS:
|
|
2866
|
+
while (bits < 16) {
|
|
2867
|
+
if (have === 0) break inf_leave;
|
|
2868
|
+
have--;
|
|
2869
|
+
hold += input[next++] << bits;
|
|
2870
|
+
bits += 8;
|
|
2871
|
+
}
|
|
2872
|
+
if (state.head) {
|
|
2873
|
+
state.head.xflags = hold & 255;
|
|
2874
|
+
state.head.os = hold >> 8;
|
|
2875
|
+
}
|
|
2876
|
+
if (state.flags & 512 && state.wrap & 4) {
|
|
2877
|
+
hbuf[0] = hold & 255;
|
|
2878
|
+
hbuf[1] = hold >>> 8 & 255;
|
|
2879
|
+
state.check = crc32_1(state.check, hbuf, 2, 0);
|
|
2880
|
+
}
|
|
2881
|
+
hold = 0;
|
|
2882
|
+
bits = 0;
|
|
2883
|
+
state.mode = EXLEN;
|
|
2884
|
+
case EXLEN:
|
|
2885
|
+
if (state.flags & 1024) {
|
|
2886
|
+
while (bits < 16) {
|
|
2887
|
+
if (have === 0) break inf_leave;
|
|
2888
|
+
have--;
|
|
2889
|
+
hold += input[next++] << bits;
|
|
2890
|
+
bits += 8;
|
|
2891
|
+
}
|
|
2892
|
+
state.length = hold;
|
|
2893
|
+
if (state.head) state.head.extra_len = hold;
|
|
2894
|
+
if (state.flags & 512 && state.wrap & 4) {
|
|
2895
|
+
hbuf[0] = hold & 255;
|
|
2896
|
+
hbuf[1] = hold >>> 8 & 255;
|
|
2897
|
+
state.check = crc32_1(state.check, hbuf, 2, 0);
|
|
2898
|
+
}
|
|
2899
|
+
hold = 0;
|
|
2900
|
+
bits = 0;
|
|
2901
|
+
} else if (state.head) state.head.extra = null;
|
|
2902
|
+
state.mode = EXTRA;
|
|
2903
|
+
case EXTRA:
|
|
2904
|
+
if (state.flags & 1024) {
|
|
2905
|
+
copy = state.length;
|
|
2906
|
+
if (copy > have) copy = have;
|
|
2907
|
+
if (copy) {
|
|
2908
|
+
if (state.head) {
|
|
2909
|
+
len = state.head.extra_len - state.length;
|
|
2910
|
+
if (!state.head.extra) state.head.extra = new Uint8Array(state.head.extra_len);
|
|
2911
|
+
state.head.extra.set(input.subarray(next, next + copy), len);
|
|
2912
|
+
}
|
|
2913
|
+
if (state.flags & 512 && state.wrap & 4) state.check = crc32_1(state.check, input, copy, next);
|
|
2914
|
+
have -= copy;
|
|
2915
|
+
next += copy;
|
|
2916
|
+
state.length -= copy;
|
|
2917
|
+
}
|
|
2918
|
+
if (state.length) break inf_leave;
|
|
2919
|
+
}
|
|
2920
|
+
state.length = 0;
|
|
2921
|
+
state.mode = NAME;
|
|
2922
|
+
case NAME:
|
|
2923
|
+
if (state.flags & 2048) {
|
|
2924
|
+
if (have === 0) break inf_leave;
|
|
2925
|
+
copy = 0;
|
|
2926
|
+
do {
|
|
2927
|
+
len = input[next + copy++];
|
|
2928
|
+
if (state.head && len && state.length < 65536) state.head.name += String.fromCharCode(len);
|
|
2929
|
+
} while (len && copy < have);
|
|
2930
|
+
if (state.flags & 512 && state.wrap & 4) state.check = crc32_1(state.check, input, copy, next);
|
|
2931
|
+
have -= copy;
|
|
2932
|
+
next += copy;
|
|
2933
|
+
if (len) break inf_leave;
|
|
2934
|
+
} else if (state.head) state.head.name = null;
|
|
2935
|
+
state.length = 0;
|
|
2936
|
+
state.mode = COMMENT;
|
|
2937
|
+
case COMMENT:
|
|
2938
|
+
if (state.flags & 4096) {
|
|
2939
|
+
if (have === 0) break inf_leave;
|
|
2940
|
+
copy = 0;
|
|
2941
|
+
do {
|
|
2942
|
+
len = input[next + copy++];
|
|
2943
|
+
if (state.head && len && state.length < 65536) state.head.comment += String.fromCharCode(len);
|
|
2944
|
+
} while (len && copy < have);
|
|
2945
|
+
if (state.flags & 512 && state.wrap & 4) state.check = crc32_1(state.check, input, copy, next);
|
|
2946
|
+
have -= copy;
|
|
2947
|
+
next += copy;
|
|
2948
|
+
if (len) break inf_leave;
|
|
2949
|
+
} else if (state.head) state.head.comment = null;
|
|
2950
|
+
state.mode = HCRC;
|
|
2951
|
+
case HCRC:
|
|
2952
|
+
if (state.flags & 512) {
|
|
2953
|
+
while (bits < 16) {
|
|
2954
|
+
if (have === 0) break inf_leave;
|
|
2955
|
+
have--;
|
|
2956
|
+
hold += input[next++] << bits;
|
|
2957
|
+
bits += 8;
|
|
2958
|
+
}
|
|
2959
|
+
if (state.wrap & 4 && hold !== (state.check & 65535)) {
|
|
2960
|
+
strm.msg = "header crc mismatch";
|
|
2961
|
+
state.mode = BAD;
|
|
2962
|
+
break;
|
|
2963
|
+
}
|
|
2964
|
+
hold = 0;
|
|
2965
|
+
bits = 0;
|
|
2966
|
+
}
|
|
2967
|
+
if (state.head) {
|
|
2968
|
+
state.head.hcrc = state.flags >> 9 & 1;
|
|
2969
|
+
state.head.done = true;
|
|
2970
|
+
}
|
|
2971
|
+
strm.adler = state.check = 0;
|
|
2972
|
+
state.mode = TYPE;
|
|
2973
|
+
break;
|
|
2974
|
+
case DICTID:
|
|
2975
|
+
while (bits < 32) {
|
|
2976
|
+
if (have === 0) break inf_leave;
|
|
2977
|
+
have--;
|
|
2978
|
+
hold += input[next++] << bits;
|
|
2979
|
+
bits += 8;
|
|
2980
|
+
}
|
|
2981
|
+
strm.adler = state.check = zswap32(hold);
|
|
2982
|
+
hold = 0;
|
|
2983
|
+
bits = 0;
|
|
2984
|
+
state.mode = DICT;
|
|
2985
|
+
case DICT:
|
|
2986
|
+
if (state.havedict === 0) {
|
|
2987
|
+
strm.next_out = put;
|
|
2988
|
+
strm.avail_out = left;
|
|
2989
|
+
strm.next_in = next;
|
|
2990
|
+
strm.avail_in = have;
|
|
2991
|
+
state.hold = hold;
|
|
2992
|
+
state.bits = bits;
|
|
2993
|
+
return Z_NEED_DICT$1;
|
|
2994
|
+
}
|
|
2995
|
+
strm.adler = state.check = 1;
|
|
2996
|
+
state.mode = TYPE;
|
|
2997
|
+
case TYPE: if (flush === Z_BLOCK || flush === Z_TREES) break inf_leave;
|
|
2998
|
+
case TYPEDO:
|
|
2999
|
+
if (state.last) {
|
|
3000
|
+
hold >>>= bits & 7;
|
|
3001
|
+
bits -= bits & 7;
|
|
3002
|
+
state.mode = CHECK;
|
|
3003
|
+
break;
|
|
3004
|
+
}
|
|
3005
|
+
while (bits < 3) {
|
|
3006
|
+
if (have === 0) break inf_leave;
|
|
3007
|
+
have--;
|
|
3008
|
+
hold += input[next++] << bits;
|
|
3009
|
+
bits += 8;
|
|
3010
|
+
}
|
|
3011
|
+
state.last = hold & 1;
|
|
3012
|
+
hold >>>= 1;
|
|
3013
|
+
bits -= 1;
|
|
3014
|
+
switch (hold & 3) {
|
|
3015
|
+
case 0:
|
|
3016
|
+
state.mode = STORED;
|
|
3017
|
+
break;
|
|
3018
|
+
case 1:
|
|
3019
|
+
fixedtables(state);
|
|
3020
|
+
state.mode = LEN_;
|
|
3021
|
+
if (flush === Z_TREES) {
|
|
3022
|
+
hold >>>= 2;
|
|
3023
|
+
bits -= 2;
|
|
3024
|
+
break inf_leave;
|
|
3025
|
+
}
|
|
3026
|
+
break;
|
|
3027
|
+
case 2:
|
|
3028
|
+
state.mode = TABLE;
|
|
3029
|
+
break;
|
|
3030
|
+
case 3:
|
|
3031
|
+
strm.msg = "invalid block type";
|
|
3032
|
+
state.mode = BAD;
|
|
3033
|
+
}
|
|
3034
|
+
hold >>>= 2;
|
|
3035
|
+
bits -= 2;
|
|
3036
|
+
break;
|
|
3037
|
+
case STORED:
|
|
3038
|
+
hold >>>= bits & 7;
|
|
3039
|
+
bits -= bits & 7;
|
|
3040
|
+
while (bits < 32) {
|
|
3041
|
+
if (have === 0) break inf_leave;
|
|
3042
|
+
have--;
|
|
3043
|
+
hold += input[next++] << bits;
|
|
3044
|
+
bits += 8;
|
|
3045
|
+
}
|
|
3046
|
+
if ((hold & 65535) !== (hold >>> 16 ^ 65535)) {
|
|
3047
|
+
strm.msg = "invalid stored block lengths";
|
|
3048
|
+
state.mode = BAD;
|
|
3049
|
+
break;
|
|
3050
|
+
}
|
|
3051
|
+
state.length = hold & 65535;
|
|
3052
|
+
hold = 0;
|
|
3053
|
+
bits = 0;
|
|
3054
|
+
state.mode = COPY_;
|
|
3055
|
+
if (flush === Z_TREES) break inf_leave;
|
|
3056
|
+
case COPY_: state.mode = COPY;
|
|
3057
|
+
case COPY:
|
|
3058
|
+
copy = state.length;
|
|
3059
|
+
if (copy) {
|
|
3060
|
+
if (copy > have) copy = have;
|
|
3061
|
+
if (copy > left) copy = left;
|
|
3062
|
+
if (copy === 0) break inf_leave;
|
|
3063
|
+
output.set(input.subarray(next, next + copy), put);
|
|
3064
|
+
have -= copy;
|
|
3065
|
+
next += copy;
|
|
3066
|
+
left -= copy;
|
|
3067
|
+
put += copy;
|
|
3068
|
+
state.length -= copy;
|
|
3069
|
+
break;
|
|
3070
|
+
}
|
|
3071
|
+
state.mode = TYPE;
|
|
3072
|
+
break;
|
|
3073
|
+
case TABLE:
|
|
3074
|
+
while (bits < 14) {
|
|
3075
|
+
if (have === 0) break inf_leave;
|
|
3076
|
+
have--;
|
|
3077
|
+
hold += input[next++] << bits;
|
|
3078
|
+
bits += 8;
|
|
3079
|
+
}
|
|
3080
|
+
state.nlen = (hold & 31) + 257;
|
|
3081
|
+
hold >>>= 5;
|
|
3082
|
+
bits -= 5;
|
|
3083
|
+
state.ndist = (hold & 31) + 1;
|
|
3084
|
+
hold >>>= 5;
|
|
3085
|
+
bits -= 5;
|
|
3086
|
+
state.ncode = (hold & 15) + 4;
|
|
3087
|
+
hold >>>= 4;
|
|
3088
|
+
bits -= 4;
|
|
3089
|
+
if (state.nlen > 286 || state.ndist > 30) {
|
|
3090
|
+
strm.msg = "too many length or distance symbols";
|
|
3091
|
+
state.mode = BAD;
|
|
3092
|
+
break;
|
|
3093
|
+
}
|
|
3094
|
+
state.have = 0;
|
|
3095
|
+
state.mode = LENLENS;
|
|
3096
|
+
case LENLENS:
|
|
3097
|
+
while (state.have < state.ncode) {
|
|
3098
|
+
while (bits < 3) {
|
|
3099
|
+
if (have === 0) break inf_leave;
|
|
3100
|
+
have--;
|
|
3101
|
+
hold += input[next++] << bits;
|
|
3102
|
+
bits += 8;
|
|
3103
|
+
}
|
|
3104
|
+
state.lens[order[state.have++]] = hold & 7;
|
|
3105
|
+
hold >>>= 3;
|
|
3106
|
+
bits -= 3;
|
|
3107
|
+
}
|
|
3108
|
+
while (state.have < 19) state.lens[order[state.have++]] = 0;
|
|
3109
|
+
state.lencode = state.lendyn;
|
|
3110
|
+
state.lenbits = 7;
|
|
3111
|
+
opts = { bits: state.lenbits };
|
|
3112
|
+
ret = inftrees(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
|
|
3113
|
+
state.lenbits = opts.bits;
|
|
3114
|
+
if (ret) {
|
|
3115
|
+
strm.msg = "invalid code lengths set";
|
|
3116
|
+
state.mode = BAD;
|
|
3117
|
+
break;
|
|
3118
|
+
}
|
|
3119
|
+
state.have = 0;
|
|
3120
|
+
state.mode = CODELENS;
|
|
3121
|
+
case CODELENS:
|
|
3122
|
+
while (state.have < state.nlen + state.ndist) {
|
|
3123
|
+
for (;;) {
|
|
3124
|
+
here = state.lencode[hold & (1 << state.lenbits) - 1];
|
|
3125
|
+
here_bits = here >>> 24;
|
|
3126
|
+
here_op = here >>> 16 & 255;
|
|
3127
|
+
here_val = here & 65535;
|
|
3128
|
+
if (here_bits <= bits) break;
|
|
3129
|
+
if (have === 0) break inf_leave;
|
|
3130
|
+
have--;
|
|
3131
|
+
hold += input[next++] << bits;
|
|
3132
|
+
bits += 8;
|
|
3133
|
+
}
|
|
3134
|
+
if (here_val < 16) {
|
|
3135
|
+
hold >>>= here_bits;
|
|
3136
|
+
bits -= here_bits;
|
|
3137
|
+
state.lens[state.have++] = here_val;
|
|
3138
|
+
} else {
|
|
3139
|
+
if (here_val === 16) {
|
|
3140
|
+
n = here_bits + 2;
|
|
3141
|
+
while (bits < n) {
|
|
3142
|
+
if (have === 0) break inf_leave;
|
|
3143
|
+
have--;
|
|
3144
|
+
hold += input[next++] << bits;
|
|
3145
|
+
bits += 8;
|
|
3146
|
+
}
|
|
3147
|
+
hold >>>= here_bits;
|
|
3148
|
+
bits -= here_bits;
|
|
3149
|
+
if (state.have === 0) {
|
|
3150
|
+
strm.msg = "invalid bit length repeat";
|
|
3151
|
+
state.mode = BAD;
|
|
3152
|
+
break;
|
|
3153
|
+
}
|
|
3154
|
+
len = state.lens[state.have - 1];
|
|
3155
|
+
copy = 3 + (hold & 3);
|
|
3156
|
+
hold >>>= 2;
|
|
3157
|
+
bits -= 2;
|
|
3158
|
+
} else if (here_val === 17) {
|
|
3159
|
+
n = here_bits + 3;
|
|
3160
|
+
while (bits < n) {
|
|
3161
|
+
if (have === 0) break inf_leave;
|
|
3162
|
+
have--;
|
|
3163
|
+
hold += input[next++] << bits;
|
|
3164
|
+
bits += 8;
|
|
3165
|
+
}
|
|
3166
|
+
hold >>>= here_bits;
|
|
3167
|
+
bits -= here_bits;
|
|
3168
|
+
len = 0;
|
|
3169
|
+
copy = 3 + (hold & 7);
|
|
3170
|
+
hold >>>= 3;
|
|
3171
|
+
bits -= 3;
|
|
3172
|
+
} else {
|
|
3173
|
+
n = here_bits + 7;
|
|
3174
|
+
while (bits < n) {
|
|
3175
|
+
if (have === 0) break inf_leave;
|
|
3176
|
+
have--;
|
|
3177
|
+
hold += input[next++] << bits;
|
|
3178
|
+
bits += 8;
|
|
3179
|
+
}
|
|
3180
|
+
hold >>>= here_bits;
|
|
3181
|
+
bits -= here_bits;
|
|
3182
|
+
len = 0;
|
|
3183
|
+
copy = 11 + (hold & 127);
|
|
3184
|
+
hold >>>= 7;
|
|
3185
|
+
bits -= 7;
|
|
3186
|
+
}
|
|
3187
|
+
if (state.have + copy > state.nlen + state.ndist) {
|
|
3188
|
+
strm.msg = "invalid bit length repeat";
|
|
3189
|
+
state.mode = BAD;
|
|
3190
|
+
break;
|
|
3191
|
+
}
|
|
3192
|
+
while (copy--) state.lens[state.have++] = len;
|
|
3193
|
+
}
|
|
3194
|
+
}
|
|
3195
|
+
if (state.mode === BAD) break;
|
|
3196
|
+
if (state.lens[256] === 0) {
|
|
3197
|
+
strm.msg = "invalid code -- missing end-of-block";
|
|
3198
|
+
state.mode = BAD;
|
|
3199
|
+
break;
|
|
3200
|
+
}
|
|
3201
|
+
state.lenbits = 9;
|
|
3202
|
+
opts = { bits: state.lenbits };
|
|
3203
|
+
ret = inftrees(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
|
|
3204
|
+
state.lenbits = opts.bits;
|
|
3205
|
+
if (ret) {
|
|
3206
|
+
strm.msg = "invalid literal/lengths set";
|
|
3207
|
+
state.mode = BAD;
|
|
3208
|
+
break;
|
|
3209
|
+
}
|
|
3210
|
+
state.distbits = 6;
|
|
3211
|
+
state.distcode = state.distdyn;
|
|
3212
|
+
opts = { bits: state.distbits };
|
|
3213
|
+
ret = inftrees(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
|
|
3214
|
+
state.distbits = opts.bits;
|
|
3215
|
+
if (ret) {
|
|
3216
|
+
strm.msg = "invalid distances set";
|
|
3217
|
+
state.mode = BAD;
|
|
3218
|
+
break;
|
|
3219
|
+
}
|
|
3220
|
+
state.mode = LEN_;
|
|
3221
|
+
if (flush === Z_TREES) break inf_leave;
|
|
3222
|
+
case LEN_: state.mode = LEN;
|
|
3223
|
+
case LEN:
|
|
3224
|
+
if (have >= 6 && left >= 258) {
|
|
3225
|
+
strm.next_out = put;
|
|
3226
|
+
strm.avail_out = left;
|
|
3227
|
+
strm.next_in = next;
|
|
3228
|
+
strm.avail_in = have;
|
|
3229
|
+
state.hold = hold;
|
|
3230
|
+
state.bits = bits;
|
|
3231
|
+
inffast(strm, _out);
|
|
3232
|
+
put = strm.next_out;
|
|
3233
|
+
output = strm.output;
|
|
3234
|
+
left = strm.avail_out;
|
|
3235
|
+
next = strm.next_in;
|
|
3236
|
+
input = strm.input;
|
|
3237
|
+
have = strm.avail_in;
|
|
3238
|
+
hold = state.hold;
|
|
3239
|
+
bits = state.bits;
|
|
3240
|
+
if (state.mode === TYPE) state.back = -1;
|
|
3241
|
+
break;
|
|
3242
|
+
}
|
|
3243
|
+
state.back = 0;
|
|
3244
|
+
for (;;) {
|
|
3245
|
+
here = state.lencode[hold & (1 << state.lenbits) - 1];
|
|
3246
|
+
here_bits = here >>> 24;
|
|
3247
|
+
here_op = here >>> 16 & 255;
|
|
3248
|
+
here_val = here & 65535;
|
|
3249
|
+
if (here_bits <= bits) break;
|
|
3250
|
+
if (have === 0) break inf_leave;
|
|
3251
|
+
have--;
|
|
3252
|
+
hold += input[next++] << bits;
|
|
3253
|
+
bits += 8;
|
|
3254
|
+
}
|
|
3255
|
+
if (here_op && (here_op & 240) === 0) {
|
|
3256
|
+
last_bits = here_bits;
|
|
3257
|
+
last_op = here_op;
|
|
3258
|
+
last_val = here_val;
|
|
3259
|
+
for (;;) {
|
|
3260
|
+
here = state.lencode[last_val + ((hold & (1 << last_bits + last_op) - 1) >> last_bits)];
|
|
3261
|
+
here_bits = here >>> 24;
|
|
3262
|
+
here_op = here >>> 16 & 255;
|
|
3263
|
+
here_val = here & 65535;
|
|
3264
|
+
if (last_bits + here_bits <= bits) break;
|
|
3265
|
+
if (have === 0) break inf_leave;
|
|
3266
|
+
have--;
|
|
3267
|
+
hold += input[next++] << bits;
|
|
3268
|
+
bits += 8;
|
|
3269
|
+
}
|
|
3270
|
+
hold >>>= last_bits;
|
|
3271
|
+
bits -= last_bits;
|
|
3272
|
+
state.back += last_bits;
|
|
3273
|
+
}
|
|
3274
|
+
hold >>>= here_bits;
|
|
3275
|
+
bits -= here_bits;
|
|
3276
|
+
state.back += here_bits;
|
|
3277
|
+
state.length = here_val;
|
|
3278
|
+
if (here_op === 0) {
|
|
3279
|
+
state.mode = LIT;
|
|
3280
|
+
break;
|
|
3281
|
+
}
|
|
3282
|
+
if (here_op & 32) {
|
|
3283
|
+
state.back = -1;
|
|
3284
|
+
state.mode = TYPE;
|
|
3285
|
+
break;
|
|
3286
|
+
}
|
|
3287
|
+
if (here_op & 64) {
|
|
3288
|
+
strm.msg = "invalid literal/length code";
|
|
3289
|
+
state.mode = BAD;
|
|
3290
|
+
break;
|
|
3291
|
+
}
|
|
3292
|
+
state.extra = here_op & 15;
|
|
3293
|
+
state.mode = LENEXT;
|
|
3294
|
+
case LENEXT:
|
|
3295
|
+
if (state.extra) {
|
|
3296
|
+
n = state.extra;
|
|
3297
|
+
while (bits < n) {
|
|
3298
|
+
if (have === 0) break inf_leave;
|
|
3299
|
+
have--;
|
|
3300
|
+
hold += input[next++] << bits;
|
|
3301
|
+
bits += 8;
|
|
3302
|
+
}
|
|
3303
|
+
state.length += hold & (1 << state.extra) - 1;
|
|
3304
|
+
hold >>>= state.extra;
|
|
3305
|
+
bits -= state.extra;
|
|
3306
|
+
state.back += state.extra;
|
|
3307
|
+
}
|
|
3308
|
+
state.was = state.length;
|
|
3309
|
+
state.mode = DIST;
|
|
3310
|
+
case DIST:
|
|
3311
|
+
for (;;) {
|
|
3312
|
+
here = state.distcode[hold & (1 << state.distbits) - 1];
|
|
3313
|
+
here_bits = here >>> 24;
|
|
3314
|
+
here_op = here >>> 16 & 255;
|
|
3315
|
+
here_val = here & 65535;
|
|
3316
|
+
if (here_bits <= bits) break;
|
|
3317
|
+
if (have === 0) break inf_leave;
|
|
3318
|
+
have--;
|
|
3319
|
+
hold += input[next++] << bits;
|
|
3320
|
+
bits += 8;
|
|
3321
|
+
}
|
|
3322
|
+
if ((here_op & 240) === 0) {
|
|
3323
|
+
last_bits = here_bits;
|
|
3324
|
+
last_op = here_op;
|
|
3325
|
+
last_val = here_val;
|
|
3326
|
+
for (;;) {
|
|
3327
|
+
here = state.distcode[last_val + ((hold & (1 << last_bits + last_op) - 1) >> last_bits)];
|
|
3328
|
+
here_bits = here >>> 24;
|
|
3329
|
+
here_op = here >>> 16 & 255;
|
|
3330
|
+
here_val = here & 65535;
|
|
3331
|
+
if (last_bits + here_bits <= bits) break;
|
|
3332
|
+
if (have === 0) break inf_leave;
|
|
3333
|
+
have--;
|
|
3334
|
+
hold += input[next++] << bits;
|
|
3335
|
+
bits += 8;
|
|
3336
|
+
}
|
|
3337
|
+
hold >>>= last_bits;
|
|
3338
|
+
bits -= last_bits;
|
|
3339
|
+
state.back += last_bits;
|
|
3340
|
+
}
|
|
3341
|
+
hold >>>= here_bits;
|
|
3342
|
+
bits -= here_bits;
|
|
3343
|
+
state.back += here_bits;
|
|
3344
|
+
if (here_op & 64) {
|
|
3345
|
+
strm.msg = "invalid distance code";
|
|
3346
|
+
state.mode = BAD;
|
|
3347
|
+
break;
|
|
3348
|
+
}
|
|
3349
|
+
state.offset = here_val;
|
|
3350
|
+
state.extra = here_op & 15;
|
|
3351
|
+
state.mode = DISTEXT;
|
|
3352
|
+
case DISTEXT:
|
|
3353
|
+
if (state.extra) {
|
|
3354
|
+
n = state.extra;
|
|
3355
|
+
while (bits < n) {
|
|
3356
|
+
if (have === 0) break inf_leave;
|
|
3357
|
+
have--;
|
|
3358
|
+
hold += input[next++] << bits;
|
|
3359
|
+
bits += 8;
|
|
3360
|
+
}
|
|
3361
|
+
state.offset += hold & (1 << state.extra) - 1;
|
|
3362
|
+
hold >>>= state.extra;
|
|
3363
|
+
bits -= state.extra;
|
|
3364
|
+
state.back += state.extra;
|
|
3365
|
+
}
|
|
3366
|
+
if (state.offset > state.dmax) {
|
|
3367
|
+
strm.msg = "invalid distance too far back";
|
|
3368
|
+
state.mode = BAD;
|
|
3369
|
+
break;
|
|
3370
|
+
}
|
|
3371
|
+
state.mode = MATCH;
|
|
3372
|
+
case MATCH:
|
|
3373
|
+
if (left === 0) break inf_leave;
|
|
3374
|
+
copy = _out - left;
|
|
3375
|
+
if (state.offset > copy) {
|
|
3376
|
+
copy = state.offset - copy;
|
|
3377
|
+
if (copy > state.whave) {
|
|
3378
|
+
if (state.sane) {
|
|
3379
|
+
strm.msg = "invalid distance too far back";
|
|
3380
|
+
state.mode = BAD;
|
|
3381
|
+
break;
|
|
3382
|
+
}
|
|
3383
|
+
}
|
|
3384
|
+
if (copy > state.wnext) {
|
|
3385
|
+
copy -= state.wnext;
|
|
3386
|
+
from = state.wsize - copy;
|
|
3387
|
+
} else from = state.wnext - copy;
|
|
3388
|
+
if (copy > state.length) copy = state.length;
|
|
3389
|
+
from_source = state.window;
|
|
3390
|
+
} else {
|
|
3391
|
+
from_source = output;
|
|
3392
|
+
from = put - state.offset;
|
|
3393
|
+
copy = state.length;
|
|
3394
|
+
}
|
|
3395
|
+
if (copy > left) copy = left;
|
|
3396
|
+
left -= copy;
|
|
3397
|
+
state.length -= copy;
|
|
3398
|
+
do
|
|
3399
|
+
output[put++] = from_source[from++];
|
|
3400
|
+
while (--copy);
|
|
3401
|
+
if (state.length === 0) state.mode = LEN;
|
|
3402
|
+
break;
|
|
3403
|
+
case LIT:
|
|
3404
|
+
if (left === 0) break inf_leave;
|
|
3405
|
+
output[put++] = state.length;
|
|
3406
|
+
left--;
|
|
3407
|
+
state.mode = LEN;
|
|
3408
|
+
break;
|
|
3409
|
+
case CHECK:
|
|
3410
|
+
if (state.wrap) {
|
|
3411
|
+
while (bits < 32) {
|
|
3412
|
+
if (have === 0) break inf_leave;
|
|
3413
|
+
have--;
|
|
3414
|
+
hold |= input[next++] << bits;
|
|
3415
|
+
bits += 8;
|
|
3416
|
+
}
|
|
3417
|
+
_out -= left;
|
|
3418
|
+
strm.total_out += _out;
|
|
3419
|
+
state.total += _out;
|
|
3420
|
+
if (state.wrap & 4 && _out) strm.adler = state.check = state.flags ? crc32_1(state.check, output, _out, put - _out) : adler32_1(state.check, output, _out, put - _out);
|
|
3421
|
+
_out = left;
|
|
3422
|
+
if (state.wrap & 4 && (state.flags ? hold : zswap32(hold)) !== state.check) {
|
|
3423
|
+
strm.msg = "incorrect data check";
|
|
3424
|
+
state.mode = BAD;
|
|
3425
|
+
break;
|
|
3426
|
+
}
|
|
3427
|
+
hold = 0;
|
|
3428
|
+
bits = 0;
|
|
3429
|
+
}
|
|
3430
|
+
state.mode = LENGTH;
|
|
3431
|
+
case LENGTH:
|
|
3432
|
+
if (state.wrap && state.flags) {
|
|
3433
|
+
while (bits < 32) {
|
|
3434
|
+
if (have === 0) break inf_leave;
|
|
3435
|
+
have--;
|
|
3436
|
+
hold += input[next++] << bits;
|
|
3437
|
+
bits += 8;
|
|
3438
|
+
}
|
|
3439
|
+
if (state.wrap & 4 && hold !== (state.total & 4294967295)) {
|
|
3440
|
+
strm.msg = "incorrect length check";
|
|
3441
|
+
state.mode = BAD;
|
|
3442
|
+
break;
|
|
3443
|
+
}
|
|
3444
|
+
hold = 0;
|
|
3445
|
+
bits = 0;
|
|
3446
|
+
}
|
|
3447
|
+
state.mode = DONE;
|
|
3448
|
+
case DONE:
|
|
3449
|
+
ret = Z_STREAM_END$1;
|
|
3450
|
+
break inf_leave;
|
|
3451
|
+
case BAD:
|
|
3452
|
+
ret = Z_DATA_ERROR$1;
|
|
3453
|
+
break inf_leave;
|
|
3454
|
+
case MEM: return Z_MEM_ERROR$1;
|
|
3455
|
+
case SYNC:
|
|
3456
|
+
default: return Z_STREAM_ERROR$1;
|
|
3457
|
+
}
|
|
3458
|
+
strm.next_out = put;
|
|
3459
|
+
strm.avail_out = left;
|
|
3460
|
+
strm.next_in = next;
|
|
3461
|
+
strm.avail_in = have;
|
|
3462
|
+
state.hold = hold;
|
|
3463
|
+
state.bits = bits;
|
|
3464
|
+
if (state.wsize || _out !== strm.avail_out && state.mode < BAD && (state.mode < CHECK || flush !== Z_FINISH$1)) {
|
|
3465
|
+
if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out));
|
|
3466
|
+
}
|
|
3467
|
+
_in -= strm.avail_in;
|
|
3468
|
+
_out -= strm.avail_out;
|
|
3469
|
+
strm.total_in += _in;
|
|
3470
|
+
strm.total_out += _out;
|
|
3471
|
+
state.total += _out;
|
|
3472
|
+
if (state.wrap & 4 && _out) strm.adler = state.check = state.flags ? crc32_1(state.check, output, _out, strm.next_out - _out) : adler32_1(state.check, output, _out, strm.next_out - _out);
|
|
3473
|
+
strm.data_type = state.bits + (state.last ? 64 : 0) + (state.mode === TYPE ? 128 : 0) + (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
|
|
3474
|
+
if ((_in === 0 && _out === 0 || flush === Z_FINISH$1) && ret === Z_OK$1) ret = Z_BUF_ERROR;
|
|
3475
|
+
return ret;
|
|
3476
|
+
};
|
|
3477
|
+
const inflateEnd = (strm) => {
|
|
3478
|
+
if (inflateStateCheck(strm)) return Z_STREAM_ERROR$1;
|
|
3479
|
+
let state = strm.state;
|
|
3480
|
+
if (state.window) state.window = null;
|
|
3481
|
+
strm.state = null;
|
|
3482
|
+
return Z_OK$1;
|
|
3483
|
+
};
|
|
3484
|
+
const inflateGetHeader = (strm, head) => {
|
|
3485
|
+
if (inflateStateCheck(strm)) return Z_STREAM_ERROR$1;
|
|
3486
|
+
const state = strm.state;
|
|
3487
|
+
if ((state.wrap & 2) === 0) return Z_STREAM_ERROR$1;
|
|
3488
|
+
state.head = head;
|
|
3489
|
+
head.done = false;
|
|
3490
|
+
return Z_OK$1;
|
|
3491
|
+
};
|
|
3492
|
+
const inflateSetDictionary = (strm, dictionary) => {
|
|
3493
|
+
const dictLength = dictionary.length;
|
|
3494
|
+
let state;
|
|
3495
|
+
let dictid;
|
|
3496
|
+
let ret;
|
|
3497
|
+
if (inflateStateCheck(strm)) return Z_STREAM_ERROR$1;
|
|
3498
|
+
state = strm.state;
|
|
3499
|
+
if (state.wrap !== 0 && state.mode !== DICT) return Z_STREAM_ERROR$1;
|
|
3500
|
+
if (state.mode === DICT) {
|
|
3501
|
+
dictid = 1;
|
|
3502
|
+
dictid = adler32_1(dictid, dictionary, dictLength, 0);
|
|
3503
|
+
if (dictid !== state.check) return Z_DATA_ERROR$1;
|
|
3504
|
+
}
|
|
3505
|
+
ret = updatewindow(strm, dictionary, dictLength, dictLength);
|
|
3506
|
+
if (ret) {
|
|
3507
|
+
state.mode = MEM;
|
|
3508
|
+
return Z_MEM_ERROR$1;
|
|
3509
|
+
}
|
|
3510
|
+
state.havedict = 1;
|
|
3511
|
+
return Z_OK$1;
|
|
3512
|
+
};
|
|
3513
|
+
var inflate_1$2 = {
|
|
3514
|
+
inflateReset,
|
|
3515
|
+
inflateReset2,
|
|
3516
|
+
inflateResetKeep,
|
|
3517
|
+
inflateInit,
|
|
3518
|
+
inflateInit2,
|
|
3519
|
+
inflate: inflate$2,
|
|
3520
|
+
inflateEnd,
|
|
3521
|
+
inflateGetHeader,
|
|
3522
|
+
inflateSetDictionary,
|
|
3523
|
+
inflateInfo: "pako inflate (from Nodeca project)"
|
|
3524
|
+
};
|
|
3525
|
+
function GZheader() {
|
|
3526
|
+
this.text = 0;
|
|
3527
|
+
this.time = 0;
|
|
3528
|
+
this.xflags = 0;
|
|
3529
|
+
this.os = 0;
|
|
3530
|
+
this.extra = null;
|
|
3531
|
+
this.extra_len = 0;
|
|
3532
|
+
this.name = "";
|
|
3533
|
+
this.comment = "";
|
|
3534
|
+
this.hcrc = 0;
|
|
3535
|
+
this.done = false;
|
|
3536
|
+
}
|
|
3537
|
+
var gzheader = GZheader;
|
|
3538
|
+
const toString = Object.prototype.toString;
|
|
3539
|
+
const { Z_NO_FLUSH, Z_FINISH, Z_OK, Z_STREAM_END, Z_NEED_DICT, Z_STREAM_ERROR, Z_DATA_ERROR, Z_MEM_ERROR } = constants$2;
|
|
3540
|
+
/**
|
|
3541
|
+
* class Inflate
|
|
3542
|
+
*
|
|
3543
|
+
* Generic JS-style wrapper for zlib calls. If you don't need
|
|
3544
|
+
* streaming behaviour - use more simple functions: [[inflate]]
|
|
3545
|
+
* and [[inflateRaw]].
|
|
3546
|
+
**/
|
|
3547
|
+
/**
|
|
3548
|
+
* Inflate.result -> Uint8Array|String
|
|
3549
|
+
*
|
|
3550
|
+
* Uncompressed result, generated by default [[Inflate#onData]]
|
|
3551
|
+
* and [[Inflate#onEnd]] handlers. Filled after you push last chunk
|
|
3552
|
+
* (call [[Inflate#push]] with `Z_FINISH` / `true` param).
|
|
3553
|
+
**/
|
|
3554
|
+
/**
|
|
3555
|
+
* Inflate.err -> Number
|
|
3556
|
+
*
|
|
3557
|
+
* Error code after inflate finished. 0 (Z_OK) on success.
|
|
3558
|
+
* Should be checked if broken data possible.
|
|
3559
|
+
**/
|
|
3560
|
+
/**
|
|
3561
|
+
* Inflate.msg -> String
|
|
3562
|
+
*
|
|
3563
|
+
* Error message, if [[Inflate.err]] != 0
|
|
3564
|
+
**/
|
|
3565
|
+
/**
|
|
3566
|
+
* new Inflate(options)
|
|
3567
|
+
* - options (Object): zlib inflate options.
|
|
3568
|
+
*
|
|
3569
|
+
* Creates new inflator instance with specified params. Throws exception
|
|
3570
|
+
* on bad params. Supported options:
|
|
3571
|
+
*
|
|
3572
|
+
* - `windowBits`
|
|
3573
|
+
* - `dictionary`
|
|
3574
|
+
*
|
|
3575
|
+
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
|
|
3576
|
+
* for more information on these.
|
|
3577
|
+
*
|
|
3578
|
+
* Additional options, for internal needs:
|
|
3579
|
+
*
|
|
3580
|
+
* - `chunkSize` - size of generated data chunks (16K by default)
|
|
3581
|
+
* - `raw` (Boolean) - do raw inflate
|
|
3582
|
+
* - `to` (String) - if equal to 'string', then result will be converted
|
|
3583
|
+
* from utf8 to utf16 (javascript) string. When string output requested,
|
|
3584
|
+
* chunk length can differ from `chunkSize`, depending on content.
|
|
3585
|
+
*
|
|
3586
|
+
* By default, when no options set, autodetect deflate/gzip data format via
|
|
3587
|
+
* wrapper header.
|
|
3588
|
+
*
|
|
3589
|
+
* ##### Example:
|
|
3590
|
+
*
|
|
3591
|
+
* ```javascript
|
|
3592
|
+
* const pako = require('pako')
|
|
3593
|
+
* const chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
|
|
3594
|
+
* const chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
|
|
3595
|
+
*
|
|
3596
|
+
* const inflate = new pako.Inflate({ level: 3});
|
|
3597
|
+
*
|
|
3598
|
+
* inflate.push(chunk1, false);
|
|
3599
|
+
* inflate.push(chunk2, true); // true -> last chunk
|
|
3600
|
+
*
|
|
3601
|
+
* if (inflate.err) { throw new Error(inflate.err); }
|
|
3602
|
+
*
|
|
3603
|
+
* console.log(inflate.result);
|
|
3604
|
+
* ```
|
|
3605
|
+
**/
|
|
3606
|
+
function Inflate$1(options) {
|
|
3607
|
+
this.options = common.assign({
|
|
3608
|
+
chunkSize: 1024 * 64,
|
|
3609
|
+
windowBits: 15,
|
|
3610
|
+
to: ""
|
|
3611
|
+
}, options || {});
|
|
3612
|
+
const opt = this.options;
|
|
3613
|
+
if (opt.raw && opt.windowBits >= 0 && opt.windowBits < 16) {
|
|
3614
|
+
opt.windowBits = -opt.windowBits;
|
|
3615
|
+
if (opt.windowBits === 0) opt.windowBits = -15;
|
|
3616
|
+
}
|
|
3617
|
+
if (opt.windowBits >= 0 && opt.windowBits < 16 && !(options && options.windowBits)) opt.windowBits += 32;
|
|
3618
|
+
if (opt.windowBits > 15 && opt.windowBits < 48) {
|
|
3619
|
+
if ((opt.windowBits & 15) === 0) opt.windowBits |= 15;
|
|
3620
|
+
}
|
|
3621
|
+
this.err = 0;
|
|
3622
|
+
this.msg = "";
|
|
3623
|
+
this.ended = false;
|
|
3624
|
+
this.chunks = [];
|
|
3625
|
+
this.strm = new zstream();
|
|
3626
|
+
this.strm.avail_out = 0;
|
|
3627
|
+
let status = inflate_1$2.inflateInit2(this.strm, opt.windowBits);
|
|
3628
|
+
if (status !== Z_OK) throw new Error(messages[status]);
|
|
3629
|
+
this.header = new gzheader();
|
|
3630
|
+
inflate_1$2.inflateGetHeader(this.strm, this.header);
|
|
3631
|
+
if (opt.dictionary) {
|
|
3632
|
+
if (typeof opt.dictionary === "string") opt.dictionary = strings.string2buf(opt.dictionary);
|
|
3633
|
+
else if (toString.call(opt.dictionary) === "[object ArrayBuffer]") opt.dictionary = new Uint8Array(opt.dictionary);
|
|
3634
|
+
if (opt.raw) {
|
|
3635
|
+
status = inflate_1$2.inflateSetDictionary(this.strm, opt.dictionary);
|
|
3636
|
+
if (status !== Z_OK) throw new Error(messages[status]);
|
|
3637
|
+
}
|
|
3638
|
+
}
|
|
3639
|
+
}
|
|
3640
|
+
/**
|
|
3641
|
+
* Inflate#push(data[, flush_mode]) -> Boolean
|
|
3642
|
+
* - data (Uint8Array|ArrayBuffer): input data
|
|
3643
|
+
* - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE
|
|
3644
|
+
* flush modes. See constants. Skipped or `false` means Z_NO_FLUSH,
|
|
3645
|
+
* `true` means Z_FINISH.
|
|
3646
|
+
*
|
|
3647
|
+
* Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
|
|
3648
|
+
* new output chunks. Returns `true` on success. If end of stream detected,
|
|
3649
|
+
* [[Inflate#onEnd]] will be called.
|
|
3650
|
+
*
|
|
3651
|
+
* `flush_mode` is not needed for normal operation, because end of stream
|
|
3652
|
+
* detected automatically. You may try to use it for advanced things, but
|
|
3653
|
+
* this functionality was not tested.
|
|
3654
|
+
*
|
|
3655
|
+
* On fail call [[Inflate#onEnd]] with error code and return false.
|
|
3656
|
+
*
|
|
3657
|
+
* ##### Example
|
|
3658
|
+
*
|
|
3659
|
+
* ```javascript
|
|
3660
|
+
* push(chunk, false); // push one of data chunks
|
|
3661
|
+
* ...
|
|
3662
|
+
* push(chunk, true); // push last chunk
|
|
3663
|
+
* ```
|
|
3664
|
+
**/
|
|
3665
|
+
Inflate$1.prototype.push = function(data, flush_mode) {
|
|
3666
|
+
const strm = this.strm;
|
|
3667
|
+
const chunkSize = this.options.chunkSize;
|
|
3668
|
+
const dictionary = this.options.dictionary;
|
|
3669
|
+
let status, _flush_mode, last_avail_out;
|
|
3670
|
+
if (this.ended) return false;
|
|
3671
|
+
if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;
|
|
3672
|
+
else _flush_mode = flush_mode === true ? Z_FINISH : Z_NO_FLUSH;
|
|
3673
|
+
if (toString.call(data) === "[object ArrayBuffer]") strm.input = new Uint8Array(data);
|
|
3674
|
+
else strm.input = data;
|
|
3675
|
+
strm.next_in = 0;
|
|
3676
|
+
strm.avail_in = strm.input.length;
|
|
3677
|
+
for (;;) {
|
|
3678
|
+
if (strm.avail_out === 0) {
|
|
3679
|
+
strm.output = new Uint8Array(chunkSize);
|
|
3680
|
+
strm.next_out = 0;
|
|
3681
|
+
strm.avail_out = chunkSize;
|
|
3682
|
+
}
|
|
3683
|
+
status = inflate_1$2.inflate(strm, _flush_mode);
|
|
3684
|
+
if (status === Z_NEED_DICT && dictionary) {
|
|
3685
|
+
status = inflate_1$2.inflateSetDictionary(strm, dictionary);
|
|
3686
|
+
if (status === Z_OK) status = inflate_1$2.inflate(strm, _flush_mode);
|
|
3687
|
+
else if (status === Z_DATA_ERROR) status = Z_NEED_DICT;
|
|
3688
|
+
}
|
|
3689
|
+
while (strm.avail_in > 0 && status === Z_STREAM_END && strm.state.wrap > 0 && data[strm.next_in] !== 0) {
|
|
3690
|
+
inflate_1$2.inflateReset(strm);
|
|
3691
|
+
status = inflate_1$2.inflate(strm, _flush_mode);
|
|
3692
|
+
}
|
|
3693
|
+
switch (status) {
|
|
3694
|
+
case Z_STREAM_ERROR:
|
|
3695
|
+
case Z_DATA_ERROR:
|
|
3696
|
+
case Z_NEED_DICT:
|
|
3697
|
+
case Z_MEM_ERROR:
|
|
3698
|
+
this.onEnd(status);
|
|
3699
|
+
this.ended = true;
|
|
3700
|
+
return false;
|
|
3701
|
+
}
|
|
3702
|
+
last_avail_out = strm.avail_out;
|
|
3703
|
+
if (strm.next_out) {
|
|
3704
|
+
if (strm.avail_out === 0 || status === Z_STREAM_END) if (this.options.to === "string") {
|
|
3705
|
+
let next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
|
|
3706
|
+
let tail = strm.next_out - next_out_utf8;
|
|
3707
|
+
let utf8str = strings.buf2string(strm.output, next_out_utf8);
|
|
3708
|
+
strm.next_out = tail;
|
|
3709
|
+
strm.avail_out = chunkSize - tail;
|
|
3710
|
+
if (tail) strm.output.set(strm.output.subarray(next_out_utf8, next_out_utf8 + tail), 0);
|
|
3711
|
+
this.onData(utf8str);
|
|
3712
|
+
} else this.onData(strm.output.length === strm.next_out ? strm.output : strm.output.subarray(0, strm.next_out));
|
|
3713
|
+
}
|
|
3714
|
+
if (status === Z_OK && last_avail_out === 0) continue;
|
|
3715
|
+
if (status === Z_STREAM_END) {
|
|
3716
|
+
status = inflate_1$2.inflateEnd(this.strm);
|
|
3717
|
+
this.onEnd(status);
|
|
3718
|
+
this.ended = true;
|
|
3719
|
+
return true;
|
|
3720
|
+
}
|
|
3721
|
+
if (strm.avail_in === 0) break;
|
|
3722
|
+
}
|
|
3723
|
+
return true;
|
|
3724
|
+
};
|
|
3725
|
+
/**
|
|
3726
|
+
* Inflate#onData(chunk) -> Void
|
|
3727
|
+
* - chunk (Uint8Array|String): output data. When string output requested,
|
|
3728
|
+
* each chunk will be string.
|
|
3729
|
+
*
|
|
3730
|
+
* By default, stores data blocks in `chunks[]` property and glue
|
|
3731
|
+
* those in `onEnd`. Override this handler, if you need another behaviour.
|
|
3732
|
+
**/
|
|
3733
|
+
Inflate$1.prototype.onData = function(chunk) {
|
|
3734
|
+
this.chunks.push(chunk);
|
|
3735
|
+
};
|
|
3736
|
+
/**
|
|
3737
|
+
* Inflate#onEnd(status) -> Void
|
|
3738
|
+
* - status (Number): inflate status. 0 (Z_OK) on success,
|
|
3739
|
+
* other if not.
|
|
3740
|
+
*
|
|
3741
|
+
* Called either after you tell inflate that the input stream is
|
|
3742
|
+
* complete (Z_FINISH). By default - join collected chunks,
|
|
3743
|
+
* free memory and fill `results` / `err` properties.
|
|
3744
|
+
**/
|
|
3745
|
+
Inflate$1.prototype.onEnd = function(status) {
|
|
3746
|
+
if (status === Z_OK) if (this.options.to === "string") this.result = this.chunks.join("");
|
|
3747
|
+
else this.result = common.flattenChunks(this.chunks);
|
|
3748
|
+
this.chunks = [];
|
|
3749
|
+
this.err = status;
|
|
3750
|
+
this.msg = this.strm.msg;
|
|
3751
|
+
};
|
|
3752
|
+
/**
|
|
3753
|
+
* inflate(data[, options]) -> Uint8Array|String
|
|
3754
|
+
* - data (Uint8Array|ArrayBuffer): input data to decompress.
|
|
3755
|
+
* - options (Object): zlib inflate options.
|
|
3756
|
+
*
|
|
3757
|
+
* Decompress `data` with inflate/ungzip and `options`. Autodetect
|
|
3758
|
+
* format via wrapper header by default. That's why we don't provide
|
|
3759
|
+
* separate `ungzip` method.
|
|
3760
|
+
*
|
|
3761
|
+
* Supported options are:
|
|
3762
|
+
*
|
|
3763
|
+
* - windowBits
|
|
3764
|
+
*
|
|
3765
|
+
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
|
|
3766
|
+
* for more information.
|
|
3767
|
+
*
|
|
3768
|
+
* Sugar (options):
|
|
3769
|
+
*
|
|
3770
|
+
* - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
|
|
3771
|
+
* negative windowBits implicitly.
|
|
3772
|
+
* - `to` (String) - if equal to 'string', then result will be converted
|
|
3773
|
+
* from utf8 to utf16 (javascript) string. When string output requested,
|
|
3774
|
+
* chunk length can differ from `chunkSize`, depending on content.
|
|
3775
|
+
*
|
|
3776
|
+
*
|
|
3777
|
+
* ##### Example:
|
|
3778
|
+
*
|
|
3779
|
+
* ```javascript
|
|
3780
|
+
* const pako = require('pako');
|
|
3781
|
+
* const input = pako.deflate(new Uint8Array([1,2,3,4,5,6,7,8,9]));
|
|
3782
|
+
* let output;
|
|
3783
|
+
*
|
|
3784
|
+
* try {
|
|
3785
|
+
* output = pako.inflate(input);
|
|
3786
|
+
* } catch (err) {
|
|
3787
|
+
* console.log(err);
|
|
3788
|
+
* }
|
|
3789
|
+
* ```
|
|
3790
|
+
**/
|
|
3791
|
+
function inflate$1(input, options) {
|
|
3792
|
+
const inflator = new Inflate$1(options);
|
|
3793
|
+
inflator.push(input);
|
|
3794
|
+
if (inflator.err) throw inflator.msg || messages[inflator.err];
|
|
3795
|
+
return inflator.result;
|
|
3796
|
+
}
|
|
3797
|
+
/**
|
|
3798
|
+
* inflateRaw(data[, options]) -> Uint8Array|String
|
|
3799
|
+
* - data (Uint8Array|ArrayBuffer): input data to decompress.
|
|
3800
|
+
* - options (Object): zlib inflate options.
|
|
3801
|
+
*
|
|
3802
|
+
* The same as [[inflate]], but creates raw data, without wrapper
|
|
3803
|
+
* (header and adler32 crc).
|
|
3804
|
+
**/
|
|
3805
|
+
function inflateRaw$1(input, options) {
|
|
3806
|
+
options = options || {};
|
|
3807
|
+
options.raw = true;
|
|
3808
|
+
return inflate$1(input, options);
|
|
3809
|
+
}
|
|
3810
|
+
var inflate_1$1 = {
|
|
3811
|
+
Inflate: Inflate$1,
|
|
3812
|
+
inflate: inflate$1,
|
|
3813
|
+
inflateRaw: inflateRaw$1,
|
|
3814
|
+
ungzip: inflate$1,
|
|
3815
|
+
constants: constants$2
|
|
3816
|
+
};
|
|
3817
|
+
const { Deflate, deflate, deflateRaw, gzip } = deflate_1$1;
|
|
3818
|
+
const { Inflate, inflate, inflateRaw, ungzip } = inflate_1$1;
|
|
3819
|
+
var inflateRaw_1 = inflateRaw;
|
|
3820
|
+
|
|
3821
|
+
//#endregion
|
|
3822
|
+
//#region src/utils/compression.ts
|
|
3823
|
+
/**
|
|
3824
|
+
* Decompress raw deflate data (no zlib header)
|
|
3825
|
+
* Equivalent to Python: zlib.decompress(data, -15)
|
|
3826
|
+
*
|
|
3827
|
+
* HWP files use raw deflate compression without zlib wrapper headers.
|
|
3828
|
+
* The windowBits=-15 in Python indicates raw deflate mode.
|
|
3829
|
+
*
|
|
3830
|
+
* @param data - Compressed data
|
|
3831
|
+
* @returns Decompressed data
|
|
3832
|
+
*/
|
|
3833
|
+
function decompressRaw(data) {
|
|
3834
|
+
try {
|
|
3835
|
+
return inflateRaw_1(data);
|
|
3836
|
+
} catch (error) {
|
|
3837
|
+
throw new Error(`Failed to decompress data: ${error.message}`);
|
|
3838
|
+
}
|
|
3839
|
+
}
|
|
3840
|
+
|
|
3841
|
+
//#endregion
|
|
3842
|
+
//#region src/parser.ts
|
|
3843
|
+
/**
|
|
3844
|
+
* HWP 5.0 File Parser
|
|
3845
|
+
* Parses OLE Compound File format HWP files
|
|
3846
|
+
*/
|
|
3847
|
+
/**
|
|
3848
|
+
* HWP File Parser
|
|
3849
|
+
* Reads and parses HWP 5.0 files using OLE Compound File format
|
|
3850
|
+
*/
|
|
3851
|
+
var HWPFile = class HWPFile {
|
|
3852
|
+
cfb = null;
|
|
3853
|
+
_fileHeader = null;
|
|
3854
|
+
_isCompressed = false;
|
|
3855
|
+
/**
|
|
3856
|
+
* Create HWPFile from raw data
|
|
3857
|
+
* @param data - Raw HWP file data
|
|
3858
|
+
*/
|
|
3859
|
+
constructor(data) {
|
|
3860
|
+
this.data = data;
|
|
3861
|
+
}
|
|
3862
|
+
/**
|
|
3863
|
+
* Create HWPFile from file path (Node.js only)
|
|
3864
|
+
* @param path - Path to HWP file
|
|
3865
|
+
*/
|
|
3866
|
+
static async fromFile(path) {
|
|
3867
|
+
const data = await (await import("node:fs/promises")).readFile(path);
|
|
3868
|
+
return new HWPFile(new Uint8Array(data));
|
|
3869
|
+
}
|
|
3870
|
+
/**
|
|
3871
|
+
* Create HWPFile from ArrayBuffer
|
|
3872
|
+
* @param data - ArrayBuffer data
|
|
3873
|
+
*/
|
|
3874
|
+
static fromArrayBuffer(data) {
|
|
3875
|
+
return new HWPFile(new Uint8Array(data));
|
|
3876
|
+
}
|
|
3877
|
+
/**
|
|
3878
|
+
* Create HWPFile from Uint8Array
|
|
3879
|
+
* @param data - Uint8Array data
|
|
3880
|
+
*/
|
|
3881
|
+
static fromUint8Array(data) {
|
|
3882
|
+
return new HWPFile(data);
|
|
3883
|
+
}
|
|
3884
|
+
/**
|
|
3885
|
+
* Open and parse HWP file
|
|
3886
|
+
*/
|
|
3887
|
+
open() {
|
|
3888
|
+
const uint8Array = this.data instanceof Uint8Array ? this.data : new Uint8Array(this.data);
|
|
3889
|
+
if (uint8Array.length >= 30) {
|
|
3890
|
+
if (new TextDecoder("utf-8").decode(uint8Array.slice(0, 30)).replace(/\0+$/, "").startsWith("HWP Document File V3")) throw new Error("HWP 3.0 format (HWP 97, 2002, etc.) is not supported. Please use HWP 5.0 or later format. You can convert HWP 3.0 files to HWP 5.0 format using Hancom Office.");
|
|
3891
|
+
}
|
|
3892
|
+
try {
|
|
3893
|
+
if (typeof Buffer !== "undefined") {
|
|
3894
|
+
const buffer = Buffer.from(uint8Array);
|
|
3895
|
+
this.cfb = CFB.read(buffer, { type: "buffer" });
|
|
3896
|
+
} else {
|
|
3897
|
+
const buffer = Array.from(uint8Array);
|
|
3898
|
+
this.cfb = CFB.read(buffer, { type: "array" });
|
|
3899
|
+
}
|
|
3900
|
+
} catch (error) {
|
|
3901
|
+
throw new Error(`Failed to parse HWP file: ${error.message}`);
|
|
3902
|
+
}
|
|
3903
|
+
this._fileHeader = this.parseFileHeader();
|
|
3904
|
+
this._isCompressed = this._fileHeader.isCompressed;
|
|
3905
|
+
}
|
|
3906
|
+
/**
|
|
3907
|
+
* Close HWP file and release resources
|
|
3908
|
+
*/
|
|
3909
|
+
close() {
|
|
3910
|
+
this.cfb = null;
|
|
3911
|
+
this._fileHeader = null;
|
|
3912
|
+
}
|
|
3913
|
+
/**
|
|
3914
|
+
* Get file header information
|
|
3915
|
+
*/
|
|
3916
|
+
get fileHeader() {
|
|
3917
|
+
return this._fileHeader;
|
|
3918
|
+
}
|
|
3919
|
+
/**
|
|
3920
|
+
* Check if file is compressed
|
|
3921
|
+
*/
|
|
3922
|
+
get isCompressed() {
|
|
3923
|
+
return this._isCompressed;
|
|
3924
|
+
}
|
|
3925
|
+
/**
|
|
3926
|
+
* Parse FileHeader stream (256 bytes fixed)
|
|
3927
|
+
*/
|
|
3928
|
+
parseFileHeader() {
|
|
3929
|
+
if (!this.cfb) throw new Error("HWP file not opened");
|
|
3930
|
+
const entry = CFB.find(this.cfb, "FileHeader");
|
|
3931
|
+
if (!entry) throw new Error("FileHeader not found in HWP file");
|
|
3932
|
+
const data = entry.content;
|
|
3933
|
+
if (data.length !== 256) throw new Error(`Invalid FileHeader size: ${data.length} (expected 256)`);
|
|
3934
|
+
const signatureBytes = data.slice(0, 32);
|
|
3935
|
+
const signature = new TextDecoder("utf-8").decode(signatureBytes).replace(/\0+$/, "");
|
|
3936
|
+
if (!signature.startsWith("HWP Document File")) throw new Error(`Invalid HWP signature: ${signature}`);
|
|
3937
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
3938
|
+
const versionRaw = view.getUint32(32, true);
|
|
3939
|
+
const major = versionRaw >> 24 & 255;
|
|
3940
|
+
const minor = versionRaw >> 16 & 255;
|
|
3941
|
+
const patch = versionRaw >> 8 & 255;
|
|
3942
|
+
const rev = versionRaw & 255;
|
|
3943
|
+
const properties = view.getUint32(36, true);
|
|
3944
|
+
const isCompressed = Boolean(properties & 1);
|
|
3945
|
+
const isEncrypted = Boolean(properties & 2);
|
|
3946
|
+
return {
|
|
3947
|
+
signature,
|
|
3948
|
+
version: `${major}.${minor}.${patch}.${rev}`,
|
|
3949
|
+
isCompressed,
|
|
3950
|
+
isEncrypted,
|
|
3951
|
+
rawProperties: properties
|
|
3952
|
+
};
|
|
3953
|
+
}
|
|
3954
|
+
/**
|
|
3955
|
+
* Read and decompress stream
|
|
3956
|
+
* @param streamPath - Stream path (e.g., 'DocInfo', 'BodyText/Section0')
|
|
3957
|
+
* @returns Decompressed data or null if stream doesn't exist
|
|
3958
|
+
*/
|
|
3959
|
+
readStream(streamPath) {
|
|
3960
|
+
if (!this.cfb) return null;
|
|
3961
|
+
const entry = CFB.find(this.cfb, streamPath);
|
|
3962
|
+
if (!entry) return null;
|
|
3963
|
+
let data = entry.content;
|
|
3964
|
+
if (this._isCompressed) try {
|
|
3965
|
+
data = decompressRaw(data);
|
|
3966
|
+
} catch (error) {
|
|
3967
|
+
throw new Error(`Failed to decompress ${streamPath}: ${error.message}`);
|
|
3968
|
+
}
|
|
3969
|
+
return data;
|
|
3970
|
+
}
|
|
3971
|
+
/**
|
|
3972
|
+
* List all streams in HWP file
|
|
3973
|
+
* @returns Array of stream paths
|
|
3974
|
+
*/
|
|
3975
|
+
listStreams() {
|
|
3976
|
+
if (!this.cfb) return [];
|
|
3977
|
+
const streams = [];
|
|
3978
|
+
for (const entry of this.cfb.FileIndex) if (entry.type === 2) {
|
|
3979
|
+
const path = entry.name.split("/").filter((p) => p);
|
|
3980
|
+
if (path.length > 0) streams.push(path);
|
|
3981
|
+
}
|
|
3982
|
+
return streams;
|
|
3983
|
+
}
|
|
3984
|
+
/**
|
|
3985
|
+
* Get file information
|
|
3986
|
+
*/
|
|
3987
|
+
getFileInfo() {
|
|
3988
|
+
if (!this._fileHeader) return {};
|
|
3989
|
+
return {
|
|
3990
|
+
signature: this._fileHeader.signature,
|
|
3991
|
+
version: this._fileHeader.version,
|
|
3992
|
+
compressed: this._isCompressed,
|
|
3993
|
+
encrypted: this._fileHeader.isEncrypted,
|
|
3994
|
+
streams: this.listStreams()
|
|
3995
|
+
};
|
|
3996
|
+
}
|
|
3997
|
+
/**
|
|
3998
|
+
* Get number of sections in BodyText
|
|
3999
|
+
*/
|
|
4000
|
+
getSectionCount() {
|
|
4001
|
+
if (!this.cfb) return 0;
|
|
4002
|
+
let count = 0;
|
|
4003
|
+
while (CFB.find(this.cfb, `BodyText/Section${count}`) || CFB.find(this.cfb, `Section${count}`)) count++;
|
|
4004
|
+
return count;
|
|
4005
|
+
}
|
|
4006
|
+
/**
|
|
4007
|
+
* Read section data
|
|
4008
|
+
* @param sectionIndex - Section index (0-based)
|
|
4009
|
+
* @returns Decompressed section data
|
|
4010
|
+
*/
|
|
4011
|
+
readSection(sectionIndex) {
|
|
4012
|
+
let data = this.readStream(`BodyText/Section${sectionIndex}`);
|
|
4013
|
+
if (!data) data = this.readStream(`Section${sectionIndex}`);
|
|
4014
|
+
return data;
|
|
4015
|
+
}
|
|
4016
|
+
};
|
|
4017
|
+
|
|
4018
|
+
//#endregion
|
|
4019
|
+
//#region src/record.ts
|
|
4020
|
+
const HWPTAG_BEGIN = 16;
|
|
4021
|
+
const HWPTAG_PARA_HEADER = HWPTAG_BEGIN + 50;
|
|
4022
|
+
const HWPTAG_PARA_TEXT = HWPTAG_BEGIN + 51;
|
|
4023
|
+
const HWPTAG_PARA_CHAR_SHAPE = HWPTAG_BEGIN + 52;
|
|
4024
|
+
const HWPTAG_PARA_LINE_SEG = HWPTAG_BEGIN + 53;
|
|
4025
|
+
const HWPTAG_PARA_RANGE_TAG = HWPTAG_BEGIN + 54;
|
|
4026
|
+
const HWPTAG_CTRL_HEADER = HWPTAG_BEGIN + 55;
|
|
4027
|
+
const HWPTAG_LIST_HEADER = HWPTAG_BEGIN + 56;
|
|
4028
|
+
const HWPTAG_PAGE_DEF = HWPTAG_BEGIN + 57;
|
|
4029
|
+
const HWPTAG_FOOTNOTE_SHAPE = HWPTAG_BEGIN + 58;
|
|
4030
|
+
const HWPTAG_PAGE_BORDER_FILL = HWPTAG_BEGIN + 59;
|
|
4031
|
+
const HWPTAG_SHAPE_COMPONENT = HWPTAG_BEGIN + 60;
|
|
4032
|
+
const HWPTAG_TABLE = HWPTAG_BEGIN + 61;
|
|
4033
|
+
const HWPTAG_SHAPE_COMPONENT_LINE = HWPTAG_BEGIN + 62;
|
|
4034
|
+
const HWPTAG_CTRL_DATA = HWPTAG_BEGIN + 71;
|
|
4035
|
+
/**
|
|
4036
|
+
* HWP Record Reader
|
|
4037
|
+
* Reads binary records from HWP stream data
|
|
4038
|
+
*/
|
|
4039
|
+
var RecordReader = class {
|
|
4040
|
+
data;
|
|
4041
|
+
offset = 0;
|
|
4042
|
+
constructor(data) {
|
|
4043
|
+
this.data = data;
|
|
4044
|
+
}
|
|
4045
|
+
/**
|
|
4046
|
+
* Check if there are more records to read
|
|
4047
|
+
*/
|
|
4048
|
+
hasMore() {
|
|
4049
|
+
return this.offset < this.data.length;
|
|
4050
|
+
}
|
|
4051
|
+
/**
|
|
4052
|
+
* Read next record
|
|
4053
|
+
* @returns Next record, or null if no more records
|
|
4054
|
+
*/
|
|
4055
|
+
readRecord() {
|
|
4056
|
+
if (this.offset + 4 > this.data.length) return null;
|
|
4057
|
+
const header = new DataView(this.data.buffer, this.data.byteOffset + this.offset, 4).getUint32(0, true);
|
|
4058
|
+
const tagId = header & 1023;
|
|
4059
|
+
const level = header >> 10 & 1023;
|
|
4060
|
+
let size = header >> 20 & 4095;
|
|
4061
|
+
let dataOffset = this.offset + 4;
|
|
4062
|
+
if (size === 4095) {
|
|
4063
|
+
if (dataOffset + 4 > this.data.length) return null;
|
|
4064
|
+
size = new DataView(this.data.buffer, this.data.byteOffset + dataOffset, 4).getUint32(0, true);
|
|
4065
|
+
dataOffset += 4;
|
|
4066
|
+
}
|
|
4067
|
+
if (dataOffset + size > this.data.length) return null;
|
|
4068
|
+
const recordData = this.data.slice(dataOffset, dataOffset + size);
|
|
4069
|
+
this.offset = dataOffset + size;
|
|
4070
|
+
return {
|
|
4071
|
+
tagId,
|
|
4072
|
+
level,
|
|
4073
|
+
data: recordData,
|
|
4074
|
+
size
|
|
4075
|
+
};
|
|
4076
|
+
}
|
|
4077
|
+
/**
|
|
4078
|
+
* Peek at next record header without consuming it
|
|
4079
|
+
* @returns Header info with tagId, level, size
|
|
4080
|
+
*/
|
|
4081
|
+
peekRecordHeader() {
|
|
4082
|
+
if (this.offset + 4 > this.data.length) return null;
|
|
4083
|
+
const header = new DataView(this.data.buffer, this.data.byteOffset + this.offset, 4).getUint32(0, true);
|
|
4084
|
+
const tagId = header & 1023;
|
|
4085
|
+
const level = header >> 10 & 1023;
|
|
4086
|
+
let size = header >> 20 & 4095;
|
|
4087
|
+
let dataOffset = this.offset + 4;
|
|
4088
|
+
if (size === 4095) {
|
|
4089
|
+
if (dataOffset + 4 > this.data.length) return null;
|
|
4090
|
+
size = new DataView(this.data.buffer, this.data.byteOffset + dataOffset, 4).getUint32(0, true);
|
|
4091
|
+
}
|
|
4092
|
+
return {
|
|
4093
|
+
tagId,
|
|
4094
|
+
level,
|
|
4095
|
+
size
|
|
4096
|
+
};
|
|
4097
|
+
}
|
|
4098
|
+
/**
|
|
4099
|
+
* Read all records (considering hierarchy)
|
|
4100
|
+
* @param parentLevel - Stop when reaching this level or below
|
|
4101
|
+
* @returns All records at current level
|
|
4102
|
+
*/
|
|
4103
|
+
readAllRecords(parentLevel) {
|
|
4104
|
+
const records = [];
|
|
4105
|
+
while (this.hasMore()) {
|
|
4106
|
+
const header = this.peekRecordHeader();
|
|
4107
|
+
if (!header) break;
|
|
4108
|
+
if (parentLevel !== void 0 && header.level <= parentLevel) break;
|
|
4109
|
+
const record = this.readRecord();
|
|
4110
|
+
if (record) records.push(record);
|
|
4111
|
+
}
|
|
4112
|
+
return records;
|
|
4113
|
+
}
|
|
4114
|
+
/**
|
|
4115
|
+
* Get current position
|
|
4116
|
+
*/
|
|
4117
|
+
get position() {
|
|
4118
|
+
return this.offset;
|
|
4119
|
+
}
|
|
4120
|
+
/**
|
|
4121
|
+
* Get remaining bytes
|
|
4122
|
+
*/
|
|
4123
|
+
get remaining() {
|
|
4124
|
+
return this.data.length - this.offset;
|
|
4125
|
+
}
|
|
4126
|
+
};
|
|
4127
|
+
|
|
4128
|
+
//#endregion
|
|
4129
|
+
//#region src/table.ts
|
|
4130
|
+
/**
|
|
4131
|
+
* HWP Table Parser
|
|
4132
|
+
* Parses table records and converts to Markdown
|
|
4133
|
+
*/
|
|
4134
|
+
/**
|
|
4135
|
+
* Parse table properties from TABLE record data
|
|
4136
|
+
* @param data - TABLE record data
|
|
4137
|
+
* @returns Table properties with rows, cols
|
|
4138
|
+
*/
|
|
4139
|
+
function parseTableProperties(data) {
|
|
4140
|
+
if (data.length < 8) throw new Error(`Invalid TABLE record size: ${data.length}`);
|
|
4141
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
4142
|
+
return {
|
|
4143
|
+
properties: view.getUint32(0, true),
|
|
4144
|
+
rows: view.getUint16(4, true),
|
|
4145
|
+
cols: view.getUint16(6, true)
|
|
4146
|
+
};
|
|
4147
|
+
}
|
|
4148
|
+
/**
|
|
4149
|
+
* Parse table from TABLE record and subsequent records
|
|
4150
|
+
* @param tableRecordData - TABLE record data
|
|
4151
|
+
* @param reader - Record reader for reading cell data
|
|
4152
|
+
* @param lineBreakStyle - How to handle line breaks in cells
|
|
4153
|
+
* @returns Parsed table
|
|
4154
|
+
*/
|
|
4155
|
+
function parseTable(tableRecordData, reader, lineBreakStyle = "space") {
|
|
4156
|
+
const props = parseTableProperties(tableRecordData);
|
|
4157
|
+
const rows = props.rows;
|
|
4158
|
+
const cols = props.cols;
|
|
4159
|
+
const cells = [];
|
|
4160
|
+
let cellIndex = 0;
|
|
4161
|
+
while (reader.hasMore() && cellIndex < rows * cols) {
|
|
4162
|
+
const header = reader.peekRecordHeader();
|
|
4163
|
+
if (!header) break;
|
|
4164
|
+
if (header.level < 2) break;
|
|
4165
|
+
if (header.tagId === HWPTAG_LIST_HEADER) {
|
|
4166
|
+
const listRecord = reader.readRecord();
|
|
4167
|
+
if (!listRecord) break;
|
|
4168
|
+
let row, col, colspan, rowspan;
|
|
4169
|
+
if (listRecord.data.length >= 16) {
|
|
4170
|
+
const view = new DataView(listRecord.data.buffer, listRecord.data.byteOffset, listRecord.data.byteLength);
|
|
4171
|
+
col = view.getUint16(8, true);
|
|
4172
|
+
row = view.getUint16(10, true);
|
|
4173
|
+
colspan = view.getUint16(12, true);
|
|
4174
|
+
rowspan = view.getUint16(14, true);
|
|
4175
|
+
if (colspan === 0) colspan = 1;
|
|
4176
|
+
if (rowspan === 0) rowspan = 1;
|
|
4177
|
+
} else {
|
|
4178
|
+
row = Math.floor(cellIndex / cols);
|
|
4179
|
+
col = cellIndex % cols;
|
|
4180
|
+
colspan = 1;
|
|
4181
|
+
rowspan = 1;
|
|
4182
|
+
}
|
|
4183
|
+
const textParts = [];
|
|
4184
|
+
while (reader.hasMore()) {
|
|
4185
|
+
const paraHeader = reader.peekRecordHeader();
|
|
4186
|
+
if (!paraHeader) break;
|
|
4187
|
+
if (paraHeader.tagId === HWPTAG_LIST_HEADER) break;
|
|
4188
|
+
if (paraHeader.level < 2) break;
|
|
4189
|
+
if (paraHeader.tagId === HWPTAG_PARA_HEADER) {
|
|
4190
|
+
const paraRec = reader.readRecord();
|
|
4191
|
+
if (!paraRec) break;
|
|
4192
|
+
if (paraRec.data.length >= 4) {
|
|
4193
|
+
const nchars = new DataView(paraRec.data.buffer, paraRec.data.byteOffset, paraRec.data.byteLength).getUint32(0, true) & 2147483647;
|
|
4194
|
+
if (nchars > 0 && reader.hasMore()) {
|
|
4195
|
+
const nextH = reader.peekRecordHeader();
|
|
4196
|
+
if (nextH && nextH.tagId === HWPTAG_PARA_TEXT) {
|
|
4197
|
+
const textRec = reader.readRecord();
|
|
4198
|
+
if (textRec) {
|
|
4199
|
+
const text = processControlChars(parseParaText(textRec.data, nchars)).text;
|
|
4200
|
+
if (text.trim()) textParts.push(text.trim());
|
|
4201
|
+
}
|
|
4202
|
+
}
|
|
4203
|
+
}
|
|
4204
|
+
}
|
|
4205
|
+
} else reader.readRecord();
|
|
4206
|
+
}
|
|
4207
|
+
let cellText;
|
|
4208
|
+
if (lineBreakStyle === "br") {
|
|
4209
|
+
cellText = textParts.join("<br>");
|
|
4210
|
+
cellText = cellText.replace(/\n/g, "<br>");
|
|
4211
|
+
} else {
|
|
4212
|
+
cellText = textParts.join(" ");
|
|
4213
|
+
cellText = cellText.replace(/\n/g, " ");
|
|
4214
|
+
}
|
|
4215
|
+
const cell = {
|
|
4216
|
+
row,
|
|
4217
|
+
col,
|
|
4218
|
+
rowspan,
|
|
4219
|
+
colspan,
|
|
4220
|
+
text: cellText
|
|
4221
|
+
};
|
|
4222
|
+
cells.push(cell);
|
|
4223
|
+
cellIndex++;
|
|
4224
|
+
} else reader.readRecord();
|
|
4225
|
+
}
|
|
4226
|
+
return {
|
|
4227
|
+
rows,
|
|
4228
|
+
cols,
|
|
4229
|
+
cells
|
|
4230
|
+
};
|
|
4231
|
+
}
|
|
4232
|
+
/**
|
|
4233
|
+
* Convert table to Markdown
|
|
4234
|
+
* @param table - Table object
|
|
4235
|
+
* @param mergeStrategy - 'repeat' (default) or 'blank'
|
|
4236
|
+
* @returns Markdown table
|
|
4237
|
+
*/
|
|
4238
|
+
function tableToMarkdown(table, mergeStrategy = "repeat") {
|
|
4239
|
+
const { rows, cols, cells } = table;
|
|
4240
|
+
const matrix = Array.from({ length: rows }, () => Array(cols).fill(""));
|
|
4241
|
+
for (const cell of cells) if (mergeStrategy === "repeat") {
|
|
4242
|
+
for (let r = cell.row; r < cell.row + cell.rowspan; r++) for (let c = cell.col; c < cell.col + cell.colspan; c++) if (r < rows && c < cols) matrix[r][c] = cell.text;
|
|
4243
|
+
} else if (cell.row < rows && cell.col < cols) matrix[cell.row][cell.col] = cell.text;
|
|
4244
|
+
const lines = [];
|
|
4245
|
+
if (rows === 0 || cols === 0) return "[Empty Table]";
|
|
4246
|
+
lines.push("| " + Array(cols).fill("").join(" | ") + " |");
|
|
4247
|
+
lines.push("| " + Array(cols).fill("---").join(" | ") + " |");
|
|
4248
|
+
for (const row of matrix) lines.push("| " + row.join(" | ") + " |");
|
|
4249
|
+
return lines.join("\n");
|
|
4250
|
+
}
|
|
4251
|
+
|
|
4252
|
+
//#endregion
|
|
4253
|
+
//#region src/paragraph.ts
|
|
4254
|
+
/**
|
|
4255
|
+
* HWP Paragraph Parser
|
|
4256
|
+
* Parses paragraph records and extracts text
|
|
4257
|
+
*/
|
|
4258
|
+
/**
|
|
4259
|
+
* Parse paragraph header
|
|
4260
|
+
* @param data - PARA_HEADER record data
|
|
4261
|
+
* @returns Paragraph header information
|
|
4262
|
+
*/
|
|
4263
|
+
function parseParaHeader(data) {
|
|
4264
|
+
if (data.length < 22) throw new Error(`Invalid PARA_HEADER size: ${data.length}`);
|
|
4265
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
4266
|
+
return {
|
|
4267
|
+
textCount: view.getUint32(0, true) & 2147483647,
|
|
4268
|
+
controlMask: view.getUint32(4, true),
|
|
4269
|
+
paraShapeId: view.getUint16(8, true),
|
|
4270
|
+
styleId: view.getUint8(10),
|
|
4271
|
+
columnType: view.getUint8(11),
|
|
4272
|
+
charShapeCount: view.getUint16(12, true)
|
|
4273
|
+
};
|
|
4274
|
+
}
|
|
4275
|
+
/**
|
|
4276
|
+
* Parse paragraph text with control info table
|
|
4277
|
+
*
|
|
4278
|
+
* PARA_TEXT structure:
|
|
4279
|
+
* [Control info table: 16 bytes per control] + [Actual text]
|
|
4280
|
+
*
|
|
4281
|
+
* Each control info block (16 bytes):
|
|
4282
|
+
* - 2 bytes: control code
|
|
4283
|
+
* - 4 bytes: control ID
|
|
4284
|
+
* - 8 bytes: control data
|
|
4285
|
+
* - 2 bytes: control code (repeated)
|
|
4286
|
+
*
|
|
4287
|
+
* @param data - PARA_TEXT record data
|
|
4288
|
+
* @param nchars - Number of WCHAR characters (control table + text)
|
|
4289
|
+
* @returns Decoded text
|
|
4290
|
+
*/
|
|
4291
|
+
function parseParaText(data, _nchars) {
|
|
4292
|
+
const CHAR_CONTROLS = new Set([
|
|
4293
|
+
0,
|
|
4294
|
+
10,
|
|
4295
|
+
13,
|
|
4296
|
+
30,
|
|
4297
|
+
31
|
|
4298
|
+
]);
|
|
4299
|
+
let offset = 0;
|
|
4300
|
+
while (offset + 2 <= data.length) {
|
|
4301
|
+
const charCode = new DataView(data.buffer, data.byteOffset + offset, 2).getUint16(0, true);
|
|
4302
|
+
if (charCode < 32 && !CHAR_CONTROLS.has(charCode)) {
|
|
4303
|
+
offset += 16;
|
|
4304
|
+
if (offset > data.length) break;
|
|
4305
|
+
} else break;
|
|
4306
|
+
}
|
|
4307
|
+
const textData = data.slice(offset);
|
|
4308
|
+
if (textData.length === 0) return "";
|
|
4309
|
+
try {
|
|
4310
|
+
return new TextDecoder("utf-16le").decode(textData);
|
|
4311
|
+
} catch (error) {
|
|
4312
|
+
return "";
|
|
4313
|
+
}
|
|
4314
|
+
}
|
|
4315
|
+
/**
|
|
4316
|
+
* Process control characters in text
|
|
4317
|
+
* @param text - Raw text with control characters
|
|
4318
|
+
* @returns Processed text and has_table flag
|
|
4319
|
+
*/
|
|
4320
|
+
function processControlChars(text) {
|
|
4321
|
+
const result = [];
|
|
4322
|
+
let hasTable = false;
|
|
4323
|
+
for (const char of text) {
|
|
4324
|
+
const code = char.codePointAt(0) ?? 0;
|
|
4325
|
+
if (code < 32) {
|
|
4326
|
+
if (code === 10) result.push("\n");
|
|
4327
|
+
else if (code === 11) hasTable = true;
|
|
4328
|
+
else if (code === 13) result.push("\n");
|
|
4329
|
+
} else result.push(char);
|
|
4330
|
+
}
|
|
4331
|
+
return {
|
|
4332
|
+
text: result.join(""),
|
|
4333
|
+
hasTable
|
|
4334
|
+
};
|
|
4335
|
+
}
|
|
4336
|
+
/**
|
|
4337
|
+
* Paragraph Parser
|
|
4338
|
+
* Parses paragraphs from HWP record stream
|
|
4339
|
+
*/
|
|
4340
|
+
var ParagraphParser = class {
|
|
4341
|
+
constructor(reader, options = {}) {
|
|
4342
|
+
this.reader = reader;
|
|
4343
|
+
this.options = options;
|
|
4344
|
+
if (!this.options.tableLineBreakStyle) this.options.tableLineBreakStyle = "space";
|
|
4345
|
+
}
|
|
4346
|
+
/**
|
|
4347
|
+
* Parse next paragraph
|
|
4348
|
+
* @returns Parsed paragraph, or null if no more paragraphs
|
|
4349
|
+
*/
|
|
4350
|
+
parseParagraph() {
|
|
4351
|
+
if (!this.reader.hasMore()) return null;
|
|
4352
|
+
let record = null;
|
|
4353
|
+
while (this.reader.hasMore()) {
|
|
4354
|
+
record = this.reader.readRecord();
|
|
4355
|
+
if (record && record.tagId === HWPTAG_PARA_HEADER) break;
|
|
4356
|
+
record = null;
|
|
4357
|
+
}
|
|
4358
|
+
if (!record) return null;
|
|
4359
|
+
const header = parseParaHeader(record.data);
|
|
4360
|
+
let text = "";
|
|
4361
|
+
if (header.textCount > 0) {
|
|
4362
|
+
const nextHeader = this.reader.peekRecordHeader();
|
|
4363
|
+
if (nextHeader && nextHeader.tagId === HWPTAG_PARA_TEXT) {
|
|
4364
|
+
record = this.reader.readRecord();
|
|
4365
|
+
if (record) text = processControlChars(parseParaText(record.data, header.textCount)).text;
|
|
4366
|
+
}
|
|
4367
|
+
}
|
|
4368
|
+
const tables = [];
|
|
4369
|
+
while (this.reader.hasMore()) {
|
|
4370
|
+
const nextHeader = this.reader.peekRecordHeader();
|
|
4371
|
+
if (!nextHeader) break;
|
|
4372
|
+
if (nextHeader.tagId === HWPTAG_PARA_HEADER) break;
|
|
4373
|
+
if (nextHeader.level === 0) break;
|
|
4374
|
+
if (nextHeader.tagId === HWPTAG_CTRL_HEADER) {
|
|
4375
|
+
this.reader.readRecord();
|
|
4376
|
+
const tableHeader = this.reader.peekRecordHeader();
|
|
4377
|
+
if (tableHeader && tableHeader.tagId === HWPTAG_TABLE) {
|
|
4378
|
+
const tableRecord = this.reader.readRecord();
|
|
4379
|
+
if (tableRecord) try {
|
|
4380
|
+
const tableMd = tableToMarkdown(parseTable(tableRecord.data, this.reader, this.options.tableLineBreakStyle));
|
|
4381
|
+
tables.push(tableMd);
|
|
4382
|
+
} catch (error) {
|
|
4383
|
+
console.warn("Warning: Failed to parse table:", error);
|
|
4384
|
+
tables.push("[TABLE - Parse Error]");
|
|
4385
|
+
}
|
|
4386
|
+
}
|
|
4387
|
+
} else this.reader.readRecord();
|
|
4388
|
+
}
|
|
4389
|
+
if (tables.length > 0) {
|
|
4390
|
+
text = text.trimEnd();
|
|
4391
|
+
if (text.length > 0 && text.length < 5 && [...text].every((c) => {
|
|
4392
|
+
return (c.codePointAt(0) ?? 0) > 127 || /\s/.test(c);
|
|
4393
|
+
})) text = tables.join("\n\n");
|
|
4394
|
+
else {
|
|
4395
|
+
if (text) text += "\n\n";
|
|
4396
|
+
text += tables.join("\n\n");
|
|
4397
|
+
}
|
|
4398
|
+
}
|
|
4399
|
+
return {
|
|
4400
|
+
text,
|
|
4401
|
+
header
|
|
4402
|
+
};
|
|
4403
|
+
}
|
|
4404
|
+
/**
|
|
4405
|
+
* Parse all paragraphs in section
|
|
4406
|
+
* @returns All paragraphs
|
|
4407
|
+
*/
|
|
4408
|
+
parseAllParagraphs() {
|
|
4409
|
+
const paragraphs = [];
|
|
4410
|
+
while (this.reader.hasMore()) {
|
|
4411
|
+
const para = this.parseParagraph();
|
|
4412
|
+
if (para) paragraphs.push(para);
|
|
4413
|
+
else break;
|
|
4414
|
+
}
|
|
4415
|
+
return paragraphs;
|
|
4416
|
+
}
|
|
4417
|
+
};
|
|
4418
|
+
|
|
4419
|
+
//#endregion
|
|
4420
|
+
//#region src/hwpx_parser.ts
|
|
4421
|
+
/**
|
|
4422
|
+
* HWPX (ZIP+XML) File Parser
|
|
4423
|
+
* Parses HWPX files (ZIP archives containing XML) into Paragraphs and Tables
|
|
4424
|
+
*/
|
|
4425
|
+
const HWPX_MIMETYPE = "application/hwp+zip";
|
|
4426
|
+
/** Options for the XML parser */
|
|
4427
|
+
const xmlParserOptions = {
|
|
4428
|
+
ignoreAttributes: false,
|
|
4429
|
+
attributeNamePrefix: "@_",
|
|
4430
|
+
removeNSPrefix: false,
|
|
4431
|
+
isArray: (name) => {
|
|
4432
|
+
return [
|
|
4433
|
+
"hp:p",
|
|
4434
|
+
"hp:run",
|
|
4435
|
+
"hp:t",
|
|
4436
|
+
"hp:tr",
|
|
4437
|
+
"hp:tc",
|
|
4438
|
+
"hp:tbl",
|
|
4439
|
+
"opf:item",
|
|
4440
|
+
"opf:itemref"
|
|
4441
|
+
].includes(name);
|
|
4442
|
+
},
|
|
4443
|
+
textNodeName: "#text"
|
|
4444
|
+
};
|
|
4445
|
+
/**
|
|
4446
|
+
* HWPX file parser
|
|
4447
|
+
*/
|
|
4448
|
+
var HWPXFile = class HWPXFile {
|
|
4449
|
+
data;
|
|
4450
|
+
entries = null;
|
|
4451
|
+
sectionPaths = [];
|
|
4452
|
+
version = "";
|
|
4453
|
+
xmlParser;
|
|
4454
|
+
constructor(data) {
|
|
4455
|
+
this.data = data;
|
|
4456
|
+
this.xmlParser = new XMLParser(xmlParserOptions);
|
|
4457
|
+
}
|
|
4458
|
+
static async fromFile(path) {
|
|
4459
|
+
const { readFileSync } = await import("node:fs");
|
|
4460
|
+
const data = readFileSync(path);
|
|
4461
|
+
return new HWPXFile(new Uint8Array(data));
|
|
4462
|
+
}
|
|
4463
|
+
static fromArrayBuffer(data) {
|
|
4464
|
+
return new HWPXFile(new Uint8Array(data));
|
|
4465
|
+
}
|
|
4466
|
+
static fromUint8Array(data) {
|
|
4467
|
+
return new HWPXFile(data);
|
|
4468
|
+
}
|
|
4469
|
+
open() {
|
|
4470
|
+
if (this.data.length < 4 || this.data[0] !== 80 || this.data[1] !== 75 || this.data[2] !== 3 || this.data[3] !== 4) throw new Error("Not a valid HWPX file (not a ZIP archive)");
|
|
4471
|
+
try {
|
|
4472
|
+
this.entries = unzipSync(this.data);
|
|
4473
|
+
} catch (e) {
|
|
4474
|
+
throw new Error(`Failed to parse HWPX file: ${e instanceof Error ? e.message : e}`);
|
|
4475
|
+
}
|
|
4476
|
+
const mimetypeEntry = this.entries["mimetype"];
|
|
4477
|
+
if (!mimetypeEntry) throw new Error("Not a valid HWPX file: missing 'mimetype' entry");
|
|
4478
|
+
const mimetype = new TextDecoder().decode(mimetypeEntry).trim();
|
|
4479
|
+
if (mimetype !== HWPX_MIMETYPE) throw new Error(`Invalid HWPX mimetype: '${mimetype}' (expected '${HWPX_MIMETYPE}')`);
|
|
4480
|
+
this.parseVersion();
|
|
4481
|
+
this.discoverSections();
|
|
4482
|
+
}
|
|
4483
|
+
close() {
|
|
4484
|
+
this.entries = null;
|
|
4485
|
+
this.sectionPaths = [];
|
|
4486
|
+
}
|
|
4487
|
+
parseVersion() {
|
|
4488
|
+
const versionEntry = this.entries?.["version.xml"];
|
|
4489
|
+
if (!versionEntry) {
|
|
4490
|
+
this.version = "unknown";
|
|
4491
|
+
return;
|
|
4492
|
+
}
|
|
4493
|
+
try {
|
|
4494
|
+
const xml = new TextDecoder().decode(versionEntry);
|
|
4495
|
+
const parsed = this.xmlParser.parse(xml);
|
|
4496
|
+
const root = parsed["hv:HCFVersion"] ?? parsed["HCFVersion"] ?? parsed;
|
|
4497
|
+
const major = root["@_major"] ?? "";
|
|
4498
|
+
const minor = root["@_minor"] ?? "";
|
|
4499
|
+
const micro = root["@_micro"] ?? "";
|
|
4500
|
+
if (major) this.version = `${major}.${minor}.${micro}`;
|
|
4501
|
+
else this.version = root["@_Version"] ?? "unknown";
|
|
4502
|
+
} catch {
|
|
4503
|
+
this.version = "unknown";
|
|
4504
|
+
}
|
|
4505
|
+
}
|
|
4506
|
+
discoverSections() {
|
|
4507
|
+
this.sectionPaths = [];
|
|
4508
|
+
const hpfEntry = this.entries?.["Contents/content.hpf"];
|
|
4509
|
+
if (!hpfEntry) {
|
|
4510
|
+
for (const name of Object.keys(this.entries ?? {})) if (name.startsWith("Contents/section") && name.endsWith(".xml")) this.sectionPaths.push(name);
|
|
4511
|
+
this.sectionPaths.sort();
|
|
4512
|
+
return;
|
|
4513
|
+
}
|
|
4514
|
+
try {
|
|
4515
|
+
const xml = new TextDecoder().decode(hpfEntry);
|
|
4516
|
+
const parsed = this.xmlParser.parse(xml);
|
|
4517
|
+
const pkg = parsed["opf:package"] ?? parsed["package"] ?? {};
|
|
4518
|
+
const manifest = pkg["opf:manifest"] ?? pkg["manifest"] ?? {};
|
|
4519
|
+
const spine = pkg["opf:spine"] ?? pkg["spine"] ?? {};
|
|
4520
|
+
const idToHref = {};
|
|
4521
|
+
const items = manifest["opf:item"] ?? manifest["item"] ?? [];
|
|
4522
|
+
const itemList = Array.isArray(items) ? items : [items];
|
|
4523
|
+
for (const item of itemList) {
|
|
4524
|
+
const id = item["@_id"] ?? "";
|
|
4525
|
+
const href = item["@_href"] ?? "";
|
|
4526
|
+
if (href) idToHref[id] = href;
|
|
4527
|
+
}
|
|
4528
|
+
const itemrefs = spine["opf:itemref"] ?? spine["itemref"] ?? [];
|
|
4529
|
+
const refList = Array.isArray(itemrefs) ? itemrefs : [itemrefs];
|
|
4530
|
+
for (const ref of refList) {
|
|
4531
|
+
const idref = ref["@_idref"] ?? "";
|
|
4532
|
+
if (idref in idToHref && idref.toLowerCase().includes("section")) {
|
|
4533
|
+
let href = idToHref[idref];
|
|
4534
|
+
if (!href.startsWith("Contents/")) href = "Contents/" + href;
|
|
4535
|
+
this.sectionPaths.push(href);
|
|
4536
|
+
}
|
|
4537
|
+
}
|
|
4538
|
+
if (this.sectionPaths.length === 0) {
|
|
4539
|
+
for (const [, href] of Object.entries(idToHref).sort()) if (href.toLowerCase().includes("section")) {
|
|
4540
|
+
const fullHref = href.startsWith("Contents/") ? href : "Contents/" + href;
|
|
4541
|
+
this.sectionPaths.push(fullHref);
|
|
4542
|
+
}
|
|
4543
|
+
}
|
|
4544
|
+
} catch {
|
|
4545
|
+
for (const name of Object.keys(this.entries ?? {})) if (name.startsWith("Contents/section") && name.endsWith(".xml")) this.sectionPaths.push(name);
|
|
4546
|
+
this.sectionPaths.sort();
|
|
4547
|
+
}
|
|
4548
|
+
}
|
|
4549
|
+
get fileInfo() {
|
|
4550
|
+
this.ensureOpen();
|
|
4551
|
+
return {
|
|
4552
|
+
format: "HWPX",
|
|
4553
|
+
version: this.version,
|
|
4554
|
+
sectionCount: this.sectionPaths.length,
|
|
4555
|
+
contents: Object.keys(this.entries ?? {})
|
|
4556
|
+
};
|
|
4557
|
+
}
|
|
4558
|
+
getSectionCount() {
|
|
4559
|
+
this.ensureOpen();
|
|
4560
|
+
return this.sectionPaths.length;
|
|
4561
|
+
}
|
|
4562
|
+
getSectionXml(index) {
|
|
4563
|
+
this.ensureOpen();
|
|
4564
|
+
if (index < 0 || index >= this.sectionPaths.length) throw new RangeError(`Section index ${index} out of range (0-${this.sectionPaths.length - 1})`);
|
|
4565
|
+
const path = this.sectionPaths[index];
|
|
4566
|
+
const entry = this.entries?.[path];
|
|
4567
|
+
if (!entry) throw new Error(`Section file not found: ${path}`);
|
|
4568
|
+
const xml = new TextDecoder().decode(entry);
|
|
4569
|
+
return this.xmlParser.parse(xml);
|
|
4570
|
+
}
|
|
4571
|
+
listContents() {
|
|
4572
|
+
this.ensureOpen();
|
|
4573
|
+
return Object.keys(this.entries ?? {});
|
|
4574
|
+
}
|
|
4575
|
+
ensureOpen() {
|
|
4576
|
+
if (!this.entries) throw new Error("HWPX file is not open. Call open() first.");
|
|
4577
|
+
}
|
|
4578
|
+
};
|
|
4579
|
+
function ensureArray(val) {
|
|
4580
|
+
if (val === void 0 || val === null) return [];
|
|
4581
|
+
return Array.isArray(val) ? val : [val];
|
|
4582
|
+
}
|
|
4583
|
+
/**
|
|
4584
|
+
* Extract text from a paragraph object (hp:p)
|
|
4585
|
+
*/
|
|
4586
|
+
function extractParagraphText(pObj) {
|
|
4587
|
+
const parts = [];
|
|
4588
|
+
for (const run of ensureArray(pObj["hp:run"])) for (const [key, value] of Object.entries(run)) if (key === "hp:t") for (const t of ensureArray(value)) {
|
|
4589
|
+
const text = typeof t === "string" ? t : t?.["#text"];
|
|
4590
|
+
if (text !== void 0 && text !== null) parts.push(String(text));
|
|
4591
|
+
}
|
|
4592
|
+
else if (key === "hp:lineBreak") parts.push("\n");
|
|
4593
|
+
else if (key === "hp:tab") parts.push(" ");
|
|
4594
|
+
return parts.join("");
|
|
4595
|
+
}
|
|
4596
|
+
/**
|
|
4597
|
+
* Extract text from a table cell (hp:tc)
|
|
4598
|
+
*/
|
|
4599
|
+
function extractCellText(tcObj, lineBreakStyle = "space") {
|
|
4600
|
+
const textParts = [];
|
|
4601
|
+
const subList = tcObj["hp:subList"];
|
|
4602
|
+
if (subList) for (const p of ensureArray(subList["hp:p"])) {
|
|
4603
|
+
const paraText = extractParagraphText(p);
|
|
4604
|
+
if (paraText.trim()) textParts.push(paraText.trim());
|
|
4605
|
+
}
|
|
4606
|
+
if (lineBreakStyle === "br") return textParts.join("<br>").replace(/\n/g, "<br>");
|
|
4607
|
+
return textParts.join(" ").replace(/\n/g, " ");
|
|
4608
|
+
}
|
|
4609
|
+
/**
|
|
4610
|
+
* Parse a table XML object (hp:tbl) into a Table
|
|
4611
|
+
*/
|
|
4612
|
+
function parseHwpxTable(tblObj, lineBreakStyle = "space") {
|
|
4613
|
+
const rows = Number(tblObj["@_rowCnt"] ?? 0);
|
|
4614
|
+
const cols = Number(tblObj["@_colCnt"] ?? 0);
|
|
4615
|
+
const cells = [];
|
|
4616
|
+
for (const tr of ensureArray(tblObj["hp:tr"])) for (const tc of ensureArray(tr["hp:tc"])) {
|
|
4617
|
+
const addr = tc["hp:cellAddr"];
|
|
4618
|
+
const col = Number(addr?.["@_colAddr"] ?? 0);
|
|
4619
|
+
const row = Number(addr?.["@_rowAddr"] ?? 0);
|
|
4620
|
+
const span = tc["hp:cellSpan"];
|
|
4621
|
+
let colspan = Number(span?.["@_colSpan"] ?? 1);
|
|
4622
|
+
let rowspan = Number(span?.["@_rowSpan"] ?? 1);
|
|
4623
|
+
if (colspan < 1) colspan = 1;
|
|
4624
|
+
if (rowspan < 1) rowspan = 1;
|
|
4625
|
+
const text = extractCellText(tc, lineBreakStyle);
|
|
4626
|
+
cells.push({
|
|
4627
|
+
row,
|
|
4628
|
+
col,
|
|
4629
|
+
rowspan,
|
|
4630
|
+
colspan,
|
|
4631
|
+
text
|
|
4632
|
+
});
|
|
4633
|
+
}
|
|
4634
|
+
return {
|
|
4635
|
+
rows,
|
|
4636
|
+
cols,
|
|
4637
|
+
cells
|
|
4638
|
+
};
|
|
4639
|
+
}
|
|
4640
|
+
/**
|
|
4641
|
+
* Parse a section XML object into paragraphs
|
|
4642
|
+
*/
|
|
4643
|
+
function parseHwpxSection(sectionObj, options = {}) {
|
|
4644
|
+
const lineBreakStyle = options.tableLineBreakStyle ?? "space";
|
|
4645
|
+
const paragraphs = [];
|
|
4646
|
+
let sec = sectionObj;
|
|
4647
|
+
if (sec["hs:sec"]) sec = sec["hs:sec"];
|
|
4648
|
+
for (const p of ensureArray(sec["hp:p"])) {
|
|
4649
|
+
let text = extractParagraphText(p);
|
|
4650
|
+
const tableMds = [];
|
|
4651
|
+
for (const run of ensureArray(p["hp:run"])) for (const tbl of ensureArray(run["hp:tbl"])) try {
|
|
4652
|
+
const table = parseHwpxTable(tbl, lineBreakStyle);
|
|
4653
|
+
tableMds.push(tableToMarkdown(table));
|
|
4654
|
+
} catch {
|
|
4655
|
+
tableMds.push("[TABLE - Parse Error]");
|
|
4656
|
+
}
|
|
4657
|
+
for (const tbl of ensureArray(p["hp:tbl"])) try {
|
|
4658
|
+
const table = parseHwpxTable(tbl, lineBreakStyle);
|
|
4659
|
+
tableMds.push(tableToMarkdown(table));
|
|
4660
|
+
} catch {
|
|
4661
|
+
tableMds.push("[TABLE - Parse Error]");
|
|
4662
|
+
}
|
|
4663
|
+
if (tableMds.length > 0) {
|
|
4664
|
+
text = text.trimEnd();
|
|
4665
|
+
if (text) text += "\n\n";
|
|
4666
|
+
text += tableMds.join("\n\n");
|
|
4667
|
+
}
|
|
4668
|
+
if (text.trim()) paragraphs.push({
|
|
4669
|
+
text,
|
|
4670
|
+
header: {
|
|
4671
|
+
textCount: 0,
|
|
4672
|
+
controlMask: 0,
|
|
4673
|
+
paraShapeId: 0,
|
|
4674
|
+
styleId: 0,
|
|
4675
|
+
columnType: 0,
|
|
4676
|
+
charShapeCount: 0
|
|
4677
|
+
}
|
|
4678
|
+
});
|
|
4679
|
+
}
|
|
4680
|
+
for (const tbl of ensureArray(sec["hp:tbl"])) try {
|
|
4681
|
+
const md = tableToMarkdown(parseHwpxTable(tbl, lineBreakStyle));
|
|
4682
|
+
if (md.trim()) paragraphs.push({
|
|
4683
|
+
text: md,
|
|
4684
|
+
header: {
|
|
4685
|
+
textCount: 0,
|
|
4686
|
+
controlMask: 0,
|
|
4687
|
+
paraShapeId: 0,
|
|
4688
|
+
styleId: 0,
|
|
4689
|
+
columnType: 0,
|
|
4690
|
+
charShapeCount: 0
|
|
4691
|
+
}
|
|
4692
|
+
});
|
|
4693
|
+
} catch {}
|
|
4694
|
+
return paragraphs;
|
|
4695
|
+
}
|
|
4696
|
+
/**
|
|
4697
|
+
* Check if binary data is a HWPX file (ZIP magic bytes)
|
|
4698
|
+
*/
|
|
4699
|
+
function isHwpxData(data) {
|
|
4700
|
+
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
4701
|
+
return bytes.length >= 4 && bytes[0] === 80 && bytes[1] === 75 && bytes[2] === 3 && bytes[3] === 4;
|
|
4702
|
+
}
|
|
4703
|
+
/**
|
|
4704
|
+
* Check if a file path has HWPX extension
|
|
4705
|
+
*/
|
|
4706
|
+
function isHwpxPath(path) {
|
|
4707
|
+
return path.toLowerCase().endsWith(".hwpx");
|
|
4708
|
+
}
|
|
4709
|
+
|
|
4710
|
+
//#endregion
|
|
4711
|
+
//#region src/converter.ts
|
|
4712
|
+
/**
|
|
4713
|
+
* HWP/HWPX to Markdown Converter
|
|
4714
|
+
* Orchestrates conversion from HWP/HWPX to Markdown
|
|
4715
|
+
*/
|
|
4716
|
+
/**
|
|
4717
|
+
* Convert paragraphs to Markdown
|
|
4718
|
+
* @param paragraphs - List of paragraphs
|
|
4719
|
+
* @returns Markdown text
|
|
4720
|
+
*/
|
|
4721
|
+
function paragraphsToMarkdown(paragraphs) {
|
|
4722
|
+
const lines = [];
|
|
4723
|
+
for (const para of paragraphs) {
|
|
4724
|
+
const text = para.text.trim();
|
|
4725
|
+
if (!text) continue;
|
|
4726
|
+
if (text.length < 5 && !text.startsWith("|") && [...text].every((c) => (c.codePointAt(0) ?? 0) > 127)) continue;
|
|
4727
|
+
lines.push(text);
|
|
4728
|
+
}
|
|
4729
|
+
return lines.join("\n\n");
|
|
4730
|
+
}
|
|
4731
|
+
/**
|
|
4732
|
+
* Convert HWP file to Markdown
|
|
4733
|
+
* @param hwp - Opened HWP file
|
|
4734
|
+
* @param options - Conversion options
|
|
4735
|
+
* @returns Markdown content
|
|
4736
|
+
*/
|
|
4737
|
+
function convertHwpToMarkdown(hwp, options = {}) {
|
|
4738
|
+
const allParagraphs = [];
|
|
4739
|
+
if (!options.tableLineBreakStyle) options.tableLineBreakStyle = "space";
|
|
4740
|
+
const sectionCount = hwp.getSectionCount();
|
|
4741
|
+
for (let i = 0; i < sectionCount; i++) {
|
|
4742
|
+
const sectionData = hwp.readSection(i);
|
|
4743
|
+
if (!sectionData) continue;
|
|
4744
|
+
const paragraphs = new ParagraphParser(new RecordReader(sectionData), options).parseAllParagraphs();
|
|
4745
|
+
allParagraphs.push(...paragraphs);
|
|
4746
|
+
}
|
|
4747
|
+
return paragraphsToMarkdown(allParagraphs);
|
|
4748
|
+
}
|
|
4749
|
+
/**
|
|
4750
|
+
* Convert HWPX file to Markdown
|
|
4751
|
+
* @param hwpx - Opened HWPX file
|
|
4752
|
+
* @param options - Conversion options
|
|
4753
|
+
* @returns Markdown content
|
|
4754
|
+
*/
|
|
4755
|
+
function convertHwpxToMarkdown(hwpx, options = {}) {
|
|
4756
|
+
const allParagraphs = [];
|
|
4757
|
+
const sectionCount = hwpx.getSectionCount();
|
|
4758
|
+
for (let i = 0; i < sectionCount; i++) {
|
|
4759
|
+
const paragraphs = parseHwpxSection(hwpx.getSectionXml(i), options);
|
|
4760
|
+
allParagraphs.push(...paragraphs);
|
|
4761
|
+
}
|
|
4762
|
+
return paragraphsToMarkdown(allParagraphs);
|
|
4763
|
+
}
|
|
4764
|
+
/**
|
|
4765
|
+
* High-level API: Convert HWP/HWPX file to Markdown
|
|
4766
|
+
* Auto-detects format based on file extension or magic bytes.
|
|
4767
|
+
* @param input - File path (Node.js), ArrayBuffer, or Uint8Array
|
|
4768
|
+
* @param options - Conversion options
|
|
4769
|
+
* @returns Markdown content
|
|
4770
|
+
*/
|
|
4771
|
+
async function convert(input, options) {
|
|
4772
|
+
if (typeof input === "string" && isHwpxPath(input)) {
|
|
4773
|
+
const hwpx = await HWPXFile.fromFile(input);
|
|
4774
|
+
try {
|
|
4775
|
+
hwpx.open();
|
|
4776
|
+
return convertHwpxToMarkdown(hwpx, options);
|
|
4777
|
+
} finally {
|
|
4778
|
+
hwpx.close();
|
|
4779
|
+
}
|
|
4780
|
+
}
|
|
4781
|
+
if ((input instanceof Uint8Array || input instanceof ArrayBuffer) && isHwpxData(input instanceof ArrayBuffer ? new Uint8Array(input) : input)) {
|
|
4782
|
+
const hwpx = input instanceof Uint8Array ? HWPXFile.fromUint8Array(input) : HWPXFile.fromArrayBuffer(input);
|
|
4783
|
+
try {
|
|
4784
|
+
hwpx.open();
|
|
4785
|
+
return convertHwpxToMarkdown(hwpx, options);
|
|
4786
|
+
} finally {
|
|
4787
|
+
hwpx.close();
|
|
4788
|
+
}
|
|
4789
|
+
}
|
|
4790
|
+
let hwp;
|
|
4791
|
+
if (typeof input === "string") hwp = await HWPFile.fromFile(input);
|
|
4792
|
+
else if (input instanceof Uint8Array) hwp = HWPFile.fromUint8Array(input);
|
|
4793
|
+
else hwp = HWPFile.fromArrayBuffer(input);
|
|
4794
|
+
try {
|
|
4795
|
+
hwp.open();
|
|
4796
|
+
return convertHwpToMarkdown(hwp, options);
|
|
4797
|
+
} finally {
|
|
4798
|
+
hwp.close();
|
|
4799
|
+
}
|
|
4800
|
+
}
|
|
4801
|
+
|
|
4802
|
+
//#endregion
|
|
4803
|
+
export { HWPFile as a, paragraphsToMarkdown as i, convertHwpToMarkdown as n, convertHwpxToMarkdown as r, convert as t };
|
|
4804
|
+
//# sourceMappingURL=converter-D6LrZNSL.mjs.map
|