From 371292dfefc5a9e855fc40e2f465fd22a4e985ae Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Fri, 21 Feb 2014 11:42:42 +0400 Subject: [PATCH] Closed TODOs and added comments --- lib/inflate.js | 2 +- lib/zlib/adler32.js | 3 +++ lib/zlib/crc32.js | 7 ++++++- lib/zlib/deflate.js | 42 ++++++++++++++++++------------------------ lib/zlib/trees.js | 37 ++++++++++++++++++++++++++++++++++--- lib/zlib/zstream.js | 8 +++----- 6 files changed, 65 insertions(+), 34 deletions(-) diff --git a/lib/inflate.js b/lib/inflate.js index 43abaa8..67b028f 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -88,7 +88,7 @@ var Inflate = function(options) { this.options = utils.assign({ chunkSize: 16384, - windowBits: 15 + 32 + windowBits: 15 + 32 // By default - autodetect deflate/gzip }, options || {}); var opt = this.options; diff --git a/lib/zlib/adler32.js b/lib/zlib/adler32.js index 5f4fd50..299d10a 100644 --- a/lib/zlib/adler32.js +++ b/lib/zlib/adler32.js @@ -1,5 +1,8 @@ 'use strict'; +// Note: adler32 takes 12% for level 0 and 2% for level 6. +// It doesn't worth to make additional optimizationa as in original. +// Small size is preferable. function adler32(adler, buf, len, pos) { var s1 = adler & 0xffff diff --git a/lib/zlib/crc32.js b/lib/zlib/crc32.js index d6a36cf..bfce100 100644 --- a/lib/zlib/crc32.js +++ b/lib/zlib/crc32.js @@ -1,5 +1,10 @@ 'use strict'; +// Note: we can't get significant speed boost here. +// So write code to minimize size - no pregenerated tables +// and array tools dependencies. + + // Use ordinary array, since untyped makes no boost here function makeTable() { var c, table = []; @@ -15,7 +20,7 @@ function makeTable() { return table; } - +// Create table on load. Just 255 signed longs. Not a problem. var crcTable = makeTable(); diff --git a/lib/zlib/deflate.js b/lib/zlib/deflate.js index b83919c..28f30c1 100644 --- a/lib/zlib/deflate.js +++ b/lib/zlib/deflate.js @@ -6,12 +6,14 @@ var trees = require('./trees'); var adler32 = require('./adler32'); var crc32 = require('./crc32'); -//var Z_NULL = c.Z_NULL; +var MAX_MEM_LEVEL = 9; +/* Maximum value for memLevel in deflateInit2 */ var MAX_WBITS = 15; /* 32K LZ77 window */ var DEF_MEM_LEVEL = 8; + var LENGTH_CODES = 29; /* number of length codes, not counting the special END_BLOCK code */ var LITERALS = 256; @@ -1207,11 +1209,9 @@ function deflateResetKeep(strm) { strm.total_in = strm.total_out = 0; strm.data_type = c.Z_UNKNOWN; - // TODO: enable - /*if (strm == Z_NULL || strm->state == Z_NULL || - strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { - return Z_STREAM_ERROR; - }*/ + if (!strm || !strm.state) { + return c.Z_STREAM_ERROR; + } var s = strm.state; s.pending = 0; @@ -1260,12 +1260,11 @@ function deflateInit2(strm, level, method, windowBits, memLevel, strategy) { } - // TODO: enable - /*if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || + if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== c.Z_DEFLATED || windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || - strategy < 0 || strategy > Z_FIXED) { - return Z_STREAM_ERROR; - }*/ + strategy < 0 || strategy > c.Z_FIXED) { + return c.Z_STREAM_ERROR; + } if (windowBits === 8) { @@ -1318,22 +1317,18 @@ function deflateInit(strm, level) { function deflate(strm, flush) { var old_flush, s; - // TODO: enable - /*if (strm.next_out == Z_NULL || - (strm.next_in == Z_NULL && strm.avail_in !== 0) || - (s.status == FINISH_STATE && flush != c.Z_FINISH)) { + if (!strm || !strm.state || + flush > c.Z_BLOCK || flush < 0) { return c.Z_STREAM_ERROR; - }*/ + } s = strm.state; - // TODO: enable - /*if (strm->next_out == Z_NULL || - (strm->next_in == Z_NULL && strm->avail_in != 0) || - (s->status == FINISH_STATE && flush != Z_FINISH)) { - ERR_RETURN(strm, Z_STREAM_ERROR); - if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); - }*/ + 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.strm = strm; /* just in case */ old_flush = s.last_flush; @@ -1448,7 +1443,6 @@ function deflate(strm, flush) { } if (bstate === BS_BLOCK_DONE) { if (flush === c.Z_PARTIAL_FLUSH) { - // TODO: implement trees._tr_align(s); } else if (flush !== c.Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ diff --git a/lib/zlib/trees.js b/lib/zlib/trees.js index 996d1a9..17e141d 100644 --- a/lib/zlib/trees.js +++ b/lib/zlib/trees.js @@ -206,6 +206,23 @@ function bi_reverse(code, len) { } +/* =========================================================================== + * Flush the bit buffer, keeping at most 7 bits in it. + */ +function bi_flush(s) { + if (s.bi_valid === 16) { + put_short(s, s.bi_buf); + s.bi_buf = 0; + s.bi_valid = 0; + + } else if (s.bi_valid >= 8) { + s.pending_buf[s.pending++] = s.bi_buf & 0xff; + s.bi_buf >>= 8; + s.bi_valid -= 8; + } +} + + /* =========================================================================== * Compute the optimal bit lengths for a tree and update the total bit length * for the current block. @@ -983,6 +1000,7 @@ function _tr_init(s) init_block(s); } + /* =========================================================================== * Send a stored block */ @@ -996,6 +1014,18 @@ function _tr_stored_block(s, buf, stored_len, last) copy_block(s, buf, stored_len, true); /* with header */ } + +/* =========================================================================== + * Send one empty static block to give enough lookahead for inflate. + * This takes 10 bits, of which 7 may remain in the bit buffer. + */ +function _tr_align(s) { + send_bits(s, STATIC_TREES<<1, 3); + send_code(s, END_BLOCK, static_ltree); + bi_flush(s); +} + + /* =========================================================================== * Determine the best encoding for the current block: dynamic trees, static * trees or store, and output the encoded block to the zip file. @@ -1140,7 +1170,8 @@ function _tr_tally(s, dist, lc) */ } -exports._tr_init = _tr_init; +exports._tr_init = _tr_init; exports._tr_stored_block = _tr_stored_block; -exports._tr_flush_block = _tr_flush_block; -exports._tr_tally = _tr_tally; \ No newline at end of file +exports._tr_flush_block = _tr_flush_block; +exports._tr_tally = _tr_tally; +exports._tr_align = _tr_align; \ No newline at end of file diff --git a/lib/zlib/zstream.js b/lib/zlib/zstream.js index f7d32d9..32252ce 100644 --- a/lib/zlib/zstream.js +++ b/lib/zlib/zstream.js @@ -1,17 +1,15 @@ 'use strict'; -var c = require('constants'); -// TODO: remove Z_NULL. Set proper values directly function ZStream() { /* next input byte */ - this.next_in = c.Z_NULL; + this.next_in = null; /* number of bytes available at next_in */ this.avail_in = 0; /* total number of input bytes read so far */ this.total_in = 0; /* next output byte should be put there */ - //this.next_out = c.Z_NULL; + this.next_out = null; /* remaining free space at next_out */ this.avail_out = 0; /* total number of bytes output so far */ @@ -19,7 +17,7 @@ function ZStream() { /* last error message, NULL if no error */ //this.msg = c.Z_NULL; /* not visible by applications */ - this.state = c.Z_NULL; + this.state = null; /* best guess about the data type: binary or text */ this.data_type = 2; /* adler32 value of the uncompressed data */