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