Reorganized constants - move locally as much as possible

master
Vitaly Puzrin 2014-02-24 06:17:36 +04:00
rodzic 1c008e7901
commit e78abad260
4 zmienionych plików z 166 dodań i 74 usunięć

Wyświetl plik

@ -3,10 +3,28 @@
var zlib_deflate = require('./zlib/deflate.js');
var utils = require('./zlib/utils');
var c = require('./zlib/constants');
var msg = require('./zlib/messages');
var zstream = require('./zlib/zstream');
/* Public constants ==========================================================*/
/* ===========================================================================*/
var Z_NO_FLUSH = 0;
var Z_FINISH = 4;
var Z_OK = 0;
var Z_STREAM_END = 1;
var Z_DEFAULT_COMPRESSION = -1;
var Z_DEFAULT_STRATEGY = 0;
var Z_DEFLATED = 8;
/* ===========================================================================*/
// return sliced buffer, trying to avoid new objects creation and mem copy
function sliceBuf(buf, size) {
if (buf.length === size) { return buf; }
@ -93,12 +111,12 @@ function sliceBuf(buf, size) {
var Deflate = function(options) {
this.options = utils.assign({
level: c.Z_DEFAULT_COMPRESSION,
method: c.Z_DEFLATED,
level: Z_DEFAULT_COMPRESSION,
method: Z_DEFLATED,
chunkSize: 16384,
windowBits: 15,
memLevel: 8,
strategy: c.Z_DEFAULT_STRATEGY
strategy: Z_DEFAULT_STRATEGY
}, options || {});
var opt = this.options;
@ -127,7 +145,7 @@ var Deflate = function(options) {
opt.strategy
);
if (status !== c.Z_OK) {
if (status !== Z_OK) {
throw new Error(msg[status]);
}
};
@ -166,7 +184,7 @@ Deflate.prototype.push = function(data, mode) {
if (this.ended) { return false; }
_mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
_mode = (mode === ~~mode) ? mode : ((mode === true) ? Z_FINISH : Z_NO_FLUSH);
strm.next_in = data;
strm.next_in_index = 0;
@ -178,7 +196,7 @@ Deflate.prototype.push = function(data, mode) {
strm.next_out_index = 0;
status = zlib_deflate.deflate(strm, _mode); /* no bad return value */
if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
if (status !== Z_STREAM_END && status !== Z_OK) {
this.onEnd(status);
this.ended = true;
return false;
@ -193,11 +211,11 @@ Deflate.prototype.push = function(data, mode) {
} while (strm.avail_in > 0 || strm.avail_out === 0);
// Finalize on the last chunk.
if (_mode === c.Z_FINISH) {
if (_mode === Z_FINISH) {
status = zlib_deflate.deflateEnd(this.strm);
this.onEnd(status);
this.ended = true;
return status === c.Z_OK;
return status === Z_OK;
}
return true;
@ -228,7 +246,7 @@ Deflate.prototype.onData = function(chunk) {
**/
Deflate.prototype.onEnd = function(status) {
// On success - join
if (status === c.Z_OK) {
if (status === Z_OK) {
this.result = utils.flattenChunks(this.chunks);
}
this.chunks = [];

Wyświetl plik

@ -1,4 +1,6 @@
module.exports = {
/* Allowed flush values; see deflate() and inflate() below for details */
Z_NO_FLUSH: 0,
Z_PARTIAL_FLUSH: 1,
Z_SYNC_FLUSH: 2,
@ -7,7 +9,9 @@ module.exports = {
Z_BLOCK: 5,
Z_TREES: 6,
/* Allowed flush values; see deflate() and inflate() below for details */
/* Return codes for the compression/decompression functions. Negative values
* are errors, positive values are used for special but normal events.
*/
Z_OK: 0,
Z_STREAM_END: 1,
Z_NEED_DICT: 2,
@ -17,27 +21,27 @@ module.exports = {
Z_MEM_ERROR: (-4),
Z_BUF_ERROR: (-5),
Z_VERSION_ERROR: (-6),
/* Return codes for the compression/decompression functions. Negative values
* are errors, positive values are used for special but normal events.
*/
/* compression levels */
Z_NO_COMPRESSION: 0,
Z_BEST_SPEED: 1,
Z_BEST_COMPRESSION: 9,
Z_DEFAULT_COMPRESSION: -1,
/* compression levels */
Z_FILTERED: 1,
Z_HUFFMAN_ONLY: 2,
Z_RLE: 3,
Z_FIXED: 4,
Z_DEFAULT_STRATEGY: 0,
/* Possible values of the data_type field (though see inflate()) */
Z_BINARY: 0,
Z_TEXT: 1,
Z_ASCII: 1, // = Z_TEXT
//Z_ASCII: 1, // = Z_TEXT (deprecated)
Z_UNKNOWN: 2,
/* Possible values of the data_type field (though see inflate()) */
Z_DEFLATED: 8,
/* The deflate compression method */
Z_NULL: -1
Z_DEFLATED: 8,
//Z_NULL: null // Use -1 or null, depending on var type
};

Wyświetl plik

@ -1,11 +1,63 @@
'use strict';
var c = require('./constants');
var utils = require('./utils');
var trees = require('./trees');
var adler32 = require('./adler32');
var crc32 = require('./crc32');
/* Public constants ==========================================================*/
/* ===========================================================================*/
/* Allowed flush values; see deflate() and inflate() below for details */
var Z_NO_FLUSH = 0;
var Z_PARTIAL_FLUSH = 1;
//var Z_SYNC_FLUSH = 2;
var Z_FULL_FLUSH = 3;
var Z_FINISH = 4;
var Z_BLOCK = 5;
//var Z_TREES = 6;
/* Return codes for the compression/decompression functions. Negative values
* are errors, positive values are used for special but normal events.
*/
var Z_OK = 0;
var Z_STREAM_END = 1;
//var Z_NEED_DICT = 2;
//var Z_ERRNO = -1;
var Z_STREAM_ERROR = -2;
var Z_DATA_ERROR = -3;
//var Z_MEM_ERROR = -4;
var Z_BUF_ERROR = -5;
//var Z_VERSION_ERROR = -6;
/* compression levels */
//var Z_NO_COMPRESSION = 0;
//var Z_BEST_SPEED = 1;
//var Z_BEST_COMPRESSION = 9;
var Z_DEFAULT_COMPRESSION = -1;
var Z_FILTERED = 1;
var Z_HUFFMAN_ONLY = 2;
var Z_RLE = 3;
var Z_FIXED = 4;
var Z_DEFAULT_STRATEGY = 0;
/* Possible values of the data_type field (though see inflate()) */
//var Z_BINARY = 0;
//var Z_TEXT = 1;
//var Z_ASCII = 1; // = Z_TEXT
var Z_UNKNOWN = 2;
/* The deflate compression method */
var Z_DEFLATED = 8;
/*============================================================================*/
var MAX_MEM_LEVEL = 9;
/* Maximum value for memLevel in deflateInit2 */
@ -442,7 +494,7 @@ function deflate_stored(s, flush) {
// }
fill_window(s);
if (s.lookahead === 0 && flush === c.Z_NO_FLUSH) {
if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
return BS_NEED_MORE;
}
@ -488,7 +540,7 @@ function deflate_stored(s, flush) {
s.insert = 0;
if (flush === c.Z_FINISH) {
if (flush === Z_FINISH) {
/*** FLUSH_BLOCK(s, 1); ***/
flush_block_only(s, true);
if (s.strm.avail_out === 0) {
@ -529,7 +581,7 @@ function deflate_fast(s, flush) {
*/
if (s.lookahead < MIN_LOOKAHEAD) {
fill_window(s);
if (s.lookahead < MIN_LOOKAHEAD && flush === c.Z_NO_FLUSH) {
if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
return BS_NEED_MORE;
}
if (s.lookahead === 0) {
@ -620,7 +672,7 @@ function deflate_fast(s, flush) {
}
}
s.insert = ((s.strstart < (MIN_MATCH-1)) ? s.strstart : MIN_MATCH-1);
if (flush === c.Z_FINISH) {
if (flush === Z_FINISH) {
/*** FLUSH_BLOCK(s, 1); ***/
flush_block_only(s, true);
if (s.strm.avail_out === 0) {
@ -660,7 +712,7 @@ function deflate_slow(s, flush) {
*/
if (s.lookahead < MIN_LOOKAHEAD) {
fill_window(s);
if (s.lookahead < MIN_LOOKAHEAD && flush === c.Z_NO_FLUSH) {
if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
return BS_NEED_MORE;
}
if (s.lookahead === 0) { break; } /* flush the current block */
@ -694,7 +746,7 @@ function deflate_slow(s, flush) {
/* longest_match() sets match_start */
if (s.match_length <= 5 &&
(s.strategy === c.Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
(s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
/* If prev_match is also MIN_MATCH, match_start is garbage
* but we will ignore the current match anyway.
@ -781,7 +833,7 @@ function deflate_slow(s, flush) {
s.match_available = 0;
}
s.insert = s.strstart < MIN_MATCH-1 ? s.strstart : MIN_MATCH-1;
if (flush === c.Z_FINISH) {
if (flush === Z_FINISH) {
/*** FLUSH_BLOCK(s, 1); ***/
flush_block_only(s, true);
if (s.strm.avail_out === 0) {
@ -822,7 +874,7 @@ function deflate_rle(s, flush) {
*/
if (s.lookahead <= MAX_MATCH) {
fill_window(s);
if (s.lookahead <= MAX_MATCH && flush === c.Z_NO_FLUSH) {
if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
return BS_NEED_MORE;
}
if (s.lookahead === 0) { break; } /* flush the current block */
@ -879,7 +931,7 @@ function deflate_rle(s, flush) {
}
}
s.insert = 0;
if (flush === c.Z_FINISH) {
if (flush === Z_FINISH) {
/*** FLUSH_BLOCK(s, 1); ***/
flush_block_only(s, true);
if (s.strm.avail_out === 0) {
@ -911,7 +963,7 @@ function deflate_huff(s, flush) {
if (s.lookahead === 0) {
fill_window(s);
if (s.lookahead === 0) {
if (flush === c.Z_NO_FLUSH) {
if (flush === Z_NO_FLUSH) {
return BS_NEED_MORE;
}
break; /* flush the current block */
@ -935,7 +987,7 @@ function deflate_huff(s, flush) {
}
}
s.insert = 0;
if (flush === c.Z_FINISH) {
if (flush === Z_FINISH) {
/*** FLUSH_BLOCK(s, 1); ***/
flush_block_only(s, true);
if (s.strm.avail_out === 0) {
@ -1022,7 +1074,7 @@ function DeflateState() {
this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
this.gzhead = null; /* gzip header information to write */
this.gzindex = 0; /* where in extra, name, or comment */
this.method = c.Z_DEFLATED; /* can only be DEFLATED */
this.method = Z_DEFLATED; /* can only be DEFLATED */
this.last_flush = -1; /* value of flush param for previous deflate call */
this.w_size = 0; /* LZ77 window size (32K by default) */
@ -1209,11 +1261,11 @@ function deflateResetKeep(strm) {
var s;
if (!strm || !strm.state) {
return c.Z_STREAM_ERROR;
return Z_STREAM_ERROR;
}
strm.total_in = strm.total_out = 0;
strm.data_type = c.Z_UNKNOWN;
strm.data_type = Z_UNKNOWN;
s = strm.state;
s.pending = 0;
@ -1228,14 +1280,14 @@ function deflateResetKeep(strm) {
0 // crc32(0, Z_NULL, 0)
:
1; // adler32(0, Z_NULL, 0)
s.last_flush = c.Z_NO_FLUSH;
s.last_flush = Z_NO_FLUSH;
trees._tr_init(s);
return c.Z_OK;
return Z_OK;
}
function deflateReset(strm) {
var ret = deflateResetKeep(strm);
if (ret === c.Z_OK) {
if (ret === Z_OK) {
lm_init(strm.state);
}
return ret;
@ -1243,11 +1295,11 @@ function deflateReset(strm) {
function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
if (!strm) { // === Z_NULL
return c.Z_STREAM_ERROR;
return Z_STREAM_ERROR;
}
var wrap = 1;
if (level === c.Z_DEFAULT_COMPRESSION) {
if (level === Z_DEFAULT_COMPRESSION) {
level = 6;
}
@ -1262,10 +1314,10 @@ function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
}
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== c.Z_DEFLATED ||
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
strategy < 0 || strategy > c.Z_FIXED) {
return c.Z_STREAM_ERROR;
strategy < 0 || strategy > Z_FIXED) {
return Z_STREAM_ERROR;
}
@ -1312,7 +1364,7 @@ function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
}
function deflateInit(strm, level) {
return deflateInit2(strm, level, c.Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, c.Z_DEFAULT_STRATEGY);
return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
}
@ -1320,16 +1372,16 @@ function deflate(strm, flush) {
var old_flush, s;
if (!strm || !strm.state ||
flush > c.Z_BLOCK || flush < 0) {
return c.Z_STREAM_ERROR;
flush > Z_BLOCK || flush < 0) {
return Z_STREAM_ERROR;
}
s = strm.state;
if (!strm.next_out ||
(!strm.next_in && strm.avail_in !== 0) ||
(s.status === FINISH_STATE && flush !== c.Z_FINISH)) {
return (strm.avail_out === 0) ? c.Z_BUF_ERROR : c.Z_STREAM_ERROR;
(s.status === FINISH_STATE && flush !== Z_FINISH)) {
return (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR;
}
s.strm = strm; /* just in case */
@ -1351,7 +1403,7 @@ function deflate(strm, flush) {
put_byte(s, 0);
put_byte(s, 0);
put_byte(s, s.level === 9 ? 2 :
(s.strategy >= c.Z_HUFFMAN_ONLY || s.level < 2 ?
(s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
4 : 0));
put_byte(s, OS_CODE);
s.status = BUSY_STATE;
@ -1362,10 +1414,10 @@ function deflate(strm, flush) {
}
else // DEFLATE header
{
var header = (c.Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
var level_flags = -1;
if (s.strategy >= c.Z_HUFFMAN_ONLY || s.level < 2) {
if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
level_flags = 0;
} else if (s.level < 6) {
level_flags = 1;
@ -1401,7 +1453,7 @@ function deflate(strm, flush) {
* return OK instead of BUF_ERROR at next call of deflate:
*/
s.last_flush = -1;
return c.Z_OK;
return Z_OK;
}
/* Make sure there is something to do and avoid duplicate consecutive
@ -1409,21 +1461,21 @@ function deflate(strm, flush) {
* returning Z_STREAM_END instead of Z_BUF_ERROR.
*/
} else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
flush !== c.Z_FINISH) {
return c.Z_BUF_ERROR;
flush !== Z_FINISH) {
return Z_BUF_ERROR;
}
/* User must not provide more input after the first FINISH: */
if (s.status === FINISH_STATE && strm.avail_in !== 0) {
return c.Z_BUF_ERROR;
return Z_BUF_ERROR;
}
/* Start a new block or continue the current one.
*/
if (strm.avail_in !== 0 || s.lookahead !== 0 ||
(flush !== c.Z_NO_FLUSH && s.status !== FINISH_STATE)) {
var bstate = (s.strategy === c.Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
(s.strategy === c.Z_RLE ? deflate_rle(s, flush) :
(flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
(s.strategy === Z_RLE ? deflate_rle(s, flush) :
configuration_table[s.level].func(s, flush));
if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
@ -1434,7 +1486,7 @@ function deflate(strm, flush) {
s.last_flush = -1;
/* avoid BUF_ERROR next call, see above */
}
return c.Z_OK;
return Z_OK;
/* If flush != Z_NO_FLUSH && avail_out == 0, the next call
* of deflate should use the same flush parameter to make sure
* that the flush is complete. So we don't have to output an
@ -1444,16 +1496,16 @@ function deflate(strm, flush) {
*/
}
if (bstate === BS_BLOCK_DONE) {
if (flush === c.Z_PARTIAL_FLUSH) {
if (flush === Z_PARTIAL_FLUSH) {
trees._tr_align(s);
}
else if (flush !== c.Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
trees._tr_stored_block(s, 0, 0, false);
/* For a full flush, this empty block will be recognized
* as a special marker by inflate_sync().
*/
if (flush === c.Z_FULL_FLUSH) {
if (flush === Z_FULL_FLUSH) {
/*** CLEAR_HASH(s); ***/ /* forget history */
zero(s.head); // Fill with NIL (= 0);
@ -1467,15 +1519,15 @@ function deflate(strm, flush) {
flush_pending(strm);
if (strm.avail_out === 0) {
s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
return c.Z_OK;
return Z_OK;
}
}
}
//Assert(strm->avail_out > 0, "bug2");
//if (strm.avail_out <= 0) { throw new Error("bug2");}
if (flush !== c.Z_FINISH) { return c.Z_OK; }
if (s.wrap <= 0) { return c.Z_STREAM_END; }
if (flush !== Z_FINISH) { return Z_OK; }
if (s.wrap <= 0) { return Z_STREAM_END; }
/* Write the trailer */
if (s.wrap === 2) {
@ -1500,7 +1552,7 @@ function deflate(strm, flush) {
*/
if (s.wrap > 0) { s.wrap = -s.wrap; }
/* write the trailer only once! */
return s.pending !== 0 ? c.Z_OK : c.Z_STREAM_END;
return s.pending !== 0 ? Z_OK : Z_STREAM_END;
}
function deflateEnd(strm) {
@ -1513,12 +1565,12 @@ function deflateEnd(strm) {
status !== BUSY_STATE &&
status !== FINISH_STATE
) {
return c.Z_STREAM_ERROR;
return Z_STREAM_ERROR;
}
strm.state = null;
return status === BUSY_STATE ? c.Z_DATA_ERROR : c.Z_OK;
return status === BUSY_STATE ? Z_DATA_ERROR : Z_OK;
}
/* =========================================================================
@ -1533,6 +1585,7 @@ exports.deflateInit2 = deflateInit2;
exports.deflateReset = deflateReset;
exports.deflate = deflate;
exports.deflateEnd = deflateEnd;
exports.deflate_info = 'pako deflate';
/* Not implemented
exports.deflateSetDictionary = deflateSetDictionary;

Wyświetl plik

@ -1,9 +1,26 @@
'use strict';
var c = require('constants');
var utils = require('./utils');
/* Public constants ==========================================================*/
/* ===========================================================================*/
//var Z_FILTERED = 1;
//var Z_HUFFMAN_ONLY = 2;
//var Z_RLE = 3;
var Z_FIXED = 4;
//var Z_DEFAULT_STRATEGY = 0;
/* Possible values of the data_type field (though see inflate()) */
var Z_BINARY = 0;
var Z_TEXT = 1;
//var Z_ASCII = 1; // = Z_TEXT
var Z_UNKNOWN = 2;
/*============================================================================*/
function zero(buf) { var len = buf.length; while (--len) { buf[len] = 0; } }
@ -954,25 +971,25 @@ function detect_data_type(s) {
/* Check for non-textual ("black-listed") bytes. */
for (n = 0; n <= 31; n++, black_mask >>>= 1) {
if ((black_mask & 1) && (s.dyn_ltree[n*2]/*.Freq*/ !== 0)) {
return c.Z_BINARY;
return Z_BINARY;
}
}
/* Check for textual ("white-listed") bytes. */
if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
return c.Z_TEXT;
return Z_TEXT;
}
for (n = 32; n < LITERALS; n++) {
if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
return c.Z_TEXT;
return Z_TEXT;
}
}
/* There are no "black-listed" or "white-listed" bytes:
* this stream either is empty or has tolerated ("gray-listed") bytes only.
*/
return c.Z_BINARY;
return Z_BINARY;
}
@ -1043,7 +1060,7 @@ function _tr_flush_block(s, buf, stored_len, last)
if (s.level > 0) {
/* Check if the file is binary or text */
if (s.strm.data_type === c.Z_UNKNOWN) {
if (s.strm.data_type === Z_UNKNOWN) {
s.strm.data_type = detect_data_type(s);
}
@ -1090,7 +1107,7 @@ function _tr_flush_block(s, buf, stored_len, last)
*/
_tr_stored_block(s, buf, stored_len, last);
} else if (s.strategy === c.Z_FIXED || static_lenb === opt_lenb) {
} else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
send_bits(s, (STATIC_TREES<<1) + (last ? 1 : 0), 3);
compress_block(s, static_ltree, static_dtree);