Sync code with madler/zlib, cosmetic changes only

5370d99a2a
8a979f6c79
c901a34c92
7096424f23
a456d898bb
21c66cd5ac
ee7d7b5dda
37ed2112a1
723e928b84
8678871f18
This commit is contained in:
Alex Kocharin 2022-06-08 18:14:56 +03:00
parent 8a3fa92fd1
commit b07a436d39
3 changed files with 26 additions and 40 deletions

View file

@ -88,7 +88,7 @@ const err = (strm, errorCode) => {
}; };
const rank = (f) => { const rank = (f) => {
return ((f) << 1) - ((f) > 4 ? 9 : 0); return ((f) * 2) - ((f) > 4 ? 9 : 0);
}; };
const zero = (buf) => { const zero = (buf) => {
@ -512,7 +512,7 @@ const deflate_stored = (s, flush) => {
s.lookahead = 0; s.lookahead = 0;
/* Emit a stored block if pending_buf will be full: */ /* 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) { if (s.strstart === 0 || s.strstart >= max_start) {
/* strstart == 0 is possible when wraparound on 16-bit machine */ /* strstart == 0 is possible when wraparound on 16-bit machine */
@ -1402,7 +1402,7 @@ const deflate = (strm, flush) => {
const s = strm.state; const s = strm.state;
if (!strm.output || if (!strm.output ||
(!strm.input && strm.avail_in !== 0) || (strm.avail_in !== 0 && !strm.input) ||
(s.status === FINISH_STATE && flush !== Z_FINISH)) { (s.status === FINISH_STATE && flush !== Z_FINISH)) {
return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR); 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 /* Not implemented
module.exports.deflateBound = deflateBound; module.exports.deflateBound = deflateBound;
module.exports.deflateCopy = deflateCopy; module.exports.deflateCopy = deflateCopy;
module.exports.deflateGetDictionary = deflateGetDictionary;
module.exports.deflateParams = deflateParams; module.exports.deflateParams = deflateParams;
module.exports.deflatePending = deflatePending; module.exports.deflatePending = deflatePending;
module.exports.deflatePrime = deflatePrime; module.exports.deflatePrime = deflatePrime;

View file

@ -1537,6 +1537,7 @@ module.exports.inflateSetDictionary = inflateSetDictionary;
module.exports.inflateInfo = 'pako inflate (from Nodeca project)'; module.exports.inflateInfo = 'pako inflate (from Nodeca project)';
/* Not implemented /* Not implemented
module.exports.inflateCodesUsed = inflateCodesUsed;
module.exports.inflateCopy = inflateCopy; module.exports.inflateCopy = inflateCopy;
module.exports.inflateGetDictionary = inflateGetDictionary; module.exports.inflateGetDictionary = inflateGetDictionary;
module.exports.inflateMark = inflateMark; module.exports.inflateMark = inflateMark;

View file

@ -328,7 +328,7 @@ const gen_bitlen = (s, desc) =>
} }
if (overflow === 0) { return; } 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 */ /* This happens for example on obj2 and pic of the Calgary corpus */
/* Find the first bit length which could increase: */ /* Find the first bit length which could increase: */
@ -355,7 +355,7 @@ const gen_bitlen = (s, desc) =>
m = s.heap[--h]; m = s.heap[--h];
if (m > max_code) { continue; } if (m > max_code) { continue; }
if (tree[m * 2 + 1]/*.Len*/ !== bits) { 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*/; s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/;
tree[m * 2 + 1]/*.Len*/ = bits; tree[m * 2 + 1]/*.Len*/ = bits;
} }
@ -387,7 +387,8 @@ const gen_codes = (tree, max_code, bl_count) =>
* without bit reversal. * without bit reversal.
*/ */
for (bits = 1; bits <= MAX_BITS; bits++) { 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 /* Check that the bit counts in bl_count are consistent. The last code
* must be all ones. * must be all ones.
@ -546,29 +547,6 @@ const bi_windup = (s) =>
s.bi_valid = 0; 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 * Compares to subtrees, using the tree depth as tie breaker when
* the subtrees have equal frequency. This minimizes the worst case length. * 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: * Check if the data type is TEXT or BINARY, using the following algorithm:
* - TEXT if the two conditions below are satisfied: * - TEXT if the two conditions below are satisfied:
* a) There are no non-portable control characters belonging to the * 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 * 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. * - BINARY otherwise.
* - The following partially-portable control characters form a * - The following partially-portable control characters form a
* "gray list" that is ignored in this detection algorithm: * "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. * IN assertion: the fields Freq of dyn_ltree are set.
*/ */
const detect_data_type = (s) => { 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 * set bits 0..6, 14..25, and 28..31
* 0xf3ffc07f = binary 11110011111111111100000001111111 * 0xf3ffc07f = binary 11110011111111111100000001111111
*/ */
let black_mask = 0xf3ffc07f; let block_mask = 0xf3ffc07f;
let n; let n;
/* Check for non-textual ("black-listed") bytes. */ /* Check for non-textual ("block-listed") bytes. */
for (n = 0; n <= 31; n++, black_mask >>>= 1) { for (n = 0; n <= 31; n++, block_mask >>>= 1) {
if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) { if ((block_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) {
return Z_BINARY; 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 || if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) { s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
return Z_TEXT; 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. * this stream either is empty or has tolerated ("gray-listed") bytes only.
*/ */
return Z_BINARY; 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 */ //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 */ 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 * 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) => const _tr_flush_block = (s, buf, stored_len, last) =>
//DeflateState *s; //DeflateState *s;