xref: /linux/lib/zlib_deflate/defutil.h (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1aa5b395bSMikhail Zaslonko #ifndef DEFUTIL_H
2aa5b395bSMikhail Zaslonko #define DEFUTIL_H
31da177e4SLinus Torvalds 
4aa5b395bSMikhail Zaslonko #include <linux/zutil.h>
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #define Assert(err, str)
71da177e4SLinus Torvalds #define Trace(dummy)
81da177e4SLinus Torvalds #define Tracev(dummy)
91da177e4SLinus Torvalds #define Tracecv(err, dummy)
101da177e4SLinus Torvalds #define Tracevv(dummy)
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #define LENGTH_CODES 29
151da177e4SLinus Torvalds /* number of length codes, not counting the special END_BLOCK code */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #define LITERALS  256
181da177e4SLinus Torvalds /* number of literal bytes 0..255 */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #define L_CODES (LITERALS+1+LENGTH_CODES)
211da177e4SLinus Torvalds /* number of Literal or Length codes, including the END_BLOCK code */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #define D_CODES   30
241da177e4SLinus Torvalds /* number of distance codes */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #define BL_CODES  19
271da177e4SLinus Torvalds /* number of codes used to transfer the bit lengths */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #define HEAP_SIZE (2*L_CODES+1)
301da177e4SLinus Torvalds /* maximum heap size */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #define MAX_BITS 15
331da177e4SLinus Torvalds /* All codes must not exceed MAX_BITS bits */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #define INIT_STATE    42
361da177e4SLinus Torvalds #define BUSY_STATE   113
371da177e4SLinus Torvalds #define FINISH_STATE 666
381da177e4SLinus Torvalds /* Stream status */
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /* Data structure describing a single value and its code string. */
421da177e4SLinus Torvalds typedef struct ct_data_s {
431da177e4SLinus Torvalds     union {
441da177e4SLinus Torvalds         ush  freq;       /* frequency count */
451da177e4SLinus Torvalds         ush  code;       /* bit string */
461da177e4SLinus Torvalds     } fc;
471da177e4SLinus Torvalds     union {
481da177e4SLinus Torvalds         ush  dad;        /* father node in Huffman tree */
491da177e4SLinus Torvalds         ush  len;        /* length of bit string */
501da177e4SLinus Torvalds     } dl;
511da177e4SLinus Torvalds } ct_data;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #define Freq fc.freq
541da177e4SLinus Torvalds #define Code fc.code
551da177e4SLinus Torvalds #define Dad  dl.dad
561da177e4SLinus Torvalds #define Len  dl.len
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds typedef struct static_tree_desc_s  static_tree_desc;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds typedef struct tree_desc_s {
611da177e4SLinus Torvalds     ct_data *dyn_tree;           /* the dynamic tree */
621da177e4SLinus Torvalds     int     max_code;            /* largest code with non zero frequency */
631da177e4SLinus Torvalds     static_tree_desc *stat_desc; /* the corresponding static tree */
641da177e4SLinus Torvalds } tree_desc;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds typedef ush Pos;
671da177e4SLinus Torvalds typedef unsigned IPos;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* A Pos is an index in the character window. We use short instead of int to
701da177e4SLinus Torvalds  * save space in the various tables. IPos is used only for parameter passing.
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds typedef struct deflate_state {
741da177e4SLinus Torvalds     z_streamp strm;      /* pointer back to this zlib stream */
751da177e4SLinus Torvalds     int   status;        /* as the name implies */
761da177e4SLinus Torvalds     Byte *pending_buf;   /* output still pending */
771da177e4SLinus Torvalds     ulg   pending_buf_size; /* size of pending_buf */
781da177e4SLinus Torvalds     Byte *pending_out;   /* next pending byte to output to the stream */
791da177e4SLinus Torvalds     int   pending;       /* nb of bytes in the pending buffer */
801da177e4SLinus Torvalds     int   noheader;      /* suppress zlib header and adler32 */
811da177e4SLinus Torvalds     Byte  data_type;     /* UNKNOWN, BINARY or ASCII */
821da177e4SLinus Torvalds     Byte  method;        /* STORED (for zip only) or DEFLATED */
831da177e4SLinus Torvalds     int   last_flush;    /* value of flush param for previous deflate call */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds                 /* used by deflate.c: */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds     uInt  w_size;        /* LZ77 window size (32K by default) */
881da177e4SLinus Torvalds     uInt  w_bits;        /* log2(w_size)  (8..16) */
891da177e4SLinus Torvalds     uInt  w_mask;        /* w_size - 1 */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds     Byte *window;
921da177e4SLinus Torvalds     /* Sliding window. Input bytes are read into the second half of the window,
931da177e4SLinus Torvalds      * and move to the first half later to keep a dictionary of at least wSize
941da177e4SLinus Torvalds      * bytes. With this organization, matches are limited to a distance of
951da177e4SLinus Torvalds      * wSize-MAX_MATCH bytes, but this ensures that IO is always
961da177e4SLinus Torvalds      * performed with a length multiple of the block size. Also, it limits
971da177e4SLinus Torvalds      * the window size to 64K, which is quite useful on MSDOS.
981da177e4SLinus Torvalds      * To do: use the user input buffer as sliding window.
991da177e4SLinus Torvalds      */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds     ulg window_size;
1021da177e4SLinus Torvalds     /* Actual size of window: 2*wSize, except when the user input buffer
1031da177e4SLinus Torvalds      * is directly used as sliding window.
1041da177e4SLinus Torvalds      */
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds     Pos *prev;
1071da177e4SLinus Torvalds     /* Link to older string with same hash index. To limit the size of this
1081da177e4SLinus Torvalds      * array to 64K, this link is maintained only for the last 32K strings.
1091da177e4SLinus Torvalds      * An index in this array is thus a window index modulo 32K.
1101da177e4SLinus Torvalds      */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds     Pos *head; /* Heads of the hash chains or NIL. */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds     uInt  ins_h;          /* hash index of string to be inserted */
1151da177e4SLinus Torvalds     uInt  hash_size;      /* number of elements in hash table */
1161da177e4SLinus Torvalds     uInt  hash_bits;      /* log2(hash_size) */
1171da177e4SLinus Torvalds     uInt  hash_mask;      /* hash_size-1 */
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds     uInt  hash_shift;
1201da177e4SLinus Torvalds     /* Number of bits by which ins_h must be shifted at each input
1211da177e4SLinus Torvalds      * step. It must be such that after MIN_MATCH steps, the oldest
1221da177e4SLinus Torvalds      * byte no longer takes part in the hash key, that is:
1231da177e4SLinus Torvalds      *   hash_shift * MIN_MATCH >= hash_bits
1241da177e4SLinus Torvalds      */
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds     long block_start;
1271da177e4SLinus Torvalds     /* Window position at the beginning of the current output block. Gets
1281da177e4SLinus Torvalds      * negative when the window is moved backwards.
1291da177e4SLinus Torvalds      */
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds     uInt match_length;           /* length of best match */
1321da177e4SLinus Torvalds     IPos prev_match;             /* previous match */
1331da177e4SLinus Torvalds     int match_available;         /* set if previous match exists */
1341da177e4SLinus Torvalds     uInt strstart;               /* start of string to insert */
1351da177e4SLinus Torvalds     uInt match_start;            /* start of matching string */
1361da177e4SLinus Torvalds     uInt lookahead;              /* number of valid bytes ahead in window */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds     uInt prev_length;
1391da177e4SLinus Torvalds     /* Length of the best match at previous step. Matches not greater than this
1401da177e4SLinus Torvalds      * are discarded. This is used in the lazy match evaluation.
1411da177e4SLinus Torvalds      */
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds     uInt max_chain_length;
1441da177e4SLinus Torvalds     /* To speed up deflation, hash chains are never searched beyond this
1451da177e4SLinus Torvalds      * length.  A higher limit improves compression ratio but degrades the
1461da177e4SLinus Torvalds      * speed.
1471da177e4SLinus Torvalds      */
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds     uInt max_lazy_match;
1501da177e4SLinus Torvalds     /* Attempt to find a better match only when the current match is strictly
1511da177e4SLinus Torvalds      * smaller than this value. This mechanism is used only for compression
1521da177e4SLinus Torvalds      * levels >= 4.
1531da177e4SLinus Torvalds      */
1541da177e4SLinus Torvalds #   define max_insert_length  max_lazy_match
1551da177e4SLinus Torvalds     /* Insert new strings in the hash table only if the match length is not
1561da177e4SLinus Torvalds      * greater than this length. This saves time but degrades compression.
1571da177e4SLinus Torvalds      * max_insert_length is used only for compression levels <= 3.
1581da177e4SLinus Torvalds      */
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds     int level;    /* compression level (1..9) */
1611da177e4SLinus Torvalds     int strategy; /* favor or force Huffman coding*/
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds     uInt good_match;
1641da177e4SLinus Torvalds     /* Use a faster search when the previous match is longer than this */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds     int nice_match; /* Stop searching when current match exceeds this */
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds                 /* used by trees.c: */
169643d1f7fSJoe Perches     /* Didn't use ct_data typedef below to suppress compiler warning */
1701da177e4SLinus Torvalds     struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
1711da177e4SLinus Torvalds     struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
1721da177e4SLinus Torvalds     struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds     struct tree_desc_s l_desc;               /* desc. for literal tree */
1751da177e4SLinus Torvalds     struct tree_desc_s d_desc;               /* desc. for distance tree */
1761da177e4SLinus Torvalds     struct tree_desc_s bl_desc;              /* desc. for bit length tree */
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds     ush bl_count[MAX_BITS+1];
1791da177e4SLinus Torvalds     /* number of codes at each bit length for an optimal tree */
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds     int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
1821da177e4SLinus Torvalds     int heap_len;               /* number of elements in the heap */
1831da177e4SLinus Torvalds     int heap_max;               /* element of largest frequency */
1841da177e4SLinus Torvalds     /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
1851da177e4SLinus Torvalds      * The same heap array is used to build all trees.
1861da177e4SLinus Torvalds      */
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds     uch depth[2*L_CODES+1];
1891da177e4SLinus Torvalds     /* Depth of each subtree used as tie breaker for trees of equal frequency
1901da177e4SLinus Torvalds      */
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds     uch *l_buf;          /* buffer for literals or lengths */
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds     uInt  lit_bufsize;
1951da177e4SLinus Torvalds     /* Size of match buffer for literals/lengths.  There are 4 reasons for
1961da177e4SLinus Torvalds      * limiting lit_bufsize to 64K:
1971da177e4SLinus Torvalds      *   - frequencies can be kept in 16 bit counters
1981da177e4SLinus Torvalds      *   - if compression is not successful for the first block, all input
1991da177e4SLinus Torvalds      *     data is still in the window so we can still emit a stored block even
2001da177e4SLinus Torvalds      *     when input comes from standard input.  (This can also be done for
2011da177e4SLinus Torvalds      *     all blocks if lit_bufsize is not greater than 32K.)
2021da177e4SLinus Torvalds      *   - if compression is not successful for a file smaller than 64K, we can
2031da177e4SLinus Torvalds      *     even emit a stored file instead of a stored block (saving 5 bytes).
2041da177e4SLinus Torvalds      *     This is applicable only for zip (not gzip or zlib).
2051da177e4SLinus Torvalds      *   - creating new Huffman trees less frequently may not provide fast
2061da177e4SLinus Torvalds      *     adaptation to changes in the input data statistics. (Take for
2071da177e4SLinus Torvalds      *     example a binary file with poorly compressible code followed by
2081da177e4SLinus Torvalds      *     a highly compressible string table.) Smaller buffer sizes give
2091da177e4SLinus Torvalds      *     fast adaptation but have of course the overhead of transmitting
2101da177e4SLinus Torvalds      *     trees more frequently.
2111da177e4SLinus Torvalds      *   - I can't count above 4
2121da177e4SLinus Torvalds      */
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds     uInt last_lit;      /* running index in l_buf */
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds     ush *d_buf;
2171da177e4SLinus Torvalds     /* Buffer for distances. To simplify the code, d_buf and l_buf have
2181da177e4SLinus Torvalds      * the same number of elements. To use different lengths, an extra flag
2191da177e4SLinus Torvalds      * array would be necessary.
2201da177e4SLinus Torvalds      */
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds     ulg opt_len;        /* bit length of current block with optimal trees */
2231da177e4SLinus Torvalds     ulg static_len;     /* bit length of current block with static trees */
2241da177e4SLinus Torvalds     ulg compressed_len; /* total bit length of compressed file */
2251da177e4SLinus Torvalds     uInt matches;       /* number of string matches in current block */
2261da177e4SLinus Torvalds     int last_eob_len;   /* bit length of EOB code for last block */
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds #ifdef DEBUG_ZLIB
2291da177e4SLinus Torvalds     ulg bits_sent;      /* bit length of the compressed data */
2301da177e4SLinus Torvalds #endif
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds     ush bi_buf;
2331da177e4SLinus Torvalds     /* Output buffer. bits are inserted starting at the bottom (least
2341da177e4SLinus Torvalds      * significant bits).
2351da177e4SLinus Torvalds      */
2361da177e4SLinus Torvalds     int bi_valid;
2371da177e4SLinus Torvalds     /* Number of valid bits in bi_buf.  All bits above the last valid bit
2381da177e4SLinus Torvalds      * are always zero.
2391da177e4SLinus Torvalds      */
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds } deflate_state;
2421da177e4SLinus Torvalds 
243aa5b395bSMikhail Zaslonko #ifdef CONFIG_ZLIB_DFLTCC
244aa5b395bSMikhail Zaslonko #define zlib_deflate_window_memsize(windowBits) \
245aa5b395bSMikhail Zaslonko 	(2 * (1 << (windowBits)) * sizeof(Byte) + PAGE_SIZE)
246aa5b395bSMikhail Zaslonko #else
247565d76cbSJim Keniston #define zlib_deflate_window_memsize(windowBits) \
248565d76cbSJim Keniston 	(2 * (1 << (windowBits)) * sizeof(Byte))
249aa5b395bSMikhail Zaslonko #endif
250565d76cbSJim Keniston #define zlib_deflate_prev_memsize(windowBits) \
251565d76cbSJim Keniston 	((1 << (windowBits)) * sizeof(Pos))
252565d76cbSJim Keniston #define zlib_deflate_head_memsize(memLevel) \
253565d76cbSJim Keniston 	((1 << ((memLevel)+7)) * sizeof(Pos))
254565d76cbSJim Keniston #define zlib_deflate_overlay_memsize(memLevel) \
255565d76cbSJim Keniston 	((1 << ((memLevel)+6)) * (sizeof(ush)+2))
256565d76cbSJim Keniston 
2571da177e4SLinus Torvalds /* Output a byte on the stream.
2581da177e4SLinus Torvalds  * IN assertion: there is enough room in pending_buf.
2591da177e4SLinus Torvalds  */
2601da177e4SLinus Torvalds #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
2641da177e4SLinus Torvalds /* Minimum amount of lookahead, except at the end of the input file.
2651da177e4SLinus Torvalds  * See deflate.c for comments about the MIN_MATCH+1.
2661da177e4SLinus Torvalds  */
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
2691da177e4SLinus Torvalds /* In order to simplify the code, particularly on 16 bit machines, match
2701da177e4SLinus Torvalds  * distances are limited to MAX_DIST instead of WSIZE.
2711da177e4SLinus Torvalds  */
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds         /* in trees.c */
2741da177e4SLinus Torvalds void zlib_tr_init         (deflate_state *s);
2751da177e4SLinus Torvalds int  zlib_tr_tally        (deflate_state *s, unsigned dist, unsigned lc);
2761da177e4SLinus Torvalds ulg  zlib_tr_flush_block  (deflate_state *s, char *buf, ulg stored_len,
2771da177e4SLinus Torvalds 			   int eof);
2781da177e4SLinus Torvalds void zlib_tr_align        (deflate_state *s);
2791da177e4SLinus Torvalds void zlib_tr_stored_block (deflate_state *s, char *buf, ulg stored_len,
2801da177e4SLinus Torvalds 			   int eof);
2811da177e4SLinus Torvalds void zlib_tr_stored_type_only (deflate_state *);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /* ===========================================================================
2851da177e4SLinus Torvalds  * Output a short LSB first on the stream.
2861da177e4SLinus Torvalds  * IN assertion: there is enough room in pendingBuf.
2871da177e4SLinus Torvalds  */
2881da177e4SLinus Torvalds #define put_short(s, w) { \
2891da177e4SLinus Torvalds     put_byte(s, (uch)((w) & 0xff)); \
2901da177e4SLinus Torvalds     put_byte(s, (uch)((ush)(w) >> 8)); \
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds /* ===========================================================================
294aa5b395bSMikhail Zaslonko  * Reverse the first len bits of a code, using straightforward code (a faster
295aa5b395bSMikhail Zaslonko  * method would use a table)
296aa5b395bSMikhail Zaslonko  * IN assertion: 1 <= len <= 15
297aa5b395bSMikhail Zaslonko  */
bi_reverse(unsigned code,int len)298aa5b395bSMikhail Zaslonko static inline unsigned  bi_reverse(
299aa5b395bSMikhail Zaslonko     unsigned code, /* the value to invert */
300aa5b395bSMikhail Zaslonko     int len        /* its bit length */
301aa5b395bSMikhail Zaslonko )
302aa5b395bSMikhail Zaslonko {
303aa5b395bSMikhail Zaslonko     register unsigned res = 0;
304aa5b395bSMikhail Zaslonko     do {
305aa5b395bSMikhail Zaslonko         res |= code & 1;
306aa5b395bSMikhail Zaslonko         code >>= 1, res <<= 1;
307aa5b395bSMikhail Zaslonko     } while (--len > 0);
308aa5b395bSMikhail Zaslonko     return res >> 1;
309aa5b395bSMikhail Zaslonko }
310aa5b395bSMikhail Zaslonko 
311aa5b395bSMikhail Zaslonko /* ===========================================================================
3121da177e4SLinus Torvalds  * Flush the bit buffer, keeping at most 7 bits in it.
3131da177e4SLinus Torvalds  */
bi_flush(deflate_state * s)3141da177e4SLinus Torvalds static inline void bi_flush(deflate_state *s)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds     if (s->bi_valid == 16) {
3171da177e4SLinus Torvalds         put_short(s, s->bi_buf);
3181da177e4SLinus Torvalds         s->bi_buf = 0;
3191da177e4SLinus Torvalds         s->bi_valid = 0;
3201da177e4SLinus Torvalds     } else if (s->bi_valid >= 8) {
3211da177e4SLinus Torvalds         put_byte(s, (Byte)s->bi_buf);
3221da177e4SLinus Torvalds         s->bi_buf >>= 8;
3231da177e4SLinus Torvalds         s->bi_valid -= 8;
3241da177e4SLinus Torvalds     }
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds /* ===========================================================================
3281da177e4SLinus Torvalds  * Flush the bit buffer and align the output on a byte boundary
3291da177e4SLinus Torvalds  */
bi_windup(deflate_state * s)3301da177e4SLinus Torvalds static inline void bi_windup(deflate_state *s)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds     if (s->bi_valid > 8) {
3331da177e4SLinus Torvalds         put_short(s, s->bi_buf);
3341da177e4SLinus Torvalds     } else if (s->bi_valid > 0) {
3351da177e4SLinus Torvalds         put_byte(s, (Byte)s->bi_buf);
3361da177e4SLinus Torvalds     }
3371da177e4SLinus Torvalds     s->bi_buf = 0;
3381da177e4SLinus Torvalds     s->bi_valid = 0;
3391da177e4SLinus Torvalds #ifdef DEBUG_ZLIB
3401da177e4SLinus Torvalds     s->bits_sent = (s->bits_sent+7) & ~7;
3411da177e4SLinus Torvalds #endif
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
344aa5b395bSMikhail Zaslonko typedef enum {
345aa5b395bSMikhail Zaslonko     need_more,      /* block not completed, need more input or more output */
346aa5b395bSMikhail Zaslonko     block_done,     /* block flush performed */
347aa5b395bSMikhail Zaslonko     finish_started, /* finish started, need only more output at next deflate */
348aa5b395bSMikhail Zaslonko     finish_done     /* finish done, accept no more input or output */
349aa5b395bSMikhail Zaslonko } block_state;
350aa5b395bSMikhail Zaslonko 
351aa5b395bSMikhail Zaslonko #define Buf_size (8 * 2*sizeof(char))
352aa5b395bSMikhail Zaslonko /* Number of bits used within bi_buf. (bi_buf might be implemented on
353aa5b395bSMikhail Zaslonko  * more than 16 bits on some systems.)
354aa5b395bSMikhail Zaslonko  */
355aa5b395bSMikhail Zaslonko 
356aa5b395bSMikhail Zaslonko /* ===========================================================================
357aa5b395bSMikhail Zaslonko  * Send a value on a given number of bits.
358aa5b395bSMikhail Zaslonko  * IN assertion: length <= 16 and value fits in length bits.
359aa5b395bSMikhail Zaslonko  */
360aa5b395bSMikhail Zaslonko #ifdef DEBUG_ZLIB
361aa5b395bSMikhail Zaslonko static void send_bits      (deflate_state *s, int value, int length);
362aa5b395bSMikhail Zaslonko 
send_bits(deflate_state * s,int value,int length)363aa5b395bSMikhail Zaslonko static void send_bits(
364aa5b395bSMikhail Zaslonko     deflate_state *s,
365aa5b395bSMikhail Zaslonko     int value,  /* value to send */
366aa5b395bSMikhail Zaslonko     int length  /* number of bits */
367aa5b395bSMikhail Zaslonko )
368aa5b395bSMikhail Zaslonko {
369aa5b395bSMikhail Zaslonko     Tracevv((stderr," l %2d v %4x ", length, value));
370aa5b395bSMikhail Zaslonko     Assert(length > 0 && length <= 15, "invalid length");
371aa5b395bSMikhail Zaslonko     s->bits_sent += (ulg)length;
372aa5b395bSMikhail Zaslonko 
373aa5b395bSMikhail Zaslonko     /* If not enough room in bi_buf, use (valid) bits from bi_buf and
374aa5b395bSMikhail Zaslonko      * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
375aa5b395bSMikhail Zaslonko      * unused bits in value.
376aa5b395bSMikhail Zaslonko      */
377aa5b395bSMikhail Zaslonko     if (s->bi_valid > (int)Buf_size - length) {
378aa5b395bSMikhail Zaslonko         s->bi_buf |= (value << s->bi_valid);
379aa5b395bSMikhail Zaslonko         put_short(s, s->bi_buf);
380aa5b395bSMikhail Zaslonko         s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
381aa5b395bSMikhail Zaslonko         s->bi_valid += length - Buf_size;
382aa5b395bSMikhail Zaslonko     } else {
383aa5b395bSMikhail Zaslonko         s->bi_buf |= value << s->bi_valid;
384aa5b395bSMikhail Zaslonko         s->bi_valid += length;
385aa5b395bSMikhail Zaslonko     }
386aa5b395bSMikhail Zaslonko }
387aa5b395bSMikhail Zaslonko #else /* !DEBUG_ZLIB */
388aa5b395bSMikhail Zaslonko 
389aa5b395bSMikhail Zaslonko #define send_bits(s, value, length) \
390aa5b395bSMikhail Zaslonko { int len = length;\
391aa5b395bSMikhail Zaslonko   if (s->bi_valid > (int)Buf_size - len) {\
392aa5b395bSMikhail Zaslonko     int val = value;\
393aa5b395bSMikhail Zaslonko     s->bi_buf |= (val << s->bi_valid);\
394aa5b395bSMikhail Zaslonko     put_short(s, s->bi_buf);\
395aa5b395bSMikhail Zaslonko     s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
396aa5b395bSMikhail Zaslonko     s->bi_valid += len - Buf_size;\
397aa5b395bSMikhail Zaslonko   } else {\
398aa5b395bSMikhail Zaslonko     s->bi_buf |= (value) << s->bi_valid;\
399aa5b395bSMikhail Zaslonko     s->bi_valid += len;\
400aa5b395bSMikhail Zaslonko   }\
401aa5b395bSMikhail Zaslonko }
402aa5b395bSMikhail Zaslonko #endif /* DEBUG_ZLIB */
403aa5b395bSMikhail Zaslonko 
zlib_tr_send_bits(deflate_state * s,int value,int length)404aa5b395bSMikhail Zaslonko static inline void zlib_tr_send_bits(
405aa5b395bSMikhail Zaslonko     deflate_state *s,
406aa5b395bSMikhail Zaslonko     int value,
407aa5b395bSMikhail Zaslonko     int length
408aa5b395bSMikhail Zaslonko )
409aa5b395bSMikhail Zaslonko {
410aa5b395bSMikhail Zaslonko     send_bits(s, value, length);
411aa5b395bSMikhail Zaslonko }
412aa5b395bSMikhail Zaslonko 
413aa5b395bSMikhail Zaslonko /* =========================================================================
414aa5b395bSMikhail Zaslonko  * Flush as much pending output as possible. All deflate() output goes
415aa5b395bSMikhail Zaslonko  * through this function so some applications may wish to modify it
416aa5b395bSMikhail Zaslonko  * to avoid allocating a large strm->next_out buffer and copying into it.
417aa5b395bSMikhail Zaslonko  * (See also read_buf()).
418aa5b395bSMikhail Zaslonko  */
flush_pending(z_streamp strm)419aa5b395bSMikhail Zaslonko static inline void flush_pending(
420aa5b395bSMikhail Zaslonko 	z_streamp strm
421aa5b395bSMikhail Zaslonko )
422aa5b395bSMikhail Zaslonko {
423*1c0a0af5SMikhail Zaslonko     unsigned len;
424aa5b395bSMikhail Zaslonko     deflate_state *s = (deflate_state *) strm->state;
425aa5b395bSMikhail Zaslonko 
426*1c0a0af5SMikhail Zaslonko     bi_flush(s);
427*1c0a0af5SMikhail Zaslonko     len = s->pending;
428aa5b395bSMikhail Zaslonko     if (len > strm->avail_out) len = strm->avail_out;
429aa5b395bSMikhail Zaslonko     if (len == 0) return;
430aa5b395bSMikhail Zaslonko 
431aa5b395bSMikhail Zaslonko     if (strm->next_out != NULL) {
432aa5b395bSMikhail Zaslonko 	memcpy(strm->next_out, s->pending_out, len);
433aa5b395bSMikhail Zaslonko 	strm->next_out += len;
434aa5b395bSMikhail Zaslonko     }
435aa5b395bSMikhail Zaslonko     s->pending_out += len;
436aa5b395bSMikhail Zaslonko     strm->total_out += len;
437aa5b395bSMikhail Zaslonko     strm->avail_out  -= len;
438aa5b395bSMikhail Zaslonko     s->pending -= len;
439aa5b395bSMikhail Zaslonko     if (s->pending == 0) {
440aa5b395bSMikhail Zaslonko         s->pending_out = s->pending_buf;
441aa5b395bSMikhail Zaslonko     }
442aa5b395bSMikhail Zaslonko }
443aa5b395bSMikhail Zaslonko #endif /* DEFUTIL_H */
444