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