Path: blob/master/src/java.base/share/native/libzip/zlib/deflate.c
41153 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* deflate.c -- compress data using the deflation algorithm25* Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler26* For conditions of distribution and use, see copyright notice in zlib.h27*/2829/*30* ALGORITHM31*32* The "deflation" process depends on being able to identify portions33* of the input text which are identical to earlier input (within a34* sliding window trailing behind the input currently being processed).35*36* The most straightforward technique turns out to be the fastest for37* most input files: try all possible matches and select the longest.38* The key feature of this algorithm is that insertions into the string39* dictionary are very simple and thus fast, and deletions are avoided40* completely. Insertions are performed at each input character, whereas41* string matches are performed only when the previous match ends. So it42* is preferable to spend more time in matches to allow very fast string43* insertions and avoid deletions. The matching algorithm for small44* strings is inspired from that of Rabin & Karp. A brute force approach45* is used to find longer strings when a small match has been found.46* A similar algorithm is used in comic (by Jan-Mark Wams) and freeze47* (by Leonid Broukhis).48* A previous version of this file used a more sophisticated algorithm49* (by Fiala and Greene) which is guaranteed to run in linear amortized50* time, but has a larger average cost, uses more memory and is patented.51* However the F&G algorithm may be faster for some highly redundant52* files if the parameter max_chain_length (described below) is too large.53*54* ACKNOWLEDGEMENTS55*56* The idea of lazy evaluation of matches is due to Jan-Mark Wams, and57* I found it in 'freeze' written by Leonid Broukhis.58* Thanks to many people for bug reports and testing.59*60* REFERENCES61*62* Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".63* Available in http://tools.ietf.org/html/rfc195164*65* A description of the Rabin and Karp algorithm is given in the book66* "Algorithms" by R. Sedgewick, Addison-Wesley, p252.67*68* Fiala,E.R., and Greene,D.H.69* Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-59570*71*/7273/* @(#) $Id$ */7475#include "deflate.h"7677const char deflate_copyright[] =78" deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler ";79/*80If you use the zlib library in a product, an acknowledgment is welcome81in the documentation of your product. If for some reason you cannot82include such an acknowledgment, I would appreciate that you keep this83copyright string in the executable of your product.84*/8586/* ===========================================================================87* Function prototypes.88*/89typedef enum {90need_more, /* block not completed, need more input or more output */91block_done, /* block flush performed */92finish_started, /* finish started, need only more output at next deflate */93finish_done /* finish done, accept no more input or output */94} block_state;9596typedef block_state (*compress_func) OF((deflate_state *s, int flush));97/* Compression function. Returns the block state after the call. */9899local int deflateStateCheck OF((z_streamp strm));100local void slide_hash OF((deflate_state *s));101local void fill_window OF((deflate_state *s));102local block_state deflate_stored OF((deflate_state *s, int flush));103local block_state deflate_fast OF((deflate_state *s, int flush));104#ifndef FASTEST105local block_state deflate_slow OF((deflate_state *s, int flush));106#endif107local block_state deflate_rle OF((deflate_state *s, int flush));108local block_state deflate_huff OF((deflate_state *s, int flush));109local void lm_init OF((deflate_state *s));110local void putShortMSB OF((deflate_state *s, uInt b));111local void flush_pending OF((z_streamp strm));112local unsigned read_buf OF((z_streamp strm, Bytef *buf, unsigned size));113#ifdef ASMV114# pragma message("Assembler code may have bugs -- use at your own risk")115void match_init OF((void)); /* asm code initialization */116uInt longest_match OF((deflate_state *s, IPos cur_match));117#else118local uInt longest_match OF((deflate_state *s, IPos cur_match));119#endif120121#ifdef ZLIB_DEBUG122local void check_match OF((deflate_state *s, IPos start, IPos match,123int length));124#endif125126/* ===========================================================================127* Local data128*/129130#define NIL 0131/* Tail of hash chains */132133#ifndef TOO_FAR134# define TOO_FAR 4096135#endif136/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */137138/* Values for max_lazy_match, good_match and max_chain_length, depending on139* the desired pack level (0..9). The values given below have been tuned to140* exclude worst case performance for pathological files. Better values may be141* found for specific files.142*/143typedef struct config_s {144ush good_length; /* reduce lazy search above this match length */145ush max_lazy; /* do not perform lazy search above this match length */146ush nice_length; /* quit search above this match length */147ush max_chain;148compress_func func;149} config;150151#ifdef FASTEST152local const config configuration_table[2] = {153/* good lazy nice chain */154/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */155/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */156#else157local const config configuration_table[10] = {158/* good lazy nice chain */159/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */160/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */161/* 2 */ {4, 5, 16, 8, deflate_fast},162/* 3 */ {4, 6, 32, 32, deflate_fast},163164/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */165/* 5 */ {8, 16, 32, 32, deflate_slow},166/* 6 */ {8, 16, 128, 128, deflate_slow},167/* 7 */ {8, 32, 128, 256, deflate_slow},168/* 8 */ {32, 128, 258, 1024, deflate_slow},169/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */170#endif171172/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4173* For deflate_fast() (levels <= 3) good is ignored and lazy has a different174* meaning.175*/176177/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */178#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))179180/* ===========================================================================181* Update a hash value with the given input byte182* IN assertion: all calls to UPDATE_HASH are made with consecutive input183* characters, so that a running hash key can be computed from the previous184* key instead of complete recalculation each time.185*/186#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)187188189/* ===========================================================================190* Insert string str in the dictionary and set match_head to the previous head191* of the hash chain (the most recent string with same hash key). Return192* the previous length of the hash chain.193* If this file is compiled with -DFASTEST, the compression level is forced194* to 1, and no hash chains are maintained.195* IN assertion: all calls to INSERT_STRING are made with consecutive input196* characters and the first MIN_MATCH bytes of str are valid (except for197* the last MIN_MATCH-1 bytes of the input file).198*/199#ifdef FASTEST200#define INSERT_STRING(s, str, match_head) \201(UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \202match_head = s->head[s->ins_h], \203s->head[s->ins_h] = (Pos)(str))204#else205#define INSERT_STRING(s, str, match_head) \206(UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \207match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \208s->head[s->ins_h] = (Pos)(str))209#endif210211/* ===========================================================================212* Initialize the hash table (avoiding 64K overflow for 16 bit systems).213* prev[] will be initialized on the fly.214*/215#define CLEAR_HASH(s) \216s->head[s->hash_size-1] = NIL; \217zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));218219/* ===========================================================================220* Slide the hash table when sliding the window down (could be avoided with 32221* bit values at the expense of memory usage). We slide even when level == 0 to222* keep the hash table consistent if we switch back to level > 0 later.223*/224local void slide_hash(s)225deflate_state *s;226{227unsigned n, m;228Posf *p;229uInt wsize = s->w_size;230231n = s->hash_size;232p = &s->head[n];233do {234m = *--p;235*p = (Pos)(m >= wsize ? m - wsize : NIL);236} while (--n);237n = wsize;238#ifndef FASTEST239p = &s->prev[n];240do {241m = *--p;242*p = (Pos)(m >= wsize ? m - wsize : NIL);243/* If n is not on any hash chain, prev[n] is garbage but244* its value will never be used.245*/246} while (--n);247#endif248}249250/* ========================================================================= */251int ZEXPORT deflateInit_(strm, level, version, stream_size)252z_streamp strm;253int level;254const char *version;255int stream_size;256{257return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,258Z_DEFAULT_STRATEGY, version, stream_size);259/* To do: ignore strm->next_in if we use it as window */260}261262/* ========================================================================= */263int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,264version, stream_size)265z_streamp strm;266int level;267int method;268int windowBits;269int memLevel;270int strategy;271const char *version;272int stream_size;273{274deflate_state *s;275int wrap = 1;276static const char my_version[] = ZLIB_VERSION;277278ushf *overlay;279/* We overlay pending_buf and d_buf+l_buf. This works since the average280* output size for (length,distance) codes is <= 24 bits.281*/282283if (version == Z_NULL || version[0] != my_version[0] ||284stream_size != sizeof(z_stream)) {285return Z_VERSION_ERROR;286}287if (strm == Z_NULL) return Z_STREAM_ERROR;288289strm->msg = Z_NULL;290if (strm->zalloc == (alloc_func)0) {291#ifdef Z_SOLO292return Z_STREAM_ERROR;293#else294strm->zalloc = zcalloc;295strm->opaque = (voidpf)0;296#endif297}298if (strm->zfree == (free_func)0)299#ifdef Z_SOLO300return Z_STREAM_ERROR;301#else302strm->zfree = zcfree;303#endif304305#ifdef FASTEST306if (level != 0) level = 1;307#else308if (level == Z_DEFAULT_COMPRESSION) level = 6;309#endif310311if (windowBits < 0) { /* suppress zlib wrapper */312wrap = 0;313windowBits = -windowBits;314}315#ifdef GZIP316else if (windowBits > 15) {317wrap = 2; /* write gzip wrapper instead */318windowBits -= 16;319}320#endif321if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||322windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||323strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {324return Z_STREAM_ERROR;325}326if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */327s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));328if (s == Z_NULL) return Z_MEM_ERROR;329strm->state = (struct internal_state FAR *)s;330s->strm = strm;331s->status = INIT_STATE; /* to pass state test in deflateReset() */332333s->wrap = wrap;334s->gzhead = Z_NULL;335s->w_bits = (uInt)windowBits;336s->w_size = 1 << s->w_bits;337s->w_mask = s->w_size - 1;338339s->hash_bits = (uInt)memLevel + 7;340s->hash_size = 1 << s->hash_bits;341s->hash_mask = s->hash_size - 1;342s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);343344s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));345s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));346s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));347348s->high_water = 0; /* nothing written to s->window yet */349350s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */351352overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);353s->pending_buf = (uchf *) overlay;354s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);355356if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||357s->pending_buf == Z_NULL) {358s->status = FINISH_STATE;359strm->msg = ERR_MSG(Z_MEM_ERROR);360deflateEnd (strm);361return Z_MEM_ERROR;362}363s->d_buf = overlay + s->lit_bufsize/sizeof(ush);364s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;365366s->level = level;367s->strategy = strategy;368s->method = (Byte)method;369370return deflateReset(strm);371}372373/* =========================================================================374* Check for a valid deflate stream state. Return 0 if ok, 1 if not.375*/376local int deflateStateCheck (strm)377z_streamp strm;378{379deflate_state *s;380if (strm == Z_NULL ||381strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)382return 1;383s = strm->state;384if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&385#ifdef GZIP386s->status != GZIP_STATE &&387#endif388s->status != EXTRA_STATE &&389s->status != NAME_STATE &&390s->status != COMMENT_STATE &&391s->status != HCRC_STATE &&392s->status != BUSY_STATE &&393s->status != FINISH_STATE))394return 1;395return 0;396}397398/* ========================================================================= */399int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)400z_streamp strm;401const Bytef *dictionary;402uInt dictLength;403{404deflate_state *s;405uInt str, n;406int wrap;407unsigned avail;408z_const unsigned char *next;409410if (deflateStateCheck(strm) || dictionary == Z_NULL)411return Z_STREAM_ERROR;412s = strm->state;413wrap = s->wrap;414if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)415return Z_STREAM_ERROR;416417/* when using zlib wrappers, compute Adler-32 for provided dictionary */418if (wrap == 1)419strm->adler = adler32(strm->adler, dictionary, dictLength);420s->wrap = 0; /* avoid computing Adler-32 in read_buf */421422/* if dictionary would fill window, just replace the history */423if (dictLength >= s->w_size) {424if (wrap == 0) { /* already empty otherwise */425CLEAR_HASH(s);426s->strstart = 0;427s->block_start = 0L;428s->insert = 0;429}430dictionary += dictLength - s->w_size; /* use the tail */431dictLength = s->w_size;432}433434/* insert dictionary into window and hash */435avail = strm->avail_in;436next = strm->next_in;437strm->avail_in = dictLength;438strm->next_in = (z_const Bytef *)dictionary;439fill_window(s);440while (s->lookahead >= MIN_MATCH) {441str = s->strstart;442n = s->lookahead - (MIN_MATCH-1);443do {444UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);445#ifndef FASTEST446s->prev[str & s->w_mask] = s->head[s->ins_h];447#endif448s->head[s->ins_h] = (Pos)str;449str++;450} while (--n);451s->strstart = str;452s->lookahead = MIN_MATCH-1;453fill_window(s);454}455s->strstart += s->lookahead;456s->block_start = (long)s->strstart;457s->insert = s->lookahead;458s->lookahead = 0;459s->match_length = s->prev_length = MIN_MATCH-1;460s->match_available = 0;461strm->next_in = next;462strm->avail_in = avail;463s->wrap = wrap;464return Z_OK;465}466467/* ========================================================================= */468int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength)469z_streamp strm;470Bytef *dictionary;471uInt *dictLength;472{473deflate_state *s;474uInt len;475476if (deflateStateCheck(strm))477return Z_STREAM_ERROR;478s = strm->state;479len = s->strstart + s->lookahead;480if (len > s->w_size)481len = s->w_size;482if (dictionary != Z_NULL && len)483zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);484if (dictLength != Z_NULL)485*dictLength = len;486return Z_OK;487}488489/* ========================================================================= */490int ZEXPORT deflateResetKeep (strm)491z_streamp strm;492{493deflate_state *s;494495if (deflateStateCheck(strm)) {496return Z_STREAM_ERROR;497}498499strm->total_in = strm->total_out = 0;500strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */501strm->data_type = Z_UNKNOWN;502503s = (deflate_state *)strm->state;504s->pending = 0;505s->pending_out = s->pending_buf;506507if (s->wrap < 0) {508s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */509}510s->status =511#ifdef GZIP512s->wrap == 2 ? GZIP_STATE :513#endif514s->wrap ? INIT_STATE : BUSY_STATE;515strm->adler =516#ifdef GZIP517s->wrap == 2 ? crc32(0L, Z_NULL, 0) :518#endif519adler32(0L, Z_NULL, 0);520s->last_flush = -2;521522_tr_init(s);523524return Z_OK;525}526527/* ========================================================================= */528int ZEXPORT deflateReset (strm)529z_streamp strm;530{531int ret;532533ret = deflateResetKeep(strm);534if (ret == Z_OK)535lm_init(strm->state);536return ret;537}538539/* ========================================================================= */540int ZEXPORT deflateSetHeader (strm, head)541z_streamp strm;542gz_headerp head;543{544if (deflateStateCheck(strm) || strm->state->wrap != 2)545return Z_STREAM_ERROR;546strm->state->gzhead = head;547return Z_OK;548}549550/* ========================================================================= */551int ZEXPORT deflatePending (strm, pending, bits)552unsigned *pending;553int *bits;554z_streamp strm;555{556if (deflateStateCheck(strm)) return Z_STREAM_ERROR;557if (pending != Z_NULL)558*pending = strm->state->pending;559if (bits != Z_NULL)560*bits = strm->state->bi_valid;561return Z_OK;562}563564/* ========================================================================= */565int ZEXPORT deflatePrime (strm, bits, value)566z_streamp strm;567int bits;568int value;569{570deflate_state *s;571int put;572573if (deflateStateCheck(strm)) return Z_STREAM_ERROR;574s = strm->state;575if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))576return Z_BUF_ERROR;577do {578put = Buf_size - s->bi_valid;579if (put > bits)580put = bits;581s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);582s->bi_valid += put;583_tr_flush_bits(s);584value >>= put;585bits -= put;586} while (bits);587return Z_OK;588}589590/* ========================================================================= */591int ZEXPORT deflateParams(strm, level, strategy)592z_streamp strm;593int level;594int strategy;595{596deflate_state *s;597compress_func func;598599if (deflateStateCheck(strm)) return Z_STREAM_ERROR;600s = strm->state;601602#ifdef FASTEST603if (level != 0) level = 1;604#else605if (level == Z_DEFAULT_COMPRESSION) level = 6;606#endif607if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {608return Z_STREAM_ERROR;609}610func = configuration_table[s->level].func;611612if ((strategy != s->strategy || func != configuration_table[level].func) &&613s->last_flush != -2) {614/* Flush the last buffer: */615int err = deflate(strm, Z_BLOCK);616if (err == Z_STREAM_ERROR)617return err;618if (strm->avail_out == 0)619return Z_BUF_ERROR;620}621if (s->level != level) {622if (s->level == 0 && s->matches != 0) {623if (s->matches == 1)624slide_hash(s);625else626CLEAR_HASH(s);627s->matches = 0;628}629s->level = level;630s->max_lazy_match = configuration_table[level].max_lazy;631s->good_match = configuration_table[level].good_length;632s->nice_match = configuration_table[level].nice_length;633s->max_chain_length = configuration_table[level].max_chain;634}635s->strategy = strategy;636return Z_OK;637}638639/* ========================================================================= */640int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)641z_streamp strm;642int good_length;643int max_lazy;644int nice_length;645int max_chain;646{647deflate_state *s;648649if (deflateStateCheck(strm)) return Z_STREAM_ERROR;650s = strm->state;651s->good_match = (uInt)good_length;652s->max_lazy_match = (uInt)max_lazy;653s->nice_match = nice_length;654s->max_chain_length = (uInt)max_chain;655return Z_OK;656}657658/* =========================================================================659* For the default windowBits of 15 and memLevel of 8, this function returns660* a close to exact, as well as small, upper bound on the compressed size.661* They are coded as constants here for a reason--if the #define's are662* changed, then this function needs to be changed as well. The return663* value for 15 and 8 only works for those exact settings.664*665* For any setting other than those defaults for windowBits and memLevel,666* the value returned is a conservative worst case for the maximum expansion667* resulting from using fixed blocks instead of stored blocks, which deflate668* can emit on compressed data for some combinations of the parameters.669*670* This function could be more sophisticated to provide closer upper bounds for671* every combination of windowBits and memLevel. But even the conservative672* upper bound of about 14% expansion does not seem onerous for output buffer673* allocation.674*/675uLong ZEXPORT deflateBound(strm, sourceLen)676z_streamp strm;677uLong sourceLen;678{679deflate_state *s;680uLong complen, wraplen;681682/* conservative upper bound for compressed data */683complen = sourceLen +684((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;685686/* if can't get parameters, return conservative bound plus zlib wrapper */687if (deflateStateCheck(strm))688return complen + 6;689690/* compute wrapper length */691s = strm->state;692switch (s->wrap) {693case 0: /* raw deflate */694wraplen = 0;695break;696case 1: /* zlib wrapper */697wraplen = 6 + (s->strstart ? 4 : 0);698break;699#ifdef GZIP700case 2: /* gzip wrapper */701wraplen = 18;702if (s->gzhead != Z_NULL) { /* user-supplied gzip header */703Bytef *str;704if (s->gzhead->extra != Z_NULL)705wraplen += 2 + s->gzhead->extra_len;706str = s->gzhead->name;707if (str != Z_NULL)708do {709wraplen++;710} while (*str++);711str = s->gzhead->comment;712if (str != Z_NULL)713do {714wraplen++;715} while (*str++);716if (s->gzhead->hcrc)717wraplen += 2;718}719break;720#endif721default: /* for compiler happiness */722wraplen = 6;723}724725/* if not default parameters, return conservative bound */726if (s->w_bits != 15 || s->hash_bits != 8 + 7)727return complen + wraplen;728729/* default settings: return tight bound for that case */730return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +731(sourceLen >> 25) + 13 - 6 + wraplen;732}733734/* =========================================================================735* Put a short in the pending buffer. The 16-bit value is put in MSB order.736* IN assertion: the stream state is correct and there is enough room in737* pending_buf.738*/739local void putShortMSB (s, b)740deflate_state *s;741uInt b;742{743put_byte(s, (Byte)(b >> 8));744put_byte(s, (Byte)(b & 0xff));745}746747/* =========================================================================748* Flush as much pending output as possible. All deflate() output, except for749* some deflate_stored() output, goes through this function so some750* applications may wish to modify it to avoid allocating a large751* strm->next_out buffer and copying into it. (See also read_buf()).752*/753local void flush_pending(strm)754z_streamp strm;755{756unsigned len;757deflate_state *s = strm->state;758759_tr_flush_bits(s);760len = s->pending;761if (len > strm->avail_out) len = strm->avail_out;762if (len == 0) return;763764zmemcpy(strm->next_out, s->pending_out, len);765strm->next_out += len;766s->pending_out += len;767strm->total_out += len;768strm->avail_out -= len;769s->pending -= len;770if (s->pending == 0) {771s->pending_out = s->pending_buf;772}773}774775/* ===========================================================================776* Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].777*/778#define HCRC_UPDATE(beg) \779do { \780if (s->gzhead->hcrc && s->pending > (beg)) \781strm->adler = crc32(strm->adler, s->pending_buf + (beg), \782s->pending - (beg)); \783} while (0)784785/* ========================================================================= */786int ZEXPORT deflate (strm, flush)787z_streamp strm;788int flush;789{790int old_flush; /* value of flush param for previous deflate call */791deflate_state *s;792793if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {794return Z_STREAM_ERROR;795}796s = strm->state;797798if (strm->next_out == Z_NULL ||799(strm->avail_in != 0 && strm->next_in == Z_NULL) ||800(s->status == FINISH_STATE && flush != Z_FINISH)) {801ERR_RETURN(strm, Z_STREAM_ERROR);802}803if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);804805old_flush = s->last_flush;806s->last_flush = flush;807808/* Flush as much pending output as possible */809if (s->pending != 0) {810flush_pending(strm);811if (strm->avail_out == 0) {812/* Since avail_out is 0, deflate will be called again with813* more output space, but possibly with both pending and814* avail_in equal to zero. There won't be anything to do,815* but this is not an error situation so make sure we816* return OK instead of BUF_ERROR at next call of deflate:817*/818s->last_flush = -1;819return Z_OK;820}821822/* Make sure there is something to do and avoid duplicate consecutive823* flushes. For repeated and useless calls with Z_FINISH, we keep824* returning Z_STREAM_END instead of Z_BUF_ERROR.825*/826} else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&827flush != Z_FINISH) {828ERR_RETURN(strm, Z_BUF_ERROR);829}830831/* User must not provide more input after the first FINISH: */832if (s->status == FINISH_STATE && strm->avail_in != 0) {833ERR_RETURN(strm, Z_BUF_ERROR);834}835836/* Write the header */837if (s->status == INIT_STATE) {838/* zlib header */839uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;840uInt level_flags;841842if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)843level_flags = 0;844else if (s->level < 6)845level_flags = 1;846else if (s->level == 6)847level_flags = 2;848else849level_flags = 3;850header |= (level_flags << 6);851if (s->strstart != 0) header |= PRESET_DICT;852header += 31 - (header % 31);853854putShortMSB(s, header);855856/* Save the adler32 of the preset dictionary: */857if (s->strstart != 0) {858putShortMSB(s, (uInt)(strm->adler >> 16));859putShortMSB(s, (uInt)(strm->adler & 0xffff));860}861strm->adler = adler32(0L, Z_NULL, 0);862s->status = BUSY_STATE;863864/* Compression must start with an empty pending buffer */865flush_pending(strm);866if (s->pending != 0) {867s->last_flush = -1;868return Z_OK;869}870}871#ifdef GZIP872if (s->status == GZIP_STATE) {873/* gzip header */874strm->adler = crc32(0L, Z_NULL, 0);875put_byte(s, 31);876put_byte(s, 139);877put_byte(s, 8);878if (s->gzhead == Z_NULL) {879put_byte(s, 0);880put_byte(s, 0);881put_byte(s, 0);882put_byte(s, 0);883put_byte(s, 0);884put_byte(s, s->level == 9 ? 2 :885(s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?8864 : 0));887put_byte(s, OS_CODE);888s->status = BUSY_STATE;889890/* Compression must start with an empty pending buffer */891flush_pending(strm);892if (s->pending != 0) {893s->last_flush = -1;894return Z_OK;895}896}897else {898put_byte(s, (s->gzhead->text ? 1 : 0) +899(s->gzhead->hcrc ? 2 : 0) +900(s->gzhead->extra == Z_NULL ? 0 : 4) +901(s->gzhead->name == Z_NULL ? 0 : 8) +902(s->gzhead->comment == Z_NULL ? 0 : 16)903);904put_byte(s, (Byte)(s->gzhead->time & 0xff));905put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));906put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));907put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));908put_byte(s, s->level == 9 ? 2 :909(s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?9104 : 0));911put_byte(s, s->gzhead->os & 0xff);912if (s->gzhead->extra != Z_NULL) {913put_byte(s, s->gzhead->extra_len & 0xff);914put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);915}916if (s->gzhead->hcrc)917strm->adler = crc32(strm->adler, s->pending_buf,918s->pending);919s->gzindex = 0;920s->status = EXTRA_STATE;921}922}923if (s->status == EXTRA_STATE) {924if (s->gzhead->extra != Z_NULL) {925ulg beg = s->pending; /* start of bytes to update crc */926uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;927while (s->pending + left > s->pending_buf_size) {928uInt copy = s->pending_buf_size - s->pending;929zmemcpy(s->pending_buf + s->pending,930s->gzhead->extra + s->gzindex, copy);931s->pending = s->pending_buf_size;932HCRC_UPDATE(beg);933s->gzindex += copy;934flush_pending(strm);935if (s->pending != 0) {936s->last_flush = -1;937return Z_OK;938}939beg = 0;940left -= copy;941}942zmemcpy(s->pending_buf + s->pending,943s->gzhead->extra + s->gzindex, left);944s->pending += left;945HCRC_UPDATE(beg);946s->gzindex = 0;947}948s->status = NAME_STATE;949}950if (s->status == NAME_STATE) {951if (s->gzhead->name != Z_NULL) {952ulg beg = s->pending; /* start of bytes to update crc */953int val;954do {955if (s->pending == s->pending_buf_size) {956HCRC_UPDATE(beg);957flush_pending(strm);958if (s->pending != 0) {959s->last_flush = -1;960return Z_OK;961}962beg = 0;963}964val = s->gzhead->name[s->gzindex++];965put_byte(s, val);966} while (val != 0);967HCRC_UPDATE(beg);968s->gzindex = 0;969}970s->status = COMMENT_STATE;971}972if (s->status == COMMENT_STATE) {973if (s->gzhead->comment != Z_NULL) {974ulg beg = s->pending; /* start of bytes to update crc */975int val;976do {977if (s->pending == s->pending_buf_size) {978HCRC_UPDATE(beg);979flush_pending(strm);980if (s->pending != 0) {981s->last_flush = -1;982return Z_OK;983}984beg = 0;985}986val = s->gzhead->comment[s->gzindex++];987put_byte(s, val);988} while (val != 0);989HCRC_UPDATE(beg);990}991s->status = HCRC_STATE;992}993if (s->status == HCRC_STATE) {994if (s->gzhead->hcrc) {995if (s->pending + 2 > s->pending_buf_size) {996flush_pending(strm);997if (s->pending != 0) {998s->last_flush = -1;999return Z_OK;1000}1001}1002put_byte(s, (Byte)(strm->adler & 0xff));1003put_byte(s, (Byte)((strm->adler >> 8) & 0xff));1004strm->adler = crc32(0L, Z_NULL, 0);1005}1006s->status = BUSY_STATE;10071008/* Compression must start with an empty pending buffer */1009flush_pending(strm);1010if (s->pending != 0) {1011s->last_flush = -1;1012return Z_OK;1013}1014}1015#endif10161017/* Start a new block or continue the current one.1018*/1019if (strm->avail_in != 0 || s->lookahead != 0 ||1020(flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {1021block_state bstate;10221023bstate = s->level == 0 ? deflate_stored(s, flush) :1024s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :1025s->strategy == Z_RLE ? deflate_rle(s, flush) :1026(*(configuration_table[s->level].func))(s, flush);10271028if (bstate == finish_started || bstate == finish_done) {1029s->status = FINISH_STATE;1030}1031if (bstate == need_more || bstate == finish_started) {1032if (strm->avail_out == 0) {1033s->last_flush = -1; /* avoid BUF_ERROR next call, see above */1034}1035return Z_OK;1036/* If flush != Z_NO_FLUSH && avail_out == 0, the next call1037* of deflate should use the same flush parameter to make sure1038* that the flush is complete. So we don't have to output an1039* empty block here, this will be done at next call. This also1040* ensures that for a very small output buffer, we emit at most1041* one empty block.1042*/1043}1044if (bstate == block_done) {1045if (flush == Z_PARTIAL_FLUSH) {1046_tr_align(s);1047} else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */1048_tr_stored_block(s, (char*)0, 0L, 0);1049/* For a full flush, this empty block will be recognized1050* as a special marker by inflate_sync().1051*/1052if (flush == Z_FULL_FLUSH) {1053CLEAR_HASH(s); /* forget history */1054if (s->lookahead == 0) {1055s->strstart = 0;1056s->block_start = 0L;1057s->insert = 0;1058}1059}1060}1061flush_pending(strm);1062if (strm->avail_out == 0) {1063s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */1064return Z_OK;1065}1066}1067}10681069if (flush != Z_FINISH) return Z_OK;1070if (s->wrap <= 0) return Z_STREAM_END;10711072/* Write the trailer */1073#ifdef GZIP1074if (s->wrap == 2) {1075put_byte(s, (Byte)(strm->adler & 0xff));1076put_byte(s, (Byte)((strm->adler >> 8) & 0xff));1077put_byte(s, (Byte)((strm->adler >> 16) & 0xff));1078put_byte(s, (Byte)((strm->adler >> 24) & 0xff));1079put_byte(s, (Byte)(strm->total_in & 0xff));1080put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));1081put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));1082put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));1083}1084else1085#endif1086{1087putShortMSB(s, (uInt)(strm->adler >> 16));1088putShortMSB(s, (uInt)(strm->adler & 0xffff));1089}1090flush_pending(strm);1091/* If avail_out is zero, the application will call deflate again1092* to flush the rest.1093*/1094if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */1095return s->pending != 0 ? Z_OK : Z_STREAM_END;1096}10971098/* ========================================================================= */1099int ZEXPORT deflateEnd (strm)1100z_streamp strm;1101{1102int status;11031104if (deflateStateCheck(strm)) return Z_STREAM_ERROR;11051106status = strm->state->status;11071108/* Deallocate in reverse order of allocations: */1109TRY_FREE(strm, strm->state->pending_buf);1110TRY_FREE(strm, strm->state->head);1111TRY_FREE(strm, strm->state->prev);1112TRY_FREE(strm, strm->state->window);11131114ZFREE(strm, strm->state);1115strm->state = Z_NULL;11161117return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;1118}11191120/* =========================================================================1121* Copy the source state to the destination state.1122* To simplify the source, this is not supported for 16-bit MSDOS (which1123* doesn't have enough memory anyway to duplicate compression states).1124*/1125int ZEXPORT deflateCopy (dest, source)1126z_streamp dest;1127z_streamp source;1128{1129#ifdef MAXSEG_64K1130return Z_STREAM_ERROR;1131#else1132deflate_state *ds;1133deflate_state *ss;1134ushf *overlay;113511361137if (deflateStateCheck(source) || dest == Z_NULL) {1138return Z_STREAM_ERROR;1139}11401141ss = source->state;11421143zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));11441145ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));1146if (ds == Z_NULL) return Z_MEM_ERROR;1147dest->state = (struct internal_state FAR *) ds;1148zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));1149ds->strm = dest;11501151ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));1152ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));1153ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));1154overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);1155ds->pending_buf = (uchf *) overlay;11561157if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||1158ds->pending_buf == Z_NULL) {1159deflateEnd (dest);1160return Z_MEM_ERROR;1161}1162/* following zmemcpy do not work for 16-bit MSDOS */1163zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));1164zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));1165zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));1166zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);11671168ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);1169ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);1170ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;11711172ds->l_desc.dyn_tree = ds->dyn_ltree;1173ds->d_desc.dyn_tree = ds->dyn_dtree;1174ds->bl_desc.dyn_tree = ds->bl_tree;11751176return Z_OK;1177#endif /* MAXSEG_64K */1178}11791180/* ===========================================================================1181* Read a new buffer from the current input stream, update the adler321182* and total number of bytes read. All deflate() input goes through1183* this function so some applications may wish to modify it to avoid1184* allocating a large strm->next_in buffer and copying from it.1185* (See also flush_pending()).1186*/1187local unsigned read_buf(strm, buf, size)1188z_streamp strm;1189Bytef *buf;1190unsigned size;1191{1192unsigned len = strm->avail_in;11931194if (len > size) len = size;1195if (len == 0) return 0;11961197strm->avail_in -= len;11981199zmemcpy(buf, strm->next_in, len);1200if (strm->state->wrap == 1) {1201strm->adler = adler32(strm->adler, buf, len);1202}1203#ifdef GZIP1204else if (strm->state->wrap == 2) {1205strm->adler = crc32(strm->adler, buf, len);1206}1207#endif1208strm->next_in += len;1209strm->total_in += len;12101211return len;1212}12131214/* ===========================================================================1215* Initialize the "longest match" routines for a new zlib stream1216*/1217local void lm_init (s)1218deflate_state *s;1219{1220s->window_size = (ulg)2L*s->w_size;12211222CLEAR_HASH(s);12231224/* Set the default configuration parameters:1225*/1226s->max_lazy_match = configuration_table[s->level].max_lazy;1227s->good_match = configuration_table[s->level].good_length;1228s->nice_match = configuration_table[s->level].nice_length;1229s->max_chain_length = configuration_table[s->level].max_chain;12301231s->strstart = 0;1232s->block_start = 0L;1233s->lookahead = 0;1234s->insert = 0;1235s->match_length = s->prev_length = MIN_MATCH-1;1236s->match_available = 0;1237s->ins_h = 0;1238#ifndef FASTEST1239#ifdef ASMV1240match_init(); /* initialize the asm code */1241#endif1242#endif1243}12441245#ifndef FASTEST1246/* ===========================================================================1247* Set match_start to the longest match starting at the given string and1248* return its length. Matches shorter or equal to prev_length are discarded,1249* in which case the result is equal to prev_length and match_start is1250* garbage.1251* IN assertions: cur_match is the head of the hash chain for the current1252* string (strstart) and its distance is <= MAX_DIST, and prev_length >= 11253* OUT assertion: the match length is not greater than s->lookahead.1254*/1255#ifndef ASMV1256/* For 80x86 and 680x0, an optimized version will be provided in match.asm or1257* match.S. The code will be functionally equivalent.1258*/1259local uInt longest_match(s, cur_match)1260deflate_state *s;1261IPos cur_match; /* current match */1262{1263unsigned chain_length = s->max_chain_length;/* max hash chain length */1264register Bytef *scan = s->window + s->strstart; /* current string */1265register Bytef *match; /* matched string */1266register int len; /* length of current match */1267int best_len = (int)s->prev_length; /* best match length so far */1268int nice_match = s->nice_match; /* stop if match long enough */1269IPos limit = s->strstart > (IPos)MAX_DIST(s) ?1270s->strstart - (IPos)MAX_DIST(s) : NIL;1271/* Stop when cur_match becomes <= limit. To simplify the code,1272* we prevent matches with the string of window index 0.1273*/1274Posf *prev = s->prev;1275uInt wmask = s->w_mask;12761277#ifdef UNALIGNED_OK1278/* Compare two bytes at a time. Note: this is not always beneficial.1279* Try with and without -DUNALIGNED_OK to check.1280*/1281register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;1282register ush scan_start = *(ushf*)scan;1283register ush scan_end = *(ushf*)(scan+best_len-1);1284#else1285register Bytef *strend = s->window + s->strstart + MAX_MATCH;1286register Byte scan_end1 = scan[best_len-1];1287register Byte scan_end = scan[best_len];1288#endif12891290/* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.1291* It is easy to get rid of this optimization if necessary.1292*/1293Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");12941295/* Do not waste too much time if we already have a good match: */1296if (s->prev_length >= s->good_match) {1297chain_length >>= 2;1298}1299/* Do not look for matches beyond the end of the input. This is necessary1300* to make deflate deterministic.1301*/1302if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;13031304Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");13051306do {1307Assert(cur_match < s->strstart, "no future");1308match = s->window + cur_match;13091310/* Skip to next match if the match length cannot increase1311* or if the match length is less than 2. Note that the checks below1312* for insufficient lookahead only occur occasionally for performance1313* reasons. Therefore uninitialized memory will be accessed, and1314* conditional jumps will be made that depend on those values.1315* However the length of the match is limited to the lookahead, so1316* the output of deflate is not affected by the uninitialized values.1317*/1318#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)1319/* This code assumes sizeof(unsigned short) == 2. Do not use1320* UNALIGNED_OK if your compiler uses a different size.1321*/1322if (*(ushf*)(match+best_len-1) != scan_end ||1323*(ushf*)match != scan_start) continue;13241325/* It is not necessary to compare scan[2] and match[2] since they are1326* always equal when the other bytes match, given that the hash keys1327* are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at1328* strstart+3, +5, ... up to strstart+257. We check for insufficient1329* lookahead only every 4th comparison; the 128th check will be made1330* at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is1331* necessary to put more guard bytes at the end of the window, or1332* to check more often for insufficient lookahead.1333*/1334Assert(scan[2] == match[2], "scan[2]?");1335scan++, match++;1336do {1337} while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&1338*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&1339*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&1340*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&1341scan < strend);1342/* The funny "do {}" generates better code on most compilers */13431344/* Here, scan <= window+strstart+257 */1345Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");1346if (*scan == *match) scan++;13471348len = (MAX_MATCH - 1) - (int)(strend-scan);1349scan = strend - (MAX_MATCH-1);13501351#else /* UNALIGNED_OK */13521353if (match[best_len] != scan_end ||1354match[best_len-1] != scan_end1 ||1355*match != *scan ||1356*++match != scan[1]) continue;13571358/* The check at best_len-1 can be removed because it will be made1359* again later. (This heuristic is not always a win.)1360* It is not necessary to compare scan[2] and match[2] since they1361* are always equal when the other bytes match, given that1362* the hash keys are equal and that HASH_BITS >= 8.1363*/1364scan += 2, match++;1365Assert(*scan == *match, "match[2]?");13661367/* We check for insufficient lookahead only every 8th comparison;1368* the 256th check will be made at strstart+258.1369*/1370do {1371} while (*++scan == *++match && *++scan == *++match &&1372*++scan == *++match && *++scan == *++match &&1373*++scan == *++match && *++scan == *++match &&1374*++scan == *++match && *++scan == *++match &&1375scan < strend);13761377Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");13781379len = MAX_MATCH - (int)(strend - scan);1380scan = strend - MAX_MATCH;13811382#endif /* UNALIGNED_OK */13831384if (len > best_len) {1385s->match_start = cur_match;1386best_len = len;1387if (len >= nice_match) break;1388#ifdef UNALIGNED_OK1389scan_end = *(ushf*)(scan+best_len-1);1390#else1391scan_end1 = scan[best_len-1];1392scan_end = scan[best_len];1393#endif1394}1395} while ((cur_match = prev[cur_match & wmask]) > limit1396&& --chain_length != 0);13971398if ((uInt)best_len <= s->lookahead) return (uInt)best_len;1399return s->lookahead;1400}1401#endif /* ASMV */14021403#else /* FASTEST */14041405/* ---------------------------------------------------------------------------1406* Optimized version for FASTEST only1407*/1408local uInt longest_match(s, cur_match)1409deflate_state *s;1410IPos cur_match; /* current match */1411{1412register Bytef *scan = s->window + s->strstart; /* current string */1413register Bytef *match; /* matched string */1414register int len; /* length of current match */1415register Bytef *strend = s->window + s->strstart + MAX_MATCH;14161417/* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.1418* It is easy to get rid of this optimization if necessary.1419*/1420Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");14211422Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");14231424Assert(cur_match < s->strstart, "no future");14251426match = s->window + cur_match;14271428/* Return failure if the match length is less than 2:1429*/1430if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;14311432/* The check at best_len-1 can be removed because it will be made1433* again later. (This heuristic is not always a win.)1434* It is not necessary to compare scan[2] and match[2] since they1435* are always equal when the other bytes match, given that1436* the hash keys are equal and that HASH_BITS >= 8.1437*/1438scan += 2, match += 2;1439Assert(*scan == *match, "match[2]?");14401441/* We check for insufficient lookahead only every 8th comparison;1442* the 256th check will be made at strstart+258.1443*/1444do {1445} while (*++scan == *++match && *++scan == *++match &&1446*++scan == *++match && *++scan == *++match &&1447*++scan == *++match && *++scan == *++match &&1448*++scan == *++match && *++scan == *++match &&1449scan < strend);14501451Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");14521453len = MAX_MATCH - (int)(strend - scan);14541455if (len < MIN_MATCH) return MIN_MATCH - 1;14561457s->match_start = cur_match;1458return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;1459}14601461#endif /* FASTEST */14621463#ifdef ZLIB_DEBUG14641465#define EQUAL 01466/* result of memcmp for equal strings */14671468/* ===========================================================================1469* Check that the match at match_start is indeed a match.1470*/1471local void check_match(s, start, match, length)1472deflate_state *s;1473IPos start, match;1474int length;1475{1476/* check that the match is indeed a match */1477if (zmemcmp(s->window + match,1478s->window + start, length) != EQUAL) {1479fprintf(stderr, " start %u, match %u, length %d\n",1480start, match, length);1481do {1482fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);1483} while (--length != 0);1484z_error("invalid match");1485}1486if (z_verbose > 1) {1487fprintf(stderr,"\\[%d,%d]", start-match, length);1488do { putc(s->window[start++], stderr); } while (--length != 0);1489}1490}1491#else1492# define check_match(s, start, match, length)1493#endif /* ZLIB_DEBUG */14941495/* ===========================================================================1496* Fill the window when the lookahead becomes insufficient.1497* Updates strstart and lookahead.1498*1499* IN assertion: lookahead < MIN_LOOKAHEAD1500* OUT assertions: strstart <= window_size-MIN_LOOKAHEAD1501* At least one byte has been read, or avail_in == 0; reads are1502* performed for at least two bytes (required for the zip translate_eol1503* option -- not supported here).1504*/1505local void fill_window(s)1506deflate_state *s;1507{1508unsigned n;1509unsigned more; /* Amount of free space at the end of the window. */1510uInt wsize = s->w_size;15111512Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");15131514do {1515more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);15161517/* Deal with !@#$% 64K limit: */1518if (sizeof(int) <= 2) {1519if (more == 0 && s->strstart == 0 && s->lookahead == 0) {1520more = wsize;15211522} else if (more == (unsigned)(-1)) {1523/* Very unlikely, but possible on 16 bit machine if1524* strstart == 0 && lookahead == 1 (input done a byte at time)1525*/1526more--;1527}1528}15291530/* If the window is almost full and there is insufficient lookahead,1531* move the upper half to the lower one to make room in the upper half.1532*/1533if (s->strstart >= wsize+MAX_DIST(s)) {15341535zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more);1536s->match_start -= wsize;1537s->strstart -= wsize; /* we now have strstart >= MAX_DIST */1538s->block_start -= (long) wsize;1539slide_hash(s);1540more += wsize;1541}1542if (s->strm->avail_in == 0) break;15431544/* If there was no sliding:1545* strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&1546* more == window_size - lookahead - strstart1547* => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)1548* => more >= window_size - 2*WSIZE + 21549* In the BIG_MEM or MMAP case (not yet supported),1550* window_size == input_size + MIN_LOOKAHEAD &&1551* strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.1552* Otherwise, window_size == 2*WSIZE so more >= 2.1553* If there was sliding, more >= WSIZE. So in all cases, more >= 2.1554*/1555Assert(more >= 2, "more < 2");15561557n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);1558s->lookahead += n;15591560/* Initialize the hash value now that we have some input: */1561if (s->lookahead + s->insert >= MIN_MATCH) {1562uInt str = s->strstart - s->insert;1563s->ins_h = s->window[str];1564UPDATE_HASH(s, s->ins_h, s->window[str + 1]);1565#if MIN_MATCH != 31566Call UPDATE_HASH() MIN_MATCH-3 more times1567#endif1568while (s->insert) {1569UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);1570#ifndef FASTEST1571s->prev[str & s->w_mask] = s->head[s->ins_h];1572#endif1573s->head[s->ins_h] = (Pos)str;1574str++;1575s->insert--;1576if (s->lookahead + s->insert < MIN_MATCH)1577break;1578}1579}1580/* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,1581* but this is not important since only literal bytes will be emitted.1582*/15831584} while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);15851586/* If the WIN_INIT bytes after the end of the current data have never been1587* written, then zero those bytes in order to avoid memory check reports of1588* the use of uninitialized (or uninitialised as Julian writes) bytes by1589* the longest match routines. Update the high water mark for the next1590* time through here. WIN_INIT is set to MAX_MATCH since the longest match1591* routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.1592*/1593if (s->high_water < s->window_size) {1594ulg curr = s->strstart + (ulg)(s->lookahead);1595ulg init;15961597if (s->high_water < curr) {1598/* Previous high water mark below current data -- zero WIN_INIT1599* bytes or up to end of window, whichever is less.1600*/1601init = s->window_size - curr;1602if (init > WIN_INIT)1603init = WIN_INIT;1604zmemzero(s->window + curr, (unsigned)init);1605s->high_water = curr + init;1606}1607else if (s->high_water < (ulg)curr + WIN_INIT) {1608/* High water mark at or above current data, but below current data1609* plus WIN_INIT -- zero out to current data plus WIN_INIT, or up1610* to end of window, whichever is less.1611*/1612init = (ulg)curr + WIN_INIT - s->high_water;1613if (init > s->window_size - s->high_water)1614init = s->window_size - s->high_water;1615zmemzero(s->window + s->high_water, (unsigned)init);1616s->high_water += init;1617}1618}16191620Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,1621"not enough room for search");1622}16231624/* ===========================================================================1625* Flush the current block, with given end-of-file flag.1626* IN assertion: strstart is set to the end of the current match.1627*/1628#define FLUSH_BLOCK_ONLY(s, last) { \1629_tr_flush_block(s, (s->block_start >= 0L ? \1630(charf *)&s->window[(unsigned)s->block_start] : \1631(charf *)Z_NULL), \1632(ulg)((long)s->strstart - s->block_start), \1633(last)); \1634s->block_start = s->strstart; \1635flush_pending(s->strm); \1636Tracev((stderr,"[FLUSH]")); \1637}16381639/* Same but force premature exit if necessary. */1640#define FLUSH_BLOCK(s, last) { \1641FLUSH_BLOCK_ONLY(s, last); \1642if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \1643}16441645/* Maximum stored block length in deflate format (not including header). */1646#define MAX_STORED 6553516471648/* Minimum of a and b. */1649#define MIN(a, b) ((a) > (b) ? (b) : (a))16501651/* ===========================================================================1652* Copy without compression as much as possible from the input stream, return1653* the current block state.1654*1655* In case deflateParams() is used to later switch to a non-zero compression1656* level, s->matches (otherwise unused when storing) keeps track of the number1657* of hash table slides to perform. If s->matches is 1, then one hash table1658* slide will be done when switching. If s->matches is 2, the maximum value1659* allowed here, then the hash table will be cleared, since two or more slides1660* is the same as a clear.1661*1662* deflate_stored() is written to minimize the number of times an input byte is1663* copied. It is most efficient with large input and output buffers, which1664* maximizes the opportunites to have a single copy from next_in to next_out.1665*/1666local block_state deflate_stored(s, flush)1667deflate_state *s;1668int flush;1669{1670/* Smallest worthy block size when not flushing or finishing. By default1671* this is 32K. This can be as small as 507 bytes for memLevel == 1. For1672* large input and output buffers, the stored block size will be larger.1673*/1674unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);16751676/* Copy as many min_block or larger stored blocks directly to next_out as1677* possible. If flushing, copy the remaining available input to next_out as1678* stored blocks, if there is enough space.1679*/1680unsigned len, left, have, last = 0;1681unsigned used = s->strm->avail_in;1682do {1683/* Set len to the maximum size block that we can copy directly with the1684* available input data and output space. Set left to how much of that1685* would be copied from what's left in the window.1686*/1687len = MAX_STORED; /* maximum deflate stored block length */1688have = (s->bi_valid + 42) >> 3; /* number of header bytes */1689if (s->strm->avail_out < have) /* need room for header */1690break;1691/* maximum stored block length that will fit in avail_out: */1692have = s->strm->avail_out - have;1693left = s->strstart - s->block_start; /* bytes left in window */1694if (len > (ulg)left + s->strm->avail_in)1695len = left + s->strm->avail_in; /* limit len to the input */1696if (len > have)1697len = have; /* limit len to the output */16981699/* If the stored block would be less than min_block in length, or if1700* unable to copy all of the available input when flushing, then try1701* copying to the window and the pending buffer instead. Also don't1702* write an empty block when flushing -- deflate() does that.1703*/1704if (len < min_block && ((len == 0 && flush != Z_FINISH) ||1705flush == Z_NO_FLUSH ||1706len != left + s->strm->avail_in))1707break;17081709/* Make a dummy stored block in pending to get the header bytes,1710* including any pending bits. This also updates the debugging counts.1711*/1712last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;1713_tr_stored_block(s, (char *)0, 0L, last);17141715/* Replace the lengths in the dummy stored block with len. */1716s->pending_buf[s->pending - 4] = len;1717s->pending_buf[s->pending - 3] = len >> 8;1718s->pending_buf[s->pending - 2] = ~len;1719s->pending_buf[s->pending - 1] = ~len >> 8;17201721/* Write the stored block header bytes. */1722flush_pending(s->strm);17231724#ifdef ZLIB_DEBUG1725/* Update debugging counts for the data about to be copied. */1726s->compressed_len += len << 3;1727s->bits_sent += len << 3;1728#endif17291730/* Copy uncompressed bytes from the window to next_out. */1731if (left) {1732if (left > len)1733left = len;1734zmemcpy(s->strm->next_out, s->window + s->block_start, left);1735s->strm->next_out += left;1736s->strm->avail_out -= left;1737s->strm->total_out += left;1738s->block_start += left;1739len -= left;1740}17411742/* Copy uncompressed bytes directly from next_in to next_out, updating1743* the check value.1744*/1745if (len) {1746read_buf(s->strm, s->strm->next_out, len);1747s->strm->next_out += len;1748s->strm->avail_out -= len;1749s->strm->total_out += len;1750}1751} while (last == 0);17521753/* Update the sliding window with the last s->w_size bytes of the copied1754* data, or append all of the copied data to the existing window if less1755* than s->w_size bytes were copied. Also update the number of bytes to1756* insert in the hash tables, in the event that deflateParams() switches to1757* a non-zero compression level.1758*/1759used -= s->strm->avail_in; /* number of input bytes directly copied */1760if (used) {1761/* If any input was used, then no unused input remains in the window,1762* therefore s->block_start == s->strstart.1763*/1764if (used >= s->w_size) { /* supplant the previous history */1765s->matches = 2; /* clear hash */1766zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);1767s->strstart = s->w_size;1768}1769else {1770if (s->window_size - s->strstart <= used) {1771/* Slide the window down. */1772s->strstart -= s->w_size;1773zmemcpy(s->window, s->window + s->w_size, s->strstart);1774if (s->matches < 2)1775s->matches++; /* add a pending slide_hash() */1776}1777zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);1778s->strstart += used;1779}1780s->block_start = s->strstart;1781s->insert += MIN(used, s->w_size - s->insert);1782}1783if (s->high_water < s->strstart)1784s->high_water = s->strstart;17851786/* If the last block was written to next_out, then done. */1787if (last)1788return finish_done;17891790/* If flushing and all input has been consumed, then done. */1791if (flush != Z_NO_FLUSH && flush != Z_FINISH &&1792s->strm->avail_in == 0 && (long)s->strstart == s->block_start)1793return block_done;17941795/* Fill the window with any remaining input. */1796have = s->window_size - s->strstart - 1;1797if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {1798/* Slide the window down. */1799s->block_start -= s->w_size;1800s->strstart -= s->w_size;1801zmemcpy(s->window, s->window + s->w_size, s->strstart);1802if (s->matches < 2)1803s->matches++; /* add a pending slide_hash() */1804have += s->w_size; /* more space now */1805}1806if (have > s->strm->avail_in)1807have = s->strm->avail_in;1808if (have) {1809read_buf(s->strm, s->window + s->strstart, have);1810s->strstart += have;1811}1812if (s->high_water < s->strstart)1813s->high_water = s->strstart;18141815/* There was not enough avail_out to write a complete worthy or flushed1816* stored block to next_out. Write a stored block to pending instead, if we1817* have enough input for a worthy block, or if flushing and there is enough1818* room for the remaining input as a stored block in the pending buffer.1819*/1820have = (s->bi_valid + 42) >> 3; /* number of header bytes */1821/* maximum stored block length that will fit in pending: */1822have = MIN(s->pending_buf_size - have, MAX_STORED);1823min_block = MIN(have, s->w_size);1824left = s->strstart - s->block_start;1825if (left >= min_block ||1826((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&1827s->strm->avail_in == 0 && left <= have)) {1828len = MIN(left, have);1829last = flush == Z_FINISH && s->strm->avail_in == 0 &&1830len == left ? 1 : 0;1831_tr_stored_block(s, (charf *)s->window + s->block_start, len, last);1832s->block_start += len;1833flush_pending(s->strm);1834}18351836/* We've done all we can with the available input and output. */1837return last ? finish_started : need_more;1838}18391840/* ===========================================================================1841* Compress as much as possible from the input stream, return the current1842* block state.1843* This function does not perform lazy evaluation of matches and inserts1844* new strings in the dictionary only for unmatched strings or for short1845* matches. It is used only for the fast compression options.1846*/1847local block_state deflate_fast(s, flush)1848deflate_state *s;1849int flush;1850{1851IPos hash_head; /* head of the hash chain */1852int bflush; /* set if current block must be flushed */18531854for (;;) {1855/* Make sure that we always have enough lookahead, except1856* at the end of the input file. We need MAX_MATCH bytes1857* for the next match, plus MIN_MATCH bytes to insert the1858* string following the next match.1859*/1860if (s->lookahead < MIN_LOOKAHEAD) {1861fill_window(s);1862if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {1863return need_more;1864}1865if (s->lookahead == 0) break; /* flush the current block */1866}18671868/* Insert the string window[strstart .. strstart+2] in the1869* dictionary, and set hash_head to the head of the hash chain:1870*/1871hash_head = NIL;1872if (s->lookahead >= MIN_MATCH) {1873INSERT_STRING(s, s->strstart, hash_head);1874}18751876/* Find the longest match, discarding those <= prev_length.1877* At this point we have always match_length < MIN_MATCH1878*/1879if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {1880/* To simplify the code, we prevent matches with the string1881* of window index 0 (in particular we have to avoid a match1882* of the string with itself at the start of the input file).1883*/1884s->match_length = longest_match (s, hash_head);1885/* longest_match() sets match_start */1886}1887if (s->match_length >= MIN_MATCH) {1888check_match(s, s->strstart, s->match_start, s->match_length);18891890_tr_tally_dist(s, s->strstart - s->match_start,1891s->match_length - MIN_MATCH, bflush);18921893s->lookahead -= s->match_length;18941895/* Insert new strings in the hash table only if the match length1896* is not too large. This saves time but degrades compression.1897*/1898#ifndef FASTEST1899if (s->match_length <= s->max_insert_length &&1900s->lookahead >= MIN_MATCH) {1901s->match_length--; /* string at strstart already in table */1902do {1903s->strstart++;1904INSERT_STRING(s, s->strstart, hash_head);1905/* strstart never exceeds WSIZE-MAX_MATCH, so there are1906* always MIN_MATCH bytes ahead.1907*/1908} while (--s->match_length != 0);1909s->strstart++;1910} else1911#endif1912{1913s->strstart += s->match_length;1914s->match_length = 0;1915s->ins_h = s->window[s->strstart];1916UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);1917#if MIN_MATCH != 31918Call UPDATE_HASH() MIN_MATCH-3 more times1919#endif1920/* If lookahead < MIN_MATCH, ins_h is garbage, but it does not1921* matter since it will be recomputed at next deflate call.1922*/1923}1924} else {1925/* No match, output a literal byte */1926Tracevv((stderr,"%c", s->window[s->strstart]));1927_tr_tally_lit (s, s->window[s->strstart], bflush);1928s->lookahead--;1929s->strstart++;1930}1931if (bflush) FLUSH_BLOCK(s, 0);1932}1933s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;1934if (flush == Z_FINISH) {1935FLUSH_BLOCK(s, 1);1936return finish_done;1937}1938if (s->last_lit)1939FLUSH_BLOCK(s, 0);1940return block_done;1941}19421943#ifndef FASTEST1944/* ===========================================================================1945* Same as above, but achieves better compression. We use a lazy1946* evaluation for matches: a match is finally adopted only if there is1947* no better match at the next window position.1948*/1949local block_state deflate_slow(s, flush)1950deflate_state *s;1951int flush;1952{1953IPos hash_head; /* head of hash chain */1954int bflush; /* set if current block must be flushed */19551956/* Process the input block. */1957for (;;) {1958/* Make sure that we always have enough lookahead, except1959* at the end of the input file. We need MAX_MATCH bytes1960* for the next match, plus MIN_MATCH bytes to insert the1961* string following the next match.1962*/1963if (s->lookahead < MIN_LOOKAHEAD) {1964fill_window(s);1965if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {1966return need_more;1967}1968if (s->lookahead == 0) break; /* flush the current block */1969}19701971/* Insert the string window[strstart .. strstart+2] in the1972* dictionary, and set hash_head to the head of the hash chain:1973*/1974hash_head = NIL;1975if (s->lookahead >= MIN_MATCH) {1976INSERT_STRING(s, s->strstart, hash_head);1977}19781979/* Find the longest match, discarding those <= prev_length.1980*/1981s->prev_length = s->match_length, s->prev_match = s->match_start;1982s->match_length = MIN_MATCH-1;19831984if (hash_head != NIL && s->prev_length < s->max_lazy_match &&1985s->strstart - hash_head <= MAX_DIST(s)) {1986/* To simplify the code, we prevent matches with the string1987* of window index 0 (in particular we have to avoid a match1988* of the string with itself at the start of the input file).1989*/1990s->match_length = longest_match (s, hash_head);1991/* longest_match() sets match_start */19921993if (s->match_length <= 5 && (s->strategy == Z_FILTERED1994#if TOO_FAR <= 327671995|| (s->match_length == MIN_MATCH &&1996s->strstart - s->match_start > TOO_FAR)1997#endif1998)) {19992000/* If prev_match is also MIN_MATCH, match_start is garbage2001* but we will ignore the current match anyway.2002*/2003s->match_length = MIN_MATCH-1;2004}2005}2006/* If there was a match at the previous step and the current2007* match is not better, output the previous match:2008*/2009if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {2010uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;2011/* Do not insert strings in hash table beyond this. */20122013check_match(s, s->strstart-1, s->prev_match, s->prev_length);20142015_tr_tally_dist(s, s->strstart -1 - s->prev_match,2016s->prev_length - MIN_MATCH, bflush);20172018/* Insert in hash table all strings up to the end of the match.2019* strstart-1 and strstart are already inserted. If there is not2020* enough lookahead, the last two strings are not inserted in2021* the hash table.2022*/2023s->lookahead -= s->prev_length-1;2024s->prev_length -= 2;2025do {2026if (++s->strstart <= max_insert) {2027INSERT_STRING(s, s->strstart, hash_head);2028}2029} while (--s->prev_length != 0);2030s->match_available = 0;2031s->match_length = MIN_MATCH-1;2032s->strstart++;20332034if (bflush) FLUSH_BLOCK(s, 0);20352036} else if (s->match_available) {2037/* If there was no match at the previous position, output a2038* single literal. If there was a match but the current match2039* is longer, truncate the previous match to a single literal.2040*/2041Tracevv((stderr,"%c", s->window[s->strstart-1]));2042_tr_tally_lit(s, s->window[s->strstart-1], bflush);2043if (bflush) {2044FLUSH_BLOCK_ONLY(s, 0);2045}2046s->strstart++;2047s->lookahead--;2048if (s->strm->avail_out == 0) return need_more;2049} else {2050/* There is no previous match to compare with, wait for2051* the next step to decide.2052*/2053s->match_available = 1;2054s->strstart++;2055s->lookahead--;2056}2057}2058Assert (flush != Z_NO_FLUSH, "no flush?");2059if (s->match_available) {2060Tracevv((stderr,"%c", s->window[s->strstart-1]));2061_tr_tally_lit(s, s->window[s->strstart-1], bflush);2062s->match_available = 0;2063}2064s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;2065if (flush == Z_FINISH) {2066FLUSH_BLOCK(s, 1);2067return finish_done;2068}2069if (s->last_lit)2070FLUSH_BLOCK(s, 0);2071return block_done;2072}2073#endif /* FASTEST */20742075/* ===========================================================================2076* For Z_RLE, simply look for runs of bytes, generate matches only of distance2077* one. Do not maintain a hash table. (It will be regenerated if this run of2078* deflate switches away from Z_RLE.)2079*/2080local block_state deflate_rle(s, flush)2081deflate_state *s;2082int flush;2083{2084int bflush; /* set if current block must be flushed */2085uInt prev; /* byte at distance one to match */2086Bytef *scan, *strend; /* scan goes up to strend for length of run */20872088for (;;) {2089/* Make sure that we always have enough lookahead, except2090* at the end of the input file. We need MAX_MATCH bytes2091* for the longest run, plus one for the unrolled loop.2092*/2093if (s->lookahead <= MAX_MATCH) {2094fill_window(s);2095if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {2096return need_more;2097}2098if (s->lookahead == 0) break; /* flush the current block */2099}21002101/* See how many times the previous byte repeats */2102s->match_length = 0;2103if (s->lookahead >= MIN_MATCH && s->strstart > 0) {2104scan = s->window + s->strstart - 1;2105prev = *scan;2106if (prev == *++scan && prev == *++scan && prev == *++scan) {2107strend = s->window + s->strstart + MAX_MATCH;2108do {2109} while (prev == *++scan && prev == *++scan &&2110prev == *++scan && prev == *++scan &&2111prev == *++scan && prev == *++scan &&2112prev == *++scan && prev == *++scan &&2113scan < strend);2114s->match_length = MAX_MATCH - (uInt)(strend - scan);2115if (s->match_length > s->lookahead)2116s->match_length = s->lookahead;2117}2118Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");2119}21202121/* Emit match if have run of MIN_MATCH or longer, else emit literal */2122if (s->match_length >= MIN_MATCH) {2123check_match(s, s->strstart, s->strstart - 1, s->match_length);21242125_tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);21262127s->lookahead -= s->match_length;2128s->strstart += s->match_length;2129s->match_length = 0;2130} else {2131/* No match, output a literal byte */2132Tracevv((stderr,"%c", s->window[s->strstart]));2133_tr_tally_lit (s, s->window[s->strstart], bflush);2134s->lookahead--;2135s->strstart++;2136}2137if (bflush) FLUSH_BLOCK(s, 0);2138}2139s->insert = 0;2140if (flush == Z_FINISH) {2141FLUSH_BLOCK(s, 1);2142return finish_done;2143}2144if (s->last_lit)2145FLUSH_BLOCK(s, 0);2146return block_done;2147}21482149/* ===========================================================================2150* For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.2151* (It will be regenerated if this run of deflate switches away from Huffman.)2152*/2153local block_state deflate_huff(s, flush)2154deflate_state *s;2155int flush;2156{2157int bflush; /* set if current block must be flushed */21582159for (;;) {2160/* Make sure that we have a literal to write. */2161if (s->lookahead == 0) {2162fill_window(s);2163if (s->lookahead == 0) {2164if (flush == Z_NO_FLUSH)2165return need_more;2166break; /* flush the current block */2167}2168}21692170/* Output a literal byte */2171s->match_length = 0;2172Tracevv((stderr,"%c", s->window[s->strstart]));2173_tr_tally_lit (s, s->window[s->strstart], bflush);2174s->lookahead--;2175s->strstart++;2176if (bflush) FLUSH_BLOCK(s, 0);2177}2178s->insert = 0;2179if (flush == Z_FINISH) {2180FLUSH_BLOCK(s, 1);2181return finish_done;2182}2183if (s->last_lit)2184FLUSH_BLOCK(s, 0);2185return block_done;2186}218721882189