Dependencies:   emwin_lib

Fork of DMemWin by Embedded Artists

Committer:
destinyXfate
Date:
Thu Jun 02 04:52:54 2016 +0000
Revision:
2:0e2ef1edf01b
;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
destinyXfate 2:0e2ef1edf01b 1 /* trees.c -- output deflated data using Huffman coding
destinyXfate 2:0e2ef1edf01b 2 * Copyright (C) 1995-2005 Jean-loup Gailly
destinyXfate 2:0e2ef1edf01b 3 * For conditions of distribution and use, see copyright notice in zlib.h
destinyXfate 2:0e2ef1edf01b 4 */
destinyXfate 2:0e2ef1edf01b 5
destinyXfate 2:0e2ef1edf01b 6 /*
destinyXfate 2:0e2ef1edf01b 7 * ALGORITHM
destinyXfate 2:0e2ef1edf01b 8 *
destinyXfate 2:0e2ef1edf01b 9 * The "deflation" process uses several Huffman trees. The more
destinyXfate 2:0e2ef1edf01b 10 * common source values are represented by shorter bit sequences.
destinyXfate 2:0e2ef1edf01b 11 *
destinyXfate 2:0e2ef1edf01b 12 * Each code tree is stored in a compressed form which is itself
destinyXfate 2:0e2ef1edf01b 13 * a Huffman encoding of the lengths of all the code strings (in
destinyXfate 2:0e2ef1edf01b 14 * ascending order by source values). The actual code strings are
destinyXfate 2:0e2ef1edf01b 15 * reconstructed from the lengths in the inflate process, as described
destinyXfate 2:0e2ef1edf01b 16 * in the deflate specification.
destinyXfate 2:0e2ef1edf01b 17 *
destinyXfate 2:0e2ef1edf01b 18 * REFERENCES
destinyXfate 2:0e2ef1edf01b 19 *
destinyXfate 2:0e2ef1edf01b 20 * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
destinyXfate 2:0e2ef1edf01b 21 * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
destinyXfate 2:0e2ef1edf01b 22 *
destinyXfate 2:0e2ef1edf01b 23 * Storer, James A.
destinyXfate 2:0e2ef1edf01b 24 * Data Compression: Methods and Theory, pp. 49-50.
destinyXfate 2:0e2ef1edf01b 25 * Computer Science Press, 1988. ISBN 0-7167-8156-5.
destinyXfate 2:0e2ef1edf01b 26 *
destinyXfate 2:0e2ef1edf01b 27 * Sedgewick, R.
destinyXfate 2:0e2ef1edf01b 28 * Algorithms, p290.
destinyXfate 2:0e2ef1edf01b 29 * Addison-Wesley, 1983. ISBN 0-201-06672-6.
destinyXfate 2:0e2ef1edf01b 30 */
destinyXfate 2:0e2ef1edf01b 31
destinyXfate 2:0e2ef1edf01b 32 /* @(#) $Id$ */
destinyXfate 2:0e2ef1edf01b 33
destinyXfate 2:0e2ef1edf01b 34 /* #define GEN_TREES_H */
destinyXfate 2:0e2ef1edf01b 35
destinyXfate 2:0e2ef1edf01b 36 #include "deflate.h"
destinyXfate 2:0e2ef1edf01b 37
destinyXfate 2:0e2ef1edf01b 38 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 39 # include <ctype.h>
destinyXfate 2:0e2ef1edf01b 40 #endif
destinyXfate 2:0e2ef1edf01b 41
destinyXfate 2:0e2ef1edf01b 42 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 43 * Constants
destinyXfate 2:0e2ef1edf01b 44 */
destinyXfate 2:0e2ef1edf01b 45
destinyXfate 2:0e2ef1edf01b 46 #define MAX_BL_BITS 7
destinyXfate 2:0e2ef1edf01b 47 /* Bit length codes must not exceed MAX_BL_BITS bits */
destinyXfate 2:0e2ef1edf01b 48
destinyXfate 2:0e2ef1edf01b 49 #define END_BLOCK 256
destinyXfate 2:0e2ef1edf01b 50 /* end of block literal code */
destinyXfate 2:0e2ef1edf01b 51
destinyXfate 2:0e2ef1edf01b 52 #define REP_3_6 16
destinyXfate 2:0e2ef1edf01b 53 /* repeat previous bit length 3-6 times (2 bits of repeat count) */
destinyXfate 2:0e2ef1edf01b 54
destinyXfate 2:0e2ef1edf01b 55 #define REPZ_3_10 17
destinyXfate 2:0e2ef1edf01b 56 /* repeat a zero length 3-10 times (3 bits of repeat count) */
destinyXfate 2:0e2ef1edf01b 57
destinyXfate 2:0e2ef1edf01b 58 #define REPZ_11_138 18
destinyXfate 2:0e2ef1edf01b 59 /* repeat a zero length 11-138 times (7 bits of repeat count) */
destinyXfate 2:0e2ef1edf01b 60
destinyXfate 2:0e2ef1edf01b 61 local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
destinyXfate 2:0e2ef1edf01b 62 = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
destinyXfate 2:0e2ef1edf01b 63
destinyXfate 2:0e2ef1edf01b 64 local const int extra_dbits[D_CODES] /* extra bits for each distance code */
destinyXfate 2:0e2ef1edf01b 65 = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
destinyXfate 2:0e2ef1edf01b 66
destinyXfate 2:0e2ef1edf01b 67 local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
destinyXfate 2:0e2ef1edf01b 68 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
destinyXfate 2:0e2ef1edf01b 69
destinyXfate 2:0e2ef1edf01b 70 local const uch bl_order[BL_CODES]
destinyXfate 2:0e2ef1edf01b 71 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
destinyXfate 2:0e2ef1edf01b 72 /* The lengths of the bit length codes are sent in order of decreasing
destinyXfate 2:0e2ef1edf01b 73 * probability, to avoid transmitting the lengths for unused bit length codes.
destinyXfate 2:0e2ef1edf01b 74 */
destinyXfate 2:0e2ef1edf01b 75
destinyXfate 2:0e2ef1edf01b 76 #define Buf_size (8 * 2*sizeof(char))
destinyXfate 2:0e2ef1edf01b 77 /* Number of bits used within bi_buf. (bi_buf might be implemented on
destinyXfate 2:0e2ef1edf01b 78 * more than 16 bits on some systems.)
destinyXfate 2:0e2ef1edf01b 79 */
destinyXfate 2:0e2ef1edf01b 80
destinyXfate 2:0e2ef1edf01b 81 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 82 * Local data. These are initialized only once.
destinyXfate 2:0e2ef1edf01b 83 */
destinyXfate 2:0e2ef1edf01b 84
destinyXfate 2:0e2ef1edf01b 85 #define DIST_CODE_LEN 512 /* see definition of array dist_code below */
destinyXfate 2:0e2ef1edf01b 86
destinyXfate 2:0e2ef1edf01b 87 #if defined(GEN_TREES_H) || !defined(STDC)
destinyXfate 2:0e2ef1edf01b 88 /* non ANSI compilers may not accept trees.h */
destinyXfate 2:0e2ef1edf01b 89
destinyXfate 2:0e2ef1edf01b 90 local ct_data static_ltree[L_CODES+2];
destinyXfate 2:0e2ef1edf01b 91 /* The static literal tree. Since the bit lengths are imposed, there is no
destinyXfate 2:0e2ef1edf01b 92 * need for the L_CODES extra codes used during heap construction. However
destinyXfate 2:0e2ef1edf01b 93 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
destinyXfate 2:0e2ef1edf01b 94 * below).
destinyXfate 2:0e2ef1edf01b 95 */
destinyXfate 2:0e2ef1edf01b 96
destinyXfate 2:0e2ef1edf01b 97 local ct_data static_dtree[D_CODES];
destinyXfate 2:0e2ef1edf01b 98 /* The static distance tree. (Actually a trivial tree since all codes use
destinyXfate 2:0e2ef1edf01b 99 * 5 bits.)
destinyXfate 2:0e2ef1edf01b 100 */
destinyXfate 2:0e2ef1edf01b 101
destinyXfate 2:0e2ef1edf01b 102 uch _dist_code[DIST_CODE_LEN];
destinyXfate 2:0e2ef1edf01b 103 /* Distance codes. The first 256 values correspond to the distances
destinyXfate 2:0e2ef1edf01b 104 * 3 .. 258, the last 256 values correspond to the top 8 bits of
destinyXfate 2:0e2ef1edf01b 105 * the 15 bit distances.
destinyXfate 2:0e2ef1edf01b 106 */
destinyXfate 2:0e2ef1edf01b 107
destinyXfate 2:0e2ef1edf01b 108 uch _length_code[MAX_MATCH-MIN_MATCH+1];
destinyXfate 2:0e2ef1edf01b 109 /* length code for each normalized match length (0 == MIN_MATCH) */
destinyXfate 2:0e2ef1edf01b 110
destinyXfate 2:0e2ef1edf01b 111 local int base_length[LENGTH_CODES];
destinyXfate 2:0e2ef1edf01b 112 /* First normalized length for each code (0 = MIN_MATCH) */
destinyXfate 2:0e2ef1edf01b 113
destinyXfate 2:0e2ef1edf01b 114 local int base_dist[D_CODES];
destinyXfate 2:0e2ef1edf01b 115 /* First normalized distance for each code (0 = distance of 1) */
destinyXfate 2:0e2ef1edf01b 116
destinyXfate 2:0e2ef1edf01b 117 #else
destinyXfate 2:0e2ef1edf01b 118 # include "trees.h"
destinyXfate 2:0e2ef1edf01b 119 #endif /* GEN_TREES_H */
destinyXfate 2:0e2ef1edf01b 120
destinyXfate 2:0e2ef1edf01b 121 struct static_tree_desc_s {
destinyXfate 2:0e2ef1edf01b 122 const ct_data *static_tree; /* static tree or NULL */
destinyXfate 2:0e2ef1edf01b 123 const intf *extra_bits; /* extra bits for each code or NULL */
destinyXfate 2:0e2ef1edf01b 124 int extra_base; /* base index for extra_bits */
destinyXfate 2:0e2ef1edf01b 125 int elems; /* max number of elements in the tree */
destinyXfate 2:0e2ef1edf01b 126 int max_length; /* max bit length for the codes */
destinyXfate 2:0e2ef1edf01b 127 };
destinyXfate 2:0e2ef1edf01b 128
destinyXfate 2:0e2ef1edf01b 129 local static_tree_desc static_l_desc =
destinyXfate 2:0e2ef1edf01b 130 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
destinyXfate 2:0e2ef1edf01b 131
destinyXfate 2:0e2ef1edf01b 132 local static_tree_desc static_d_desc =
destinyXfate 2:0e2ef1edf01b 133 {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
destinyXfate 2:0e2ef1edf01b 134
destinyXfate 2:0e2ef1edf01b 135 local static_tree_desc static_bl_desc =
destinyXfate 2:0e2ef1edf01b 136 {(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
destinyXfate 2:0e2ef1edf01b 137
destinyXfate 2:0e2ef1edf01b 138 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 139 * Local (static) routines in this file.
destinyXfate 2:0e2ef1edf01b 140 */
destinyXfate 2:0e2ef1edf01b 141
destinyXfate 2:0e2ef1edf01b 142 local void tr_static_init OF((void));
destinyXfate 2:0e2ef1edf01b 143 local void init_block OF((deflate_state *s));
destinyXfate 2:0e2ef1edf01b 144 local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
destinyXfate 2:0e2ef1edf01b 145 local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
destinyXfate 2:0e2ef1edf01b 146 local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
destinyXfate 2:0e2ef1edf01b 147 local void build_tree OF((deflate_state *s, tree_desc *desc));
destinyXfate 2:0e2ef1edf01b 148 local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
destinyXfate 2:0e2ef1edf01b 149 local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
destinyXfate 2:0e2ef1edf01b 150 local int build_bl_tree OF((deflate_state *s));
destinyXfate 2:0e2ef1edf01b 151 local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
destinyXfate 2:0e2ef1edf01b 152 int blcodes));
destinyXfate 2:0e2ef1edf01b 153 local void compress_block OF((deflate_state *s, ct_data *ltree,
destinyXfate 2:0e2ef1edf01b 154 ct_data *dtree));
destinyXfate 2:0e2ef1edf01b 155 local void set_data_type OF((deflate_state *s));
destinyXfate 2:0e2ef1edf01b 156 local unsigned bi_reverse OF((unsigned value, int length));
destinyXfate 2:0e2ef1edf01b 157 local void bi_windup OF((deflate_state *s));
destinyXfate 2:0e2ef1edf01b 158 local void bi_flush OF((deflate_state *s));
destinyXfate 2:0e2ef1edf01b 159 local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
destinyXfate 2:0e2ef1edf01b 160 int header));
destinyXfate 2:0e2ef1edf01b 161
destinyXfate 2:0e2ef1edf01b 162 #ifdef GEN_TREES_H
destinyXfate 2:0e2ef1edf01b 163 local void gen_trees_header OF((void));
destinyXfate 2:0e2ef1edf01b 164 #endif
destinyXfate 2:0e2ef1edf01b 165
destinyXfate 2:0e2ef1edf01b 166 #ifndef DEBUG
destinyXfate 2:0e2ef1edf01b 167 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
destinyXfate 2:0e2ef1edf01b 168 /* Send a code of the given tree. c and tree must not have side effects */
destinyXfate 2:0e2ef1edf01b 169
destinyXfate 2:0e2ef1edf01b 170 #else /* DEBUG */
destinyXfate 2:0e2ef1edf01b 171 # define send_code(s, c, tree) \
destinyXfate 2:0e2ef1edf01b 172 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
destinyXfate 2:0e2ef1edf01b 173 send_bits(s, tree[c].Code, tree[c].Len); }
destinyXfate 2:0e2ef1edf01b 174 #endif
destinyXfate 2:0e2ef1edf01b 175
destinyXfate 2:0e2ef1edf01b 176 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 177 * Output a short LSB first on the stream.
destinyXfate 2:0e2ef1edf01b 178 * IN assertion: there is enough room in pendingBuf.
destinyXfate 2:0e2ef1edf01b 179 */
destinyXfate 2:0e2ef1edf01b 180 #define put_short(s, w) { \
destinyXfate 2:0e2ef1edf01b 181 put_byte(s, (uch)((w) & 0xff)); \
destinyXfate 2:0e2ef1edf01b 182 put_byte(s, (uch)((ush)(w) >> 8)); \
destinyXfate 2:0e2ef1edf01b 183 }
destinyXfate 2:0e2ef1edf01b 184
destinyXfate 2:0e2ef1edf01b 185 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 186 * Send a value on a given number of bits.
destinyXfate 2:0e2ef1edf01b 187 * IN assertion: length <= 16 and value fits in length bits.
destinyXfate 2:0e2ef1edf01b 188 */
destinyXfate 2:0e2ef1edf01b 189 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 190 local void send_bits OF((deflate_state *s, int value, int length));
destinyXfate 2:0e2ef1edf01b 191
destinyXfate 2:0e2ef1edf01b 192 local void send_bits(s, value, length)
destinyXfate 2:0e2ef1edf01b 193 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 194 int value; /* value to send */
destinyXfate 2:0e2ef1edf01b 195 int length; /* number of bits */
destinyXfate 2:0e2ef1edf01b 196 {
destinyXfate 2:0e2ef1edf01b 197 Tracevv((stderr," l %2d v %4x ", length, value));
destinyXfate 2:0e2ef1edf01b 198 Assert(length > 0 && length <= 15, "invalid length");
destinyXfate 2:0e2ef1edf01b 199 s->bits_sent += (ulg)length;
destinyXfate 2:0e2ef1edf01b 200
destinyXfate 2:0e2ef1edf01b 201 /* If not enough room in bi_buf, use (valid) bits from bi_buf and
destinyXfate 2:0e2ef1edf01b 202 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
destinyXfate 2:0e2ef1edf01b 203 * unused bits in value.
destinyXfate 2:0e2ef1edf01b 204 */
destinyXfate 2:0e2ef1edf01b 205 if (s->bi_valid > (int)Buf_size - length) {
destinyXfate 2:0e2ef1edf01b 206 s->bi_buf |= (value << s->bi_valid);
destinyXfate 2:0e2ef1edf01b 207 put_short(s, s->bi_buf);
destinyXfate 2:0e2ef1edf01b 208 s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
destinyXfate 2:0e2ef1edf01b 209 s->bi_valid += length - Buf_size;
destinyXfate 2:0e2ef1edf01b 210 } else {
destinyXfate 2:0e2ef1edf01b 211 s->bi_buf |= value << s->bi_valid;
destinyXfate 2:0e2ef1edf01b 212 s->bi_valid += length;
destinyXfate 2:0e2ef1edf01b 213 }
destinyXfate 2:0e2ef1edf01b 214 }
destinyXfate 2:0e2ef1edf01b 215 #else /* !DEBUG */
destinyXfate 2:0e2ef1edf01b 216
destinyXfate 2:0e2ef1edf01b 217 #define send_bits(s, value, length) \
destinyXfate 2:0e2ef1edf01b 218 { int len = length;\
destinyXfate 2:0e2ef1edf01b 219 if (s->bi_valid > (int)Buf_size - len) {\
destinyXfate 2:0e2ef1edf01b 220 int val = value;\
destinyXfate 2:0e2ef1edf01b 221 s->bi_buf |= (val << s->bi_valid);\
destinyXfate 2:0e2ef1edf01b 222 put_short(s, s->bi_buf);\
destinyXfate 2:0e2ef1edf01b 223 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
destinyXfate 2:0e2ef1edf01b 224 s->bi_valid += len - Buf_size;\
destinyXfate 2:0e2ef1edf01b 225 } else {\
destinyXfate 2:0e2ef1edf01b 226 s->bi_buf |= (value) << s->bi_valid;\
destinyXfate 2:0e2ef1edf01b 227 s->bi_valid += len;\
destinyXfate 2:0e2ef1edf01b 228 }\
destinyXfate 2:0e2ef1edf01b 229 }
destinyXfate 2:0e2ef1edf01b 230 #endif /* DEBUG */
destinyXfate 2:0e2ef1edf01b 231
destinyXfate 2:0e2ef1edf01b 232
destinyXfate 2:0e2ef1edf01b 233 /* the arguments must not have side effects */
destinyXfate 2:0e2ef1edf01b 234
destinyXfate 2:0e2ef1edf01b 235 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 236 * Initialize the various 'constant' tables.
destinyXfate 2:0e2ef1edf01b 237 */
destinyXfate 2:0e2ef1edf01b 238 local void tr_static_init()
destinyXfate 2:0e2ef1edf01b 239 {
destinyXfate 2:0e2ef1edf01b 240 #if defined(GEN_TREES_H) || !defined(STDC)
destinyXfate 2:0e2ef1edf01b 241 static int static_init_done = 0;
destinyXfate 2:0e2ef1edf01b 242 int n; /* iterates over tree elements */
destinyXfate 2:0e2ef1edf01b 243 int bits; /* bit counter */
destinyXfate 2:0e2ef1edf01b 244 int length; /* length value */
destinyXfate 2:0e2ef1edf01b 245 int code; /* code value */
destinyXfate 2:0e2ef1edf01b 246 int dist; /* distance index */
destinyXfate 2:0e2ef1edf01b 247 ush bl_count[MAX_BITS+1];
destinyXfate 2:0e2ef1edf01b 248 /* number of codes at each bit length for an optimal tree */
destinyXfate 2:0e2ef1edf01b 249
destinyXfate 2:0e2ef1edf01b 250 if (static_init_done) return;
destinyXfate 2:0e2ef1edf01b 251
destinyXfate 2:0e2ef1edf01b 252 /* For some embedded targets, global variables are not initialized: */
destinyXfate 2:0e2ef1edf01b 253 static_l_desc.static_tree = static_ltree;
destinyXfate 2:0e2ef1edf01b 254 static_l_desc.extra_bits = extra_lbits;
destinyXfate 2:0e2ef1edf01b 255 static_d_desc.static_tree = static_dtree;
destinyXfate 2:0e2ef1edf01b 256 static_d_desc.extra_bits = extra_dbits;
destinyXfate 2:0e2ef1edf01b 257 static_bl_desc.extra_bits = extra_blbits;
destinyXfate 2:0e2ef1edf01b 258
destinyXfate 2:0e2ef1edf01b 259 /* Initialize the mapping length (0..255) -> length code (0..28) */
destinyXfate 2:0e2ef1edf01b 260 length = 0;
destinyXfate 2:0e2ef1edf01b 261 for (code = 0; code < LENGTH_CODES-1; code++) {
destinyXfate 2:0e2ef1edf01b 262 base_length[code] = length;
destinyXfate 2:0e2ef1edf01b 263 for (n = 0; n < (1<<extra_lbits[code]); n++) {
destinyXfate 2:0e2ef1edf01b 264 _length_code[length++] = (uch)code;
destinyXfate 2:0e2ef1edf01b 265 }
destinyXfate 2:0e2ef1edf01b 266 }
destinyXfate 2:0e2ef1edf01b 267 Assert (length == 256, "tr_static_init: length != 256");
destinyXfate 2:0e2ef1edf01b 268 /* Note that the length 255 (match length 258) can be represented
destinyXfate 2:0e2ef1edf01b 269 * in two different ways: code 284 + 5 bits or code 285, so we
destinyXfate 2:0e2ef1edf01b 270 * overwrite length_code[255] to use the best encoding:
destinyXfate 2:0e2ef1edf01b 271 */
destinyXfate 2:0e2ef1edf01b 272 _length_code[length-1] = (uch)code;
destinyXfate 2:0e2ef1edf01b 273
destinyXfate 2:0e2ef1edf01b 274 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
destinyXfate 2:0e2ef1edf01b 275 dist = 0;
destinyXfate 2:0e2ef1edf01b 276 for (code = 0 ; code < 16; code++) {
destinyXfate 2:0e2ef1edf01b 277 base_dist[code] = dist;
destinyXfate 2:0e2ef1edf01b 278 for (n = 0; n < (1<<extra_dbits[code]); n++) {
destinyXfate 2:0e2ef1edf01b 279 _dist_code[dist++] = (uch)code;
destinyXfate 2:0e2ef1edf01b 280 }
destinyXfate 2:0e2ef1edf01b 281 }
destinyXfate 2:0e2ef1edf01b 282 Assert (dist == 256, "tr_static_init: dist != 256");
destinyXfate 2:0e2ef1edf01b 283 dist >>= 7; /* from now on, all distances are divided by 128 */
destinyXfate 2:0e2ef1edf01b 284 for ( ; code < D_CODES; code++) {
destinyXfate 2:0e2ef1edf01b 285 base_dist[code] = dist << 7;
destinyXfate 2:0e2ef1edf01b 286 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
destinyXfate 2:0e2ef1edf01b 287 _dist_code[256 + dist++] = (uch)code;
destinyXfate 2:0e2ef1edf01b 288 }
destinyXfate 2:0e2ef1edf01b 289 }
destinyXfate 2:0e2ef1edf01b 290 Assert (dist == 256, "tr_static_init: 256+dist != 512");
destinyXfate 2:0e2ef1edf01b 291
destinyXfate 2:0e2ef1edf01b 292 /* Construct the codes of the static literal tree */
destinyXfate 2:0e2ef1edf01b 293 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
destinyXfate 2:0e2ef1edf01b 294 n = 0;
destinyXfate 2:0e2ef1edf01b 295 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
destinyXfate 2:0e2ef1edf01b 296 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
destinyXfate 2:0e2ef1edf01b 297 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
destinyXfate 2:0e2ef1edf01b 298 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
destinyXfate 2:0e2ef1edf01b 299 /* Codes 286 and 287 do not exist, but we must include them in the
destinyXfate 2:0e2ef1edf01b 300 * tree construction to get a canonical Huffman tree (longest code
destinyXfate 2:0e2ef1edf01b 301 * all ones)
destinyXfate 2:0e2ef1edf01b 302 */
destinyXfate 2:0e2ef1edf01b 303 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
destinyXfate 2:0e2ef1edf01b 304
destinyXfate 2:0e2ef1edf01b 305 /* The static distance tree is trivial: */
destinyXfate 2:0e2ef1edf01b 306 for (n = 0; n < D_CODES; n++) {
destinyXfate 2:0e2ef1edf01b 307 static_dtree[n].Len = 5;
destinyXfate 2:0e2ef1edf01b 308 static_dtree[n].Code = bi_reverse((unsigned)n, 5);
destinyXfate 2:0e2ef1edf01b 309 }
destinyXfate 2:0e2ef1edf01b 310 static_init_done = 1;
destinyXfate 2:0e2ef1edf01b 311
destinyXfate 2:0e2ef1edf01b 312 # ifdef GEN_TREES_H
destinyXfate 2:0e2ef1edf01b 313 gen_trees_header();
destinyXfate 2:0e2ef1edf01b 314 # endif
destinyXfate 2:0e2ef1edf01b 315 #endif /* defined(GEN_TREES_H) || !defined(STDC) */
destinyXfate 2:0e2ef1edf01b 316 }
destinyXfate 2:0e2ef1edf01b 317
destinyXfate 2:0e2ef1edf01b 318 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 319 * Genererate the file trees.h describing the static trees.
destinyXfate 2:0e2ef1edf01b 320 */
destinyXfate 2:0e2ef1edf01b 321 #ifdef GEN_TREES_H
destinyXfate 2:0e2ef1edf01b 322 # ifndef DEBUG
destinyXfate 2:0e2ef1edf01b 323 # include <stdio.h>
destinyXfate 2:0e2ef1edf01b 324 # endif
destinyXfate 2:0e2ef1edf01b 325
destinyXfate 2:0e2ef1edf01b 326 # define SEPARATOR(i, last, width) \
destinyXfate 2:0e2ef1edf01b 327 ((i) == (last)? "\n};\n\n" : \
destinyXfate 2:0e2ef1edf01b 328 ((i) % (width) == (width)-1 ? ",\n" : ", "))
destinyXfate 2:0e2ef1edf01b 329
destinyXfate 2:0e2ef1edf01b 330 void gen_trees_header()
destinyXfate 2:0e2ef1edf01b 331 {
destinyXfate 2:0e2ef1edf01b 332 FILE *header = fopen("trees.h", "w");
destinyXfate 2:0e2ef1edf01b 333 int i;
destinyXfate 2:0e2ef1edf01b 334
destinyXfate 2:0e2ef1edf01b 335 Assert (header != NULL, "Can't open trees.h");
destinyXfate 2:0e2ef1edf01b 336 fprintf(header,
destinyXfate 2:0e2ef1edf01b 337 "/* header created automatically with -DGEN_TREES_H */\n\n");
destinyXfate 2:0e2ef1edf01b 338
destinyXfate 2:0e2ef1edf01b 339 fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
destinyXfate 2:0e2ef1edf01b 340 for (i = 0; i < L_CODES+2; i++) {
destinyXfate 2:0e2ef1edf01b 341 fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
destinyXfate 2:0e2ef1edf01b 342 static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
destinyXfate 2:0e2ef1edf01b 343 }
destinyXfate 2:0e2ef1edf01b 344
destinyXfate 2:0e2ef1edf01b 345 fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
destinyXfate 2:0e2ef1edf01b 346 for (i = 0; i < D_CODES; i++) {
destinyXfate 2:0e2ef1edf01b 347 fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
destinyXfate 2:0e2ef1edf01b 348 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
destinyXfate 2:0e2ef1edf01b 349 }
destinyXfate 2:0e2ef1edf01b 350
destinyXfate 2:0e2ef1edf01b 351 fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
destinyXfate 2:0e2ef1edf01b 352 for (i = 0; i < DIST_CODE_LEN; i++) {
destinyXfate 2:0e2ef1edf01b 353 fprintf(header, "%2u%s", _dist_code[i],
destinyXfate 2:0e2ef1edf01b 354 SEPARATOR(i, DIST_CODE_LEN-1, 20));
destinyXfate 2:0e2ef1edf01b 355 }
destinyXfate 2:0e2ef1edf01b 356
destinyXfate 2:0e2ef1edf01b 357 fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
destinyXfate 2:0e2ef1edf01b 358 for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
destinyXfate 2:0e2ef1edf01b 359 fprintf(header, "%2u%s", _length_code[i],
destinyXfate 2:0e2ef1edf01b 360 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
destinyXfate 2:0e2ef1edf01b 361 }
destinyXfate 2:0e2ef1edf01b 362
destinyXfate 2:0e2ef1edf01b 363 fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
destinyXfate 2:0e2ef1edf01b 364 for (i = 0; i < LENGTH_CODES; i++) {
destinyXfate 2:0e2ef1edf01b 365 fprintf(header, "%1u%s", base_length[i],
destinyXfate 2:0e2ef1edf01b 366 SEPARATOR(i, LENGTH_CODES-1, 20));
destinyXfate 2:0e2ef1edf01b 367 }
destinyXfate 2:0e2ef1edf01b 368
destinyXfate 2:0e2ef1edf01b 369 fprintf(header, "local const int base_dist[D_CODES] = {\n");
destinyXfate 2:0e2ef1edf01b 370 for (i = 0; i < D_CODES; i++) {
destinyXfate 2:0e2ef1edf01b 371 fprintf(header, "%5u%s", base_dist[i],
destinyXfate 2:0e2ef1edf01b 372 SEPARATOR(i, D_CODES-1, 10));
destinyXfate 2:0e2ef1edf01b 373 }
destinyXfate 2:0e2ef1edf01b 374
destinyXfate 2:0e2ef1edf01b 375 fclose(header);
destinyXfate 2:0e2ef1edf01b 376 }
destinyXfate 2:0e2ef1edf01b 377 #endif /* GEN_TREES_H */
destinyXfate 2:0e2ef1edf01b 378
destinyXfate 2:0e2ef1edf01b 379 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 380 * Initialize the tree data structures for a new zlib stream.
destinyXfate 2:0e2ef1edf01b 381 */
destinyXfate 2:0e2ef1edf01b 382 void _tr_init(s)
destinyXfate 2:0e2ef1edf01b 383 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 384 {
destinyXfate 2:0e2ef1edf01b 385 tr_static_init();
destinyXfate 2:0e2ef1edf01b 386
destinyXfate 2:0e2ef1edf01b 387 s->l_desc.dyn_tree = s->dyn_ltree;
destinyXfate 2:0e2ef1edf01b 388 s->l_desc.stat_desc = &static_l_desc;
destinyXfate 2:0e2ef1edf01b 389
destinyXfate 2:0e2ef1edf01b 390 s->d_desc.dyn_tree = s->dyn_dtree;
destinyXfate 2:0e2ef1edf01b 391 s->d_desc.stat_desc = &static_d_desc;
destinyXfate 2:0e2ef1edf01b 392
destinyXfate 2:0e2ef1edf01b 393 s->bl_desc.dyn_tree = s->bl_tree;
destinyXfate 2:0e2ef1edf01b 394 s->bl_desc.stat_desc = &static_bl_desc;
destinyXfate 2:0e2ef1edf01b 395
destinyXfate 2:0e2ef1edf01b 396 s->bi_buf = 0;
destinyXfate 2:0e2ef1edf01b 397 s->bi_valid = 0;
destinyXfate 2:0e2ef1edf01b 398 s->last_eob_len = 8; /* enough lookahead for inflate */
destinyXfate 2:0e2ef1edf01b 399 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 400 s->compressed_len = 0L;
destinyXfate 2:0e2ef1edf01b 401 s->bits_sent = 0L;
destinyXfate 2:0e2ef1edf01b 402 #endif
destinyXfate 2:0e2ef1edf01b 403
destinyXfate 2:0e2ef1edf01b 404 /* Initialize the first block of the first file: */
destinyXfate 2:0e2ef1edf01b 405 init_block(s);
destinyXfate 2:0e2ef1edf01b 406 }
destinyXfate 2:0e2ef1edf01b 407
destinyXfate 2:0e2ef1edf01b 408 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 409 * Initialize a new block.
destinyXfate 2:0e2ef1edf01b 410 */
destinyXfate 2:0e2ef1edf01b 411 local void init_block(s)
destinyXfate 2:0e2ef1edf01b 412 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 413 {
destinyXfate 2:0e2ef1edf01b 414 int n; /* iterates over tree elements */
destinyXfate 2:0e2ef1edf01b 415
destinyXfate 2:0e2ef1edf01b 416 /* Initialize the trees. */
destinyXfate 2:0e2ef1edf01b 417 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
destinyXfate 2:0e2ef1edf01b 418 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
destinyXfate 2:0e2ef1edf01b 419 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
destinyXfate 2:0e2ef1edf01b 420
destinyXfate 2:0e2ef1edf01b 421 s->dyn_ltree[END_BLOCK].Freq = 1;
destinyXfate 2:0e2ef1edf01b 422 s->opt_len = s->static_len = 0L;
destinyXfate 2:0e2ef1edf01b 423 s->last_lit = s->matches = 0;
destinyXfate 2:0e2ef1edf01b 424 }
destinyXfate 2:0e2ef1edf01b 425
destinyXfate 2:0e2ef1edf01b 426 #define SMALLEST 1
destinyXfate 2:0e2ef1edf01b 427 /* Index within the heap array of least frequent node in the Huffman tree */
destinyXfate 2:0e2ef1edf01b 428
destinyXfate 2:0e2ef1edf01b 429
destinyXfate 2:0e2ef1edf01b 430 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 431 * Remove the smallest element from the heap and recreate the heap with
destinyXfate 2:0e2ef1edf01b 432 * one less element. Updates heap and heap_len.
destinyXfate 2:0e2ef1edf01b 433 */
destinyXfate 2:0e2ef1edf01b 434 #define pqremove(s, tree, top) \
destinyXfate 2:0e2ef1edf01b 435 {\
destinyXfate 2:0e2ef1edf01b 436 top = s->heap[SMALLEST]; \
destinyXfate 2:0e2ef1edf01b 437 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
destinyXfate 2:0e2ef1edf01b 438 pqdownheap(s, tree, SMALLEST); \
destinyXfate 2:0e2ef1edf01b 439 }
destinyXfate 2:0e2ef1edf01b 440
destinyXfate 2:0e2ef1edf01b 441 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 442 * Compares to subtrees, using the tree depth as tie breaker when
destinyXfate 2:0e2ef1edf01b 443 * the subtrees have equal frequency. This minimizes the worst case length.
destinyXfate 2:0e2ef1edf01b 444 */
destinyXfate 2:0e2ef1edf01b 445 #define smaller(tree, n, m, depth) \
destinyXfate 2:0e2ef1edf01b 446 (tree[n].Freq < tree[m].Freq || \
destinyXfate 2:0e2ef1edf01b 447 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
destinyXfate 2:0e2ef1edf01b 448
destinyXfate 2:0e2ef1edf01b 449 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 450 * Restore the heap property by moving down the tree starting at node k,
destinyXfate 2:0e2ef1edf01b 451 * exchanging a node with the smallest of its two sons if necessary, stopping
destinyXfate 2:0e2ef1edf01b 452 * when the heap property is re-established (each father smaller than its
destinyXfate 2:0e2ef1edf01b 453 * two sons).
destinyXfate 2:0e2ef1edf01b 454 */
destinyXfate 2:0e2ef1edf01b 455 local void pqdownheap(s, tree, k)
destinyXfate 2:0e2ef1edf01b 456 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 457 ct_data *tree; /* the tree to restore */
destinyXfate 2:0e2ef1edf01b 458 int k; /* node to move down */
destinyXfate 2:0e2ef1edf01b 459 {
destinyXfate 2:0e2ef1edf01b 460 int v = s->heap[k];
destinyXfate 2:0e2ef1edf01b 461 int j = k << 1; /* left son of k */
destinyXfate 2:0e2ef1edf01b 462 while (j <= s->heap_len) {
destinyXfate 2:0e2ef1edf01b 463 /* Set j to the smallest of the two sons: */
destinyXfate 2:0e2ef1edf01b 464 if (j < s->heap_len &&
destinyXfate 2:0e2ef1edf01b 465 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
destinyXfate 2:0e2ef1edf01b 466 j++;
destinyXfate 2:0e2ef1edf01b 467 }
destinyXfate 2:0e2ef1edf01b 468 /* Exit if v is smaller than both sons */
destinyXfate 2:0e2ef1edf01b 469 if (smaller(tree, v, s->heap[j], s->depth)) break;
destinyXfate 2:0e2ef1edf01b 470
destinyXfate 2:0e2ef1edf01b 471 /* Exchange v with the smallest son */
destinyXfate 2:0e2ef1edf01b 472 s->heap[k] = s->heap[j]; k = j;
destinyXfate 2:0e2ef1edf01b 473
destinyXfate 2:0e2ef1edf01b 474 /* And continue down the tree, setting j to the left son of k */
destinyXfate 2:0e2ef1edf01b 475 j <<= 1;
destinyXfate 2:0e2ef1edf01b 476 }
destinyXfate 2:0e2ef1edf01b 477 s->heap[k] = v;
destinyXfate 2:0e2ef1edf01b 478 }
destinyXfate 2:0e2ef1edf01b 479
destinyXfate 2:0e2ef1edf01b 480 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 481 * Compute the optimal bit lengths for a tree and update the total bit length
destinyXfate 2:0e2ef1edf01b 482 * for the current block.
destinyXfate 2:0e2ef1edf01b 483 * IN assertion: the fields freq and dad are set, heap[heap_max] and
destinyXfate 2:0e2ef1edf01b 484 * above are the tree nodes sorted by increasing frequency.
destinyXfate 2:0e2ef1edf01b 485 * OUT assertions: the field len is set to the optimal bit length, the
destinyXfate 2:0e2ef1edf01b 486 * array bl_count contains the frequencies for each bit length.
destinyXfate 2:0e2ef1edf01b 487 * The length opt_len is updated; static_len is also updated if stree is
destinyXfate 2:0e2ef1edf01b 488 * not null.
destinyXfate 2:0e2ef1edf01b 489 */
destinyXfate 2:0e2ef1edf01b 490 local void gen_bitlen(s, desc)
destinyXfate 2:0e2ef1edf01b 491 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 492 tree_desc *desc; /* the tree descriptor */
destinyXfate 2:0e2ef1edf01b 493 {
destinyXfate 2:0e2ef1edf01b 494 ct_data *tree = desc->dyn_tree;
destinyXfate 2:0e2ef1edf01b 495 int max_code = desc->max_code;
destinyXfate 2:0e2ef1edf01b 496 const ct_data *stree = desc->stat_desc->static_tree;
destinyXfate 2:0e2ef1edf01b 497 const intf *extra = desc->stat_desc->extra_bits;
destinyXfate 2:0e2ef1edf01b 498 int base = desc->stat_desc->extra_base;
destinyXfate 2:0e2ef1edf01b 499 int max_length = desc->stat_desc->max_length;
destinyXfate 2:0e2ef1edf01b 500 int h; /* heap index */
destinyXfate 2:0e2ef1edf01b 501 int n, m; /* iterate over the tree elements */
destinyXfate 2:0e2ef1edf01b 502 int bits; /* bit length */
destinyXfate 2:0e2ef1edf01b 503 int xbits; /* extra bits */
destinyXfate 2:0e2ef1edf01b 504 ush f; /* frequency */
destinyXfate 2:0e2ef1edf01b 505 int overflow = 0; /* number of elements with bit length too large */
destinyXfate 2:0e2ef1edf01b 506
destinyXfate 2:0e2ef1edf01b 507 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
destinyXfate 2:0e2ef1edf01b 508
destinyXfate 2:0e2ef1edf01b 509 /* In a first pass, compute the optimal bit lengths (which may
destinyXfate 2:0e2ef1edf01b 510 * overflow in the case of the bit length tree).
destinyXfate 2:0e2ef1edf01b 511 */
destinyXfate 2:0e2ef1edf01b 512 tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
destinyXfate 2:0e2ef1edf01b 513
destinyXfate 2:0e2ef1edf01b 514 for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
destinyXfate 2:0e2ef1edf01b 515 n = s->heap[h];
destinyXfate 2:0e2ef1edf01b 516 bits = tree[tree[n].Dad].Len + 1;
destinyXfate 2:0e2ef1edf01b 517 if (bits > max_length) bits = max_length, overflow++;
destinyXfate 2:0e2ef1edf01b 518 tree[n].Len = (ush)bits;
destinyXfate 2:0e2ef1edf01b 519 /* We overwrite tree[n].Dad which is no longer needed */
destinyXfate 2:0e2ef1edf01b 520
destinyXfate 2:0e2ef1edf01b 521 if (n > max_code) continue; /* not a leaf node */
destinyXfate 2:0e2ef1edf01b 522
destinyXfate 2:0e2ef1edf01b 523 s->bl_count[bits]++;
destinyXfate 2:0e2ef1edf01b 524 xbits = 0;
destinyXfate 2:0e2ef1edf01b 525 if (n >= base) xbits = extra[n-base];
destinyXfate 2:0e2ef1edf01b 526 f = tree[n].Freq;
destinyXfate 2:0e2ef1edf01b 527 s->opt_len += (ulg)f * (bits + xbits);
destinyXfate 2:0e2ef1edf01b 528 if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
destinyXfate 2:0e2ef1edf01b 529 }
destinyXfate 2:0e2ef1edf01b 530 if (overflow == 0) return;
destinyXfate 2:0e2ef1edf01b 531
destinyXfate 2:0e2ef1edf01b 532 Trace((stderr,"\nbit length overflow\n"));
destinyXfate 2:0e2ef1edf01b 533 /* This happens for example on obj2 and pic of the Calgary corpus */
destinyXfate 2:0e2ef1edf01b 534
destinyXfate 2:0e2ef1edf01b 535 /* Find the first bit length which could increase: */
destinyXfate 2:0e2ef1edf01b 536 do {
destinyXfate 2:0e2ef1edf01b 537 bits = max_length-1;
destinyXfate 2:0e2ef1edf01b 538 while (s->bl_count[bits] == 0) bits--;
destinyXfate 2:0e2ef1edf01b 539 s->bl_count[bits]--; /* move one leaf down the tree */
destinyXfate 2:0e2ef1edf01b 540 s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
destinyXfate 2:0e2ef1edf01b 541 s->bl_count[max_length]--;
destinyXfate 2:0e2ef1edf01b 542 /* The brother of the overflow item also moves one step up,
destinyXfate 2:0e2ef1edf01b 543 * but this does not affect bl_count[max_length]
destinyXfate 2:0e2ef1edf01b 544 */
destinyXfate 2:0e2ef1edf01b 545 overflow -= 2;
destinyXfate 2:0e2ef1edf01b 546 } while (overflow > 0);
destinyXfate 2:0e2ef1edf01b 547
destinyXfate 2:0e2ef1edf01b 548 /* Now recompute all bit lengths, scanning in increasing frequency.
destinyXfate 2:0e2ef1edf01b 549 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
destinyXfate 2:0e2ef1edf01b 550 * lengths instead of fixing only the wrong ones. This idea is taken
destinyXfate 2:0e2ef1edf01b 551 * from 'ar' written by Haruhiko Okumura.)
destinyXfate 2:0e2ef1edf01b 552 */
destinyXfate 2:0e2ef1edf01b 553 for (bits = max_length; bits != 0; bits--) {
destinyXfate 2:0e2ef1edf01b 554 n = s->bl_count[bits];
destinyXfate 2:0e2ef1edf01b 555 while (n != 0) {
destinyXfate 2:0e2ef1edf01b 556 m = s->heap[--h];
destinyXfate 2:0e2ef1edf01b 557 if (m > max_code) continue;
destinyXfate 2:0e2ef1edf01b 558 if ((unsigned) tree[m].Len != (unsigned) bits) {
destinyXfate 2:0e2ef1edf01b 559 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
destinyXfate 2:0e2ef1edf01b 560 s->opt_len += ((long)bits - (long)tree[m].Len)
destinyXfate 2:0e2ef1edf01b 561 *(long)tree[m].Freq;
destinyXfate 2:0e2ef1edf01b 562 tree[m].Len = (ush)bits;
destinyXfate 2:0e2ef1edf01b 563 }
destinyXfate 2:0e2ef1edf01b 564 n--;
destinyXfate 2:0e2ef1edf01b 565 }
destinyXfate 2:0e2ef1edf01b 566 }
destinyXfate 2:0e2ef1edf01b 567 }
destinyXfate 2:0e2ef1edf01b 568
destinyXfate 2:0e2ef1edf01b 569 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 570 * Generate the codes for a given tree and bit counts (which need not be
destinyXfate 2:0e2ef1edf01b 571 * optimal).
destinyXfate 2:0e2ef1edf01b 572 * IN assertion: the array bl_count contains the bit length statistics for
destinyXfate 2:0e2ef1edf01b 573 * the given tree and the field len is set for all tree elements.
destinyXfate 2:0e2ef1edf01b 574 * OUT assertion: the field code is set for all tree elements of non
destinyXfate 2:0e2ef1edf01b 575 * zero code length.
destinyXfate 2:0e2ef1edf01b 576 */
destinyXfate 2:0e2ef1edf01b 577 local void gen_codes (tree, max_code, bl_count)
destinyXfate 2:0e2ef1edf01b 578 ct_data *tree; /* the tree to decorate */
destinyXfate 2:0e2ef1edf01b 579 int max_code; /* largest code with non zero frequency */
destinyXfate 2:0e2ef1edf01b 580 ushf *bl_count; /* number of codes at each bit length */
destinyXfate 2:0e2ef1edf01b 581 {
destinyXfate 2:0e2ef1edf01b 582 ush next_code[MAX_BITS+1]; /* next code value for each bit length */
destinyXfate 2:0e2ef1edf01b 583 ush code = 0; /* running code value */
destinyXfate 2:0e2ef1edf01b 584 int bits; /* bit index */
destinyXfate 2:0e2ef1edf01b 585 int n; /* code index */
destinyXfate 2:0e2ef1edf01b 586
destinyXfate 2:0e2ef1edf01b 587 /* The distribution counts are first used to generate the code values
destinyXfate 2:0e2ef1edf01b 588 * without bit reversal.
destinyXfate 2:0e2ef1edf01b 589 */
destinyXfate 2:0e2ef1edf01b 590 for (bits = 1; bits <= MAX_BITS; bits++) {
destinyXfate 2:0e2ef1edf01b 591 next_code[bits] = code = (code + bl_count[bits-1]) << 1;
destinyXfate 2:0e2ef1edf01b 592 }
destinyXfate 2:0e2ef1edf01b 593 /* Check that the bit counts in bl_count are consistent. The last code
destinyXfate 2:0e2ef1edf01b 594 * must be all ones.
destinyXfate 2:0e2ef1edf01b 595 */
destinyXfate 2:0e2ef1edf01b 596 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
destinyXfate 2:0e2ef1edf01b 597 "inconsistent bit counts");
destinyXfate 2:0e2ef1edf01b 598 Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
destinyXfate 2:0e2ef1edf01b 599
destinyXfate 2:0e2ef1edf01b 600 for (n = 0; n <= max_code; n++) {
destinyXfate 2:0e2ef1edf01b 601 int len = tree[n].Len;
destinyXfate 2:0e2ef1edf01b 602 if (len == 0) continue;
destinyXfate 2:0e2ef1edf01b 603 /* Now reverse the bits */
destinyXfate 2:0e2ef1edf01b 604 tree[n].Code = bi_reverse(next_code[len]++, len);
destinyXfate 2:0e2ef1edf01b 605
destinyXfate 2:0e2ef1edf01b 606 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
destinyXfate 2:0e2ef1edf01b 607 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
destinyXfate 2:0e2ef1edf01b 608 }
destinyXfate 2:0e2ef1edf01b 609 }
destinyXfate 2:0e2ef1edf01b 610
destinyXfate 2:0e2ef1edf01b 611 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 612 * Construct one Huffman tree and assigns the code bit strings and lengths.
destinyXfate 2:0e2ef1edf01b 613 * Update the total bit length for the current block.
destinyXfate 2:0e2ef1edf01b 614 * IN assertion: the field freq is set for all tree elements.
destinyXfate 2:0e2ef1edf01b 615 * OUT assertions: the fields len and code are set to the optimal bit length
destinyXfate 2:0e2ef1edf01b 616 * and corresponding code. The length opt_len is updated; static_len is
destinyXfate 2:0e2ef1edf01b 617 * also updated if stree is not null. The field max_code is set.
destinyXfate 2:0e2ef1edf01b 618 */
destinyXfate 2:0e2ef1edf01b 619 local void build_tree(s, desc)
destinyXfate 2:0e2ef1edf01b 620 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 621 tree_desc *desc; /* the tree descriptor */
destinyXfate 2:0e2ef1edf01b 622 {
destinyXfate 2:0e2ef1edf01b 623 ct_data *tree = desc->dyn_tree;
destinyXfate 2:0e2ef1edf01b 624 const ct_data *stree = desc->stat_desc->static_tree;
destinyXfate 2:0e2ef1edf01b 625 int elems = desc->stat_desc->elems;
destinyXfate 2:0e2ef1edf01b 626 int n, m; /* iterate over heap elements */
destinyXfate 2:0e2ef1edf01b 627 int max_code = -1; /* largest code with non zero frequency */
destinyXfate 2:0e2ef1edf01b 628 int node; /* new node being created */
destinyXfate 2:0e2ef1edf01b 629
destinyXfate 2:0e2ef1edf01b 630 /* Construct the initial heap, with least frequent element in
destinyXfate 2:0e2ef1edf01b 631 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
destinyXfate 2:0e2ef1edf01b 632 * heap[0] is not used.
destinyXfate 2:0e2ef1edf01b 633 */
destinyXfate 2:0e2ef1edf01b 634 s->heap_len = 0, s->heap_max = HEAP_SIZE;
destinyXfate 2:0e2ef1edf01b 635
destinyXfate 2:0e2ef1edf01b 636 for (n = 0; n < elems; n++) {
destinyXfate 2:0e2ef1edf01b 637 if (tree[n].Freq != 0) {
destinyXfate 2:0e2ef1edf01b 638 s->heap[++(s->heap_len)] = max_code = n;
destinyXfate 2:0e2ef1edf01b 639 s->depth[n] = 0;
destinyXfate 2:0e2ef1edf01b 640 } else {
destinyXfate 2:0e2ef1edf01b 641 tree[n].Len = 0;
destinyXfate 2:0e2ef1edf01b 642 }
destinyXfate 2:0e2ef1edf01b 643 }
destinyXfate 2:0e2ef1edf01b 644
destinyXfate 2:0e2ef1edf01b 645 /* The pkzip format requires that at least one distance code exists,
destinyXfate 2:0e2ef1edf01b 646 * and that at least one bit should be sent even if there is only one
destinyXfate 2:0e2ef1edf01b 647 * possible code. So to avoid special checks later on we force at least
destinyXfate 2:0e2ef1edf01b 648 * two codes of non zero frequency.
destinyXfate 2:0e2ef1edf01b 649 */
destinyXfate 2:0e2ef1edf01b 650 while (s->heap_len < 2) {
destinyXfate 2:0e2ef1edf01b 651 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
destinyXfate 2:0e2ef1edf01b 652 tree[node].Freq = 1;
destinyXfate 2:0e2ef1edf01b 653 s->depth[node] = 0;
destinyXfate 2:0e2ef1edf01b 654 s->opt_len--; if (stree) s->static_len -= stree[node].Len;
destinyXfate 2:0e2ef1edf01b 655 /* node is 0 or 1 so it does not have extra bits */
destinyXfate 2:0e2ef1edf01b 656 }
destinyXfate 2:0e2ef1edf01b 657 desc->max_code = max_code;
destinyXfate 2:0e2ef1edf01b 658
destinyXfate 2:0e2ef1edf01b 659 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
destinyXfate 2:0e2ef1edf01b 660 * establish sub-heaps of increasing lengths:
destinyXfate 2:0e2ef1edf01b 661 */
destinyXfate 2:0e2ef1edf01b 662 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
destinyXfate 2:0e2ef1edf01b 663
destinyXfate 2:0e2ef1edf01b 664 /* Construct the Huffman tree by repeatedly combining the least two
destinyXfate 2:0e2ef1edf01b 665 * frequent nodes.
destinyXfate 2:0e2ef1edf01b 666 */
destinyXfate 2:0e2ef1edf01b 667 node = elems; /* next internal node of the tree */
destinyXfate 2:0e2ef1edf01b 668 do {
destinyXfate 2:0e2ef1edf01b 669 pqremove(s, tree, n); /* n = node of least frequency */
destinyXfate 2:0e2ef1edf01b 670 m = s->heap[SMALLEST]; /* m = node of next least frequency */
destinyXfate 2:0e2ef1edf01b 671
destinyXfate 2:0e2ef1edf01b 672 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
destinyXfate 2:0e2ef1edf01b 673 s->heap[--(s->heap_max)] = m;
destinyXfate 2:0e2ef1edf01b 674
destinyXfate 2:0e2ef1edf01b 675 /* Create a new node father of n and m */
destinyXfate 2:0e2ef1edf01b 676 tree[node].Freq = tree[n].Freq + tree[m].Freq;
destinyXfate 2:0e2ef1edf01b 677 s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
destinyXfate 2:0e2ef1edf01b 678 s->depth[n] : s->depth[m]) + 1);
destinyXfate 2:0e2ef1edf01b 679 tree[n].Dad = tree[m].Dad = (ush)node;
destinyXfate 2:0e2ef1edf01b 680 #ifdef DUMP_BL_TREE
destinyXfate 2:0e2ef1edf01b 681 if (tree == s->bl_tree) {
destinyXfate 2:0e2ef1edf01b 682 fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
destinyXfate 2:0e2ef1edf01b 683 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
destinyXfate 2:0e2ef1edf01b 684 }
destinyXfate 2:0e2ef1edf01b 685 #endif
destinyXfate 2:0e2ef1edf01b 686 /* and insert the new node in the heap */
destinyXfate 2:0e2ef1edf01b 687 s->heap[SMALLEST] = node++;
destinyXfate 2:0e2ef1edf01b 688 pqdownheap(s, tree, SMALLEST);
destinyXfate 2:0e2ef1edf01b 689
destinyXfate 2:0e2ef1edf01b 690 } while (s->heap_len >= 2);
destinyXfate 2:0e2ef1edf01b 691
destinyXfate 2:0e2ef1edf01b 692 s->heap[--(s->heap_max)] = s->heap[SMALLEST];
destinyXfate 2:0e2ef1edf01b 693
destinyXfate 2:0e2ef1edf01b 694 /* At this point, the fields freq and dad are set. We can now
destinyXfate 2:0e2ef1edf01b 695 * generate the bit lengths.
destinyXfate 2:0e2ef1edf01b 696 */
destinyXfate 2:0e2ef1edf01b 697 gen_bitlen(s, (tree_desc *)desc);
destinyXfate 2:0e2ef1edf01b 698
destinyXfate 2:0e2ef1edf01b 699 /* The field len is now set, we can generate the bit codes */
destinyXfate 2:0e2ef1edf01b 700 gen_codes ((ct_data *)tree, max_code, s->bl_count);
destinyXfate 2:0e2ef1edf01b 701 }
destinyXfate 2:0e2ef1edf01b 702
destinyXfate 2:0e2ef1edf01b 703 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 704 * Scan a literal or distance tree to determine the frequencies of the codes
destinyXfate 2:0e2ef1edf01b 705 * in the bit length tree.
destinyXfate 2:0e2ef1edf01b 706 */
destinyXfate 2:0e2ef1edf01b 707 local void scan_tree (s, tree, max_code)
destinyXfate 2:0e2ef1edf01b 708 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 709 ct_data *tree; /* the tree to be scanned */
destinyXfate 2:0e2ef1edf01b 710 int max_code; /* and its largest code of non zero frequency */
destinyXfate 2:0e2ef1edf01b 711 {
destinyXfate 2:0e2ef1edf01b 712 int n; /* iterates over all tree elements */
destinyXfate 2:0e2ef1edf01b 713 int prevlen = -1; /* last emitted length */
destinyXfate 2:0e2ef1edf01b 714 int curlen; /* length of current code */
destinyXfate 2:0e2ef1edf01b 715 int nextlen = tree[0].Len; /* length of next code */
destinyXfate 2:0e2ef1edf01b 716 int count = 0; /* repeat count of the current code */
destinyXfate 2:0e2ef1edf01b 717 int max_count = 7; /* max repeat count */
destinyXfate 2:0e2ef1edf01b 718 int min_count = 4; /* min repeat count */
destinyXfate 2:0e2ef1edf01b 719
destinyXfate 2:0e2ef1edf01b 720 if (nextlen == 0) max_count = 138, min_count = 3;
destinyXfate 2:0e2ef1edf01b 721 tree[max_code+1].Len = (ush)0xffff; /* guard */
destinyXfate 2:0e2ef1edf01b 722
destinyXfate 2:0e2ef1edf01b 723 for (n = 0; n <= max_code; n++) {
destinyXfate 2:0e2ef1edf01b 724 curlen = nextlen; nextlen = tree[n+1].Len;
destinyXfate 2:0e2ef1edf01b 725 if (++count < max_count && curlen == nextlen) {
destinyXfate 2:0e2ef1edf01b 726 continue;
destinyXfate 2:0e2ef1edf01b 727 } else if (count < min_count) {
destinyXfate 2:0e2ef1edf01b 728 s->bl_tree[curlen].Freq += count;
destinyXfate 2:0e2ef1edf01b 729 } else if (curlen != 0) {
destinyXfate 2:0e2ef1edf01b 730 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
destinyXfate 2:0e2ef1edf01b 731 s->bl_tree[REP_3_6].Freq++;
destinyXfate 2:0e2ef1edf01b 732 } else if (count <= 10) {
destinyXfate 2:0e2ef1edf01b 733 s->bl_tree[REPZ_3_10].Freq++;
destinyXfate 2:0e2ef1edf01b 734 } else {
destinyXfate 2:0e2ef1edf01b 735 s->bl_tree[REPZ_11_138].Freq++;
destinyXfate 2:0e2ef1edf01b 736 }
destinyXfate 2:0e2ef1edf01b 737 count = 0; prevlen = curlen;
destinyXfate 2:0e2ef1edf01b 738 if (nextlen == 0) {
destinyXfate 2:0e2ef1edf01b 739 max_count = 138, min_count = 3;
destinyXfate 2:0e2ef1edf01b 740 } else if (curlen == nextlen) {
destinyXfate 2:0e2ef1edf01b 741 max_count = 6, min_count = 3;
destinyXfate 2:0e2ef1edf01b 742 } else {
destinyXfate 2:0e2ef1edf01b 743 max_count = 7, min_count = 4;
destinyXfate 2:0e2ef1edf01b 744 }
destinyXfate 2:0e2ef1edf01b 745 }
destinyXfate 2:0e2ef1edf01b 746 }
destinyXfate 2:0e2ef1edf01b 747
destinyXfate 2:0e2ef1edf01b 748 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 749 * Send a literal or distance tree in compressed form, using the codes in
destinyXfate 2:0e2ef1edf01b 750 * bl_tree.
destinyXfate 2:0e2ef1edf01b 751 */
destinyXfate 2:0e2ef1edf01b 752 local void send_tree (s, tree, max_code)
destinyXfate 2:0e2ef1edf01b 753 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 754 ct_data *tree; /* the tree to be scanned */
destinyXfate 2:0e2ef1edf01b 755 int max_code; /* and its largest code of non zero frequency */
destinyXfate 2:0e2ef1edf01b 756 {
destinyXfate 2:0e2ef1edf01b 757 int n; /* iterates over all tree elements */
destinyXfate 2:0e2ef1edf01b 758 int prevlen = -1; /* last emitted length */
destinyXfate 2:0e2ef1edf01b 759 int curlen; /* length of current code */
destinyXfate 2:0e2ef1edf01b 760 int nextlen = tree[0].Len; /* length of next code */
destinyXfate 2:0e2ef1edf01b 761 int count = 0; /* repeat count of the current code */
destinyXfate 2:0e2ef1edf01b 762 int max_count = 7; /* max repeat count */
destinyXfate 2:0e2ef1edf01b 763 int min_count = 4; /* min repeat count */
destinyXfate 2:0e2ef1edf01b 764
destinyXfate 2:0e2ef1edf01b 765 /* tree[max_code+1].Len = -1; */ /* guard already set */
destinyXfate 2:0e2ef1edf01b 766 if (nextlen == 0) max_count = 138, min_count = 3;
destinyXfate 2:0e2ef1edf01b 767
destinyXfate 2:0e2ef1edf01b 768 for (n = 0; n <= max_code; n++) {
destinyXfate 2:0e2ef1edf01b 769 curlen = nextlen; nextlen = tree[n+1].Len;
destinyXfate 2:0e2ef1edf01b 770 if (++count < max_count && curlen == nextlen) {
destinyXfate 2:0e2ef1edf01b 771 continue;
destinyXfate 2:0e2ef1edf01b 772 } else if (count < min_count) {
destinyXfate 2:0e2ef1edf01b 773 do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
destinyXfate 2:0e2ef1edf01b 774
destinyXfate 2:0e2ef1edf01b 775 } else if (curlen != 0) {
destinyXfate 2:0e2ef1edf01b 776 if (curlen != prevlen) {
destinyXfate 2:0e2ef1edf01b 777 send_code(s, curlen, s->bl_tree); count--;
destinyXfate 2:0e2ef1edf01b 778 }
destinyXfate 2:0e2ef1edf01b 779 Assert(count >= 3 && count <= 6, " 3_6?");
destinyXfate 2:0e2ef1edf01b 780 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
destinyXfate 2:0e2ef1edf01b 781
destinyXfate 2:0e2ef1edf01b 782 } else if (count <= 10) {
destinyXfate 2:0e2ef1edf01b 783 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
destinyXfate 2:0e2ef1edf01b 784
destinyXfate 2:0e2ef1edf01b 785 } else {
destinyXfate 2:0e2ef1edf01b 786 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
destinyXfate 2:0e2ef1edf01b 787 }
destinyXfate 2:0e2ef1edf01b 788 count = 0; prevlen = curlen;
destinyXfate 2:0e2ef1edf01b 789 if (nextlen == 0) {
destinyXfate 2:0e2ef1edf01b 790 max_count = 138, min_count = 3;
destinyXfate 2:0e2ef1edf01b 791 } else if (curlen == nextlen) {
destinyXfate 2:0e2ef1edf01b 792 max_count = 6, min_count = 3;
destinyXfate 2:0e2ef1edf01b 793 } else {
destinyXfate 2:0e2ef1edf01b 794 max_count = 7, min_count = 4;
destinyXfate 2:0e2ef1edf01b 795 }
destinyXfate 2:0e2ef1edf01b 796 }
destinyXfate 2:0e2ef1edf01b 797 }
destinyXfate 2:0e2ef1edf01b 798
destinyXfate 2:0e2ef1edf01b 799 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 800 * Construct the Huffman tree for the bit lengths and return the index in
destinyXfate 2:0e2ef1edf01b 801 * bl_order of the last bit length code to send.
destinyXfate 2:0e2ef1edf01b 802 */
destinyXfate 2:0e2ef1edf01b 803 local int build_bl_tree(s)
destinyXfate 2:0e2ef1edf01b 804 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 805 {
destinyXfate 2:0e2ef1edf01b 806 int max_blindex; /* index of last bit length code of non zero freq */
destinyXfate 2:0e2ef1edf01b 807
destinyXfate 2:0e2ef1edf01b 808 /* Determine the bit length frequencies for literal and distance trees */
destinyXfate 2:0e2ef1edf01b 809 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
destinyXfate 2:0e2ef1edf01b 810 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
destinyXfate 2:0e2ef1edf01b 811
destinyXfate 2:0e2ef1edf01b 812 /* Build the bit length tree: */
destinyXfate 2:0e2ef1edf01b 813 build_tree(s, (tree_desc *)(&(s->bl_desc)));
destinyXfate 2:0e2ef1edf01b 814 /* opt_len now includes the length of the tree representations, except
destinyXfate 2:0e2ef1edf01b 815 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
destinyXfate 2:0e2ef1edf01b 816 */
destinyXfate 2:0e2ef1edf01b 817
destinyXfate 2:0e2ef1edf01b 818 /* Determine the number of bit length codes to send. The pkzip format
destinyXfate 2:0e2ef1edf01b 819 * requires that at least 4 bit length codes be sent. (appnote.txt says
destinyXfate 2:0e2ef1edf01b 820 * 3 but the actual value used is 4.)
destinyXfate 2:0e2ef1edf01b 821 */
destinyXfate 2:0e2ef1edf01b 822 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
destinyXfate 2:0e2ef1edf01b 823 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
destinyXfate 2:0e2ef1edf01b 824 }
destinyXfate 2:0e2ef1edf01b 825 /* Update opt_len to include the bit length tree and counts */
destinyXfate 2:0e2ef1edf01b 826 s->opt_len += 3*(max_blindex+1) + 5+5+4;
destinyXfate 2:0e2ef1edf01b 827 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
destinyXfate 2:0e2ef1edf01b 828 s->opt_len, s->static_len));
destinyXfate 2:0e2ef1edf01b 829
destinyXfate 2:0e2ef1edf01b 830 return max_blindex;
destinyXfate 2:0e2ef1edf01b 831 }
destinyXfate 2:0e2ef1edf01b 832
destinyXfate 2:0e2ef1edf01b 833 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 834 * Send the header for a block using dynamic Huffman trees: the counts, the
destinyXfate 2:0e2ef1edf01b 835 * lengths of the bit length codes, the literal tree and the distance tree.
destinyXfate 2:0e2ef1edf01b 836 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
destinyXfate 2:0e2ef1edf01b 837 */
destinyXfate 2:0e2ef1edf01b 838 local void send_all_trees(s, lcodes, dcodes, blcodes)
destinyXfate 2:0e2ef1edf01b 839 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 840 int lcodes, dcodes, blcodes; /* number of codes for each tree */
destinyXfate 2:0e2ef1edf01b 841 {
destinyXfate 2:0e2ef1edf01b 842 int rank; /* index in bl_order */
destinyXfate 2:0e2ef1edf01b 843
destinyXfate 2:0e2ef1edf01b 844 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
destinyXfate 2:0e2ef1edf01b 845 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
destinyXfate 2:0e2ef1edf01b 846 "too many codes");
destinyXfate 2:0e2ef1edf01b 847 Tracev((stderr, "\nbl counts: "));
destinyXfate 2:0e2ef1edf01b 848 send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
destinyXfate 2:0e2ef1edf01b 849 send_bits(s, dcodes-1, 5);
destinyXfate 2:0e2ef1edf01b 850 send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
destinyXfate 2:0e2ef1edf01b 851 for (rank = 0; rank < blcodes; rank++) {
destinyXfate 2:0e2ef1edf01b 852 Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
destinyXfate 2:0e2ef1edf01b 853 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
destinyXfate 2:0e2ef1edf01b 854 }
destinyXfate 2:0e2ef1edf01b 855 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
destinyXfate 2:0e2ef1edf01b 856
destinyXfate 2:0e2ef1edf01b 857 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
destinyXfate 2:0e2ef1edf01b 858 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
destinyXfate 2:0e2ef1edf01b 859
destinyXfate 2:0e2ef1edf01b 860 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
destinyXfate 2:0e2ef1edf01b 861 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
destinyXfate 2:0e2ef1edf01b 862 }
destinyXfate 2:0e2ef1edf01b 863
destinyXfate 2:0e2ef1edf01b 864 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 865 * Send a stored block
destinyXfate 2:0e2ef1edf01b 866 */
destinyXfate 2:0e2ef1edf01b 867 void _tr_stored_block(s, buf, stored_len, eof)
destinyXfate 2:0e2ef1edf01b 868 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 869 charf *buf; /* input block */
destinyXfate 2:0e2ef1edf01b 870 ulg stored_len; /* length of input block */
destinyXfate 2:0e2ef1edf01b 871 int eof; /* true if this is the last block for a file */
destinyXfate 2:0e2ef1edf01b 872 {
destinyXfate 2:0e2ef1edf01b 873 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
destinyXfate 2:0e2ef1edf01b 874 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 875 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
destinyXfate 2:0e2ef1edf01b 876 s->compressed_len += (stored_len + 4) << 3;
destinyXfate 2:0e2ef1edf01b 877 #endif
destinyXfate 2:0e2ef1edf01b 878 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
destinyXfate 2:0e2ef1edf01b 879 }
destinyXfate 2:0e2ef1edf01b 880
destinyXfate 2:0e2ef1edf01b 881 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 882 * Send one empty static block to give enough lookahead for inflate.
destinyXfate 2:0e2ef1edf01b 883 * This takes 10 bits, of which 7 may remain in the bit buffer.
destinyXfate 2:0e2ef1edf01b 884 * The current inflate code requires 9 bits of lookahead. If the
destinyXfate 2:0e2ef1edf01b 885 * last two codes for the previous block (real code plus EOB) were coded
destinyXfate 2:0e2ef1edf01b 886 * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
destinyXfate 2:0e2ef1edf01b 887 * the last real code. In this case we send two empty static blocks instead
destinyXfate 2:0e2ef1edf01b 888 * of one. (There are no problems if the previous block is stored or fixed.)
destinyXfate 2:0e2ef1edf01b 889 * To simplify the code, we assume the worst case of last real code encoded
destinyXfate 2:0e2ef1edf01b 890 * on one bit only.
destinyXfate 2:0e2ef1edf01b 891 */
destinyXfate 2:0e2ef1edf01b 892 void _tr_align(s)
destinyXfate 2:0e2ef1edf01b 893 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 894 {
destinyXfate 2:0e2ef1edf01b 895 send_bits(s, STATIC_TREES<<1, 3);
destinyXfate 2:0e2ef1edf01b 896 send_code(s, END_BLOCK, static_ltree);
destinyXfate 2:0e2ef1edf01b 897 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 898 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
destinyXfate 2:0e2ef1edf01b 899 #endif
destinyXfate 2:0e2ef1edf01b 900 bi_flush(s);
destinyXfate 2:0e2ef1edf01b 901 /* Of the 10 bits for the empty block, we have already sent
destinyXfate 2:0e2ef1edf01b 902 * (10 - bi_valid) bits. The lookahead for the last real code (before
destinyXfate 2:0e2ef1edf01b 903 * the EOB of the previous block) was thus at least one plus the length
destinyXfate 2:0e2ef1edf01b 904 * of the EOB plus what we have just sent of the empty static block.
destinyXfate 2:0e2ef1edf01b 905 */
destinyXfate 2:0e2ef1edf01b 906 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
destinyXfate 2:0e2ef1edf01b 907 send_bits(s, STATIC_TREES<<1, 3);
destinyXfate 2:0e2ef1edf01b 908 send_code(s, END_BLOCK, static_ltree);
destinyXfate 2:0e2ef1edf01b 909 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 910 s->compressed_len += 10L;
destinyXfate 2:0e2ef1edf01b 911 #endif
destinyXfate 2:0e2ef1edf01b 912 bi_flush(s);
destinyXfate 2:0e2ef1edf01b 913 }
destinyXfate 2:0e2ef1edf01b 914 s->last_eob_len = 7;
destinyXfate 2:0e2ef1edf01b 915 }
destinyXfate 2:0e2ef1edf01b 916
destinyXfate 2:0e2ef1edf01b 917 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 918 * Determine the best encoding for the current block: dynamic trees, static
destinyXfate 2:0e2ef1edf01b 919 * trees or store, and output the encoded block to the zip file.
destinyXfate 2:0e2ef1edf01b 920 */
destinyXfate 2:0e2ef1edf01b 921 void _tr_flush_block(s, buf, stored_len, eof)
destinyXfate 2:0e2ef1edf01b 922 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 923 charf *buf; /* input block, or NULL if too old */
destinyXfate 2:0e2ef1edf01b 924 ulg stored_len; /* length of input block */
destinyXfate 2:0e2ef1edf01b 925 int eof; /* true if this is the last block for a file */
destinyXfate 2:0e2ef1edf01b 926 {
destinyXfate 2:0e2ef1edf01b 927 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
destinyXfate 2:0e2ef1edf01b 928 int max_blindex = 0; /* index of last bit length code of non zero freq */
destinyXfate 2:0e2ef1edf01b 929
destinyXfate 2:0e2ef1edf01b 930 /* Build the Huffman trees unless a stored block is forced */
destinyXfate 2:0e2ef1edf01b 931 if (s->level > 0) {
destinyXfate 2:0e2ef1edf01b 932
destinyXfate 2:0e2ef1edf01b 933 /* Check if the file is binary or text */
destinyXfate 2:0e2ef1edf01b 934 if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
destinyXfate 2:0e2ef1edf01b 935 set_data_type(s);
destinyXfate 2:0e2ef1edf01b 936
destinyXfate 2:0e2ef1edf01b 937 /* Construct the literal and distance trees */
destinyXfate 2:0e2ef1edf01b 938 build_tree(s, (tree_desc *)(&(s->l_desc)));
destinyXfate 2:0e2ef1edf01b 939 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
destinyXfate 2:0e2ef1edf01b 940 s->static_len));
destinyXfate 2:0e2ef1edf01b 941
destinyXfate 2:0e2ef1edf01b 942 build_tree(s, (tree_desc *)(&(s->d_desc)));
destinyXfate 2:0e2ef1edf01b 943 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
destinyXfate 2:0e2ef1edf01b 944 s->static_len));
destinyXfate 2:0e2ef1edf01b 945 /* At this point, opt_len and static_len are the total bit lengths of
destinyXfate 2:0e2ef1edf01b 946 * the compressed block data, excluding the tree representations.
destinyXfate 2:0e2ef1edf01b 947 */
destinyXfate 2:0e2ef1edf01b 948
destinyXfate 2:0e2ef1edf01b 949 /* Build the bit length tree for the above two trees, and get the index
destinyXfate 2:0e2ef1edf01b 950 * in bl_order of the last bit length code to send.
destinyXfate 2:0e2ef1edf01b 951 */
destinyXfate 2:0e2ef1edf01b 952 max_blindex = build_bl_tree(s);
destinyXfate 2:0e2ef1edf01b 953
destinyXfate 2:0e2ef1edf01b 954 /* Determine the best encoding. Compute the block lengths in bytes. */
destinyXfate 2:0e2ef1edf01b 955 opt_lenb = (s->opt_len+3+7)>>3;
destinyXfate 2:0e2ef1edf01b 956 static_lenb = (s->static_len+3+7)>>3;
destinyXfate 2:0e2ef1edf01b 957
destinyXfate 2:0e2ef1edf01b 958 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
destinyXfate 2:0e2ef1edf01b 959 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
destinyXfate 2:0e2ef1edf01b 960 s->last_lit));
destinyXfate 2:0e2ef1edf01b 961
destinyXfate 2:0e2ef1edf01b 962 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
destinyXfate 2:0e2ef1edf01b 963
destinyXfate 2:0e2ef1edf01b 964 } else {
destinyXfate 2:0e2ef1edf01b 965 Assert(buf != (char*)0, "lost buf");
destinyXfate 2:0e2ef1edf01b 966 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
destinyXfate 2:0e2ef1edf01b 967 }
destinyXfate 2:0e2ef1edf01b 968
destinyXfate 2:0e2ef1edf01b 969 #ifdef FORCE_STORED
destinyXfate 2:0e2ef1edf01b 970 if (buf != (char*)0) { /* force stored block */
destinyXfate 2:0e2ef1edf01b 971 #else
destinyXfate 2:0e2ef1edf01b 972 if (stored_len+4 <= opt_lenb && buf != (char*)0) {
destinyXfate 2:0e2ef1edf01b 973 /* 4: two words for the lengths */
destinyXfate 2:0e2ef1edf01b 974 #endif
destinyXfate 2:0e2ef1edf01b 975 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
destinyXfate 2:0e2ef1edf01b 976 * Otherwise we can't have processed more than WSIZE input bytes since
destinyXfate 2:0e2ef1edf01b 977 * the last block flush, because compression would have been
destinyXfate 2:0e2ef1edf01b 978 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
destinyXfate 2:0e2ef1edf01b 979 * transform a block into a stored block.
destinyXfate 2:0e2ef1edf01b 980 */
destinyXfate 2:0e2ef1edf01b 981 _tr_stored_block(s, buf, stored_len, eof);
destinyXfate 2:0e2ef1edf01b 982
destinyXfate 2:0e2ef1edf01b 983 #ifdef FORCE_STATIC
destinyXfate 2:0e2ef1edf01b 984 } else if (static_lenb >= 0) { /* force static trees */
destinyXfate 2:0e2ef1edf01b 985 #else
destinyXfate 2:0e2ef1edf01b 986 } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
destinyXfate 2:0e2ef1edf01b 987 #endif
destinyXfate 2:0e2ef1edf01b 988 send_bits(s, (STATIC_TREES<<1)+eof, 3);
destinyXfate 2:0e2ef1edf01b 989 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
destinyXfate 2:0e2ef1edf01b 990 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 991 s->compressed_len += 3 + s->static_len;
destinyXfate 2:0e2ef1edf01b 992 #endif
destinyXfate 2:0e2ef1edf01b 993 } else {
destinyXfate 2:0e2ef1edf01b 994 send_bits(s, (DYN_TREES<<1)+eof, 3);
destinyXfate 2:0e2ef1edf01b 995 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
destinyXfate 2:0e2ef1edf01b 996 max_blindex+1);
destinyXfate 2:0e2ef1edf01b 997 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
destinyXfate 2:0e2ef1edf01b 998 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 999 s->compressed_len += 3 + s->opt_len;
destinyXfate 2:0e2ef1edf01b 1000 #endif
destinyXfate 2:0e2ef1edf01b 1001 }
destinyXfate 2:0e2ef1edf01b 1002 Assert (s->compressed_len == s->bits_sent, "bad compressed size");
destinyXfate 2:0e2ef1edf01b 1003 /* The above check is made mod 2^32, for files larger than 512 MB
destinyXfate 2:0e2ef1edf01b 1004 * and uLong implemented on 32 bits.
destinyXfate 2:0e2ef1edf01b 1005 */
destinyXfate 2:0e2ef1edf01b 1006 init_block(s);
destinyXfate 2:0e2ef1edf01b 1007
destinyXfate 2:0e2ef1edf01b 1008 if (eof) {
destinyXfate 2:0e2ef1edf01b 1009 bi_windup(s);
destinyXfate 2:0e2ef1edf01b 1010 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 1011 s->compressed_len += 7; /* align on byte boundary */
destinyXfate 2:0e2ef1edf01b 1012 #endif
destinyXfate 2:0e2ef1edf01b 1013 }
destinyXfate 2:0e2ef1edf01b 1014 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
destinyXfate 2:0e2ef1edf01b 1015 s->compressed_len-7*eof));
destinyXfate 2:0e2ef1edf01b 1016 }
destinyXfate 2:0e2ef1edf01b 1017
destinyXfate 2:0e2ef1edf01b 1018 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 1019 * Save the match info and tally the frequency counts. Return true if
destinyXfate 2:0e2ef1edf01b 1020 * the current block must be flushed.
destinyXfate 2:0e2ef1edf01b 1021 */
destinyXfate 2:0e2ef1edf01b 1022 int _tr_tally (s, dist, lc)
destinyXfate 2:0e2ef1edf01b 1023 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 1024 unsigned dist; /* distance of matched string */
destinyXfate 2:0e2ef1edf01b 1025 unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
destinyXfate 2:0e2ef1edf01b 1026 {
destinyXfate 2:0e2ef1edf01b 1027 s->d_buf[s->last_lit] = (ush)dist;
destinyXfate 2:0e2ef1edf01b 1028 s->l_buf[s->last_lit++] = (uch)lc;
destinyXfate 2:0e2ef1edf01b 1029 if (dist == 0) {
destinyXfate 2:0e2ef1edf01b 1030 /* lc is the unmatched char */
destinyXfate 2:0e2ef1edf01b 1031 s->dyn_ltree[lc].Freq++;
destinyXfate 2:0e2ef1edf01b 1032 } else {
destinyXfate 2:0e2ef1edf01b 1033 s->matches++;
destinyXfate 2:0e2ef1edf01b 1034 /* Here, lc is the match length - MIN_MATCH */
destinyXfate 2:0e2ef1edf01b 1035 dist--; /* dist = match distance - 1 */
destinyXfate 2:0e2ef1edf01b 1036 Assert((ush)dist < (ush)MAX_DIST(s) &&
destinyXfate 2:0e2ef1edf01b 1037 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
destinyXfate 2:0e2ef1edf01b 1038 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
destinyXfate 2:0e2ef1edf01b 1039
destinyXfate 2:0e2ef1edf01b 1040 s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
destinyXfate 2:0e2ef1edf01b 1041 s->dyn_dtree[d_code(dist)].Freq++;
destinyXfate 2:0e2ef1edf01b 1042 }
destinyXfate 2:0e2ef1edf01b 1043
destinyXfate 2:0e2ef1edf01b 1044 #ifdef TRUNCATE_BLOCK
destinyXfate 2:0e2ef1edf01b 1045 /* Try to guess if it is profitable to stop the current block here */
destinyXfate 2:0e2ef1edf01b 1046 if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
destinyXfate 2:0e2ef1edf01b 1047 /* Compute an upper bound for the compressed length */
destinyXfate 2:0e2ef1edf01b 1048 ulg out_length = (ulg)s->last_lit*8L;
destinyXfate 2:0e2ef1edf01b 1049 ulg in_length = (ulg)((long)s->strstart - s->block_start);
destinyXfate 2:0e2ef1edf01b 1050 int dcode;
destinyXfate 2:0e2ef1edf01b 1051 for (dcode = 0; dcode < D_CODES; dcode++) {
destinyXfate 2:0e2ef1edf01b 1052 out_length += (ulg)s->dyn_dtree[dcode].Freq *
destinyXfate 2:0e2ef1edf01b 1053 (5L+extra_dbits[dcode]);
destinyXfate 2:0e2ef1edf01b 1054 }
destinyXfate 2:0e2ef1edf01b 1055 out_length >>= 3;
destinyXfate 2:0e2ef1edf01b 1056 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
destinyXfate 2:0e2ef1edf01b 1057 s->last_lit, in_length, out_length,
destinyXfate 2:0e2ef1edf01b 1058 100L - out_length*100L/in_length));
destinyXfate 2:0e2ef1edf01b 1059 if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
destinyXfate 2:0e2ef1edf01b 1060 }
destinyXfate 2:0e2ef1edf01b 1061 #endif
destinyXfate 2:0e2ef1edf01b 1062 return (s->last_lit == s->lit_bufsize-1);
destinyXfate 2:0e2ef1edf01b 1063 /* We avoid equality with lit_bufsize because of wraparound at 64K
destinyXfate 2:0e2ef1edf01b 1064 * on 16 bit machines and because stored blocks are restricted to
destinyXfate 2:0e2ef1edf01b 1065 * 64K-1 bytes.
destinyXfate 2:0e2ef1edf01b 1066 */
destinyXfate 2:0e2ef1edf01b 1067 }
destinyXfate 2:0e2ef1edf01b 1068
destinyXfate 2:0e2ef1edf01b 1069 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 1070 * Send the block data compressed using the given Huffman trees
destinyXfate 2:0e2ef1edf01b 1071 */
destinyXfate 2:0e2ef1edf01b 1072 local void compress_block(s, ltree, dtree)
destinyXfate 2:0e2ef1edf01b 1073 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 1074 ct_data *ltree; /* literal tree */
destinyXfate 2:0e2ef1edf01b 1075 ct_data *dtree; /* distance tree */
destinyXfate 2:0e2ef1edf01b 1076 {
destinyXfate 2:0e2ef1edf01b 1077 unsigned dist; /* distance of matched string */
destinyXfate 2:0e2ef1edf01b 1078 int lc; /* match length or unmatched char (if dist == 0) */
destinyXfate 2:0e2ef1edf01b 1079 unsigned lx = 0; /* running index in l_buf */
destinyXfate 2:0e2ef1edf01b 1080 unsigned code; /* the code to send */
destinyXfate 2:0e2ef1edf01b 1081 int extra; /* number of extra bits to send */
destinyXfate 2:0e2ef1edf01b 1082
destinyXfate 2:0e2ef1edf01b 1083 if (s->last_lit != 0) do {
destinyXfate 2:0e2ef1edf01b 1084 dist = s->d_buf[lx];
destinyXfate 2:0e2ef1edf01b 1085 lc = s->l_buf[lx++];
destinyXfate 2:0e2ef1edf01b 1086 if (dist == 0) {
destinyXfate 2:0e2ef1edf01b 1087 send_code(s, lc, ltree); /* send a literal byte */
destinyXfate 2:0e2ef1edf01b 1088 Tracecv(isgraph(lc), (stderr," '%c' ", lc));
destinyXfate 2:0e2ef1edf01b 1089 } else {
destinyXfate 2:0e2ef1edf01b 1090 /* Here, lc is the match length - MIN_MATCH */
destinyXfate 2:0e2ef1edf01b 1091 code = _length_code[lc];
destinyXfate 2:0e2ef1edf01b 1092 send_code(s, code+LITERALS+1, ltree); /* send the length code */
destinyXfate 2:0e2ef1edf01b 1093 extra = extra_lbits[code];
destinyXfate 2:0e2ef1edf01b 1094 if (extra != 0) {
destinyXfate 2:0e2ef1edf01b 1095 lc -= base_length[code];
destinyXfate 2:0e2ef1edf01b 1096 send_bits(s, lc, extra); /* send the extra length bits */
destinyXfate 2:0e2ef1edf01b 1097 }
destinyXfate 2:0e2ef1edf01b 1098 dist--; /* dist is now the match distance - 1 */
destinyXfate 2:0e2ef1edf01b 1099 code = d_code(dist);
destinyXfate 2:0e2ef1edf01b 1100 Assert (code < D_CODES, "bad d_code");
destinyXfate 2:0e2ef1edf01b 1101
destinyXfate 2:0e2ef1edf01b 1102 send_code(s, code, dtree); /* send the distance code */
destinyXfate 2:0e2ef1edf01b 1103 extra = extra_dbits[code];
destinyXfate 2:0e2ef1edf01b 1104 if (extra != 0) {
destinyXfate 2:0e2ef1edf01b 1105 dist -= base_dist[code];
destinyXfate 2:0e2ef1edf01b 1106 send_bits(s, dist, extra); /* send the extra distance bits */
destinyXfate 2:0e2ef1edf01b 1107 }
destinyXfate 2:0e2ef1edf01b 1108 } /* literal or match pair ? */
destinyXfate 2:0e2ef1edf01b 1109
destinyXfate 2:0e2ef1edf01b 1110 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
destinyXfate 2:0e2ef1edf01b 1111 Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
destinyXfate 2:0e2ef1edf01b 1112 "pendingBuf overflow");
destinyXfate 2:0e2ef1edf01b 1113
destinyXfate 2:0e2ef1edf01b 1114 } while (lx < s->last_lit);
destinyXfate 2:0e2ef1edf01b 1115
destinyXfate 2:0e2ef1edf01b 1116 send_code(s, END_BLOCK, ltree);
destinyXfate 2:0e2ef1edf01b 1117 s->last_eob_len = ltree[END_BLOCK].Len;
destinyXfate 2:0e2ef1edf01b 1118 }
destinyXfate 2:0e2ef1edf01b 1119
destinyXfate 2:0e2ef1edf01b 1120 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 1121 * Set the data type to BINARY or TEXT, using a crude approximation:
destinyXfate 2:0e2ef1edf01b 1122 * set it to Z_TEXT if all symbols are either printable characters (33 to 255)
destinyXfate 2:0e2ef1edf01b 1123 * or white spaces (9 to 13, or 32); or set it to Z_BINARY otherwise.
destinyXfate 2:0e2ef1edf01b 1124 * IN assertion: the fields Freq of dyn_ltree are set.
destinyXfate 2:0e2ef1edf01b 1125 */
destinyXfate 2:0e2ef1edf01b 1126 local void set_data_type(s)
destinyXfate 2:0e2ef1edf01b 1127 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 1128 {
destinyXfate 2:0e2ef1edf01b 1129 int n;
destinyXfate 2:0e2ef1edf01b 1130
destinyXfate 2:0e2ef1edf01b 1131 for (n = 0; n < 9; n++)
destinyXfate 2:0e2ef1edf01b 1132 if (s->dyn_ltree[n].Freq != 0)
destinyXfate 2:0e2ef1edf01b 1133 break;
destinyXfate 2:0e2ef1edf01b 1134 if (n == 9)
destinyXfate 2:0e2ef1edf01b 1135 for (n = 14; n < 32; n++)
destinyXfate 2:0e2ef1edf01b 1136 if (s->dyn_ltree[n].Freq != 0)
destinyXfate 2:0e2ef1edf01b 1137 break;
destinyXfate 2:0e2ef1edf01b 1138 s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
destinyXfate 2:0e2ef1edf01b 1139 }
destinyXfate 2:0e2ef1edf01b 1140
destinyXfate 2:0e2ef1edf01b 1141 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 1142 * Reverse the first len bits of a code, using straightforward code (a faster
destinyXfate 2:0e2ef1edf01b 1143 * method would use a table)
destinyXfate 2:0e2ef1edf01b 1144 * IN assertion: 1 <= len <= 15
destinyXfate 2:0e2ef1edf01b 1145 */
destinyXfate 2:0e2ef1edf01b 1146 local unsigned bi_reverse(code, len)
destinyXfate 2:0e2ef1edf01b 1147 unsigned code; /* the value to invert */
destinyXfate 2:0e2ef1edf01b 1148 int len; /* its bit length */
destinyXfate 2:0e2ef1edf01b 1149 {
destinyXfate 2:0e2ef1edf01b 1150 register unsigned res = 0;
destinyXfate 2:0e2ef1edf01b 1151 do {
destinyXfate 2:0e2ef1edf01b 1152 res |= code & 1;
destinyXfate 2:0e2ef1edf01b 1153 code >>= 1, res <<= 1;
destinyXfate 2:0e2ef1edf01b 1154 } while (--len > 0);
destinyXfate 2:0e2ef1edf01b 1155 return res >> 1;
destinyXfate 2:0e2ef1edf01b 1156 }
destinyXfate 2:0e2ef1edf01b 1157
destinyXfate 2:0e2ef1edf01b 1158 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 1159 * Flush the bit buffer, keeping at most 7 bits in it.
destinyXfate 2:0e2ef1edf01b 1160 */
destinyXfate 2:0e2ef1edf01b 1161 local void bi_flush(s)
destinyXfate 2:0e2ef1edf01b 1162 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 1163 {
destinyXfate 2:0e2ef1edf01b 1164 if (s->bi_valid == 16) {
destinyXfate 2:0e2ef1edf01b 1165 put_short(s, s->bi_buf);
destinyXfate 2:0e2ef1edf01b 1166 s->bi_buf = 0;
destinyXfate 2:0e2ef1edf01b 1167 s->bi_valid = 0;
destinyXfate 2:0e2ef1edf01b 1168 } else if (s->bi_valid >= 8) {
destinyXfate 2:0e2ef1edf01b 1169 put_byte(s, (Byte)s->bi_buf);
destinyXfate 2:0e2ef1edf01b 1170 s->bi_buf >>= 8;
destinyXfate 2:0e2ef1edf01b 1171 s->bi_valid -= 8;
destinyXfate 2:0e2ef1edf01b 1172 }
destinyXfate 2:0e2ef1edf01b 1173 }
destinyXfate 2:0e2ef1edf01b 1174
destinyXfate 2:0e2ef1edf01b 1175 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 1176 * Flush the bit buffer and align the output on a byte boundary
destinyXfate 2:0e2ef1edf01b 1177 */
destinyXfate 2:0e2ef1edf01b 1178 local void bi_windup(s)
destinyXfate 2:0e2ef1edf01b 1179 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 1180 {
destinyXfate 2:0e2ef1edf01b 1181 if (s->bi_valid > 8) {
destinyXfate 2:0e2ef1edf01b 1182 put_short(s, s->bi_buf);
destinyXfate 2:0e2ef1edf01b 1183 } else if (s->bi_valid > 0) {
destinyXfate 2:0e2ef1edf01b 1184 put_byte(s, (Byte)s->bi_buf);
destinyXfate 2:0e2ef1edf01b 1185 }
destinyXfate 2:0e2ef1edf01b 1186 s->bi_buf = 0;
destinyXfate 2:0e2ef1edf01b 1187 s->bi_valid = 0;
destinyXfate 2:0e2ef1edf01b 1188 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 1189 s->bits_sent = (s->bits_sent+7) & ~7;
destinyXfate 2:0e2ef1edf01b 1190 #endif
destinyXfate 2:0e2ef1edf01b 1191 }
destinyXfate 2:0e2ef1edf01b 1192
destinyXfate 2:0e2ef1edf01b 1193 /* ===========================================================================
destinyXfate 2:0e2ef1edf01b 1194 * Copy a stored block, storing first the length and its
destinyXfate 2:0e2ef1edf01b 1195 * one's complement if requested.
destinyXfate 2:0e2ef1edf01b 1196 */
destinyXfate 2:0e2ef1edf01b 1197 local void copy_block(s, buf, len, header)
destinyXfate 2:0e2ef1edf01b 1198 deflate_state *s;
destinyXfate 2:0e2ef1edf01b 1199 charf *buf; /* the input data */
destinyXfate 2:0e2ef1edf01b 1200 unsigned len; /* its length */
destinyXfate 2:0e2ef1edf01b 1201 int header; /* true if block header must be written */
destinyXfate 2:0e2ef1edf01b 1202 {
destinyXfate 2:0e2ef1edf01b 1203 bi_windup(s); /* align on byte boundary */
destinyXfate 2:0e2ef1edf01b 1204 s->last_eob_len = 8; /* enough lookahead for inflate */
destinyXfate 2:0e2ef1edf01b 1205
destinyXfate 2:0e2ef1edf01b 1206 if (header) {
destinyXfate 2:0e2ef1edf01b 1207 put_short(s, (ush)len);
destinyXfate 2:0e2ef1edf01b 1208 put_short(s, (ush)~len);
destinyXfate 2:0e2ef1edf01b 1209 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 1210 s->bits_sent += 2*16;
destinyXfate 2:0e2ef1edf01b 1211 #endif
destinyXfate 2:0e2ef1edf01b 1212 }
destinyXfate 2:0e2ef1edf01b 1213 #ifdef DEBUG
destinyXfate 2:0e2ef1edf01b 1214 s->bits_sent += (ulg)len<<3;
destinyXfate 2:0e2ef1edf01b 1215 #endif
destinyXfate 2:0e2ef1edf01b 1216 while (len--) {
destinyXfate 2:0e2ef1edf01b 1217 put_byte(s, *buf++);
destinyXfate 2:0e2ef1edf01b 1218 }
destinyXfate 2:0e2ef1edf01b 1219 }
destinyXfate 2:0e2ef1edf01b 1220