Blame zlib-src/deflate.c

Packit Service 4a2782
/* deflate.c -- compress data using the deflation algorithm
Packit Service 4a2782
 * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
Packit Service 4a2782
 * For conditions of distribution and use, see copyright notice in zlib.h
Packit Service 4a2782
 */
Packit Service 4a2782
Packit Service 4a2782
/*
Packit Service 4a2782
 *  ALGORITHM
Packit Service 4a2782
 *
Packit Service 4a2782
 *      The "deflation" process depends on being able to identify portions
Packit Service 4a2782
 *      of the input text which are identical to earlier input (within a
Packit Service 4a2782
 *      sliding window trailing behind the input currently being processed).
Packit Service 4a2782
 *
Packit Service 4a2782
 *      The most straightforward technique turns out to be the fastest for
Packit Service 4a2782
 *      most input files: try all possible matches and select the longest.
Packit Service 4a2782
 *      The key feature of this algorithm is that insertions into the string
Packit Service 4a2782
 *      dictionary are very simple and thus fast, and deletions are avoided
Packit Service 4a2782
 *      completely. Insertions are performed at each input character, whereas
Packit Service 4a2782
 *      string matches are performed only when the previous match ends. So it
Packit Service 4a2782
 *      is preferable to spend more time in matches to allow very fast string
Packit Service 4a2782
 *      insertions and avoid deletions. The matching algorithm for small
Packit Service 4a2782
 *      strings is inspired from that of Rabin & Karp. A brute force approach
Packit Service 4a2782
 *      is used to find longer strings when a small match has been found.
Packit Service 4a2782
 *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
Packit Service 4a2782
 *      (by Leonid Broukhis).
Packit Service 4a2782
 *         A previous version of this file used a more sophisticated algorithm
Packit Service 4a2782
 *      (by Fiala and Greene) which is guaranteed to run in linear amortized
Packit Service 4a2782
 *      time, but has a larger average cost, uses more memory and is patented.
Packit Service 4a2782
 *      However the F&G algorithm may be faster for some highly redundant
Packit Service 4a2782
 *      files if the parameter max_chain_length (described below) is too large.
Packit Service 4a2782
 *
Packit Service 4a2782
 *  ACKNOWLEDGEMENTS
Packit Service 4a2782
 *
Packit Service 4a2782
 *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
Packit Service 4a2782
 *      I found it in 'freeze' written by Leonid Broukhis.
Packit Service 4a2782
 *      Thanks to many people for bug reports and testing.
Packit Service 4a2782
 *
Packit Service 4a2782
 *  REFERENCES
Packit Service 4a2782
 *
Packit Service 4a2782
 *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
Packit Service 4a2782
 *      Available in http://tools.ietf.org/html/rfc1951
Packit Service 4a2782
 *
Packit Service 4a2782
 *      A description of the Rabin and Karp algorithm is given in the book
Packit Service 4a2782
 *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
Packit Service 4a2782
 *
Packit Service 4a2782
 *      Fiala,E.R., and Greene,D.H.
Packit Service 4a2782
 *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
Packit Service 4a2782
 *
Packit Service 4a2782
 */
Packit Service 4a2782
Packit Service 4a2782
/* @(#) $Id$ */
Packit Service 4a2782
Packit Service 4a2782
#include "deflate.h"
Packit Service 4a2782
Packit Service 4a2782
const char deflate_copyright[] =
Packit Service 4a2782
   " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler ";
Packit Service 4a2782
/*
Packit Service 4a2782
  If you use the zlib library in a product, an acknowledgment is welcome
Packit Service 4a2782
  in the documentation of your product. If for some reason you cannot
Packit Service 4a2782
  include such an acknowledgment, I would appreciate that you keep this
Packit Service 4a2782
  copyright string in the executable of your product.
Packit Service 4a2782
 */
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 *  Function prototypes.
Packit Service 4a2782
 */
Packit Service 4a2782
typedef enum {
Packit Service 4a2782
    need_more,      /* block not completed, need more input or more output */
Packit Service 4a2782
    block_done,     /* block flush performed */
Packit Service 4a2782
    finish_started, /* finish started, need only more output at next deflate */
Packit Service 4a2782
    finish_done     /* finish done, accept no more input or output */
Packit Service 4a2782
} block_state;
Packit Service 4a2782
Packit Service 4a2782
typedef block_state (*compress_func) OF((deflate_state *s, int flush));
Packit Service 4a2782
/* Compression function. Returns the block state after the call. */
Packit Service 4a2782
Packit Service 4a2782
local int deflateStateCheck      OF((z_streamp strm));
Packit Service 4a2782
local void slide_hash     OF((deflate_state *s));
Packit Service 4a2782
local void fill_window    OF((deflate_state *s));
Packit Service 4a2782
local block_state deflate_stored OF((deflate_state *s, int flush));
Packit Service 4a2782
local block_state deflate_fast   OF((deflate_state *s, int flush));
Packit Service 4a2782
#ifndef FASTEST
Packit Service 4a2782
local block_state deflate_slow   OF((deflate_state *s, int flush));
Packit Service 4a2782
#endif
Packit Service 4a2782
local block_state deflate_rle    OF((deflate_state *s, int flush));
Packit Service 4a2782
local block_state deflate_huff   OF((deflate_state *s, int flush));
Packit Service 4a2782
local void lm_init        OF((deflate_state *s));
Packit Service 4a2782
local void putShortMSB    OF((deflate_state *s, uInt b));
Packit Service 4a2782
local void flush_pending  OF((z_streamp strm));
Packit Service 4a2782
local unsigned read_buf   OF((z_streamp strm, Bytef *buf, unsigned size));
Packit Service 4a2782
#ifdef ASMV
Packit Service 4a2782
#  pragma message("Assembler code may have bugs -- use at your own risk")
Packit Service 4a2782
      void match_init OF((void)); /* asm code initialization */
Packit Service 4a2782
      uInt longest_match  OF((deflate_state *s, IPos cur_match));
Packit Service 4a2782
#else
Packit Service 4a2782
local uInt longest_match  OF((deflate_state *s, IPos cur_match));
Packit Service 4a2782
#endif
Packit Service 4a2782
Packit Service 4a2782
#ifdef ZLIB_DEBUG
Packit Service 4a2782
local  void check_match OF((deflate_state *s, IPos start, IPos match,
Packit Service 4a2782
                            int length));
Packit Service 4a2782
#endif
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Local data
Packit Service 4a2782
 */
Packit Service 4a2782
Packit Service 4a2782
#define NIL 0
Packit Service 4a2782
/* Tail of hash chains */
Packit Service 4a2782
Packit Service 4a2782
#ifndef TOO_FAR
Packit Service 4a2782
#  define TOO_FAR 4096
Packit Service 4a2782
#endif
Packit Service 4a2782
/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
Packit Service 4a2782
Packit Service 4a2782
/* Values for max_lazy_match, good_match and max_chain_length, depending on
Packit Service 4a2782
 * the desired pack level (0..9). The values given below have been tuned to
Packit Service 4a2782
 * exclude worst case performance for pathological files. Better values may be
Packit Service 4a2782
 * found for specific files.
Packit Service 4a2782
 */
Packit Service 4a2782
typedef struct config_s {
Packit Service 4a2782
   ush good_length; /* reduce lazy search above this match length */
Packit Service 4a2782
   ush max_lazy;    /* do not perform lazy search above this match length */
Packit Service 4a2782
   ush nice_length; /* quit search above this match length */
Packit Service 4a2782
   ush max_chain;
Packit Service 4a2782
   compress_func func;
Packit Service 4a2782
} config;
Packit Service 4a2782
Packit Service 4a2782
#ifdef FASTEST
Packit Service 4a2782
local const config configuration_table[2] = {
Packit Service 4a2782
/*      good lazy nice chain */
Packit Service 4a2782
/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
Packit Service 4a2782
/* 1 */ {4,    4,  8,    4, deflate_fast}}; /* max speed, no lazy matches */
Packit Service 4a2782
#else
Packit Service 4a2782
local const config configuration_table[10] = {
Packit Service 4a2782
/*      good lazy nice chain */
Packit Service 4a2782
/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
Packit Service 4a2782
/* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
Packit Service 4a2782
/* 2 */ {4,    5, 16,    8, deflate_fast},
Packit Service 4a2782
/* 3 */ {4,    6, 32,   32, deflate_fast},
Packit Service 4a2782
Packit Service 4a2782
/* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
Packit Service 4a2782
/* 5 */ {8,   16, 32,   32, deflate_slow},
Packit Service 4a2782
/* 6 */ {8,   16, 128, 128, deflate_slow},
Packit Service 4a2782
/* 7 */ {8,   32, 128, 256, deflate_slow},
Packit Service 4a2782
/* 8 */ {32, 128, 258, 1024, deflate_slow},
Packit Service 4a2782
/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
Packit Service 4a2782
#endif
Packit Service 4a2782
Packit Service 4a2782
/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
Packit Service 4a2782
 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
Packit Service 4a2782
 * meaning.
Packit Service 4a2782
 */
Packit Service 4a2782
Packit Service 4a2782
/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
Packit Service 4a2782
#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Update a hash value with the given input byte
Packit Service 4a2782
 * IN  assertion: all calls to UPDATE_HASH are made with consecutive input
Packit Service 4a2782
 *    characters, so that a running hash key can be computed from the previous
Packit Service 4a2782
 *    key instead of complete recalculation each time.
Packit Service 4a2782
 */
Packit Service 4a2782
#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Insert string str in the dictionary and set match_head to the previous head
Packit Service 4a2782
 * of the hash chain (the most recent string with same hash key). Return
Packit Service 4a2782
 * the previous length of the hash chain.
Packit Service 4a2782
 * If this file is compiled with -DFASTEST, the compression level is forced
Packit Service 4a2782
 * to 1, and no hash chains are maintained.
Packit Service 4a2782
 * IN  assertion: all calls to INSERT_STRING are made with consecutive input
Packit Service 4a2782
 *    characters and the first MIN_MATCH bytes of str are valid (except for
Packit Service 4a2782
 *    the last MIN_MATCH-1 bytes of the input file).
Packit Service 4a2782
 */
Packit Service 4a2782
#ifdef FASTEST
Packit Service 4a2782
#define INSERT_STRING(s, str, match_head) \
Packit Service 4a2782
   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
Packit Service 4a2782
    match_head = s->head[s->ins_h], \
Packit Service 4a2782
    s->head[s->ins_h] = (Pos)(str))
Packit Service 4a2782
#else
Packit Service 4a2782
#define INSERT_STRING(s, str, match_head) \
Packit Service 4a2782
   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
Packit Service 4a2782
    match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
Packit Service 4a2782
    s->head[s->ins_h] = (Pos)(str))
Packit Service 4a2782
#endif
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
Packit Service 4a2782
 * prev[] will be initialized on the fly.
Packit Service 4a2782
 */
Packit Service 4a2782
#define CLEAR_HASH(s) \
Packit Service 4a2782
    s->head[s->hash_size-1] = NIL; \
Packit Service 4a2782
    zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Slide the hash table when sliding the window down (could be avoided with 32
Packit Service 4a2782
 * bit values at the expense of memory usage). We slide even when level == 0 to
Packit Service 4a2782
 * keep the hash table consistent if we switch back to level > 0 later.
Packit Service 4a2782
 */
Packit Service 4a2782
local void slide_hash(
Packit Service 4a2782
    deflate_state *s)
Packit Service 4a2782
{
Packit Service 4a2782
    unsigned n, m;
Packit Service 4a2782
    Posf *p;
Packit Service 4a2782
    uInt wsize = s->w_size;
Packit Service 4a2782
Packit Service 4a2782
    n = s->hash_size;
Packit Service 4a2782
    p = &s->head[n];
Packit Service 4a2782
    do {
Packit Service 4a2782
        m = *--p;
Packit Service 4a2782
        *p = (Pos)(m >= wsize ? m - wsize : NIL);
Packit Service 4a2782
    } while (--n);
Packit Service 4a2782
    n = wsize;
Packit Service 4a2782
#ifndef FASTEST
Packit Service 4a2782
    p = &s->prev[n];
Packit Service 4a2782
    do {
Packit Service 4a2782
        m = *--p;
Packit Service 4a2782
        *p = (Pos)(m >= wsize ? m - wsize : NIL);
Packit Service 4a2782
        /* If n is not on any hash chain, prev[n] is garbage but
Packit Service 4a2782
         * its value will never be used.
Packit Service 4a2782
         */
Packit Service 4a2782
    } while (--n);
Packit Service 4a2782
#endif
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateInit_(
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    int level,
Packit Service 4a2782
    const char *version,
Packit Service 4a2782
    int stream_size)
Packit Service 4a2782
{
Packit Service 4a2782
    return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
Packit Service 4a2782
                         Z_DEFAULT_STRATEGY, version, stream_size);
Packit Service 4a2782
    /* To do: ignore strm->next_in if we use it as window */
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateInit2_(
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    int  level,
Packit Service 4a2782
    int  method,
Packit Service 4a2782
    int  windowBits,
Packit Service 4a2782
    int  memLevel,
Packit Service 4a2782
    int  strategy,
Packit Service 4a2782
    const char *version,
Packit Service 4a2782
    int stream_size)
Packit Service 4a2782
{
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
    int wrap = 1;
Packit Service 4a2782
    static const char my_version[] = ZLIB_VERSION;
Packit Service 4a2782
Packit Service 4a2782
    ushf *overlay;
Packit Service 4a2782
    /* We overlay pending_buf and d_buf+l_buf. This works since the average
Packit Service 4a2782
     * output size for (length,distance) codes is <= 24 bits.
Packit Service 4a2782
     */
Packit Service 4a2782
Packit Service 4a2782
    if (version == Z_NULL || version[0] != my_version[0] ||
Packit Service 4a2782
        stream_size != sizeof(z_stream)) {
Packit Service 4a2782
        return Z_VERSION_ERROR;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (strm == Z_NULL) return Z_STREAM_ERROR;
Packit Service 4a2782
Packit Service 4a2782
    strm->msg = Z_NULL;
Packit Service 4a2782
    if (strm->zalloc == (alloc_func)0) {
Packit Service 4a2782
#ifdef Z_SOLO
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
#else
Packit Service 4a2782
        strm->zalloc = zcalloc;
Packit Service 4a2782
        strm->opaque = (voidpf)0;
Packit Service 4a2782
#endif
Packit Service 4a2782
    }
Packit Service 4a2782
    if (strm->zfree == (free_func)0)
Packit Service 4a2782
#ifdef Z_SOLO
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
#else
Packit Service 4a2782
        strm->zfree = zcfree;
Packit Service 4a2782
#endif
Packit Service 4a2782
Packit Service 4a2782
#ifdef FASTEST
Packit Service 4a2782
    if (level != 0) level = 1;
Packit Service 4a2782
#else
Packit Service 4a2782
    if (level == Z_DEFAULT_COMPRESSION) level = 6;
Packit Service 4a2782
#endif
Packit Service 4a2782
Packit Service 4a2782
    if (windowBits < 0) { /* suppress zlib wrapper */
Packit Service 4a2782
        wrap = 0;
Packit Service 4a2782
        windowBits = -windowBits;
Packit Service 4a2782
    }
Packit Service 4a2782
#ifdef GZIP
Packit Service 4a2782
    else if (windowBits > 15) {
Packit Service 4a2782
        wrap = 2;       /* write gzip wrapper instead */
Packit Service 4a2782
        windowBits -= 16;
Packit Service 4a2782
    }
Packit Service 4a2782
#endif
Packit Service 4a2782
    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
Packit Service 4a2782
        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
Packit Service 4a2782
        strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
Packit Service 4a2782
    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
Packit Service 4a2782
    if (s == Z_NULL) return Z_MEM_ERROR;
Packit Service 4a2782
    strm->state = (struct internal_state FAR *)s;
Packit Service 4a2782
    s->strm = strm;
Packit Service 4a2782
    s->status = INIT_STATE;     /* to pass state test in deflateReset() */
Packit Service 4a2782
Packit Service 4a2782
    s->wrap = wrap;
Packit Service 4a2782
    s->gzhead = Z_NULL;
Packit Service 4a2782
    s->w_bits = (uInt)windowBits;
Packit Service 4a2782
    s->w_size = 1 << s->w_bits;
Packit Service 4a2782
    s->w_mask = s->w_size - 1;
Packit Service 4a2782
Packit Service 4a2782
    s->hash_bits = (uInt)memLevel + 7;
Packit Service 4a2782
    s->hash_size = 1 << s->hash_bits;
Packit Service 4a2782
    s->hash_mask = s->hash_size - 1;
Packit Service 4a2782
    s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
Packit Service 4a2782
Packit Service 4a2782
    s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
Packit Service 4a2782
    s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
Packit Service 4a2782
    s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
Packit Service 4a2782
Packit Service 4a2782
    s->high_water = 0;      /* nothing written to s->window yet */
Packit Service 4a2782
Packit Service 4a2782
    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
Packit Service 4a2782
Packit Service 4a2782
    overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
Packit Service 4a2782
    s->pending_buf = (uchf *) overlay;
Packit Service 4a2782
    s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
Packit Service 4a2782
Packit Service 4a2782
    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
Packit Service 4a2782
        s->pending_buf == Z_NULL) {
Packit Service 4a2782
        s->status = FINISH_STATE;
Packit Service 4a2782
        strm->msg = ERR_MSG(Z_MEM_ERROR);
Packit Service 4a2782
        deflateEnd (strm);
Packit Service 4a2782
        return Z_MEM_ERROR;
Packit Service 4a2782
    }
Packit Service 4a2782
    s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
Packit Service 4a2782
    s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
Packit Service 4a2782
Packit Service 4a2782
    s->level = level;
Packit Service 4a2782
    s->strategy = strategy;
Packit Service 4a2782
    s->method = (Byte)method;
Packit Service 4a2782
Packit Service 4a2782
    return deflateReset(strm);
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* =========================================================================
Packit Service 4a2782
 * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
Packit Service 4a2782
 */
Packit Service 4a2782
local int deflateStateCheck (
Packit Service 4a2782
    z_streamp strm)
Packit Service 4a2782
{
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
    if (strm == Z_NULL ||
Packit Service 4a2782
        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
Packit Service 4a2782
        return 1;
Packit Service 4a2782
    s = strm->state;
Packit Service 4a2782
    if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
Packit Service 4a2782
#ifdef GZIP
Packit Service 4a2782
                                           s->status != GZIP_STATE &&
Packit Service 4a2782
#endif
Packit Service 4a2782
                                           s->status != EXTRA_STATE &&
Packit Service 4a2782
                                           s->status != NAME_STATE &&
Packit Service 4a2782
                                           s->status != COMMENT_STATE &&
Packit Service 4a2782
                                           s->status != HCRC_STATE &&
Packit Service 4a2782
                                           s->status != BUSY_STATE &&
Packit Service 4a2782
                                           s->status != FINISH_STATE))
Packit Service 4a2782
        return 1;
Packit Service 4a2782
    return 0;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateSetDictionary (
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    const Bytef *dictionary,
Packit Service 4a2782
    uInt  dictLength)
Packit Service 4a2782
{
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
    uInt str, n;
Packit Service 4a2782
    int wrap;
Packit Service 4a2782
    unsigned avail;
Packit Service 4a2782
    z_const unsigned char *next;
Packit Service 4a2782
Packit Service 4a2782
    if (deflateStateCheck(strm) || dictionary == Z_NULL)
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
    s = strm->state;
Packit Service 4a2782
    wrap = s->wrap;
Packit Service 4a2782
    if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
Packit Service 4a2782
    /* when using zlib wrappers, compute Adler-32 for provided dictionary */
Packit Service 4a2782
    if (wrap == 1)
Packit Service 4a2782
        strm->adler = adler32(strm->adler, dictionary, dictLength);
Packit Service 4a2782
    s->wrap = 0;                    /* avoid computing Adler-32 in read_buf */
Packit Service 4a2782
Packit Service 4a2782
    /* if dictionary would fill window, just replace the history */
Packit Service 4a2782
    if (dictLength >= s->w_size) {
Packit Service 4a2782
        if (wrap == 0) {            /* already empty otherwise */
Packit Service 4a2782
            CLEAR_HASH(s);
Packit Service 4a2782
            s->strstart = 0;
Packit Service 4a2782
            s->block_start = 0L;
Packit Service 4a2782
            s->insert = 0;
Packit Service 4a2782
        }
Packit Service 4a2782
        dictionary += dictLength - s->w_size;  /* use the tail */
Packit Service 4a2782
        dictLength = s->w_size;
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    /* insert dictionary into window and hash */
Packit Service 4a2782
    avail = strm->avail_in;
Packit Service 4a2782
    next = strm->next_in;
Packit Service 4a2782
    strm->avail_in = dictLength;
Packit Service 4a2782
    strm->next_in = (z_const Bytef *)dictionary;
Packit Service 4a2782
    fill_window(s);
Packit Service 4a2782
    while (s->lookahead >= MIN_MATCH) {
Packit Service 4a2782
        str = s->strstart;
Packit Service 4a2782
        n = s->lookahead - (MIN_MATCH-1);
Packit Service 4a2782
        do {
Packit Service 4a2782
            UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
Packit Service 4a2782
#ifndef FASTEST
Packit Service 4a2782
            s->prev[str & s->w_mask] = s->head[s->ins_h];
Packit Service 4a2782
#endif
Packit Service 4a2782
            s->head[s->ins_h] = (Pos)str;
Packit Service 4a2782
            str++;
Packit Service 4a2782
        } while (--n);
Packit Service 4a2782
        s->strstart = str;
Packit Service 4a2782
        s->lookahead = MIN_MATCH-1;
Packit Service 4a2782
        fill_window(s);
Packit Service 4a2782
    }
Packit Service 4a2782
    s->strstart += s->lookahead;
Packit Service 4a2782
    s->block_start = (long)s->strstart;
Packit Service 4a2782
    s->insert = s->lookahead;
Packit Service 4a2782
    s->lookahead = 0;
Packit Service 4a2782
    s->match_length = s->prev_length = MIN_MATCH-1;
Packit Service 4a2782
    s->match_available = 0;
Packit Service 4a2782
    strm->next_in = next;
Packit Service 4a2782
    strm->avail_in = avail;
Packit Service 4a2782
    s->wrap = wrap;
Packit Service 4a2782
    return Z_OK;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateGetDictionary (
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    Bytef *dictionary,
Packit Service 4a2782
    uInt  *dictLength)
Packit Service 4a2782
{
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
    uInt len;
Packit Service 4a2782
Packit Service 4a2782
    if (deflateStateCheck(strm))
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
    s = strm->state;
Packit Service 4a2782
    len = s->strstart + s->lookahead;
Packit Service 4a2782
    if (len > s->w_size)
Packit Service 4a2782
        len = s->w_size;
Packit Service 4a2782
    if (dictionary != Z_NULL && len)
Packit Service 4a2782
        zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
Packit Service 4a2782
    if (dictLength != Z_NULL)
Packit Service 4a2782
        *dictLength = len;
Packit Service 4a2782
    return Z_OK;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateResetKeep (
Packit Service 4a2782
    z_streamp strm)
Packit Service 4a2782
{
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
Packit Service 4a2782
    if (deflateStateCheck(strm)) {
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    strm->total_in = strm->total_out = 0;
Packit Service 4a2782
    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
Packit Service 4a2782
    strm->data_type = Z_UNKNOWN;
Packit Service 4a2782
Packit Service 4a2782
    s = (deflate_state *)strm->state;
Packit Service 4a2782
    s->pending = 0;
Packit Service 4a2782
    s->pending_out = s->pending_buf;
Packit Service 4a2782
Packit Service 4a2782
    if (s->wrap < 0) {
Packit Service 4a2782
        s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
Packit Service 4a2782
    }
Packit Service 4a2782
    s->status =
Packit Service 4a2782
#ifdef GZIP
Packit Service 4a2782
        s->wrap == 2 ? GZIP_STATE :
Packit Service 4a2782
#endif
Packit Service 4a2782
        s->wrap ? INIT_STATE : BUSY_STATE;
Packit Service 4a2782
    strm->adler =
Packit Service 4a2782
#ifdef GZIP
Packit Service 4a2782
        s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
Packit Service 4a2782
#endif
Packit Service 4a2782
        adler32(0L, Z_NULL, 0);
Packit Service 4a2782
    s->last_flush = Z_NO_FLUSH;
Packit Service 4a2782
Packit Service 4a2782
    _tr_init(s);
Packit Service 4a2782
Packit Service 4a2782
    return Z_OK;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateReset (
Packit Service 4a2782
    z_streamp strm)
Packit Service 4a2782
{
Packit Service 4a2782
    int ret;
Packit Service 4a2782
Packit Service 4a2782
    ret = deflateResetKeep(strm);
Packit Service 4a2782
    if (ret == Z_OK)
Packit Service 4a2782
        lm_init(strm->state);
Packit Service 4a2782
    return ret;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateSetHeader (
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    gz_headerp head)
Packit Service 4a2782
{
Packit Service 4a2782
    if (deflateStateCheck(strm) || strm->state->wrap != 2)
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
    strm->state->gzhead = head;
Packit Service 4a2782
    return Z_OK;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflatePending (
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    unsigned *pending,
Packit Service 4a2782
    int *bits)
Packit Service 4a2782
{
Packit Service 4a2782
    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
Packit Service 4a2782
    if (pending != Z_NULL)
Packit Service 4a2782
        *pending = strm->state->pending;
Packit Service 4a2782
    if (bits != Z_NULL)
Packit Service 4a2782
        *bits = strm->state->bi_valid;
Packit Service 4a2782
    return Z_OK;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflatePrime (
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    int bits,
Packit Service 4a2782
    int value)
Packit Service 4a2782
{
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
    int put;
Packit Service 4a2782
Packit Service 4a2782
    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
Packit Service 4a2782
    s = strm->state;
Packit Service 4a2782
    if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
Packit Service 4a2782
        return Z_BUF_ERROR;
Packit Service 4a2782
    do {
Packit Service 4a2782
        put = Buf_size - s->bi_valid;
Packit Service 4a2782
        if (put > bits)
Packit Service 4a2782
            put = bits;
Packit Service 4a2782
        s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
Packit Service 4a2782
        s->bi_valid += put;
Packit Service 4a2782
        _tr_flush_bits(s);
Packit Service 4a2782
        value >>= put;
Packit Service 4a2782
        bits -= put;
Packit Service 4a2782
    } while (bits);
Packit Service 4a2782
    return Z_OK;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateParams(
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    int level,
Packit Service 4a2782
    int strategy)
Packit Service 4a2782
{
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
    compress_func func;
Packit Service 4a2782
Packit Service 4a2782
    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
Packit Service 4a2782
    s = strm->state;
Packit Service 4a2782
Packit Service 4a2782
#ifdef FASTEST
Packit Service 4a2782
    if (level != 0) level = 1;
Packit Service 4a2782
#else
Packit Service 4a2782
    if (level == Z_DEFAULT_COMPRESSION) level = 6;
Packit Service 4a2782
#endif
Packit Service 4a2782
    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
    }
Packit Service 4a2782
    func = configuration_table[s->level].func;
Packit Service 4a2782
Packit Service 4a2782
    if ((strategy != s->strategy || func != configuration_table[level].func) &&
Packit Service 4a2782
        s->high_water) {
Packit Service 4a2782
        /* Flush the last buffer: */
Packit Service 4a2782
        int err = deflate(strm, Z_BLOCK);
Packit Service 4a2782
        if (err == Z_STREAM_ERROR)
Packit Service 4a2782
            return err;
Packit Service 4a2782
        if (strm->avail_out == 0)
Packit Service 4a2782
            return Z_BUF_ERROR;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->level != level) {
Packit Service 4a2782
        if (s->level == 0 && s->matches != 0) {
Packit Service 4a2782
            if (s->matches == 1)
Packit Service 4a2782
                slide_hash(s);
Packit Service 4a2782
            else
Packit Service 4a2782
                CLEAR_HASH(s);
Packit Service 4a2782
            s->matches = 0;
Packit Service 4a2782
        }
Packit Service 4a2782
        s->level = level;
Packit Service 4a2782
        s->max_lazy_match   = configuration_table[level].max_lazy;
Packit Service 4a2782
        s->good_match       = configuration_table[level].good_length;
Packit Service 4a2782
        s->nice_match       = configuration_table[level].nice_length;
Packit Service 4a2782
        s->max_chain_length = configuration_table[level].max_chain;
Packit Service 4a2782
    }
Packit Service 4a2782
    s->strategy = strategy;
Packit Service 4a2782
    return Z_OK;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateTune(
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    int good_length,
Packit Service 4a2782
    int max_lazy,
Packit Service 4a2782
    int nice_length,
Packit Service 4a2782
    int max_chain)
Packit Service 4a2782
{
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
Packit Service 4a2782
    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
Packit Service 4a2782
    s = strm->state;
Packit Service 4a2782
    s->good_match = (uInt)good_length;
Packit Service 4a2782
    s->max_lazy_match = (uInt)max_lazy;
Packit Service 4a2782
    s->nice_match = nice_length;
Packit Service 4a2782
    s->max_chain_length = (uInt)max_chain;
Packit Service 4a2782
    return Z_OK;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* =========================================================================
Packit Service 4a2782
 * For the default windowBits of 15 and memLevel of 8, this function returns
Packit Service 4a2782
 * a close to exact, as well as small, upper bound on the compressed size.
Packit Service 4a2782
 * They are coded as constants here for a reason--if the #define's are
Packit Service 4a2782
 * changed, then this function needs to be changed as well.  The return
Packit Service 4a2782
 * value for 15 and 8 only works for those exact settings.
Packit Service 4a2782
 *
Packit Service 4a2782
 * For any setting other than those defaults for windowBits and memLevel,
Packit Service 4a2782
 * the value returned is a conservative worst case for the maximum expansion
Packit Service 4a2782
 * resulting from using fixed blocks instead of stored blocks, which deflate
Packit Service 4a2782
 * can emit on compressed data for some combinations of the parameters.
Packit Service 4a2782
 *
Packit Service 4a2782
 * This function could be more sophisticated to provide closer upper bounds for
Packit Service 4a2782
 * every combination of windowBits and memLevel.  But even the conservative
Packit Service 4a2782
 * upper bound of about 14% expansion does not seem onerous for output buffer
Packit Service 4a2782
 * allocation.
Packit Service 4a2782
 */
Packit Service 4a2782
uLong ZEXPORT deflateBound(
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    uLong sourceLen)
Packit Service 4a2782
{
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
    uLong complen, wraplen;
Packit Service 4a2782
Packit Service 4a2782
    /* conservative upper bound for compressed data */
Packit Service 4a2782
    complen = sourceLen +
Packit Service 4a2782
              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
Packit Service 4a2782
Packit Service 4a2782
    /* if can't get parameters, return conservative bound plus zlib wrapper */
Packit Service 4a2782
    if (deflateStateCheck(strm))
Packit Service 4a2782
        return complen + 6;
Packit Service 4a2782
Packit Service 4a2782
    /* compute wrapper length */
Packit Service 4a2782
    s = strm->state;
Packit Service 4a2782
    switch (s->wrap) {
Packit Service 4a2782
    case 0:                                 /* raw deflate */
Packit Service 4a2782
        wraplen = 0;
Packit Service 4a2782
        break;
Packit Service 4a2782
    case 1:                                 /* zlib wrapper */
Packit Service 4a2782
        wraplen = 6 + (s->strstart ? 4 : 0);
Packit Service 4a2782
        break;
Packit Service 4a2782
#ifdef GZIP
Packit Service 4a2782
    case 2:                                 /* gzip wrapper */
Packit Service 4a2782
        wraplen = 18;
Packit Service 4a2782
        if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
Packit Service 4a2782
            Bytef *str;
Packit Service 4a2782
            if (s->gzhead->extra != Z_NULL)
Packit Service 4a2782
                wraplen += 2 + s->gzhead->extra_len;
Packit Service 4a2782
            str = s->gzhead->name;
Packit Service 4a2782
            if (str != Z_NULL)
Packit Service 4a2782
                do {
Packit Service 4a2782
                    wraplen++;
Packit Service 4a2782
                } while (*str++);
Packit Service 4a2782
            str = s->gzhead->comment;
Packit Service 4a2782
            if (str != Z_NULL)
Packit Service 4a2782
                do {
Packit Service 4a2782
                    wraplen++;
Packit Service 4a2782
                } while (*str++);
Packit Service 4a2782
            if (s->gzhead->hcrc)
Packit Service 4a2782
                wraplen += 2;
Packit Service 4a2782
        }
Packit Service 4a2782
        break;
Packit Service 4a2782
#endif
Packit Service 4a2782
    default:                                /* for compiler happiness */
Packit Service 4a2782
        wraplen = 6;
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    /* if not default parameters, return conservative bound */
Packit Service 4a2782
    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
Packit Service 4a2782
        return complen + wraplen;
Packit Service 4a2782
Packit Service 4a2782
    /* default settings: return tight bound for that case */
Packit Service 4a2782
    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
Packit Service 4a2782
           (sourceLen >> 25) + 13 - 6 + wraplen;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* =========================================================================
Packit Service 4a2782
 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
Packit Service 4a2782
 * IN assertion: the stream state is correct and there is enough room in
Packit Service 4a2782
 * pending_buf.
Packit Service 4a2782
 */
Packit Service 4a2782
local void putShortMSB (
Packit Service 4a2782
    deflate_state *s,
Packit Service 4a2782
    uInt b)
Packit Service 4a2782
{
Packit Service 4a2782
    put_byte(s, (Byte)(b >> 8));
Packit Service 4a2782
    put_byte(s, (Byte)(b & 0xff));
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* =========================================================================
Packit Service 4a2782
 * Flush as much pending output as possible. All deflate() output, except for
Packit Service 4a2782
 * some deflate_stored() output, goes through this function so some
Packit Service 4a2782
 * applications may wish to modify it to avoid allocating a large
Packit Service 4a2782
 * strm->next_out buffer and copying into it. (See also read_buf()).
Packit Service 4a2782
 */
Packit Service 4a2782
local void flush_pending(
Packit Service 4a2782
    z_streamp strm)
Packit Service 4a2782
{
Packit Service 4a2782
    unsigned len;
Packit Service 4a2782
    deflate_state *s = strm->state;
Packit Service 4a2782
Packit Service 4a2782
    _tr_flush_bits(s);
Packit Service 4a2782
    len = s->pending;
Packit Service 4a2782
    if (len > strm->avail_out) len = strm->avail_out;
Packit Service 4a2782
    if (len == 0) return;
Packit Service 4a2782
Packit Service 4a2782
    zmemcpy(strm->next_out, s->pending_out, len);
Packit Service 4a2782
    strm->next_out  += len;
Packit Service 4a2782
    s->pending_out  += len;
Packit Service 4a2782
    strm->total_out += len;
Packit Service 4a2782
    strm->avail_out -= len;
Packit Service 4a2782
    s->pending      -= len;
Packit Service 4a2782
    if (s->pending == 0) {
Packit Service 4a2782
        s->pending_out = s->pending_buf;
Packit Service 4a2782
    }
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
Packit Service 4a2782
 */
Packit Service 4a2782
#define HCRC_UPDATE(beg) \
Packit Service 4a2782
    do { \
Packit Service 4a2782
        if (s->gzhead->hcrc && s->pending > (beg)) \
Packit Service 4a2782
            strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
Packit Service 4a2782
                                s->pending - (beg)); \
Packit Service 4a2782
    } while (0)
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflate (
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    int flush)
Packit Service 4a2782
{
Packit Service 4a2782
    int old_flush; /* value of flush param for previous deflate call */
Packit Service 4a2782
    deflate_state *s;
Packit Service 4a2782
Packit Service 4a2782
    if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
    }
Packit Service 4a2782
    s = strm->state;
Packit Service 4a2782
Packit Service 4a2782
    if (strm->next_out == Z_NULL ||
Packit Service 4a2782
        (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
Packit Service 4a2782
        (s->status == FINISH_STATE && flush != Z_FINISH)) {
Packit Service 4a2782
        ERR_RETURN(strm, Z_STREAM_ERROR);
Packit Service 4a2782
    }
Packit Service 4a2782
    if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
Packit Service 4a2782
Packit Service 4a2782
    old_flush = s->last_flush;
Packit Service 4a2782
    s->last_flush = flush;
Packit Service 4a2782
Packit Service 4a2782
    /* Flush as much pending output as possible */
Packit Service 4a2782
    if (s->pending != 0) {
Packit Service 4a2782
        flush_pending(strm);
Packit Service 4a2782
        if (strm->avail_out == 0) {
Packit Service 4a2782
            /* Since avail_out is 0, deflate will be called again with
Packit Service 4a2782
             * more output space, but possibly with both pending and
Packit Service 4a2782
             * avail_in equal to zero. There won't be anything to do,
Packit Service 4a2782
             * but this is not an error situation so make sure we
Packit Service 4a2782
             * return OK instead of BUF_ERROR at next call of deflate:
Packit Service 4a2782
             */
Packit Service 4a2782
            s->last_flush = -1;
Packit Service 4a2782
            return Z_OK;
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
    /* Make sure there is something to do and avoid duplicate consecutive
Packit Service 4a2782
     * flushes. For repeated and useless calls with Z_FINISH, we keep
Packit Service 4a2782
     * returning Z_STREAM_END instead of Z_BUF_ERROR.
Packit Service 4a2782
     */
Packit Service 4a2782
    } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
Packit Service 4a2782
               flush != Z_FINISH) {
Packit Service 4a2782
        ERR_RETURN(strm, Z_BUF_ERROR);
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    /* User must not provide more input after the first FINISH: */
Packit Service 4a2782
    if (s->status == FINISH_STATE && strm->avail_in != 0) {
Packit Service 4a2782
        ERR_RETURN(strm, Z_BUF_ERROR);
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    /* Write the header */
Packit Service 4a2782
    if (s->status == INIT_STATE) {
Packit Service 4a2782
        /* zlib header */
Packit Service 4a2782
        uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
Packit Service 4a2782
        uInt level_flags;
Packit Service 4a2782
Packit Service 4a2782
        if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
Packit Service 4a2782
            level_flags = 0;
Packit Service 4a2782
        else if (s->level < 6)
Packit Service 4a2782
            level_flags = 1;
Packit Service 4a2782
        else if (s->level == 6)
Packit Service 4a2782
            level_flags = 2;
Packit Service 4a2782
        else
Packit Service 4a2782
            level_flags = 3;
Packit Service 4a2782
        header |= (level_flags << 6);
Packit Service 4a2782
        if (s->strstart != 0) header |= PRESET_DICT;
Packit Service 4a2782
        header += 31 - (header % 31);
Packit Service 4a2782
Packit Service 4a2782
        putShortMSB(s, header);
Packit Service 4a2782
Packit Service 4a2782
        /* Save the adler32 of the preset dictionary: */
Packit Service 4a2782
        if (s->strstart != 0) {
Packit Service 4a2782
            putShortMSB(s, (uInt)(strm->adler >> 16));
Packit Service 4a2782
            putShortMSB(s, (uInt)(strm->adler & 0xffff));
Packit Service 4a2782
        }
Packit Service 4a2782
        strm->adler = adler32(0L, Z_NULL, 0);
Packit Service 4a2782
        s->status = BUSY_STATE;
Packit Service 4a2782
Packit Service 4a2782
        /* Compression must start with an empty pending buffer */
Packit Service 4a2782
        flush_pending(strm);
Packit Service 4a2782
        if (s->pending != 0) {
Packit Service 4a2782
            s->last_flush = -1;
Packit Service 4a2782
            return Z_OK;
Packit Service 4a2782
        }
Packit Service 4a2782
    }
Packit Service 4a2782
#ifdef GZIP
Packit Service 4a2782
    if (s->status == GZIP_STATE) {
Packit Service 4a2782
        /* gzip header */
Packit Service 4a2782
        strm->adler = crc32(0L, Z_NULL, 0);
Packit Service 4a2782
        put_byte(s, 31);
Packit Service 4a2782
        put_byte(s, 139);
Packit Service 4a2782
        put_byte(s, 8);
Packit Service 4a2782
        if (s->gzhead == Z_NULL) {
Packit Service 4a2782
            put_byte(s, 0);
Packit Service 4a2782
            put_byte(s, 0);
Packit Service 4a2782
            put_byte(s, 0);
Packit Service 4a2782
            put_byte(s, 0);
Packit Service 4a2782
            put_byte(s, 0);
Packit Service 4a2782
            put_byte(s, s->level == 9 ? 2 :
Packit Service 4a2782
                     (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
Packit Service 4a2782
                      4 : 0));
Packit Service 4a2782
            put_byte(s, OS_CODE);
Packit Service 4a2782
            s->status = BUSY_STATE;
Packit Service 4a2782
Packit Service 4a2782
            /* Compression must start with an empty pending buffer */
Packit Service 4a2782
            flush_pending(strm);
Packit Service 4a2782
            if (s->pending != 0) {
Packit Service 4a2782
                s->last_flush = -1;
Packit Service 4a2782
                return Z_OK;
Packit Service 4a2782
            }
Packit Service 4a2782
        }
Packit Service 4a2782
        else {
Packit Service 4a2782
            put_byte(s, (s->gzhead->text ? 1 : 0) +
Packit Service 4a2782
                     (s->gzhead->hcrc ? 2 : 0) +
Packit Service 4a2782
                     (s->gzhead->extra == Z_NULL ? 0 : 4) +
Packit Service 4a2782
                     (s->gzhead->name == Z_NULL ? 0 : 8) +
Packit Service 4a2782
                     (s->gzhead->comment == Z_NULL ? 0 : 16)
Packit Service 4a2782
                     );
Packit Service 4a2782
            put_byte(s, (Byte)(s->gzhead->time & 0xff));
Packit Service 4a2782
            put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
Packit Service 4a2782
            put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
Packit Service 4a2782
            put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
Packit Service 4a2782
            put_byte(s, s->level == 9 ? 2 :
Packit Service 4a2782
                     (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
Packit Service 4a2782
                      4 : 0));
Packit Service 4a2782
            put_byte(s, s->gzhead->os & 0xff);
Packit Service 4a2782
            if (s->gzhead->extra != Z_NULL) {
Packit Service 4a2782
                put_byte(s, s->gzhead->extra_len & 0xff);
Packit Service 4a2782
                put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
Packit Service 4a2782
            }
Packit Service 4a2782
            if (s->gzhead->hcrc)
Packit Service 4a2782
                strm->adler = crc32(strm->adler, s->pending_buf,
Packit Service 4a2782
                                    s->pending);
Packit Service 4a2782
            s->gzindex = 0;
Packit Service 4a2782
            s->status = EXTRA_STATE;
Packit Service 4a2782
        }
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->status == EXTRA_STATE) {
Packit Service 4a2782
        if (s->gzhead->extra != Z_NULL) {
Packit Service 4a2782
            ulg beg = s->pending;   /* start of bytes to update crc */
Packit Service 4a2782
            uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
Packit Service 4a2782
            while (s->pending + left > s->pending_buf_size) {
Packit Service 4a2782
                uInt copy = s->pending_buf_size - s->pending;
Packit Service 4a2782
                zmemcpy(s->pending_buf + s->pending,
Packit Service 4a2782
                        s->gzhead->extra + s->gzindex, copy);
Packit Service 4a2782
                s->pending = s->pending_buf_size;
Packit Service 4a2782
                HCRC_UPDATE(beg);
Packit Service 4a2782
                s->gzindex += copy;
Packit Service 4a2782
                flush_pending(strm);
Packit Service 4a2782
                if (s->pending != 0) {
Packit Service 4a2782
                    s->last_flush = -1;
Packit Service 4a2782
                    return Z_OK;
Packit Service 4a2782
                }
Packit Service 4a2782
                beg = 0;
Packit Service 4a2782
                left -= copy;
Packit Service 4a2782
            }
Packit Service 4a2782
            zmemcpy(s->pending_buf + s->pending,
Packit Service 4a2782
                    s->gzhead->extra + s->gzindex, left);
Packit Service 4a2782
            s->pending += left;
Packit Service 4a2782
            HCRC_UPDATE(beg);
Packit Service 4a2782
            s->gzindex = 0;
Packit Service 4a2782
        }
Packit Service 4a2782
        s->status = NAME_STATE;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->status == NAME_STATE) {
Packit Service 4a2782
        if (s->gzhead->name != Z_NULL) {
Packit Service 4a2782
            ulg beg = s->pending;   /* start of bytes to update crc */
Packit Service 4a2782
            int val;
Packit Service 4a2782
            do {
Packit Service 4a2782
                if (s->pending == s->pending_buf_size) {
Packit Service 4a2782
                    HCRC_UPDATE(beg);
Packit Service 4a2782
                    flush_pending(strm);
Packit Service 4a2782
                    if (s->pending != 0) {
Packit Service 4a2782
                        s->last_flush = -1;
Packit Service 4a2782
                        return Z_OK;
Packit Service 4a2782
                    }
Packit Service 4a2782
                    beg = 0;
Packit Service 4a2782
                }
Packit Service 4a2782
                val = s->gzhead->name[s->gzindex++];
Packit Service 4a2782
                put_byte(s, val);
Packit Service 4a2782
            } while (val != 0);
Packit Service 4a2782
            HCRC_UPDATE(beg);
Packit Service 4a2782
            s->gzindex = 0;
Packit Service 4a2782
        }
Packit Service 4a2782
        s->status = COMMENT_STATE;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->status == COMMENT_STATE) {
Packit Service 4a2782
        if (s->gzhead->comment != Z_NULL) {
Packit Service 4a2782
            ulg beg = s->pending;   /* start of bytes to update crc */
Packit Service 4a2782
            int val;
Packit Service 4a2782
            do {
Packit Service 4a2782
                if (s->pending == s->pending_buf_size) {
Packit Service 4a2782
                    HCRC_UPDATE(beg);
Packit Service 4a2782
                    flush_pending(strm);
Packit Service 4a2782
                    if (s->pending != 0) {
Packit Service 4a2782
                        s->last_flush = -1;
Packit Service 4a2782
                        return Z_OK;
Packit Service 4a2782
                    }
Packit Service 4a2782
                    beg = 0;
Packit Service 4a2782
                }
Packit Service 4a2782
                val = s->gzhead->comment[s->gzindex++];
Packit Service 4a2782
                put_byte(s, val);
Packit Service 4a2782
            } while (val != 0);
Packit Service 4a2782
            HCRC_UPDATE(beg);
Packit Service 4a2782
        }
Packit Service 4a2782
        s->status = HCRC_STATE;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->status == HCRC_STATE) {
Packit Service 4a2782
        if (s->gzhead->hcrc) {
Packit Service 4a2782
            if (s->pending + 2 > s->pending_buf_size) {
Packit Service 4a2782
                flush_pending(strm);
Packit Service 4a2782
                if (s->pending != 0) {
Packit Service 4a2782
                    s->last_flush = -1;
Packit Service 4a2782
                    return Z_OK;
Packit Service 4a2782
                }
Packit Service 4a2782
            }
Packit Service 4a2782
            put_byte(s, (Byte)(strm->adler & 0xff));
Packit Service 4a2782
            put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
Packit Service 4a2782
            strm->adler = crc32(0L, Z_NULL, 0);
Packit Service 4a2782
        }
Packit Service 4a2782
        s->status = BUSY_STATE;
Packit Service 4a2782
Packit Service 4a2782
        /* Compression must start with an empty pending buffer */
Packit Service 4a2782
        flush_pending(strm);
Packit Service 4a2782
        if (s->pending != 0) {
Packit Service 4a2782
            s->last_flush = -1;
Packit Service 4a2782
            return Z_OK;
Packit Service 4a2782
        }
Packit Service 4a2782
    }
Packit Service 4a2782
#endif
Packit Service 4a2782
Packit Service 4a2782
    /* Start a new block or continue the current one.
Packit Service 4a2782
     */
Packit Service 4a2782
    if (strm->avail_in != 0 || s->lookahead != 0 ||
Packit Service 4a2782
        (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
Packit Service 4a2782
        block_state bstate;
Packit Service 4a2782
Packit Service 4a2782
        bstate = s->level == 0 ? deflate_stored(s, flush) :
Packit Service 4a2782
                 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
Packit Service 4a2782
                 s->strategy == Z_RLE ? deflate_rle(s, flush) :
Packit Service 4a2782
                 (*(configuration_table[s->level].func))(s, flush);
Packit Service 4a2782
Packit Service 4a2782
        if (bstate == finish_started || bstate == finish_done) {
Packit Service 4a2782
            s->status = FINISH_STATE;
Packit Service 4a2782
        }
Packit Service 4a2782
        if (bstate == need_more || bstate == finish_started) {
Packit Service 4a2782
            if (strm->avail_out == 0) {
Packit Service 4a2782
                s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
Packit Service 4a2782
            }
Packit Service 4a2782
            return Z_OK;
Packit Service 4a2782
            /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
Packit Service 4a2782
             * of deflate should use the same flush parameter to make sure
Packit Service 4a2782
             * that the flush is complete. So we don't have to output an
Packit Service 4a2782
             * empty block here, this will be done at next call. This also
Packit Service 4a2782
             * ensures that for a very small output buffer, we emit at most
Packit Service 4a2782
             * one empty block.
Packit Service 4a2782
             */
Packit Service 4a2782
        }
Packit Service 4a2782
        if (bstate == block_done) {
Packit Service 4a2782
            if (flush == Z_PARTIAL_FLUSH) {
Packit Service 4a2782
                _tr_align(s);
Packit Service 4a2782
            } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
Packit Service 4a2782
                _tr_stored_block(s, (char*)0, 0L, 0);
Packit Service 4a2782
                /* For a full flush, this empty block will be recognized
Packit Service 4a2782
                 * as a special marker by inflate_sync().
Packit Service 4a2782
                 */
Packit Service 4a2782
                if (flush == Z_FULL_FLUSH) {
Packit Service 4a2782
                    CLEAR_HASH(s);             /* forget history */
Packit Service 4a2782
                    if (s->lookahead == 0) {
Packit Service 4a2782
                        s->strstart = 0;
Packit Service 4a2782
                        s->block_start = 0L;
Packit Service 4a2782
                        s->insert = 0;
Packit Service 4a2782
                    }
Packit Service 4a2782
                }
Packit Service 4a2782
            }
Packit Service 4a2782
            flush_pending(strm);
Packit Service 4a2782
            if (strm->avail_out == 0) {
Packit Service 4a2782
              s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
Packit Service 4a2782
              return Z_OK;
Packit Service 4a2782
            }
Packit Service 4a2782
        }
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    if (flush != Z_FINISH) return Z_OK;
Packit Service 4a2782
    if (s->wrap <= 0) return Z_STREAM_END;
Packit Service 4a2782
Packit Service 4a2782
    /* Write the trailer */
Packit Service 4a2782
#ifdef GZIP
Packit Service 4a2782
    if (s->wrap == 2) {
Packit Service 4a2782
        put_byte(s, (Byte)(strm->adler & 0xff));
Packit Service 4a2782
        put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
Packit Service 4a2782
        put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
Packit Service 4a2782
        put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
Packit Service 4a2782
        put_byte(s, (Byte)(strm->total_in & 0xff));
Packit Service 4a2782
        put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
Packit Service 4a2782
        put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
Packit Service 4a2782
        put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
Packit Service 4a2782
    }
Packit Service 4a2782
    else
Packit Service 4a2782
#endif
Packit Service 4a2782
    {
Packit Service 4a2782
        putShortMSB(s, (uInt)(strm->adler >> 16));
Packit Service 4a2782
        putShortMSB(s, (uInt)(strm->adler & 0xffff));
Packit Service 4a2782
    }
Packit Service 4a2782
    flush_pending(strm);
Packit Service 4a2782
    /* If avail_out is zero, the application will call deflate again
Packit Service 4a2782
     * to flush the rest.
Packit Service 4a2782
     */
Packit Service 4a2782
    if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
Packit Service 4a2782
    return s->pending != 0 ? Z_OK : Z_STREAM_END;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ========================================================================= */
Packit Service 4a2782
int ZEXPORT deflateEnd (
Packit Service 4a2782
    z_streamp strm)
Packit Service 4a2782
{
Packit Service 4a2782
    int status;
Packit Service 4a2782
Packit Service 4a2782
    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
Packit Service 4a2782
Packit Service 4a2782
    status = strm->state->status;
Packit Service 4a2782
Packit Service 4a2782
    /* Deallocate in reverse order of allocations: */
Packit Service 4a2782
    TRY_FREE(strm, strm->state->pending_buf);
Packit Service 4a2782
    TRY_FREE(strm, strm->state->head);
Packit Service 4a2782
    TRY_FREE(strm, strm->state->prev);
Packit Service 4a2782
    TRY_FREE(strm, strm->state->window);
Packit Service 4a2782
Packit Service 4a2782
    ZFREE(strm, strm->state);
Packit Service 4a2782
    strm->state = Z_NULL;
Packit Service 4a2782
Packit Service 4a2782
    return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* =========================================================================
Packit Service 4a2782
 * Copy the source state to the destination state.
Packit Service 4a2782
 * To simplify the source, this is not supported for 16-bit MSDOS (which
Packit Service 4a2782
 * doesn't have enough memory anyway to duplicate compression states).
Packit Service 4a2782
 */
Packit Service 4a2782
int ZEXPORT deflateCopy (
Packit Service 4a2782
    z_streamp dest,
Packit Service 4a2782
    z_streamp source)
Packit Service 4a2782
{
Packit Service 4a2782
#ifdef MAXSEG_64K
Packit Service 4a2782
    return Z_STREAM_ERROR;
Packit Service 4a2782
#else
Packit Service 4a2782
    deflate_state *ds;
Packit Service 4a2782
    deflate_state *ss;
Packit Service 4a2782
    ushf *overlay;
Packit Service 4a2782
Packit Service 4a2782
Packit Service 4a2782
    if (deflateStateCheck(source) || dest == Z_NULL) {
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    ss = source->state;
Packit Service 4a2782
Packit Service 4a2782
    zmemcpy((Bytef*)dest, (Bytef*)source, sizeof(z_stream));
Packit Service 4a2782
Packit Service 4a2782
    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
Packit Service 4a2782
    if (ds == Z_NULL) return Z_MEM_ERROR;
Packit Service 4a2782
    dest->state = (struct internal_state FAR *) ds;
Packit Service 4a2782
    zmemcpy((Bytef*)ds, (Bytef*)ss, sizeof(deflate_state));
Packit Service 4a2782
    ds->strm = dest;
Packit Service 4a2782
Packit Service 4a2782
    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
Packit Service 4a2782
    ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
Packit Service 4a2782
    ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
Packit Service 4a2782
    overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
Packit Service 4a2782
    ds->pending_buf = (uchf *) overlay;
Packit Service 4a2782
Packit Service 4a2782
    if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
Packit Service 4a2782
        ds->pending_buf == Z_NULL) {
Packit Service 4a2782
        deflateEnd (dest);
Packit Service 4a2782
        return Z_MEM_ERROR;
Packit Service 4a2782
    }
Packit Service 4a2782
    /* following zmemcpy do not work for 16-bit MSDOS */
Packit Service 4a2782
    zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
Packit Service 4a2782
    zmemcpy((Bytef*)ds->prev, (Bytef*)ss->prev, ds->w_size * sizeof(Pos));
Packit Service 4a2782
    zmemcpy((Bytef*)ds->head, (Bytef*)ss->head, ds->hash_size * sizeof(Pos));
Packit Service 4a2782
    zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
Packit Service 4a2782
Packit Service 4a2782
    ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
Packit Service 4a2782
    ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
Packit Service 4a2782
    ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
Packit Service 4a2782
Packit Service 4a2782
    ds->l_desc.dyn_tree = ds->dyn_ltree;
Packit Service 4a2782
    ds->d_desc.dyn_tree = ds->dyn_dtree;
Packit Service 4a2782
    ds->bl_desc.dyn_tree = ds->bl_tree;
Packit Service 4a2782
Packit Service 4a2782
    return Z_OK;
Packit Service 4a2782
#endif /* MAXSEG_64K */
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Read a new buffer from the current input stream, update the adler32
Packit Service 4a2782
 * and total number of bytes read.  All deflate() input goes through
Packit Service 4a2782
 * this function so some applications may wish to modify it to avoid
Packit Service 4a2782
 * allocating a large strm->next_in buffer and copying from it.
Packit Service 4a2782
 * (See also flush_pending()).
Packit Service 4a2782
 */
Packit Service 4a2782
local unsigned read_buf(
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    Bytef *buf,
Packit Service 4a2782
    unsigned size)
Packit Service 4a2782
{
Packit Service 4a2782
    unsigned len = strm->avail_in;
Packit Service 4a2782
Packit Service 4a2782
    if (len > size) len = size;
Packit Service 4a2782
    if (len == 0) return 0;
Packit Service 4a2782
Packit Service 4a2782
    strm->avail_in  -= len;
Packit Service 4a2782
Packit Service 4a2782
    zmemcpy(buf, strm->next_in, len);
Packit Service 4a2782
    if (strm->state->wrap == 1) {
Packit Service 4a2782
        strm->adler = adler32(strm->adler, buf, len);
Packit Service 4a2782
    }
Packit Service 4a2782
#ifdef GZIP
Packit Service 4a2782
    else if (strm->state->wrap == 2) {
Packit Service 4a2782
        strm->adler = crc32(strm->adler, buf, len);
Packit Service 4a2782
    }
Packit Service 4a2782
#endif
Packit Service 4a2782
    strm->next_in  += len;
Packit Service 4a2782
    strm->total_in += len;
Packit Service 4a2782
Packit Service 4a2782
    return len;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Initialize the "longest match" routines for a new zlib stream
Packit Service 4a2782
 */
Packit Service 4a2782
local void lm_init (
Packit Service 4a2782
    deflate_state *s)
Packit Service 4a2782
{
Packit Service 4a2782
    s->window_size = (ulg)2L*s->w_size;
Packit Service 4a2782
Packit Service 4a2782
    CLEAR_HASH(s);
Packit Service 4a2782
Packit Service 4a2782
    /* Set the default configuration parameters:
Packit Service 4a2782
     */
Packit Service 4a2782
    s->max_lazy_match   = configuration_table[s->level].max_lazy;
Packit Service 4a2782
    s->good_match       = configuration_table[s->level].good_length;
Packit Service 4a2782
    s->nice_match       = configuration_table[s->level].nice_length;
Packit Service 4a2782
    s->max_chain_length = configuration_table[s->level].max_chain;
Packit Service 4a2782
Packit Service 4a2782
    s->strstart = 0;
Packit Service 4a2782
    s->block_start = 0L;
Packit Service 4a2782
    s->lookahead = 0;
Packit Service 4a2782
    s->insert = 0;
Packit Service 4a2782
    s->match_length = s->prev_length = MIN_MATCH-1;
Packit Service 4a2782
    s->match_available = 0;
Packit Service 4a2782
    s->ins_h = 0;
Packit Service 4a2782
#ifndef FASTEST
Packit Service 4a2782
#ifdef ASMV
Packit Service 4a2782
    match_init(); /* initialize the asm code */
Packit Service 4a2782
#endif
Packit Service 4a2782
#endif
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
#ifndef FASTEST
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Set match_start to the longest match starting at the given string and
Packit Service 4a2782
 * return its length. Matches shorter or equal to prev_length are discarded,
Packit Service 4a2782
 * in which case the result is equal to prev_length and match_start is
Packit Service 4a2782
 * garbage.
Packit Service 4a2782
 * IN assertions: cur_match is the head of the hash chain for the current
Packit Service 4a2782
 *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
Packit Service 4a2782
 * OUT assertion: the match length is not greater than s->lookahead.
Packit Service 4a2782
 */
Packit Service 4a2782
#ifndef ASMV
Packit Service 4a2782
/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
Packit Service 4a2782
 * match.S. The code will be functionally equivalent.
Packit Service 4a2782
 */
Packit Service 4a2782
local uInt longest_match(
Packit Service 4a2782
    deflate_state *s,
Packit Service 4a2782
    IPos cur_match)
Packit Service 4a2782
{
Packit Service 4a2782
    unsigned chain_length = s->max_chain_length;/* max hash chain length */
Packit Service 4a2782
    register Bytef *scan = s->window + s->strstart; /* current string */
Packit Service 4a2782
    register Bytef *match;                      /* matched string */
Packit Service 4a2782
    register int len;                           /* length of current match */
Packit Service 4a2782
    int best_len = (int)s->prev_length;         /* best match length so far */
Packit Service 4a2782
    int nice_match = s->nice_match;             /* stop if match long enough */
Packit Service 4a2782
    IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
Packit Service 4a2782
        s->strstart - (IPos)MAX_DIST(s) : NIL;
Packit Service 4a2782
    /* Stop when cur_match becomes <= limit. To simplify the code,
Packit Service 4a2782
     * we prevent matches with the string of window index 0.
Packit Service 4a2782
     */
Packit Service 4a2782
    Posf *prev = s->prev;
Packit Service 4a2782
    uInt wmask = s->w_mask;
Packit Service 4a2782
Packit Service 4a2782
#ifdef UNALIGNED_OK
Packit Service 4a2782
    /* Compare two bytes at a time. Note: this is not always beneficial.
Packit Service 4a2782
     * Try with and without -DUNALIGNED_OK to check.
Packit Service 4a2782
     */
Packit Service 4a2782
    register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
Packit Service 4a2782
    register ush scan_start = *(ushf*)scan;
Packit Service 4a2782
    register ush scan_end   = *(ushf*)(scan+best_len-1);
Packit Service 4a2782
#else
Packit Service 4a2782
    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
Packit Service 4a2782
    register Byte scan_end1  = scan[best_len-1];
Packit Service 4a2782
    register Byte scan_end   = scan[best_len];
Packit Service 4a2782
#endif
Packit Service 4a2782
Packit Service 4a2782
    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
Packit Service 4a2782
     * It is easy to get rid of this optimization if necessary.
Packit Service 4a2782
     */
Packit Service 4a2782
    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
Packit Service 4a2782
Packit Service 4a2782
    /* Do not waste too much time if we already have a good match: */
Packit Service 4a2782
    if (s->prev_length >= s->good_match) {
Packit Service 4a2782
        chain_length >>= 2;
Packit Service 4a2782
    }
Packit Service 4a2782
    /* Do not look for matches beyond the end of the input. This is necessary
Packit Service 4a2782
     * to make deflate deterministic.
Packit Service 4a2782
     */
Packit Service 4a2782
    if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
Packit Service 4a2782
Packit Service 4a2782
    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
Packit Service 4a2782
Packit Service 4a2782
    do {
Packit Service 4a2782
        Assert(cur_match < s->strstart, "no future");
Packit Service 4a2782
        match = s->window + cur_match;
Packit Service 4a2782
Packit Service 4a2782
        /* Skip to next match if the match length cannot increase
Packit Service 4a2782
         * or if the match length is less than 2.  Note that the checks below
Packit Service 4a2782
         * for insufficient lookahead only occur occasionally for performance
Packit Service 4a2782
         * reasons.  Therefore uninitialized memory will be accessed, and
Packit Service 4a2782
         * conditional jumps will be made that depend on those values.
Packit Service 4a2782
         * However the length of the match is limited to the lookahead, so
Packit Service 4a2782
         * the output of deflate is not affected by the uninitialized values.
Packit Service 4a2782
         */
Packit Service 4a2782
#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
Packit Service 4a2782
        /* This code assumes sizeof(unsigned short) == 2. Do not use
Packit Service 4a2782
         * UNALIGNED_OK if your compiler uses a different size.
Packit Service 4a2782
         */
Packit Service 4a2782
        if (*(ushf*)(match+best_len-1) != scan_end ||
Packit Service 4a2782
            *(ushf*)match != scan_start) continue;
Packit Service 4a2782
Packit Service 4a2782
        /* It is not necessary to compare scan[2] and match[2] since they are
Packit Service 4a2782
         * always equal when the other bytes match, given that the hash keys
Packit Service 4a2782
         * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
Packit Service 4a2782
         * strstart+3, +5, ... up to strstart+257. We check for insufficient
Packit Service 4a2782
         * lookahead only every 4th comparison; the 128th check will be made
Packit Service 4a2782
         * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
Packit Service 4a2782
         * necessary to put more guard bytes at the end of the window, or
Packit Service 4a2782
         * to check more often for insufficient lookahead.
Packit Service 4a2782
         */
Packit Service 4a2782
        Assert(scan[2] == match[2], "scan[2]?");
Packit Service 4a2782
        scan++, match++;
Packit Service 4a2782
        do {
Packit Service 4a2782
        } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
Packit Service 4a2782
                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
Packit Service 4a2782
                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
Packit Service 4a2782
                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
Packit Service 4a2782
                 scan < strend);
Packit Service 4a2782
        /* The funny "do {}" generates better code on most compilers */
Packit Service 4a2782
Packit Service 4a2782
        /* Here, scan <= window+strstart+257 */
Packit Service 4a2782
        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
Packit Service 4a2782
        if (*scan == *match) scan++;
Packit Service 4a2782
Packit Service 4a2782
        len = (MAX_MATCH - 1) - (int)(strend-scan);
Packit Service 4a2782
        scan = strend - (MAX_MATCH-1);
Packit Service 4a2782
Packit Service 4a2782
#else /* UNALIGNED_OK */
Packit Service 4a2782
Packit Service 4a2782
        if (match[best_len]   != scan_end  ||
Packit Service 4a2782
            match[best_len-1] != scan_end1 ||
Packit Service 4a2782
            *match            != *scan     ||
Packit Service 4a2782
            *++match          != scan[1])      continue;
Packit Service 4a2782
Packit Service 4a2782
        /* The check at best_len-1 can be removed because it will be made
Packit Service 4a2782
         * again later. (This heuristic is not always a win.)
Packit Service 4a2782
         * It is not necessary to compare scan[2] and match[2] since they
Packit Service 4a2782
         * are always equal when the other bytes match, given that
Packit Service 4a2782
         * the hash keys are equal and that HASH_BITS >= 8.
Packit Service 4a2782
         */
Packit Service 4a2782
        scan += 2, match++;
Packit Service 4a2782
        Assert(*scan == *match, "match[2]?");
Packit Service 4a2782
Packit Service 4a2782
        /* We check for insufficient lookahead only every 8th comparison;
Packit Service 4a2782
         * the 256th check will be made at strstart+258.
Packit Service 4a2782
         */
Packit Service 4a2782
        do {
Packit Service 4a2782
        } while (*++scan == *++match && *++scan == *++match &&
Packit Service 4a2782
                 *++scan == *++match && *++scan == *++match &&
Packit Service 4a2782
                 *++scan == *++match && *++scan == *++match &&
Packit Service 4a2782
                 *++scan == *++match && *++scan == *++match &&
Packit Service 4a2782
                 scan < strend);
Packit Service 4a2782
Packit Service 4a2782
        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
Packit Service 4a2782
Packit Service 4a2782
        len = MAX_MATCH - (int)(strend - scan);
Packit Service 4a2782
        scan = strend - MAX_MATCH;
Packit Service 4a2782
Packit Service 4a2782
#endif /* UNALIGNED_OK */
Packit Service 4a2782
Packit Service 4a2782
        if (len > best_len) {
Packit Service 4a2782
            s->match_start = cur_match;
Packit Service 4a2782
            best_len = len;
Packit Service 4a2782
            if (len >= nice_match) break;
Packit Service 4a2782
#ifdef UNALIGNED_OK
Packit Service 4a2782
            scan_end = *(ushf*)(scan+best_len-1);
Packit Service 4a2782
#else
Packit Service 4a2782
            scan_end1  = scan[best_len-1];
Packit Service 4a2782
            scan_end   = scan[best_len];
Packit Service 4a2782
#endif
Packit Service 4a2782
        }
Packit Service 4a2782
    } while ((cur_match = prev[cur_match & wmask]) > limit
Packit Service 4a2782
             && --chain_length != 0);
Packit Service 4a2782
Packit Service 4a2782
    if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
Packit Service 4a2782
    return s->lookahead;
Packit Service 4a2782
}
Packit Service 4a2782
#endif /* ASMV */
Packit Service 4a2782
Packit Service 4a2782
#else /* FASTEST */
Packit Service 4a2782
Packit Service 4a2782
/* ---------------------------------------------------------------------------
Packit Service 4a2782
 * Optimized version for FASTEST only
Packit Service 4a2782
 */
Packit Service 4a2782
local uInt longest_match(
Packit Service 4a2782
    deflate_state *s,
Packit Service 4a2782
    IPos cur_match)
Packit Service 4a2782
{
Packit Service 4a2782
    register Bytef *scan = s->window + s->strstart; /* current string */
Packit Service 4a2782
    register Bytef *match;                       /* matched string */
Packit Service 4a2782
    register int len;                           /* length of current match */
Packit Service 4a2782
    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
Packit Service 4a2782
Packit Service 4a2782
    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
Packit Service 4a2782
     * It is easy to get rid of this optimization if necessary.
Packit Service 4a2782
     */
Packit Service 4a2782
    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
Packit Service 4a2782
Packit Service 4a2782
    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
Packit Service 4a2782
Packit Service 4a2782
    Assert(cur_match < s->strstart, "no future");
Packit Service 4a2782
Packit Service 4a2782
    match = s->window + cur_match;
Packit Service 4a2782
Packit Service 4a2782
    /* Return failure if the match length is less than 2:
Packit Service 4a2782
     */
Packit Service 4a2782
    if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
Packit Service 4a2782
Packit Service 4a2782
    /* The check at best_len-1 can be removed because it will be made
Packit Service 4a2782
     * again later. (This heuristic is not always a win.)
Packit Service 4a2782
     * It is not necessary to compare scan[2] and match[2] since they
Packit Service 4a2782
     * are always equal when the other bytes match, given that
Packit Service 4a2782
     * the hash keys are equal and that HASH_BITS >= 8.
Packit Service 4a2782
     */
Packit Service 4a2782
    scan += 2, match += 2;
Packit Service 4a2782
    Assert(*scan == *match, "match[2]?");
Packit Service 4a2782
Packit Service 4a2782
    /* We check for insufficient lookahead only every 8th comparison;
Packit Service 4a2782
     * the 256th check will be made at strstart+258.
Packit Service 4a2782
     */
Packit Service 4a2782
    do {
Packit Service 4a2782
    } while (*++scan == *++match && *++scan == *++match &&
Packit Service 4a2782
             *++scan == *++match && *++scan == *++match &&
Packit Service 4a2782
             *++scan == *++match && *++scan == *++match &&
Packit Service 4a2782
             *++scan == *++match && *++scan == *++match &&
Packit Service 4a2782
             scan < strend);
Packit Service 4a2782
Packit Service 4a2782
    Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
Packit Service 4a2782
Packit Service 4a2782
    len = MAX_MATCH - (int)(strend - scan);
Packit Service 4a2782
Packit Service 4a2782
    if (len < MIN_MATCH) return MIN_MATCH - 1;
Packit Service 4a2782
Packit Service 4a2782
    s->match_start = cur_match;
Packit Service 4a2782
    return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
#endif /* FASTEST */
Packit Service 4a2782
Packit Service 4a2782
#ifdef ZLIB_DEBUG
Packit Service 4a2782
Packit Service 4a2782
#define EQUAL 0
Packit Service 4a2782
/* result of memcmp for equal strings */
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Check that the match at match_start is indeed a match.
Packit Service 4a2782
 */
Packit Service 4a2782
local void check_match(
Packit Service 4a2782
    deflate_state *s,
Packit Service 4a2782
    IPos start,
Packit Service 4a2782
    IPos match,
Packit Service 4a2782
    int length)
Packit Service 4a2782
{
Packit Service 4a2782
    /* check that the match is indeed a match */
Packit Service 4a2782
    if (zmemcmp(s->window + match,
Packit Service 4a2782
                s->window + start, length) != EQUAL) {
Packit Service 4a2782
        fprintf(stderr, " start %u, match %u, length %d\n",
Packit Service 4a2782
                start, match, length);
Packit Service 4a2782
        do {
Packit Service 4a2782
            fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
Packit Service 4a2782
        } while (--length != 0);
Packit Service 4a2782
        z_error("invalid match");
Packit Service 4a2782
    }
Packit Service 4a2782
    if (z_verbose > 1) {
Packit Service 4a2782
        fprintf(stderr,"\\[%d,%d]", start-match, length);
Packit Service 4a2782
        do { putc(s->window[start++], stderr); } while (--length != 0);
Packit Service 4a2782
    }
Packit Service 4a2782
}
Packit Service 4a2782
#else
Packit Service 4a2782
#  define check_match(s, start, match, length)
Packit Service 4a2782
#endif /* ZLIB_DEBUG */
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Fill the window when the lookahead becomes insufficient.
Packit Service 4a2782
 * Updates strstart and lookahead.
Packit Service 4a2782
 *
Packit Service 4a2782
 * IN assertion: lookahead < MIN_LOOKAHEAD
Packit Service 4a2782
 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
Packit Service 4a2782
 *    At least one byte has been read, or avail_in == 0; reads are
Packit Service 4a2782
 *    performed for at least two bytes (required for the zip translate_eol
Packit Service 4a2782
 *    option -- not supported here).
Packit Service 4a2782
 */
Packit Service 4a2782
local void fill_window(
Packit Service 4a2782
    deflate_state *s)
Packit Service 4a2782
{
Packit Service 4a2782
    unsigned n;
Packit Service 4a2782
    unsigned more;    /* Amount of free space at the end of the window. */
Packit Service 4a2782
    uInt wsize = s->w_size;
Packit Service 4a2782
Packit Service 4a2782
    Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
Packit Service 4a2782
Packit Service 4a2782
    do {
Packit Service 4a2782
        more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
Packit Service 4a2782
Packit Service 4a2782
        /* Deal with !@#$% 64K limit: */
Packit Service 4a2782
        if (sizeof(int) <= 2) {
Packit Service 4a2782
            if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
Packit Service 4a2782
                more = wsize;
Packit Service 4a2782
Packit Service 4a2782
            } else if (more == (unsigned)(-1)) {
Packit Service 4a2782
                /* Very unlikely, but possible on 16 bit machine if
Packit Service 4a2782
                 * strstart == 0 && lookahead == 1 (input done a byte at time)
Packit Service 4a2782
                 */
Packit Service 4a2782
                more--;
Packit Service 4a2782
            }
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
        /* If the window is almost full and there is insufficient lookahead,
Packit Service 4a2782
         * move the upper half to the lower one to make room in the upper half.
Packit Service 4a2782
         */
Packit Service 4a2782
        if (s->strstart >= wsize+MAX_DIST(s)) {
Packit Service 4a2782
Packit Service 4a2782
            zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more);
Packit Service 4a2782
            s->match_start -= wsize;
Packit Service 4a2782
            s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
Packit Service 4a2782
            s->block_start -= (long) wsize;
Packit Service 4a2782
            slide_hash(s);
Packit Service 4a2782
            more += wsize;
Packit Service 4a2782
        }
Packit Service 4a2782
        if (s->strm->avail_in == 0) break;
Packit Service 4a2782
Packit Service 4a2782
        /* If there was no sliding:
Packit Service 4a2782
         *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
Packit Service 4a2782
         *    more == window_size - lookahead - strstart
Packit Service 4a2782
         * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
Packit Service 4a2782
         * => more >= window_size - 2*WSIZE + 2
Packit Service 4a2782
         * In the BIG_MEM or MMAP case (not yet supported),
Packit Service 4a2782
         *   window_size == input_size + MIN_LOOKAHEAD  &&
Packit Service 4a2782
         *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
Packit Service 4a2782
         * Otherwise, window_size == 2*WSIZE so more >= 2.
Packit Service 4a2782
         * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
Packit Service 4a2782
         */
Packit Service 4a2782
        Assert(more >= 2, "more < 2");
Packit Service 4a2782
Packit Service 4a2782
        n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
Packit Service 4a2782
        s->lookahead += n;
Packit Service 4a2782
Packit Service 4a2782
        /* Initialize the hash value now that we have some input: */
Packit Service 4a2782
        if (s->lookahead + s->insert >= MIN_MATCH) {
Packit Service 4a2782
            uInt str = s->strstart - s->insert;
Packit Service 4a2782
            s->ins_h = s->window[str];
Packit Service 4a2782
            UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
Packit Service 4a2782
#if MIN_MATCH != 3
Packit Service 4a2782
            Call UPDATE_HASH() MIN_MATCH-3 more times
Packit Service 4a2782
#endif
Packit Service 4a2782
            while (s->insert) {
Packit Service 4a2782
                UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
Packit Service 4a2782
#ifndef FASTEST
Packit Service 4a2782
                s->prev[str & s->w_mask] = s->head[s->ins_h];
Packit Service 4a2782
#endif
Packit Service 4a2782
                s->head[s->ins_h] = (Pos)str;
Packit Service 4a2782
                str++;
Packit Service 4a2782
                s->insert--;
Packit Service 4a2782
                if (s->lookahead + s->insert < MIN_MATCH)
Packit Service 4a2782
                    break;
Packit Service 4a2782
            }
Packit Service 4a2782
        }
Packit Service 4a2782
        /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
Packit Service 4a2782
         * but this is not important since only literal bytes will be emitted.
Packit Service 4a2782
         */
Packit Service 4a2782
Packit Service 4a2782
    } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
Packit Service 4a2782
Packit Service 4a2782
    /* If the WIN_INIT bytes after the end of the current data have never been
Packit Service 4a2782
     * written, then zero those bytes in order to avoid memory check reports of
Packit Service 4a2782
     * the use of uninitialized (or uninitialised as Julian writes) bytes by
Packit Service 4a2782
     * the longest match routines.  Update the high water mark for the next
Packit Service 4a2782
     * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
Packit Service 4a2782
     * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
Packit Service 4a2782
     */
Packit Service 4a2782
    if (s->high_water < s->window_size) {
Packit Service 4a2782
        ulg curr = s->strstart + (ulg)(s->lookahead);
Packit Service 4a2782
        ulg init;
Packit Service 4a2782
Packit Service 4a2782
        if (s->high_water < curr) {
Packit Service 4a2782
            /* Previous high water mark below current data -- zero WIN_INIT
Packit Service 4a2782
             * bytes or up to end of window, whichever is less.
Packit Service 4a2782
             */
Packit Service 4a2782
            init = s->window_size - curr;
Packit Service 4a2782
            if (init > WIN_INIT)
Packit Service 4a2782
                init = WIN_INIT;
Packit Service 4a2782
            zmemzero(s->window + curr, (unsigned)init);
Packit Service 4a2782
            s->high_water = curr + init;
Packit Service 4a2782
        }
Packit Service 4a2782
        else if (s->high_water < (ulg)curr + WIN_INIT) {
Packit Service 4a2782
            /* High water mark at or above current data, but below current data
Packit Service 4a2782
             * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
Packit Service 4a2782
             * to end of window, whichever is less.
Packit Service 4a2782
             */
Packit Service 4a2782
            init = (ulg)curr + WIN_INIT - s->high_water;
Packit Service 4a2782
            if (init > s->window_size - s->high_water)
Packit Service 4a2782
                init = s->window_size - s->high_water;
Packit Service 4a2782
            zmemzero(s->window + s->high_water, (unsigned)init);
Packit Service 4a2782
            s->high_water += init;
Packit Service 4a2782
        }
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
Packit Service 4a2782
           "not enough room for search");
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Flush the current block, with given end-of-file flag.
Packit Service 4a2782
 * IN assertion: strstart is set to the end of the current match.
Packit Service 4a2782
 */
Packit Service 4a2782
#define FLUSH_BLOCK_ONLY(s, last) { \
Packit Service 4a2782
   _tr_flush_block(s, (s->block_start >= 0L ? \
Packit Service 4a2782
                   (charf *)&s->window[(unsigned)s->block_start] : \
Packit Service 4a2782
                   (charf *)Z_NULL), \
Packit Service 4a2782
                (ulg)((long)s->strstart - s->block_start), \
Packit Service 4a2782
                (last)); \
Packit Service 4a2782
   s->block_start = s->strstart; \
Packit Service 4a2782
   flush_pending(s->strm); \
Packit Service 4a2782
   Tracev((stderr,"[FLUSH]")); \
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* Same but force premature exit if necessary. */
Packit Service 4a2782
#define FLUSH_BLOCK(s, last) { \
Packit Service 4a2782
   FLUSH_BLOCK_ONLY(s, last); \
Packit Service 4a2782
   if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* Maximum stored block length in deflate format (not including header). */
Packit Service 4a2782
#define MAX_STORED 65535
Packit Service 4a2782
Packit Service 4a2782
/* Minimum of a and b. */
Packit Service 4a2782
#define MIN(a, b) ((a) > (b) ? (b) : (a))
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Copy without compression as much as possible from the input stream, return
Packit Service 4a2782
 * the current block state.
Packit Service 4a2782
 *
Packit Service 4a2782
 * In case deflateParams() is used to later switch to a non-zero compression
Packit Service 4a2782
 * level, s->matches (otherwise unused when storing) keeps track of the number
Packit Service 4a2782
 * of hash table slides to perform. If s->matches is 1, then one hash table
Packit Service 4a2782
 * slide will be done when switching. If s->matches is 2, the maximum value
Packit Service 4a2782
 * allowed here, then the hash table will be cleared, since two or more slides
Packit Service 4a2782
 * is the same as a clear.
Packit Service 4a2782
 *
Packit Service 4a2782
 * deflate_stored() is written to minimize the number of times an input byte is
Packit Service 4a2782
 * copied. It is most efficient with large input and output buffers, which
Packit Service 4a2782
 * maximizes the opportunites to have a single copy from next_in to next_out.
Packit Service 4a2782
 */
Packit Service 4a2782
local block_state deflate_stored(
Packit Service 4a2782
    deflate_state *s,
Packit Service 4a2782
    int flush)
Packit Service 4a2782
{
Packit Service 4a2782
    /* Smallest worthy block size when not flushing or finishing. By default
Packit Service 4a2782
     * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
Packit Service 4a2782
     * large input and output buffers, the stored block size will be larger.
Packit Service 4a2782
     */
Packit Service 4a2782
    unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
Packit Service 4a2782
Packit Service 4a2782
    /* Copy as many min_block or larger stored blocks directly to next_out as
Packit Service 4a2782
     * possible. If flushing, copy the remaining available input to next_out as
Packit Service 4a2782
     * stored blocks, if there is enough space.
Packit Service 4a2782
     */
Packit Service 4a2782
    unsigned len, left, have, last = 0;
Packit Service 4a2782
    unsigned used = s->strm->avail_in;
Packit Service 4a2782
    do {
Packit Service 4a2782
        /* Set len to the maximum size block that we can copy directly with the
Packit Service 4a2782
         * available input data and output space. Set left to how much of that
Packit Service 4a2782
         * would be copied from what's left in the window.
Packit Service 4a2782
         */
Packit Service 4a2782
        len = MAX_STORED;       /* maximum deflate stored block length */
Packit Service 4a2782
        have = (s->bi_valid + 42) >> 3;         /* number of header bytes */
Packit Service 4a2782
        if (s->strm->avail_out < have)          /* need room for header */
Packit Service 4a2782
            break;
Packit Service 4a2782
            /* maximum stored block length that will fit in avail_out: */
Packit Service 4a2782
        have = s->strm->avail_out - have;
Packit Service 4a2782
        left = s->strstart - s->block_start;    /* bytes left in window */
Packit Service 4a2782
        if (len > (ulg)left + s->strm->avail_in)
Packit Service 4a2782
            len = left + s->strm->avail_in;     /* limit len to the input */
Packit Service 4a2782
        if (len > have)
Packit Service 4a2782
            len = have;                         /* limit len to the output */
Packit Service 4a2782
Packit Service 4a2782
        /* If the stored block would be less than min_block in length, or if
Packit Service 4a2782
         * unable to copy all of the available input when flushing, then try
Packit Service 4a2782
         * copying to the window and the pending buffer instead. Also don't
Packit Service 4a2782
         * write an empty block when flushing -- deflate() does that.
Packit Service 4a2782
         */
Packit Service 4a2782
        if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
Packit Service 4a2782
                                flush == Z_NO_FLUSH ||
Packit Service 4a2782
                                len != left + s->strm->avail_in))
Packit Service 4a2782
            break;
Packit Service 4a2782
Packit Service 4a2782
        /* Make a dummy stored block in pending to get the header bytes,
Packit Service 4a2782
         * including any pending bits. This also updates the debugging counts.
Packit Service 4a2782
         */
Packit Service 4a2782
        last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
Packit Service 4a2782
        _tr_stored_block(s, (char *)0, 0L, last);
Packit Service 4a2782
Packit Service 4a2782
        /* Replace the lengths in the dummy stored block with len. */
Packit Service 4a2782
        s->pending_buf[s->pending - 4] = len;
Packit Service 4a2782
        s->pending_buf[s->pending - 3] = len >> 8;
Packit Service 4a2782
        s->pending_buf[s->pending - 2] = ~len;
Packit Service 4a2782
        s->pending_buf[s->pending - 1] = ~len >> 8;
Packit Service 4a2782
Packit Service 4a2782
        /* Write the stored block header bytes. */
Packit Service 4a2782
        flush_pending(s->strm);
Packit Service 4a2782
Packit Service 4a2782
#ifdef ZLIB_DEBUG
Packit Service 4a2782
        /* Update debugging counts for the data about to be copied. */
Packit Service 4a2782
        s->compressed_len += len << 3;
Packit Service 4a2782
        s->bits_sent += len << 3;
Packit Service 4a2782
#endif
Packit Service 4a2782
Packit Service 4a2782
        /* Copy uncompressed bytes from the window to next_out. */
Packit Service 4a2782
        if (left) {
Packit Service 4a2782
            if (left > len)
Packit Service 4a2782
                left = len;
Packit Service 4a2782
            zmemcpy(s->strm->next_out, s->window + s->block_start, left);
Packit Service 4a2782
            s->strm->next_out += left;
Packit Service 4a2782
            s->strm->avail_out -= left;
Packit Service 4a2782
            s->strm->total_out += left;
Packit Service 4a2782
            s->block_start += left;
Packit Service 4a2782
            len -= left;
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
        /* Copy uncompressed bytes directly from next_in to next_out, updating
Packit Service 4a2782
         * the check value.
Packit Service 4a2782
         */
Packit Service 4a2782
        if (len) {
Packit Service 4a2782
            read_buf(s->strm, s->strm->next_out, len);
Packit Service 4a2782
            s->strm->next_out += len;
Packit Service 4a2782
            s->strm->avail_out -= len;
Packit Service 4a2782
            s->strm->total_out += len;
Packit Service 4a2782
        }
Packit Service 4a2782
    } while (last == 0);
Packit Service 4a2782
Packit Service 4a2782
    /* Update the sliding window with the last s->w_size bytes of the copied
Packit Service 4a2782
     * data, or append all of the copied data to the existing window if less
Packit Service 4a2782
     * than s->w_size bytes were copied. Also update the number of bytes to
Packit Service 4a2782
     * insert in the hash tables, in the event that deflateParams() switches to
Packit Service 4a2782
     * a non-zero compression level.
Packit Service 4a2782
     */
Packit Service 4a2782
    used -= s->strm->avail_in;      /* number of input bytes directly copied */
Packit Service 4a2782
    if (used) {
Packit Service 4a2782
        /* If any input was used, then no unused input remains in the window,
Packit Service 4a2782
         * therefore s->block_start == s->strstart.
Packit Service 4a2782
         */
Packit Service 4a2782
        if (used >= s->w_size) {    /* supplant the previous history */
Packit Service 4a2782
            s->matches = 2;         /* clear hash */
Packit Service 4a2782
            zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
Packit Service 4a2782
            s->strstart = s->w_size;
Packit Service 4a2782
        }
Packit Service 4a2782
        else {
Packit Service 4a2782
            if (s->window_size - s->strstart <= used) {
Packit Service 4a2782
                /* Slide the window down. */
Packit Service 4a2782
                s->strstart -= s->w_size;
Packit Service 4a2782
                zmemcpy(s->window, s->window + s->w_size, s->strstart);
Packit Service 4a2782
                if (s->matches < 2)
Packit Service 4a2782
                    s->matches++;   /* add a pending slide_hash() */
Packit Service 4a2782
            }
Packit Service 4a2782
            zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
Packit Service 4a2782
            s->strstart += used;
Packit Service 4a2782
        }
Packit Service 4a2782
        s->block_start = s->strstart;
Packit Service 4a2782
        s->insert += MIN(used, s->w_size - s->insert);
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->high_water < s->strstart)
Packit Service 4a2782
        s->high_water = s->strstart;
Packit Service 4a2782
Packit Service 4a2782
    /* If the last block was written to next_out, then done. */
Packit Service 4a2782
    if (last)
Packit Service 4a2782
        return finish_done;
Packit Service 4a2782
Packit Service 4a2782
    /* If flushing and all input has been consumed, then done. */
Packit Service 4a2782
    if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
Packit Service 4a2782
        s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
Packit Service 4a2782
        return block_done;
Packit Service 4a2782
Packit Service 4a2782
    /* Fill the window with any remaining input. */
Packit Service 4a2782
    have = s->window_size - s->strstart - 1;
Packit Service 4a2782
    if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
Packit Service 4a2782
        /* Slide the window down. */
Packit Service 4a2782
        s->block_start -= s->w_size;
Packit Service 4a2782
        s->strstart -= s->w_size;
Packit Service 4a2782
        zmemcpy(s->window, s->window + s->w_size, s->strstart);
Packit Service 4a2782
        if (s->matches < 2)
Packit Service 4a2782
            s->matches++;           /* add a pending slide_hash() */
Packit Service 4a2782
        have += s->w_size;          /* more space now */
Packit Service 4a2782
    }
Packit Service 4a2782
    if (have > s->strm->avail_in)
Packit Service 4a2782
        have = s->strm->avail_in;
Packit Service 4a2782
    if (have) {
Packit Service 4a2782
        read_buf(s->strm, s->window + s->strstart, have);
Packit Service 4a2782
        s->strstart += have;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->high_water < s->strstart)
Packit Service 4a2782
        s->high_water = s->strstart;
Packit Service 4a2782
Packit Service 4a2782
    /* There was not enough avail_out to write a complete worthy or flushed
Packit Service 4a2782
     * stored block to next_out. Write a stored block to pending instead, if we
Packit Service 4a2782
     * have enough input for a worthy block, or if flushing and there is enough
Packit Service 4a2782
     * room for the remaining input as a stored block in the pending buffer.
Packit Service 4a2782
     */
Packit Service 4a2782
    have = (s->bi_valid + 42) >> 3;         /* number of header bytes */
Packit Service 4a2782
        /* maximum stored block length that will fit in pending: */
Packit Service 4a2782
    have = MIN(s->pending_buf_size - have, MAX_STORED);
Packit Service 4a2782
    min_block = MIN(have, s->w_size);
Packit Service 4a2782
    left = s->strstart - s->block_start;
Packit Service 4a2782
    if (left >= min_block ||
Packit Service 4a2782
        ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
Packit Service 4a2782
         s->strm->avail_in == 0 && left <= have)) {
Packit Service 4a2782
        len = MIN(left, have);
Packit Service 4a2782
        last = flush == Z_FINISH && s->strm->avail_in == 0 &&
Packit Service 4a2782
               len == left ? 1 : 0;
Packit Service 4a2782
        _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
Packit Service 4a2782
        s->block_start += len;
Packit Service 4a2782
        flush_pending(s->strm);
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    /* We've done all we can with the available input and output. */
Packit Service 4a2782
    return last ? finish_started : need_more;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Compress as much as possible from the input stream, return the current
Packit Service 4a2782
 * block state.
Packit Service 4a2782
 * This function does not perform lazy evaluation of matches and inserts
Packit Service 4a2782
 * new strings in the dictionary only for unmatched strings or for short
Packit Service 4a2782
 * matches. It is used only for the fast compression options.
Packit Service 4a2782
 */
Packit Service 4a2782
local block_state deflate_fast(
Packit Service 4a2782
    deflate_state *s,
Packit Service 4a2782
    int flush)
Packit Service 4a2782
{
Packit Service 4a2782
    IPos hash_head;       /* head of the hash chain */
Packit Service 4a2782
    int bflush;           /* set if current block must be flushed */
Packit Service 4a2782
Packit Service 4a2782
    for (;;) {
Packit Service 4a2782
        /* Make sure that we always have enough lookahead, except
Packit Service 4a2782
         * at the end of the input file. We need MAX_MATCH bytes
Packit Service 4a2782
         * for the next match, plus MIN_MATCH bytes to insert the
Packit Service 4a2782
         * string following the next match.
Packit Service 4a2782
         */
Packit Service 4a2782
        if (s->lookahead < MIN_LOOKAHEAD) {
Packit Service 4a2782
            fill_window(s);
Packit Service 4a2782
            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
Packit Service 4a2782
                return need_more;
Packit Service 4a2782
            }
Packit Service 4a2782
            if (s->lookahead == 0) break; /* flush the current block */
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
        /* Insert the string window[strstart .. strstart+2] in the
Packit Service 4a2782
         * dictionary, and set hash_head to the head of the hash chain:
Packit Service 4a2782
         */
Packit Service 4a2782
        hash_head = NIL;
Packit Service 4a2782
        if (s->lookahead >= MIN_MATCH) {
Packit Service 4a2782
            INSERT_STRING(s, s->strstart, hash_head);
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
        /* Find the longest match, discarding those <= prev_length.
Packit Service 4a2782
         * At this point we have always match_length < MIN_MATCH
Packit Service 4a2782
         */
Packit Service 4a2782
        if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
Packit Service 4a2782
            /* To simplify the code, we prevent matches with the string
Packit Service 4a2782
             * of window index 0 (in particular we have to avoid a match
Packit Service 4a2782
             * of the string with itself at the start of the input file).
Packit Service 4a2782
             */
Packit Service 4a2782
            s->match_length = longest_match (s, hash_head);
Packit Service 4a2782
            /* longest_match() sets match_start */
Packit Service 4a2782
        }
Packit Service 4a2782
        if (s->match_length >= MIN_MATCH) {
Packit Service 4a2782
            check_match(s, s->strstart, s->match_start, s->match_length);
Packit Service 4a2782
Packit Service 4a2782
            _tr_tally_dist(s, s->strstart - s->match_start,
Packit Service 4a2782
                           s->match_length - MIN_MATCH, bflush);
Packit Service 4a2782
Packit Service 4a2782
            s->lookahead -= s->match_length;
Packit Service 4a2782
Packit Service 4a2782
            /* Insert new strings in the hash table only if the match length
Packit Service 4a2782
             * is not too large. This saves time but degrades compression.
Packit Service 4a2782
             */
Packit Service 4a2782
#ifndef FASTEST
Packit Service 4a2782
            if (s->match_length <= s->max_insert_length &&
Packit Service 4a2782
                s->lookahead >= MIN_MATCH) {
Packit Service 4a2782
                s->match_length--; /* string at strstart already in table */
Packit Service 4a2782
                do {
Packit Service 4a2782
                    s->strstart++;
Packit Service 4a2782
                    INSERT_STRING(s, s->strstart, hash_head);
Packit Service 4a2782
                    /* strstart never exceeds WSIZE-MAX_MATCH, so there are
Packit Service 4a2782
                     * always MIN_MATCH bytes ahead.
Packit Service 4a2782
                     */
Packit Service 4a2782
                } while (--s->match_length != 0);
Packit Service 4a2782
                s->strstart++;
Packit Service 4a2782
            } else
Packit Service 4a2782
#endif
Packit Service 4a2782
            {
Packit Service 4a2782
                s->strstart += s->match_length;
Packit Service 4a2782
                s->match_length = 0;
Packit Service 4a2782
                s->ins_h = s->window[s->strstart];
Packit Service 4a2782
                UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
Packit Service 4a2782
#if MIN_MATCH != 3
Packit Service 4a2782
                Call UPDATE_HASH() MIN_MATCH-3 more times
Packit Service 4a2782
#endif
Packit Service 4a2782
                /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
Packit Service 4a2782
                 * matter since it will be recomputed at next deflate call.
Packit Service 4a2782
                 */
Packit Service 4a2782
            }
Packit Service 4a2782
        } else {
Packit Service 4a2782
            /* No match, output a literal byte */
Packit Service 4a2782
            Tracevv((stderr,"%c", s->window[s->strstart]));
Packit Service 4a2782
            _tr_tally_lit (s, s->window[s->strstart], bflush);
Packit Service 4a2782
            s->lookahead--;
Packit Service 4a2782
            s->strstart++;
Packit Service 4a2782
        }
Packit Service 4a2782
        if (bflush) FLUSH_BLOCK(s, 0);
Packit Service 4a2782
    }
Packit Service 4a2782
    s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
Packit Service 4a2782
    if (flush == Z_FINISH) {
Packit Service 4a2782
        FLUSH_BLOCK(s, 1);
Packit Service 4a2782
        return finish_done;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->last_lit)
Packit Service 4a2782
        FLUSH_BLOCK(s, 0);
Packit Service 4a2782
    return block_done;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
#ifndef FASTEST
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * Same as above, but achieves better compression. We use a lazy
Packit Service 4a2782
 * evaluation for matches: a match is finally adopted only if there is
Packit Service 4a2782
 * no better match at the next window position.
Packit Service 4a2782
 */
Packit Service 4a2782
local block_state deflate_slow(
Packit Service 4a2782
    deflate_state *s,
Packit Service 4a2782
    int flush)
Packit Service 4a2782
{
Packit Service 4a2782
    IPos hash_head;          /* head of hash chain */
Packit Service 4a2782
    int bflush;              /* set if current block must be flushed */
Packit Service 4a2782
Packit Service 4a2782
    /* Process the input block. */
Packit Service 4a2782
    for (;;) {
Packit Service 4a2782
        /* Make sure that we always have enough lookahead, except
Packit Service 4a2782
         * at the end of the input file. We need MAX_MATCH bytes
Packit Service 4a2782
         * for the next match, plus MIN_MATCH bytes to insert the
Packit Service 4a2782
         * string following the next match.
Packit Service 4a2782
         */
Packit Service 4a2782
        if (s->lookahead < MIN_LOOKAHEAD) {
Packit Service 4a2782
            fill_window(s);
Packit Service 4a2782
            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
Packit Service 4a2782
                return need_more;
Packit Service 4a2782
            }
Packit Service 4a2782
            if (s->lookahead == 0) break; /* flush the current block */
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
        /* Insert the string window[strstart .. strstart+2] in the
Packit Service 4a2782
         * dictionary, and set hash_head to the head of the hash chain:
Packit Service 4a2782
         */
Packit Service 4a2782
        hash_head = NIL;
Packit Service 4a2782
        if (s->lookahead >= MIN_MATCH) {
Packit Service 4a2782
            INSERT_STRING(s, s->strstart, hash_head);
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
        /* Find the longest match, discarding those <= prev_length.
Packit Service 4a2782
         */
Packit Service 4a2782
        s->prev_length = s->match_length, s->prev_match = s->match_start;
Packit Service 4a2782
        s->match_length = MIN_MATCH-1;
Packit Service 4a2782
Packit Service 4a2782
        if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
Packit Service 4a2782
            s->strstart - hash_head <= MAX_DIST(s)) {
Packit Service 4a2782
            /* To simplify the code, we prevent matches with the string
Packit Service 4a2782
             * of window index 0 (in particular we have to avoid a match
Packit Service 4a2782
             * of the string with itself at the start of the input file).
Packit Service 4a2782
             */
Packit Service 4a2782
            s->match_length = longest_match (s, hash_head);
Packit Service 4a2782
            /* longest_match() sets match_start */
Packit Service 4a2782
Packit Service 4a2782
            if (s->match_length <= 5 && (s->strategy == Z_FILTERED
Packit Service 4a2782
#if TOO_FAR <= 32767
Packit Service 4a2782
                || (s->match_length == MIN_MATCH &&
Packit Service 4a2782
                    s->strstart - s->match_start > TOO_FAR)
Packit Service 4a2782
#endif
Packit Service 4a2782
                )) {
Packit Service 4a2782
Packit Service 4a2782
                /* If prev_match is also MIN_MATCH, match_start is garbage
Packit Service 4a2782
                 * but we will ignore the current match anyway.
Packit Service 4a2782
                 */
Packit Service 4a2782
                s->match_length = MIN_MATCH-1;
Packit Service 4a2782
            }
Packit Service 4a2782
        }
Packit Service 4a2782
        /* If there was a match at the previous step and the current
Packit Service 4a2782
         * match is not better, output the previous match:
Packit Service 4a2782
         */
Packit Service 4a2782
        if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
Packit Service 4a2782
            uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
Packit Service 4a2782
            /* Do not insert strings in hash table beyond this. */
Packit Service 4a2782
Packit Service 4a2782
            check_match(s, s->strstart-1, s->prev_match, s->prev_length);
Packit Service 4a2782
Packit Service 4a2782
            _tr_tally_dist(s, s->strstart -1 - s->prev_match,
Packit Service 4a2782
                           s->prev_length - MIN_MATCH, bflush);
Packit Service 4a2782
Packit Service 4a2782
            /* Insert in hash table all strings up to the end of the match.
Packit Service 4a2782
             * strstart-1 and strstart are already inserted. If there is not
Packit Service 4a2782
             * enough lookahead, the last two strings are not inserted in
Packit Service 4a2782
             * the hash table.
Packit Service 4a2782
             */
Packit Service 4a2782
            s->lookahead -= s->prev_length-1;
Packit Service 4a2782
            s->prev_length -= 2;
Packit Service 4a2782
            do {
Packit Service 4a2782
                if (++s->strstart <= max_insert) {
Packit Service 4a2782
                    INSERT_STRING(s, s->strstart, hash_head);
Packit Service 4a2782
                }
Packit Service 4a2782
            } while (--s->prev_length != 0);
Packit Service 4a2782
            s->match_available = 0;
Packit Service 4a2782
            s->match_length = MIN_MATCH-1;
Packit Service 4a2782
            s->strstart++;
Packit Service 4a2782
Packit Service 4a2782
            if (bflush) FLUSH_BLOCK(s, 0);
Packit Service 4a2782
Packit Service 4a2782
        } else if (s->match_available) {
Packit Service 4a2782
            /* If there was no match at the previous position, output a
Packit Service 4a2782
             * single literal. If there was a match but the current match
Packit Service 4a2782
             * is longer, truncate the previous match to a single literal.
Packit Service 4a2782
             */
Packit Service 4a2782
            Tracevv((stderr,"%c", s->window[s->strstart-1]));
Packit Service 4a2782
            _tr_tally_lit(s, s->window[s->strstart-1], bflush);
Packit Service 4a2782
            if (bflush) {
Packit Service 4a2782
                FLUSH_BLOCK_ONLY(s, 0);
Packit Service 4a2782
            }
Packit Service 4a2782
            s->strstart++;
Packit Service 4a2782
            s->lookahead--;
Packit Service 4a2782
            if (s->strm->avail_out == 0) return need_more;
Packit Service 4a2782
        } else {
Packit Service 4a2782
            /* There is no previous match to compare with, wait for
Packit Service 4a2782
             * the next step to decide.
Packit Service 4a2782
             */
Packit Service 4a2782
            s->match_available = 1;
Packit Service 4a2782
            s->strstart++;
Packit Service 4a2782
            s->lookahead--;
Packit Service 4a2782
        }
Packit Service 4a2782
    }
Packit Service 4a2782
    Assert (flush != Z_NO_FLUSH, "no flush?");
Packit Service 4a2782
    if (s->match_available) {
Packit Service 4a2782
        Tracevv((stderr,"%c", s->window[s->strstart-1]));
Packit Service 4a2782
        _tr_tally_lit(s, s->window[s->strstart-1], bflush);
Packit Service 4a2782
        s->match_available = 0;
Packit Service 4a2782
    }
Packit Service 4a2782
    s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
Packit Service 4a2782
    if (flush == Z_FINISH) {
Packit Service 4a2782
        FLUSH_BLOCK(s, 1);
Packit Service 4a2782
        return finish_done;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->last_lit)
Packit Service 4a2782
        FLUSH_BLOCK(s, 0);
Packit Service 4a2782
    return block_done;
Packit Service 4a2782
}
Packit Service 4a2782
#endif /* FASTEST */
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
Packit Service 4a2782
 * one.  Do not maintain a hash table.  (It will be regenerated if this run of
Packit Service 4a2782
 * deflate switches away from Z_RLE.)
Packit Service 4a2782
 */
Packit Service 4a2782
local block_state deflate_rle(
Packit Service 4a2782
    deflate_state *s,
Packit Service 4a2782
    int flush)
Packit Service 4a2782
{
Packit Service 4a2782
    int bflush;             /* set if current block must be flushed */
Packit Service 4a2782
    uInt prev;              /* byte at distance one to match */
Packit Service 4a2782
    Bytef *scan, *strend;   /* scan goes up to strend for length of run */
Packit Service 4a2782
Packit Service 4a2782
    for (;;) {
Packit Service 4a2782
        /* Make sure that we always have enough lookahead, except
Packit Service 4a2782
         * at the end of the input file. We need MAX_MATCH bytes
Packit Service 4a2782
         * for the longest run, plus one for the unrolled loop.
Packit Service 4a2782
         */
Packit Service 4a2782
        if (s->lookahead <= MAX_MATCH) {
Packit Service 4a2782
            fill_window(s);
Packit Service 4a2782
            if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
Packit Service 4a2782
                return need_more;
Packit Service 4a2782
            }
Packit Service 4a2782
            if (s->lookahead == 0) break; /* flush the current block */
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
        /* See how many times the previous byte repeats */
Packit Service 4a2782
        s->match_length = 0;
Packit Service 4a2782
        if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
Packit Service 4a2782
            scan = s->window + s->strstart - 1;
Packit Service 4a2782
            prev = *scan;
Packit Service 4a2782
            if (prev == *++scan && prev == *++scan && prev == *++scan) {
Packit Service 4a2782
                strend = s->window + s->strstart + MAX_MATCH;
Packit Service 4a2782
                do {
Packit Service 4a2782
                } while (prev == *++scan && prev == *++scan &&
Packit Service 4a2782
                         prev == *++scan && prev == *++scan &&
Packit Service 4a2782
                         prev == *++scan && prev == *++scan &&
Packit Service 4a2782
                         prev == *++scan && prev == *++scan &&
Packit Service 4a2782
                         scan < strend);
Packit Service 4a2782
                s->match_length = MAX_MATCH - (uInt)(strend - scan);
Packit Service 4a2782
                if (s->match_length > s->lookahead)
Packit Service 4a2782
                    s->match_length = s->lookahead;
Packit Service 4a2782
            }
Packit Service 4a2782
            Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
        /* Emit match if have run of MIN_MATCH or longer, else emit literal */
Packit Service 4a2782
        if (s->match_length >= MIN_MATCH) {
Packit Service 4a2782
            check_match(s, s->strstart, s->strstart - 1, s->match_length);
Packit Service 4a2782
Packit Service 4a2782
            _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
Packit Service 4a2782
Packit Service 4a2782
            s->lookahead -= s->match_length;
Packit Service 4a2782
            s->strstart += s->match_length;
Packit Service 4a2782
            s->match_length = 0;
Packit Service 4a2782
        } else {
Packit Service 4a2782
            /* No match, output a literal byte */
Packit Service 4a2782
            Tracevv((stderr,"%c", s->window[s->strstart]));
Packit Service 4a2782
            _tr_tally_lit (s, s->window[s->strstart], bflush);
Packit Service 4a2782
            s->lookahead--;
Packit Service 4a2782
            s->strstart++;
Packit Service 4a2782
        }
Packit Service 4a2782
        if (bflush) FLUSH_BLOCK(s, 0);
Packit Service 4a2782
    }
Packit Service 4a2782
    s->insert = 0;
Packit Service 4a2782
    if (flush == Z_FINISH) {
Packit Service 4a2782
        FLUSH_BLOCK(s, 1);
Packit Service 4a2782
        return finish_done;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->last_lit)
Packit Service 4a2782
        FLUSH_BLOCK(s, 0);
Packit Service 4a2782
    return block_done;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
 * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
Packit Service 4a2782
 * (It will be regenerated if this run of deflate switches away from Huffman.)
Packit Service 4a2782
 */
Packit Service 4a2782
local block_state deflate_huff(
Packit Service 4a2782
    deflate_state *s,
Packit Service 4a2782
    int flush)
Packit Service 4a2782
{
Packit Service 4a2782
    int bflush;             /* set if current block must be flushed */
Packit Service 4a2782
Packit Service 4a2782
    for (;;) {
Packit Service 4a2782
        /* Make sure that we have a literal to write. */
Packit Service 4a2782
        if (s->lookahead == 0) {
Packit Service 4a2782
            fill_window(s);
Packit Service 4a2782
            if (s->lookahead == 0) {
Packit Service 4a2782
                if (flush == Z_NO_FLUSH)
Packit Service 4a2782
                    return need_more;
Packit Service 4a2782
                break;      /* flush the current block */
Packit Service 4a2782
            }
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
        /* Output a literal byte */
Packit Service 4a2782
        s->match_length = 0;
Packit Service 4a2782
        Tracevv((stderr,"%c", s->window[s->strstart]));
Packit Service 4a2782
        _tr_tally_lit (s, s->window[s->strstart], bflush);
Packit Service 4a2782
        s->lookahead--;
Packit Service 4a2782
        s->strstart++;
Packit Service 4a2782
        if (bflush) FLUSH_BLOCK(s, 0);
Packit Service 4a2782
    }
Packit Service 4a2782
    s->insert = 0;
Packit Service 4a2782
    if (flush == Z_FINISH) {
Packit Service 4a2782
        FLUSH_BLOCK(s, 1);
Packit Service 4a2782
        return finish_done;
Packit Service 4a2782
    }
Packit Service 4a2782
    if (s->last_lit)
Packit Service 4a2782
        FLUSH_BLOCK(s, 0);
Packit Service 4a2782
    return block_done;
Packit Service 4a2782
}