Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80540 views
1
'use strict';
2
3
4
var zlib_inflate = require('./zlib/inflate.js');
5
var utils = require('./utils/common');
6
var strings = require('./utils/strings');
7
var c = require('./zlib/constants');
8
var msg = require('./zlib/messages');
9
var zstream = require('./zlib/zstream');
10
var gzheader = require('./zlib/gzheader');
11
12
var toString = Object.prototype.toString;
13
14
/**
15
* class Inflate
16
*
17
* Generic JS-style wrapper for zlib calls. If you don't need
18
* streaming behaviour - use more simple functions: [[inflate]]
19
* and [[inflateRaw]].
20
**/
21
22
/* internal
23
* inflate.chunks -> Array
24
*
25
* Chunks of output data, if [[Inflate#onData]] not overriden.
26
**/
27
28
/**
29
* Inflate.result -> Uint8Array|Array|String
30
*
31
* Uncompressed result, generated by default [[Inflate#onData]]
32
* and [[Inflate#onEnd]] handlers. Filled after you push last chunk
33
* (call [[Inflate#push]] with `Z_FINISH` / `true` param).
34
**/
35
36
/**
37
* Inflate.err -> Number
38
*
39
* Error code after inflate finished. 0 (Z_OK) on success.
40
* Should be checked if broken data possible.
41
**/
42
43
/**
44
* Inflate.msg -> String
45
*
46
* Error message, if [[Inflate.err]] != 0
47
**/
48
49
50
/**
51
* new Inflate(options)
52
* - options (Object): zlib inflate options.
53
*
54
* Creates new inflator instance with specified params. Throws exception
55
* on bad params. Supported options:
56
*
57
* - `windowBits`
58
*
59
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
60
* for more information on these.
61
*
62
* Additional options, for internal needs:
63
*
64
* - `chunkSize` - size of generated data chunks (16K by default)
65
* - `raw` (Boolean) - do raw inflate
66
* - `to` (String) - if equal to 'string', then result will be converted
67
* from utf8 to utf16 (javascript) string. When string output requested,
68
* chunk length can differ from `chunkSize`, depending on content.
69
*
70
* By default, when no options set, autodetect deflate/gzip data format via
71
* wrapper header.
72
*
73
* ##### Example:
74
*
75
* ```javascript
76
* var pako = require('pako')
77
* , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
78
* , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
79
*
80
* var inflate = new pako.Inflate({ level: 3});
81
*
82
* inflate.push(chunk1, false);
83
* inflate.push(chunk2, true); // true -> last chunk
84
*
85
* if (inflate.err) { throw new Error(inflate.err); }
86
*
87
* console.log(inflate.result);
88
* ```
89
**/
90
var Inflate = function(options) {
91
92
this.options = utils.assign({
93
chunkSize: 16384,
94
windowBits: 0,
95
to: ''
96
}, options || {});
97
98
var opt = this.options;
99
100
// Force window size for `raw` data, if not set directly,
101
// because we have no header for autodetect.
102
if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
103
opt.windowBits = -opt.windowBits;
104
if (opt.windowBits === 0) { opt.windowBits = -15; }
105
}
106
107
// If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
108
if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
109
!(options && options.windowBits)) {
110
opt.windowBits += 32;
111
}
112
113
// Gzip header has no info about windows size, we can do autodetect only
114
// for deflate. So, if window size not set, force it to max when gzip possible
115
if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
116
// bit 3 (16) -> gzipped data
117
// bit 4 (32) -> autodetect gzip/deflate
118
if ((opt.windowBits & 15) === 0) {
119
opt.windowBits |= 15;
120
}
121
}
122
123
this.err = 0; // error code, if happens (0 = Z_OK)
124
this.msg = ''; // error message
125
this.ended = false; // used to avoid multiple onEnd() calls
126
this.chunks = []; // chunks of compressed data
127
128
this.strm = new zstream();
129
this.strm.avail_out = 0;
130
131
var status = zlib_inflate.inflateInit2(
132
this.strm,
133
opt.windowBits
134
);
135
136
if (status !== c.Z_OK) {
137
throw new Error(msg[status]);
138
}
139
140
this.header = new gzheader();
141
142
zlib_inflate.inflateGetHeader(this.strm, this.header);
143
};
144
145
/**
146
* Inflate#push(data[, mode]) -> Boolean
147
* - data (Uint8Array|Array|ArrayBuffer|String): input data
148
* - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
149
* See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
150
*
151
* Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
152
* new output chunks. Returns `true` on success. The last data block must have
153
* mode Z_FINISH (or `true`). That flush internal pending buffers and call
154
* [[Inflate#onEnd]].
155
*
156
* On fail call [[Inflate#onEnd]] with error code and return false.
157
*
158
* We strongly recommend to use `Uint8Array` on input for best speed (output
159
* format is detected automatically). Also, don't skip last param and always
160
* use the same type in your code (boolean or number). That will improve JS speed.
161
*
162
* For regular `Array`-s make sure all elements are [0..255].
163
*
164
* ##### Example
165
*
166
* ```javascript
167
* push(chunk, false); // push one of data chunks
168
* ...
169
* push(chunk, true); // push last chunk
170
* ```
171
**/
172
Inflate.prototype.push = function(data, mode) {
173
var strm = this.strm;
174
var chunkSize = this.options.chunkSize;
175
var status, _mode;
176
var next_out_utf8, tail, utf8str;
177
178
if (this.ended) { return false; }
179
_mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
180
181
// Convert data if needed
182
if (typeof data === 'string') {
183
// Only binary strings can be decompressed on practice
184
strm.input = strings.binstring2buf(data);
185
} else if (toString.call(data) === '[object ArrayBuffer]') {
186
strm.input = new Uint8Array(data);
187
} else {
188
strm.input = data;
189
}
190
191
strm.next_in = 0;
192
strm.avail_in = strm.input.length;
193
194
do {
195
if (strm.avail_out === 0) {
196
strm.output = new utils.Buf8(chunkSize);
197
strm.next_out = 0;
198
strm.avail_out = chunkSize;
199
}
200
201
status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */
202
203
if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
204
this.onEnd(status);
205
this.ended = true;
206
return false;
207
}
208
209
if (strm.next_out) {
210
if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && _mode === c.Z_FINISH)) {
211
212
if (this.options.to === 'string') {
213
214
next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
215
216
tail = strm.next_out - next_out_utf8;
217
utf8str = strings.buf2string(strm.output, next_out_utf8);
218
219
// move tail
220
strm.next_out = tail;
221
strm.avail_out = chunkSize - tail;
222
if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); }
223
224
this.onData(utf8str);
225
226
} else {
227
this.onData(utils.shrinkBuf(strm.output, strm.next_out));
228
}
229
}
230
}
231
} while ((strm.avail_in > 0) && status !== c.Z_STREAM_END);
232
233
if (status === c.Z_STREAM_END) {
234
_mode = c.Z_FINISH;
235
}
236
// Finalize on the last chunk.
237
if (_mode === c.Z_FINISH) {
238
status = zlib_inflate.inflateEnd(this.strm);
239
this.onEnd(status);
240
this.ended = true;
241
return status === c.Z_OK;
242
}
243
244
return true;
245
};
246
247
248
/**
249
* Inflate#onData(chunk) -> Void
250
* - chunk (Uint8Array|Array|String): ouput data. Type of array depends
251
* on js engine support. When string output requested, each chunk
252
* will be string.
253
*
254
* By default, stores data blocks in `chunks[]` property and glue
255
* those in `onEnd`. Override this handler, if you need another behaviour.
256
**/
257
Inflate.prototype.onData = function(chunk) {
258
this.chunks.push(chunk);
259
};
260
261
262
/**
263
* Inflate#onEnd(status) -> Void
264
* - status (Number): inflate status. 0 (Z_OK) on success,
265
* other if not.
266
*
267
* Called once after you tell inflate that input stream complete
268
* or error happenned. By default - join collected chunks,
269
* free memory and fill `results` / `err` properties.
270
**/
271
Inflate.prototype.onEnd = function(status) {
272
// On success - join
273
if (status === c.Z_OK) {
274
if (this.options.to === 'string') {
275
// Glue & convert here, until we teach pako to send
276
// utf8 alligned strings to onData
277
this.result = this.chunks.join('');
278
} else {
279
this.result = utils.flattenChunks(this.chunks);
280
}
281
}
282
this.chunks = [];
283
this.err = status;
284
this.msg = this.strm.msg;
285
};
286
287
288
/**
289
* inflate(data[, options]) -> Uint8Array|Array|String
290
* - data (Uint8Array|Array|String): input data to decompress.
291
* - options (Object): zlib inflate options.
292
*
293
* Decompress `data` with inflate/ungzip and `options`. Autodetect
294
* format via wrapper header by default. That's why we don't provide
295
* separate `ungzip` method.
296
*
297
* Supported options are:
298
*
299
* - windowBits
300
*
301
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
302
* for more information.
303
*
304
* Sugar (options):
305
*
306
* - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
307
* negative windowBits implicitly.
308
* - `to` (String) - if equal to 'string', then result will be converted
309
* from utf8 to utf16 (javascript) string. When string output requested,
310
* chunk length can differ from `chunkSize`, depending on content.
311
*
312
*
313
* ##### Example:
314
*
315
* ```javascript
316
* var pako = require('pako')
317
* , input = pako.deflate([1,2,3,4,5,6,7,8,9])
318
* , output;
319
*
320
* try {
321
* output = pako.inflate(input);
322
* } catch (err)
323
* console.log(err);
324
* }
325
* ```
326
**/
327
function inflate(input, options) {
328
var inflator = new Inflate(options);
329
330
inflator.push(input, true);
331
332
// That will never happens, if you don't cheat with options :)
333
if (inflator.err) { throw inflator.msg; }
334
335
return inflator.result;
336
}
337
338
339
/**
340
* inflateRaw(data[, options]) -> Uint8Array|Array|String
341
* - data (Uint8Array|Array|String): input data to decompress.
342
* - options (Object): zlib inflate options.
343
*
344
* The same as [[inflate]], but creates raw data, without wrapper
345
* (header and adler32 crc).
346
**/
347
function inflateRaw(input, options) {
348
options = options || {};
349
options.raw = true;
350
return inflate(input, options);
351
}
352
353
354
/**
355
* ungzip(data[, options]) -> Uint8Array|Array|String
356
* - data (Uint8Array|Array|String): input data to decompress.
357
* - options (Object): zlib inflate options.
358
*
359
* Just shortcut to [[inflate]], because it autodetects format
360
* by header.content. Done for convenience.
361
**/
362
363
364
exports.Inflate = Inflate;
365
exports.inflate = inflate;
366
exports.inflateRaw = inflateRaw;
367
exports.ungzip = inflate;
368
369