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