1592N/A/*
1592N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1592N/A *
1592N/A * This code is free software; you can redistribute it and/or modify it
1592N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1592N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1592N/A *
1592N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1592N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1592N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1592N/A * version 2 for more details (a copy is included in the LICENSE file that
1592N/A * accompanied this code).
1592N/A *
1592N/A * You should have received a copy of the GNU General Public License version
1592N/A * 2 along with this work; if not, write to the Free Software Foundation,
1592N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1592N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1592N/A */
1592N/A
1592N/A/* deflate.c -- compress data using the deflation algorithm
1592N/A * Copyright (C) 1995-2005 Jean-loup Gailly.
1592N/A * For conditions of distribution and use, see copyright notice in zlib.h
1592N/A */
1592N/A
1592N/A/*
1592N/A * ALGORITHM
1592N/A *
1592N/A * The "deflation" process depends on being able to identify portions
1592N/A * of the input text which are identical to earlier input (within a
1592N/A * sliding window trailing behind the input currently being processed).
1592N/A *
1592N/A * The most straightforward technique turns out to be the fastest for
1592N/A * most input files: try all possible matches and select the longest.
1592N/A * The key feature of this algorithm is that insertions into the string
1592N/A * dictionary are very simple and thus fast, and deletions are avoided
1592N/A * completely. Insertions are performed at each input character, whereas
1592N/A * string matches are performed only when the previous match ends. So it
1592N/A * is preferable to spend more time in matches to allow very fast string
1592N/A * insertions and avoid deletions. The matching algorithm for small
1592N/A * strings is inspired from that of Rabin & Karp. A brute force approach
1592N/A * is used to find longer strings when a small match has been found.
1592N/A * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
1592N/A * (by Leonid Broukhis).
1592N/A * A previous version of this file used a more sophisticated algorithm
1592N/A * (by Fiala and Greene) which is guaranteed to run in linear amortized
1592N/A * time, but has a larger average cost, uses more memory and is patented.
1592N/A * However the F&G algorithm may be faster for some highly redundant
1592N/A * files if the parameter max_chain_length (described below) is too large.
1592N/A *
1592N/A * ACKNOWLEDGEMENTS
1592N/A *
1592N/A * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
1592N/A * I found it in 'freeze' written by Leonid Broukhis.
1592N/A * Thanks to many people for bug reports and testing.
1592N/A *
1592N/A * REFERENCES
1592N/A *
1592N/A * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
1592N/A * Available in http://www.ietf.org/rfc/rfc1951.txt
1592N/A *
1592N/A * A description of the Rabin and Karp algorithm is given in the book
1592N/A * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
1592N/A *
1592N/A * Fiala,E.R., and Greene,D.H.
1592N/A * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
1592N/A *
1592N/A */
1592N/A
1592N/A/* @(#) $Id$ */
1592N/A
1592N/A#include "deflate.h"
1592N/A
1592N/Aconst char deflate_copyright[] =
1592N/A " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
1592N/A/*
1592N/A If you use the zlib library in a product, an acknowledgment is welcome
1592N/A in the documentation of your product. If for some reason you cannot
1592N/A include such an acknowledgment, I would appreciate that you keep this
1592N/A copyright string in the executable of your product.
1592N/A */
1592N/A
1592N/A/* ===========================================================================
1592N/A * Function prototypes.
1592N/A */
1592N/Atypedef enum {
1592N/A need_more, /* block not completed, need more input or more output */
1592N/A block_done, /* block flush performed */
1592N/A finish_started, /* finish started, need only more output at next deflate */
1592N/A finish_done /* finish done, accept no more input or output */
1592N/A} block_state;
1592N/A
1592N/Atypedef block_state (*compress_func) OF((deflate_state *s, int flush));
1592N/A/* Compression function. Returns the block state after the call. */
1592N/A
1592N/Alocal void fill_window OF((deflate_state *s));
1592N/Alocal block_state deflate_stored OF((deflate_state *s, int flush));
1592N/Alocal block_state deflate_fast OF((deflate_state *s, int flush));
1592N/A#ifndef FASTEST
1592N/Alocal block_state deflate_slow OF((deflate_state *s, int flush));
1592N/A#endif
1592N/Alocal void lm_init OF((deflate_state *s));
1592N/Alocal void putShortMSB OF((deflate_state *s, uInt b));
1592N/Alocal void flush_pending OF((z_streamp strm));
1592N/Alocal int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
1592N/A#ifndef FASTEST
1592N/A#ifdef ASMV
1592N/A void match_init OF((void)); /* asm code initialization */
1592N/A uInt longest_match OF((deflate_state *s, IPos cur_match));
1592N/A#else
1592N/Alocal uInt longest_match OF((deflate_state *s, IPos cur_match));
1592N/A#endif
1592N/A#endif
1592N/Alocal uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
1592N/A
1592N/A#ifdef DEBUG
1592N/Alocal void check_match OF((deflate_state *s, IPos start, IPos match,
1592N/A int length));
1592N/A#endif
1592N/A
1592N/A/* ===========================================================================
1592N/A * Local data
1592N/A */
1592N/A
1592N/A#define NIL 0
1592N/A/* Tail of hash chains */
1592N/A
1592N/A#ifndef TOO_FAR
1592N/A# define TOO_FAR 4096
1592N/A#endif
1592N/A/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
1592N/A
1592N/A#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
1592N/A/* Minimum amount of lookahead, except at the end of the input file.
1592N/A * See deflate.c for comments about the MIN_MATCH+1.
1592N/A */
1592N/A
1592N/A/* Values for max_lazy_match, good_match and max_chain_length, depending on
1592N/A * the desired pack level (0..9). The values given below have been tuned to
1592N/A * exclude worst case performance for pathological files. Better values may be
1592N/A * found for specific files.
1592N/A */
1592N/Atypedef struct config_s {
1592N/A ush good_length; /* reduce lazy search above this match length */
1592N/A ush max_lazy; /* do not perform lazy search above this match length */
1592N/A ush nice_length; /* quit search above this match length */
1592N/A ush max_chain;
1592N/A compress_func func;
1592N/A} config;
1592N/A
1592N/A#ifdef FASTEST
1592N/Alocal const config configuration_table[2] = {
1592N/A/* good lazy nice chain */
1592N/A/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
1592N/A/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
1592N/A#else
1592N/Alocal const config configuration_table[10] = {
1592N/A/* good lazy nice chain */
1592N/A/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
1592N/A/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
1592N/A/* 2 */ {4, 5, 16, 8, deflate_fast},
1592N/A/* 3 */ {4, 6, 32, 32, deflate_fast},
1592N/A
1592N/A/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
1592N/A/* 5 */ {8, 16, 32, 32, deflate_slow},
1592N/A/* 6 */ {8, 16, 128, 128, deflate_slow},
1592N/A/* 7 */ {8, 32, 128, 256, deflate_slow},
1592N/A/* 8 */ {32, 128, 258, 1024, deflate_slow},
1592N/A/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
1592N/A#endif
1592N/A
1592N/A/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
1592N/A * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
1592N/A * meaning.
1592N/A */
1592N/A
1592N/A#define EQUAL 0
1592N/A/* result of memcmp for equal strings */
1592N/A
1592N/A#ifndef NO_DUMMY_DECL
1592N/Astruct static_tree_desc_s {int dummy;}; /* for buggy compilers */
1592N/A#endif
1592N/A
1592N/A/* ===========================================================================
1592N/A * Update a hash value with the given input byte
1592N/A * IN assertion: all calls to to UPDATE_HASH are made with consecutive
1592N/A * input characters, so that a running hash key can be computed from the
1592N/A * previous key instead of complete recalculation each time.
1592N/A */
1592N/A#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
1592N/A
1592N/A
1592N/A/* ===========================================================================
1592N/A * Insert string str in the dictionary and set match_head to the previous head
1592N/A * of the hash chain (the most recent string with same hash key). Return
1592N/A * the previous length of the hash chain.
1592N/A * If this file is compiled with -DFASTEST, the compression level is forced
1592N/A * to 1, and no hash chains are maintained.
1592N/A * IN assertion: all calls to to INSERT_STRING are made with consecutive
1592N/A * input characters and the first MIN_MATCH bytes of str are valid
1592N/A * (except for the last MIN_MATCH-1 bytes of the input file).
1592N/A */
1592N/A#ifdef FASTEST
1592N/A#define INSERT_STRING(s, str, match_head) \
1592N/A (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
1592N/A match_head = s->head[s->ins_h], \
1592N/A s->head[s->ins_h] = (Pos)(str))
1592N/A#else
1592N/A#define INSERT_STRING(s, str, match_head) \
1592N/A (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
1592N/A match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
1592N/A s->head[s->ins_h] = (Pos)(str))
1592N/A#endif
1592N/A
1592N/A/* ===========================================================================
1592N/A * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
1592N/A * prev[] will be initialized on the fly.
1592N/A */
1592N/A#define CLEAR_HASH(s) \
1592N/A s->head[s->hash_size-1] = NIL; \
1592N/A zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflateInit_(strm, level, version, stream_size)
1592N/A z_streamp strm;
1592N/A int level;
1592N/A const char *version;
1592N/A int stream_size;
1592N/A{
1592N/A return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
1592N/A Z_DEFAULT_STRATEGY, version, stream_size);
1592N/A /* To do: ignore strm->next_in if we use it as window */
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
1592N/A version, stream_size)
1592N/A z_streamp strm;
1592N/A int level;
1592N/A int method;
1592N/A int windowBits;
1592N/A int memLevel;
1592N/A int strategy;
1592N/A const char *version;
1592N/A int stream_size;
1592N/A{
1592N/A deflate_state *s;
1592N/A int wrap = 1;
1592N/A static const char my_version[] = ZLIB_VERSION;
1592N/A
1592N/A ushf *overlay;
1592N/A /* We overlay pending_buf and d_buf+l_buf. This works since the average
1592N/A * output size for (length,distance) codes is <= 24 bits.
1592N/A */
1592N/A
1592N/A if (version == Z_NULL || version[0] != my_version[0] ||
1592N/A stream_size != sizeof(z_stream)) {
1592N/A return Z_VERSION_ERROR;
1592N/A }
1592N/A if (strm == Z_NULL) return Z_STREAM_ERROR;
1592N/A
1592N/A strm->msg = Z_NULL;
1592N/A if (strm->zalloc == (alloc_func)0) {
1592N/A strm->zalloc = zcalloc;
1592N/A strm->opaque = (voidpf)0;
1592N/A }
1592N/A if (strm->zfree == (free_func)0) strm->zfree = zcfree;
1592N/A
1592N/A#ifdef FASTEST
1592N/A if (level != 0) level = 1;
1592N/A#else
1592N/A if (level == Z_DEFAULT_COMPRESSION) level = 6;
1592N/A#endif
1592N/A
1592N/A if (windowBits < 0) { /* suppress zlib wrapper */
1592N/A wrap = 0;
1592N/A windowBits = -windowBits;
1592N/A }
1592N/A#ifdef GZIP
1592N/A else if (windowBits > 15) {
1592N/A wrap = 2; /* write gzip wrapper instead */
1592N/A windowBits -= 16;
1592N/A }
1592N/A#endif
1592N/A if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
1592N/A windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
1592N/A strategy < 0 || strategy > Z_FIXED) {
1592N/A return Z_STREAM_ERROR;
1592N/A }
1592N/A if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
1592N/A s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
1592N/A if (s == Z_NULL) return Z_MEM_ERROR;
1592N/A strm->state = (struct internal_state FAR *)s;
1592N/A s->strm = strm;
1592N/A
1592N/A s->wrap = wrap;
1592N/A s->gzhead = Z_NULL;
1592N/A s->w_bits = windowBits;
1592N/A s->w_size = 1 << s->w_bits;
1592N/A s->w_mask = s->w_size - 1;
1592N/A
1592N/A s->hash_bits = memLevel + 7;
1592N/A s->hash_size = 1 << s->hash_bits;
1592N/A s->hash_mask = s->hash_size - 1;
1592N/A s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
1592N/A
1592N/A s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
1592N/A s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
1592N/A s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
1592N/A
1592N/A s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
1592N/A
1592N/A overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
1592N/A s->pending_buf = (uchf *) overlay;
1592N/A s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
1592N/A
1592N/A if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
1592N/A s->pending_buf == Z_NULL) {
1592N/A s->status = FINISH_STATE;
1592N/A strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
1592N/A deflateEnd (strm);
1592N/A return Z_MEM_ERROR;
1592N/A }
1592N/A s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
1592N/A s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
1592N/A
1592N/A s->level = level;
1592N/A s->strategy = strategy;
1592N/A s->method = (Byte)method;
1592N/A
1592N/A return deflateReset(strm);
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
1592N/A z_streamp strm;
1592N/A const Bytef *dictionary;
1592N/A uInt dictLength;
1592N/A{
1592N/A deflate_state *s;
1592N/A uInt length = dictLength;
1592N/A uInt n;
1592N/A IPos hash_head = 0;
1592N/A
1592N/A if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
1592N/A strm->state->wrap == 2 ||
1592N/A (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
1592N/A return Z_STREAM_ERROR;
1592N/A
1592N/A s = strm->state;
1592N/A if (s->wrap)
1592N/A strm->adler = adler32(strm->adler, dictionary, dictLength);
1592N/A
1592N/A if (length < MIN_MATCH) return Z_OK;
1592N/A if (length > MAX_DIST(s)) {
1592N/A length = MAX_DIST(s);
1592N/A dictionary += dictLength - length; /* use the tail of the dictionary */
1592N/A }
1592N/A zmemcpy(s->window, dictionary, length);
1592N/A s->strstart = length;
1592N/A s->block_start = (long)length;
1592N/A
1592N/A /* Insert all strings in the hash table (except for the last two bytes).
1592N/A * s->lookahead stays null, so s->ins_h will be recomputed at the next
1592N/A * call of fill_window.
1592N/A */
1592N/A s->ins_h = s->window[0];
1592N/A UPDATE_HASH(s, s->ins_h, s->window[1]);
1592N/A for (n = 0; n <= length - MIN_MATCH; n++) {
1592N/A INSERT_STRING(s, n, hash_head);
1592N/A }
1592N/A if (hash_head) hash_head = 0; /* to make compiler happy */
1592N/A return Z_OK;
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflateReset (strm)
1592N/A z_streamp strm;
1592N/A{
1592N/A deflate_state *s;
1592N/A
1592N/A if (strm == Z_NULL || strm->state == Z_NULL ||
1592N/A strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
1592N/A return Z_STREAM_ERROR;
1592N/A }
1592N/A
1592N/A strm->total_in = strm->total_out = 0;
1592N/A strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
1592N/A strm->data_type = Z_UNKNOWN;
1592N/A
1592N/A s = (deflate_state *)strm->state;
1592N/A s->pending = 0;
1592N/A s->pending_out = s->pending_buf;
1592N/A
1592N/A if (s->wrap < 0) {
1592N/A s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
1592N/A }
1592N/A s->status = s->wrap ? INIT_STATE : BUSY_STATE;
1592N/A strm->adler =
1592N/A#ifdef GZIP
1592N/A s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
1592N/A#endif
1592N/A adler32(0L, Z_NULL, 0);
1592N/A s->last_flush = Z_NO_FLUSH;
1592N/A
1592N/A _tr_init(s);
1592N/A lm_init(s);
1592N/A
1592N/A return Z_OK;
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflateSetHeader (strm, head)
1592N/A z_streamp strm;
1592N/A gz_headerp head;
1592N/A{
1592N/A if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1592N/A if (strm->state->wrap != 2) return Z_STREAM_ERROR;
1592N/A strm->state->gzhead = head;
1592N/A return Z_OK;
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflatePrime (strm, bits, value)
1592N/A z_streamp strm;
1592N/A int bits;
1592N/A int value;
1592N/A{
1592N/A if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1592N/A strm->state->bi_valid = bits;
1592N/A strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
1592N/A return Z_OK;
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflateParams(strm, level, strategy)
1592N/A z_streamp strm;
1592N/A int level;
1592N/A int strategy;
1592N/A{
1592N/A deflate_state *s;
1592N/A compress_func func;
1592N/A int err = Z_OK;
1592N/A
1592N/A if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1592N/A s = strm->state;
1592N/A
1592N/A#ifdef FASTEST
1592N/A if (level != 0) level = 1;
1592N/A#else
1592N/A if (level == Z_DEFAULT_COMPRESSION) level = 6;
1592N/A#endif
1592N/A if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
1592N/A return Z_STREAM_ERROR;
1592N/A }
1592N/A func = configuration_table[s->level].func;
1592N/A
1592N/A if (func != configuration_table[level].func && strm->total_in != 0) {
1592N/A /* Flush the last buffer: */
1592N/A err = deflate(strm, Z_PARTIAL_FLUSH);
1592N/A }
1592N/A if (s->level != level) {
1592N/A s->level = level;
1592N/A s->max_lazy_match = configuration_table[level].max_lazy;
1592N/A s->good_match = configuration_table[level].good_length;
1592N/A s->nice_match = configuration_table[level].nice_length;
1592N/A s->max_chain_length = configuration_table[level].max_chain;
1592N/A }
1592N/A s->strategy = strategy;
1592N/A return err;
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
1592N/A z_streamp strm;
1592N/A int good_length;
1592N/A int max_lazy;
1592N/A int nice_length;
1592N/A int max_chain;
1592N/A{
1592N/A deflate_state *s;
1592N/A
1592N/A if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1592N/A s = strm->state;
1592N/A s->good_match = good_length;
1592N/A s->max_lazy_match = max_lazy;
1592N/A s->nice_match = nice_length;
1592N/A s->max_chain_length = max_chain;
1592N/A return Z_OK;
1592N/A}
1592N/A
1592N/A/* =========================================================================
1592N/A * For the default windowBits of 15 and memLevel of 8, this function returns
1592N/A * a close to exact, as well as small, upper bound on the compressed size.
1592N/A * They are coded as constants here for a reason--if the #define's are
1592N/A * changed, then this function needs to be changed as well. The return
1592N/A * value for 15 and 8 only works for those exact settings.
1592N/A *
1592N/A * For any setting other than those defaults for windowBits and memLevel,
1592N/A * the value returned is a conservative worst case for the maximum expansion
1592N/A * resulting from using fixed blocks instead of stored blocks, which deflate
1592N/A * can emit on compressed data for some combinations of the parameters.
1592N/A *
1592N/A * This function could be more sophisticated to provide closer upper bounds
1592N/A * for every combination of windowBits and memLevel, as well as wrap.
1592N/A * But even the conservative upper bound of about 14% expansion does not
1592N/A * seem onerous for output buffer allocation.
1592N/A */
1592N/AuLong ZEXPORT deflateBound(strm, sourceLen)
1592N/A z_streamp strm;
1592N/A uLong sourceLen;
1592N/A{
1592N/A deflate_state *s;
1592N/A uLong destLen;
1592N/A
1592N/A /* conservative upper bound */
1592N/A destLen = sourceLen +
1592N/A ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
1592N/A
1592N/A /* if can't get parameters, return conservative bound */
1592N/A if (strm == Z_NULL || strm->state == Z_NULL)
1592N/A return destLen;
1592N/A
1592N/A /* if not default parameters, return conservative bound */
1592N/A s = strm->state;
1592N/A if (s->w_bits != 15 || s->hash_bits != 8 + 7)
1592N/A return destLen;
1592N/A
1592N/A /* default settings: return tight bound for that case */
1592N/A return compressBound(sourceLen);
1592N/A}
1592N/A
1592N/A/* =========================================================================
1592N/A * Put a short in the pending buffer. The 16-bit value is put in MSB order.
1592N/A * IN assertion: the stream state is correct and there is enough room in
1592N/A * pending_buf.
1592N/A */
1592N/Alocal void putShortMSB (s, b)
1592N/A deflate_state *s;
1592N/A uInt b;
1592N/A{
1592N/A put_byte(s, (Byte)(b >> 8));
1592N/A put_byte(s, (Byte)(b & 0xff));
1592N/A}
1592N/A
1592N/A/* =========================================================================
1592N/A * Flush as much pending output as possible. All deflate() output goes
1592N/A * through this function so some applications may wish to modify it
1592N/A * to avoid allocating a large strm->next_out buffer and copying into it.
1592N/A * (See also read_buf()).
1592N/A */
1592N/Alocal void flush_pending(strm)
1592N/A z_streamp strm;
1592N/A{
1592N/A unsigned len = strm->state->pending;
1592N/A
1592N/A if (len > strm->avail_out) len = strm->avail_out;
1592N/A if (len == 0) return;
1592N/A
1592N/A zmemcpy(strm->next_out, strm->state->pending_out, len);
1592N/A strm->next_out += len;
1592N/A strm->state->pending_out += len;
1592N/A strm->total_out += len;
1592N/A strm->avail_out -= len;
1592N/A strm->state->pending -= len;
1592N/A if (strm->state->pending == 0) {
1592N/A strm->state->pending_out = strm->state->pending_buf;
1592N/A }
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflate (strm, flush)
1592N/A z_streamp strm;
1592N/A int flush;
1592N/A{
1592N/A int old_flush; /* value of flush param for previous deflate call */
1592N/A deflate_state *s;
1592N/A
1592N/A if (strm == Z_NULL || strm->state == Z_NULL ||
1592N/A flush > Z_FINISH || flush < 0) {
1592N/A return Z_STREAM_ERROR;
1592N/A }
1592N/A s = strm->state;
1592N/A
1592N/A if (strm->next_out == Z_NULL ||
1592N/A (strm->next_in == Z_NULL && strm->avail_in != 0) ||
1592N/A (s->status == FINISH_STATE && flush != Z_FINISH)) {
1592N/A ERR_RETURN(strm, Z_STREAM_ERROR);
1592N/A }
1592N/A if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
1592N/A
1592N/A s->strm = strm; /* just in case */
1592N/A old_flush = s->last_flush;
1592N/A s->last_flush = flush;
1592N/A
1592N/A /* Write the header */
1592N/A if (s->status == INIT_STATE) {
1592N/A#ifdef GZIP
1592N/A if (s->wrap == 2) {
1592N/A strm->adler = crc32(0L, Z_NULL, 0);
1592N/A put_byte(s, 31);
1592N/A put_byte(s, 139);
1592N/A put_byte(s, 8);
1592N/A if (s->gzhead == NULL) {
1592N/A put_byte(s, 0);
1592N/A put_byte(s, 0);
1592N/A put_byte(s, 0);
1592N/A put_byte(s, 0);
1592N/A put_byte(s, 0);
1592N/A put_byte(s, s->level == 9 ? 2 :
1592N/A (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
1592N/A 4 : 0));
1592N/A put_byte(s, OS_CODE);
1592N/A s->status = BUSY_STATE;
1592N/A }
1592N/A else {
1592N/A put_byte(s, (s->gzhead->text ? 1 : 0) +
1592N/A (s->gzhead->hcrc ? 2 : 0) +
1592N/A (s->gzhead->extra == Z_NULL ? 0 : 4) +
1592N/A (s->gzhead->name == Z_NULL ? 0 : 8) +
1592N/A (s->gzhead->comment == Z_NULL ? 0 : 16)
1592N/A );
1592N/A put_byte(s, (Byte)(s->gzhead->time & 0xff));
1592N/A put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
1592N/A put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
1592N/A put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
1592N/A put_byte(s, s->level == 9 ? 2 :
1592N/A (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
1592N/A 4 : 0));
1592N/A put_byte(s, s->gzhead->os & 0xff);
1592N/A if (s->gzhead->extra != NULL) {
1592N/A put_byte(s, s->gzhead->extra_len & 0xff);
1592N/A put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
1592N/A }
1592N/A if (s->gzhead->hcrc)
1592N/A strm->adler = crc32(strm->adler, s->pending_buf,
1592N/A s->pending);
1592N/A s->gzindex = 0;
1592N/A s->status = EXTRA_STATE;
1592N/A }
1592N/A }
1592N/A else
1592N/A#endif
1592N/A {
1592N/A uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
1592N/A uInt level_flags;
1592N/A
1592N/A if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
1592N/A level_flags = 0;
1592N/A else if (s->level < 6)
1592N/A level_flags = 1;
1592N/A else if (s->level == 6)
1592N/A level_flags = 2;
1592N/A else
1592N/A level_flags = 3;
1592N/A header |= (level_flags << 6);
1592N/A if (s->strstart != 0) header |= PRESET_DICT;
1592N/A header += 31 - (header % 31);
1592N/A
1592N/A s->status = BUSY_STATE;
1592N/A putShortMSB(s, header);
1592N/A
1592N/A /* Save the adler32 of the preset dictionary: */
1592N/A if (s->strstart != 0) {
1592N/A putShortMSB(s, (uInt)(strm->adler >> 16));
1592N/A putShortMSB(s, (uInt)(strm->adler & 0xffff));
1592N/A }
1592N/A strm->adler = adler32(0L, Z_NULL, 0);
1592N/A }
1592N/A }
1592N/A#ifdef GZIP
1592N/A if (s->status == EXTRA_STATE) {
1592N/A if (s->gzhead->extra != NULL) {
1592N/A uInt beg = s->pending; /* start of bytes to update crc */
1592N/A
1592N/A while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
1592N/A if (s->pending == s->pending_buf_size) {
1592N/A if (s->gzhead->hcrc && s->pending > beg)
1592N/A strm->adler = crc32(strm->adler, s->pending_buf + beg,
1592N/A s->pending - beg);
1592N/A flush_pending(strm);
1592N/A beg = s->pending;
1592N/A if (s->pending == s->pending_buf_size)
1592N/A break;
1592N/A }
1592N/A put_byte(s, s->gzhead->extra[s->gzindex]);
1592N/A s->gzindex++;
1592N/A }
1592N/A if (s->gzhead->hcrc && s->pending > beg)
1592N/A strm->adler = crc32(strm->adler, s->pending_buf + beg,
1592N/A s->pending - beg);
1592N/A if (s->gzindex == s->gzhead->extra_len) {
1592N/A s->gzindex = 0;
1592N/A s->status = NAME_STATE;
1592N/A }
1592N/A }
1592N/A else
1592N/A s->status = NAME_STATE;
1592N/A }
1592N/A if (s->status == NAME_STATE) {
1592N/A if (s->gzhead->name != NULL) {
1592N/A uInt beg = s->pending; /* start of bytes to update crc */
1592N/A int val;
1592N/A
1592N/A do {
1592N/A if (s->pending == s->pending_buf_size) {
1592N/A if (s->gzhead->hcrc && s->pending > beg)
1592N/A strm->adler = crc32(strm->adler, s->pending_buf + beg,
1592N/A s->pending - beg);
1592N/A flush_pending(strm);
1592N/A beg = s->pending;
1592N/A if (s->pending == s->pending_buf_size) {
1592N/A val = 1;
1592N/A break;
1592N/A }
1592N/A }
1592N/A val = s->gzhead->name[s->gzindex++];
1592N/A put_byte(s, val);
1592N/A } while (val != 0);
1592N/A if (s->gzhead->hcrc && s->pending > beg)
1592N/A strm->adler = crc32(strm->adler, s->pending_buf + beg,
1592N/A s->pending - beg);
1592N/A if (val == 0) {
1592N/A s->gzindex = 0;
1592N/A s->status = COMMENT_STATE;
1592N/A }
1592N/A }
1592N/A else
1592N/A s->status = COMMENT_STATE;
1592N/A }
1592N/A if (s->status == COMMENT_STATE) {
1592N/A if (s->gzhead->comment != NULL) {
1592N/A uInt beg = s->pending; /* start of bytes to update crc */
1592N/A int val;
1592N/A
1592N/A do {
1592N/A if (s->pending == s->pending_buf_size) {
1592N/A if (s->gzhead->hcrc && s->pending > beg)
1592N/A strm->adler = crc32(strm->adler, s->pending_buf + beg,
1592N/A s->pending - beg);
1592N/A flush_pending(strm);
1592N/A beg = s->pending;
1592N/A if (s->pending == s->pending_buf_size) {
1592N/A val = 1;
1592N/A break;
1592N/A }
1592N/A }
1592N/A val = s->gzhead->comment[s->gzindex++];
1592N/A put_byte(s, val);
1592N/A } while (val != 0);
1592N/A if (s->gzhead->hcrc && s->pending > beg)
1592N/A strm->adler = crc32(strm->adler, s->pending_buf + beg,
1592N/A s->pending - beg);
1592N/A if (val == 0)
1592N/A s->status = HCRC_STATE;
1592N/A }
1592N/A else
1592N/A s->status = HCRC_STATE;
1592N/A }
1592N/A if (s->status == HCRC_STATE) {
1592N/A if (s->gzhead->hcrc) {
1592N/A if (s->pending + 2 > s->pending_buf_size)
1592N/A flush_pending(strm);
1592N/A if (s->pending + 2 <= s->pending_buf_size) {
1592N/A put_byte(s, (Byte)(strm->adler & 0xff));
1592N/A put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1592N/A strm->adler = crc32(0L, Z_NULL, 0);
1592N/A s->status = BUSY_STATE;
1592N/A }
1592N/A }
1592N/A else
1592N/A s->status = BUSY_STATE;
1592N/A }
1592N/A#endif
1592N/A
1592N/A /* Flush as much pending output as possible */
1592N/A if (s->pending != 0) {
1592N/A flush_pending(strm);
1592N/A if (strm->avail_out == 0) {
1592N/A /* Since avail_out is 0, deflate will be called again with
1592N/A * more output space, but possibly with both pending and
1592N/A * avail_in equal to zero. There won't be anything to do,
1592N/A * but this is not an error situation so make sure we
1592N/A * return OK instead of BUF_ERROR at next call of deflate:
1592N/A */
1592N/A s->last_flush = -1;
1592N/A return Z_OK;
1592N/A }
1592N/A
1592N/A /* Make sure there is something to do and avoid duplicate consecutive
1592N/A * flushes. For repeated and useless calls with Z_FINISH, we keep
1592N/A * returning Z_STREAM_END instead of Z_BUF_ERROR.
1592N/A */
1592N/A } else if (strm->avail_in == 0 && flush <= old_flush &&
1592N/A flush != Z_FINISH) {
1592N/A ERR_RETURN(strm, Z_BUF_ERROR);
1592N/A }
1592N/A
1592N/A /* User must not provide more input after the first FINISH: */
1592N/A if (s->status == FINISH_STATE && strm->avail_in != 0) {
1592N/A ERR_RETURN(strm, Z_BUF_ERROR);
1592N/A }
1592N/A
1592N/A /* Start a new block or continue the current one.
1592N/A */
1592N/A if (strm->avail_in != 0 || s->lookahead != 0 ||
1592N/A (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
1592N/A block_state bstate;
1592N/A
1592N/A bstate = (*(configuration_table[s->level].func))(s, flush);
1592N/A
1592N/A if (bstate == finish_started || bstate == finish_done) {
1592N/A s->status = FINISH_STATE;
1592N/A }
1592N/A if (bstate == need_more || bstate == finish_started) {
1592N/A if (strm->avail_out == 0) {
1592N/A s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
1592N/A }
1592N/A return Z_OK;
1592N/A /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1592N/A * of deflate should use the same flush parameter to make sure
1592N/A * that the flush is complete. So we don't have to output an
1592N/A * empty block here, this will be done at next call. This also
1592N/A * ensures that for a very small output buffer, we emit at most
1592N/A * one empty block.
1592N/A */
1592N/A }
1592N/A if (bstate == block_done) {
1592N/A if (flush == Z_PARTIAL_FLUSH) {
1592N/A _tr_align(s);
1592N/A } else { /* FULL_FLUSH or SYNC_FLUSH */
1592N/A _tr_stored_block(s, (char*)0, 0L, 0);
1592N/A /* For a full flush, this empty block will be recognized
1592N/A * as a special marker by inflate_sync().
1592N/A */
1592N/A if (flush == Z_FULL_FLUSH) {
1592N/A CLEAR_HASH(s); /* forget history */
1592N/A }
1592N/A }
1592N/A flush_pending(strm);
1592N/A if (strm->avail_out == 0) {
1592N/A s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1592N/A return Z_OK;
1592N/A }
1592N/A }
1592N/A }
1592N/A Assert(strm->avail_out > 0, "bug2");
1592N/A
1592N/A if (flush != Z_FINISH) return Z_OK;
1592N/A if (s->wrap <= 0) return Z_STREAM_END;
1592N/A
1592N/A /* Write the trailer */
1592N/A#ifdef GZIP
1592N/A if (s->wrap == 2) {
1592N/A put_byte(s, (Byte)(strm->adler & 0xff));
1592N/A put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1592N/A put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
1592N/A put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
1592N/A put_byte(s, (Byte)(strm->total_in & 0xff));
1592N/A put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
1592N/A put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
1592N/A put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
1592N/A }
1592N/A else
1592N/A#endif
1592N/A {
1592N/A putShortMSB(s, (uInt)(strm->adler >> 16));
1592N/A putShortMSB(s, (uInt)(strm->adler & 0xffff));
1592N/A }
1592N/A flush_pending(strm);
1592N/A /* If avail_out is zero, the application will call deflate again
1592N/A * to flush the rest.
1592N/A */
1592N/A if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
1592N/A return s->pending != 0 ? Z_OK : Z_STREAM_END;
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/Aint ZEXPORT deflateEnd (strm)
1592N/A z_streamp strm;
1592N/A{
1592N/A int status;
1592N/A
1592N/A if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1592N/A
1592N/A status = strm->state->status;
1592N/A if (status != INIT_STATE &&
1592N/A status != EXTRA_STATE &&
1592N/A status != NAME_STATE &&
1592N/A status != COMMENT_STATE &&
1592N/A status != HCRC_STATE &&
1592N/A status != BUSY_STATE &&
1592N/A status != FINISH_STATE) {
1592N/A return Z_STREAM_ERROR;
1592N/A }
1592N/A
1592N/A /* Deallocate in reverse order of allocations: */
1592N/A TRY_FREE(strm, strm->state->pending_buf);
1592N/A TRY_FREE(strm, strm->state->head);
1592N/A TRY_FREE(strm, strm->state->prev);
1592N/A TRY_FREE(strm, strm->state->window);
1592N/A
1592N/A ZFREE(strm, strm->state);
1592N/A strm->state = Z_NULL;
1592N/A
1592N/A return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1592N/A}
1592N/A
1592N/A/* =========================================================================
1592N/A * Copy the source state to the destination state.
1592N/A * To simplify the source, this is not supported for 16-bit MSDOS (which
1592N/A * doesn't have enough memory anyway to duplicate compression states).
1592N/A */
1592N/Aint ZEXPORT deflateCopy (dest, source)
1592N/A z_streamp dest;
1592N/A z_streamp source;
1592N/A{
1592N/A#ifdef MAXSEG_64K
1592N/A return Z_STREAM_ERROR;
1592N/A#else
1592N/A deflate_state *ds;
1592N/A deflate_state *ss;
1592N/A ushf *overlay;
1592N/A
1592N/A
1592N/A if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
1592N/A return Z_STREAM_ERROR;
1592N/A }
1592N/A
1592N/A ss = source->state;
1592N/A
1592N/A zmemcpy(dest, source, sizeof(z_stream));
1592N/A
1592N/A ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1592N/A if (ds == Z_NULL) return Z_MEM_ERROR;
1592N/A dest->state = (struct internal_state FAR *) ds;
1592N/A zmemcpy(ds, ss, sizeof(deflate_state));
1592N/A ds->strm = dest;
1592N/A
1592N/A ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
1592N/A ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
1592N/A ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
1592N/A overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
1592N/A ds->pending_buf = (uchf *) overlay;
1592N/A
1592N/A if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1592N/A ds->pending_buf == Z_NULL) {
1592N/A deflateEnd (dest);
1592N/A return Z_MEM_ERROR;
1592N/A }
1592N/A /* following zmemcpy do not work for 16-bit MSDOS */
1592N/A zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
1592N/A zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
1592N/A zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
1592N/A zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1592N/A
1592N/A ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1592N/A ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
1592N/A ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
1592N/A
1592N/A ds->l_desc.dyn_tree = ds->dyn_ltree;
1592N/A ds->d_desc.dyn_tree = ds->dyn_dtree;
1592N/A ds->bl_desc.dyn_tree = ds->bl_tree;
1592N/A
1592N/A return Z_OK;
1592N/A#endif /* MAXSEG_64K */
1592N/A}
1592N/A
1592N/A/* ===========================================================================
1592N/A * Read a new buffer from the current input stream, update the adler32
1592N/A * and total number of bytes read. All deflate() input goes through
1592N/A * this function so some applications may wish to modify it to avoid
1592N/A * allocating a large strm->next_in buffer and copying from it.
1592N/A * (See also flush_pending()).
1592N/A */
1592N/Alocal int read_buf(strm, buf, size)
1592N/A z_streamp strm;
1592N/A Bytef *buf;
1592N/A unsigned size;
1592N/A{
1592N/A unsigned len = strm->avail_in;
1592N/A
1592N/A if (len > size) len = size;
1592N/A if (len == 0) return 0;
1592N/A
1592N/A strm->avail_in -= len;
1592N/A
1592N/A if (strm->state->wrap == 1) {
1592N/A strm->adler = adler32(strm->adler, strm->next_in, len);
1592N/A }
1592N/A#ifdef GZIP
1592N/A else if (strm->state->wrap == 2) {
1592N/A strm->adler = crc32(strm->adler, strm->next_in, len);
1592N/A }
1592N/A#endif
1592N/A zmemcpy(buf, strm->next_in, len);
1592N/A strm->next_in += len;
1592N/A strm->total_in += len;
1592N/A
1592N/A return (int)len;
1592N/A}
1592N/A
1592N/A/* ===========================================================================
1592N/A * Initialize the "longest match" routines for a new zlib stream
1592N/A */
1592N/Alocal void lm_init (s)
1592N/A deflate_state *s;
1592N/A{
1592N/A s->window_size = (ulg)2L*s->w_size;
1592N/A
1592N/A CLEAR_HASH(s);
1592N/A
1592N/A /* Set the default configuration parameters:
1592N/A */
1592N/A s->max_lazy_match = configuration_table[s->level].max_lazy;
1592N/A s->good_match = configuration_table[s->level].good_length;
1592N/A s->nice_match = configuration_table[s->level].nice_length;
1592N/A s->max_chain_length = configuration_table[s->level].max_chain;
1592N/A
1592N/A s->strstart = 0;
1592N/A s->block_start = 0L;
1592N/A s->lookahead = 0;
1592N/A s->match_length = s->prev_length = MIN_MATCH-1;
1592N/A s->match_available = 0;
1592N/A s->ins_h = 0;
1592N/A#ifndef FASTEST
1592N/A#ifdef ASMV
1592N/A match_init(); /* initialize the asm code */
1592N/A#endif
1592N/A#endif
1592N/A}
1592N/A
1592N/A#ifndef FASTEST
1592N/A/* ===========================================================================
1592N/A * Set match_start to the longest match starting at the given string and
1592N/A * return its length. Matches shorter or equal to prev_length are discarded,
1592N/A * in which case the result is equal to prev_length and match_start is
1592N/A * garbage.
1592N/A * IN assertions: cur_match is the head of the hash chain for the current
1592N/A * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1592N/A * OUT assertion: the match length is not greater than s->lookahead.
1592N/A */
1592N/A#ifndef ASMV
1592N/A/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
1592N/A * match.S. The code will be functionally equivalent.
1592N/A */
1592N/Alocal uInt longest_match(s, cur_match)
1592N/A deflate_state *s;
1592N/A IPos cur_match; /* current match */
1592N/A{
1592N/A unsigned chain_length = s->max_chain_length;/* max hash chain length */
1592N/A register Bytef *scan = s->window + s->strstart; /* current string */
1592N/A register Bytef *match; /* matched string */
1592N/A register int len; /* length of current match */
1592N/A int best_len = s->prev_length; /* best match length so far */
1592N/A int nice_match = s->nice_match; /* stop if match long enough */
1592N/A IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1592N/A s->strstart - (IPos)MAX_DIST(s) : NIL;
1592N/A /* Stop when cur_match becomes <= limit. To simplify the code,
1592N/A * we prevent matches with the string of window index 0.
1592N/A */
1592N/A Posf *prev = s->prev;
1592N/A uInt wmask = s->w_mask;
1592N/A
1592N/A#ifdef UNALIGNED_OK
1592N/A /* Compare two bytes at a time. Note: this is not always beneficial.
1592N/A * Try with and without -DUNALIGNED_OK to check.
1592N/A */
1592N/A register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1592N/A register ush scan_start = *(ushf*)scan;
1592N/A register ush scan_end = *(ushf*)(scan+best_len-1);
1592N/A#else
1592N/A register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1592N/A register Byte scan_end1 = scan[best_len-1];
1592N/A register Byte scan_end = scan[best_len];
1592N/A#endif
1592N/A
1592N/A /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1592N/A * It is easy to get rid of this optimization if necessary.
1592N/A */
1592N/A Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1592N/A
1592N/A /* Do not waste too much time if we already have a good match: */
1592N/A if (s->prev_length >= s->good_match) {
1592N/A chain_length >>= 2;
1592N/A }
1592N/A /* Do not look for matches beyond the end of the input. This is necessary
1592N/A * to make deflate deterministic.
1592N/A */
1592N/A if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1592N/A
1592N/A Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1592N/A
1592N/A do {
1592N/A Assert(cur_match < s->strstart, "no future");
1592N/A match = s->window + cur_match;
1592N/A
1592N/A /* Skip to next match if the match length cannot increase
1592N/A * or if the match length is less than 2. Note that the checks below
1592N/A * for insufficient lookahead only occur occasionally for performance
1592N/A * reasons. Therefore uninitialized memory will be accessed, and
1592N/A * conditional jumps will be made that depend on those values.
1592N/A * However the length of the match is limited to the lookahead, so
1592N/A * the output of deflate is not affected by the uninitialized values.
1592N/A */
1592N/A#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1592N/A /* This code assumes sizeof(unsigned short) == 2. Do not use
1592N/A * UNALIGNED_OK if your compiler uses a different size.
1592N/A */
1592N/A if (*(ushf*)(match+best_len-1) != scan_end ||
1592N/A *(ushf*)match != scan_start) continue;
1592N/A
1592N/A /* It is not necessary to compare scan[2] and match[2] since they are
1592N/A * always equal when the other bytes match, given that the hash keys
1592N/A * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1592N/A * strstart+3, +5, ... up to strstart+257. We check for insufficient
1592N/A * lookahead only every 4th comparison; the 128th check will be made
1592N/A * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1592N/A * necessary to put more guard bytes at the end of the window, or
1592N/A * to check more often for insufficient lookahead.
1592N/A */
1592N/A Assert(scan[2] == match[2], "scan[2]?");
1592N/A scan++, match++;
1592N/A do {
1592N/A } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1592N/A *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1592N/A *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1592N/A *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1592N/A scan < strend);
1592N/A /* The funny "do {}" generates better code on most compilers */
1592N/A
1592N/A /* Here, scan <= window+strstart+257 */
1592N/A Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1592N/A if (*scan == *match) scan++;
1592N/A
1592N/A len = (MAX_MATCH - 1) - (int)(strend-scan);
1592N/A scan = strend - (MAX_MATCH-1);
1592N/A
1592N/A#else /* UNALIGNED_OK */
1592N/A
1592N/A if (match[best_len] != scan_end ||
1592N/A match[best_len-1] != scan_end1 ||
1592N/A *match != *scan ||
1592N/A *++match != scan[1]) continue;
1592N/A
1592N/A /* The check at best_len-1 can be removed because it will be made
1592N/A * again later. (This heuristic is not always a win.)
1592N/A * It is not necessary to compare scan[2] and match[2] since they
1592N/A * are always equal when the other bytes match, given that
1592N/A * the hash keys are equal and that HASH_BITS >= 8.
1592N/A */
1592N/A scan += 2, match++;
1592N/A Assert(*scan == *match, "match[2]?");
1592N/A
1592N/A /* We check for insufficient lookahead only every 8th comparison;
1592N/A * the 256th check will be made at strstart+258.
1592N/A */
1592N/A do {
1592N/A } while (*++scan == *++match && *++scan == *++match &&
1592N/A *++scan == *++match && *++scan == *++match &&
1592N/A *++scan == *++match && *++scan == *++match &&
1592N/A *++scan == *++match && *++scan == *++match &&
1592N/A scan < strend);
1592N/A
1592N/A Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1592N/A
1592N/A len = MAX_MATCH - (int)(strend - scan);
1592N/A scan = strend - MAX_MATCH;
1592N/A
1592N/A#endif /* UNALIGNED_OK */
1592N/A
1592N/A if (len > best_len) {
1592N/A s->match_start = cur_match;
1592N/A best_len = len;
1592N/A if (len >= nice_match) break;
1592N/A#ifdef UNALIGNED_OK
1592N/A scan_end = *(ushf*)(scan+best_len-1);
1592N/A#else
1592N/A scan_end1 = scan[best_len-1];
1592N/A scan_end = scan[best_len];
1592N/A#endif
1592N/A }
1592N/A } while ((cur_match = prev[cur_match & wmask]) > limit
1592N/A && --chain_length != 0);
1592N/A
1592N/A if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1592N/A return s->lookahead;
1592N/A}
1592N/A#endif /* ASMV */
1592N/A#endif /* FASTEST */
1592N/A
1592N/A/* ---------------------------------------------------------------------------
1592N/A * Optimized version for level == 1 or strategy == Z_RLE only
1592N/A */
1592N/Alocal uInt longest_match_fast(s, cur_match)
1592N/A deflate_state *s;
1592N/A IPos cur_match; /* current match */
1592N/A{
1592N/A register Bytef *scan = s->window + s->strstart; /* current string */
1592N/A register Bytef *match; /* matched string */
1592N/A register int len; /* length of current match */
1592N/A register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1592N/A
1592N/A /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1592N/A * It is easy to get rid of this optimization if necessary.
1592N/A */
1592N/A Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1592N/A
1592N/A Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1592N/A
1592N/A Assert(cur_match < s->strstart, "no future");
1592N/A
1592N/A match = s->window + cur_match;
1592N/A
1592N/A /* Return failure if the match length is less than 2:
1592N/A */
1592N/A if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1592N/A
1592N/A /* The check at best_len-1 can be removed because it will be made
1592N/A * again later. (This heuristic is not always a win.)
1592N/A * It is not necessary to compare scan[2] and match[2] since they
1592N/A * are always equal when the other bytes match, given that
1592N/A * the hash keys are equal and that HASH_BITS >= 8.
1592N/A */
1592N/A scan += 2, match += 2;
1592N/A Assert(*scan == *match, "match[2]?");
1592N/A
1592N/A /* We check for insufficient lookahead only every 8th comparison;
1592N/A * the 256th check will be made at strstart+258.
1592N/A */
1592N/A do {
1592N/A } while (*++scan == *++match && *++scan == *++match &&
1592N/A *++scan == *++match && *++scan == *++match &&
1592N/A *++scan == *++match && *++scan == *++match &&
1592N/A *++scan == *++match && *++scan == *++match &&
1592N/A scan < strend);
1592N/A
1592N/A Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1592N/A
1592N/A len = MAX_MATCH - (int)(strend - scan);
1592N/A
1592N/A if (len < MIN_MATCH) return MIN_MATCH - 1;
1592N/A
1592N/A s->match_start = cur_match;
1592N/A return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1592N/A}
1592N/A
1592N/A#ifdef DEBUG
1592N/A/* ===========================================================================
1592N/A * Check that the match at match_start is indeed a match.
1592N/A */
1592N/Alocal void check_match(s, start, match, length)
1592N/A deflate_state *s;
1592N/A IPos start, match;
1592N/A int length;
1592N/A{
1592N/A /* check that the match is indeed a match */
1592N/A if (zmemcmp(s->window + match,
1592N/A s->window + start, length) != EQUAL) {
1592N/A fprintf(stderr, " start %u, match %u, length %d\n",
1592N/A start, match, length);
1592N/A do {
1592N/A fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1592N/A } while (--length != 0);
1592N/A z_error("invalid match");
1592N/A }
1592N/A if (z_verbose > 1) {
1592N/A fprintf(stderr,"\\[%d,%d]", start-match, length);
1592N/A do { putc(s->window[start++], stderr); } while (--length != 0);
1592N/A }
1592N/A}
1592N/A#else
1592N/A# define check_match(s, start, match, length)
1592N/A#endif /* DEBUG */
1592N/A
1592N/A/* ===========================================================================
1592N/A * Fill the window when the lookahead becomes insufficient.
1592N/A * Updates strstart and lookahead.
1592N/A *
1592N/A * IN assertion: lookahead < MIN_LOOKAHEAD
1592N/A * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1592N/A * At least one byte has been read, or avail_in == 0; reads are
1592N/A * performed for at least two bytes (required for the zip translate_eol
1592N/A * option -- not supported here).
1592N/A */
1592N/Alocal void fill_window(s)
1592N/A deflate_state *s;
1592N/A{
1592N/A register unsigned n, m;
1592N/A register Posf *p;
1592N/A unsigned more; /* Amount of free space at the end of the window. */
1592N/A uInt wsize = s->w_size;
1592N/A
1592N/A do {
1592N/A more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1592N/A
1592N/A /* Deal with !@#$% 64K limit: */
1592N/A if (sizeof(int) <= 2) {
1592N/A if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1592N/A more = wsize;
1592N/A
1592N/A } else if (more == (unsigned)(-1)) {
1592N/A /* Very unlikely, but possible on 16 bit machine if
1592N/A * strstart == 0 && lookahead == 1 (input done a byte at time)
1592N/A */
1592N/A more--;
1592N/A }
1592N/A }
1592N/A
1592N/A /* If the window is almost full and there is insufficient lookahead,
1592N/A * move the upper half to the lower one to make room in the upper half.
1592N/A */
1592N/A if (s->strstart >= wsize+MAX_DIST(s)) {
1592N/A
1592N/A zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
1592N/A s->match_start -= wsize;
1592N/A s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1592N/A s->block_start -= (long) wsize;
1592N/A
1592N/A /* Slide the hash table (could be avoided with 32 bit values
1592N/A at the expense of memory usage). We slide even when level == 0
1592N/A to keep the hash table consistent if we switch back to level > 0
1592N/A later. (Using level 0 permanently is not an optimal usage of
1592N/A zlib, so we don't care about this pathological case.)
1592N/A */
1592N/A /* %%% avoid this when Z_RLE */
1592N/A n = s->hash_size;
1592N/A p = &s->head[n];
1592N/A do {
1592N/A m = *--p;
1592N/A *p = (Pos)(m >= wsize ? m-wsize : NIL);
1592N/A } while (--n);
1592N/A
1592N/A n = wsize;
1592N/A#ifndef FASTEST
1592N/A p = &s->prev[n];
1592N/A do {
1592N/A m = *--p;
1592N/A *p = (Pos)(m >= wsize ? m-wsize : NIL);
1592N/A /* If n is not on any hash chain, prev[n] is garbage but
1592N/A * its value will never be used.
1592N/A */
1592N/A } while (--n);
1592N/A#endif
1592N/A more += wsize;
1592N/A }
1592N/A if (s->strm->avail_in == 0) return;
1592N/A
1592N/A /* If there was no sliding:
1592N/A * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1592N/A * more == window_size - lookahead - strstart
1592N/A * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1592N/A * => more >= window_size - 2*WSIZE + 2
1592N/A * In the BIG_MEM or MMAP case (not yet supported),
1592N/A * window_size == input_size + MIN_LOOKAHEAD &&
1592N/A * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1592N/A * Otherwise, window_size == 2*WSIZE so more >= 2.
1592N/A * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1592N/A */
1592N/A Assert(more >= 2, "more < 2");
1592N/A
1592N/A n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1592N/A s->lookahead += n;
1592N/A
1592N/A /* Initialize the hash value now that we have some input: */
1592N/A if (s->lookahead >= MIN_MATCH) {
1592N/A s->ins_h = s->window[s->strstart];
1592N/A UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1592N/A#if MIN_MATCH != 3
1592N/A Call UPDATE_HASH() MIN_MATCH-3 more times
1592N/A#endif
1592N/A }
1592N/A /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1592N/A * but this is not important since only literal bytes will be emitted.
1592N/A */
1592N/A
1592N/A } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1592N/A}
1592N/A
1592N/A/* ===========================================================================
1592N/A * Flush the current block, with given end-of-file flag.
1592N/A * IN assertion: strstart is set to the end of the current match.
1592N/A */
1592N/A#define FLUSH_BLOCK_ONLY(s, eof) { \
1592N/A _tr_flush_block(s, (s->block_start >= 0L ? \
1592N/A (charf *)&s->window[(unsigned)s->block_start] : \
1592N/A (charf *)Z_NULL), \
1592N/A (ulg)((long)s->strstart - s->block_start), \
1592N/A (eof)); \
1592N/A s->block_start = s->strstart; \
1592N/A flush_pending(s->strm); \
1592N/A Tracev((stderr,"[FLUSH]")); \
1592N/A}
1592N/A
1592N/A/* Same but force premature exit if necessary. */
1592N/A#define FLUSH_BLOCK(s, eof) { \
1592N/A FLUSH_BLOCK_ONLY(s, eof); \
1592N/A if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
1592N/A}
1592N/A
1592N/A/* ===========================================================================
1592N/A * Copy without compression as much as possible from the input stream, return
1592N/A * the current block state.
1592N/A * This function does not insert new strings in the dictionary since
1592N/A * uncompressible data is probably not useful. This function is used
1592N/A * only for the level=0 compression option.
1592N/A * NOTE: this function should be optimized to avoid extra copying from
1592N/A * window to pending_buf.
1592N/A */
1592N/Alocal block_state deflate_stored(s, flush)
1592N/A deflate_state *s;
1592N/A int flush;
1592N/A{
1592N/A /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
1592N/A * to pending_buf_size, and each stored block has a 5 byte header:
1592N/A */
1592N/A ulg max_block_size = 0xffff;
1592N/A ulg max_start;
1592N/A
1592N/A if (max_block_size > s->pending_buf_size - 5) {
1592N/A max_block_size = s->pending_buf_size - 5;
1592N/A }
1592N/A
1592N/A /* Copy as much as possible from input to output: */
1592N/A for (;;) {
1592N/A /* Fill the window as much as possible: */
1592N/A if (s->lookahead <= 1) {
1592N/A
1592N/A Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1592N/A s->block_start >= (long)s->w_size, "slide too late");
1592N/A
1592N/A fill_window(s);
1592N/A if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1592N/A
1592N/A if (s->lookahead == 0) break; /* flush the current block */
1592N/A }
1592N/A Assert(s->block_start >= 0L, "block gone");
1592N/A
1592N/A s->strstart += s->lookahead;
1592N/A s->lookahead = 0;
1592N/A
1592N/A /* Emit a stored block if pending_buf will be full: */
1592N/A max_start = s->block_start + max_block_size;
1592N/A if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1592N/A /* strstart == 0 is possible when wraparound on 16-bit machine */
1592N/A s->lookahead = (uInt)(s->strstart - max_start);
1592N/A s->strstart = (uInt)max_start;
1592N/A FLUSH_BLOCK(s, 0);
1592N/A }
1592N/A /* Flush if we may have to slide, otherwise block_start may become
1592N/A * negative and the data will be gone:
1592N/A */
1592N/A if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1592N/A FLUSH_BLOCK(s, 0);
1592N/A }
1592N/A }
1592N/A FLUSH_BLOCK(s, flush == Z_FINISH);
1592N/A return flush == Z_FINISH ? finish_done : block_done;
1592N/A}
1592N/A
1592N/A/* ===========================================================================
1592N/A * Compress as much as possible from the input stream, return the current
1592N/A * block state.
1592N/A * This function does not perform lazy evaluation of matches and inserts
1592N/A * new strings in the dictionary only for unmatched strings or for short
1592N/A * matches. It is used only for the fast compression options.
1592N/A */
1592N/Alocal block_state deflate_fast(s, flush)
1592N/A deflate_state *s;
1592N/A int flush;
1592N/A{
1592N/A IPos hash_head = NIL; /* head of the hash chain */
1592N/A int bflush; /* set if current block must be flushed */
1592N/A
1592N/A for (;;) {
1592N/A /* Make sure that we always have enough lookahead, except
1592N/A * at the end of the input file. We need MAX_MATCH bytes
1592N/A * for the next match, plus MIN_MATCH bytes to insert the
1592N/A * string following the next match.
1592N/A */
1592N/A if (s->lookahead < MIN_LOOKAHEAD) {
1592N/A fill_window(s);
1592N/A if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1592N/A return need_more;
1592N/A }
1592N/A if (s->lookahead == 0) break; /* flush the current block */
1592N/A }
1592N/A
1592N/A /* Insert the string window[strstart .. strstart+2] in the
1592N/A * dictionary, and set hash_head to the head of the hash chain:
1592N/A */
1592N/A if (s->lookahead >= MIN_MATCH) {
1592N/A INSERT_STRING(s, s->strstart, hash_head);
1592N/A }
1592N/A
1592N/A /* Find the longest match, discarding those <= prev_length.
1592N/A * At this point we have always match_length < MIN_MATCH
1592N/A */
1592N/A if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1592N/A /* To simplify the code, we prevent matches with the string
1592N/A * of window index 0 (in particular we have to avoid a match
1592N/A * of the string with itself at the start of the input file).
1592N/A */
1592N/A#ifdef FASTEST
1592N/A if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
1592N/A (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
1592N/A s->match_length = longest_match_fast (s, hash_head);
1592N/A }
1592N/A#else
1592N/A if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1592N/A s->match_length = longest_match (s, hash_head);
1592N/A } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1592N/A s->match_length = longest_match_fast (s, hash_head);
1592N/A }
1592N/A#endif
1592N/A /* longest_match() or longest_match_fast() sets match_start */
1592N/A }
1592N/A if (s->match_length >= MIN_MATCH) {
1592N/A check_match(s, s->strstart, s->match_start, s->match_length);
1592N/A
1592N/A _tr_tally_dist(s, s->strstart - s->match_start,
1592N/A s->match_length - MIN_MATCH, bflush);
1592N/A
1592N/A s->lookahead -= s->match_length;
1592N/A
1592N/A /* Insert new strings in the hash table only if the match length
1592N/A * is not too large. This saves time but degrades compression.
1592N/A */
1592N/A#ifndef FASTEST
1592N/A if (s->match_length <= s->max_insert_length &&
1592N/A s->lookahead >= MIN_MATCH) {
1592N/A s->match_length--; /* string at strstart already in table */
1592N/A do {
1592N/A s->strstart++;
1592N/A INSERT_STRING(s, s->strstart, hash_head);
1592N/A /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1592N/A * always MIN_MATCH bytes ahead.
1592N/A */
1592N/A } while (--s->match_length != 0);
1592N/A s->strstart++;
1592N/A } else
1592N/A#endif
1592N/A {
1592N/A s->strstart += s->match_length;
1592N/A s->match_length = 0;
1592N/A s->ins_h = s->window[s->strstart];
1592N/A UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1592N/A#if MIN_MATCH != 3
1592N/A Call UPDATE_HASH() MIN_MATCH-3 more times
1592N/A#endif
1592N/A /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1592N/A * matter since it will be recomputed at next deflate call.
1592N/A */
1592N/A }
1592N/A } else {
1592N/A /* No match, output a literal byte */
1592N/A Tracevv((stderr,"%c", s->window[s->strstart]));
1592N/A _tr_tally_lit (s, s->window[s->strstart], bflush);
1592N/A s->lookahead--;
1592N/A s->strstart++;
1592N/A }
1592N/A if (bflush) FLUSH_BLOCK(s, 0);
1592N/A }
1592N/A FLUSH_BLOCK(s, flush == Z_FINISH);
1592N/A return flush == Z_FINISH ? finish_done : block_done;
1592N/A}
1592N/A
1592N/A#ifndef FASTEST
1592N/A/* ===========================================================================
1592N/A * Same as above, but achieves better compression. We use a lazy
1592N/A * evaluation for matches: a match is finally adopted only if there is
1592N/A * no better match at the next window position.
1592N/A */
1592N/Alocal block_state deflate_slow(s, flush)
1592N/A deflate_state *s;
1592N/A int flush;
1592N/A{
1592N/A IPos hash_head = NIL; /* head of hash chain */
1592N/A int bflush; /* set if current block must be flushed */
1592N/A
1592N/A /* Process the input block. */
1592N/A for (;;) {
1592N/A /* Make sure that we always have enough lookahead, except
1592N/A * at the end of the input file. We need MAX_MATCH bytes
1592N/A * for the next match, plus MIN_MATCH bytes to insert the
1592N/A * string following the next match.
1592N/A */
1592N/A if (s->lookahead < MIN_LOOKAHEAD) {
1592N/A fill_window(s);
1592N/A if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1592N/A return need_more;
1592N/A }
1592N/A if (s->lookahead == 0) break; /* flush the current block */
1592N/A }
1592N/A
1592N/A /* Insert the string window[strstart .. strstart+2] in the
1592N/A * dictionary, and set hash_head to the head of the hash chain:
1592N/A */
1592N/A if (s->lookahead >= MIN_MATCH) {
1592N/A INSERT_STRING(s, s->strstart, hash_head);
1592N/A }
1592N/A
1592N/A /* Find the longest match, discarding those <= prev_length.
1592N/A */
1592N/A s->prev_length = s->match_length, s->prev_match = s->match_start;
1592N/A s->match_length = MIN_MATCH-1;
1592N/A
1592N/A if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1592N/A s->strstart - hash_head <= MAX_DIST(s)) {
1592N/A /* To simplify the code, we prevent matches with the string
1592N/A * of window index 0 (in particular we have to avoid a match
1592N/A * of the string with itself at the start of the input file).
1592N/A */
1592N/A if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1592N/A s->match_length = longest_match (s, hash_head);
1592N/A } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1592N/A s->match_length = longest_match_fast (s, hash_head);
1592N/A }
1592N/A /* longest_match() or longest_match_fast() sets match_start */
1592N/A
1592N/A if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1592N/A#if TOO_FAR <= 32767
1592N/A || (s->match_length == MIN_MATCH &&
1592N/A s->strstart - s->match_start > TOO_FAR)
1592N/A#endif
1592N/A )) {
1592N/A
1592N/A /* If prev_match is also MIN_MATCH, match_start is garbage
1592N/A * but we will ignore the current match anyway.
1592N/A */
1592N/A s->match_length = MIN_MATCH-1;
1592N/A }
1592N/A }
1592N/A /* If there was a match at the previous step and the current
1592N/A * match is not better, output the previous match:
1592N/A */
1592N/A if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1592N/A uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1592N/A /* Do not insert strings in hash table beyond this. */
1592N/A
1592N/A check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1592N/A
1592N/A _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1592N/A s->prev_length - MIN_MATCH, bflush);
1592N/A
1592N/A /* Insert in hash table all strings up to the end of the match.
1592N/A * strstart-1 and strstart are already inserted. If there is not
1592N/A * enough lookahead, the last two strings are not inserted in
1592N/A * the hash table.
1592N/A */
1592N/A s->lookahead -= s->prev_length-1;
1592N/A s->prev_length -= 2;
1592N/A do {
1592N/A if (++s->strstart <= max_insert) {
1592N/A INSERT_STRING(s, s->strstart, hash_head);
1592N/A }
1592N/A } while (--s->prev_length != 0);
1592N/A s->match_available = 0;
1592N/A s->match_length = MIN_MATCH-1;
1592N/A s->strstart++;
1592N/A
1592N/A if (bflush) FLUSH_BLOCK(s, 0);
1592N/A
1592N/A } else if (s->match_available) {
1592N/A /* If there was no match at the previous position, output a
1592N/A * single literal. If there was a match but the current match
1592N/A * is longer, truncate the previous match to a single literal.
1592N/A */
1592N/A Tracevv((stderr,"%c", s->window[s->strstart-1]));
1592N/A _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1592N/A if (bflush) {
1592N/A FLUSH_BLOCK_ONLY(s, 0);
1592N/A }
1592N/A s->strstart++;
1592N/A s->lookahead--;
1592N/A if (s->strm->avail_out == 0) return need_more;
1592N/A } else {
1592N/A /* There is no previous match to compare with, wait for
1592N/A * the next step to decide.
1592N/A */
1592N/A s->match_available = 1;
1592N/A s->strstart++;
1592N/A s->lookahead--;
1592N/A }
1592N/A }
1592N/A Assert (flush != Z_NO_FLUSH, "no flush?");
1592N/A if (s->match_available) {
1592N/A Tracevv((stderr,"%c", s->window[s->strstart-1]));
1592N/A _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1592N/A s->match_available = 0;
1592N/A }
1592N/A FLUSH_BLOCK(s, flush == Z_FINISH);
1592N/A return flush == Z_FINISH ? finish_done : block_done;
1592N/A}
1592N/A#endif /* FASTEST */
1592N/A
1592N/A#if 0
1592N/A/* ===========================================================================
1592N/A * For Z_RLE, simply look for runs of bytes, generate matches only of distance
1592N/A * one. Do not maintain a hash table. (It will be regenerated if this run of
1592N/A * deflate switches away from Z_RLE.)
1592N/A */
1592N/Alocal block_state deflate_rle(s, flush)
1592N/A deflate_state *s;
1592N/A int flush;
1592N/A{
1592N/A int bflush; /* set if current block must be flushed */
1592N/A uInt run; /* length of run */
1592N/A uInt max; /* maximum length of run */
1592N/A uInt prev; /* byte at distance one to match */
1592N/A Bytef *scan; /* scan for end of run */
1592N/A
1592N/A for (;;) {
1592N/A /* Make sure that we always have enough lookahead, except
1592N/A * at the end of the input file. We need MAX_MATCH bytes
1592N/A * for the longest encodable run.
1592N/A */
1592N/A if (s->lookahead < MAX_MATCH) {
1592N/A fill_window(s);
1592N/A if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
1592N/A return need_more;
1592N/A }
1592N/A if (s->lookahead == 0) break; /* flush the current block */
1592N/A }
1592N/A
1592N/A /* See how many times the previous byte repeats */
1592N/A run = 0;
1592N/A if (s->strstart > 0) { /* if there is a previous byte, that is */
1592N/A max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
1592N/A scan = s->window + s->strstart - 1;
1592N/A prev = *scan++;
1592N/A do {
1592N/A if (*scan++ != prev)
1592N/A break;
1592N/A } while (++run < max);
1592N/A }
1592N/A
1592N/A /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1592N/A if (run >= MIN_MATCH) {
1592N/A check_match(s, s->strstart, s->strstart - 1, run);
1592N/A _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
1592N/A s->lookahead -= run;
1592N/A s->strstart += run;
1592N/A } else {
1592N/A /* No match, output a literal byte */
1592N/A Tracevv((stderr,"%c", s->window[s->strstart]));
1592N/A _tr_tally_lit (s, s->window[s->strstart], bflush);
1592N/A s->lookahead--;
1592N/A s->strstart++;
1592N/A }
1592N/A if (bflush) FLUSH_BLOCK(s, 0);
1592N/A }
1592N/A FLUSH_BLOCK(s, flush == Z_FINISH);
1592N/A return flush == Z_FINISH ? finish_done : block_done;
1592N/A}
1592N/A#endif