pako/lib/inflate.js

401 wiersze
11 KiB
JavaScript
Czysty Zwykły widok Historia

2014-02-03 15:25:40 +00:00
'use strict';
2014-02-03 14:00:28 +00:00
2014-02-20 14:55:09 +00:00
var zlib_inflate = require('./zlib/inflate.js');
2014-04-13 09:45:12 +00:00
var utils = require('./utils/common');
var strings = require('./utils/strings');
2014-02-20 14:55:09 +00:00
var c = require('./zlib/constants');
var msg = require('./zlib/messages');
var zstream = require('./zlib/zstream');
var gzheader = require('./zlib/gzheader');
2014-02-03 14:00:28 +00:00
2015-03-24 00:46:30 +00:00
var toString = Object.prototype.toString;
2014-02-03 14:00:28 +00:00
2014-02-20 14:55:09 +00:00
/**
* class Inflate
*
* Generic JS-style wrapper for zlib calls. If you don't need
* streaming behaviour - use more simple functions: [[inflate]]
* and [[inflateRaw]].
**/
2014-02-03 14:00:28 +00:00
2014-02-20 14:55:09 +00:00
/* internal
* inflate.chunks -> Array
*
* Chunks of output data, if [[Inflate#onData]] not overriden.
**/
2014-02-03 14:00:28 +00:00
2014-02-20 14:55:09 +00:00
/**
2014-04-08 14:55:17 +00:00
* Inflate.result -> Uint8Array|Array|String
2014-02-20 14:55:09 +00:00
*
* Uncompressed result, generated by default [[Inflate#onData]]
* and [[Inflate#onEnd]] handlers. Filled after you push last chunk
* (call [[Inflate#push]] with `Z_FINISH` / `true` param) or if you
* push a chunk with explicit flush (call [[Inflate#push]] with
* `Z_SYNC_FLUSH` param).
2014-02-20 14:55:09 +00:00
**/
2014-02-03 15:25:40 +00:00
2014-02-20 14:55:09 +00:00
/**
* Inflate.err -> Number
*
* Error code after inflate finished. 0 (Z_OK) on success.
* Should be checked if broken data possible.
**/
2014-02-03 14:00:28 +00:00
2014-02-20 17:29:22 +00:00
/**
* Inflate.msg -> String
*
* Error message, if [[Inflate.err]] != 0
**/
2014-02-03 14:00:28 +00:00
2014-02-20 14:55:09 +00:00
/**
* new Inflate(options)
* - options (Object): zlib inflate options.
*
2014-02-20 17:29:22 +00:00
* Creates new inflator instance with specified params. Throws exception
* on bad params. Supported options:
2014-02-20 14:55:09 +00:00
*
* - `windowBits`
*
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
* for more information on these.
*
* Additional options, for internal needs:
*
* - `chunkSize` - size of generated data chunks (16K by default)
2014-04-08 14:55:17 +00:00
* - `raw` (Boolean) - do raw inflate
* - `to` (String) - if equal to 'string', then result will be converted
* from utf8 to utf16 (javascript) string. When string output requested,
* chunk length can differ from `chunkSize`, depending on content.
2014-02-20 14:55:09 +00:00
*
2014-03-13 22:04:31 +00:00
* By default, when no options set, autodetect deflate/gzip data format via
* wrapper header.
*
2014-02-20 14:55:09 +00:00
* ##### Example:
*
* ```javascript
* var pako = require('pako')
* , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
* , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
*
* var inflate = new pako.Inflate({ level: 3});
*
* inflate.push(chunk1, false);
* inflate.push(chunk2, true); // true -> last chunk
*
* if (inflate.err) { throw new Error(inflate.err); }
*
* console.log(inflate.result);
* ```
**/
var Inflate = function(options) {
2014-02-03 14:00:28 +00:00
2014-02-20 14:55:09 +00:00
this.options = utils.assign({
chunkSize: 16384,
2014-04-08 14:55:17 +00:00
windowBits: 0,
to: ''
2014-02-20 14:55:09 +00:00
}, options || {});
2014-02-03 14:00:28 +00:00
2014-02-20 14:55:09 +00:00
var opt = this.options;
2014-03-13 22:04:31 +00:00
// Force window size for `raw` data, if not set directly,
// because we have no header for autodetect.
if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
2014-02-20 14:55:09 +00:00
opt.windowBits = -opt.windowBits;
if (opt.windowBits === 0) { opt.windowBits = -15; }
2014-02-20 14:55:09 +00:00
}
2014-02-03 14:00:28 +00:00
2014-03-11 22:46:29 +00:00
// If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
2014-03-11 22:46:29 +00:00
!(options && options.windowBits)) {
opt.windowBits += 32;
}
// Gzip header has no info about windows size, we can do autodetect only
// for deflate. So, if window size not set, force it to max when gzip possible
if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
// bit 3 (16) -> gzipped data
// bit 4 (32) -> autodetect gzip/deflate
if ((opt.windowBits & 15) === 0) {
opt.windowBits |= 15;
}
}
2014-02-20 17:29:22 +00:00
this.err = 0; // error code, if happens (0 = Z_OK)
this.msg = ''; // error message
this.ended = false; // used to avoid multiple onEnd() calls
this.chunks = []; // chunks of compressed data
2014-02-03 14:00:28 +00:00
2014-02-20 17:29:22 +00:00
this.strm = new zstream();
2014-04-08 21:47:38 +00:00
this.strm.avail_out = 0;
2014-02-20 14:55:09 +00:00
2014-02-20 17:29:22 +00:00
var status = zlib_inflate.inflateInit2(
2014-02-20 14:55:09 +00:00
this.strm,
opt.windowBits
);
if (status !== c.Z_OK) {
throw new Error(msg[status]);
}
this.header = new gzheader();
zlib_inflate.inflateGetHeader(this.strm, this.header);
2014-02-03 15:25:40 +00:00
};
2014-02-03 14:00:28 +00:00
2014-02-20 14:55:09 +00:00
/**
* Inflate#push(data[, mode]) -> Boolean
2015-03-24 00:46:30 +00:00
* - data (Uint8Array|Array|ArrayBuffer|String): input data
2014-02-20 14:55:09 +00:00
* - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
* See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
*
* Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
* new output chunks. Returns `true` on success. The last data block must have
* mode Z_FINISH (or `true`). That will flush internal pending buffers and call
* [[Inflate#onEnd]]. For interim explicit flushes (without ending the stream) you
* can use mode Z_SYNC_FLUSH, keeping the decompression context.
2014-02-20 14:55:09 +00:00
*
* On fail call [[Inflate#onEnd]] with error code and return false.
*
* We strongly recommend to use `Uint8Array` on input for best speed (output
* format is detected automatically). Also, don't skip last param and always
* use the same type in your code (boolean or number). That will improve JS speed.
*
* For regular `Array`-s make sure all elements are [0..255].
*
* ##### Example
*
* ```javascript
* push(chunk, false); // push one of data chunks
* ...
* push(chunk, true); // push last chunk
* ```
**/
Inflate.prototype.push = function(data, mode) {
var strm = this.strm;
var chunkSize = this.options.chunkSize;
var status, _mode;
var next_out_utf8, tail, utf8str;
2015-09-14 12:01:53 +00:00
// Flag to properly process Z_BUF_ERROR on testing inflate call
// when we check that all output data was flushed.
var allowBufError = false;
2014-02-03 15:25:40 +00:00
2014-02-20 14:55:09 +00:00
if (this.ended) { return false; }
2014-04-08 21:47:38 +00:00
_mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
2014-02-03 14:00:28 +00:00
2014-04-08 14:55:17 +00:00
// Convert data if needed
if (typeof data === 'string') {
// Only binary strings can be decompressed on practice
strm.input = strings.binstring2buf(data);
2015-03-24 00:46:30 +00:00
} else if (toString.call(data) === '[object ArrayBuffer]') {
strm.input = new Uint8Array(data);
2014-04-08 14:55:17 +00:00
} else {
strm.input = data;
2014-04-08 14:55:17 +00:00
}
strm.next_in = 0;
strm.avail_in = strm.input.length;
2014-02-03 15:25:40 +00:00
2014-02-20 14:55:09 +00:00
do {
if (strm.avail_out === 0) {
strm.output = new utils.Buf8(chunkSize);
strm.next_out = 0;
strm.avail_out = chunkSize;
}
2014-04-08 21:47:38 +00:00
status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */
2014-02-20 14:55:09 +00:00
2015-09-14 12:01:53 +00:00
if (status === c.Z_BUF_ERROR && allowBufError === true) {
status = c.Z_OK;
2015-09-14 12:01:53 +00:00
allowBufError = false;
}
2014-02-20 14:55:09 +00:00
if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
this.onEnd(status);
this.ended = true;
return false;
}
2014-04-08 14:55:17 +00:00
if (strm.next_out) {
if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && (_mode === c.Z_FINISH || _mode === c.Z_SYNC_FLUSH))) {
2014-04-08 14:55:17 +00:00
2014-04-13 17:30:00 +00:00
if (this.options.to === 'string') {
2014-04-08 14:55:17 +00:00
next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
2014-04-08 14:55:17 +00:00
tail = strm.next_out - next_out_utf8;
utf8str = strings.buf2string(strm.output, next_out_utf8);
2014-04-08 14:55:17 +00:00
// move tail
strm.next_out = tail;
2014-04-08 14:55:17 +00:00
strm.avail_out = chunkSize - tail;
if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); }
2014-04-08 14:55:17 +00:00
this.onData(utf8str);
} else {
this.onData(utils.shrinkBuf(strm.output, strm.next_out));
2014-04-13 17:30:00 +00:00
}
2014-02-20 14:55:09 +00:00
}
2014-02-03 15:25:40 +00:00
}
2015-09-14 12:01:53 +00:00
// When no more input data, we should check that internal inflate buffers
// are flushed. The only way to do it when avail_out = 0 - run one more
// inflate pass. But if output data not exists, inflate return Z_BUF_ERROR.
// Here we set flag to process this error properly.
//
// NOTE. Deflate does not return error in this case and does not needs such
// logic.
if (strm.avail_in === 0 && strm.avail_out === 0) {
2015-09-14 12:01:53 +00:00
allowBufError = true;
}
2015-09-14 12:01:53 +00:00
} while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== c.Z_STREAM_END);
2014-02-03 15:25:40 +00:00
2014-04-04 15:50:40 +00:00
if (status === c.Z_STREAM_END) {
_mode = c.Z_FINISH;
}
2014-02-20 14:55:09 +00:00
// Finalize on the last chunk.
if (_mode === c.Z_FINISH) {
status = zlib_inflate.inflateEnd(this.strm);
this.onEnd(status);
this.ended = true;
return status === c.Z_OK;
}
2014-02-03 15:25:40 +00:00
// callback interim results if Z_SYNC_FLUSH.
if (_mode === c.Z_SYNC_FLUSH) {
this.onEnd(c.Z_OK);
strm.avail_out = 0;
return true;
}
2014-02-20 14:55:09 +00:00
return true;
};
/**
* Inflate#onData(chunk) -> Void
2014-04-08 14:55:17 +00:00
* - chunk (Uint8Array|Array|String): ouput data. Type of array depends
* on js engine support. When string output requested, each chunk
* will be string.
2014-02-20 14:55:09 +00:00
*
* By default, stores data blocks in `chunks[]` property and glue
* those in `onEnd`. Override this handler, if you need another behaviour.
**/
Inflate.prototype.onData = function(chunk) {
this.chunks.push(chunk);
};
/**
* Inflate#onEnd(status) -> Void
* - status (Number): inflate status. 0 (Z_OK) on success,
* other if not.
*
* Called either after you tell inflate that the input stream is
* complete (Z_FINISH) or should be flushed (Z_SYNC_FLUSH)
* or if an error happened. By default - join collected chunks,
2014-02-20 14:55:09 +00:00
* free memory and fill `results` / `err` properties.
**/
Inflate.prototype.onEnd = function(status) {
// On success - join
if (status === c.Z_OK) {
2014-04-08 14:55:17 +00:00
if (this.options.to === 'string') {
// Glue & convert here, until we teach pako to send
// utf8 alligned strings to onData
2014-04-13 17:30:00 +00:00
this.result = this.chunks.join('');
2014-04-08 14:55:17 +00:00
} else {
this.result = utils.flattenChunks(this.chunks);
}
2014-02-20 14:55:09 +00:00
}
this.chunks = [];
this.err = status;
2014-03-13 22:04:31 +00:00
this.msg = this.strm.msg;
2014-02-20 14:55:09 +00:00
};
2014-02-03 15:25:40 +00:00
2014-02-20 14:55:09 +00:00
/**
2014-04-08 14:55:17 +00:00
* inflate(data[, options]) -> Uint8Array|Array|String
2014-05-17 16:41:40 +00:00
* - data (Uint8Array|Array|String): input data to decompress.
2014-02-20 14:55:09 +00:00
* - options (Object): zlib inflate options.
*
2014-03-13 22:04:31 +00:00
* Decompress `data` with inflate/ungzip and `options`. Autodetect
* format via wrapper header by default. That's why we don't provide
* separate `ungzip` method.
2014-02-20 14:55:09 +00:00
*
* Supported options are:
*
* - windowBits
*
* [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
2014-03-13 22:04:31 +00:00
* for more information.
*
* Sugar (options):
*
2014-04-08 14:55:17 +00:00
* - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
2014-03-13 22:04:31 +00:00
* negative windowBits implicitly.
2014-04-08 14:55:17 +00:00
* - `to` (String) - if equal to 'string', then result will be converted
* from utf8 to utf16 (javascript) string. When string output requested,
* chunk length can differ from `chunkSize`, depending on content.
2014-03-13 22:04:31 +00:00
*
2014-02-20 14:55:09 +00:00
*
* ##### Example:
*
* ```javascript
* var pako = require('pako')
2014-03-12 01:01:30 +00:00
* , input = pako.deflate([1,2,3,4,5,6,7,8,9])
* , output;
*
* try {
* output = pako.inflate(input);
* } catch (err)
* console.log(err);
* }
2014-02-20 14:55:09 +00:00
* ```
**/
function inflate(input, options) {
var inflator = new Inflate(options);
inflator.push(input, true);
// That will never happens, if you don't cheat with options :)
2014-03-13 22:04:31 +00:00
if (inflator.err) { throw inflator.msg; }
2014-02-20 14:55:09 +00:00
return inflator.result;
2014-02-03 14:00:28 +00:00
}
2014-02-20 14:55:09 +00:00
/**
2014-04-08 14:55:17 +00:00
* inflateRaw(data[, options]) -> Uint8Array|Array|String
2014-05-17 16:41:40 +00:00
* - data (Uint8Array|Array|String): input data to decompress.
2014-02-20 14:55:09 +00:00
* - options (Object): zlib inflate options.
*
* The same as [[inflate]], but creates raw data, without wrapper
* (header and adler32 crc).
**/
function inflateRaw(input, options) {
2014-02-03 15:25:40 +00:00
options = options || {};
options.raw = true;
return inflate(input, options);
}
2014-02-03 14:00:28 +00:00
2014-02-20 14:55:09 +00:00
2014-04-08 14:55:17 +00:00
/**
* ungzip(data[, options]) -> Uint8Array|Array|String
2014-05-17 16:41:40 +00:00
* - data (Uint8Array|Array|String): input data to decompress.
2014-04-08 14:55:17 +00:00
* - options (Object): zlib inflate options.
*
* Just shortcut to [[inflate]], because it autodetects format
* by header.content. Done for convenience.
**/
2014-02-03 15:25:40 +00:00
exports.Inflate = Inflate;
exports.inflate = inflate;
2014-02-20 14:55:09 +00:00
exports.inflateRaw = inflateRaw;
2014-04-08 14:55:17 +00:00
exports.ungzip = inflate;