diff --git a/lib/zlib/deflate.js b/lib/zlib/deflate.js index 75a5b8d..0f9bbad 100644 --- a/lib/zlib/deflate.js +++ b/lib/zlib/deflate.js @@ -88,7 +88,7 @@ const err = (strm, errorCode) => { }; const rank = (f) => { - return ((f) << 1) - ((f) > 4 ? 9 : 0); + return ((f) * 2) - ((f) > 4 ? 9 : 0); }; const zero = (buf) => { @@ -512,7 +512,7 @@ const deflate_stored = (s, flush) => { s.lookahead = 0; /* Emit a stored block if pending_buf will be full: */ - const max_start = s.block_start + max_block_size; + const max_start = max_block_size + s.block_start; if (s.strstart === 0 || s.strstart >= max_start) { /* strstart == 0 is possible when wraparound on 16-bit machine */ @@ -1402,7 +1402,7 @@ const deflate = (strm, flush) => { const s = strm.state; if (!strm.output || - (!strm.input && strm.avail_in !== 0) || + (strm.avail_in !== 0 && !strm.input) || (s.status === FINISH_STATE && flush !== Z_FINISH)) { return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR); } @@ -1843,6 +1843,7 @@ module.exports.deflateInfo = 'pako deflate (from Nodeca project)'; /* Not implemented module.exports.deflateBound = deflateBound; module.exports.deflateCopy = deflateCopy; +module.exports.deflateGetDictionary = deflateGetDictionary; module.exports.deflateParams = deflateParams; module.exports.deflatePending = deflatePending; module.exports.deflatePrime = deflatePrime; diff --git a/lib/zlib/inflate.js b/lib/zlib/inflate.js index 2146513..e0047c6 100644 --- a/lib/zlib/inflate.js +++ b/lib/zlib/inflate.js @@ -1537,6 +1537,7 @@ module.exports.inflateSetDictionary = inflateSetDictionary; module.exports.inflateInfo = 'pako inflate (from Nodeca project)'; /* Not implemented +module.exports.inflateCodesUsed = inflateCodesUsed; module.exports.inflateCopy = inflateCopy; module.exports.inflateGetDictionary = inflateGetDictionary; module.exports.inflateMark = inflateMark; diff --git a/lib/zlib/trees.js b/lib/zlib/trees.js index 70f7e77..06224ef 100644 --- a/lib/zlib/trees.js +++ b/lib/zlib/trees.js @@ -328,7 +328,7 @@ const gen_bitlen = (s, desc) => } if (overflow === 0) { return; } - // Trace((stderr,"\nbit length overflow\n")); + // Tracev((stderr,"\nbit length overflow\n")); /* This happens for example on obj2 and pic of the Calgary corpus */ /* Find the first bit length which could increase: */ @@ -355,7 +355,7 @@ const gen_bitlen = (s, desc) => m = s.heap[--h]; if (m > max_code) { continue; } if (tree[m * 2 + 1]/*.Len*/ !== bits) { - // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); + // Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/; tree[m * 2 + 1]/*.Len*/ = bits; } @@ -387,7 +387,8 @@ const gen_codes = (tree, max_code, bl_count) => * without bit reversal. */ for (bits = 1; bits <= MAX_BITS; bits++) { - next_code[bits] = code = (code + bl_count[bits - 1]) << 1; + code = (code + bl_count[bits - 1]) << 1; + next_code[bits] = code; } /* Check that the bit counts in bl_count are consistent. The last code * must be all ones. @@ -546,29 +547,6 @@ const bi_windup = (s) => s.bi_valid = 0; }; -/* =========================================================================== - * Copy a stored block, storing first the length and its - * one's complement if requested. - */ -const copy_block = (s, buf, len, header) => -//DeflateState *s; -//charf *buf; /* the input data */ -//unsigned len; /* its length */ -//int header; /* true if block header must be written */ -{ - bi_windup(s); /* align on byte boundary */ - - if (header) { - put_short(s, len); - put_short(s, ~len); - } -// while (len--) { -// put_byte(s, *buf++); -// } - s.pending_buf.set(s.window.subarray(buf, buf + len), s.pending); - s.pending += len; -}; - /* =========================================================================== * Compares to subtrees, using the tree depth as tie breaker when * the subtrees have equal frequency. This minimizes the worst case length. @@ -981,9 +959,9 @@ const send_all_trees = (s, lcodes, dcodes, blcodes) => * Check if the data type is TEXT or BINARY, using the following algorithm: * - TEXT if the two conditions below are satisfied: * a) There are no non-portable control characters belonging to the - * "black list" (0..6, 14..25, 28..31). + * "block list" (0..6, 14..25, 28..31). * b) There is at least one printable character belonging to the - * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). + * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). * - BINARY otherwise. * - The following partially-portable control characters form a * "gray list" that is ignored in this detection algorithm: @@ -991,21 +969,21 @@ const send_all_trees = (s, lcodes, dcodes, blcodes) => * IN assertion: the fields Freq of dyn_ltree are set. */ const detect_data_type = (s) => { - /* black_mask is the bit mask of black-listed bytes + /* block_mask is the bit mask of block-listed bytes * set bits 0..6, 14..25, and 28..31 * 0xf3ffc07f = binary 11110011111111111100000001111111 */ - let black_mask = 0xf3ffc07f; + let block_mask = 0xf3ffc07f; let n; - /* 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)) { + /* Check for non-textual ("block-listed") bytes. */ + for (n = 0; n <= 31; n++, block_mask >>>= 1) { + if ((block_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) { return Z_BINARY; } } - /* Check for textual ("white-listed") bytes. */ + /* Check for textual ("allow-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 Z_TEXT; @@ -1016,7 +994,7 @@ const detect_data_type = (s) => { } } - /* There are no "black-listed" or "white-listed" bytes: + /* There are no "block-listed" or "allow-listed" bytes: * this stream either is empty or has tolerated ("gray-listed") bytes only. */ return Z_BINARY; @@ -1058,7 +1036,13 @@ const _tr_stored_block = (s, buf, stored_len, last) => //int last; /* one if this is the last block for a file */ { send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */ - copy_block(s, buf, stored_len, true); /* with header */ + bi_windup(s); /* align on byte boundary */ + put_short(s, stored_len); + put_short(s, ~stored_len); + if (stored_len) { + s.pending_buf.set(s.window.subarray(buf, buf + stored_len), s.pending); + } + s.pending += stored_len; }; @@ -1075,7 +1059,7 @@ const _tr_align = (s) => { /* =========================================================================== * Determine the best encoding for the current block: dynamic trees, static - * trees or store, and output the encoded block to the zip file. + * trees or store, and write out the encoded block. */ const _tr_flush_block = (s, buf, stored_len, last) => //DeflateState *s;