Blame deps/zlib/deflate.h

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