Blame deps/zlib/trees.c

Packit Service 9885aa
/* trees.c -- output deflated data using Huffman coding
Packit Service 9885aa
 * Copyright (C) 1995-2017 Jean-loup Gailly
Packit Service 9885aa
 * detect_data_type() function provided freely by Cosmin Truta, 2006
Packit Service 9885aa
 * For conditions of distribution and use, see copyright notice in zlib.h
Packit Service 9885aa
 */
Packit Service 9885aa
Packit Service 9885aa
/*
Packit Service 9885aa
 *  ALGORITHM
Packit Service 9885aa
 *
Packit Service 9885aa
 *      The "deflation" process uses several Huffman trees. The more
Packit Service 9885aa
 *      common source values are represented by shorter bit sequences.
Packit Service 9885aa
 *
Packit Service 9885aa
 *      Each code tree is stored in a compressed form which is itself
Packit Service 9885aa
 * a Huffman encoding of the lengths of all the code strings (in
Packit Service 9885aa
 * ascending order by source values).  The actual code strings are
Packit Service 9885aa
 * reconstructed from the lengths in the inflate process, as described
Packit Service 9885aa
 * in the deflate specification.
Packit Service 9885aa
 *
Packit Service 9885aa
 *  REFERENCES
Packit Service 9885aa
 *
Packit Service 9885aa
 *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
Packit Service 9885aa
 *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
Packit Service 9885aa
 *
Packit Service 9885aa
 *      Storer, James A.
Packit Service 9885aa
 *          Data Compression:  Methods and Theory, pp. 49-50.
Packit Service 9885aa
 *          Computer Science Press, 1988.  ISBN 0-7167-8156-5.
Packit Service 9885aa
 *
Packit Service 9885aa
 *      Sedgewick, R.
Packit Service 9885aa
 *          Algorithms, p290.
Packit Service 9885aa
 *          Addison-Wesley, 1983. ISBN 0-201-06672-6.
Packit Service 9885aa
 */
Packit Service 9885aa
Packit Service 9885aa
/* @(#) $Id$ */
Packit Service 9885aa
Packit Service 9885aa
/* #define GEN_TREES_H */
Packit Service 9885aa
Packit Service 9885aa
#include "deflate.h"
Packit Service 9885aa
Packit Service 9885aa
#ifdef ZLIB_DEBUG
Packit Service 9885aa
#  include <ctype.h>
Packit Service 9885aa
#endif
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Constants
Packit Service 9885aa
 */
Packit Service 9885aa
Packit Service 9885aa
#define MAX_BL_BITS 7
Packit Service 9885aa
/* Bit length codes must not exceed MAX_BL_BITS bits */
Packit Service 9885aa
Packit Service 9885aa
#define END_BLOCK 256
Packit Service 9885aa
/* end of block literal code */
Packit Service 9885aa
Packit Service 9885aa
#define REP_3_6      16
Packit Service 9885aa
/* repeat previous bit length 3-6 times (2 bits of repeat count) */
Packit Service 9885aa
Packit Service 9885aa
#define REPZ_3_10    17
Packit Service 9885aa
/* repeat a zero length 3-10 times  (3 bits of repeat count) */
Packit Service 9885aa
Packit Service 9885aa
#define REPZ_11_138  18
Packit Service 9885aa
/* repeat a zero length 11-138 times  (7 bits of repeat count) */
Packit Service 9885aa
Packit Service 9885aa
local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
Packit Service 9885aa
   = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
Packit Service 9885aa
Packit Service 9885aa
local const int extra_dbits[D_CODES] /* extra bits for each distance code */
Packit Service 9885aa
   = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
Packit Service 9885aa
Packit Service 9885aa
local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
Packit Service 9885aa
   = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
Packit Service 9885aa
Packit Service 9885aa
local const uch bl_order[BL_CODES]
Packit Service 9885aa
   = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
Packit Service 9885aa
/* The lengths of the bit length codes are sent in order of decreasing
Packit Service 9885aa
 * probability, to avoid transmitting the lengths for unused bit length codes.
Packit Service 9885aa
 */
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Local data. These are initialized only once.
Packit Service 9885aa
 */
Packit Service 9885aa
Packit Service 9885aa
#define DIST_CODE_LEN  512 /* see definition of array dist_code below */
Packit Service 9885aa
Packit Service 9885aa
#if defined(GEN_TREES_H) || !defined(STDC)
Packit Service 9885aa
/* non ANSI compilers may not accept trees.h */
Packit Service 9885aa
Packit Service 9885aa
local ct_data static_ltree[L_CODES+2];
Packit Service 9885aa
/* The static literal tree. Since the bit lengths are imposed, there is no
Packit Service 9885aa
 * need for the L_CODES extra codes used during heap construction. However
Packit Service 9885aa
 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
Packit Service 9885aa
 * below).
Packit Service 9885aa
 */
Packit Service 9885aa
Packit Service 9885aa
local ct_data static_dtree[D_CODES];
Packit Service 9885aa
/* The static distance tree. (Actually a trivial tree since all codes use
Packit Service 9885aa
 * 5 bits.)
Packit Service 9885aa
 */
Packit Service 9885aa
Packit Service 9885aa
uch _dist_code[DIST_CODE_LEN];
Packit Service 9885aa
/* Distance codes. The first 256 values correspond to the distances
Packit Service 9885aa
 * 3 .. 258, the last 256 values correspond to the top 8 bits of
Packit Service 9885aa
 * the 15 bit distances.
Packit Service 9885aa
 */
Packit Service 9885aa
Packit Service 9885aa
uch _length_code[MAX_MATCH-MIN_MATCH+1];
Packit Service 9885aa
/* length code for each normalized match length (0 == MIN_MATCH) */
Packit Service 9885aa
Packit Service 9885aa
local int base_length[LENGTH_CODES];
Packit Service 9885aa
/* First normalized length for each code (0 = MIN_MATCH) */
Packit Service 9885aa
Packit Service 9885aa
local int base_dist[D_CODES];
Packit Service 9885aa
/* First normalized distance for each code (0 = distance of 1) */
Packit Service 9885aa
Packit Service 9885aa
#else
Packit Service 9885aa
#  include "trees.h"
Packit Service 9885aa
#endif /* GEN_TREES_H */
Packit Service 9885aa
Packit Service 9885aa
struct static_tree_desc_s {
Packit Service 9885aa
    const ct_data *static_tree;  /* static tree or NULL */
Packit Service 9885aa
    const intf *extra_bits;      /* extra bits for each code or NULL */
Packit Service 9885aa
    int     extra_base;          /* base index for extra_bits */
Packit Service 9885aa
    int     elems;               /* max number of elements in the tree */
Packit Service 9885aa
    int     max_length;          /* max bit length for the codes */
Packit Service 9885aa
};
Packit Service 9885aa
Packit Service 9885aa
local const static_tree_desc  static_l_desc =
Packit Service 9885aa
{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
Packit Service 9885aa
Packit Service 9885aa
local const static_tree_desc  static_d_desc =
Packit Service 9885aa
{static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS};
Packit Service 9885aa
Packit Service 9885aa
local const static_tree_desc  static_bl_desc =
Packit Service 9885aa
{(const ct_data *)0, extra_blbits, 0,   BL_CODES, MAX_BL_BITS};
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Local (static) routines in this file.
Packit Service 9885aa
 */
Packit Service 9885aa
Packit Service 9885aa
local void tr_static_init OF((void));
Packit Service 9885aa
local void init_block     OF((deflate_state *s));
Packit Service 9885aa
local void pqdownheap     OF((deflate_state *s, ct_data *tree, int k));
Packit Service 9885aa
local void gen_bitlen     OF((deflate_state *s, tree_desc *desc));
Packit Service 9885aa
local void gen_codes      OF((ct_data *tree, int max_code, ushf *bl_count));
Packit Service 9885aa
local void build_tree     OF((deflate_state *s, tree_desc *desc));
Packit Service 9885aa
local void scan_tree      OF((deflate_state *s, ct_data *tree, int max_code));
Packit Service 9885aa
local void send_tree      OF((deflate_state *s, ct_data *tree, int max_code));
Packit Service 9885aa
local int  build_bl_tree  OF((deflate_state *s));
Packit Service 9885aa
local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
Packit Service 9885aa
                              int blcodes));
Packit Service 9885aa
local void compress_block OF((deflate_state *s, const ct_data *ltree,
Packit Service 9885aa
                              const ct_data *dtree));
Packit Service 9885aa
local int  detect_data_type OF((deflate_state *s));
Packit Service 9885aa
local unsigned bi_reverse OF((unsigned value, int length));
Packit Service 9885aa
local void bi_windup      OF((deflate_state *s));
Packit Service 9885aa
local void bi_flush       OF((deflate_state *s));
Packit Service 9885aa
Packit Service 9885aa
#ifdef GEN_TREES_H
Packit Service 9885aa
local void gen_trees_header OF((void));
Packit Service 9885aa
#endif
Packit Service 9885aa
Packit Service 9885aa
#ifndef ZLIB_DEBUG
Packit Service 9885aa
#  define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
Packit Service 9885aa
   /* Send a code of the given tree. c and tree must not have side effects */
Packit Service 9885aa
Packit Service 9885aa
#else /* !ZLIB_DEBUG */
Packit Service 9885aa
#  define send_code(s, c, tree) \
Packit Service 9885aa
     { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
Packit Service 9885aa
       send_bits(s, tree[c].Code, tree[c].Len); }
Packit Service 9885aa
#endif
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Output a short LSB first on the stream.
Packit Service 9885aa
 * IN assertion: there is enough room in pendingBuf.
Packit Service 9885aa
 */
Packit Service 9885aa
#define put_short(s, w) { \
Packit Service 9885aa
    put_byte(s, (uch)((w) & 0xff)); \
Packit Service 9885aa
    put_byte(s, (uch)((ush)(w) >> 8)); \
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Send a value on a given number of bits.
Packit Service 9885aa
 * IN assertion: length <= 16 and value fits in length bits.
Packit Service 9885aa
 */
Packit Service 9885aa
#ifdef ZLIB_DEBUG
Packit Service 9885aa
local void send_bits      OF((deflate_state *s, int value, int length));
Packit Service 9885aa
Packit Service 9885aa
local void send_bits(s, value, length)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    int value;  /* value to send */
Packit Service 9885aa
    int length; /* number of bits */
Packit Service 9885aa
{
Packit Service 9885aa
    Tracevv((stderr," l %2d v %4x ", length, value));
Packit Service 9885aa
    Assert(length > 0 && length <= 15, "invalid length");
Packit Service 9885aa
    s->bits_sent += (ulg)length;
Packit Service 9885aa
Packit Service 9885aa
    /* If not enough room in bi_buf, use (valid) bits from bi_buf and
Packit Service 9885aa
     * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
Packit Service 9885aa
     * unused bits in value.
Packit Service 9885aa
     */
Packit Service 9885aa
    if (s->bi_valid > (int)Buf_size - length) {
Packit Service 9885aa
        s->bi_buf |= (ush)value << s->bi_valid;
Packit Service 9885aa
        put_short(s, s->bi_buf);
Packit Service 9885aa
        s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
Packit Service 9885aa
        s->bi_valid += length - Buf_size;
Packit Service 9885aa
    } else {
Packit Service 9885aa
        s->bi_buf |= (ush)value << s->bi_valid;
Packit Service 9885aa
        s->bi_valid += length;
Packit Service 9885aa
    }
Packit Service 9885aa
}
Packit Service 9885aa
#else /* !ZLIB_DEBUG */
Packit Service 9885aa
Packit Service 9885aa
#define send_bits(s, value, length) \
Packit Service 9885aa
{ int len = length;\
Packit Service 9885aa
  if (s->bi_valid > (int)Buf_size - len) {\
Packit Service 9885aa
    int val = (int)value;\
Packit Service 9885aa
    s->bi_buf |= (ush)val << s->bi_valid;\
Packit Service 9885aa
    put_short(s, s->bi_buf);\
Packit Service 9885aa
    s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
Packit Service 9885aa
    s->bi_valid += len - Buf_size;\
Packit Service 9885aa
  } else {\
Packit Service 9885aa
    s->bi_buf |= (ush)(value) << s->bi_valid;\
Packit Service 9885aa
    s->bi_valid += len;\
Packit Service 9885aa
  }\
Packit Service 9885aa
}
Packit Service 9885aa
#endif /* ZLIB_DEBUG */
Packit Service 9885aa
Packit Service 9885aa
Packit Service 9885aa
/* the arguments must not have side effects */
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Initialize the various 'constant' tables.
Packit Service 9885aa
 */
Packit Service 9885aa
local void tr_static_init()
Packit Service 9885aa
{
Packit Service 9885aa
#if defined(GEN_TREES_H) || !defined(STDC)
Packit Service 9885aa
    static int static_init_done = 0;
Packit Service 9885aa
    int n;        /* iterates over tree elements */
Packit Service 9885aa
    int bits;     /* bit counter */
Packit Service 9885aa
    int length;   /* length value */
Packit Service 9885aa
    int code;     /* code value */
Packit Service 9885aa
    int dist;     /* distance index */
Packit Service 9885aa
    ush bl_count[MAX_BITS+1];
Packit Service 9885aa
    /* number of codes at each bit length for an optimal tree */
Packit Service 9885aa
Packit Service 9885aa
    if (static_init_done) return;
Packit Service 9885aa
Packit Service 9885aa
    /* For some embedded targets, global variables are not initialized: */
Packit Service 9885aa
#ifdef NO_INIT_GLOBAL_POINTERS
Packit Service 9885aa
    static_l_desc.static_tree = static_ltree;
Packit Service 9885aa
    static_l_desc.extra_bits = extra_lbits;
Packit Service 9885aa
    static_d_desc.static_tree = static_dtree;
Packit Service 9885aa
    static_d_desc.extra_bits = extra_dbits;
Packit Service 9885aa
    static_bl_desc.extra_bits = extra_blbits;
Packit Service 9885aa
#endif
Packit Service 9885aa
Packit Service 9885aa
    /* Initialize the mapping length (0..255) -> length code (0..28) */
Packit Service 9885aa
    length = 0;
Packit Service 9885aa
    for (code = 0; code < LENGTH_CODES-1; code++) {
Packit Service 9885aa
        base_length[code] = length;
Packit Service 9885aa
        for (n = 0; n < (1<
Packit Service 9885aa
            _length_code[length++] = (uch)code;
Packit Service 9885aa
        }
Packit Service 9885aa
    }
Packit Service 9885aa
    Assert (length == 256, "tr_static_init: length != 256");
Packit Service 9885aa
    /* Note that the length 255 (match length 258) can be represented
Packit Service 9885aa
     * in two different ways: code 284 + 5 bits or code 285, so we
Packit Service 9885aa
     * overwrite length_code[255] to use the best encoding:
Packit Service 9885aa
     */
Packit Service 9885aa
    _length_code[length-1] = (uch)code;
Packit Service 9885aa
Packit Service 9885aa
    /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
Packit Service 9885aa
    dist = 0;
Packit Service 9885aa
    for (code = 0 ; code < 16; code++) {
Packit Service 9885aa
        base_dist[code] = dist;
Packit Service 9885aa
        for (n = 0; n < (1<
Packit Service 9885aa
            _dist_code[dist++] = (uch)code;
Packit Service 9885aa
        }
Packit Service 9885aa
    }
Packit Service 9885aa
    Assert (dist == 256, "tr_static_init: dist != 256");
Packit Service 9885aa
    dist >>= 7; /* from now on, all distances are divided by 128 */
Packit Service 9885aa
    for ( ; code < D_CODES; code++) {
Packit Service 9885aa
        base_dist[code] = dist << 7;
Packit Service 9885aa
        for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
Packit Service 9885aa
            _dist_code[256 + dist++] = (uch)code;
Packit Service 9885aa
        }
Packit Service 9885aa
    }
Packit Service 9885aa
    Assert (dist == 256, "tr_static_init: 256+dist != 512");
Packit Service 9885aa
Packit Service 9885aa
    /* Construct the codes of the static literal tree */
Packit Service 9885aa
    for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
Packit Service 9885aa
    n = 0;
Packit Service 9885aa
    while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
Packit Service 9885aa
    while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
Packit Service 9885aa
    while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
Packit Service 9885aa
    while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
Packit Service 9885aa
    /* Codes 286 and 287 do not exist, but we must include them in the
Packit Service 9885aa
     * tree construction to get a canonical Huffman tree (longest code
Packit Service 9885aa
     * all ones)
Packit Service 9885aa
     */
Packit Service 9885aa
    gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
Packit Service 9885aa
Packit Service 9885aa
    /* The static distance tree is trivial: */
Packit Service 9885aa
    for (n = 0; n < D_CODES; n++) {
Packit Service 9885aa
        static_dtree[n].Len = 5;
Packit Service 9885aa
        static_dtree[n].Code = bi_reverse((unsigned)n, 5);
Packit Service 9885aa
    }
Packit Service 9885aa
    static_init_done = 1;
Packit Service 9885aa
Packit Service 9885aa
#  ifdef GEN_TREES_H
Packit Service 9885aa
    gen_trees_header();
Packit Service 9885aa
#  endif
Packit Service 9885aa
#endif /* defined(GEN_TREES_H) || !defined(STDC) */
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Genererate the file trees.h describing the static trees.
Packit Service 9885aa
 */
Packit Service 9885aa
#ifdef GEN_TREES_H
Packit Service 9885aa
#  ifndef ZLIB_DEBUG
Packit Service 9885aa
#    include <stdio.h>
Packit Service 9885aa
#  endif
Packit Service 9885aa
Packit Service 9885aa
#  define SEPARATOR(i, last, width) \
Packit Service 9885aa
      ((i) == (last)? "\n};\n\n" :    \
Packit Service 9885aa
       ((i) % (width) == (width)-1 ? ",\n" : ", "))
Packit Service 9885aa
Packit Service 9885aa
void gen_trees_header()
Packit Service 9885aa
{
Packit Service 9885aa
    FILE *header = fopen("trees.h", "w");
Packit Service 9885aa
    int i;
Packit Service 9885aa
Packit Service 9885aa
    Assert (header != NULL, "Can't open trees.h");
Packit Service 9885aa
    fprintf(header,
Packit Service 9885aa
            "/* header created automatically with -DGEN_TREES_H */\n\n");
Packit Service 9885aa
Packit Service 9885aa
    fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
Packit Service 9885aa
    for (i = 0; i < L_CODES+2; i++) {
Packit Service 9885aa
        fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
Packit Service 9885aa
                static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
Packit Service 9885aa
    }
Packit Service 9885aa
Packit Service 9885aa
    fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
Packit Service 9885aa
    for (i = 0; i < D_CODES; i++) {
Packit Service 9885aa
        fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
Packit Service 9885aa
                static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
Packit Service 9885aa
    }
Packit Service 9885aa
Packit Service 9885aa
    fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
Packit Service 9885aa
    for (i = 0; i < DIST_CODE_LEN; i++) {
Packit Service 9885aa
        fprintf(header, "%2u%s", _dist_code[i],
Packit Service 9885aa
                SEPARATOR(i, DIST_CODE_LEN-1, 20));
Packit Service 9885aa
    }
Packit Service 9885aa
Packit Service 9885aa
    fprintf(header,
Packit Service 9885aa
        "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
Packit Service 9885aa
    for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
Packit Service 9885aa
        fprintf(header, "%2u%s", _length_code[i],
Packit Service 9885aa
                SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
Packit Service 9885aa
    }
Packit Service 9885aa
Packit Service 9885aa
    fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
Packit Service 9885aa
    for (i = 0; i < LENGTH_CODES; i++) {
Packit Service 9885aa
        fprintf(header, "%1u%s", base_length[i],
Packit Service 9885aa
                SEPARATOR(i, LENGTH_CODES-1, 20));
Packit Service 9885aa
    }
Packit Service 9885aa
Packit Service 9885aa
    fprintf(header, "local const int base_dist[D_CODES] = {\n");
Packit Service 9885aa
    for (i = 0; i < D_CODES; i++) {
Packit Service 9885aa
        fprintf(header, "%5u%s", base_dist[i],
Packit Service 9885aa
                SEPARATOR(i, D_CODES-1, 10));
Packit Service 9885aa
    }
Packit Service 9885aa
Packit Service 9885aa
    fclose(header);
Packit Service 9885aa
}
Packit Service 9885aa
#endif /* GEN_TREES_H */
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Initialize the tree data structures for a new zlib stream.
Packit Service 9885aa
 */
Packit Service 9885aa
void ZLIB_INTERNAL _tr_init(s)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
{
Packit Service 9885aa
    tr_static_init();
Packit Service 9885aa
Packit Service 9885aa
    s->l_desc.dyn_tree = s->dyn_ltree;
Packit Service 9885aa
    s->l_desc.stat_desc = &static_l_desc;
Packit Service 9885aa
Packit Service 9885aa
    s->d_desc.dyn_tree = s->dyn_dtree;
Packit Service 9885aa
    s->d_desc.stat_desc = &static_d_desc;
Packit Service 9885aa
Packit Service 9885aa
    s->bl_desc.dyn_tree = s->bl_tree;
Packit Service 9885aa
    s->bl_desc.stat_desc = &static_bl_desc;
Packit Service 9885aa
Packit Service 9885aa
    s->bi_buf = 0;
Packit Service 9885aa
    s->bi_valid = 0;
Packit Service 9885aa
#ifdef ZLIB_DEBUG
Packit Service 9885aa
    s->compressed_len = 0L;
Packit Service 9885aa
    s->bits_sent = 0L;
Packit Service 9885aa
#endif
Packit Service 9885aa
Packit Service 9885aa
    /* Initialize the first block of the first file: */
Packit Service 9885aa
    init_block(s);
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Initialize a new block.
Packit Service 9885aa
 */
Packit Service 9885aa
local void init_block(s)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
{
Packit Service 9885aa
    int n; /* iterates over tree elements */
Packit Service 9885aa
Packit Service 9885aa
    /* Initialize the trees. */
Packit Service 9885aa
    for (n = 0; n < L_CODES;  n++) s->dyn_ltree[n].Freq = 0;
Packit Service 9885aa
    for (n = 0; n < D_CODES;  n++) s->dyn_dtree[n].Freq = 0;
Packit Service 9885aa
    for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
Packit Service 9885aa
Packit Service 9885aa
    s->dyn_ltree[END_BLOCK].Freq = 1;
Packit Service 9885aa
    s->opt_len = s->static_len = 0L;
Packit Service 9885aa
    s->last_lit = s->matches = 0;
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
#define SMALLEST 1
Packit Service 9885aa
/* Index within the heap array of least frequent node in the Huffman tree */
Packit Service 9885aa
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Remove the smallest element from the heap and recreate the heap with
Packit Service 9885aa
 * one less element. Updates heap and heap_len.
Packit Service 9885aa
 */
Packit Service 9885aa
#define pqremove(s, tree, top) \
Packit Service 9885aa
{\
Packit Service 9885aa
    top = s->heap[SMALLEST]; \
Packit Service 9885aa
    s->heap[SMALLEST] = s->heap[s->heap_len--]; \
Packit Service 9885aa
    pqdownheap(s, tree, SMALLEST); \
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Compares to subtrees, using the tree depth as tie breaker when
Packit Service 9885aa
 * the subtrees have equal frequency. This minimizes the worst case length.
Packit Service 9885aa
 */
Packit Service 9885aa
#define smaller(tree, n, m, depth) \
Packit Service 9885aa
   (tree[n].Freq < tree[m].Freq || \
Packit Service 9885aa
   (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Restore the heap property by moving down the tree starting at node k,
Packit Service 9885aa
 * exchanging a node with the smallest of its two sons if necessary, stopping
Packit Service 9885aa
 * when the heap property is re-established (each father smaller than its
Packit Service 9885aa
 * two sons).
Packit Service 9885aa
 */
Packit Service 9885aa
local void pqdownheap(s, tree, k)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    ct_data *tree;  /* the tree to restore */
Packit Service 9885aa
    int k;               /* node to move down */
Packit Service 9885aa
{
Packit Service 9885aa
    int v = s->heap[k];
Packit Service 9885aa
    int j = k << 1;  /* left son of k */
Packit Service 9885aa
    while (j <= s->heap_len) {
Packit Service 9885aa
        /* Set j to the smallest of the two sons: */
Packit Service 9885aa
        if (j < s->heap_len &&
Packit Service 9885aa
            smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
Packit Service 9885aa
            j++;
Packit Service 9885aa
        }
Packit Service 9885aa
        /* Exit if v is smaller than both sons */
Packit Service 9885aa
        if (smaller(tree, v, s->heap[j], s->depth)) break;
Packit Service 9885aa
Packit Service 9885aa
        /* Exchange v with the smallest son */
Packit Service 9885aa
        s->heap[k] = s->heap[j];  k = j;
Packit Service 9885aa
Packit Service 9885aa
        /* And continue down the tree, setting j to the left son of k */
Packit Service 9885aa
        j <<= 1;
Packit Service 9885aa
    }
Packit Service 9885aa
    s->heap[k] = v;
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Compute the optimal bit lengths for a tree and update the total bit length
Packit Service 9885aa
 * for the current block.
Packit Service 9885aa
 * IN assertion: the fields freq and dad are set, heap[heap_max] and
Packit Service 9885aa
 *    above are the tree nodes sorted by increasing frequency.
Packit Service 9885aa
 * OUT assertions: the field len is set to the optimal bit length, the
Packit Service 9885aa
 *     array bl_count contains the frequencies for each bit length.
Packit Service 9885aa
 *     The length opt_len is updated; static_len is also updated if stree is
Packit Service 9885aa
 *     not null.
Packit Service 9885aa
 */
Packit Service 9885aa
local void gen_bitlen(s, desc)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    tree_desc *desc;    /* the tree descriptor */
Packit Service 9885aa
{
Packit Service 9885aa
    ct_data *tree        = desc->dyn_tree;
Packit Service 9885aa
    int max_code         = desc->max_code;
Packit Service 9885aa
    const ct_data *stree = desc->stat_desc->static_tree;
Packit Service 9885aa
    const intf *extra    = desc->stat_desc->extra_bits;
Packit Service 9885aa
    int base             = desc->stat_desc->extra_base;
Packit Service 9885aa
    int max_length       = desc->stat_desc->max_length;
Packit Service 9885aa
    int h;              /* heap index */
Packit Service 9885aa
    int n, m;           /* iterate over the tree elements */
Packit Service 9885aa
    int bits;           /* bit length */
Packit Service 9885aa
    int xbits;          /* extra bits */
Packit Service 9885aa
    ush f;              /* frequency */
Packit Service 9885aa
    int overflow = 0;   /* number of elements with bit length too large */
Packit Service 9885aa
Packit Service 9885aa
    for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
Packit Service 9885aa
Packit Service 9885aa
    /* In a first pass, compute the optimal bit lengths (which may
Packit Service 9885aa
     * overflow in the case of the bit length tree).
Packit Service 9885aa
     */
Packit Service 9885aa
    tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
Packit Service 9885aa
Packit Service 9885aa
    for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
Packit Service 9885aa
        n = s->heap[h];
Packit Service 9885aa
        bits = tree[tree[n].Dad].Len + 1;
Packit Service 9885aa
        if (bits > max_length) bits = max_length, overflow++;
Packit Service 9885aa
        tree[n].Len = (ush)bits;
Packit Service 9885aa
        /* We overwrite tree[n].Dad which is no longer needed */
Packit Service 9885aa
Packit Service 9885aa
        if (n > max_code) continue; /* not a leaf node */
Packit Service 9885aa
Packit Service 9885aa
        s->bl_count[bits]++;
Packit Service 9885aa
        xbits = 0;
Packit Service 9885aa
        if (n >= base) xbits = extra[n-base];
Packit Service 9885aa
        f = tree[n].Freq;
Packit Service 9885aa
        s->opt_len += (ulg)f * (unsigned)(bits + xbits);
Packit Service 9885aa
        if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits);
Packit Service 9885aa
    }
Packit Service 9885aa
    if (overflow == 0) return;
Packit Service 9885aa
Packit Service 9885aa
    Tracev((stderr,"\nbit length overflow\n"));
Packit Service 9885aa
    /* This happens for example on obj2 and pic of the Calgary corpus */
Packit Service 9885aa
Packit Service 9885aa
    /* Find the first bit length which could increase: */
Packit Service 9885aa
    do {
Packit Service 9885aa
        bits = max_length-1;
Packit Service 9885aa
        while (s->bl_count[bits] == 0) bits--;
Packit Service 9885aa
        s->bl_count[bits]--;      /* move one leaf down the tree */
Packit Service 9885aa
        s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
Packit Service 9885aa
        s->bl_count[max_length]--;
Packit Service 9885aa
        /* The brother of the overflow item also moves one step up,
Packit Service 9885aa
         * but this does not affect bl_count[max_length]
Packit Service 9885aa
         */
Packit Service 9885aa
        overflow -= 2;
Packit Service 9885aa
    } while (overflow > 0);
Packit Service 9885aa
Packit Service 9885aa
    /* Now recompute all bit lengths, scanning in increasing frequency.
Packit Service 9885aa
     * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
Packit Service 9885aa
     * lengths instead of fixing only the wrong ones. This idea is taken
Packit Service 9885aa
     * from 'ar' written by Haruhiko Okumura.)
Packit Service 9885aa
     */
Packit Service 9885aa
    for (bits = max_length; bits != 0; bits--) {
Packit Service 9885aa
        n = s->bl_count[bits];
Packit Service 9885aa
        while (n != 0) {
Packit Service 9885aa
            m = s->heap[--h];
Packit Service 9885aa
            if (m > max_code) continue;
Packit Service 9885aa
            if ((unsigned) tree[m].Len != (unsigned) bits) {
Packit Service 9885aa
                Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
Packit Service 9885aa
                s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq;
Packit Service 9885aa
                tree[m].Len = (ush)bits;
Packit Service 9885aa
            }
Packit Service 9885aa
            n--;
Packit Service 9885aa
        }
Packit Service 9885aa
    }
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Generate the codes for a given tree and bit counts (which need not be
Packit Service 9885aa
 * optimal).
Packit Service 9885aa
 * IN assertion: the array bl_count contains the bit length statistics for
Packit Service 9885aa
 * the given tree and the field len is set for all tree elements.
Packit Service 9885aa
 * OUT assertion: the field code is set for all tree elements of non
Packit Service 9885aa
 *     zero code length.
Packit Service 9885aa
 */
Packit Service 9885aa
local void gen_codes (tree, max_code, bl_count)
Packit Service 9885aa
    ct_data *tree;             /* the tree to decorate */
Packit Service 9885aa
    int max_code;              /* largest code with non zero frequency */
Packit Service 9885aa
    ushf *bl_count;            /* number of codes at each bit length */
Packit Service 9885aa
{
Packit Service 9885aa
    ush next_code[MAX_BITS+1]; /* next code value for each bit length */
Packit Service 9885aa
    unsigned code = 0;         /* running code value */
Packit Service 9885aa
    int bits;                  /* bit index */
Packit Service 9885aa
    int n;                     /* code index */
Packit Service 9885aa
Packit Service 9885aa
    /* The distribution counts are first used to generate the code values
Packit Service 9885aa
     * without bit reversal.
Packit Service 9885aa
     */
Packit Service 9885aa
    for (bits = 1; bits <= MAX_BITS; bits++) {
Packit Service 9885aa
        code = (code + bl_count[bits-1]) << 1;
Packit Service 9885aa
        next_code[bits] = (ush)code;
Packit Service 9885aa
    }
Packit Service 9885aa
    /* Check that the bit counts in bl_count are consistent. The last code
Packit Service 9885aa
     * must be all ones.
Packit Service 9885aa
     */
Packit Service 9885aa
    Assert (code + bl_count[MAX_BITS]-1 == (1<
Packit Service 9885aa
            "inconsistent bit counts");
Packit Service 9885aa
    Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
Packit Service 9885aa
Packit Service 9885aa
    for (n = 0;  n <= max_code; n++) {
Packit Service 9885aa
        int len = tree[n].Len;
Packit Service 9885aa
        if (len == 0) continue;
Packit Service 9885aa
        /* Now reverse the bits */
Packit Service 9885aa
        tree[n].Code = (ush)bi_reverse(next_code[len]++, len);
Packit Service 9885aa
Packit Service 9885aa
        Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
Packit Service 9885aa
             n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
Packit Service 9885aa
    }
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Construct one Huffman tree and assigns the code bit strings and lengths.
Packit Service 9885aa
 * Update the total bit length for the current block.
Packit Service 9885aa
 * IN assertion: the field freq is set for all tree elements.
Packit Service 9885aa
 * OUT assertions: the fields len and code are set to the optimal bit length
Packit Service 9885aa
 *     and corresponding code. The length opt_len is updated; static_len is
Packit Service 9885aa
 *     also updated if stree is not null. The field max_code is set.
Packit Service 9885aa
 */
Packit Service 9885aa
local void build_tree(s, desc)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    tree_desc *desc; /* the tree descriptor */
Packit Service 9885aa
{
Packit Service 9885aa
    ct_data *tree         = desc->dyn_tree;
Packit Service 9885aa
    const ct_data *stree  = desc->stat_desc->static_tree;
Packit Service 9885aa
    int elems             = desc->stat_desc->elems;
Packit Service 9885aa
    int n, m;          /* iterate over heap elements */
Packit Service 9885aa
    int max_code = -1; /* largest code with non zero frequency */
Packit Service 9885aa
    int node;          /* new node being created */
Packit Service 9885aa
Packit Service 9885aa
    /* Construct the initial heap, with least frequent element in
Packit Service 9885aa
     * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
Packit Service 9885aa
     * heap[0] is not used.
Packit Service 9885aa
     */
Packit Service 9885aa
    s->heap_len = 0, s->heap_max = HEAP_SIZE;
Packit Service 9885aa
Packit Service 9885aa
    for (n = 0; n < elems; n++) {
Packit Service 9885aa
        if (tree[n].Freq != 0) {
Packit Service 9885aa
            s->heap[++(s->heap_len)] = max_code = n;
Packit Service 9885aa
            s->depth[n] = 0;
Packit Service 9885aa
        } else {
Packit Service 9885aa
            tree[n].Len = 0;
Packit Service 9885aa
        }
Packit Service 9885aa
    }
Packit Service 9885aa
Packit Service 9885aa
    /* The pkzip format requires that at least one distance code exists,
Packit Service 9885aa
     * and that at least one bit should be sent even if there is only one
Packit Service 9885aa
     * possible code. So to avoid special checks later on we force at least
Packit Service 9885aa
     * two codes of non zero frequency.
Packit Service 9885aa
     */
Packit Service 9885aa
    while (s->heap_len < 2) {
Packit Service 9885aa
        node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
Packit Service 9885aa
        tree[node].Freq = 1;
Packit Service 9885aa
        s->depth[node] = 0;
Packit Service 9885aa
        s->opt_len--; if (stree) s->static_len -= stree[node].Len;
Packit Service 9885aa
        /* node is 0 or 1 so it does not have extra bits */
Packit Service 9885aa
    }
Packit Service 9885aa
    desc->max_code = max_code;
Packit Service 9885aa
Packit Service 9885aa
    /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
Packit Service 9885aa
     * establish sub-heaps of increasing lengths:
Packit Service 9885aa
     */
Packit Service 9885aa
    for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
Packit Service 9885aa
Packit Service 9885aa
    /* Construct the Huffman tree by repeatedly combining the least two
Packit Service 9885aa
     * frequent nodes.
Packit Service 9885aa
     */
Packit Service 9885aa
    node = elems;              /* next internal node of the tree */
Packit Service 9885aa
    do {
Packit Service 9885aa
        pqremove(s, tree, n);  /* n = node of least frequency */
Packit Service 9885aa
        m = s->heap[SMALLEST]; /* m = node of next least frequency */
Packit Service 9885aa
Packit Service 9885aa
        s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
Packit Service 9885aa
        s->heap[--(s->heap_max)] = m;
Packit Service 9885aa
Packit Service 9885aa
        /* Create a new node father of n and m */
Packit Service 9885aa
        tree[node].Freq = tree[n].Freq + tree[m].Freq;
Packit Service 9885aa
        s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
Packit Service 9885aa
                                s->depth[n] : s->depth[m]) + 1);
Packit Service 9885aa
        tree[n].Dad = tree[m].Dad = (ush)node;
Packit Service 9885aa
#ifdef DUMP_BL_TREE
Packit Service 9885aa
        if (tree == s->bl_tree) {
Packit Service 9885aa
            fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
Packit Service 9885aa
                    node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
Packit Service 9885aa
        }
Packit Service 9885aa
#endif
Packit Service 9885aa
        /* and insert the new node in the heap */
Packit Service 9885aa
        s->heap[SMALLEST] = node++;
Packit Service 9885aa
        pqdownheap(s, tree, SMALLEST);
Packit Service 9885aa
Packit Service 9885aa
    } while (s->heap_len >= 2);
Packit Service 9885aa
Packit Service 9885aa
    s->heap[--(s->heap_max)] = s->heap[SMALLEST];
Packit Service 9885aa
Packit Service 9885aa
    /* At this point, the fields freq and dad are set. We can now
Packit Service 9885aa
     * generate the bit lengths.
Packit Service 9885aa
     */
Packit Service 9885aa
    gen_bitlen(s, (tree_desc *)desc);
Packit Service 9885aa
Packit Service 9885aa
    /* The field len is now set, we can generate the bit codes */
Packit Service 9885aa
    gen_codes ((ct_data *)tree, max_code, s->bl_count);
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Scan a literal or distance tree to determine the frequencies of the codes
Packit Service 9885aa
 * in the bit length tree.
Packit Service 9885aa
 */
Packit Service 9885aa
local void scan_tree (s, tree, max_code)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    ct_data *tree;   /* the tree to be scanned */
Packit Service 9885aa
    int max_code;    /* and its largest code of non zero frequency */
Packit Service 9885aa
{
Packit Service 9885aa
    int n;                     /* iterates over all tree elements */
Packit Service 9885aa
    int prevlen = -1;          /* last emitted length */
Packit Service 9885aa
    int curlen;                /* length of current code */
Packit Service 9885aa
    int nextlen = tree[0].Len; /* length of next code */
Packit Service 9885aa
    int count = 0;             /* repeat count of the current code */
Packit Service 9885aa
    int max_count = 7;         /* max repeat count */
Packit Service 9885aa
    int min_count = 4;         /* min repeat count */
Packit Service 9885aa
Packit Service 9885aa
    if (nextlen == 0) max_count = 138, min_count = 3;
Packit Service 9885aa
    tree[max_code+1].Len = (ush)0xffff; /* guard */
Packit Service 9885aa
Packit Service 9885aa
    for (n = 0; n <= max_code; n++) {
Packit Service 9885aa
        curlen = nextlen; nextlen = tree[n+1].Len;
Packit Service 9885aa
        if (++count < max_count && curlen == nextlen) {
Packit Service 9885aa
            continue;
Packit Service 9885aa
        } else if (count < min_count) {
Packit Service 9885aa
            s->bl_tree[curlen].Freq += count;
Packit Service 9885aa
        } else if (curlen != 0) {
Packit Service 9885aa
            if (curlen != prevlen) s->bl_tree[curlen].Freq++;
Packit Service 9885aa
            s->bl_tree[REP_3_6].Freq++;
Packit Service 9885aa
        } else if (count <= 10) {
Packit Service 9885aa
            s->bl_tree[REPZ_3_10].Freq++;
Packit Service 9885aa
        } else {
Packit Service 9885aa
            s->bl_tree[REPZ_11_138].Freq++;
Packit Service 9885aa
        }
Packit Service 9885aa
        count = 0; prevlen = curlen;
Packit Service 9885aa
        if (nextlen == 0) {
Packit Service 9885aa
            max_count = 138, min_count = 3;
Packit Service 9885aa
        } else if (curlen == nextlen) {
Packit Service 9885aa
            max_count = 6, min_count = 3;
Packit Service 9885aa
        } else {
Packit Service 9885aa
            max_count = 7, min_count = 4;
Packit Service 9885aa
        }
Packit Service 9885aa
    }
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Send a literal or distance tree in compressed form, using the codes in
Packit Service 9885aa
 * bl_tree.
Packit Service 9885aa
 */
Packit Service 9885aa
local void send_tree (s, tree, max_code)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    ct_data *tree; /* the tree to be scanned */
Packit Service 9885aa
    int max_code;       /* and its largest code of non zero frequency */
Packit Service 9885aa
{
Packit Service 9885aa
    int n;                     /* iterates over all tree elements */
Packit Service 9885aa
    int prevlen = -1;          /* last emitted length */
Packit Service 9885aa
    int curlen;                /* length of current code */
Packit Service 9885aa
    int nextlen = tree[0].Len; /* length of next code */
Packit Service 9885aa
    int count = 0;             /* repeat count of the current code */
Packit Service 9885aa
    int max_count = 7;         /* max repeat count */
Packit Service 9885aa
    int min_count = 4;         /* min repeat count */
Packit Service 9885aa
Packit Service 9885aa
    /* tree[max_code+1].Len = -1; */  /* guard already set */
Packit Service 9885aa
    if (nextlen == 0) max_count = 138, min_count = 3;
Packit Service 9885aa
Packit Service 9885aa
    for (n = 0; n <= max_code; n++) {
Packit Service 9885aa
        curlen = nextlen; nextlen = tree[n+1].Len;
Packit Service 9885aa
        if (++count < max_count && curlen == nextlen) {
Packit Service 9885aa
            continue;
Packit Service 9885aa
        } else if (count < min_count) {
Packit Service 9885aa
            do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
Packit Service 9885aa
Packit Service 9885aa
        } else if (curlen != 0) {
Packit Service 9885aa
            if (curlen != prevlen) {
Packit Service 9885aa
                send_code(s, curlen, s->bl_tree); count--;
Packit Service 9885aa
            }
Packit Service 9885aa
            Assert(count >= 3 && count <= 6, " 3_6?");
Packit Service 9885aa
            send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
Packit Service 9885aa
Packit Service 9885aa
        } else if (count <= 10) {
Packit Service 9885aa
            send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
Packit Service 9885aa
Packit Service 9885aa
        } else {
Packit Service 9885aa
            send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
Packit Service 9885aa
        }
Packit Service 9885aa
        count = 0; prevlen = curlen;
Packit Service 9885aa
        if (nextlen == 0) {
Packit Service 9885aa
            max_count = 138, min_count = 3;
Packit Service 9885aa
        } else if (curlen == nextlen) {
Packit Service 9885aa
            max_count = 6, min_count = 3;
Packit Service 9885aa
        } else {
Packit Service 9885aa
            max_count = 7, min_count = 4;
Packit Service 9885aa
        }
Packit Service 9885aa
    }
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Construct the Huffman tree for the bit lengths and return the index in
Packit Service 9885aa
 * bl_order of the last bit length code to send.
Packit Service 9885aa
 */
Packit Service 9885aa
local int build_bl_tree(s)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
{
Packit Service 9885aa
    int max_blindex;  /* index of last bit length code of non zero freq */
Packit Service 9885aa
Packit Service 9885aa
    /* Determine the bit length frequencies for literal and distance trees */
Packit Service 9885aa
    scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
Packit Service 9885aa
    scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
Packit Service 9885aa
Packit Service 9885aa
    /* Build the bit length tree: */
Packit Service 9885aa
    build_tree(s, (tree_desc *)(&(s->bl_desc)));
Packit Service 9885aa
    /* opt_len now includes the length of the tree representations, except
Packit Service 9885aa
     * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
Packit Service 9885aa
     */
Packit Service 9885aa
Packit Service 9885aa
    /* Determine the number of bit length codes to send. The pkzip format
Packit Service 9885aa
     * requires that at least 4 bit length codes be sent. (appnote.txt says
Packit Service 9885aa
     * 3 but the actual value used is 4.)
Packit Service 9885aa
     */
Packit Service 9885aa
    for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
Packit Service 9885aa
        if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
Packit Service 9885aa
    }
Packit Service 9885aa
    /* Update opt_len to include the bit length tree and counts */
Packit Service 9885aa
    s->opt_len += 3*((ulg)max_blindex+1) + 5+5+4;
Packit Service 9885aa
    Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
Packit Service 9885aa
            s->opt_len, s->static_len));
Packit Service 9885aa
Packit Service 9885aa
    return max_blindex;
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Send the header for a block using dynamic Huffman trees: the counts, the
Packit Service 9885aa
 * lengths of the bit length codes, the literal tree and the distance tree.
Packit Service 9885aa
 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
Packit Service 9885aa
 */
Packit Service 9885aa
local void send_all_trees(s, lcodes, dcodes, blcodes)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    int lcodes, dcodes, blcodes; /* number of codes for each tree */
Packit Service 9885aa
{
Packit Service 9885aa
    int rank;                    /* index in bl_order */
Packit Service 9885aa
Packit Service 9885aa
    Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
Packit Service 9885aa
    Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
Packit Service 9885aa
            "too many codes");
Packit Service 9885aa
    Tracev((stderr, "\nbl counts: "));
Packit Service 9885aa
    send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
Packit Service 9885aa
    send_bits(s, dcodes-1,   5);
Packit Service 9885aa
    send_bits(s, blcodes-4,  4); /* not -3 as stated in appnote.txt */
Packit Service 9885aa
    for (rank = 0; rank < blcodes; rank++) {
Packit Service 9885aa
        Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
Packit Service 9885aa
        send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
Packit Service 9885aa
    }
Packit Service 9885aa
    Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
Packit Service 9885aa
Packit Service 9885aa
    send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
Packit Service 9885aa
    Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
Packit Service 9885aa
Packit Service 9885aa
    send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
Packit Service 9885aa
    Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Send a stored block
Packit Service 9885aa
 */
Packit Service 9885aa
void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    charf *buf;       /* input block */
Packit Service 9885aa
    ulg stored_len;   /* length of input block */
Packit Service 9885aa
    int last;         /* one if this is the last block for a file */
Packit Service 9885aa
{
Packit Service 9885aa
    send_bits(s, (STORED_BLOCK<<1)+last, 3);    /* send block type */
Packit Service 9885aa
    bi_windup(s);        /* align on byte boundary */
Packit Service 9885aa
    put_short(s, (ush)stored_len);
Packit Service 9885aa
    put_short(s, (ush)~stored_len);
Packit Service 9885aa
    zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len);
Packit Service 9885aa
    s->pending += stored_len;
Packit Service 9885aa
#ifdef ZLIB_DEBUG
Packit Service 9885aa
    s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
Packit Service 9885aa
    s->compressed_len += (stored_len + 4) << 3;
Packit Service 9885aa
    s->bits_sent += 2*16;
Packit Service 9885aa
    s->bits_sent += stored_len<<3;
Packit Service 9885aa
#endif
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
Packit Service 9885aa
 */
Packit Service 9885aa
void ZLIB_INTERNAL _tr_flush_bits(s)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
{
Packit Service 9885aa
    bi_flush(s);
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Send one empty static block to give enough lookahead for inflate.
Packit Service 9885aa
 * This takes 10 bits, of which 7 may remain in the bit buffer.
Packit Service 9885aa
 */
Packit Service 9885aa
void ZLIB_INTERNAL _tr_align(s)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
{
Packit Service 9885aa
    send_bits(s, STATIC_TREES<<1, 3);
Packit Service 9885aa
    send_code(s, END_BLOCK, static_ltree);
Packit Service 9885aa
#ifdef ZLIB_DEBUG
Packit Service 9885aa
    s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
Packit Service 9885aa
#endif
Packit Service 9885aa
    bi_flush(s);
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Determine the best encoding for the current block: dynamic trees, static
Packit Service 9885aa
 * trees or store, and write out the encoded block.
Packit Service 9885aa
 */
Packit Service 9885aa
void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    charf *buf;       /* input block, or NULL if too old */
Packit Service 9885aa
    ulg stored_len;   /* length of input block */
Packit Service 9885aa
    int last;         /* one if this is the last block for a file */
Packit Service 9885aa
{
Packit Service 9885aa
    ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
Packit Service 9885aa
    int max_blindex = 0;  /* index of last bit length code of non zero freq */
Packit Service 9885aa
Packit Service 9885aa
    /* Build the Huffman trees unless a stored block is forced */
Packit Service 9885aa
    if (s->level > 0) {
Packit Service 9885aa
Packit Service 9885aa
        /* Check if the file is binary or text */
Packit Service 9885aa
        if (s->strm->data_type == Z_UNKNOWN)
Packit Service 9885aa
            s->strm->data_type = detect_data_type(s);
Packit Service 9885aa
Packit Service 9885aa
        /* Construct the literal and distance trees */
Packit Service 9885aa
        build_tree(s, (tree_desc *)(&(s->l_desc)));
Packit Service 9885aa
        Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
Packit Service 9885aa
                s->static_len));
Packit Service 9885aa
Packit Service 9885aa
        build_tree(s, (tree_desc *)(&(s->d_desc)));
Packit Service 9885aa
        Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
Packit Service 9885aa
                s->static_len));
Packit Service 9885aa
        /* At this point, opt_len and static_len are the total bit lengths of
Packit Service 9885aa
         * the compressed block data, excluding the tree representations.
Packit Service 9885aa
         */
Packit Service 9885aa
Packit Service 9885aa
        /* Build the bit length tree for the above two trees, and get the index
Packit Service 9885aa
         * in bl_order of the last bit length code to send.
Packit Service 9885aa
         */
Packit Service 9885aa
        max_blindex = build_bl_tree(s);
Packit Service 9885aa
Packit Service 9885aa
        /* Determine the best encoding. Compute the block lengths in bytes. */
Packit Service 9885aa
        opt_lenb = (s->opt_len+3+7)>>3;
Packit Service 9885aa
        static_lenb = (s->static_len+3+7)>>3;
Packit Service 9885aa
Packit Service 9885aa
        Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
Packit Service 9885aa
                opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
Packit Service 9885aa
                s->last_lit));
Packit Service 9885aa
Packit Service 9885aa
        if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
Packit Service 9885aa
Packit Service 9885aa
    } else {
Packit Service 9885aa
        Assert(buf != (char*)0, "lost buf");
Packit Service 9885aa
        opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
Packit Service 9885aa
    }
Packit Service 9885aa
Packit Service 9885aa
#ifdef FORCE_STORED
Packit Service 9885aa
    if (buf != (char*)0) { /* force stored block */
Packit Service 9885aa
#else
Packit Service 9885aa
    if (stored_len+4 <= opt_lenb && buf != (char*)0) {
Packit Service 9885aa
                       /* 4: two words for the lengths */
Packit Service 9885aa
#endif
Packit Service 9885aa
        /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
Packit Service 9885aa
         * Otherwise we can't have processed more than WSIZE input bytes since
Packit Service 9885aa
         * the last block flush, because compression would have been
Packit Service 9885aa
         * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
Packit Service 9885aa
         * transform a block into a stored block.
Packit Service 9885aa
         */
Packit Service 9885aa
        _tr_stored_block(s, buf, stored_len, last);
Packit Service 9885aa
Packit Service 9885aa
#ifdef FORCE_STATIC
Packit Service 9885aa
    } else if (static_lenb >= 0) { /* force static trees */
Packit Service 9885aa
#else
Packit Service 9885aa
    } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
Packit Service 9885aa
#endif
Packit Service 9885aa
        send_bits(s, (STATIC_TREES<<1)+last, 3);
Packit Service 9885aa
        compress_block(s, (const ct_data *)static_ltree,
Packit Service 9885aa
                       (const ct_data *)static_dtree);
Packit Service 9885aa
#ifdef ZLIB_DEBUG
Packit Service 9885aa
        s->compressed_len += 3 + s->static_len;
Packit Service 9885aa
#endif
Packit Service 9885aa
    } else {
Packit Service 9885aa
        send_bits(s, (DYN_TREES<<1)+last, 3);
Packit Service 9885aa
        send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
Packit Service 9885aa
                       max_blindex+1);
Packit Service 9885aa
        compress_block(s, (const ct_data *)s->dyn_ltree,
Packit Service 9885aa
                       (const ct_data *)s->dyn_dtree);
Packit Service 9885aa
#ifdef ZLIB_DEBUG
Packit Service 9885aa
        s->compressed_len += 3 + s->opt_len;
Packit Service 9885aa
#endif
Packit Service 9885aa
    }
Packit Service 9885aa
    Assert (s->compressed_len == s->bits_sent, "bad compressed size");
Packit Service 9885aa
    /* The above check is made mod 2^32, for files larger than 512 MB
Packit Service 9885aa
     * and uLong implemented on 32 bits.
Packit Service 9885aa
     */
Packit Service 9885aa
    init_block(s);
Packit Service 9885aa
Packit Service 9885aa
    if (last) {
Packit Service 9885aa
        bi_windup(s);
Packit Service 9885aa
#ifdef ZLIB_DEBUG
Packit Service 9885aa
        s->compressed_len += 7;  /* align on byte boundary */
Packit Service 9885aa
#endif
Packit Service 9885aa
    }
Packit Service 9885aa
    Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
Packit Service 9885aa
           s->compressed_len-7*last));
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Save the match info and tally the frequency counts. Return true if
Packit Service 9885aa
 * the current block must be flushed.
Packit Service 9885aa
 */
Packit Service 9885aa
int ZLIB_INTERNAL _tr_tally (s, dist, lc)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    unsigned dist;  /* distance of matched string */
Packit Service 9885aa
    unsigned lc;    /* match length-MIN_MATCH or unmatched char (if dist==0) */
Packit Service 9885aa
{
Packit Service 9885aa
    s->d_buf[s->last_lit] = (ush)dist;
Packit Service 9885aa
    s->l_buf[s->last_lit++] = (uch)lc;
Packit Service 9885aa
    if (dist == 0) {
Packit Service 9885aa
        /* lc is the unmatched char */
Packit Service 9885aa
        s->dyn_ltree[lc].Freq++;
Packit Service 9885aa
    } else {
Packit Service 9885aa
        s->matches++;
Packit Service 9885aa
        /* Here, lc is the match length - MIN_MATCH */
Packit Service 9885aa
        dist--;             /* dist = match distance - 1 */
Packit Service 9885aa
        Assert((ush)dist < (ush)MAX_DIST(s) &&
Packit Service 9885aa
               (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
Packit Service 9885aa
               (ush)d_code(dist) < (ush)D_CODES,  "_tr_tally: bad match");
Packit Service 9885aa
Packit Service 9885aa
        s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
Packit Service 9885aa
        s->dyn_dtree[d_code(dist)].Freq++;
Packit Service 9885aa
    }
Packit Service 9885aa
Packit Service 9885aa
#ifdef TRUNCATE_BLOCK
Packit Service 9885aa
    /* Try to guess if it is profitable to stop the current block here */
Packit Service 9885aa
    if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
Packit Service 9885aa
        /* Compute an upper bound for the compressed length */
Packit Service 9885aa
        ulg out_length = (ulg)s->last_lit*8L;
Packit Service 9885aa
        ulg in_length = (ulg)((long)s->strstart - s->block_start);
Packit Service 9885aa
        int dcode;
Packit Service 9885aa
        for (dcode = 0; dcode < D_CODES; dcode++) {
Packit Service 9885aa
            out_length += (ulg)s->dyn_dtree[dcode].Freq *
Packit Service 9885aa
                (5L+extra_dbits[dcode]);
Packit Service 9885aa
        }
Packit Service 9885aa
        out_length >>= 3;
Packit Service 9885aa
        Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
Packit Service 9885aa
               s->last_lit, in_length, out_length,
Packit Service 9885aa
               100L - out_length*100L/in_length));
Packit Service 9885aa
        if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
Packit Service 9885aa
    }
Packit Service 9885aa
#endif
Packit Service 9885aa
    return (s->last_lit == s->lit_bufsize-1);
Packit Service 9885aa
    /* We avoid equality with lit_bufsize because of wraparound at 64K
Packit Service 9885aa
     * on 16 bit machines and because stored blocks are restricted to
Packit Service 9885aa
     * 64K-1 bytes.
Packit Service 9885aa
     */
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Send the block data compressed using the given Huffman trees
Packit Service 9885aa
 */
Packit Service 9885aa
local void compress_block(s, ltree, dtree)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
    const ct_data *ltree; /* literal tree */
Packit Service 9885aa
    const ct_data *dtree; /* distance tree */
Packit Service 9885aa
{
Packit Service 9885aa
    unsigned dist;      /* distance of matched string */
Packit Service 9885aa
    int lc;             /* match length or unmatched char (if dist == 0) */
Packit Service 9885aa
    unsigned lx = 0;    /* running index in l_buf */
Packit Service 9885aa
    unsigned code;      /* the code to send */
Packit Service 9885aa
    int extra;          /* number of extra bits to send */
Packit Service 9885aa
Packit Service 9885aa
    if (s->last_lit != 0) do {
Packit Service 9885aa
        dist = s->d_buf[lx];
Packit Service 9885aa
        lc = s->l_buf[lx++];
Packit Service 9885aa
        if (dist == 0) {
Packit Service 9885aa
            send_code(s, lc, ltree); /* send a literal byte */
Packit Service 9885aa
            Tracecv(isgraph(lc), (stderr," '%c' ", lc));
Packit Service 9885aa
        } else {
Packit Service 9885aa
            /* Here, lc is the match length - MIN_MATCH */
Packit Service 9885aa
            code = _length_code[lc];
Packit Service 9885aa
            send_code(s, code+LITERALS+1, ltree); /* send the length code */
Packit Service 9885aa
            extra = extra_lbits[code];
Packit Service 9885aa
            if (extra != 0) {
Packit Service 9885aa
                lc -= base_length[code];
Packit Service 9885aa
                send_bits(s, lc, extra);       /* send the extra length bits */
Packit Service 9885aa
            }
Packit Service 9885aa
            dist--; /* dist is now the match distance - 1 */
Packit Service 9885aa
            code = d_code(dist);
Packit Service 9885aa
            Assert (code < D_CODES, "bad d_code");
Packit Service 9885aa
Packit Service 9885aa
            send_code(s, code, dtree);       /* send the distance code */
Packit Service 9885aa
            extra = extra_dbits[code];
Packit Service 9885aa
            if (extra != 0) {
Packit Service 9885aa
                dist -= (unsigned)base_dist[code];
Packit Service 9885aa
                send_bits(s, dist, extra);   /* send the extra distance bits */
Packit Service 9885aa
            }
Packit Service 9885aa
        } /* literal or match pair ? */
Packit Service 9885aa
Packit Service 9885aa
        /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
Packit Service 9885aa
        Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
Packit Service 9885aa
               "pendingBuf overflow");
Packit Service 9885aa
Packit Service 9885aa
    } while (lx < s->last_lit);
Packit Service 9885aa
Packit Service 9885aa
    send_code(s, END_BLOCK, ltree);
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Check if the data type is TEXT or BINARY, using the following algorithm:
Packit Service 9885aa
 * - TEXT if the two conditions below are satisfied:
Packit Service 9885aa
 *    a) There are no non-portable control characters belonging to the
Packit Service 9885aa
 *       "black list" (0..6, 14..25, 28..31).
Packit Service 9885aa
 *    b) There is at least one printable character belonging to the
Packit Service 9885aa
 *       "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
Packit Service 9885aa
 * - BINARY otherwise.
Packit Service 9885aa
 * - The following partially-portable control characters form a
Packit Service 9885aa
 *   "gray list" that is ignored in this detection algorithm:
Packit Service 9885aa
 *   (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
Packit Service 9885aa
 * IN assertion: the fields Freq of dyn_ltree are set.
Packit Service 9885aa
 */
Packit Service 9885aa
local int detect_data_type(s)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
{
Packit Service 9885aa
    /* black_mask is the bit mask of black-listed bytes
Packit Service 9885aa
     * set bits 0..6, 14..25, and 28..31
Packit Service 9885aa
     * 0xf3ffc07f = binary 11110011111111111100000001111111
Packit Service 9885aa
     */
Packit Service 9885aa
    unsigned long black_mask = 0xf3ffc07fUL;
Packit Service 9885aa
    int n;
Packit Service 9885aa
Packit Service 9885aa
    /* Check for non-textual ("black-listed") bytes. */
Packit Service 9885aa
    for (n = 0; n <= 31; n++, black_mask >>= 1)
Packit Service 9885aa
        if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
Packit Service 9885aa
            return Z_BINARY;
Packit Service 9885aa
Packit Service 9885aa
    /* Check for textual ("white-listed") bytes. */
Packit Service 9885aa
    if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
Packit Service 9885aa
            || s->dyn_ltree[13].Freq != 0)
Packit Service 9885aa
        return Z_TEXT;
Packit Service 9885aa
    for (n = 32; n < LITERALS; n++)
Packit Service 9885aa
        if (s->dyn_ltree[n].Freq != 0)
Packit Service 9885aa
            return Z_TEXT;
Packit Service 9885aa
Packit Service 9885aa
    /* There are no "black-listed" or "white-listed" bytes:
Packit Service 9885aa
     * this stream either is empty or has tolerated ("gray-listed") bytes only.
Packit Service 9885aa
     */
Packit Service 9885aa
    return Z_BINARY;
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Reverse the first len bits of a code, using straightforward code (a faster
Packit Service 9885aa
 * method would use a table)
Packit Service 9885aa
 * IN assertion: 1 <= len <= 15
Packit Service 9885aa
 */
Packit Service 9885aa
local unsigned bi_reverse(code, len)
Packit Service 9885aa
    unsigned code; /* the value to invert */
Packit Service 9885aa
    int len;       /* its bit length */
Packit Service 9885aa
{
Packit Service 9885aa
    register unsigned res = 0;
Packit Service 9885aa
    do {
Packit Service 9885aa
        res |= code & 1;
Packit Service 9885aa
        code >>= 1, res <<= 1;
Packit Service 9885aa
    } while (--len > 0);
Packit Service 9885aa
    return res >> 1;
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Flush the bit buffer, keeping at most 7 bits in it.
Packit Service 9885aa
 */
Packit Service 9885aa
local void bi_flush(s)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
{
Packit Service 9885aa
    if (s->bi_valid == 16) {
Packit Service 9885aa
        put_short(s, s->bi_buf);
Packit Service 9885aa
        s->bi_buf = 0;
Packit Service 9885aa
        s->bi_valid = 0;
Packit Service 9885aa
    } else if (s->bi_valid >= 8) {
Packit Service 9885aa
        put_byte(s, (Byte)s->bi_buf);
Packit Service 9885aa
        s->bi_buf >>= 8;
Packit Service 9885aa
        s->bi_valid -= 8;
Packit Service 9885aa
    }
Packit Service 9885aa
}
Packit Service 9885aa
Packit Service 9885aa
/* ===========================================================================
Packit Service 9885aa
 * Flush the bit buffer and align the output on a byte boundary
Packit Service 9885aa
 */
Packit Service 9885aa
local void bi_windup(s)
Packit Service 9885aa
    deflate_state *s;
Packit Service 9885aa
{
Packit Service 9885aa
    if (s->bi_valid > 8) {
Packit Service 9885aa
        put_short(s, s->bi_buf);
Packit Service 9885aa
    } else if (s->bi_valid > 0) {
Packit Service 9885aa
        put_byte(s, (Byte)s->bi_buf);
Packit Service 9885aa
    }
Packit Service 9885aa
    s->bi_buf = 0;
Packit Service 9885aa
    s->bi_valid = 0;
Packit Service 9885aa
#ifdef ZLIB_DEBUG
Packit Service 9885aa
    s->bits_sent = (s->bits_sent+7) & ~7;
Packit Service 9885aa
#endif
Packit Service 9885aa
}