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');
12
var toString = Object.prototype.toString;
17
* Generic JS-style wrapper for zlib calls. If you don't need
18
* streaming behaviour - use more simple functions: [[inflate]]
23
* inflate.chunks -> Array
25
* Chunks of output data, if [[Inflate#onData]] not overriden.
29
* Inflate.result -> Uint8Array|Array|String
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) or if you
34
* push a chunk with explicit flush (call [[Inflate#push]] with
35
* `Z_SYNC_FLUSH` param).
39
* Inflate.err -> Number
41
* Error code after inflate finished. 0 (Z_OK) on success.
42
* Should be checked if broken data possible.
46
* Inflate.msg -> String
48
* Error message, if [[Inflate.err]] != 0
53
* new Inflate(options)
54
* - options (Object): zlib inflate options.
56
* Creates new inflator instance with specified params. Throws exception
57
* on bad params. Supported options:
61
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
62
* for more information on these.
64
* Additional options, for internal needs:
66
* - `chunkSize` - size of generated data chunks (16K by default)
67
* - `raw` (Boolean) - do raw inflate
68
* - `to` (String) - if equal to 'string', then result will be converted
69
* from utf8 to utf16 (javascript) string. When string output requested,
70
* chunk length can differ from `chunkSize`, depending on content.
72
* By default, when no options set, autodetect deflate/gzip data format via
78
* var pako = require('pako')
79
* , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
80
* , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
82
* var inflate = new pako.Inflate({ level: 3});
84
* inflate.push(chunk1, false);
85
* inflate.push(chunk2, true); // true -> last chunk
87
* if (inflate.err) { throw new Error(inflate.err); }
89
* console.log(inflate.result);
92
var Inflate = function(options) {
94
this.options = utils.assign({
100
var opt = this.options;
102
// Force window size for `raw` data, if not set directly,
103
// because we have no header for autodetect.
104
if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
105
opt.windowBits = -opt.windowBits;
106
if (opt.windowBits === 0) { opt.windowBits = -15; }
109
// If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
110
if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
111
!(options && options.windowBits)) {
112
opt.windowBits += 32;
115
// Gzip header has no info about windows size, we can do autodetect only
116
// for deflate. So, if window size not set, force it to max when gzip possible
117
if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
118
// bit 3 (16) -> gzipped data
119
// bit 4 (32) -> autodetect gzip/deflate
120
if ((opt.windowBits & 15) === 0) {
121
opt.windowBits |= 15;
125
this.err = 0; // error code, if happens (0 = Z_OK)
126
this.msg = ''; // error message
127
this.ended = false; // used to avoid multiple onEnd() calls
128
this.chunks = []; // chunks of compressed data
130
this.strm = new zstream();
131
this.strm.avail_out = 0;
133
var status = zlib_inflate.inflateInit2(
138
if (status !== c.Z_OK) {
139
throw new Error(msg[status]);
142
this.header = new gzheader();
144
zlib_inflate.inflateGetHeader(this.strm, this.header);
148
* Inflate#push(data[, mode]) -> Boolean
149
* - data (Uint8Array|Array|ArrayBuffer|String): input data
150
* - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
151
* See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
153
* Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
154
* new output chunks. Returns `true` on success. The last data block must have
155
* mode Z_FINISH (or `true`). That will flush internal pending buffers and call
156
* [[Inflate#onEnd]]. For interim explicit flushes (without ending the stream) you
157
* can use mode Z_SYNC_FLUSH, keeping the decompression context.
159
* On fail call [[Inflate#onEnd]] with error code and return false.
161
* We strongly recommend to use `Uint8Array` on input for best speed (output
162
* format is detected automatically). Also, don't skip last param and always
163
* use the same type in your code (boolean or number). That will improve JS speed.
165
* For regular `Array`-s make sure all elements are [0..255].
170
* push(chunk, false); // push one of data chunks
172
* push(chunk, true); // push last chunk
175
Inflate.prototype.push = function(data, mode) {
176
var strm = this.strm;
177
var chunkSize = this.options.chunkSize;
179
var next_out_utf8, tail, utf8str;
181
// Flag to properly process Z_BUF_ERROR on testing inflate call
182
// when we check that all output data was flushed.
183
var allowBufError = false;
185
if (this.ended) { return false; }
186
_mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
188
// Convert data if needed
189
if (typeof data === 'string') {
190
// Only binary strings can be decompressed on practice
191
strm.input = strings.binstring2buf(data);
192
} else if (toString.call(data) === '[object ArrayBuffer]') {
193
strm.input = new Uint8Array(data);
199
strm.avail_in = strm.input.length;
202
if (strm.avail_out === 0) {
203
strm.output = new utils.Buf8(chunkSize);
205
strm.avail_out = chunkSize;
208
status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */
210
if (status === c.Z_BUF_ERROR && allowBufError === true) {
212
allowBufError = false;
215
if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
222
if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && (_mode === c.Z_FINISH || _mode === c.Z_SYNC_FLUSH))) {
224
if (this.options.to === 'string') {
226
next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
228
tail = strm.next_out - next_out_utf8;
229
utf8str = strings.buf2string(strm.output, next_out_utf8);
232
strm.next_out = tail;
233
strm.avail_out = chunkSize - tail;
234
if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); }
236
this.onData(utf8str);
239
this.onData(utils.shrinkBuf(strm.output, strm.next_out));
244
// When no more input data, we should check that internal inflate buffers
245
// are flushed. The only way to do it when avail_out = 0 - run one more
246
// inflate pass. But if output data not exists, inflate return Z_BUF_ERROR.
247
// Here we set flag to process this error properly.
249
// NOTE. Deflate does not return error in this case and does not needs such
251
if (strm.avail_in === 0 && strm.avail_out === 0) {
252
allowBufError = true;
255
} while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== c.Z_STREAM_END);
257
if (status === c.Z_STREAM_END) {
261
// Finalize on the last chunk.
262
if (_mode === c.Z_FINISH) {
263
status = zlib_inflate.inflateEnd(this.strm);
266
return status === c.Z_OK;
269
// callback interim results if Z_SYNC_FLUSH.
270
if (_mode === c.Z_SYNC_FLUSH) {
281
* Inflate#onData(chunk) -> Void
282
* - chunk (Uint8Array|Array|String): ouput data. Type of array depends
283
* on js engine support. When string output requested, each chunk
286
* By default, stores data blocks in `chunks[]` property and glue
287
* those in `onEnd`. Override this handler, if you need another behaviour.
289
Inflate.prototype.onData = function(chunk) {
290
this.chunks.push(chunk);
295
* Inflate#onEnd(status) -> Void
296
* - status (Number): inflate status. 0 (Z_OK) on success,
299
* Called either after you tell inflate that the input stream is
300
* complete (Z_FINISH) or should be flushed (Z_SYNC_FLUSH)
301
* or if an error happened. By default - join collected chunks,
302
* free memory and fill `results` / `err` properties.
304
Inflate.prototype.onEnd = function(status) {
306
if (status === c.Z_OK) {
307
if (this.options.to === 'string') {
308
// Glue & convert here, until we teach pako to send
309
// utf8 alligned strings to onData
310
this.result = this.chunks.join('');
312
this.result = utils.flattenChunks(this.chunks);
317
this.msg = this.strm.msg;
322
* inflate(data[, options]) -> Uint8Array|Array|String
323
* - data (Uint8Array|Array|String): input data to decompress.
324
* - options (Object): zlib inflate options.
326
* Decompress `data` with inflate/ungzip and `options`. Autodetect
327
* format via wrapper header by default. That's why we don't provide
328
* separate `ungzip` method.
330
* Supported options are:
334
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
335
* for more information.
339
* - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
340
* negative windowBits implicitly.
341
* - `to` (String) - if equal to 'string', then result will be converted
342
* from utf8 to utf16 (javascript) string. When string output requested,
343
* chunk length can differ from `chunkSize`, depending on content.
349
* var pako = require('pako')
350
* , input = pako.deflate([1,2,3,4,5,6,7,8,9])
354
* output = pako.inflate(input);
360
function inflate(input, options) {
361
var inflator = new Inflate(options);
363
inflator.push(input, true);
365
// That will never happens, if you don't cheat with options :)
366
if (inflator.err) { throw inflator.msg; }
368
return inflator.result;
373
* inflateRaw(data[, options]) -> Uint8Array|Array|String
374
* - data (Uint8Array|Array|String): input data to decompress.
375
* - options (Object): zlib inflate options.
377
* The same as [[inflate]], but creates raw data, without wrapper
378
* (header and adler32 crc).
380
function inflateRaw(input, options) {
381
options = options || {};
383
return inflate(input, options);
388
* ungzip(data[, options]) -> Uint8Array|Array|String
389
* - data (Uint8Array|Array|String): input data to decompress.
390
* - options (Object): zlib inflate options.
392
* Just shortcut to [[inflate]], because it autodetects format
393
* by header.content. Done for convenience.
397
exports.Inflate = Inflate;
398
exports.inflate = inflate;
399
exports.inflateRaw = inflateRaw;
400
exports.ungzip = inflate;