os/ossrv/compressionlibs/ziplib/test/oldezlib/Zlib/deflate.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/oldezlib/Zlib/deflate.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1307 @@
     1.4 +/* deflate.c -- compress data using the deflation algorithm
     1.5 + * Copyright (C) 1995-1998 Jean-loup Gailly.
     1.6 + * For conditions of distribution and use, see copyright notice in zlib.h 
     1.7 + */
     1.8 +
     1.9 +/*
    1.10 + *  ALGORITHM
    1.11 + *
    1.12 + *      The "deflation" process depends on being able to identify portions
    1.13 + *      of the input text which are identical to earlier input (within a
    1.14 + *      sliding window trailing behind the input currently being processed).
    1.15 + *
    1.16 + *      The most straightforward technique turns out to be the fastest for
    1.17 + *      most input files: try all possible matches and select the longest.
    1.18 + *      The key feature of this algorithm is that insertions into the string
    1.19 + *      dictionary are very simple and thus fast, and deletions are avoided
    1.20 + *      completely. Insertions are performed at each input character, whereas
    1.21 + *      string matches are performed only when the previous match ends. So it
    1.22 + *      is preferable to spend more time in matches to allow very fast string
    1.23 + *      insertions and avoid deletions. The matching algorithm for small
    1.24 + *      strings is inspired from that of Rabin & Karp. A brute force approach
    1.25 + *      is used to find longer strings when a small match has been found.
    1.26 + *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
    1.27 + *      (by Leonid Broukhis).
    1.28 + *         A previous version of this file used a more sophisticated algorithm
    1.29 + *      (by Fiala and Greene) which is guaranteed to run in linear amortized
    1.30 + *      time, but has a larger average cost, uses more memory and is patented.
    1.31 + *      However the F&G algorithm may be faster for some highly redundant
    1.32 + *      files if the parameter max_chain_length (described below) is too large.
    1.33 + *
    1.34 + *  ACKNOWLEDGEMENTS
    1.35 + *
    1.36 + *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
    1.37 + *      I found it in 'freeze' written by Leonid Broukhis.
    1.38 + *      Thanks to many people for bug reports and testing.
    1.39 + *
    1.40 + *  REFERENCES
    1.41 + *
    1.42 + *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
    1.43 + *      Available in ftp://ds.internic.net/rfc/rfc1951.txt
    1.44 + *
    1.45 + *      A description of the Rabin and Karp algorithm is given in the book
    1.46 + *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
    1.47 + *
    1.48 + *      Fiala,E.R., and Greene,D.H.
    1.49 + *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
    1.50 + *
    1.51 + */
    1.52 +
    1.53 +/* @(#) $Id$ */
    1.54 +
    1.55 +#include "deflate.h"
    1.56 +
    1.57 +const char deflate_copyright[] =
    1.58 +   " deflate 1.1.3 Copyright 1995-1998 Jean-loup Gailly ";
    1.59 +/*
    1.60 +  If you use the zlib library in a product, an acknowledgment is welcome
    1.61 +  in the documentation of your product. If for some reason you cannot
    1.62 +  include such an acknowledgment, I would appreciate that you keep this
    1.63 +  copyright string in the executable of your product.
    1.64 + */
    1.65 +
    1.66 +/* ===========================================================================
    1.67 + *  Function prototypes.
    1.68 + */
    1.69 +typedef enum {
    1.70 +    need_more,      /* block not completed, need more input or more output */
    1.71 +    block_done,     /* block flush performed */
    1.72 +    finish_started, /* finish started, need only more output at next deflate */
    1.73 +    finish_done     /* finish done, accept no more input or output */
    1.74 +} block_state;
    1.75 +
    1.76 +typedef block_state (*compress_func) OF((deflate_state *s, int flush));
    1.77 +/* Compression function. Returns the block state after the call. */
    1.78 +
    1.79 +local void fill_window    OF((deflate_state *s));
    1.80 +local block_state deflate_stored OF((deflate_state *s, int flush));
    1.81 +local block_state deflate_fast   OF((deflate_state *s, int flush));
    1.82 +local block_state deflate_slow   OF((deflate_state *s, int flush));
    1.83 +local void lm_init        OF((deflate_state *s));
    1.84 +local void putShortMSB    OF((deflate_state *s, uInt b));
    1.85 +local void flush_pending  OF((z_streamp strm));
    1.86 +local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
    1.87 +#ifdef ASMV
    1.88 +      void match_init OF((void)); /* asm code initialization */
    1.89 +      uInt longest_match  OF((deflate_state *s, IPos cur_match));
    1.90 +#else
    1.91 +local uInt longest_match  OF((deflate_state *s, IPos cur_match));
    1.92 +#endif
    1.93 +
    1.94 +#ifdef DEBUG
    1.95 +local  void check_match OF((deflate_state *s, IPos start, IPos match,
    1.96 +                            int length));
    1.97 +#endif
    1.98 +
    1.99 +/* ===========================================================================
   1.100 + * Local data
   1.101 + */
   1.102 +
   1.103 +#define NIL 0
   1.104 +/* Tail of hash chains */
   1.105 +
   1.106 +#ifndef TOO_FAR
   1.107 +#  define TOO_FAR 4096
   1.108 +#endif
   1.109 +/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
   1.110 +
   1.111 +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
   1.112 +/* Minimum amount of lookahead, except at the end of the input file.
   1.113 + * See deflate.c for comments about the MIN_MATCH+1.
   1.114 + */
   1.115 +
   1.116 +/* Values for max_lazy_match, good_match and max_chain_length, depending on
   1.117 + * the desired pack level (0..9). The values given below have been tuned to
   1.118 + * exclude worst case performance for pathological files. Better values may be
   1.119 + * found for specific files.
   1.120 + */
   1.121 +typedef struct config_s {
   1.122 +   ush good_length; /* reduce lazy search above this match length */
   1.123 +   ush max_lazy;    /* do not perform lazy search above this match length */
   1.124 +   ush nice_length; /* quit search above this match length */
   1.125 +   ush max_chain;
   1.126 +   compress_func func;
   1.127 +} config;
   1.128 +
   1.129 +local const config configuration_table[10] = {
   1.130 +/*      good lazy nice chain */
   1.131 +/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
   1.132 +/* 1 */ {4,    4,  8,    4, deflate_fast}, /* maximum speed, no lazy matches */
   1.133 +/* 2 */ {4,    5, 16,    8, deflate_fast},
   1.134 +/* 3 */ {4,    6, 32,   32, deflate_fast},
   1.135 +
   1.136 +/* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
   1.137 +/* 5 */ {8,   16, 32,   32, deflate_slow},
   1.138 +/* 6 */ {8,   16, 128, 128, deflate_slow},
   1.139 +/* 7 */ {8,   32, 128, 256, deflate_slow},
   1.140 +/* 8 */ {32, 128, 258, 1024, deflate_slow},
   1.141 +/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
   1.142 +
   1.143 +/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
   1.144 + * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
   1.145 + * meaning.
   1.146 + */
   1.147 +
   1.148 +#define EQUAL 0
   1.149 +/* result of memcmp for equal strings */
   1.150 +
   1.151 +struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
   1.152 +
   1.153 +/* ===========================================================================
   1.154 + * Update a hash value with the given input byte
   1.155 + * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
   1.156 + *    input characters, so that a running hash key can be computed from the
   1.157 + *    previous key instead of complete recalculation each time.
   1.158 + */
   1.159 +#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
   1.160 +
   1.161 +
   1.162 +/* ===========================================================================
   1.163 + * Insert string str in the dictionary and set match_head to the previous head
   1.164 + * of the hash chain (the most recent string with same hash key). Return
   1.165 + * the previous length of the hash chain.
   1.166 + * If this file is compiled with -DFASTEST, the compression level is forced
   1.167 + * to 1, and no hash chains are maintained.
   1.168 + * IN  assertion: all calls to to INSERT_STRING are made with consecutive
   1.169 + *    input characters and the first MIN_MATCH bytes of str are valid
   1.170 + *    (except for the last MIN_MATCH-1 bytes of the input file).
   1.171 + */
   1.172 +#ifdef FASTEST
   1.173 +#define INSERT_STRING(s, str, match_head) \
   1.174 +   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
   1.175 +    match_head = s->head[s->ins_h], \
   1.176 +    s->head[s->ins_h] = (Pos)(str))
   1.177 +#else
   1.178 +#define INSERT_STRING(s, str, match_head) \
   1.179 +   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
   1.180 +    s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
   1.181 +    s->head[s->ins_h] = (Pos)(str))
   1.182 +#endif
   1.183 +
   1.184 +/* ===========================================================================
   1.185 + * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
   1.186 + * prev[] will be initialized on the fly.
   1.187 + */
   1.188 +#define CLEAR_HASH(s) \
   1.189 +    s->head[s->hash_size-1] = NIL; \
   1.190 +    zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
   1.191 +
   1.192 +/* ========================================================================= */
   1.193 +int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
   1.194 +{
   1.195 +    return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
   1.196 +			 Z_DEFAULT_STRATEGY, version, stream_size);
   1.197 +    /* To do: ignore strm->next_in if we use it as window */
   1.198 +}
   1.199 +
   1.200 +/* ========================================================================= */
   1.201 +int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy,
   1.202 +		  const char *version, int stream_size)
   1.203 +{
   1.204 +    deflate_state *s;
   1.205 +    int noheader = 0;
   1.206 +    static const char* my_version = ZLIB_VERSION;
   1.207 +
   1.208 +    ushf *overlay;
   1.209 +    /* We overlay pending_buf and d_buf+l_buf. This works since the average
   1.210 +     * output size for (length,distance) codes is <= 24 bits.
   1.211 +     */
   1.212 +
   1.213 +    if (version == Z_NULL || version[0] != my_version[0] ||
   1.214 +        stream_size != sizeof(z_stream)) {
   1.215 +	return Z_VERSION_ERROR;
   1.216 +    }
   1.217 +    if (strm == Z_NULL) return Z_STREAM_ERROR;
   1.218 +
   1.219 +    strm->msg = Z_NULL;
   1.220 +    if (strm->zalloc == Z_NULL) {
   1.221 +	strm->zalloc = zcalloc;
   1.222 +	strm->opaque = (voidpf)0;
   1.223 +    }
   1.224 +    if (strm->zfree == Z_NULL) strm->zfree = zcfree;
   1.225 +
   1.226 +    if (level == Z_DEFAULT_COMPRESSION) level = 6;
   1.227 +#ifdef FASTEST
   1.228 +    level = 1;
   1.229 +#endif
   1.230 +
   1.231 +    if (windowBits < 0) { /* undocumented feature: suppress zlib header */
   1.232 +        noheader = 1;
   1.233 +        windowBits = -windowBits;
   1.234 +    }
   1.235 +    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
   1.236 +        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
   1.237 +	strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
   1.238 +        return Z_STREAM_ERROR;
   1.239 +    }
   1.240 +    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
   1.241 +    if (s == Z_NULL) return Z_MEM_ERROR;
   1.242 +    strm->state = (struct internal_state FAR *)s;
   1.243 +    s->strm = strm;
   1.244 +
   1.245 +    s->noheader = noheader;
   1.246 +    s->w_bits = windowBits;
   1.247 +    s->w_size = 1 << s->w_bits;
   1.248 +    s->w_mask = s->w_size - 1;
   1.249 +
   1.250 +    s->hash_bits = memLevel + 7;
   1.251 +    s->hash_size = 1 << s->hash_bits;
   1.252 +    s->hash_mask = s->hash_size - 1;
   1.253 +    s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
   1.254 +
   1.255 +    s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
   1.256 +    s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
   1.257 +    s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
   1.258 +
   1.259 +    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
   1.260 +
   1.261 +    overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
   1.262 +    s->pending_buf = (uchf *) overlay;
   1.263 +    s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
   1.264 +
   1.265 +    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
   1.266 +        s->pending_buf == Z_NULL) {
   1.267 +        strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
   1.268 +        deflateEnd (strm);
   1.269 +        return Z_MEM_ERROR;
   1.270 +    }
   1.271 +    s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
   1.272 +    s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
   1.273 +
   1.274 +    s->level = level;
   1.275 +    s->strategy = strategy;
   1.276 +    s->method = (Byte)method;
   1.277 +
   1.278 +    return deflateReset(strm);
   1.279 +}
   1.280 +
   1.281 +/* ========================================================================= */
   1.282 +int ZEXPORT deflateSetDictionary (z_streamp strm, const Bytef *dictionary, uInt dictLength)
   1.283 +{
   1.284 +    deflate_state *s;
   1.285 +    uInt length = dictLength;
   1.286 +    uInt n;
   1.287 +    IPos hash_head = 0;
   1.288 +
   1.289 +    if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
   1.290 +        strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
   1.291 +
   1.292 +    s = strm->state;
   1.293 +    strm->adler = adler32(strm->adler, dictionary, dictLength);
   1.294 +
   1.295 +    if (length < MIN_MATCH) return Z_OK;
   1.296 +    if (length > MAX_DIST(s)) {
   1.297 +	length = MAX_DIST(s);
   1.298 +#ifndef USE_DICT_HEAD
   1.299 +	dictionary += dictLength - length; /* use the tail of the dictionary */
   1.300 +#endif
   1.301 +    }
   1.302 +    zmemcpy(s->window, dictionary, length);
   1.303 +    s->strstart = length;
   1.304 +    s->block_start = (long)length;
   1.305 +
   1.306 +    /* Insert all strings in the hash table (except for the last two bytes).
   1.307 +     * s->lookahead stays null, so s->ins_h will be recomputed at the next
   1.308 +     * call of fill_window.
   1.309 +     */
   1.310 +    s->ins_h = s->window[0];
   1.311 +    UPDATE_HASH(s, s->ins_h, s->window[1]);
   1.312 +    for (n = 0; n <= length - MIN_MATCH; n++) {
   1.313 +	INSERT_STRING(s, n, hash_head);
   1.314 +    }
   1.315 +    if (hash_head) hash_head = 0;  /* to make compiler happy */
   1.316 +    return Z_OK;
   1.317 +}
   1.318 +
   1.319 +/* ========================================================================= */
   1.320 +int ZEXPORT deflateReset (z_streamp strm)
   1.321 +{
   1.322 +    deflate_state *s;
   1.323 +    
   1.324 +    if (strm == Z_NULL || strm->state == Z_NULL ||
   1.325 +        strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
   1.326 +
   1.327 +    strm->total_in = strm->total_out = 0;
   1.328 +    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
   1.329 +    strm->data_type = Z_UNKNOWN;
   1.330 +
   1.331 +    s = (deflate_state *)strm->state;
   1.332 +    s->pending = 0;
   1.333 +    s->pending_out = s->pending_buf;
   1.334 +
   1.335 +    if (s->noheader < 0) {
   1.336 +        s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
   1.337 +    }
   1.338 +    s->status = s->noheader ? BUSY_STATE : INIT_STATE;
   1.339 +    strm->adler = 1;
   1.340 +    s->last_flush = Z_NO_FLUSH;
   1.341 +
   1.342 +    _tr_init(s);
   1.343 +    lm_init(s);
   1.344 +
   1.345 +    return Z_OK;
   1.346 +}
   1.347 +
   1.348 +/* ========================================================================= */
   1.349 +int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
   1.350 +{
   1.351 +    deflate_state *s;
   1.352 +    compress_func func;
   1.353 +    int err = Z_OK;
   1.354 +
   1.355 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
   1.356 +    s = strm->state;
   1.357 +
   1.358 +    if (level == Z_DEFAULT_COMPRESSION) {
   1.359 +	level = 6;
   1.360 +    }
   1.361 +    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
   1.362 +	return Z_STREAM_ERROR;
   1.363 +    }
   1.364 +    func = configuration_table[s->level].func;
   1.365 +
   1.366 +    if (func != configuration_table[level].func && strm->total_in != 0) {
   1.367 +	/* Flush the last buffer: */
   1.368 +	err = deflate(strm, Z_PARTIAL_FLUSH);
   1.369 +    }
   1.370 +    if (s->level != level) {
   1.371 +	s->level = level;
   1.372 +	s->max_lazy_match   = configuration_table[level].max_lazy;
   1.373 +	s->good_match       = configuration_table[level].good_length;
   1.374 +	s->nice_match       = configuration_table[level].nice_length;
   1.375 +	s->max_chain_length = configuration_table[level].max_chain;
   1.376 +    }
   1.377 +    s->strategy = strategy;
   1.378 +    return err;
   1.379 +}
   1.380 +
   1.381 +/* =========================================================================
   1.382 + * Put a short in the pending buffer. The 16-bit value is put in MSB order.
   1.383 + * IN assertion: the stream state is correct and there is enough room in
   1.384 + * pending_buf.
   1.385 + */
   1.386 +local void putShortMSB (deflate_state *s, uInt b)
   1.387 +{
   1.388 +    put_byte(s, (Byte)(b >> 8));
   1.389 +    put_byte(s, (Byte)(b & 0xff));
   1.390 +}   
   1.391 +
   1.392 +/* =========================================================================
   1.393 + * Flush as much pending output as possible. All deflate() output goes
   1.394 + * through this function so some applications may wish to modify it
   1.395 + * to avoid allocating a large strm->next_out buffer and copying into it.
   1.396 + * (See also read_buf()).
   1.397 + */
   1.398 +local void flush_pending(z_streamp strm)
   1.399 +{
   1.400 +    unsigned len = strm->state->pending;
   1.401 +
   1.402 +    if (len > strm->avail_out) len = strm->avail_out;
   1.403 +    if (len == 0) return;
   1.404 +
   1.405 +    zmemcpy(strm->next_out, strm->state->pending_out, len);
   1.406 +    strm->next_out  += len;
   1.407 +    strm->state->pending_out  += len;
   1.408 +    strm->total_out += len;
   1.409 +    strm->avail_out  -= len;
   1.410 +    strm->state->pending -= len;
   1.411 +    if (strm->state->pending == 0) {
   1.412 +        strm->state->pending_out = strm->state->pending_buf;
   1.413 +    }
   1.414 +}
   1.415 +
   1.416 +/* ========================================================================= */
   1.417 +int ZEXPORT deflate (z_streamp strm, int flush)
   1.418 +{
   1.419 +    int old_flush; /* value of flush param for previous deflate call */
   1.420 +    deflate_state *s;
   1.421 +
   1.422 +    if (strm == Z_NULL || strm->state == Z_NULL ||
   1.423 +	flush > Z_FINISH || flush < 0) {
   1.424 +        return Z_STREAM_ERROR;
   1.425 +    }
   1.426 +    s = strm->state;
   1.427 +
   1.428 +    if (strm->next_out == Z_NULL ||
   1.429 +        (strm->next_in == Z_NULL && strm->avail_in != 0) ||
   1.430 +	(s->status == FINISH_STATE && flush != Z_FINISH)) {
   1.431 +        ERR_RETURN(strm, Z_STREAM_ERROR);
   1.432 +    }
   1.433 +    if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
   1.434 +
   1.435 +    s->strm = strm; /* just in case */
   1.436 +    old_flush = s->last_flush;
   1.437 +    s->last_flush = flush;
   1.438 +
   1.439 +    /* Write the zlib header */
   1.440 +    if (s->status == INIT_STATE) {
   1.441 +
   1.442 +        uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
   1.443 +        uInt level_flags = (s->level-1) >> 1;
   1.444 +
   1.445 +        if (level_flags > 3) level_flags = 3;
   1.446 +        header |= (level_flags << 6);
   1.447 +	if (s->strstart != 0) header |= PRESET_DICT;
   1.448 +        header += 31 - (header % 31);
   1.449 +
   1.450 +        s->status = BUSY_STATE;
   1.451 +        putShortMSB(s, header);
   1.452 +
   1.453 +	/* Save the adler32 of the preset dictionary: */
   1.454 +	if (s->strstart != 0) {
   1.455 +	    putShortMSB(s, (uInt)(strm->adler >> 16));
   1.456 +	    putShortMSB(s, (uInt)(strm->adler & 0xffff));
   1.457 +	}
   1.458 +	strm->adler = 1L;
   1.459 +    }
   1.460 +
   1.461 +    /* Flush as much pending output as possible */
   1.462 +    if (s->pending != 0) {
   1.463 +        flush_pending(strm);
   1.464 +        if (strm->avail_out == 0) {
   1.465 +	    /* Since avail_out is 0, deflate will be called again with
   1.466 +	     * more output space, but possibly with both pending and
   1.467 +	     * avail_in equal to zero. There won't be anything to do,
   1.468 +	     * but this is not an error situation so make sure we
   1.469 +	     * return OK instead of BUF_ERROR at next call of deflate:
   1.470 +             */
   1.471 +	    s->last_flush = -1;
   1.472 +	    return Z_OK;
   1.473 +	}
   1.474 +
   1.475 +    /* Make sure there is something to do and avoid duplicate consecutive
   1.476 +     * flushes. For repeated and useless calls with Z_FINISH, we keep
   1.477 +     * returning Z_STREAM_END instead of Z_BUFF_ERROR.
   1.478 +     */
   1.479 +    } else if (strm->avail_in == 0 && flush <= old_flush &&
   1.480 +	       flush != Z_FINISH) {
   1.481 +        ERR_RETURN(strm, Z_BUF_ERROR);
   1.482 +    }
   1.483 +
   1.484 +    /* User must not provide more input after the first FINISH: */
   1.485 +    if (s->status == FINISH_STATE && strm->avail_in != 0) {
   1.486 +        ERR_RETURN(strm, Z_BUF_ERROR);
   1.487 +    }
   1.488 +
   1.489 +    /* Start a new block or continue the current one.
   1.490 +     */
   1.491 +    if (strm->avail_in != 0 || s->lookahead != 0 ||
   1.492 +        (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
   1.493 +        block_state bstate;
   1.494 +
   1.495 +	bstate = (*(configuration_table[s->level].func))(s, flush);
   1.496 +
   1.497 +        if (bstate == finish_started || bstate == finish_done) {
   1.498 +            s->status = FINISH_STATE;
   1.499 +        }
   1.500 +        if (bstate == need_more || bstate == finish_started) {
   1.501 +	    if (strm->avail_out == 0) {
   1.502 +	        s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
   1.503 +	    }
   1.504 +	    return Z_OK;
   1.505 +	    /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
   1.506 +	     * of deflate should use the same flush parameter to make sure
   1.507 +	     * that the flush is complete. So we don't have to output an
   1.508 +	     * empty block here, this will be done at next call. This also
   1.509 +	     * ensures that for a very small output buffer, we emit at most
   1.510 +	     * one empty block.
   1.511 +	     */
   1.512 +	}
   1.513 +        if (bstate == block_done) {
   1.514 +            if (flush == Z_PARTIAL_FLUSH) {
   1.515 +                _tr_align(s);
   1.516 +            } else { /* FULL_FLUSH or SYNC_FLUSH */
   1.517 +                _tr_stored_block(s, (char*)0, 0L, 0);
   1.518 +                /* For a full flush, this empty block will be recognized
   1.519 +                 * as a special marker by inflate_sync().
   1.520 +                 */
   1.521 +                if (flush == Z_FULL_FLUSH) {
   1.522 +                    CLEAR_HASH(s);             /* forget history */
   1.523 +                }
   1.524 +            }
   1.525 +            flush_pending(strm);
   1.526 +	    if (strm->avail_out == 0) {
   1.527 +	      s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
   1.528 +	      return Z_OK;
   1.529 +	    }
   1.530 +        }
   1.531 +    }
   1.532 +    Assert(strm->avail_out > 0, "bug2");
   1.533 +
   1.534 +    if (flush != Z_FINISH) return Z_OK;
   1.535 +    if (s->noheader) return Z_STREAM_END;
   1.536 +
   1.537 +    /* Write the zlib trailer (adler32) */
   1.538 +    putShortMSB(s, (uInt)(strm->adler >> 16));
   1.539 +    putShortMSB(s, (uInt)(strm->adler & 0xffff));
   1.540 +    flush_pending(strm);
   1.541 +    /* If avail_out is zero, the application will call deflate again
   1.542 +     * to flush the rest.
   1.543 +     */
   1.544 +    s->noheader = -1; /* write the trailer only once! */
   1.545 +    return s->pending != 0 ? Z_OK : Z_STREAM_END;
   1.546 +}
   1.547 +
   1.548 +/* ========================================================================= */
   1.549 +int ZEXPORT deflateEnd (z_streamp strm)
   1.550 +{
   1.551 +    int status;
   1.552 +
   1.553 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
   1.554 +
   1.555 +    status = strm->state->status;
   1.556 +    if (status != INIT_STATE && status != BUSY_STATE &&
   1.557 +	status != FINISH_STATE) {
   1.558 +      return Z_STREAM_ERROR;
   1.559 +    }
   1.560 +
   1.561 +    /* Deallocate in reverse order of allocations: */
   1.562 +    TRY_FREE(strm, strm->state->pending_buf);
   1.563 +    TRY_FREE(strm, strm->state->head);
   1.564 +    TRY_FREE(strm, strm->state->prev);
   1.565 +    TRY_FREE(strm, strm->state->window);
   1.566 +
   1.567 +    ZFREE(strm, strm->state);
   1.568 +    strm->state = Z_NULL;
   1.569 +
   1.570 +    return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
   1.571 +}
   1.572 +
   1.573 +/* =========================================================================
   1.574 + * Copy the source state to the destination state.
   1.575 + * To simplify the source, this is not supported for 16-bit MSDOS (which
   1.576 + * doesn't have enough memory anyway to duplicate compression states).
   1.577 + */
   1.578 +int ZEXPORT deflateCopy (z_streamp dest, z_streamp source)
   1.579 +{
   1.580 +#ifdef MAXSEG_64K
   1.581 +    return Z_STREAM_ERROR;
   1.582 +#else
   1.583 +    deflate_state *ds;
   1.584 +    deflate_state *ss;
   1.585 +    ushf *overlay;
   1.586 +
   1.587 +
   1.588 +    if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
   1.589 +        return Z_STREAM_ERROR;
   1.590 +    }
   1.591 +
   1.592 +    ss = source->state;
   1.593 +
   1.594 +    *dest = *source;
   1.595 +
   1.596 +    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
   1.597 +    if (ds == Z_NULL) return Z_MEM_ERROR;
   1.598 +    dest->state = (struct internal_state FAR *) ds;
   1.599 +    *ds = *ss;
   1.600 +    ds->strm = dest;
   1.601 +
   1.602 +    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
   1.603 +    ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
   1.604 +    ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
   1.605 +    overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
   1.606 +    ds->pending_buf = (uchf *) overlay;
   1.607 +
   1.608 +    if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
   1.609 +        ds->pending_buf == Z_NULL) {
   1.610 +        deflateEnd (dest);
   1.611 +        return Z_MEM_ERROR;
   1.612 +    }
   1.613 +    /* following zmemcpy do not work for 16-bit MSDOS */
   1.614 +    zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
   1.615 +    zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
   1.616 +    zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
   1.617 +    zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
   1.618 +
   1.619 +    ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
   1.620 +    ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
   1.621 +    ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
   1.622 +
   1.623 +    ds->l_desc.dyn_tree = ds->dyn_ltree;
   1.624 +    ds->d_desc.dyn_tree = ds->dyn_dtree;
   1.625 +    ds->bl_desc.dyn_tree = ds->bl_tree;
   1.626 +
   1.627 +    return Z_OK;
   1.628 +#endif
   1.629 +}
   1.630 +
   1.631 +/* ===========================================================================
   1.632 + * Read a new buffer from the current input stream, update the adler32
   1.633 + * and total number of bytes read.  All deflate() input goes through
   1.634 + * this function so some applications may wish to modify it to avoid
   1.635 + * allocating a large strm->next_in buffer and copying from it.
   1.636 + * (See also flush_pending()).
   1.637 + */
   1.638 +local int read_buf(z_streamp strm, Bytef *buf, unsigned size)
   1.639 +{
   1.640 +    unsigned len = strm->avail_in;
   1.641 +
   1.642 +    if (len > size) len = size;
   1.643 +    if (len == 0) return 0;
   1.644 +
   1.645 +    strm->avail_in  -= len;
   1.646 +
   1.647 +    if (!strm->state->noheader) {
   1.648 +        strm->adler = adler32(strm->adler, strm->next_in, len);
   1.649 +    }
   1.650 +    zmemcpy(buf, strm->next_in, len);
   1.651 +    strm->next_in  += len;
   1.652 +    strm->total_in += len;
   1.653 +
   1.654 +    return (int)len;
   1.655 +}
   1.656 +
   1.657 +/* ===========================================================================
   1.658 + * Initialize the "longest match" routines for a new zlib stream
   1.659 + */
   1.660 +local void lm_init (deflate_state *s)
   1.661 +{
   1.662 +    s->window_size = (ulg)2L*s->w_size;
   1.663 +
   1.664 +    CLEAR_HASH(s);
   1.665 +
   1.666 +    /* Set the default configuration parameters:
   1.667 +     */
   1.668 +    s->max_lazy_match   = configuration_table[s->level].max_lazy;
   1.669 +    s->good_match       = configuration_table[s->level].good_length;
   1.670 +    s->nice_match       = configuration_table[s->level].nice_length;
   1.671 +    s->max_chain_length = configuration_table[s->level].max_chain;
   1.672 +
   1.673 +    s->strstart = 0;
   1.674 +    s->block_start = 0L;
   1.675 +    s->lookahead = 0;
   1.676 +    s->match_length = s->prev_length = MIN_MATCH-1;
   1.677 +    s->match_available = 0;
   1.678 +    s->ins_h = 0;
   1.679 +#ifdef ASMV
   1.680 +    match_init(); /* initialize the asm code */
   1.681 +#endif
   1.682 +}
   1.683 +
   1.684 +/* ===========================================================================
   1.685 + * Set match_start to the longest match starting at the given string and
   1.686 + * return its length. Matches shorter or equal to prev_length are discarded,
   1.687 + * in which case the result is equal to prev_length and match_start is
   1.688 + * garbage.
   1.689 + * IN assertions: cur_match is the head of the hash chain for the current
   1.690 + *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
   1.691 + * OUT assertion: the match length is not greater than s->lookahead.
   1.692 + */
   1.693 +#ifndef ASMV
   1.694 +/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
   1.695 + * match.S. The code will be functionally equivalent.
   1.696 + */
   1.697 +#ifndef FASTEST
   1.698 +local uInt longest_match(deflate_state *s, IPos cur_match)
   1.699 +/* IPos cur_match;         current match */
   1.700 +{
   1.701 +    unsigned chain_length = s->max_chain_length;/* max hash chain length */
   1.702 +    register Bytef *scan = s->window + s->strstart; /* current string */
   1.703 +    register Bytef *match;                       /* matched string */
   1.704 +    register int len;                           /* length of current match */
   1.705 +    int best_len = s->prev_length;              /* best match length so far */
   1.706 +    int nice_match = s->nice_match;             /* stop if match long enough */
   1.707 +    IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
   1.708 +        s->strstart - (IPos)MAX_DIST(s) : NIL;
   1.709 +    /* Stop when cur_match becomes <= limit. To simplify the code,
   1.710 +     * we prevent matches with the string of window index 0.
   1.711 +     */
   1.712 +    Posf *prev = s->prev;
   1.713 +    uInt wmask = s->w_mask;
   1.714 +
   1.715 +#ifdef UNALIGNED_OK
   1.716 +    /* Compare two bytes at a time. Note: this is not always beneficial.
   1.717 +     * Try with and without -DUNALIGNED_OK to check.
   1.718 +     */
   1.719 +    register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
   1.720 +    register ush scan_start = *(ushf*)scan;
   1.721 +    register ush scan_end   = *(ushf*)(scan+best_len-1);
   1.722 +#else
   1.723 +    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
   1.724 +    register Byte scan_end1  = scan[best_len-1];
   1.725 +    register Byte scan_end   = scan[best_len];
   1.726 +#endif
   1.727 +
   1.728 +    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
   1.729 +     * It is easy to get rid of this optimization if necessary.
   1.730 +     */
   1.731 +    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
   1.732 +
   1.733 +    /* Do not waste too much time if we already have a good match: */
   1.734 +    if (s->prev_length >= s->good_match) {
   1.735 +        chain_length >>= 2;
   1.736 +    }
   1.737 +    /* Do not look for matches beyond the end of the input. This is necessary
   1.738 +     * to make deflate deterministic.
   1.739 +     */
   1.740 +    if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
   1.741 +
   1.742 +    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
   1.743 +
   1.744 +    do {
   1.745 +        Assert(cur_match < s->strstart, "no future");
   1.746 +        match = s->window + cur_match;
   1.747 +
   1.748 +        /* Skip to next match if the match length cannot increase
   1.749 +         * or if the match length is less than 2:
   1.750 +         */
   1.751 +#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
   1.752 +        /* This code assumes sizeof(unsigned short) == 2. Do not use
   1.753 +         * UNALIGNED_OK if your compiler uses a different size.
   1.754 +         */
   1.755 +        if (*(ushf*)(match+best_len-1) != scan_end ||
   1.756 +            *(ushf*)match != scan_start) continue;
   1.757 +
   1.758 +        /* It is not necessary to compare scan[2] and match[2] since they are
   1.759 +         * always equal when the other bytes match, given that the hash keys
   1.760 +         * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
   1.761 +         * strstart+3, +5, ... up to strstart+257. We check for insufficient
   1.762 +         * lookahead only every 4th comparison; the 128th check will be made
   1.763 +         * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
   1.764 +         * necessary to put more guard bytes at the end of the window, or
   1.765 +         * to check more often for insufficient lookahead.
   1.766 +         */
   1.767 +        Assert(scan[2] == match[2], "scan[2]?");
   1.768 +        scan++, match++;
   1.769 +        do {
   1.770 +        } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
   1.771 +                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
   1.772 +                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
   1.773 +                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
   1.774 +                 scan < strend);
   1.775 +        /* The funny "do {}" generates better code on most compilers */
   1.776 +
   1.777 +        /* Here, scan <= window+strstart+257 */
   1.778 +        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
   1.779 +        if (*scan == *match) scan++;
   1.780 +
   1.781 +        len = (MAX_MATCH - 1) - (int)(strend-scan);
   1.782 +        scan = strend - (MAX_MATCH-1);
   1.783 +
   1.784 +#else /* UNALIGNED_OK */
   1.785 +
   1.786 +        if (match[best_len]   != scan_end  ||
   1.787 +            match[best_len-1] != scan_end1 ||
   1.788 +            *match            != *scan     ||
   1.789 +            *++match          != scan[1])      continue;
   1.790 +
   1.791 +        /* The check at best_len-1 can be removed because it will be made
   1.792 +         * again later. (This heuristic is not always a win.)
   1.793 +         * It is not necessary to compare scan[2] and match[2] since they
   1.794 +         * are always equal when the other bytes match, given that
   1.795 +         * the hash keys are equal and that HASH_BITS >= 8.
   1.796 +         */
   1.797 +        scan += 2, match++;
   1.798 +        Assert(*scan == *match, "match[2]?");
   1.799 +
   1.800 +        /* We check for insufficient lookahead only every 8th comparison;
   1.801 +         * the 256th check will be made at strstart+258.
   1.802 +         */
   1.803 +        do {
   1.804 +        } while (*++scan == *++match && *++scan == *++match &&
   1.805 +                 *++scan == *++match && *++scan == *++match &&
   1.806 +                 *++scan == *++match && *++scan == *++match &&
   1.807 +                 *++scan == *++match && *++scan == *++match &&
   1.808 +                 scan < strend);
   1.809 +
   1.810 +        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
   1.811 +
   1.812 +        len = MAX_MATCH - (int)(strend - scan);
   1.813 +        scan = strend - MAX_MATCH;
   1.814 +
   1.815 +#endif /* UNALIGNED_OK */
   1.816 +
   1.817 +        if (len > best_len) {
   1.818 +            s->match_start = cur_match;
   1.819 +            best_len = len;
   1.820 +            if (len >= nice_match) break;
   1.821 +#ifdef UNALIGNED_OK
   1.822 +            scan_end = *(ushf*)(scan+best_len-1);
   1.823 +#else
   1.824 +            scan_end1  = scan[best_len-1];
   1.825 +            scan_end   = scan[best_len];
   1.826 +#endif
   1.827 +        }
   1.828 +    } while ((cur_match = prev[cur_match & wmask]) > limit
   1.829 +             && --chain_length != 0);
   1.830 +
   1.831 +    if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
   1.832 +    return s->lookahead;
   1.833 +}
   1.834 +
   1.835 +#else /* FASTEST */
   1.836 +/* ---------------------------------------------------------------------------
   1.837 + * Optimized version for level == 1 only
   1.838 + */
   1.839 +local uInt longest_match(deflate_state *s, IPos cur_match)
   1.840 +/*  IPos cur_match      current match. */
   1.841 +{
   1.842 +    register Bytef *scan = s->window + s->strstart; /* current string */
   1.843 +    register Bytef *match;                       /* matched string */
   1.844 +    register int len;                           /* length of current match */
   1.845 +    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
   1.846 +
   1.847 +    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
   1.848 +     * It is easy to get rid of this optimization if necessary.
   1.849 +     */
   1.850 +    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
   1.851 +
   1.852 +    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
   1.853 +
   1.854 +    Assert(cur_match < s->strstart, "no future");
   1.855 +
   1.856 +    match = s->window + cur_match;
   1.857 +
   1.858 +    /* Return failure if the match length is less than 2:
   1.859 +     */
   1.860 +    if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
   1.861 +
   1.862 +    /* The check at best_len-1 can be removed because it will be made
   1.863 +     * again later. (This heuristic is not always a win.)
   1.864 +     * It is not necessary to compare scan[2] and match[2] since they
   1.865 +     * are always equal when the other bytes match, given that
   1.866 +     * the hash keys are equal and that HASH_BITS >= 8.
   1.867 +     */
   1.868 +    scan += 2, match += 2;
   1.869 +    Assert(*scan == *match, "match[2]?");
   1.870 +
   1.871 +    /* We check for insufficient lookahead only every 8th comparison;
   1.872 +     * the 256th check will be made at strstart+258.
   1.873 +     */
   1.874 +    do {
   1.875 +    } while (*++scan == *++match && *++scan == *++match &&
   1.876 +	     *++scan == *++match && *++scan == *++match &&
   1.877 +	     *++scan == *++match && *++scan == *++match &&
   1.878 +	     *++scan == *++match && *++scan == *++match &&
   1.879 +	     scan < strend);
   1.880 +
   1.881 +    Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
   1.882 +
   1.883 +    len = MAX_MATCH - (int)(strend - scan);
   1.884 +
   1.885 +    if (len < MIN_MATCH) return MIN_MATCH - 1;
   1.886 +
   1.887 +    s->match_start = cur_match;
   1.888 +    return len <= s->lookahead ? len : s->lookahead;
   1.889 +}
   1.890 +#endif /* FASTEST */
   1.891 +#endif /* ASMV */
   1.892 +
   1.893 +#ifdef DEBUG
   1.894 +/* ===========================================================================
   1.895 + * Check that the match at match_start is indeed a match.
   1.896 + */
   1.897 +local void check_match(deflate_state *s, IPos start, IPos match, int length)
   1.898 +{
   1.899 +    /* check that the match is indeed a match */
   1.900 +    if (zmemcmp(s->window + match,
   1.901 +                s->window + start, length) != EQUAL) {
   1.902 +        fprintf(stderr, " start %u, match %u, length %d\n",
   1.903 +		start, match, length);
   1.904 +        do {
   1.905 +	    fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
   1.906 +	} while (--length != 0);
   1.907 +        z_error("invalid match");
   1.908 +    }
   1.909 +    if (z_verbose > 1) {
   1.910 +        fprintf(stderr,"\\[%d,%d]", start-match, length);
   1.911 +        do { putc(s->window[start++], stderr); } while (--length != 0);
   1.912 +    }
   1.913 +}
   1.914 +#else
   1.915 +#  define check_match(s, start, match, length)
   1.916 +#endif
   1.917 +
   1.918 +/* ===========================================================================
   1.919 + * Fill the window when the lookahead becomes insufficient.
   1.920 + * Updates strstart and lookahead.
   1.921 + *
   1.922 + * IN assertion: lookahead < MIN_LOOKAHEAD
   1.923 + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
   1.924 + *    At least one byte has been read, or avail_in == 0; reads are
   1.925 + *    performed for at least two bytes (required for the zip translate_eol
   1.926 + *    option -- not supported here).
   1.927 + */
   1.928 +local void fill_window(deflate_state *s)
   1.929 +{
   1.930 +    register unsigned n, m;
   1.931 +    register Posf *p;
   1.932 +    unsigned more;    /* Amount of free space at the end of the window. */
   1.933 +    uInt wsize = s->w_size;
   1.934 +
   1.935 +    do {
   1.936 +        more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
   1.937 +
   1.938 +        /* Deal with !@#$% 64K limit: */
   1.939 +        if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
   1.940 +            more = wsize;
   1.941 +
   1.942 +        } else if (more == (unsigned)(-1)) {
   1.943 +            /* Very unlikely, but possible on 16 bit machine if strstart == 0
   1.944 +             * and lookahead == 1 (input done one byte at time)
   1.945 +             */
   1.946 +            more--;
   1.947 +
   1.948 +        /* If the window is almost full and there is insufficient lookahead,
   1.949 +         * move the upper half to the lower one to make room in the upper half.
   1.950 +         */
   1.951 +        } else if (s->strstart >= wsize+MAX_DIST(s)) {
   1.952 +
   1.953 +            zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
   1.954 +            s->match_start -= wsize;
   1.955 +            s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
   1.956 +            s->block_start -= (long) wsize;
   1.957 +
   1.958 +            /* Slide the hash table (could be avoided with 32 bit values
   1.959 +               at the expense of memory usage). We slide even when level == 0
   1.960 +               to keep the hash table consistent if we switch back to level > 0
   1.961 +               later. (Using level 0 permanently is not an optimal usage of
   1.962 +               zlib, so we don't care about this pathological case.)
   1.963 +             */
   1.964 +	    n = s->hash_size;
   1.965 +	    p = &s->head[n];
   1.966 +	    do {
   1.967 +		m = *--p;
   1.968 +		*p = (Pos)(m >= wsize ? m-wsize : NIL);
   1.969 +	    } while (--n);
   1.970 +
   1.971 +	    n = wsize;
   1.972 +#ifndef FASTEST
   1.973 +	    p = &s->prev[n];
   1.974 +	    do {
   1.975 +		m = *--p;
   1.976 +		*p = (Pos)(m >= wsize ? m-wsize : NIL);
   1.977 +		/* If n is not on any hash chain, prev[n] is garbage but
   1.978 +		 * its value will never be used.
   1.979 +		 */
   1.980 +	    } while (--n);
   1.981 +#endif
   1.982 +            more += wsize;
   1.983 +        }
   1.984 +        if (s->strm->avail_in == 0) return;
   1.985 +
   1.986 +        /* If there was no sliding:
   1.987 +         *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
   1.988 +         *    more == window_size - lookahead - strstart
   1.989 +         * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
   1.990 +         * => more >= window_size - 2*WSIZE + 2
   1.991 +         * In the BIG_MEM or MMAP case (not yet supported),
   1.992 +         *   window_size == input_size + MIN_LOOKAHEAD  &&
   1.993 +         *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
   1.994 +         * Otherwise, window_size == 2*WSIZE so more >= 2.
   1.995 +         * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
   1.996 +         */
   1.997 +        Assert(more >= 2, "more < 2");
   1.998 +
   1.999 +        n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
  1.1000 +        s->lookahead += n;
  1.1001 +
  1.1002 +        /* Initialize the hash value now that we have some input: */
  1.1003 +        if (s->lookahead >= MIN_MATCH) {
  1.1004 +            s->ins_h = s->window[s->strstart];
  1.1005 +            UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  1.1006 +#if MIN_MATCH != 3
  1.1007 +            Call UPDATE_HASH() MIN_MATCH-3 more times
  1.1008 +#endif
  1.1009 +        }
  1.1010 +        /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  1.1011 +         * but this is not important since only literal bytes will be emitted.
  1.1012 +         */
  1.1013 +
  1.1014 +    } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  1.1015 +}
  1.1016 +
  1.1017 +/* ===========================================================================
  1.1018 + * Flush the current block, with given end-of-file flag.
  1.1019 + * IN assertion: strstart is set to the end of the current match.
  1.1020 + */
  1.1021 +#define FLUSH_BLOCK_ONLY(s, eof) { \
  1.1022 +   _tr_flush_block(s, (s->block_start >= 0L ? \
  1.1023 +                   (charf *)&s->window[(unsigned)s->block_start] : \
  1.1024 +                   (charf *)Z_NULL), \
  1.1025 +		(ulg)((long)s->strstart - s->block_start), \
  1.1026 +		(eof)); \
  1.1027 +   s->block_start = s->strstart; \
  1.1028 +   flush_pending(s->strm); \
  1.1029 +   Tracev((stderr,"[FLUSH]")); \
  1.1030 +}
  1.1031 +
  1.1032 +/* Same but force premature exit if necessary. */
  1.1033 +#define FLUSH_BLOCK(s, eof) { \
  1.1034 +   FLUSH_BLOCK_ONLY(s, eof); \
  1.1035 +   if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
  1.1036 +}
  1.1037 +
  1.1038 +/* ===========================================================================
  1.1039 + * Copy without compression as much as possible from the input stream, return
  1.1040 + * the current block state.
  1.1041 + * This function does not insert new strings in the dictionary since
  1.1042 + * uncompressible data is probably not useful. This function is used
  1.1043 + * only for the level=0 compression option.
  1.1044 + * NOTE: this function should be optimized to avoid extra copying from
  1.1045 + * window to pending_buf.
  1.1046 + */
  1.1047 +local block_state deflate_stored(deflate_state *s, int flush)
  1.1048 +{
  1.1049 +    /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1.1050 +     * to pending_buf_size, and each stored block has a 5 byte header:
  1.1051 +     */
  1.1052 +    ulg max_block_size = 0xffff;
  1.1053 +    ulg max_start;
  1.1054 +
  1.1055 +    if (max_block_size > s->pending_buf_size - 5) {
  1.1056 +        max_block_size = s->pending_buf_size - 5;
  1.1057 +    }
  1.1058 +
  1.1059 +    /* Copy as much as possible from input to output: */
  1.1060 +    for (;;) {
  1.1061 +        /* Fill the window as much as possible: */
  1.1062 +        if (s->lookahead <= 1) {
  1.1063 +
  1.1064 +            Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  1.1065 +		   s->block_start >= (long)s->w_size, "slide too late");
  1.1066 +
  1.1067 +            fill_window(s);
  1.1068 +            if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
  1.1069 +
  1.1070 +            if (s->lookahead == 0) break; /* flush the current block */
  1.1071 +        }
  1.1072 +	Assert(s->block_start >= 0L, "block gone");
  1.1073 +
  1.1074 +	s->strstart += s->lookahead;
  1.1075 +	s->lookahead = 0;
  1.1076 +
  1.1077 +	/* Emit a stored block if pending_buf will be full: */
  1.1078 + 	max_start = s->block_start + max_block_size;
  1.1079 +        if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
  1.1080 +	    /* strstart == 0 is possible when wraparound on 16-bit machine */
  1.1081 +	    s->lookahead = (uInt)(s->strstart - max_start);
  1.1082 +	    s->strstart = (uInt)max_start;
  1.1083 +            FLUSH_BLOCK(s, 0);
  1.1084 +	}
  1.1085 +	/* Flush if we may have to slide, otherwise block_start may become
  1.1086 +         * negative and the data will be gone:
  1.1087 +         */
  1.1088 +        if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
  1.1089 +            FLUSH_BLOCK(s, 0);
  1.1090 +	}
  1.1091 +    }
  1.1092 +    FLUSH_BLOCK(s, flush == Z_FINISH);
  1.1093 +    return flush == Z_FINISH ? finish_done : block_done;
  1.1094 +}
  1.1095 +
  1.1096 +/* ===========================================================================
  1.1097 + * Compress as much as possible from the input stream, return the current
  1.1098 + * block state.
  1.1099 + * This function does not perform lazy evaluation of matches and inserts
  1.1100 + * new strings in the dictionary only for unmatched strings or for short
  1.1101 + * matches. It is used only for the fast compression options.
  1.1102 + */
  1.1103 +local block_state deflate_fast(deflate_state *s, int flush)
  1.1104 +{
  1.1105 +    IPos hash_head = NIL; /* head of the hash chain */
  1.1106 +    int bflush;           /* set if current block must be flushed */
  1.1107 +
  1.1108 +    for (;;) {
  1.1109 +        /* Make sure that we always have enough lookahead, except
  1.1110 +         * at the end of the input file. We need MAX_MATCH bytes
  1.1111 +         * for the next match, plus MIN_MATCH bytes to insert the
  1.1112 +         * string following the next match.
  1.1113 +         */
  1.1114 +        if (s->lookahead < MIN_LOOKAHEAD) {
  1.1115 +            fill_window(s);
  1.1116 +            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1.1117 +	        return need_more;
  1.1118 +	    }
  1.1119 +            if (s->lookahead == 0) break; /* flush the current block */
  1.1120 +        }
  1.1121 +
  1.1122 +        /* Insert the string window[strstart .. strstart+2] in the
  1.1123 +         * dictionary, and set hash_head to the head of the hash chain:
  1.1124 +         */
  1.1125 +        if (s->lookahead >= MIN_MATCH) {
  1.1126 +            INSERT_STRING(s, s->strstart, hash_head);
  1.1127 +        }
  1.1128 +
  1.1129 +        /* Find the longest match, discarding those <= prev_length.
  1.1130 +         * At this point we have always match_length < MIN_MATCH
  1.1131 +         */
  1.1132 +        if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
  1.1133 +            /* To simplify the code, we prevent matches with the string
  1.1134 +             * of window index 0 (in particular we have to avoid a match
  1.1135 +             * of the string with itself at the start of the input file).
  1.1136 +             */
  1.1137 +            if (s->strategy != Z_HUFFMAN_ONLY) {
  1.1138 +                s->match_length = longest_match (s, hash_head);
  1.1139 +            }
  1.1140 +            /* longest_match() sets match_start */
  1.1141 +        }
  1.1142 +        if (s->match_length >= MIN_MATCH) {
  1.1143 +            check_match(s, s->strstart, s->match_start, s->match_length);
  1.1144 +
  1.1145 +            _tr_tally_dist(s, s->strstart - s->match_start,
  1.1146 +                           s->match_length - MIN_MATCH, bflush);
  1.1147 +
  1.1148 +            s->lookahead -= s->match_length;
  1.1149 +
  1.1150 +            /* Insert new strings in the hash table only if the match length
  1.1151 +             * is not too large. This saves time but degrades compression.
  1.1152 +             */
  1.1153 +#ifndef FASTEST
  1.1154 +            if (s->match_length <= s->max_insert_length &&
  1.1155 +                s->lookahead >= MIN_MATCH) {
  1.1156 +                s->match_length--; /* string at strstart already in hash table */
  1.1157 +                do {
  1.1158 +                    s->strstart++;
  1.1159 +                    INSERT_STRING(s, s->strstart, hash_head);
  1.1160 +                    /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1.1161 +                     * always MIN_MATCH bytes ahead.
  1.1162 +                     */
  1.1163 +                } while (--s->match_length != 0);
  1.1164 +                s->strstart++; 
  1.1165 +            } else
  1.1166 +#endif
  1.1167 +	    {
  1.1168 +                s->strstart += s->match_length;
  1.1169 +                s->match_length = 0;
  1.1170 +                s->ins_h = s->window[s->strstart];
  1.1171 +                UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  1.1172 +#if MIN_MATCH != 3
  1.1173 +                Call UPDATE_HASH() MIN_MATCH-3 more times
  1.1174 +#endif
  1.1175 +                /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1.1176 +                 * matter since it will be recomputed at next deflate call.
  1.1177 +                 */
  1.1178 +            }
  1.1179 +        } else {
  1.1180 +            /* No match, output a literal byte */
  1.1181 +            Tracevv((stderr,"%c", s->window[s->strstart]));
  1.1182 +            _tr_tally_lit (s, s->window[s->strstart], bflush);
  1.1183 +            s->lookahead--;
  1.1184 +            s->strstart++; 
  1.1185 +        }
  1.1186 +        if (bflush) FLUSH_BLOCK(s, 0);
  1.1187 +    }
  1.1188 +    FLUSH_BLOCK(s, flush == Z_FINISH);
  1.1189 +    return flush == Z_FINISH ? finish_done : block_done;
  1.1190 +}
  1.1191 +
  1.1192 +/* ===========================================================================
  1.1193 + * Same as above, but achieves better compression. We use a lazy
  1.1194 + * evaluation for matches: a match is finally adopted only if there is
  1.1195 + * no better match at the next window position.
  1.1196 + */
  1.1197 +local block_state deflate_slow(deflate_state *s, int flush)
  1.1198 +{
  1.1199 +    IPos hash_head = NIL;    /* head of hash chain */
  1.1200 +    int bflush;              /* set if current block must be flushed */
  1.1201 +
  1.1202 +    /* Process the input block. */
  1.1203 +    for (;;) {
  1.1204 +        /* Make sure that we always have enough lookahead, except
  1.1205 +         * at the end of the input file. We need MAX_MATCH bytes
  1.1206 +         * for the next match, plus MIN_MATCH bytes to insert the
  1.1207 +         * string following the next match.
  1.1208 +         */
  1.1209 +        if (s->lookahead < MIN_LOOKAHEAD) {
  1.1210 +            fill_window(s);
  1.1211 +            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1.1212 +	        return need_more;
  1.1213 +	    }
  1.1214 +            if (s->lookahead == 0) break; /* flush the current block */
  1.1215 +        }
  1.1216 +
  1.1217 +        /* Insert the string window[strstart .. strstart+2] in the
  1.1218 +         * dictionary, and set hash_head to the head of the hash chain:
  1.1219 +         */
  1.1220 +        if (s->lookahead >= MIN_MATCH) {
  1.1221 +            INSERT_STRING(s, s->strstart, hash_head);
  1.1222 +        }
  1.1223 +
  1.1224 +        /* Find the longest match, discarding those <= prev_length.
  1.1225 +         */
  1.1226 +        s->prev_length = s->match_length, s->prev_match = s->match_start;
  1.1227 +        s->match_length = MIN_MATCH-1;
  1.1228 +
  1.1229 +        if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
  1.1230 +            s->strstart - hash_head <= MAX_DIST(s)) {
  1.1231 +            /* To simplify the code, we prevent matches with the string
  1.1232 +             * of window index 0 (in particular we have to avoid a match
  1.1233 +             * of the string with itself at the start of the input file).
  1.1234 +             */
  1.1235 +            if (s->strategy != Z_HUFFMAN_ONLY) {
  1.1236 +                s->match_length = longest_match (s, hash_head);
  1.1237 +            }
  1.1238 +            /* longest_match() sets match_start */
  1.1239 +
  1.1240 +            if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
  1.1241 +                 (s->match_length == MIN_MATCH &&
  1.1242 +                  s->strstart - s->match_start > TOO_FAR))) {
  1.1243 +
  1.1244 +                /* If prev_match is also MIN_MATCH, match_start is garbage
  1.1245 +                 * but we will ignore the current match anyway.
  1.1246 +                 */
  1.1247 +                s->match_length = MIN_MATCH-1;
  1.1248 +            }
  1.1249 +        }
  1.1250 +        /* If there was a match at the previous step and the current
  1.1251 +         * match is not better, output the previous match:
  1.1252 +         */
  1.1253 +        if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
  1.1254 +            uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
  1.1255 +            /* Do not insert strings in hash table beyond this. */
  1.1256 +
  1.1257 +            check_match(s, s->strstart-1, s->prev_match, s->prev_length);
  1.1258 +
  1.1259 +            _tr_tally_dist(s, s->strstart -1 - s->prev_match,
  1.1260 +			   s->prev_length - MIN_MATCH, bflush);
  1.1261 +
  1.1262 +            /* Insert in hash table all strings up to the end of the match.
  1.1263 +             * strstart-1 and strstart are already inserted. If there is not
  1.1264 +             * enough lookahead, the last two strings are not inserted in
  1.1265 +             * the hash table.
  1.1266 +             */
  1.1267 +            s->lookahead -= s->prev_length-1;
  1.1268 +            s->prev_length -= 2;
  1.1269 +            do {
  1.1270 +                if (++s->strstart <= max_insert) {
  1.1271 +                    INSERT_STRING(s, s->strstart, hash_head);
  1.1272 +                }
  1.1273 +            } while (--s->prev_length != 0);
  1.1274 +            s->match_available = 0;
  1.1275 +            s->match_length = MIN_MATCH-1;
  1.1276 +            s->strstart++;
  1.1277 +
  1.1278 +            if (bflush) FLUSH_BLOCK(s, 0);
  1.1279 +
  1.1280 +        } else if (s->match_available) {
  1.1281 +            /* If there was no match at the previous position, output a
  1.1282 +             * single literal. If there was a match but the current match
  1.1283 +             * is longer, truncate the previous match to a single literal.
  1.1284 +             */
  1.1285 +            Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1.1286 +	    _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  1.1287 +	    if (bflush) {
  1.1288 +                FLUSH_BLOCK_ONLY(s, 0);
  1.1289 +            }
  1.1290 +            s->strstart++;
  1.1291 +            s->lookahead--;
  1.1292 +            if (s->strm->avail_out == 0) return need_more;
  1.1293 +        } else {
  1.1294 +            /* There is no previous match to compare with, wait for
  1.1295 +             * the next step to decide.
  1.1296 +             */
  1.1297 +            s->match_available = 1;
  1.1298 +            s->strstart++;
  1.1299 +            s->lookahead--;
  1.1300 +        }
  1.1301 +    }
  1.1302 +    Assert (flush != Z_NO_FLUSH, "no flush?");
  1.1303 +    if (s->match_available) {
  1.1304 +        Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1.1305 +        _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  1.1306 +        s->match_available = 0;
  1.1307 +    }
  1.1308 +    FLUSH_BLOCK(s, flush == Z_FINISH);
  1.1309 +    return flush == Z_FINISH ? finish_done : block_done;
  1.1310 +}