18c2ecf20Sopenharmony_ci#ifndef DEFUTIL_H
28c2ecf20Sopenharmony_ci#define DEFUTIL_H
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/zutil.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#define Assert(err, str)
78c2ecf20Sopenharmony_ci#define Trace(dummy)
88c2ecf20Sopenharmony_ci#define Tracev(dummy)
98c2ecf20Sopenharmony_ci#define Tracecv(err, dummy)
108c2ecf20Sopenharmony_ci#define Tracevv(dummy)
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define LENGTH_CODES 29
158c2ecf20Sopenharmony_ci/* number of length codes, not counting the special END_BLOCK code */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define LITERALS  256
188c2ecf20Sopenharmony_ci/* number of literal bytes 0..255 */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define L_CODES (LITERALS+1+LENGTH_CODES)
218c2ecf20Sopenharmony_ci/* number of Literal or Length codes, including the END_BLOCK code */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define D_CODES   30
248c2ecf20Sopenharmony_ci/* number of distance codes */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define BL_CODES  19
278c2ecf20Sopenharmony_ci/* number of codes used to transfer the bit lengths */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define HEAP_SIZE (2*L_CODES+1)
308c2ecf20Sopenharmony_ci/* maximum heap size */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define MAX_BITS 15
338c2ecf20Sopenharmony_ci/* All codes must not exceed MAX_BITS bits */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define INIT_STATE    42
368c2ecf20Sopenharmony_ci#define BUSY_STATE   113
378c2ecf20Sopenharmony_ci#define FINISH_STATE 666
388c2ecf20Sopenharmony_ci/* Stream status */
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Data structure describing a single value and its code string. */
428c2ecf20Sopenharmony_citypedef struct ct_data_s {
438c2ecf20Sopenharmony_ci    union {
448c2ecf20Sopenharmony_ci        ush  freq;       /* frequency count */
458c2ecf20Sopenharmony_ci        ush  code;       /* bit string */
468c2ecf20Sopenharmony_ci    } fc;
478c2ecf20Sopenharmony_ci    union {
488c2ecf20Sopenharmony_ci        ush  dad;        /* father node in Huffman tree */
498c2ecf20Sopenharmony_ci        ush  len;        /* length of bit string */
508c2ecf20Sopenharmony_ci    } dl;
518c2ecf20Sopenharmony_ci} ct_data;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define Freq fc.freq
548c2ecf20Sopenharmony_ci#define Code fc.code
558c2ecf20Sopenharmony_ci#define Dad  dl.dad
568c2ecf20Sopenharmony_ci#define Len  dl.len
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_citypedef struct static_tree_desc_s  static_tree_desc;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_citypedef struct tree_desc_s {
618c2ecf20Sopenharmony_ci    ct_data *dyn_tree;           /* the dynamic tree */
628c2ecf20Sopenharmony_ci    int     max_code;            /* largest code with non zero frequency */
638c2ecf20Sopenharmony_ci    static_tree_desc *stat_desc; /* the corresponding static tree */
648c2ecf20Sopenharmony_ci} tree_desc;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_citypedef ush Pos;
678c2ecf20Sopenharmony_citypedef unsigned IPos;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* A Pos is an index in the character window. We use short instead of int to
708c2ecf20Sopenharmony_ci * save space in the various tables. IPos is used only for parameter passing.
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_citypedef struct deflate_state {
748c2ecf20Sopenharmony_ci    z_streamp strm;      /* pointer back to this zlib stream */
758c2ecf20Sopenharmony_ci    int   status;        /* as the name implies */
768c2ecf20Sopenharmony_ci    Byte *pending_buf;   /* output still pending */
778c2ecf20Sopenharmony_ci    ulg   pending_buf_size; /* size of pending_buf */
788c2ecf20Sopenharmony_ci    Byte *pending_out;   /* next pending byte to output to the stream */
798c2ecf20Sopenharmony_ci    int   pending;       /* nb of bytes in the pending buffer */
808c2ecf20Sopenharmony_ci    int   noheader;      /* suppress zlib header and adler32 */
818c2ecf20Sopenharmony_ci    Byte  data_type;     /* UNKNOWN, BINARY or ASCII */
828c2ecf20Sopenharmony_ci    Byte  method;        /* STORED (for zip only) or DEFLATED */
838c2ecf20Sopenharmony_ci    int   last_flush;    /* value of flush param for previous deflate call */
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci                /* used by deflate.c: */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci    uInt  w_size;        /* LZ77 window size (32K by default) */
888c2ecf20Sopenharmony_ci    uInt  w_bits;        /* log2(w_size)  (8..16) */
898c2ecf20Sopenharmony_ci    uInt  w_mask;        /* w_size - 1 */
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci    Byte *window;
928c2ecf20Sopenharmony_ci    /* Sliding window. Input bytes are read into the second half of the window,
938c2ecf20Sopenharmony_ci     * and move to the first half later to keep a dictionary of at least wSize
948c2ecf20Sopenharmony_ci     * bytes. With this organization, matches are limited to a distance of
958c2ecf20Sopenharmony_ci     * wSize-MAX_MATCH bytes, but this ensures that IO is always
968c2ecf20Sopenharmony_ci     * performed with a length multiple of the block size. Also, it limits
978c2ecf20Sopenharmony_ci     * the window size to 64K, which is quite useful on MSDOS.
988c2ecf20Sopenharmony_ci     * To do: use the user input buffer as sliding window.
998c2ecf20Sopenharmony_ci     */
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci    ulg window_size;
1028c2ecf20Sopenharmony_ci    /* Actual size of window: 2*wSize, except when the user input buffer
1038c2ecf20Sopenharmony_ci     * is directly used as sliding window.
1048c2ecf20Sopenharmony_ci     */
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci    Pos *prev;
1078c2ecf20Sopenharmony_ci    /* Link to older string with same hash index. To limit the size of this
1088c2ecf20Sopenharmony_ci     * array to 64K, this link is maintained only for the last 32K strings.
1098c2ecf20Sopenharmony_ci     * An index in this array is thus a window index modulo 32K.
1108c2ecf20Sopenharmony_ci     */
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci    Pos *head; /* Heads of the hash chains or NIL. */
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci    uInt  ins_h;          /* hash index of string to be inserted */
1158c2ecf20Sopenharmony_ci    uInt  hash_size;      /* number of elements in hash table */
1168c2ecf20Sopenharmony_ci    uInt  hash_bits;      /* log2(hash_size) */
1178c2ecf20Sopenharmony_ci    uInt  hash_mask;      /* hash_size-1 */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci    uInt  hash_shift;
1208c2ecf20Sopenharmony_ci    /* Number of bits by which ins_h must be shifted at each input
1218c2ecf20Sopenharmony_ci     * step. It must be such that after MIN_MATCH steps, the oldest
1228c2ecf20Sopenharmony_ci     * byte no longer takes part in the hash key, that is:
1238c2ecf20Sopenharmony_ci     *   hash_shift * MIN_MATCH >= hash_bits
1248c2ecf20Sopenharmony_ci     */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci    long block_start;
1278c2ecf20Sopenharmony_ci    /* Window position at the beginning of the current output block. Gets
1288c2ecf20Sopenharmony_ci     * negative when the window is moved backwards.
1298c2ecf20Sopenharmony_ci     */
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci    uInt match_length;           /* length of best match */
1328c2ecf20Sopenharmony_ci    IPos prev_match;             /* previous match */
1338c2ecf20Sopenharmony_ci    int match_available;         /* set if previous match exists */
1348c2ecf20Sopenharmony_ci    uInt strstart;               /* start of string to insert */
1358c2ecf20Sopenharmony_ci    uInt match_start;            /* start of matching string */
1368c2ecf20Sopenharmony_ci    uInt lookahead;              /* number of valid bytes ahead in window */
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci    uInt prev_length;
1398c2ecf20Sopenharmony_ci    /* Length of the best match at previous step. Matches not greater than this
1408c2ecf20Sopenharmony_ci     * are discarded. This is used in the lazy match evaluation.
1418c2ecf20Sopenharmony_ci     */
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci    uInt max_chain_length;
1448c2ecf20Sopenharmony_ci    /* To speed up deflation, hash chains are never searched beyond this
1458c2ecf20Sopenharmony_ci     * length.  A higher limit improves compression ratio but degrades the
1468c2ecf20Sopenharmony_ci     * speed.
1478c2ecf20Sopenharmony_ci     */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci    uInt max_lazy_match;
1508c2ecf20Sopenharmony_ci    /* Attempt to find a better match only when the current match is strictly
1518c2ecf20Sopenharmony_ci     * smaller than this value. This mechanism is used only for compression
1528c2ecf20Sopenharmony_ci     * levels >= 4.
1538c2ecf20Sopenharmony_ci     */
1548c2ecf20Sopenharmony_ci#   define max_insert_length  max_lazy_match
1558c2ecf20Sopenharmony_ci    /* Insert new strings in the hash table only if the match length is not
1568c2ecf20Sopenharmony_ci     * greater than this length. This saves time but degrades compression.
1578c2ecf20Sopenharmony_ci     * max_insert_length is used only for compression levels <= 3.
1588c2ecf20Sopenharmony_ci     */
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci    int level;    /* compression level (1..9) */
1618c2ecf20Sopenharmony_ci    int strategy; /* favor or force Huffman coding*/
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci    uInt good_match;
1648c2ecf20Sopenharmony_ci    /* Use a faster search when the previous match is longer than this */
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci    int nice_match; /* Stop searching when current match exceeds this */
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci                /* used by trees.c: */
1698c2ecf20Sopenharmony_ci    /* Didn't use ct_data typedef below to suppress compiler warning */
1708c2ecf20Sopenharmony_ci    struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
1718c2ecf20Sopenharmony_ci    struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
1728c2ecf20Sopenharmony_ci    struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci    struct tree_desc_s l_desc;               /* desc. for literal tree */
1758c2ecf20Sopenharmony_ci    struct tree_desc_s d_desc;               /* desc. for distance tree */
1768c2ecf20Sopenharmony_ci    struct tree_desc_s bl_desc;              /* desc. for bit length tree */
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci    ush bl_count[MAX_BITS+1];
1798c2ecf20Sopenharmony_ci    /* number of codes at each bit length for an optimal tree */
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci    int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
1828c2ecf20Sopenharmony_ci    int heap_len;               /* number of elements in the heap */
1838c2ecf20Sopenharmony_ci    int heap_max;               /* element of largest frequency */
1848c2ecf20Sopenharmony_ci    /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
1858c2ecf20Sopenharmony_ci     * The same heap array is used to build all trees.
1868c2ecf20Sopenharmony_ci     */
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci    uch depth[2*L_CODES+1];
1898c2ecf20Sopenharmony_ci    /* Depth of each subtree used as tie breaker for trees of equal frequency
1908c2ecf20Sopenharmony_ci     */
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci    uch *l_buf;          /* buffer for literals or lengths */
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci    uInt  lit_bufsize;
1958c2ecf20Sopenharmony_ci    /* Size of match buffer for literals/lengths.  There are 4 reasons for
1968c2ecf20Sopenharmony_ci     * limiting lit_bufsize to 64K:
1978c2ecf20Sopenharmony_ci     *   - frequencies can be kept in 16 bit counters
1988c2ecf20Sopenharmony_ci     *   - if compression is not successful for the first block, all input
1998c2ecf20Sopenharmony_ci     *     data is still in the window so we can still emit a stored block even
2008c2ecf20Sopenharmony_ci     *     when input comes from standard input.  (This can also be done for
2018c2ecf20Sopenharmony_ci     *     all blocks if lit_bufsize is not greater than 32K.)
2028c2ecf20Sopenharmony_ci     *   - if compression is not successful for a file smaller than 64K, we can
2038c2ecf20Sopenharmony_ci     *     even emit a stored file instead of a stored block (saving 5 bytes).
2048c2ecf20Sopenharmony_ci     *     This is applicable only for zip (not gzip or zlib).
2058c2ecf20Sopenharmony_ci     *   - creating new Huffman trees less frequently may not provide fast
2068c2ecf20Sopenharmony_ci     *     adaptation to changes in the input data statistics. (Take for
2078c2ecf20Sopenharmony_ci     *     example a binary file with poorly compressible code followed by
2088c2ecf20Sopenharmony_ci     *     a highly compressible string table.) Smaller buffer sizes give
2098c2ecf20Sopenharmony_ci     *     fast adaptation but have of course the overhead of transmitting
2108c2ecf20Sopenharmony_ci     *     trees more frequently.
2118c2ecf20Sopenharmony_ci     *   - I can't count above 4
2128c2ecf20Sopenharmony_ci     */
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci    uInt last_lit;      /* running index in l_buf */
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci    ush *d_buf;
2178c2ecf20Sopenharmony_ci    /* Buffer for distances. To simplify the code, d_buf and l_buf have
2188c2ecf20Sopenharmony_ci     * the same number of elements. To use different lengths, an extra flag
2198c2ecf20Sopenharmony_ci     * array would be necessary.
2208c2ecf20Sopenharmony_ci     */
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci    ulg opt_len;        /* bit length of current block with optimal trees */
2238c2ecf20Sopenharmony_ci    ulg static_len;     /* bit length of current block with static trees */
2248c2ecf20Sopenharmony_ci    ulg compressed_len; /* total bit length of compressed file */
2258c2ecf20Sopenharmony_ci    uInt matches;       /* number of string matches in current block */
2268c2ecf20Sopenharmony_ci    int last_eob_len;   /* bit length of EOB code for last block */
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci#ifdef DEBUG_ZLIB
2298c2ecf20Sopenharmony_ci    ulg bits_sent;      /* bit length of the compressed data */
2308c2ecf20Sopenharmony_ci#endif
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci    ush bi_buf;
2338c2ecf20Sopenharmony_ci    /* Output buffer. bits are inserted starting at the bottom (least
2348c2ecf20Sopenharmony_ci     * significant bits).
2358c2ecf20Sopenharmony_ci     */
2368c2ecf20Sopenharmony_ci    int bi_valid;
2378c2ecf20Sopenharmony_ci    /* Number of valid bits in bi_buf.  All bits above the last valid bit
2388c2ecf20Sopenharmony_ci     * are always zero.
2398c2ecf20Sopenharmony_ci     */
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci} deflate_state;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci#ifdef CONFIG_ZLIB_DFLTCC
2448c2ecf20Sopenharmony_ci#define zlib_deflate_window_memsize(windowBits) \
2458c2ecf20Sopenharmony_ci	(2 * (1 << (windowBits)) * sizeof(Byte) + PAGE_SIZE)
2468c2ecf20Sopenharmony_ci#else
2478c2ecf20Sopenharmony_ci#define zlib_deflate_window_memsize(windowBits) \
2488c2ecf20Sopenharmony_ci	(2 * (1 << (windowBits)) * sizeof(Byte))
2498c2ecf20Sopenharmony_ci#endif
2508c2ecf20Sopenharmony_ci#define zlib_deflate_prev_memsize(windowBits) \
2518c2ecf20Sopenharmony_ci	((1 << (windowBits)) * sizeof(Pos))
2528c2ecf20Sopenharmony_ci#define zlib_deflate_head_memsize(memLevel) \
2538c2ecf20Sopenharmony_ci	((1 << ((memLevel)+7)) * sizeof(Pos))
2548c2ecf20Sopenharmony_ci#define zlib_deflate_overlay_memsize(memLevel) \
2558c2ecf20Sopenharmony_ci	((1 << ((memLevel)+6)) * (sizeof(ush)+2))
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci/* Output a byte on the stream.
2588c2ecf20Sopenharmony_ci * IN assertion: there is enough room in pending_buf.
2598c2ecf20Sopenharmony_ci */
2608c2ecf20Sopenharmony_ci#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
2648c2ecf20Sopenharmony_ci/* Minimum amount of lookahead, except at the end of the input file.
2658c2ecf20Sopenharmony_ci * See deflate.c for comments about the MIN_MATCH+1.
2668c2ecf20Sopenharmony_ci */
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci#define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
2698c2ecf20Sopenharmony_ci/* In order to simplify the code, particularly on 16 bit machines, match
2708c2ecf20Sopenharmony_ci * distances are limited to MAX_DIST instead of WSIZE.
2718c2ecf20Sopenharmony_ci */
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci        /* in trees.c */
2748c2ecf20Sopenharmony_civoid zlib_tr_init         (deflate_state *s);
2758c2ecf20Sopenharmony_ciint  zlib_tr_tally        (deflate_state *s, unsigned dist, unsigned lc);
2768c2ecf20Sopenharmony_ciulg  zlib_tr_flush_block  (deflate_state *s, char *buf, ulg stored_len,
2778c2ecf20Sopenharmony_ci			   int eof);
2788c2ecf20Sopenharmony_civoid zlib_tr_align        (deflate_state *s);
2798c2ecf20Sopenharmony_civoid zlib_tr_stored_block (deflate_state *s, char *buf, ulg stored_len,
2808c2ecf20Sopenharmony_ci			   int eof);
2818c2ecf20Sopenharmony_civoid zlib_tr_stored_type_only (deflate_state *);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/* ===========================================================================
2858c2ecf20Sopenharmony_ci * Output a short LSB first on the stream.
2868c2ecf20Sopenharmony_ci * IN assertion: there is enough room in pendingBuf.
2878c2ecf20Sopenharmony_ci */
2888c2ecf20Sopenharmony_ci#define put_short(s, w) { \
2898c2ecf20Sopenharmony_ci    put_byte(s, (uch)((w) & 0xff)); \
2908c2ecf20Sopenharmony_ci    put_byte(s, (uch)((ush)(w) >> 8)); \
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci/* ===========================================================================
2948c2ecf20Sopenharmony_ci * Reverse the first len bits of a code, using straightforward code (a faster
2958c2ecf20Sopenharmony_ci * method would use a table)
2968c2ecf20Sopenharmony_ci * IN assertion: 1 <= len <= 15
2978c2ecf20Sopenharmony_ci */
2988c2ecf20Sopenharmony_cistatic inline unsigned  bi_reverse(
2998c2ecf20Sopenharmony_ci    unsigned code, /* the value to invert */
3008c2ecf20Sopenharmony_ci    int len        /* its bit length */
3018c2ecf20Sopenharmony_ci)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci    register unsigned res = 0;
3048c2ecf20Sopenharmony_ci    do {
3058c2ecf20Sopenharmony_ci        res |= code & 1;
3068c2ecf20Sopenharmony_ci        code >>= 1, res <<= 1;
3078c2ecf20Sopenharmony_ci    } while (--len > 0);
3088c2ecf20Sopenharmony_ci    return res >> 1;
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci/* ===========================================================================
3128c2ecf20Sopenharmony_ci * Flush the bit buffer, keeping at most 7 bits in it.
3138c2ecf20Sopenharmony_ci */
3148c2ecf20Sopenharmony_cistatic inline void bi_flush(deflate_state *s)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci    if (s->bi_valid == 16) {
3178c2ecf20Sopenharmony_ci        put_short(s, s->bi_buf);
3188c2ecf20Sopenharmony_ci        s->bi_buf = 0;
3198c2ecf20Sopenharmony_ci        s->bi_valid = 0;
3208c2ecf20Sopenharmony_ci    } else if (s->bi_valid >= 8) {
3218c2ecf20Sopenharmony_ci        put_byte(s, (Byte)s->bi_buf);
3228c2ecf20Sopenharmony_ci        s->bi_buf >>= 8;
3238c2ecf20Sopenharmony_ci        s->bi_valid -= 8;
3248c2ecf20Sopenharmony_ci    }
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci/* ===========================================================================
3288c2ecf20Sopenharmony_ci * Flush the bit buffer and align the output on a byte boundary
3298c2ecf20Sopenharmony_ci */
3308c2ecf20Sopenharmony_cistatic inline void bi_windup(deflate_state *s)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci    if (s->bi_valid > 8) {
3338c2ecf20Sopenharmony_ci        put_short(s, s->bi_buf);
3348c2ecf20Sopenharmony_ci    } else if (s->bi_valid > 0) {
3358c2ecf20Sopenharmony_ci        put_byte(s, (Byte)s->bi_buf);
3368c2ecf20Sopenharmony_ci    }
3378c2ecf20Sopenharmony_ci    s->bi_buf = 0;
3388c2ecf20Sopenharmony_ci    s->bi_valid = 0;
3398c2ecf20Sopenharmony_ci#ifdef DEBUG_ZLIB
3408c2ecf20Sopenharmony_ci    s->bits_sent = (s->bits_sent+7) & ~7;
3418c2ecf20Sopenharmony_ci#endif
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_citypedef enum {
3458c2ecf20Sopenharmony_ci    need_more,      /* block not completed, need more input or more output */
3468c2ecf20Sopenharmony_ci    block_done,     /* block flush performed */
3478c2ecf20Sopenharmony_ci    finish_started, /* finish started, need only more output at next deflate */
3488c2ecf20Sopenharmony_ci    finish_done     /* finish done, accept no more input or output */
3498c2ecf20Sopenharmony_ci} block_state;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci#define Buf_size (8 * 2*sizeof(char))
3528c2ecf20Sopenharmony_ci/* Number of bits used within bi_buf. (bi_buf might be implemented on
3538c2ecf20Sopenharmony_ci * more than 16 bits on some systems.)
3548c2ecf20Sopenharmony_ci */
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci/* ===========================================================================
3578c2ecf20Sopenharmony_ci * Send a value on a given number of bits.
3588c2ecf20Sopenharmony_ci * IN assertion: length <= 16 and value fits in length bits.
3598c2ecf20Sopenharmony_ci */
3608c2ecf20Sopenharmony_ci#ifdef DEBUG_ZLIB
3618c2ecf20Sopenharmony_cistatic void send_bits      (deflate_state *s, int value, int length);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic void send_bits(
3648c2ecf20Sopenharmony_ci    deflate_state *s,
3658c2ecf20Sopenharmony_ci    int value,  /* value to send */
3668c2ecf20Sopenharmony_ci    int length  /* number of bits */
3678c2ecf20Sopenharmony_ci)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci    Tracevv((stderr," l %2d v %4x ", length, value));
3708c2ecf20Sopenharmony_ci    Assert(length > 0 && length <= 15, "invalid length");
3718c2ecf20Sopenharmony_ci    s->bits_sent += (ulg)length;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci    /* If not enough room in bi_buf, use (valid) bits from bi_buf and
3748c2ecf20Sopenharmony_ci     * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
3758c2ecf20Sopenharmony_ci     * unused bits in value.
3768c2ecf20Sopenharmony_ci     */
3778c2ecf20Sopenharmony_ci    if (s->bi_valid > (int)Buf_size - length) {
3788c2ecf20Sopenharmony_ci        s->bi_buf |= (value << s->bi_valid);
3798c2ecf20Sopenharmony_ci        put_short(s, s->bi_buf);
3808c2ecf20Sopenharmony_ci        s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
3818c2ecf20Sopenharmony_ci        s->bi_valid += length - Buf_size;
3828c2ecf20Sopenharmony_ci    } else {
3838c2ecf20Sopenharmony_ci        s->bi_buf |= value << s->bi_valid;
3848c2ecf20Sopenharmony_ci        s->bi_valid += length;
3858c2ecf20Sopenharmony_ci    }
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci#else /* !DEBUG_ZLIB */
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci#define send_bits(s, value, length) \
3908c2ecf20Sopenharmony_ci{ int len = length;\
3918c2ecf20Sopenharmony_ci  if (s->bi_valid > (int)Buf_size - len) {\
3928c2ecf20Sopenharmony_ci    int val = value;\
3938c2ecf20Sopenharmony_ci    s->bi_buf |= (val << s->bi_valid);\
3948c2ecf20Sopenharmony_ci    put_short(s, s->bi_buf);\
3958c2ecf20Sopenharmony_ci    s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
3968c2ecf20Sopenharmony_ci    s->bi_valid += len - Buf_size;\
3978c2ecf20Sopenharmony_ci  } else {\
3988c2ecf20Sopenharmony_ci    s->bi_buf |= (value) << s->bi_valid;\
3998c2ecf20Sopenharmony_ci    s->bi_valid += len;\
4008c2ecf20Sopenharmony_ci  }\
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci#endif /* DEBUG_ZLIB */
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic inline void zlib_tr_send_bits(
4058c2ecf20Sopenharmony_ci    deflate_state *s,
4068c2ecf20Sopenharmony_ci    int value,
4078c2ecf20Sopenharmony_ci    int length
4088c2ecf20Sopenharmony_ci)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci    send_bits(s, value, length);
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci/* =========================================================================
4148c2ecf20Sopenharmony_ci * Flush as much pending output as possible. All deflate() output goes
4158c2ecf20Sopenharmony_ci * through this function so some applications may wish to modify it
4168c2ecf20Sopenharmony_ci * to avoid allocating a large strm->next_out buffer and copying into it.
4178c2ecf20Sopenharmony_ci * (See also read_buf()).
4188c2ecf20Sopenharmony_ci */
4198c2ecf20Sopenharmony_cistatic inline void flush_pending(
4208c2ecf20Sopenharmony_ci	z_streamp strm
4218c2ecf20Sopenharmony_ci)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci    deflate_state *s = (deflate_state *) strm->state;
4248c2ecf20Sopenharmony_ci    unsigned len = s->pending;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci    if (len > strm->avail_out) len = strm->avail_out;
4278c2ecf20Sopenharmony_ci    if (len == 0) return;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci    if (strm->next_out != NULL) {
4308c2ecf20Sopenharmony_ci	memcpy(strm->next_out, s->pending_out, len);
4318c2ecf20Sopenharmony_ci	strm->next_out += len;
4328c2ecf20Sopenharmony_ci    }
4338c2ecf20Sopenharmony_ci    s->pending_out += len;
4348c2ecf20Sopenharmony_ci    strm->total_out += len;
4358c2ecf20Sopenharmony_ci    strm->avail_out  -= len;
4368c2ecf20Sopenharmony_ci    s->pending -= len;
4378c2ecf20Sopenharmony_ci    if (s->pending == 0) {
4388c2ecf20Sopenharmony_ci        s->pending_out = s->pending_buf;
4398c2ecf20Sopenharmony_ci    }
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ci#endif /* DEFUTIL_H */
442