diff --git a/lib/zlib/constants.js b/lib/zlib/constants.js index 3f3b6fb..74bc8d8 100644 --- a/lib/zlib/constants.js +++ b/lib/zlib/constants.js @@ -15,12 +15,12 @@ module.exports = { Z_OK: 0, Z_STREAM_END: 1, Z_NEED_DICT: 2, - Z_ERRNO: (-1), - Z_STREAM_ERROR: (-2), - Z_DATA_ERROR: (-3), - //Z_MEM_ERROR: (-4), - Z_BUF_ERROR: (-5), - //Z_VERSION_ERROR: (-6), + Z_ERRNO: -1, + Z_STREAM_ERROR: -2, + Z_DATA_ERROR: -3, + //Z_MEM_ERROR: -4, + Z_BUF_ERROR: -5, + //Z_VERSION_ERROR: -6, /* compression levels */ Z_NO_COMPRESSION: 0, @@ -38,10 +38,10 @@ module.exports = { /* Possible values of the data_type field (though see inflate()) */ Z_BINARY: 0, Z_TEXT: 1, - //Z_ASCII: 1, // = Z_TEXT (deprecated) + //Z_ASCII: 1, // = Z_TEXT (deprecated) Z_UNKNOWN: 2, /* The deflate compression method */ Z_DEFLATED: 8 - //Z_NULL: null // Use -1 or null, depending on var type + //Z_NULL: null // Use -1 or null inline, depending on var type }; \ No newline at end of file diff --git a/lib/zlib/deflate.js b/lib/zlib/deflate.js index a128932..6884b32 100644 --- a/lib/zlib/deflate.js +++ b/lib/zlib/deflate.js @@ -112,7 +112,7 @@ function rank(f) { return ((f) << 1) - ((f) > 4 ? 9 : 0); } -function zero(buf) { var len = buf.length; while (--len) { buf[len] = 0; } } +function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } } /* ========================================================================= @@ -330,6 +330,7 @@ function fill_window(s) { do { more = s.window_size - s.lookahead - s.strstart; + // JS ints have 32 bit, block below not needed /* Deal with !@#$% 64K limit: */ //if (sizeof(int) <= 2) { // if (more == 0 && s->strstart == 0 && s->lookahead == 0) { @@ -1253,7 +1254,9 @@ function DeflateState() { * are always zero. */ - this.high_water = 0; + // Used for window memory init. We safely ignore it for JS. That makes + // sense only for pointers and memory check tools. + //this.high_water = 0; /* High water mark offset in window for initialized bytes -- bytes above * this are set to zero in order to avoid memory check warnings when * longest match routines access bytes past the input. This is then @@ -1350,7 +1353,8 @@ function deflateInit2(strm, level, method, windowBits, memLevel, strategy) { s.head = new utils.Buf16(s.hash_size); s.prev = new utils.Buf16(s.w_size); - s.high_water = 0; /* nothing written to s->window yet */ + // Don't need mem init magic for JS. + //s.high_water = 0; /* nothing written to s->window yet */ s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ @@ -1560,7 +1564,13 @@ function deflate(strm, flush) { } function deflateEnd(strm) { - var status = strm.state.status; + var status; + + if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) { + return Z_STREAM_ERROR; + } + + status = strm.state.status; if (status !== INIT_STATE && status !== EXTRA_STATE && status !== NAME_STATE && diff --git a/lib/zlib/inffast.js b/lib/zlib/inffast.js index 177aecf..df0ae2f 100644 --- a/lib/zlib/inffast.js +++ b/lib/zlib/inffast.js @@ -183,7 +183,6 @@ module.exports = function inflate_fast(strm, start) { // (!) This block is disabled in zlib defailts, // don't enable it for binary compatibility - //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR // if (len <= op - whave) { // do { diff --git a/lib/zlib/inflate.js b/lib/zlib/inflate.js index ec5957f..7fcd0d9 100644 --- a/lib/zlib/inflate.js +++ b/lib/zlib/inflate.js @@ -149,7 +149,6 @@ function InflateState() { this.lens = new utils.Buf16(320); /* temporary storage for code lengths */ this.work = new utils.Buf16(288); /* work area for code table building */ - // TODO: 8 or 16 bits? this.codes = new utils.Buf32(ENOUGH); /* space for code tables */ this.sane = 0; /* if false, allow invalid distance too far */ this.back = 0; /* bits back of last unprocessed length/lit */ @@ -162,7 +161,7 @@ function inflateResetKeep(strm) { if (!strm || !strm.state) { return Z_STREAM_ERROR; } state = strm.state; strm.total_in = strm.total_out = state.total = 0; - //strm.msg = Z_NULL; + strm.msg = ''; /*Z_NULL*/ if (state.wrap) { /* to support ill-conceived Java test suite */ strm.adler = state.wrap & 1; } @@ -170,13 +169,10 @@ function inflateResetKeep(strm) { state.last = 0; state.havedict = 0; state.dmax = 32768; - // TODO: may be {} state.head = null/*Z_NULL*/; state.hold = 0; state.bits = 0; //state.lencode = state.distcode = state.next = state.codes; - //utils.arraySet(state.lencode,state.codes,0,state.codes.length,0); - //utils.arraySet(state.distcode,state.codes,0,state.codes.length,0); state.lencode = new utils.Buf32(ENOUGH); state.distcode = new utils.Buf32(ENOUGH); @@ -269,8 +265,6 @@ function inflateInit(strm) { */ var virgin = true; -// TODO: check if we can use single array forbetter CPU cache use -// That will require to pass offset, when operating with distance tables. var lenfix, distfix; // We have no pointers in JS, so keep tables separate function fixedtables(state) { @@ -376,9 +370,9 @@ function inflate(strm, flush) { var from; /* where to copy match bytes from */ var from_source; var here = 0; /* current decoding table entry */ - var here_bits, here_op, here_val; + var here_bits, here_op, here_val; // paked "here" denormalized (JS specific) //var last; /* parent table entry */ - var last_bits, last_op, last_val; // paked "last" denormalized + var last_bits, last_op, last_val; // paked "last" denormalized (JS specific) var len; /* length to copy for repeats, bits to drop */ var ret; /* return code */ var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */ @@ -905,10 +899,9 @@ function inflate(strm, flush) { while (state.have < 19) { state.lens[order[state.have++]] = 0; } + // We have separate tables & no pointers. 2 commented lines below not needed. //state.next = state.codes; - // TODO: //state.lencode = state.next; - //state.lencode.copy(state.codes); utils.arraySet(state.lencode, state.codes, 0, state.codes.length, 0); state.lenbits = 7; @@ -1047,9 +1040,10 @@ function inflate(strm, flush) { opts = {bits: state.lenbits}; ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts); -// state.next_index = opts.table_index; + // We have separate tables & no pointers. 2 commented lines below not needed. + // state.next_index = opts.table_index; state.lenbits = opts.bits; -// state.lencode = state.next; + // state.lencode = state.next; if (ret) { strm.msg = 'invalid literal/lengths set'; @@ -1062,9 +1056,10 @@ function inflate(strm, flush) { utils.arraySet(state.distcode, state.codes, 0, state.codes.length, 0); opts = {bits: state.distbits}; ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts); -// state.next_index = opts.table_index; + // We have separate tables & no pointers. 2 commented lines below not needed. + // state.next_index = opts.table_index; state.distbits = opts.bits; -// state.distcode = state.next; + // state.distcode = state.next; if (ret) { strm.msg = 'invalid distances set'; @@ -1287,8 +1282,10 @@ function inflate(strm, flush) { state.mode = BAD; break; } +// (!) This block is disabled in zlib defailts, +// don't enable it for binary compatibility //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR - //Trace((stderr, "inflate.c too far\n")); +// Trace((stderr, "inflate.c too far\n")); // copy -= state.whave; // if (copy > state.length) { copy = state.length; } // if (copy > left) { copy = left; } diff --git a/lib/zlib/inftrees.js b/lib/zlib/inftrees.js index 1427cd8..4b894b5 100644 --- a/lib/zlib/inftrees.js +++ b/lib/zlib/inftrees.js @@ -305,7 +305,7 @@ module.exports = function inflate_table(type, lens, lens_index, codes, table, ta /*table.op[low] = curr; table.bits[low] = root; table.val[low] = next - opts.table_index;*/ - table[low] = (root << 24) | (curr << 16) | (next - table_index); + table[low] = (root << 24) | (curr << 16) | (next - table_index) |0; } } @@ -316,7 +316,7 @@ module.exports = function inflate_table(type, lens, lens_index, codes, table, ta //table.op[next + huff] = 64; /* invalid code marker */ //table.bits[next + huff] = len - drop; //table.val[next + huff] = 0; - table[next + huff] = ((len - drop) << 24) | (64 << 16) | 0; + table[next + huff] = ((len - drop) << 24) | (64 << 16) |0; } /* set return parameters */ diff --git a/lib/zlib/trees.js b/lib/zlib/trees.js index 3c56682..37645ac 100644 --- a/lib/zlib/trees.js +++ b/lib/zlib/trees.js @@ -7,22 +7,22 @@ var utils = require('./utils'); /* ===========================================================================*/ -//var Z_FILTERED = 1; -//var Z_HUFFMAN_ONLY = 2; -//var Z_RLE = 3; +//var Z_FILTERED = 1; +//var Z_HUFFMAN_ONLY = 2; +//var Z_RLE = 3; var Z_FIXED = 4; -//var Z_DEFAULT_STRATEGY = 0; +//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_ASCII = 1; // = Z_TEXT var Z_UNKNOWN = 2; /*============================================================================*/ -function zero(buf) { var len = buf.length; while (--len) { buf[len] = 0; } } +function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } } // From zutil.h