^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #ifndef DEFUTIL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #define DEFUTIL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/zutil.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define Assert(err, str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define Trace(dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define Tracev(dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define Tracecv(err, dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define Tracevv(dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define LENGTH_CODES 29
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* number of length codes, not counting the special END_BLOCK code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define LITERALS 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* number of literal bytes 0..255 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define L_CODES (LITERALS+1+LENGTH_CODES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* number of Literal or Length codes, including the END_BLOCK code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define D_CODES 30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* number of distance codes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define BL_CODES 19
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* number of codes used to transfer the bit lengths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define HEAP_SIZE (2*L_CODES+1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* maximum heap size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define MAX_BITS 15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* All codes must not exceed MAX_BITS bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define INIT_STATE 42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define BUSY_STATE 113
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define FINISH_STATE 666
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* Stream status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Data structure describing a single value and its code string. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) typedef struct ct_data_s {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ush freq; /* frequency count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ush code; /* bit string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) } fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ush dad; /* father node in Huffman tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ush len; /* length of bit string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) } dl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) } ct_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define Freq fc.freq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define Code fc.code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define Dad dl.dad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define Len dl.len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) typedef struct static_tree_desc_s static_tree_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) typedef struct tree_desc_s {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ct_data *dyn_tree; /* the dynamic tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int max_code; /* largest code with non zero frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static_tree_desc *stat_desc; /* the corresponding static tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) } tree_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) typedef ush Pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) typedef unsigned IPos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* A Pos is an index in the character window. We use short instead of int to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * save space in the various tables. IPos is used only for parameter passing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) typedef struct deflate_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) z_streamp strm; /* pointer back to this zlib stream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int status; /* as the name implies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) Byte *pending_buf; /* output still pending */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ulg pending_buf_size; /* size of pending_buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) Byte *pending_out; /* next pending byte to output to the stream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int pending; /* nb of bytes in the pending buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int noheader; /* suppress zlib header and adler32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) Byte data_type; /* UNKNOWN, BINARY or ASCII */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) Byte method; /* STORED (for zip only) or DEFLATED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int last_flush; /* value of flush param for previous deflate call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* used by deflate.c: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) uInt w_size; /* LZ77 window size (32K by default) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) uInt w_bits; /* log2(w_size) (8..16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) uInt w_mask; /* w_size - 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) Byte *window;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* Sliding window. Input bytes are read into the second half of the window,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * and move to the first half later to keep a dictionary of at least wSize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * bytes. With this organization, matches are limited to a distance of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * wSize-MAX_MATCH bytes, but this ensures that IO is always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * performed with a length multiple of the block size. Also, it limits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * the window size to 64K, which is quite useful on MSDOS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * To do: use the user input buffer as sliding window.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) ulg window_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* Actual size of window: 2*wSize, except when the user input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * is directly used as sliding window.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) Pos *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Link to older string with same hash index. To limit the size of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * array to 64K, this link is maintained only for the last 32K strings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * An index in this array is thus a window index modulo 32K.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) Pos *head; /* Heads of the hash chains or NIL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) uInt ins_h; /* hash index of string to be inserted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) uInt hash_size; /* number of elements in hash table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) uInt hash_bits; /* log2(hash_size) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) uInt hash_mask; /* hash_size-1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) uInt hash_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* Number of bits by which ins_h must be shifted at each input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * step. It must be such that after MIN_MATCH steps, the oldest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * byte no longer takes part in the hash key, that is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * hash_shift * MIN_MATCH >= hash_bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) long block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* Window position at the beginning of the current output block. Gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * negative when the window is moved backwards.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) uInt match_length; /* length of best match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) IPos prev_match; /* previous match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int match_available; /* set if previous match exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) uInt strstart; /* start of string to insert */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) uInt match_start; /* start of matching string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) uInt lookahead; /* number of valid bytes ahead in window */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) uInt prev_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Length of the best match at previous step. Matches not greater than this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * are discarded. This is used in the lazy match evaluation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) uInt max_chain_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* To speed up deflation, hash chains are never searched beyond this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * length. A higher limit improves compression ratio but degrades the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * speed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) uInt max_lazy_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* Attempt to find a better match only when the current match is strictly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * smaller than this value. This mechanism is used only for compression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * levels >= 4.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) # define max_insert_length max_lazy_match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* Insert new strings in the hash table only if the match length is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * greater than this length. This saves time but degrades compression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * max_insert_length is used only for compression levels <= 3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int level; /* compression level (1..9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int strategy; /* favor or force Huffman coding*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) uInt good_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* Use a faster search when the previous match is longer than this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int nice_match; /* Stop searching when current match exceeds this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* used by trees.c: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* Didn't use ct_data typedef below to suppress compiler warning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct tree_desc_s l_desc; /* desc. for literal tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct tree_desc_s d_desc; /* desc. for distance tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct tree_desc_s bl_desc; /* desc. for bit length tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ush bl_count[MAX_BITS+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* number of codes at each bit length for an optimal tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int heap_len; /* number of elements in the heap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int heap_max; /* element of largest frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * The same heap array is used to build all trees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) uch depth[2*L_CODES+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* Depth of each subtree used as tie breaker for trees of equal frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) uch *l_buf; /* buffer for literals or lengths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) uInt lit_bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /* Size of match buffer for literals/lengths. There are 4 reasons for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * limiting lit_bufsize to 64K:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * - frequencies can be kept in 16 bit counters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * - if compression is not successful for the first block, all input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * data is still in the window so we can still emit a stored block even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * when input comes from standard input. (This can also be done for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * all blocks if lit_bufsize is not greater than 32K.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * - if compression is not successful for a file smaller than 64K, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * even emit a stored file instead of a stored block (saving 5 bytes).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * This is applicable only for zip (not gzip or zlib).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * - creating new Huffman trees less frequently may not provide fast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * adaptation to changes in the input data statistics. (Take for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * example a binary file with poorly compressible code followed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * a highly compressible string table.) Smaller buffer sizes give
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * fast adaptation but have of course the overhead of transmitting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * trees more frequently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * - I can't count above 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) uInt last_lit; /* running index in l_buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ush *d_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* Buffer for distances. To simplify the code, d_buf and l_buf have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * the same number of elements. To use different lengths, an extra flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * array would be necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) ulg opt_len; /* bit length of current block with optimal trees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ulg static_len; /* bit length of current block with static trees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ulg compressed_len; /* total bit length of compressed file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) uInt matches; /* number of string matches in current block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int last_eob_len; /* bit length of EOB code for last block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) #ifdef DEBUG_ZLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ulg bits_sent; /* bit length of the compressed data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ush bi_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* Output buffer. bits are inserted starting at the bottom (least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * significant bits).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) int bi_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /* Number of valid bits in bi_buf. All bits above the last valid bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * are always zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) } deflate_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) #ifdef CONFIG_ZLIB_DFLTCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) #define zlib_deflate_window_memsize(windowBits) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) (2 * (1 << (windowBits)) * sizeof(Byte) + PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) #define zlib_deflate_window_memsize(windowBits) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) (2 * (1 << (windowBits)) * sizeof(Byte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) #define zlib_deflate_prev_memsize(windowBits) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ((1 << (windowBits)) * sizeof(Pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) #define zlib_deflate_head_memsize(memLevel) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ((1 << ((memLevel)+7)) * sizeof(Pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) #define zlib_deflate_overlay_memsize(memLevel) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) ((1 << ((memLevel)+6)) * (sizeof(ush)+2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* Output a byte on the stream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * IN assertion: there is enough room in pending_buf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* Minimum amount of lookahead, except at the end of the input file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * See deflate.c for comments about the MIN_MATCH+1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /* In order to simplify the code, particularly on 16 bit machines, match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * distances are limited to MAX_DIST instead of WSIZE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* in trees.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) void zlib_tr_init (deflate_state *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int zlib_tr_tally (deflate_state *s, unsigned dist, unsigned lc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ulg zlib_tr_flush_block (deflate_state *s, char *buf, ulg stored_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int eof);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) void zlib_tr_align (deflate_state *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) void zlib_tr_stored_block (deflate_state *s, char *buf, ulg stored_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int eof);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) void zlib_tr_stored_type_only (deflate_state *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * Output a short LSB first on the stream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * IN assertion: there is enough room in pendingBuf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) #define put_short(s, w) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) put_byte(s, (uch)((w) & 0xff)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) put_byte(s, (uch)((ush)(w) >> 8)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * Reverse the first len bits of a code, using straightforward code (a faster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * method would use a table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * IN assertion: 1 <= len <= 15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static inline unsigned bi_reverse(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) unsigned code, /* the value to invert */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int len /* its bit length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) register unsigned res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) res |= code & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) code >>= 1, res <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) } while (--len > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return res >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * Flush the bit buffer, keeping at most 7 bits in it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static inline void bi_flush(deflate_state *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (s->bi_valid == 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) put_short(s, s->bi_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) s->bi_buf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) s->bi_valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) } else if (s->bi_valid >= 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) put_byte(s, (Byte)s->bi_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) s->bi_buf >>= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) s->bi_valid -= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * Flush the bit buffer and align the output on a byte boundary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static inline void bi_windup(deflate_state *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (s->bi_valid > 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) put_short(s, s->bi_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) } else if (s->bi_valid > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) put_byte(s, (Byte)s->bi_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) s->bi_buf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) s->bi_valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #ifdef DEBUG_ZLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) s->bits_sent = (s->bits_sent+7) & ~7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) typedef enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) need_more, /* block not completed, need more input or more output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) block_done, /* block flush performed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) finish_started, /* finish started, need only more output at next deflate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) finish_done /* finish done, accept no more input or output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) } block_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) #define Buf_size (8 * 2*sizeof(char))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /* Number of bits used within bi_buf. (bi_buf might be implemented on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * more than 16 bits on some systems.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * Send a value on a given number of bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * IN assertion: length <= 16 and value fits in length bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) #ifdef DEBUG_ZLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static void send_bits (deflate_state *s, int value, int length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static void send_bits(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) deflate_state *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int value, /* value to send */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) int length /* number of bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) Tracevv((stderr," l %2d v %4x ", length, value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) Assert(length > 0 && length <= 15, "invalid length");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) s->bits_sent += (ulg)length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /* If not enough room in bi_buf, use (valid) bits from bi_buf and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * unused bits in value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (s->bi_valid > (int)Buf_size - length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) s->bi_buf |= (value << s->bi_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) put_short(s, s->bi_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) s->bi_valid += length - Buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) s->bi_buf |= value << s->bi_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) s->bi_valid += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) #else /* !DEBUG_ZLIB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) #define send_bits(s, value, length) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) { int len = length;\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (s->bi_valid > (int)Buf_size - len) {\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int val = value;\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) s->bi_buf |= (val << s->bi_valid);\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) put_short(s, s->bi_buf);\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) s->bi_valid += len - Buf_size;\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) } else {\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) s->bi_buf |= (value) << s->bi_valid;\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) s->bi_valid += len;\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) #endif /* DEBUG_ZLIB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static inline void zlib_tr_send_bits(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) deflate_state *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) int value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) int length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) send_bits(s, value, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) /* =========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * Flush as much pending output as possible. All deflate() output goes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * through this function so some applications may wish to modify it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * to avoid allocating a large strm->next_out buffer and copying into it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * (See also read_buf()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static inline void flush_pending(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) z_streamp strm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) deflate_state *s = (deflate_state *) strm->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) unsigned len = s->pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (len > strm->avail_out) len = strm->avail_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (len == 0) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (strm->next_out != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) memcpy(strm->next_out, s->pending_out, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) strm->next_out += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) s->pending_out += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) strm->total_out += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) strm->avail_out -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) s->pending -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (s->pending == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) s->pending_out = s->pending_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) #endif /* DEFUTIL_H */