Blame deps/zlib/deflate.h

Packit Service 20376f
/* deflate.h -- internal compression state
Packit Service 20376f
 * Copyright (C) 1995-2016 Jean-loup Gailly
Packit Service 20376f
 * For conditions of distribution and use, see copyright notice in zlib.h
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
/* WARNING: this file should *not* be used by applications. It is
Packit Service 20376f
   part of the implementation of the compression library and is
Packit Service 20376f
   subject to change. Applications should only use zlib.h.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
/* @(#) $Id$ */
Packit Service 20376f
Packit Service 20376f
#ifndef DEFLATE_H
Packit Service 20376f
#define DEFLATE_H
Packit Service 20376f
Packit Service 20376f
#include "zutil.h"
Packit Service 20376f
Packit Service 20376f
/* define NO_GZIP when compiling if you want to disable gzip header and
Packit Service 20376f
   trailer creation by deflate().  NO_GZIP would be used to avoid linking in
Packit Service 20376f
   the crc code when it is not needed.  For shared libraries, gzip encoding
Packit Service 20376f
   should be left enabled. */
Packit Service 20376f
#ifndef NO_GZIP
Packit Service 20376f
#  define GZIP
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
/* ===========================================================================
Packit Service 20376f
 * Internal compression state.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#define LENGTH_CODES 29
Packit Service 20376f
/* number of length codes, not counting the special END_BLOCK code */
Packit Service 20376f
Packit Service 20376f
#define LITERALS  256
Packit Service 20376f
/* number of literal bytes 0..255 */
Packit Service 20376f
Packit Service 20376f
#define L_CODES (LITERALS+1+LENGTH_CODES)
Packit Service 20376f
/* number of Literal or Length codes, including the END_BLOCK code */
Packit Service 20376f
Packit Service 20376f
#define D_CODES   30
Packit Service 20376f
/* number of distance codes */
Packit Service 20376f
Packit Service 20376f
#define BL_CODES  19
Packit Service 20376f
/* number of codes used to transfer the bit lengths */
Packit Service 20376f
Packit Service 20376f
#define HEAP_SIZE (2*L_CODES+1)
Packit Service 20376f
/* maximum heap size */
Packit Service 20376f
Packit Service 20376f
#define MAX_BITS 15
Packit Service 20376f
/* All codes must not exceed MAX_BITS bits */
Packit Service 20376f
Packit Service 20376f
#define Buf_size 16
Packit Service 20376f
/* size of bit buffer in bi_buf */
Packit Service 20376f
Packit Service 20376f
#define INIT_STATE    42    /* zlib header -> BUSY_STATE */
Packit Service 20376f
#ifdef GZIP
Packit Service 20376f
#  define GZIP_STATE  57    /* gzip header -> BUSY_STATE | EXTRA_STATE */
Packit Service 20376f
#endif
Packit Service 20376f
#define EXTRA_STATE   69    /* gzip extra block -> NAME_STATE */
Packit Service 20376f
#define NAME_STATE    73    /* gzip file name -> COMMENT_STATE */
Packit Service 20376f
#define COMMENT_STATE 91    /* gzip comment -> HCRC_STATE */
Packit Service 20376f
#define HCRC_STATE   103    /* gzip header CRC -> BUSY_STATE */
Packit Service 20376f
#define BUSY_STATE   113    /* deflate -> FINISH_STATE */
Packit Service 20376f
#define FINISH_STATE 666    /* stream complete */
Packit Service 20376f
/* Stream status */
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
/* Data structure describing a single value and its code string. */
Packit Service 20376f
typedef struct ct_data_s {
Packit Service 20376f
    union {
Packit Service 20376f
        ush  freq;       /* frequency count */
Packit Service 20376f
        ush  code;       /* bit string */
Packit Service 20376f
    } fc;
Packit Service 20376f
    union {
Packit Service 20376f
        ush  dad;        /* father node in Huffman tree */
Packit Service 20376f
        ush  len;        /* length of bit string */
Packit Service 20376f
    } dl;
Packit Service 20376f
} FAR ct_data;
Packit Service 20376f
Packit Service 20376f
#define Freq fc.freq
Packit Service 20376f
#define Code fc.code
Packit Service 20376f
#define Dad  dl.dad
Packit Service 20376f
#define Len  dl.len
Packit Service 20376f
Packit Service 20376f
typedef struct static_tree_desc_s  static_tree_desc;
Packit Service 20376f
Packit Service 20376f
typedef struct tree_desc_s {
Packit Service 20376f
    ct_data *dyn_tree;           /* the dynamic tree */
Packit Service 20376f
    int     max_code;            /* largest code with non zero frequency */
Packit Service 20376f
    const static_tree_desc *stat_desc;  /* the corresponding static tree */
Packit Service 20376f
} FAR tree_desc;
Packit Service 20376f
Packit Service 20376f
typedef ush Pos;
Packit Service 20376f
typedef Pos FAR Posf;
Packit Service 20376f
typedef unsigned IPos;
Packit Service 20376f
Packit Service 20376f
/* A Pos is an index in the character window. We use short instead of int to
Packit Service 20376f
 * save space in the various tables. IPos is used only for parameter passing.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
typedef struct internal_state {
Packit Service 20376f
    z_streamp strm;      /* pointer back to this zlib stream */
Packit Service 20376f
    int   status;        /* as the name implies */
Packit Service 20376f
    Bytef *pending_buf;  /* output still pending */
Packit Service 20376f
    ulg   pending_buf_size; /* size of pending_buf */
Packit Service 20376f
    Bytef *pending_out;  /* next pending byte to output to the stream */
Packit Service 20376f
    ulg   pending;       /* nb of bytes in the pending buffer */
Packit Service 20376f
    int   wrap;          /* bit 0 true for zlib, bit 1 true for gzip */
Packit Service 20376f
    gz_headerp  gzhead;  /* gzip header information to write */
Packit Service 20376f
    ulg   gzindex;       /* where in extra, name, or comment */
Packit Service 20376f
    Byte  method;        /* can only be DEFLATED */
Packit Service 20376f
    int   last_flush;    /* value of flush param for previous deflate call */
Packit Service 20376f
Packit Service 20376f
                /* used by deflate.c: */
Packit Service 20376f
Packit Service 20376f
    uInt  w_size;        /* LZ77 window size (32K by default) */
Packit Service 20376f
    uInt  w_bits;        /* log2(w_size)  (8..16) */
Packit Service 20376f
    uInt  w_mask;        /* w_size - 1 */
Packit Service 20376f
Packit Service 20376f
    Bytef *window;
Packit Service 20376f
    /* Sliding window. Input bytes are read into the second half of the window,
Packit Service 20376f
     * and move to the first half later to keep a dictionary of at least wSize
Packit Service 20376f
     * bytes. With this organization, matches are limited to a distance of
Packit Service 20376f
     * wSize-MAX_MATCH bytes, but this ensures that IO is always
Packit Service 20376f
     * performed with a length multiple of the block size. Also, it limits
Packit Service 20376f
     * the window size to 64K, which is quite useful on MSDOS.
Packit Service 20376f
     * To do: use the user input buffer as sliding window.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    ulg window_size;
Packit Service 20376f
    /* Actual size of window: 2*wSize, except when the user input buffer
Packit Service 20376f
     * is directly used as sliding window.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    Posf *prev;
Packit Service 20376f
    /* Link to older string with same hash index. To limit the size of this
Packit Service 20376f
     * array to 64K, this link is maintained only for the last 32K strings.
Packit Service 20376f
     * An index in this array is thus a window index modulo 32K.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    Posf *head; /* Heads of the hash chains or NIL. */
Packit Service 20376f
Packit Service 20376f
    uInt  ins_h;          /* hash index of string to be inserted */
Packit Service 20376f
    uInt  hash_size;      /* number of elements in hash table */
Packit Service 20376f
    uInt  hash_bits;      /* log2(hash_size) */
Packit Service 20376f
    uInt  hash_mask;      /* hash_size-1 */
Packit Service 20376f
Packit Service 20376f
    uInt  hash_shift;
Packit Service 20376f
    /* Number of bits by which ins_h must be shifted at each input
Packit Service 20376f
     * step. It must be such that after MIN_MATCH steps, the oldest
Packit Service 20376f
     * byte no longer takes part in the hash key, that is:
Packit Service 20376f
     *   hash_shift * MIN_MATCH >= hash_bits
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    long block_start;
Packit Service 20376f
    /* Window position at the beginning of the current output block. Gets
Packit Service 20376f
     * negative when the window is moved backwards.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    uInt match_length;           /* length of best match */
Packit Service 20376f
    IPos prev_match;             /* previous match */
Packit Service 20376f
    int match_available;         /* set if previous match exists */
Packit Service 20376f
    uInt strstart;               /* start of string to insert */
Packit Service 20376f
    uInt match_start;            /* start of matching string */
Packit Service 20376f
    uInt lookahead;              /* number of valid bytes ahead in window */
Packit Service 20376f
Packit Service 20376f
    uInt prev_length;
Packit Service 20376f
    /* Length of the best match at previous step. Matches not greater than this
Packit Service 20376f
     * are discarded. This is used in the lazy match evaluation.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    uInt max_chain_length;
Packit Service 20376f
    /* To speed up deflation, hash chains are never searched beyond this
Packit Service 20376f
     * length.  A higher limit improves compression ratio but degrades the
Packit Service 20376f
     * speed.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    uInt max_lazy_match;
Packit Service 20376f
    /* Attempt to find a better match only when the current match is strictly
Packit Service 20376f
     * smaller than this value. This mechanism is used only for compression
Packit Service 20376f
     * levels >= 4.
Packit Service 20376f
     */
Packit Service 20376f
#   define max_insert_length  max_lazy_match
Packit Service 20376f
    /* Insert new strings in the hash table only if the match length is not
Packit Service 20376f
     * greater than this length. This saves time but degrades compression.
Packit Service 20376f
     * max_insert_length is used only for compression levels <= 3.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    int level;    /* compression level (1..9) */
Packit Service 20376f
    int strategy; /* favor or force Huffman coding*/
Packit Service 20376f
Packit Service 20376f
    uInt good_match;
Packit Service 20376f
    /* Use a faster search when the previous match is longer than this */
Packit Service 20376f
Packit Service 20376f
    int nice_match; /* Stop searching when current match exceeds this */
Packit Service 20376f
Packit Service 20376f
                /* used by trees.c: */
Packit Service 20376f
    /* Didn't use ct_data typedef below to suppress compiler warning */
Packit Service 20376f
    struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
Packit Service 20376f
    struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
Packit Service 20376f
    struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
Packit Service 20376f
Packit Service 20376f
    struct tree_desc_s l_desc;               /* desc. for literal tree */
Packit Service 20376f
    struct tree_desc_s d_desc;               /* desc. for distance tree */
Packit Service 20376f
    struct tree_desc_s bl_desc;              /* desc. for bit length tree */
Packit Service 20376f
Packit Service 20376f
    ush bl_count[MAX_BITS+1];
Packit Service 20376f
    /* number of codes at each bit length for an optimal tree */
Packit Service 20376f
Packit Service 20376f
    int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
Packit Service 20376f
    int heap_len;               /* number of elements in the heap */
Packit Service 20376f
    int heap_max;               /* element of largest frequency */
Packit Service 20376f
    /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
Packit Service 20376f
     * The same heap array is used to build all trees.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    uch depth[2*L_CODES+1];
Packit Service 20376f
    /* Depth of each subtree used as tie breaker for trees of equal frequency
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    uchf *l_buf;          /* buffer for literals or lengths */
Packit Service 20376f
Packit Service 20376f
    uInt  lit_bufsize;
Packit Service 20376f
    /* Size of match buffer for literals/lengths.  There are 4 reasons for
Packit Service 20376f
     * limiting lit_bufsize to 64K:
Packit Service 20376f
     *   - frequencies can be kept in 16 bit counters
Packit Service 20376f
     *   - if compression is not successful for the first block, all input
Packit Service 20376f
     *     data is still in the window so we can still emit a stored block even
Packit Service 20376f
     *     when input comes from standard input.  (This can also be done for
Packit Service 20376f
     *     all blocks if lit_bufsize is not greater than 32K.)
Packit Service 20376f
     *   - if compression is not successful for a file smaller than 64K, we can
Packit Service 20376f
     *     even emit a stored file instead of a stored block (saving 5 bytes).
Packit Service 20376f
     *     This is applicable only for zip (not gzip or zlib).
Packit Service 20376f
     *   - creating new Huffman trees less frequently may not provide fast
Packit Service 20376f
     *     adaptation to changes in the input data statistics. (Take for
Packit Service 20376f
     *     example a binary file with poorly compressible code followed by
Packit Service 20376f
     *     a highly compressible string table.) Smaller buffer sizes give
Packit Service 20376f
     *     fast adaptation but have of course the overhead of transmitting
Packit Service 20376f
     *     trees more frequently.
Packit Service 20376f
     *   - I can't count above 4
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    uInt last_lit;      /* running index in l_buf */
Packit Service 20376f
Packit Service 20376f
    ushf *d_buf;
Packit Service 20376f
    /* Buffer for distances. To simplify the code, d_buf and l_buf have
Packit Service 20376f
     * the same number of elements. To use different lengths, an extra flag
Packit Service 20376f
     * array would be necessary.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    ulg opt_len;        /* bit length of current block with optimal trees */
Packit Service 20376f
    ulg static_len;     /* bit length of current block with static trees */
Packit Service 20376f
    uInt matches;       /* number of string matches in current block */
Packit Service 20376f
    uInt insert;        /* bytes at end of window left to insert */
Packit Service 20376f
Packit Service 20376f
#ifdef ZLIB_DEBUG
Packit Service 20376f
    ulg compressed_len; /* total bit length of compressed file mod 2^32 */
Packit Service 20376f
    ulg bits_sent;      /* bit length of compressed data sent mod 2^32 */
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
    ush bi_buf;
Packit Service 20376f
    /* Output buffer. bits are inserted starting at the bottom (least
Packit Service 20376f
     * significant bits).
Packit Service 20376f
     */
Packit Service 20376f
    int bi_valid;
Packit Service 20376f
    /* Number of valid bits in bi_buf.  All bits above the last valid bit
Packit Service 20376f
     * are always zero.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
    ulg high_water;
Packit Service 20376f
    /* High water mark offset in window for initialized bytes -- bytes above
Packit Service 20376f
     * this are set to zero in order to avoid memory check warnings when
Packit Service 20376f
     * longest match routines access bytes past the input.  This is then
Packit Service 20376f
     * updated to the new high water mark.
Packit Service 20376f
     */
Packit Service 20376f
Packit Service 20376f
} FAR deflate_state;
Packit Service 20376f
Packit Service 20376f
/* Output a byte on the stream.
Packit Service 20376f
 * IN assertion: there is enough room in pending_buf.
Packit Service 20376f
 */
Packit Service 20376f
#define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
Packit Service 20376f
/* Minimum amount of lookahead, except at the end of the input file.
Packit Service 20376f
 * See deflate.c for comments about the MIN_MATCH+1.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
Packit Service 20376f
/* In order to simplify the code, particularly on 16 bit machines, match
Packit Service 20376f
 * distances are limited to MAX_DIST instead of WSIZE.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#define WIN_INIT MAX_MATCH
Packit Service 20376f
/* Number of bytes after end of data in window to initialize in order to avoid
Packit Service 20376f
   memory checker errors from longest match routines */
Packit Service 20376f
Packit Service 20376f
        /* in trees.c */
Packit Service 20376f
void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
Packit Service 20376f
int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
Packit Service 20376f
void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
Packit Service 20376f
                        ulg stored_len, int last));
Packit Service 20376f
void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
Packit Service 20376f
void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
Packit Service 20376f
void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
Packit Service 20376f
                        ulg stored_len, int last));
Packit Service 20376f
Packit Service 20376f
#define d_code(dist) \
Packit Service 20376f
   ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
Packit Service 20376f
/* Mapping from a distance to a distance code. dist is the distance - 1 and
Packit Service 20376f
 * must not have side effects. _dist_code[256] and _dist_code[257] are never
Packit Service 20376f
 * used.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#ifndef ZLIB_DEBUG
Packit Service 20376f
/* Inline versions of _tr_tally for speed: */
Packit Service 20376f
Packit Service 20376f
#if defined(GEN_TREES_H) || !defined(STDC)
Packit Service 20376f
  extern uch ZLIB_INTERNAL _length_code[];
Packit Service 20376f
  extern uch ZLIB_INTERNAL _dist_code[];
Packit Service 20376f
#else
Packit Service 20376f
  extern const uch ZLIB_INTERNAL _length_code[];
Packit Service 20376f
  extern const uch ZLIB_INTERNAL _dist_code[];
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
# define _tr_tally_lit(s, c, flush) \
Packit Service 20376f
  { uch cc = (c); \
Packit Service 20376f
    s->d_buf[s->last_lit] = 0; \
Packit Service 20376f
    s->l_buf[s->last_lit++] = cc; \
Packit Service 20376f
    s->dyn_ltree[cc].Freq++; \
Packit Service 20376f
    flush = (s->last_lit == s->lit_bufsize-1); \
Packit Service 20376f
   }
Packit Service 20376f
# define _tr_tally_dist(s, distance, length, flush) \
Packit Service 20376f
  { uch len = (uch)(length); \
Packit Service 20376f
    ush dist = (ush)(distance); \
Packit Service 20376f
    s->d_buf[s->last_lit] = dist; \
Packit Service 20376f
    s->l_buf[s->last_lit++] = len; \
Packit Service 20376f
    dist--; \
Packit Service 20376f
    s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
Packit Service 20376f
    s->dyn_dtree[d_code(dist)].Freq++; \
Packit Service 20376f
    flush = (s->last_lit == s->lit_bufsize-1); \
Packit Service 20376f
  }
Packit Service 20376f
#else
Packit Service 20376f
# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
Packit Service 20376f
# define _tr_tally_dist(s, distance, length, flush) \
Packit Service 20376f
              flush = _tr_tally(s, distance, length)
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#endif /* DEFLATE_H */