X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a4019ec2806519e044bd79881ccc2a803c12fb7d..a9fc5eec8b1facb001552e61e28c88a22034f199:/src/zlib/trees.c diff --git a/src/zlib/trees.c b/src/zlib/trees.c index d37dff4951..bb09554938 100644 --- a/src/zlib/trees.c +++ b/src/zlib/trees.c @@ -1,5 +1,5 @@ /* trees.c -- output deflated data using Huffman coding - * Copyright (C) 1995-2002 Jean-loup Gailly + * Copyright (C) 1995-2003 Jean-loup Gailly * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -35,7 +35,7 @@ #include "deflate.h" -#ifdef __WXDEBUG__ +#ifdef DEBUG # include #endif @@ -163,11 +163,11 @@ local void copy_block OF((deflate_state *s, charf *buf, unsigned len, local void gen_trees_header OF((void)); #endif -#ifndef __WXDEBUG__ +#ifndef DEBUG # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) /* Send a code of the given tree. c and tree must not have side effects */ -#else /* __WXDEBUG__ */ +#else /* DEBUG */ # define send_code(s, c, tree) \ { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ send_bits(s, tree[c].Code, tree[c].Len); } @@ -186,17 +186,13 @@ local void gen_trees_header OF((void)); * Send a value on a given number of bits. * IN assertion: length <= 16 and value fits in length bits. */ -#ifdef __WXDEBUG__ +#ifdef DEBUG local void send_bits OF((deflate_state *s, int value, int length)); -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void send_bits(deflate_state* s, int value, int length) -#else local void send_bits(s, value, length) deflate_state *s; int value; /* value to send */ int length; /* number of bits */ -#endif { Tracevv((stderr," l %2d v %4x ", length, value)); Assert(length > 0 && length <= 15, "invalid length"); @@ -216,7 +212,7 @@ local void send_bits(s, value, length) s->bi_valid += length; } } -#else /* !__WXDEBUG__ */ +#else /* !DEBUG */ #define send_bits(s, value, length) \ { int len = length;\ @@ -231,10 +227,9 @@ local void send_bits(s, value, length) s->bi_valid += len;\ }\ } -#endif /* __WXDEBUG__ */ +#endif /* DEBUG */ -#define MAX(a,b) (a >= b ? a : b) /* the arguments must not have side effects */ /* =========================================================================== @@ -324,7 +319,7 @@ local void tr_static_init() * Genererate the file trees.h describing the static trees. */ #ifdef GEN_TREES_H -# ifndef __WXDEBUG__ +# ifndef DEBUG # include # endif @@ -339,42 +334,42 @@ void gen_trees_header() Assert (header != NULL, "Can't open trees.h"); fprintf(header, - "/* header created automatically with -DGEN_TREES_H */\n\n"); + "/* header created automatically with -DGEN_TREES_H */\n\n"); fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); for (i = 0; i < L_CODES+2; i++) { - fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, - static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); + fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, + static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); } fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); for (i = 0; i < D_CODES; i++) { - fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, - static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); + fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, + static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); } fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n"); for (i = 0; i < DIST_CODE_LEN; i++) { - fprintf(header, "%2u%s", _dist_code[i], - SEPARATOR(i, DIST_CODE_LEN-1, 20)); + fprintf(header, "%2u%s", _dist_code[i], + SEPARATOR(i, DIST_CODE_LEN-1, 20)); } fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) { - fprintf(header, "%2u%s", _length_code[i], - SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); + fprintf(header, "%2u%s", _length_code[i], + SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); } fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); for (i = 0; i < LENGTH_CODES; i++) { - fprintf(header, "%1u%s", base_length[i], - SEPARATOR(i, LENGTH_CODES-1, 20)); + fprintf(header, "%1u%s", base_length[i], + SEPARATOR(i, LENGTH_CODES-1, 20)); } fprintf(header, "local const int base_dist[D_CODES] = {\n"); for (i = 0; i < D_CODES; i++) { - fprintf(header, "%5u%s", base_dist[i], - SEPARATOR(i, D_CODES-1, 10)); + fprintf(header, "%5u%s", base_dist[i], + SEPARATOR(i, D_CODES-1, 10)); } fclose(header); @@ -384,12 +379,8 @@ void gen_trees_header() /* =========================================================================== * Initialize the tree data structures for a new zlib stream. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -void _tr_init(deflate_state* s) -#else void _tr_init(s) deflate_state *s; -#endif { tr_static_init(); @@ -405,7 +396,7 @@ void _tr_init(s) s->bi_buf = 0; s->bi_valid = 0; s->last_eob_len = 8; /* enough lookahead for inflate */ -#ifdef __WXDEBUG__ +#ifdef DEBUG s->compressed_len = 0L; s->bits_sent = 0L; #endif @@ -417,12 +408,8 @@ void _tr_init(s) /* =========================================================================== * Initialize a new block. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void init_block(deflate_state* s) -#else local void init_block(s) deflate_state *s; -#endif { int n; /* iterates over tree elements */ @@ -465,14 +452,10 @@ local void init_block(s) * when the heap property is re-established (each father smaller than its * two sons). */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void pqdownheap(deflate_state* s, ct_data* tree, int k) -#else local void pqdownheap(s, tree, k) deflate_state *s; ct_data *tree; /* the tree to restore */ int k; /* node to move down */ -#endif { int v = s->heap[k]; int j = k << 1; /* left son of k */ @@ -504,13 +487,9 @@ local void pqdownheap(s, tree, k) * The length opt_len is updated; static_len is also updated if stree is * not null. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void gen_bitlen(deflate_state* s, tree_desc* desc) -#else local void gen_bitlen(s, desc) deflate_state *s; tree_desc *desc; /* the tree descriptor */ -#endif { ct_data *tree = desc->dyn_tree; int max_code = desc->max_code; @@ -595,14 +574,10 @@ local void gen_bitlen(s, desc) * OUT assertion: the field code is set for all tree elements of non * zero code length. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void gen_codes (ct_data* tree, int max_code, ushf* bl_count) -#else local void gen_codes (tree, max_code, bl_count) ct_data *tree; /* the tree to decorate */ int max_code; /* largest code with non zero frequency */ ushf *bl_count; /* number of codes at each bit length */ -#endif { ush next_code[MAX_BITS+1]; /* next code value for each bit length */ ush code = 0; /* running code value */ @@ -641,13 +616,9 @@ local void gen_codes (tree, max_code, bl_count) * and corresponding code. The length opt_len is updated; static_len is * also updated if stree is not null. The field max_code is set. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void build_tree(deflate_state* s, tree_desc* desc) -#else local void build_tree(s, desc) deflate_state *s; tree_desc *desc; /* the tree descriptor */ -#endif { ct_data *tree = desc->dyn_tree; const ct_data *stree = desc->stat_desc->static_tree; @@ -703,7 +674,8 @@ local void build_tree(s, desc) /* Create a new node father of n and m */ tree[node].Freq = tree[n].Freq + tree[m].Freq; - s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1); + s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ? + s->depth[n] : s->depth[m]) + 1); tree[n].Dad = tree[m].Dad = (ush)node; #ifdef DUMP_BL_TREE if (tree == s->bl_tree) { @@ -732,14 +704,10 @@ local void build_tree(s, desc) * Scan a literal or distance tree to determine the frequencies of the codes * in the bit length tree. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void scan_tree (deflate_state* s, ct_data* tree, int max_code) -#else local void scan_tree (s, tree, max_code) deflate_state *s; ct_data *tree; /* the tree to be scanned */ int max_code; /* and its largest code of non zero frequency */ -#endif { int n; /* iterates over all tree elements */ int prevlen = -1; /* last emitted length */ @@ -781,14 +749,10 @@ local void scan_tree (s, tree, max_code) * Send a literal or distance tree in compressed form, using the codes in * bl_tree. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void send_tree (deflate_state* s, ct_data* tree, int max_code) -#else local void send_tree (s, tree, max_code) deflate_state *s; ct_data *tree; /* the tree to be scanned */ int max_code; /* and its largest code of non zero frequency */ -#endif { int n; /* iterates over all tree elements */ int prevlen = -1; /* last emitted length */ @@ -836,12 +800,8 @@ local void send_tree (s, tree, max_code) * Construct the Huffman tree for the bit lengths and return the index in * bl_order of the last bit length code to send. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local int build_bl_tree( deflate_state* s) -#else local int build_bl_tree(s) deflate_state *s; -#endif { int max_blindex; /* index of last bit length code of non zero freq */ @@ -875,13 +835,9 @@ local int build_bl_tree(s) * lengths of the bit length codes, the literal tree and the distance tree. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void send_all_trees(deflate_state* s, int lcodes, int dcodes, int blcodes) -#else local void send_all_trees(s, lcodes, dcodes, blcodes) deflate_state *s; int lcodes, dcodes, blcodes; /* number of codes for each tree */ -#endif { int rank; /* index in bl_order */ @@ -908,18 +864,14 @@ local void send_all_trees(s, lcodes, dcodes, blcodes) /* =========================================================================== * Send a stored block */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -void _tr_stored_block(deflate_state* s, charf* buf, ulg stored_len, int eof) -#else void _tr_stored_block(s, buf, stored_len, eof) deflate_state *s; charf *buf; /* input block */ ulg stored_len; /* length of input block */ int eof; /* true if this is the last block for a file */ -#endif { send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */ -#ifdef __WXDEBUG__ +#ifdef DEBUG s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; s->compressed_len += (stored_len + 4) << 3; #endif @@ -937,16 +889,12 @@ void _tr_stored_block(s, buf, stored_len, eof) * To simplify the code, we assume the worst case of last real code encoded * on one bit only. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -void _tr_align(deflate_state* s) -#else void _tr_align(s) deflate_state *s; -#endif { send_bits(s, STATIC_TREES<<1, 3); send_code(s, END_BLOCK, static_ltree); -#ifdef __WXDEBUG__ +#ifdef DEBUG s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ #endif bi_flush(s); @@ -958,7 +906,7 @@ void _tr_align(s) if (1 + s->last_eob_len + 10 - s->bi_valid < 9) { send_bits(s, STATIC_TREES<<1, 3); send_code(s, END_BLOCK, static_ltree); -#ifdef __WXDEBUG__ +#ifdef DEBUG s->compressed_len += 10L; #endif bi_flush(s); @@ -970,15 +918,11 @@ void _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. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -void _tr_flush_block(deflate_state* s, charf* buf, ulg stored_len, int eof) -#else void _tr_flush_block(s, buf, stored_len, eof) deflate_state *s; charf *buf; /* input block, or NULL if too old */ ulg stored_len; /* length of input block */ int eof; /* true if this is the last block for a file */ -#endif { ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ int max_blindex = 0; /* index of last bit length code of non zero freq */ @@ -986,39 +930,39 @@ void _tr_flush_block(s, buf, stored_len, eof) /* Build the Huffman trees unless a stored block is forced */ if (s->level > 0) { - /* Check if the file is ascii or binary */ - if (s->data_type == Z_UNKNOWN) set_data_type(s); + /* Check if the file is ascii or binary */ + if (s->data_type == Z_UNKNOWN) set_data_type(s); - /* Construct the literal and distance trees */ - build_tree(s, (tree_desc *)(&(s->l_desc))); - Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, - s->static_len)); + /* Construct the literal and distance trees */ + build_tree(s, (tree_desc *)(&(s->l_desc))); + Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); - build_tree(s, (tree_desc *)(&(s->d_desc))); - Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, - s->static_len)); - /* At this point, opt_len and static_len are the total bit lengths of - * the compressed block data, excluding the tree representations. - */ + build_tree(s, (tree_desc *)(&(s->d_desc))); + Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); + /* At this point, opt_len and static_len are the total bit lengths of + * the compressed block data, excluding the tree representations. + */ - /* Build the bit length tree for the above two trees, and get the index - * in bl_order of the last bit length code to send. - */ - max_blindex = build_bl_tree(s); + /* Build the bit length tree for the above two trees, and get the index + * in bl_order of the last bit length code to send. + */ + max_blindex = build_bl_tree(s); - /* Determine the best encoding. Compute first the block length in bytes*/ - opt_lenb = (s->opt_len+3+7)>>3; - static_lenb = (s->static_len+3+7)>>3; + /* Determine the best encoding. Compute the block lengths in bytes. */ + opt_lenb = (s->opt_len+3+7)>>3; + static_lenb = (s->static_len+3+7)>>3; - Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", - opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, - s->last_lit)); + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, + s->last_lit)); - if (static_lenb <= opt_lenb) opt_lenb = static_lenb; + if (static_lenb <= opt_lenb) opt_lenb = static_lenb; } else { Assert(buf != (char*)0, "lost buf"); - opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ } #ifdef FORCE_STORED @@ -1042,7 +986,7 @@ void _tr_flush_block(s, buf, stored_len, eof) #endif send_bits(s, (STATIC_TREES<<1)+eof, 3); compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree); -#ifdef __WXDEBUG__ +#ifdef DEBUG s->compressed_len += 3 + s->static_len; #endif } else { @@ -1050,7 +994,7 @@ void _tr_flush_block(s, buf, stored_len, eof) send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, max_blindex+1); compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree); -#ifdef __WXDEBUG__ +#ifdef DEBUG s->compressed_len += 3 + s->opt_len; #endif } @@ -1062,7 +1006,7 @@ void _tr_flush_block(s, buf, stored_len, eof) if (eof) { bi_windup(s); -#ifdef __WXDEBUG__ +#ifdef DEBUG s->compressed_len += 7; /* align on byte boundary */ #endif } @@ -1074,14 +1018,10 @@ void _tr_flush_block(s, buf, stored_len, eof) * Save the match info and tally the frequency counts. Return true if * the current block must be flushed. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -int _tr_tally (deflate_state* s, unsigned dist, unsigned lc) -#else int _tr_tally (s, dist, lc) deflate_state *s; unsigned dist; /* distance of matched string */ unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ -#endif { s->d_buf[s->last_lit] = (ush)dist; s->l_buf[s->last_lit++] = (uch)lc; @@ -1128,14 +1068,10 @@ int _tr_tally (s, dist, lc) /* =========================================================================== * Send the block data compressed using the given Huffman trees */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void compress_block(deflate_state* s, ct_data* ltree, ct_data* dtree) -#else local void compress_block(s, ltree, dtree) deflate_state *s; ct_data *ltree; /* literal tree */ ct_data *dtree; /* distance tree */ -#endif { unsigned dist; /* distance of matched string */ int lc; /* match length or unmatched char (if dist == 0) */ @@ -1171,7 +1107,8 @@ local void compress_block(s, ltree, dtree) } /* literal or match pair ? */ /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ - Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow"); + Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, + "pendingBuf overflow"); } while (lx < s->last_lit); @@ -1185,12 +1122,8 @@ local void compress_block(s, ltree, dtree) * IN assertion: the fields freq of dyn_ltree are set and the total of all * frequencies does not exceed 64K (to fit in an int on 16 bit machines). */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void set_data_type(deflate_state* s) -#else local void set_data_type(s) deflate_state *s; -#endif { int n = 0; unsigned ascii_freq = 0; @@ -1206,13 +1139,9 @@ local void set_data_type(s) * method would use a table) * IN assertion: 1 <= len <= 15 */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local unsigned bi_reverse(unsigned code, int len) -#else local unsigned bi_reverse(code, len) unsigned code; /* the value to invert */ int len; /* its bit length */ -#endif { register unsigned res = 0; do { @@ -1225,12 +1154,8 @@ local unsigned bi_reverse(code, len) /* =========================================================================== * Flush the bit buffer, keeping at most 7 bits in it. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void bi_flush(deflate_state* s) -#else local void bi_flush(s) deflate_state *s; -#endif { if (s->bi_valid == 16) { put_short(s, s->bi_buf); @@ -1246,12 +1171,8 @@ local void bi_flush(s) /* =========================================================================== * Flush the bit buffer and align the output on a byte boundary */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void bi_windup(deflate_state* s) -#else local void bi_windup(s) deflate_state *s; -#endif { if (s->bi_valid > 8) { put_short(s, s->bi_buf); @@ -1260,7 +1181,7 @@ local void bi_windup(s) } s->bi_buf = 0; s->bi_valid = 0; -#ifdef __WXDEBUG__ +#ifdef DEBUG s->bits_sent = (s->bits_sent+7) & ~7; #endif } @@ -1269,15 +1190,11 @@ local void bi_windup(s) * Copy a stored block, storing first the length and its * one's complement if requested. */ -#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ -local void copy_block(deflate_state* s, charf* buf, unsigned len, int header) -#else local void copy_block(s, buf, len, header) deflate_state *s; charf *buf; /* the input data */ unsigned len; /* its length */ int header; /* true if block header must be written */ -#endif { bi_windup(s); /* align on byte boundary */ s->last_eob_len = 8; /* enough lookahead for inflate */ @@ -1285,11 +1202,11 @@ local void copy_block(s, buf, len, header) if (header) { put_short(s, (ush)len); put_short(s, (ush)~len); -#ifdef __WXDEBUG__ +#ifdef DEBUG s->bits_sent += 2*16; #endif } -#ifdef __WXDEBUG__ +#ifdef DEBUG s->bits_sent += (ulg)len<<3; #endif while (len--) {