18a5f34efSLei Wen /* deflate.c -- compress data using the deflation algorithm 28a5f34efSLei Wen * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler 38a5f34efSLei Wen * For conditions of distribution and use, see copyright notice in zlib.h 48a5f34efSLei Wen */ 58a5f34efSLei Wen 68a5f34efSLei Wen /* 78a5f34efSLei Wen * ALGORITHM 88a5f34efSLei Wen * 98a5f34efSLei Wen * The "deflation" process depends on being able to identify portions 108a5f34efSLei Wen * of the input text which are identical to earlier input (within a 118a5f34efSLei Wen * sliding window trailing behind the input currently being processed). 128a5f34efSLei Wen * 138a5f34efSLei Wen * The most straightforward technique turns out to be the fastest for 148a5f34efSLei Wen * most input files: try all possible matches and select the longest. 158a5f34efSLei Wen * The key feature of this algorithm is that insertions into the string 168a5f34efSLei Wen * dictionary are very simple and thus fast, and deletions are avoided 178a5f34efSLei Wen * completely. Insertions are performed at each input character, whereas 188a5f34efSLei Wen * string matches are performed only when the previous match ends. So it 198a5f34efSLei Wen * is preferable to spend more time in matches to allow very fast string 208a5f34efSLei Wen * insertions and avoid deletions. The matching algorithm for small 218a5f34efSLei Wen * strings is inspired from that of Rabin & Karp. A brute force approach 228a5f34efSLei Wen * is used to find longer strings when a small match has been found. 238a5f34efSLei Wen * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze 248a5f34efSLei Wen * (by Leonid Broukhis). 258a5f34efSLei Wen * A previous version of this file used a more sophisticated algorithm 268a5f34efSLei Wen * (by Fiala and Greene) which is guaranteed to run in linear amortized 278a5f34efSLei Wen * time, but has a larger average cost, uses more memory and is patented. 288a5f34efSLei Wen * However the F&G algorithm may be faster for some highly redundant 298a5f34efSLei Wen * files if the parameter max_chain_length (described below) is too large. 308a5f34efSLei Wen * 318a5f34efSLei Wen * ACKNOWLEDGEMENTS 328a5f34efSLei Wen * 338a5f34efSLei Wen * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and 348a5f34efSLei Wen * I found it in 'freeze' written by Leonid Broukhis. 358a5f34efSLei Wen * Thanks to many people for bug reports and testing. 368a5f34efSLei Wen * 378a5f34efSLei Wen * REFERENCES 388a5f34efSLei Wen * 398a5f34efSLei Wen * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". 408a5f34efSLei Wen * Available in http://www.ietf.org/rfc/rfc1951.txt 418a5f34efSLei Wen * 428a5f34efSLei Wen * A description of the Rabin and Karp algorithm is given in the book 438a5f34efSLei Wen * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. 448a5f34efSLei Wen * 458a5f34efSLei Wen * Fiala,E.R., and Greene,D.H. 468a5f34efSLei Wen * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 478a5f34efSLei Wen * 488a5f34efSLei Wen */ 498a5f34efSLei Wen 508a5f34efSLei Wen /* @(#) $Id$ */ 518a5f34efSLei Wen 528a5f34efSLei Wen #include "deflate.h" 538a5f34efSLei Wen 548a5f34efSLei Wen const char deflate_copyright[] = 558a5f34efSLei Wen " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler "; 568a5f34efSLei Wen /* 578a5f34efSLei Wen If you use the zlib library in a product, an acknowledgment is welcome 588a5f34efSLei Wen in the documentation of your product. If for some reason you cannot 598a5f34efSLei Wen include such an acknowledgment, I would appreciate that you keep this 608a5f34efSLei Wen copyright string in the executable of your product. 618a5f34efSLei Wen */ 628a5f34efSLei Wen 638a5f34efSLei Wen /* =========================================================================== 648a5f34efSLei Wen * Function prototypes. 658a5f34efSLei Wen */ 668a5f34efSLei Wen typedef enum { 678a5f34efSLei Wen need_more, /* block not completed, need more input or more output */ 688a5f34efSLei Wen block_done, /* block flush performed */ 698a5f34efSLei Wen finish_started, /* finish started, need only more output at next deflate */ 708a5f34efSLei Wen finish_done /* finish done, accept no more input or output */ 718a5f34efSLei Wen } block_state; 728a5f34efSLei Wen 738a5f34efSLei Wen typedef block_state (*compress_func) OF((deflate_state *s, int flush)); 748a5f34efSLei Wen /* Compression function. Returns the block state after the call. */ 758a5f34efSLei Wen 768a5f34efSLei Wen local void fill_window OF((deflate_state *s)); 778a5f34efSLei Wen local block_state deflate_stored OF((deflate_state *s, int flush)); 788a5f34efSLei Wen local block_state deflate_fast OF((deflate_state *s, int flush)); 798a5f34efSLei Wen #ifndef FASTEST 808a5f34efSLei Wen local block_state deflate_slow OF((deflate_state *s, int flush)); 818a5f34efSLei Wen #endif 828a5f34efSLei Wen local block_state deflate_rle OF((deflate_state *s, int flush)); 838a5f34efSLei Wen local block_state deflate_huff OF((deflate_state *s, int flush)); 848a5f34efSLei Wen local void lm_init OF((deflate_state *s)); 858a5f34efSLei Wen local void putShortMSB OF((deflate_state *s, uInt b)); 868a5f34efSLei Wen local void flush_pending OF((z_streamp strm)); 878a5f34efSLei Wen local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); 888a5f34efSLei Wen #ifdef ASMV 898a5f34efSLei Wen void match_init OF((void)); /* asm code initialization */ 908a5f34efSLei Wen uInt longest_match OF((deflate_state *s, IPos cur_match)); 918a5f34efSLei Wen #else 928a5f34efSLei Wen local uInt longest_match OF((deflate_state *s, IPos cur_match)); 938a5f34efSLei Wen #endif 948a5f34efSLei Wen 958a5f34efSLei Wen #ifdef DEBUG 968a5f34efSLei Wen local void check_match OF((deflate_state *s, IPos start, IPos match, 978a5f34efSLei Wen int length)); 988a5f34efSLei Wen #endif 998a5f34efSLei Wen 1008a5f34efSLei Wen /* =========================================================================== 1018a5f34efSLei Wen * Local data 1028a5f34efSLei Wen */ 1038a5f34efSLei Wen 1048a5f34efSLei Wen #define NIL 0 1058a5f34efSLei Wen /* Tail of hash chains */ 1068a5f34efSLei Wen 1078a5f34efSLei Wen #ifndef TOO_FAR 1088a5f34efSLei Wen # define TOO_FAR 4096 1098a5f34efSLei Wen #endif 1108a5f34efSLei Wen /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ 1118a5f34efSLei Wen 1128a5f34efSLei Wen /* Values for max_lazy_match, good_match and max_chain_length, depending on 1138a5f34efSLei Wen * the desired pack level (0..9). The values given below have been tuned to 1148a5f34efSLei Wen * exclude worst case performance for pathological files. Better values may be 1158a5f34efSLei Wen * found for specific files. 1168a5f34efSLei Wen */ 1178a5f34efSLei Wen typedef struct config_s { 1188a5f34efSLei Wen ush good_length; /* reduce lazy search above this match length */ 1198a5f34efSLei Wen ush max_lazy; /* do not perform lazy search above this match length */ 1208a5f34efSLei Wen ush nice_length; /* quit search above this match length */ 1218a5f34efSLei Wen ush max_chain; 1228a5f34efSLei Wen compress_func func; 1238a5f34efSLei Wen } config; 1248a5f34efSLei Wen 1258a5f34efSLei Wen #ifdef FASTEST 1268a5f34efSLei Wen local const config configuration_table[2] = { 1278a5f34efSLei Wen /* good lazy nice chain */ 1288a5f34efSLei Wen /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 1298a5f34efSLei Wen /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ 1308a5f34efSLei Wen #else 1318a5f34efSLei Wen local const config configuration_table[10] = { 1328a5f34efSLei Wen /* good lazy nice chain */ 1338a5f34efSLei Wen /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 1348a5f34efSLei Wen /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ 1358a5f34efSLei Wen /* 2 */ {4, 5, 16, 8, deflate_fast}, 1368a5f34efSLei Wen /* 3 */ {4, 6, 32, 32, deflate_fast}, 1378a5f34efSLei Wen 1388a5f34efSLei Wen /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ 1398a5f34efSLei Wen /* 5 */ {8, 16, 32, 32, deflate_slow}, 1408a5f34efSLei Wen /* 6 */ {8, 16, 128, 128, deflate_slow}, 1418a5f34efSLei Wen /* 7 */ {8, 32, 128, 256, deflate_slow}, 1428a5f34efSLei Wen /* 8 */ {32, 128, 258, 1024, deflate_slow}, 1438a5f34efSLei Wen /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ 1448a5f34efSLei Wen #endif 1458a5f34efSLei Wen 1468a5f34efSLei Wen /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 1478a5f34efSLei Wen * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 1488a5f34efSLei Wen * meaning. 1498a5f34efSLei Wen */ 1508a5f34efSLei Wen 1518a5f34efSLei Wen #define EQUAL 0 1528a5f34efSLei Wen /* result of memcmp for equal strings */ 1538a5f34efSLei Wen 1548a5f34efSLei Wen #ifndef NO_DUMMY_DECL 1558a5f34efSLei Wen struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ 1568a5f34efSLei Wen #endif 1578a5f34efSLei Wen 1588a5f34efSLei Wen /* =========================================================================== 1598a5f34efSLei Wen * Update a hash value with the given input byte 1608a5f34efSLei Wen * IN assertion: all calls to to UPDATE_HASH are made with consecutive 1618a5f34efSLei Wen * input characters, so that a running hash key can be computed from the 1628a5f34efSLei Wen * previous key instead of complete recalculation each time. 1638a5f34efSLei Wen */ 1648a5f34efSLei Wen #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) 1658a5f34efSLei Wen 1668a5f34efSLei Wen 1678a5f34efSLei Wen /* =========================================================================== 1688a5f34efSLei Wen * Insert string str in the dictionary and set match_head to the previous head 1698a5f34efSLei Wen * of the hash chain (the most recent string with same hash key). Return 1708a5f34efSLei Wen * the previous length of the hash chain. 1718a5f34efSLei Wen * If this file is compiled with -DFASTEST, the compression level is forced 1728a5f34efSLei Wen * to 1, and no hash chains are maintained. 1738a5f34efSLei Wen * IN assertion: all calls to to INSERT_STRING are made with consecutive 1748a5f34efSLei Wen * input characters and the first MIN_MATCH bytes of str are valid 1758a5f34efSLei Wen * (except for the last MIN_MATCH-1 bytes of the input file). 1768a5f34efSLei Wen */ 1778a5f34efSLei Wen #ifdef FASTEST 1788a5f34efSLei Wen #define INSERT_STRING(s, str, match_head) \ 1798a5f34efSLei Wen (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 1808a5f34efSLei Wen match_head = s->head[s->ins_h], \ 1818a5f34efSLei Wen s->head[s->ins_h] = (Pos)(str)) 1828a5f34efSLei Wen #else 1838a5f34efSLei Wen #define INSERT_STRING(s, str, match_head) \ 1848a5f34efSLei Wen (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 1858a5f34efSLei Wen match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ 1868a5f34efSLei Wen s->head[s->ins_h] = (Pos)(str)) 1878a5f34efSLei Wen #endif 1888a5f34efSLei Wen 1898a5f34efSLei Wen /* =========================================================================== 1908a5f34efSLei Wen * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 1918a5f34efSLei Wen * prev[] will be initialized on the fly. 1928a5f34efSLei Wen */ 1938a5f34efSLei Wen #define CLEAR_HASH(s) \ 1948a5f34efSLei Wen s->head[s->hash_size-1] = NIL; \ 1958a5f34efSLei Wen zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 1968a5f34efSLei Wen 1978a5f34efSLei Wen /* ========================================================================= */ 1988a5f34efSLei Wen int ZEXPORT deflateInit_(strm, level, version, stream_size) 1998a5f34efSLei Wen z_streamp strm; 2008a5f34efSLei Wen int level; 2018a5f34efSLei Wen const char *version; 2028a5f34efSLei Wen int stream_size; 2038a5f34efSLei Wen { 2048a5f34efSLei Wen return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 2058a5f34efSLei Wen Z_DEFAULT_STRATEGY, version, stream_size); 2068a5f34efSLei Wen /* To do: ignore strm->next_in if we use it as window */ 2078a5f34efSLei Wen } 2088a5f34efSLei Wen 2098a5f34efSLei Wen /* ========================================================================= */ 2108a5f34efSLei Wen int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, 2118a5f34efSLei Wen version, stream_size) 2128a5f34efSLei Wen z_streamp strm; 2138a5f34efSLei Wen int level; 2148a5f34efSLei Wen int method; 2158a5f34efSLei Wen int windowBits; 2168a5f34efSLei Wen int memLevel; 2178a5f34efSLei Wen int strategy; 2188a5f34efSLei Wen const char *version; 2198a5f34efSLei Wen int stream_size; 2208a5f34efSLei Wen { 2218a5f34efSLei Wen deflate_state *s; 2228a5f34efSLei Wen int wrap = 1; 2238a5f34efSLei Wen static const char my_version[] = ZLIB_VERSION; 2248a5f34efSLei Wen 2258a5f34efSLei Wen ushf *overlay; 2268a5f34efSLei Wen /* We overlay pending_buf and d_buf+l_buf. This works since the average 2278a5f34efSLei Wen * output size for (length,distance) codes is <= 24 bits. 2288a5f34efSLei Wen */ 2298a5f34efSLei Wen 2308a5f34efSLei Wen if (version == Z_NULL || version[0] != my_version[0] || 2318a5f34efSLei Wen stream_size != sizeof(z_stream)) { 2328a5f34efSLei Wen return Z_VERSION_ERROR; 2338a5f34efSLei Wen } 2348a5f34efSLei Wen if (strm == Z_NULL) return Z_STREAM_ERROR; 2358a5f34efSLei Wen 2368a5f34efSLei Wen strm->msg = Z_NULL; 2378a5f34efSLei Wen if (strm->zalloc == (alloc_func)0) { 2388a5f34efSLei Wen strm->zalloc = zcalloc; 2398a5f34efSLei Wen strm->opaque = (voidpf)0; 2408a5f34efSLei Wen } 2418a5f34efSLei Wen if (strm->zfree == (free_func)0) strm->zfree = zcfree; 2428a5f34efSLei Wen 2438a5f34efSLei Wen #ifdef FASTEST 2448a5f34efSLei Wen if (level != 0) level = 1; 2458a5f34efSLei Wen #else 2468a5f34efSLei Wen if (level == Z_DEFAULT_COMPRESSION) level = 6; 2478a5f34efSLei Wen #endif 2488a5f34efSLei Wen 2498a5f34efSLei Wen if (windowBits < 0) { /* suppress zlib wrapper */ 2508a5f34efSLei Wen wrap = 0; 2518a5f34efSLei Wen windowBits = -windowBits; 2528a5f34efSLei Wen } 2538a5f34efSLei Wen #ifdef GZIP 2548a5f34efSLei Wen else if (windowBits > 15) { 2558a5f34efSLei Wen wrap = 2; /* write gzip wrapper instead */ 2568a5f34efSLei Wen windowBits -= 16; 2578a5f34efSLei Wen } 2588a5f34efSLei Wen #endif 2598a5f34efSLei Wen if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || 2608a5f34efSLei Wen windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || 2618a5f34efSLei Wen strategy < 0 || strategy > Z_FIXED) { 2628a5f34efSLei Wen return Z_STREAM_ERROR; 2638a5f34efSLei Wen } 2648a5f34efSLei Wen if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ 2658a5f34efSLei Wen s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); 2668a5f34efSLei Wen if (s == Z_NULL) return Z_MEM_ERROR; 2678a5f34efSLei Wen strm->state = (struct internal_state FAR *)s; 2688a5f34efSLei Wen s->strm = strm; 2698a5f34efSLei Wen 2708a5f34efSLei Wen s->wrap = wrap; 2718a5f34efSLei Wen s->gzhead = Z_NULL; 2728a5f34efSLei Wen s->w_bits = windowBits; 2738a5f34efSLei Wen s->w_size = 1 << s->w_bits; 2748a5f34efSLei Wen s->w_mask = s->w_size - 1; 2758a5f34efSLei Wen 2768a5f34efSLei Wen s->hash_bits = memLevel + 7; 2778a5f34efSLei Wen s->hash_size = 1 << s->hash_bits; 2788a5f34efSLei Wen s->hash_mask = s->hash_size - 1; 2798a5f34efSLei Wen s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); 2808a5f34efSLei Wen 2818a5f34efSLei Wen s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); 2828a5f34efSLei Wen s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 2838a5f34efSLei Wen s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 2848a5f34efSLei Wen 2858a5f34efSLei Wen s->high_water = 0; /* nothing written to s->window yet */ 2868a5f34efSLei Wen 2878a5f34efSLei Wen s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 2888a5f34efSLei Wen 2898a5f34efSLei Wen overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); 2908a5f34efSLei Wen s->pending_buf = (uchf *) overlay; 2918a5f34efSLei Wen s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); 2928a5f34efSLei Wen 2938a5f34efSLei Wen if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 2948a5f34efSLei Wen s->pending_buf == Z_NULL) { 2958a5f34efSLei Wen s->status = FINISH_STATE; 2968a5f34efSLei Wen strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); 2978a5f34efSLei Wen deflateEnd (strm); 2988a5f34efSLei Wen return Z_MEM_ERROR; 2998a5f34efSLei Wen } 3008a5f34efSLei Wen s->d_buf = overlay + s->lit_bufsize/sizeof(ush); 3018a5f34efSLei Wen s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; 3028a5f34efSLei Wen 3038a5f34efSLei Wen s->level = level; 3048a5f34efSLei Wen s->strategy = strategy; 3058a5f34efSLei Wen s->method = (Byte)method; 3068a5f34efSLei Wen 3078a5f34efSLei Wen return deflateReset(strm); 3088a5f34efSLei Wen } 3098a5f34efSLei Wen 3108a5f34efSLei Wen /* ========================================================================= */ 3118a5f34efSLei Wen int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) 3128a5f34efSLei Wen z_streamp strm; 3138a5f34efSLei Wen const Bytef *dictionary; 3148a5f34efSLei Wen uInt dictLength; 3158a5f34efSLei Wen { 3168a5f34efSLei Wen deflate_state *s; 3178a5f34efSLei Wen uInt length = dictLength; 3188a5f34efSLei Wen uInt n; 3198a5f34efSLei Wen IPos hash_head = 0; 3208a5f34efSLei Wen 3218a5f34efSLei Wen if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || 3228a5f34efSLei Wen strm->state->wrap == 2 || 3238a5f34efSLei Wen (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) 3248a5f34efSLei Wen return Z_STREAM_ERROR; 3258a5f34efSLei Wen 3268a5f34efSLei Wen s = strm->state; 3278a5f34efSLei Wen if (s->wrap) 3288a5f34efSLei Wen strm->adler = adler32(strm->adler, dictionary, dictLength); 3298a5f34efSLei Wen 3308a5f34efSLei Wen if (length < MIN_MATCH) return Z_OK; 3318a5f34efSLei Wen if (length > s->w_size) { 3328a5f34efSLei Wen length = s->w_size; 3338a5f34efSLei Wen dictionary += dictLength - length; /* use the tail of the dictionary */ 3348a5f34efSLei Wen } 3358a5f34efSLei Wen zmemcpy(s->window, dictionary, length); 3368a5f34efSLei Wen s->strstart = length; 3378a5f34efSLei Wen s->block_start = (long)length; 3388a5f34efSLei Wen 3398a5f34efSLei Wen /* Insert all strings in the hash table (except for the last two bytes). 3408a5f34efSLei Wen * s->lookahead stays null, so s->ins_h will be recomputed at the next 3418a5f34efSLei Wen * call of fill_window. 3428a5f34efSLei Wen */ 3438a5f34efSLei Wen s->ins_h = s->window[0]; 3448a5f34efSLei Wen UPDATE_HASH(s, s->ins_h, s->window[1]); 3458a5f34efSLei Wen for (n = 0; n <= length - MIN_MATCH; n++) { 3468a5f34efSLei Wen INSERT_STRING(s, n, hash_head); 3478a5f34efSLei Wen } 3488a5f34efSLei Wen if (hash_head) hash_head = 0; /* to make compiler happy */ 3498a5f34efSLei Wen return Z_OK; 3508a5f34efSLei Wen } 3518a5f34efSLei Wen 3528a5f34efSLei Wen /* ========================================================================= */ 3538a5f34efSLei Wen int ZEXPORT deflateReset (strm) 3548a5f34efSLei Wen z_streamp strm; 3558a5f34efSLei Wen { 3568a5f34efSLei Wen deflate_state *s; 3578a5f34efSLei Wen 3588a5f34efSLei Wen if (strm == Z_NULL || strm->state == Z_NULL || 3598a5f34efSLei Wen strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { 3608a5f34efSLei Wen return Z_STREAM_ERROR; 3618a5f34efSLei Wen } 3628a5f34efSLei Wen 3638a5f34efSLei Wen strm->total_in = strm->total_out = 0; 3648a5f34efSLei Wen strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ 3658a5f34efSLei Wen strm->data_type = Z_UNKNOWN; 3668a5f34efSLei Wen 3678a5f34efSLei Wen s = (deflate_state *)strm->state; 3688a5f34efSLei Wen s->pending = 0; 3698a5f34efSLei Wen s->pending_out = s->pending_buf; 3708a5f34efSLei Wen 3718a5f34efSLei Wen if (s->wrap < 0) { 3728a5f34efSLei Wen s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ 3738a5f34efSLei Wen } 3748a5f34efSLei Wen s->status = s->wrap ? INIT_STATE : BUSY_STATE; 3758a5f34efSLei Wen strm->adler = 3768a5f34efSLei Wen #ifdef GZIP 3778a5f34efSLei Wen s->wrap == 2 ? crc32(0L, Z_NULL, 0) : 3788a5f34efSLei Wen #endif 3798a5f34efSLei Wen adler32(0L, Z_NULL, 0); 3808a5f34efSLei Wen s->last_flush = Z_NO_FLUSH; 3818a5f34efSLei Wen 3828a5f34efSLei Wen _tr_init(s); 3838a5f34efSLei Wen lm_init(s); 3848a5f34efSLei Wen 3858a5f34efSLei Wen return Z_OK; 3868a5f34efSLei Wen } 3878a5f34efSLei Wen 3888a5f34efSLei Wen /* ========================================================================= */ 3898a5f34efSLei Wen int ZEXPORT deflateSetHeader (strm, head) 3908a5f34efSLei Wen z_streamp strm; 3918a5f34efSLei Wen gz_headerp head; 3928a5f34efSLei Wen { 3938a5f34efSLei Wen if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 3948a5f34efSLei Wen if (strm->state->wrap != 2) return Z_STREAM_ERROR; 3958a5f34efSLei Wen strm->state->gzhead = head; 3968a5f34efSLei Wen return Z_OK; 3978a5f34efSLei Wen } 3988a5f34efSLei Wen 3998a5f34efSLei Wen /* ========================================================================= */ 4008a5f34efSLei Wen int ZEXPORT deflatePrime (strm, bits, value) 4018a5f34efSLei Wen z_streamp strm; 4028a5f34efSLei Wen int bits; 4038a5f34efSLei Wen int value; 4048a5f34efSLei Wen { 4058a5f34efSLei Wen if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 4068a5f34efSLei Wen strm->state->bi_valid = bits; 4078a5f34efSLei Wen strm->state->bi_buf = (ush)(value & ((1 << bits) - 1)); 4088a5f34efSLei Wen return Z_OK; 4098a5f34efSLei Wen } 4108a5f34efSLei Wen 4118a5f34efSLei Wen /* ========================================================================= */ 4128a5f34efSLei Wen int ZEXPORT deflateParams(strm, level, strategy) 4138a5f34efSLei Wen z_streamp strm; 4148a5f34efSLei Wen int level; 4158a5f34efSLei Wen int strategy; 4168a5f34efSLei Wen { 4178a5f34efSLei Wen deflate_state *s; 4188a5f34efSLei Wen compress_func func; 4198a5f34efSLei Wen int err = Z_OK; 4208a5f34efSLei Wen 4218a5f34efSLei Wen if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 4228a5f34efSLei Wen s = strm->state; 4238a5f34efSLei Wen 4248a5f34efSLei Wen #ifdef FASTEST 4258a5f34efSLei Wen if (level != 0) level = 1; 4268a5f34efSLei Wen #else 4278a5f34efSLei Wen if (level == Z_DEFAULT_COMPRESSION) level = 6; 4288a5f34efSLei Wen #endif 4298a5f34efSLei Wen if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { 4308a5f34efSLei Wen return Z_STREAM_ERROR; 4318a5f34efSLei Wen } 4328a5f34efSLei Wen func = configuration_table[s->level].func; 4338a5f34efSLei Wen 4348a5f34efSLei Wen if ((strategy != s->strategy || func != configuration_table[level].func) && 4358a5f34efSLei Wen strm->total_in != 0) { 4368a5f34efSLei Wen /* Flush the last buffer: */ 4378a5f34efSLei Wen err = deflate(strm, Z_BLOCK); 4388a5f34efSLei Wen } 4398a5f34efSLei Wen if (s->level != level) { 4408a5f34efSLei Wen s->level = level; 4418a5f34efSLei Wen s->max_lazy_match = configuration_table[level].max_lazy; 4428a5f34efSLei Wen s->good_match = configuration_table[level].good_length; 4438a5f34efSLei Wen s->nice_match = configuration_table[level].nice_length; 4448a5f34efSLei Wen s->max_chain_length = configuration_table[level].max_chain; 4458a5f34efSLei Wen } 4468a5f34efSLei Wen s->strategy = strategy; 4478a5f34efSLei Wen return err; 4488a5f34efSLei Wen } 4498a5f34efSLei Wen 4508a5f34efSLei Wen /* ========================================================================= */ 4518a5f34efSLei Wen int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) 4528a5f34efSLei Wen z_streamp strm; 4538a5f34efSLei Wen int good_length; 4548a5f34efSLei Wen int max_lazy; 4558a5f34efSLei Wen int nice_length; 4568a5f34efSLei Wen int max_chain; 4578a5f34efSLei Wen { 4588a5f34efSLei Wen deflate_state *s; 4598a5f34efSLei Wen 4608a5f34efSLei Wen if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 4618a5f34efSLei Wen s = strm->state; 4628a5f34efSLei Wen s->good_match = good_length; 4638a5f34efSLei Wen s->max_lazy_match = max_lazy; 4648a5f34efSLei Wen s->nice_match = nice_length; 4658a5f34efSLei Wen s->max_chain_length = max_chain; 4668a5f34efSLei Wen return Z_OK; 4678a5f34efSLei Wen } 4688a5f34efSLei Wen 4698a5f34efSLei Wen /* ========================================================================= 4708a5f34efSLei Wen * For the default windowBits of 15 and memLevel of 8, this function returns 4718a5f34efSLei Wen * a close to exact, as well as small, upper bound on the compressed size. 4728a5f34efSLei Wen * They are coded as constants here for a reason--if the #define's are 4738a5f34efSLei Wen * changed, then this function needs to be changed as well. The return 4748a5f34efSLei Wen * value for 15 and 8 only works for those exact settings. 4758a5f34efSLei Wen * 4768a5f34efSLei Wen * For any setting other than those defaults for windowBits and memLevel, 4778a5f34efSLei Wen * the value returned is a conservative worst case for the maximum expansion 4788a5f34efSLei Wen * resulting from using fixed blocks instead of stored blocks, which deflate 4798a5f34efSLei Wen * can emit on compressed data for some combinations of the parameters. 4808a5f34efSLei Wen * 4818a5f34efSLei Wen * This function could be more sophisticated to provide closer upper bounds for 4828a5f34efSLei Wen * every combination of windowBits and memLevel. But even the conservative 4838a5f34efSLei Wen * upper bound of about 14% expansion does not seem onerous for output buffer 4848a5f34efSLei Wen * allocation. 4858a5f34efSLei Wen */ 4868a5f34efSLei Wen uLong ZEXPORT deflateBound(strm, sourceLen) 4878a5f34efSLei Wen z_streamp strm; 4888a5f34efSLei Wen uLong sourceLen; 4898a5f34efSLei Wen { 4908a5f34efSLei Wen deflate_state *s; 4918a5f34efSLei Wen uLong complen, wraplen; 4928a5f34efSLei Wen Bytef *str; 4938a5f34efSLei Wen 4948a5f34efSLei Wen /* conservative upper bound for compressed data */ 4958a5f34efSLei Wen complen = sourceLen + 4968a5f34efSLei Wen ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; 4978a5f34efSLei Wen 4988a5f34efSLei Wen /* if can't get parameters, return conservative bound plus zlib wrapper */ 4998a5f34efSLei Wen if (strm == Z_NULL || strm->state == Z_NULL) 5008a5f34efSLei Wen return complen + 6; 5018a5f34efSLei Wen 5028a5f34efSLei Wen /* compute wrapper length */ 5038a5f34efSLei Wen s = strm->state; 5048a5f34efSLei Wen switch (s->wrap) { 5058a5f34efSLei Wen case 0: /* raw deflate */ 5068a5f34efSLei Wen wraplen = 0; 5078a5f34efSLei Wen break; 5088a5f34efSLei Wen case 1: /* zlib wrapper */ 5098a5f34efSLei Wen wraplen = 6 + (s->strstart ? 4 : 0); 5108a5f34efSLei Wen break; 5118a5f34efSLei Wen case 2: /* gzip wrapper */ 5128a5f34efSLei Wen wraplen = 18; 5138a5f34efSLei Wen if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ 5148a5f34efSLei Wen if (s->gzhead->extra != Z_NULL) 5158a5f34efSLei Wen wraplen += 2 + s->gzhead->extra_len; 5168a5f34efSLei Wen str = s->gzhead->name; 5178a5f34efSLei Wen if (str != Z_NULL) 5188a5f34efSLei Wen do { 5198a5f34efSLei Wen wraplen++; 5208a5f34efSLei Wen } while (*str++); 5218a5f34efSLei Wen str = s->gzhead->comment; 5228a5f34efSLei Wen if (str != Z_NULL) 5238a5f34efSLei Wen do { 5248a5f34efSLei Wen wraplen++; 5258a5f34efSLei Wen } while (*str++); 5268a5f34efSLei Wen if (s->gzhead->hcrc) 5278a5f34efSLei Wen wraplen += 2; 5288a5f34efSLei Wen } 5298a5f34efSLei Wen break; 5308a5f34efSLei Wen default: /* for compiler happiness */ 5318a5f34efSLei Wen wraplen = 6; 5328a5f34efSLei Wen } 5338a5f34efSLei Wen 5348a5f34efSLei Wen /* if not default parameters, return conservative bound */ 5358a5f34efSLei Wen if (s->w_bits != 15 || s->hash_bits != 8 + 7) 5368a5f34efSLei Wen return complen + wraplen; 5378a5f34efSLei Wen 5388a5f34efSLei Wen /* default settings: return tight bound for that case */ 5398a5f34efSLei Wen return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 5408a5f34efSLei Wen (sourceLen >> 25) + 13 - 6 + wraplen; 5418a5f34efSLei Wen } 5428a5f34efSLei Wen 5438a5f34efSLei Wen /* ========================================================================= 5448a5f34efSLei Wen * Put a short in the pending buffer. The 16-bit value is put in MSB order. 5458a5f34efSLei Wen * IN assertion: the stream state is correct and there is enough room in 5468a5f34efSLei Wen * pending_buf. 5478a5f34efSLei Wen */ 5488a5f34efSLei Wen local void putShortMSB (s, b) 5498a5f34efSLei Wen deflate_state *s; 5508a5f34efSLei Wen uInt b; 5518a5f34efSLei Wen { 5528a5f34efSLei Wen put_byte(s, (Byte)(b >> 8)); 5538a5f34efSLei Wen put_byte(s, (Byte)(b & 0xff)); 5548a5f34efSLei Wen } 5558a5f34efSLei Wen 5568a5f34efSLei Wen /* ========================================================================= 5578a5f34efSLei Wen * Flush as much pending output as possible. All deflate() output goes 5588a5f34efSLei Wen * through this function so some applications may wish to modify it 5598a5f34efSLei Wen * to avoid allocating a large strm->next_out buffer and copying into it. 5608a5f34efSLei Wen * (See also read_buf()). 5618a5f34efSLei Wen */ 5628a5f34efSLei Wen local void flush_pending(strm) 5638a5f34efSLei Wen z_streamp strm; 5648a5f34efSLei Wen { 5658a5f34efSLei Wen unsigned len = strm->state->pending; 5668a5f34efSLei Wen 5678a5f34efSLei Wen if (len > strm->avail_out) len = strm->avail_out; 5688a5f34efSLei Wen if (len == 0) return; 5698a5f34efSLei Wen 5708a5f34efSLei Wen zmemcpy(strm->next_out, strm->state->pending_out, len); 5718a5f34efSLei Wen strm->next_out += len; 5728a5f34efSLei Wen strm->state->pending_out += len; 5738a5f34efSLei Wen strm->total_out += len; 5748a5f34efSLei Wen strm->avail_out -= len; 5758a5f34efSLei Wen strm->state->pending -= len; 5768a5f34efSLei Wen if (strm->state->pending == 0) { 5778a5f34efSLei Wen strm->state->pending_out = strm->state->pending_buf; 5788a5f34efSLei Wen } 5798a5f34efSLei Wen } 5808a5f34efSLei Wen 5818a5f34efSLei Wen /* ========================================================================= */ 5828a5f34efSLei Wen int ZEXPORT deflate (strm, flush) 5838a5f34efSLei Wen z_streamp strm; 5848a5f34efSLei Wen int flush; 5858a5f34efSLei Wen { 5868a5f34efSLei Wen int old_flush; /* value of flush param for previous deflate call */ 5878a5f34efSLei Wen deflate_state *s; 5888a5f34efSLei Wen 5898a5f34efSLei Wen if (strm == Z_NULL || strm->state == Z_NULL || 5908a5f34efSLei Wen flush > Z_BLOCK || flush < 0) { 5918a5f34efSLei Wen return Z_STREAM_ERROR; 5928a5f34efSLei Wen } 5938a5f34efSLei Wen s = strm->state; 5948a5f34efSLei Wen 595*869c2abbSLei Wen if (s->status == FINISH_STATE && flush != Z_FINISH) { 5968a5f34efSLei Wen ERR_RETURN(strm, Z_STREAM_ERROR); 5978a5f34efSLei Wen } 5988a5f34efSLei Wen if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 5998a5f34efSLei Wen 6008a5f34efSLei Wen s->strm = strm; /* just in case */ 6018a5f34efSLei Wen old_flush = s->last_flush; 6028a5f34efSLei Wen s->last_flush = flush; 6038a5f34efSLei Wen 6048a5f34efSLei Wen /* Write the header */ 6058a5f34efSLei Wen if (s->status == INIT_STATE) { 6068a5f34efSLei Wen #ifdef GZIP 6078a5f34efSLei Wen if (s->wrap == 2) { 6088a5f34efSLei Wen strm->adler = crc32(0L, Z_NULL, 0); 6098a5f34efSLei Wen put_byte(s, 31); 6108a5f34efSLei Wen put_byte(s, 139); 6118a5f34efSLei Wen put_byte(s, 8); 6128a5f34efSLei Wen if (s->gzhead == Z_NULL) { 6138a5f34efSLei Wen put_byte(s, 0); 6148a5f34efSLei Wen put_byte(s, 0); 6158a5f34efSLei Wen put_byte(s, 0); 6168a5f34efSLei Wen put_byte(s, 0); 6178a5f34efSLei Wen put_byte(s, 0); 6188a5f34efSLei Wen put_byte(s, s->level == 9 ? 2 : 6198a5f34efSLei Wen (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 6208a5f34efSLei Wen 4 : 0)); 6218a5f34efSLei Wen put_byte(s, OS_CODE); 6228a5f34efSLei Wen s->status = BUSY_STATE; 6238a5f34efSLei Wen } 6248a5f34efSLei Wen else { 6258a5f34efSLei Wen put_byte(s, (s->gzhead->text ? 1 : 0) + 6268a5f34efSLei Wen (s->gzhead->hcrc ? 2 : 0) + 6278a5f34efSLei Wen (s->gzhead->extra == Z_NULL ? 0 : 4) + 6288a5f34efSLei Wen (s->gzhead->name == Z_NULL ? 0 : 8) + 6298a5f34efSLei Wen (s->gzhead->comment == Z_NULL ? 0 : 16) 6308a5f34efSLei Wen ); 6318a5f34efSLei Wen put_byte(s, (Byte)(s->gzhead->time & 0xff)); 6328a5f34efSLei Wen put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); 6338a5f34efSLei Wen put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); 6348a5f34efSLei Wen put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); 6358a5f34efSLei Wen put_byte(s, s->level == 9 ? 2 : 6368a5f34efSLei Wen (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 6378a5f34efSLei Wen 4 : 0)); 6388a5f34efSLei Wen put_byte(s, s->gzhead->os & 0xff); 6398a5f34efSLei Wen if (s->gzhead->extra != Z_NULL) { 6408a5f34efSLei Wen put_byte(s, s->gzhead->extra_len & 0xff); 6418a5f34efSLei Wen put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); 6428a5f34efSLei Wen } 6438a5f34efSLei Wen if (s->gzhead->hcrc) 6448a5f34efSLei Wen strm->adler = crc32(strm->adler, s->pending_buf, 6458a5f34efSLei Wen s->pending); 6468a5f34efSLei Wen s->gzindex = 0; 6478a5f34efSLei Wen s->status = EXTRA_STATE; 6488a5f34efSLei Wen } 6498a5f34efSLei Wen } 6508a5f34efSLei Wen else 6518a5f34efSLei Wen #endif 6528a5f34efSLei Wen { 6538a5f34efSLei Wen uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; 6548a5f34efSLei Wen uInt level_flags; 6558a5f34efSLei Wen 6568a5f34efSLei Wen if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) 6578a5f34efSLei Wen level_flags = 0; 6588a5f34efSLei Wen else if (s->level < 6) 6598a5f34efSLei Wen level_flags = 1; 6608a5f34efSLei Wen else if (s->level == 6) 6618a5f34efSLei Wen level_flags = 2; 6628a5f34efSLei Wen else 6638a5f34efSLei Wen level_flags = 3; 6648a5f34efSLei Wen header |= (level_flags << 6); 6658a5f34efSLei Wen if (s->strstart != 0) header |= PRESET_DICT; 6668a5f34efSLei Wen header += 31 - (header % 31); 6678a5f34efSLei Wen 6688a5f34efSLei Wen s->status = BUSY_STATE; 6698a5f34efSLei Wen putShortMSB(s, header); 6708a5f34efSLei Wen 6718a5f34efSLei Wen /* Save the adler32 of the preset dictionary: */ 6728a5f34efSLei Wen if (s->strstart != 0) { 6738a5f34efSLei Wen putShortMSB(s, (uInt)(strm->adler >> 16)); 6748a5f34efSLei Wen putShortMSB(s, (uInt)(strm->adler & 0xffff)); 6758a5f34efSLei Wen } 6768a5f34efSLei Wen strm->adler = adler32(0L, Z_NULL, 0); 6778a5f34efSLei Wen } 6788a5f34efSLei Wen } 6798a5f34efSLei Wen #ifdef GZIP 6808a5f34efSLei Wen if (s->status == EXTRA_STATE) { 6818a5f34efSLei Wen if (s->gzhead->extra != Z_NULL) { 6828a5f34efSLei Wen uInt beg = s->pending; /* start of bytes to update crc */ 6838a5f34efSLei Wen 6848a5f34efSLei Wen while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { 6858a5f34efSLei Wen if (s->pending == s->pending_buf_size) { 6868a5f34efSLei Wen if (s->gzhead->hcrc && s->pending > beg) 6878a5f34efSLei Wen strm->adler = crc32(strm->adler, s->pending_buf + beg, 6888a5f34efSLei Wen s->pending - beg); 6898a5f34efSLei Wen flush_pending(strm); 6908a5f34efSLei Wen beg = s->pending; 6918a5f34efSLei Wen if (s->pending == s->pending_buf_size) 6928a5f34efSLei Wen break; 6938a5f34efSLei Wen } 6948a5f34efSLei Wen put_byte(s, s->gzhead->extra[s->gzindex]); 6958a5f34efSLei Wen s->gzindex++; 6968a5f34efSLei Wen } 6978a5f34efSLei Wen if (s->gzhead->hcrc && s->pending > beg) 6988a5f34efSLei Wen strm->adler = crc32(strm->adler, s->pending_buf + beg, 6998a5f34efSLei Wen s->pending - beg); 7008a5f34efSLei Wen if (s->gzindex == s->gzhead->extra_len) { 7018a5f34efSLei Wen s->gzindex = 0; 7028a5f34efSLei Wen s->status = NAME_STATE; 7038a5f34efSLei Wen } 7048a5f34efSLei Wen } 7058a5f34efSLei Wen else 7068a5f34efSLei Wen s->status = NAME_STATE; 7078a5f34efSLei Wen } 7088a5f34efSLei Wen if (s->status == NAME_STATE) { 7098a5f34efSLei Wen if (s->gzhead->name != Z_NULL) { 7108a5f34efSLei Wen uInt beg = s->pending; /* start of bytes to update crc */ 7118a5f34efSLei Wen int val; 7128a5f34efSLei Wen 7138a5f34efSLei Wen do { 7148a5f34efSLei Wen if (s->pending == s->pending_buf_size) { 7158a5f34efSLei Wen if (s->gzhead->hcrc && s->pending > beg) 7168a5f34efSLei Wen strm->adler = crc32(strm->adler, s->pending_buf + beg, 7178a5f34efSLei Wen s->pending - beg); 7188a5f34efSLei Wen flush_pending(strm); 7198a5f34efSLei Wen beg = s->pending; 7208a5f34efSLei Wen if (s->pending == s->pending_buf_size) { 7218a5f34efSLei Wen val = 1; 7228a5f34efSLei Wen break; 7238a5f34efSLei Wen } 7248a5f34efSLei Wen } 7258a5f34efSLei Wen val = s->gzhead->name[s->gzindex++]; 7268a5f34efSLei Wen put_byte(s, val); 7278a5f34efSLei Wen } while (val != 0); 7288a5f34efSLei Wen if (s->gzhead->hcrc && s->pending > beg) 7298a5f34efSLei Wen strm->adler = crc32(strm->adler, s->pending_buf + beg, 7308a5f34efSLei Wen s->pending - beg); 7318a5f34efSLei Wen if (val == 0) { 7328a5f34efSLei Wen s->gzindex = 0; 7338a5f34efSLei Wen s->status = COMMENT_STATE; 7348a5f34efSLei Wen } 7358a5f34efSLei Wen } 7368a5f34efSLei Wen else 7378a5f34efSLei Wen s->status = COMMENT_STATE; 7388a5f34efSLei Wen } 7398a5f34efSLei Wen if (s->status == COMMENT_STATE) { 7408a5f34efSLei Wen if (s->gzhead->comment != Z_NULL) { 7418a5f34efSLei Wen uInt beg = s->pending; /* start of bytes to update crc */ 7428a5f34efSLei Wen int val; 7438a5f34efSLei Wen 7448a5f34efSLei Wen do { 7458a5f34efSLei Wen if (s->pending == s->pending_buf_size) { 7468a5f34efSLei Wen if (s->gzhead->hcrc && s->pending > beg) 7478a5f34efSLei Wen strm->adler = crc32(strm->adler, s->pending_buf + beg, 7488a5f34efSLei Wen s->pending - beg); 7498a5f34efSLei Wen flush_pending(strm); 7508a5f34efSLei Wen beg = s->pending; 7518a5f34efSLei Wen if (s->pending == s->pending_buf_size) { 7528a5f34efSLei Wen val = 1; 7538a5f34efSLei Wen break; 7548a5f34efSLei Wen } 7558a5f34efSLei Wen } 7568a5f34efSLei Wen val = s->gzhead->comment[s->gzindex++]; 7578a5f34efSLei Wen put_byte(s, val); 7588a5f34efSLei Wen } while (val != 0); 7598a5f34efSLei Wen if (s->gzhead->hcrc && s->pending > beg) 7608a5f34efSLei Wen strm->adler = crc32(strm->adler, s->pending_buf + beg, 7618a5f34efSLei Wen s->pending - beg); 7628a5f34efSLei Wen if (val == 0) 7638a5f34efSLei Wen s->status = HCRC_STATE; 7648a5f34efSLei Wen } 7658a5f34efSLei Wen else 7668a5f34efSLei Wen s->status = HCRC_STATE; 7678a5f34efSLei Wen } 7688a5f34efSLei Wen if (s->status == HCRC_STATE) { 7698a5f34efSLei Wen if (s->gzhead->hcrc) { 7708a5f34efSLei Wen if (s->pending + 2 > s->pending_buf_size) 7718a5f34efSLei Wen flush_pending(strm); 7728a5f34efSLei Wen if (s->pending + 2 <= s->pending_buf_size) { 7738a5f34efSLei Wen put_byte(s, (Byte)(strm->adler & 0xff)); 7748a5f34efSLei Wen put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 7758a5f34efSLei Wen strm->adler = crc32(0L, Z_NULL, 0); 7768a5f34efSLei Wen s->status = BUSY_STATE; 7778a5f34efSLei Wen } 7788a5f34efSLei Wen } 7798a5f34efSLei Wen else 7808a5f34efSLei Wen s->status = BUSY_STATE; 7818a5f34efSLei Wen } 7828a5f34efSLei Wen #endif 7838a5f34efSLei Wen 7848a5f34efSLei Wen /* Flush as much pending output as possible */ 7858a5f34efSLei Wen if (s->pending != 0) { 7868a5f34efSLei Wen flush_pending(strm); 7878a5f34efSLei Wen if (strm->avail_out == 0) { 7888a5f34efSLei Wen /* Since avail_out is 0, deflate will be called again with 7898a5f34efSLei Wen * more output space, but possibly with both pending and 7908a5f34efSLei Wen * avail_in equal to zero. There won't be anything to do, 7918a5f34efSLei Wen * but this is not an error situation so make sure we 7928a5f34efSLei Wen * return OK instead of BUF_ERROR at next call of deflate: 7938a5f34efSLei Wen */ 7948a5f34efSLei Wen s->last_flush = -1; 7958a5f34efSLei Wen return Z_OK; 7968a5f34efSLei Wen } 7978a5f34efSLei Wen 7988a5f34efSLei Wen /* Make sure there is something to do and avoid duplicate consecutive 7998a5f34efSLei Wen * flushes. For repeated and useless calls with Z_FINISH, we keep 8008a5f34efSLei Wen * returning Z_STREAM_END instead of Z_BUF_ERROR. 8018a5f34efSLei Wen */ 8028a5f34efSLei Wen } else if (strm->avail_in == 0 && flush <= old_flush && 8038a5f34efSLei Wen flush != Z_FINISH) { 8048a5f34efSLei Wen ERR_RETURN(strm, Z_BUF_ERROR); 8058a5f34efSLei Wen } 8068a5f34efSLei Wen 8078a5f34efSLei Wen /* User must not provide more input after the first FINISH: */ 8088a5f34efSLei Wen if (s->status == FINISH_STATE && strm->avail_in != 0) { 8098a5f34efSLei Wen ERR_RETURN(strm, Z_BUF_ERROR); 8108a5f34efSLei Wen } 8118a5f34efSLei Wen 8128a5f34efSLei Wen /* Start a new block or continue the current one. 8138a5f34efSLei Wen */ 8148a5f34efSLei Wen if (strm->avail_in != 0 || s->lookahead != 0 || 8158a5f34efSLei Wen (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { 8168a5f34efSLei Wen block_state bstate; 8178a5f34efSLei Wen 8188a5f34efSLei Wen bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : 8198a5f34efSLei Wen (s->strategy == Z_RLE ? deflate_rle(s, flush) : 8208a5f34efSLei Wen (*(configuration_table[s->level].func))(s, flush)); 8218a5f34efSLei Wen 8228a5f34efSLei Wen if (bstate == finish_started || bstate == finish_done) { 8238a5f34efSLei Wen s->status = FINISH_STATE; 8248a5f34efSLei Wen } 8258a5f34efSLei Wen if (bstate == need_more || bstate == finish_started) { 8268a5f34efSLei Wen if (strm->avail_out == 0) { 8278a5f34efSLei Wen s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ 8288a5f34efSLei Wen } 8298a5f34efSLei Wen return Z_OK; 8308a5f34efSLei Wen /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 8318a5f34efSLei Wen * of deflate should use the same flush parameter to make sure 8328a5f34efSLei Wen * that the flush is complete. So we don't have to output an 8338a5f34efSLei Wen * empty block here, this will be done at next call. This also 8348a5f34efSLei Wen * ensures that for a very small output buffer, we emit at most 8358a5f34efSLei Wen * one empty block. 8368a5f34efSLei Wen */ 8378a5f34efSLei Wen } 8388a5f34efSLei Wen if (bstate == block_done) { 8398a5f34efSLei Wen if (flush == Z_PARTIAL_FLUSH) { 8408a5f34efSLei Wen _tr_align(s); 8418a5f34efSLei Wen } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ 8428a5f34efSLei Wen _tr_stored_block(s, (char*)0, 0L, 0); 8438a5f34efSLei Wen /* For a full flush, this empty block will be recognized 8448a5f34efSLei Wen * as a special marker by inflate_sync(). 8458a5f34efSLei Wen */ 8468a5f34efSLei Wen if (flush == Z_FULL_FLUSH) { 8478a5f34efSLei Wen CLEAR_HASH(s); /* forget history */ 8488a5f34efSLei Wen if (s->lookahead == 0) { 8498a5f34efSLei Wen s->strstart = 0; 8508a5f34efSLei Wen s->block_start = 0L; 8518a5f34efSLei Wen } 8528a5f34efSLei Wen } 8538a5f34efSLei Wen } 8548a5f34efSLei Wen flush_pending(strm); 8558a5f34efSLei Wen if (strm->avail_out == 0) { 8568a5f34efSLei Wen s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ 8578a5f34efSLei Wen return Z_OK; 8588a5f34efSLei Wen } 8598a5f34efSLei Wen } 8608a5f34efSLei Wen } 8618a5f34efSLei Wen Assert(strm->avail_out > 0, "bug2"); 8628a5f34efSLei Wen 8638a5f34efSLei Wen if (flush != Z_FINISH) return Z_OK; 8648a5f34efSLei Wen if (s->wrap <= 0) return Z_STREAM_END; 8658a5f34efSLei Wen 8668a5f34efSLei Wen /* Write the trailer */ 8678a5f34efSLei Wen #ifdef GZIP 8688a5f34efSLei Wen if (s->wrap == 2) { 8698a5f34efSLei Wen put_byte(s, (Byte)(strm->adler & 0xff)); 8708a5f34efSLei Wen put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 8718a5f34efSLei Wen put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); 8728a5f34efSLei Wen put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); 8738a5f34efSLei Wen put_byte(s, (Byte)(strm->total_in & 0xff)); 8748a5f34efSLei Wen put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); 8758a5f34efSLei Wen put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); 8768a5f34efSLei Wen put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); 8778a5f34efSLei Wen } 8788a5f34efSLei Wen else 8798a5f34efSLei Wen #endif 8808a5f34efSLei Wen { 8818a5f34efSLei Wen putShortMSB(s, (uInt)(strm->adler >> 16)); 8828a5f34efSLei Wen putShortMSB(s, (uInt)(strm->adler & 0xffff)); 8838a5f34efSLei Wen } 8848a5f34efSLei Wen flush_pending(strm); 8858a5f34efSLei Wen /* If avail_out is zero, the application will call deflate again 8868a5f34efSLei Wen * to flush the rest. 8878a5f34efSLei Wen */ 8888a5f34efSLei Wen if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ 8898a5f34efSLei Wen return s->pending != 0 ? Z_OK : Z_STREAM_END; 8908a5f34efSLei Wen } 8918a5f34efSLei Wen 8928a5f34efSLei Wen /* ========================================================================= */ 8938a5f34efSLei Wen int ZEXPORT deflateEnd (strm) 8948a5f34efSLei Wen z_streamp strm; 8958a5f34efSLei Wen { 8968a5f34efSLei Wen int status; 8978a5f34efSLei Wen 8988a5f34efSLei Wen if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 8998a5f34efSLei Wen 9008a5f34efSLei Wen status = strm->state->status; 9018a5f34efSLei Wen if (status != INIT_STATE && 9028a5f34efSLei Wen status != EXTRA_STATE && 9038a5f34efSLei Wen status != NAME_STATE && 9048a5f34efSLei Wen status != COMMENT_STATE && 9058a5f34efSLei Wen status != HCRC_STATE && 9068a5f34efSLei Wen status != BUSY_STATE && 9078a5f34efSLei Wen status != FINISH_STATE) { 9088a5f34efSLei Wen return Z_STREAM_ERROR; 9098a5f34efSLei Wen } 9108a5f34efSLei Wen 9118a5f34efSLei Wen /* Deallocate in reverse order of allocations: */ 9128a5f34efSLei Wen TRY_FREE(strm, strm->state->pending_buf); 9138a5f34efSLei Wen TRY_FREE(strm, strm->state->head); 9148a5f34efSLei Wen TRY_FREE(strm, strm->state->prev); 9158a5f34efSLei Wen TRY_FREE(strm, strm->state->window); 9168a5f34efSLei Wen 9178a5f34efSLei Wen ZFREE(strm, strm->state); 9188a5f34efSLei Wen strm->state = Z_NULL; 9198a5f34efSLei Wen 9208a5f34efSLei Wen return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; 9218a5f34efSLei Wen } 9228a5f34efSLei Wen 9238a5f34efSLei Wen /* ========================================================================= 9248a5f34efSLei Wen * Copy the source state to the destination state. 9258a5f34efSLei Wen * To simplify the source, this is not supported for 16-bit MSDOS (which 9268a5f34efSLei Wen * doesn't have enough memory anyway to duplicate compression states). 9278a5f34efSLei Wen */ 9288a5f34efSLei Wen int ZEXPORT deflateCopy (dest, source) 9298a5f34efSLei Wen z_streamp dest; 9308a5f34efSLei Wen z_streamp source; 9318a5f34efSLei Wen { 9328a5f34efSLei Wen #ifdef MAXSEG_64K 9338a5f34efSLei Wen return Z_STREAM_ERROR; 9348a5f34efSLei Wen #else 9358a5f34efSLei Wen deflate_state *ds; 9368a5f34efSLei Wen deflate_state *ss; 9378a5f34efSLei Wen ushf *overlay; 9388a5f34efSLei Wen 9398a5f34efSLei Wen 9408a5f34efSLei Wen if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { 9418a5f34efSLei Wen return Z_STREAM_ERROR; 9428a5f34efSLei Wen } 9438a5f34efSLei Wen 9448a5f34efSLei Wen ss = source->state; 9458a5f34efSLei Wen 9468a5f34efSLei Wen zmemcpy(dest, source, sizeof(z_stream)); 9478a5f34efSLei Wen 9488a5f34efSLei Wen ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); 9498a5f34efSLei Wen if (ds == Z_NULL) return Z_MEM_ERROR; 9508a5f34efSLei Wen dest->state = (struct internal_state FAR *) ds; 9518a5f34efSLei Wen zmemcpy(ds, ss, sizeof(deflate_state)); 9528a5f34efSLei Wen ds->strm = dest; 9538a5f34efSLei Wen 9548a5f34efSLei Wen ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); 9558a5f34efSLei Wen ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); 9568a5f34efSLei Wen ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); 9578a5f34efSLei Wen overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); 9588a5f34efSLei Wen ds->pending_buf = (uchf *) overlay; 9598a5f34efSLei Wen 9608a5f34efSLei Wen if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || 9618a5f34efSLei Wen ds->pending_buf == Z_NULL) { 9628a5f34efSLei Wen deflateEnd (dest); 9638a5f34efSLei Wen return Z_MEM_ERROR; 9648a5f34efSLei Wen } 9658a5f34efSLei Wen /* following zmemcpy do not work for 16-bit MSDOS */ 9668a5f34efSLei Wen zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); 9678a5f34efSLei Wen zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos)); 9688a5f34efSLei Wen zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos)); 9698a5f34efSLei Wen zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); 9708a5f34efSLei Wen 9718a5f34efSLei Wen ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); 9728a5f34efSLei Wen ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); 9738a5f34efSLei Wen ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; 9748a5f34efSLei Wen 9758a5f34efSLei Wen ds->l_desc.dyn_tree = ds->dyn_ltree; 9768a5f34efSLei Wen ds->d_desc.dyn_tree = ds->dyn_dtree; 9778a5f34efSLei Wen ds->bl_desc.dyn_tree = ds->bl_tree; 9788a5f34efSLei Wen 9798a5f34efSLei Wen return Z_OK; 9808a5f34efSLei Wen #endif /* MAXSEG_64K */ 9818a5f34efSLei Wen } 9828a5f34efSLei Wen 9838a5f34efSLei Wen /* =========================================================================== 9848a5f34efSLei Wen * Read a new buffer from the current input stream, update the adler32 9858a5f34efSLei Wen * and total number of bytes read. All deflate() input goes through 9868a5f34efSLei Wen * this function so some applications may wish to modify it to avoid 9878a5f34efSLei Wen * allocating a large strm->next_in buffer and copying from it. 9888a5f34efSLei Wen * (See also flush_pending()). 9898a5f34efSLei Wen */ 9908a5f34efSLei Wen local int read_buf(strm, buf, size) 9918a5f34efSLei Wen z_streamp strm; 9928a5f34efSLei Wen Bytef *buf; 9938a5f34efSLei Wen unsigned size; 9948a5f34efSLei Wen { 9958a5f34efSLei Wen unsigned len = strm->avail_in; 9968a5f34efSLei Wen 9978a5f34efSLei Wen if (len > size) len = size; 9988a5f34efSLei Wen if (len == 0) return 0; 9998a5f34efSLei Wen 10008a5f34efSLei Wen strm->avail_in -= len; 10018a5f34efSLei Wen 10028a5f34efSLei Wen if (strm->state->wrap == 1) { 10038a5f34efSLei Wen strm->adler = adler32(strm->adler, strm->next_in, len); 10048a5f34efSLei Wen } 10058a5f34efSLei Wen #ifdef GZIP 10068a5f34efSLei Wen else if (strm->state->wrap == 2) { 10078a5f34efSLei Wen strm->adler = crc32(strm->adler, strm->next_in, len); 10088a5f34efSLei Wen } 10098a5f34efSLei Wen #endif 10108a5f34efSLei Wen zmemcpy(buf, strm->next_in, len); 10118a5f34efSLei Wen strm->next_in += len; 10128a5f34efSLei Wen strm->total_in += len; 10138a5f34efSLei Wen 10148a5f34efSLei Wen return (int)len; 10158a5f34efSLei Wen } 10168a5f34efSLei Wen 10178a5f34efSLei Wen /* =========================================================================== 10188a5f34efSLei Wen * Initialize the "longest match" routines for a new zlib stream 10198a5f34efSLei Wen */ 10208a5f34efSLei Wen local void lm_init (s) 10218a5f34efSLei Wen deflate_state *s; 10228a5f34efSLei Wen { 10238a5f34efSLei Wen s->window_size = (ulg)2L*s->w_size; 10248a5f34efSLei Wen 10258a5f34efSLei Wen CLEAR_HASH(s); 10268a5f34efSLei Wen 10278a5f34efSLei Wen /* Set the default configuration parameters: 10288a5f34efSLei Wen */ 10298a5f34efSLei Wen s->max_lazy_match = configuration_table[s->level].max_lazy; 10308a5f34efSLei Wen s->good_match = configuration_table[s->level].good_length; 10318a5f34efSLei Wen s->nice_match = configuration_table[s->level].nice_length; 10328a5f34efSLei Wen s->max_chain_length = configuration_table[s->level].max_chain; 10338a5f34efSLei Wen 10348a5f34efSLei Wen s->strstart = 0; 10358a5f34efSLei Wen s->block_start = 0L; 10368a5f34efSLei Wen s->lookahead = 0; 10378a5f34efSLei Wen s->match_length = s->prev_length = MIN_MATCH-1; 10388a5f34efSLei Wen s->match_available = 0; 10398a5f34efSLei Wen s->ins_h = 0; 10408a5f34efSLei Wen #ifndef FASTEST 10418a5f34efSLei Wen #ifdef ASMV 10428a5f34efSLei Wen match_init(); /* initialize the asm code */ 10438a5f34efSLei Wen #endif 10448a5f34efSLei Wen #endif 10458a5f34efSLei Wen } 10468a5f34efSLei Wen 10478a5f34efSLei Wen #ifndef FASTEST 10488a5f34efSLei Wen /* =========================================================================== 10498a5f34efSLei Wen * Set match_start to the longest match starting at the given string and 10508a5f34efSLei Wen * return its length. Matches shorter or equal to prev_length are discarded, 10518a5f34efSLei Wen * in which case the result is equal to prev_length and match_start is 10528a5f34efSLei Wen * garbage. 10538a5f34efSLei Wen * IN assertions: cur_match is the head of the hash chain for the current 10548a5f34efSLei Wen * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 10558a5f34efSLei Wen * OUT assertion: the match length is not greater than s->lookahead. 10568a5f34efSLei Wen */ 10578a5f34efSLei Wen #ifndef ASMV 10588a5f34efSLei Wen /* For 80x86 and 680x0, an optimized version will be provided in match.asm or 10598a5f34efSLei Wen * match.S. The code will be functionally equivalent. 10608a5f34efSLei Wen */ 10618a5f34efSLei Wen local uInt longest_match(s, cur_match) 10628a5f34efSLei Wen deflate_state *s; 10638a5f34efSLei Wen IPos cur_match; /* current match */ 10648a5f34efSLei Wen { 10658a5f34efSLei Wen unsigned chain_length = s->max_chain_length;/* max hash chain length */ 10668a5f34efSLei Wen register Bytef *scan = s->window + s->strstart; /* current string */ 10678a5f34efSLei Wen register Bytef *match; /* matched string */ 10688a5f34efSLei Wen register int len; /* length of current match */ 10698a5f34efSLei Wen int best_len = s->prev_length; /* best match length so far */ 10708a5f34efSLei Wen int nice_match = s->nice_match; /* stop if match long enough */ 10718a5f34efSLei Wen IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 10728a5f34efSLei Wen s->strstart - (IPos)MAX_DIST(s) : NIL; 10738a5f34efSLei Wen /* Stop when cur_match becomes <= limit. To simplify the code, 10748a5f34efSLei Wen * we prevent matches with the string of window index 0. 10758a5f34efSLei Wen */ 10768a5f34efSLei Wen Posf *prev = s->prev; 10778a5f34efSLei Wen uInt wmask = s->w_mask; 10788a5f34efSLei Wen 10798a5f34efSLei Wen #ifdef UNALIGNED_OK 10808a5f34efSLei Wen /* Compare two bytes at a time. Note: this is not always beneficial. 10818a5f34efSLei Wen * Try with and without -DUNALIGNED_OK to check. 10828a5f34efSLei Wen */ 10838a5f34efSLei Wen register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; 10848a5f34efSLei Wen register ush scan_start = *(ushf*)scan; 10858a5f34efSLei Wen register ush scan_end = *(ushf*)(scan+best_len-1); 10868a5f34efSLei Wen #else 10878a5f34efSLei Wen register Bytef *strend = s->window + s->strstart + MAX_MATCH; 10888a5f34efSLei Wen register Byte scan_end1 = scan[best_len-1]; 10898a5f34efSLei Wen register Byte scan_end = scan[best_len]; 10908a5f34efSLei Wen #endif 10918a5f34efSLei Wen 10928a5f34efSLei Wen /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 10938a5f34efSLei Wen * It is easy to get rid of this optimization if necessary. 10948a5f34efSLei Wen */ 10958a5f34efSLei Wen Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 10968a5f34efSLei Wen 10978a5f34efSLei Wen /* Do not waste too much time if we already have a good match: */ 10988a5f34efSLei Wen if (s->prev_length >= s->good_match) { 10998a5f34efSLei Wen chain_length >>= 2; 11008a5f34efSLei Wen } 11018a5f34efSLei Wen /* Do not look for matches beyond the end of the input. This is necessary 11028a5f34efSLei Wen * to make deflate deterministic. 11038a5f34efSLei Wen */ 11048a5f34efSLei Wen if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; 11058a5f34efSLei Wen 11068a5f34efSLei Wen Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 11078a5f34efSLei Wen 11088a5f34efSLei Wen do { 11098a5f34efSLei Wen Assert(cur_match < s->strstart, "no future"); 11108a5f34efSLei Wen match = s->window + cur_match; 11118a5f34efSLei Wen 11128a5f34efSLei Wen /* Skip to next match if the match length cannot increase 11138a5f34efSLei Wen * or if the match length is less than 2. Note that the checks below 11148a5f34efSLei Wen * for insufficient lookahead only occur occasionally for performance 11158a5f34efSLei Wen * reasons. Therefore uninitialized memory will be accessed, and 11168a5f34efSLei Wen * conditional jumps will be made that depend on those values. 11178a5f34efSLei Wen * However the length of the match is limited to the lookahead, so 11188a5f34efSLei Wen * the output of deflate is not affected by the uninitialized values. 11198a5f34efSLei Wen */ 11208a5f34efSLei Wen #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 11218a5f34efSLei Wen /* This code assumes sizeof(unsigned short) == 2. Do not use 11228a5f34efSLei Wen * UNALIGNED_OK if your compiler uses a different size. 11238a5f34efSLei Wen */ 11248a5f34efSLei Wen if (*(ushf*)(match+best_len-1) != scan_end || 11258a5f34efSLei Wen *(ushf*)match != scan_start) continue; 11268a5f34efSLei Wen 11278a5f34efSLei Wen /* It is not necessary to compare scan[2] and match[2] since they are 11288a5f34efSLei Wen * always equal when the other bytes match, given that the hash keys 11298a5f34efSLei Wen * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at 11308a5f34efSLei Wen * strstart+3, +5, ... up to strstart+257. We check for insufficient 11318a5f34efSLei Wen * lookahead only every 4th comparison; the 128th check will be made 11328a5f34efSLei Wen * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is 11338a5f34efSLei Wen * necessary to put more guard bytes at the end of the window, or 11348a5f34efSLei Wen * to check more often for insufficient lookahead. 11358a5f34efSLei Wen */ 11368a5f34efSLei Wen Assert(scan[2] == match[2], "scan[2]?"); 11378a5f34efSLei Wen scan++, match++; 11388a5f34efSLei Wen do { 11398a5f34efSLei Wen } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && 11408a5f34efSLei Wen *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 11418a5f34efSLei Wen *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 11428a5f34efSLei Wen *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 11438a5f34efSLei Wen scan < strend); 11448a5f34efSLei Wen /* The funny "do {}" generates better code on most compilers */ 11458a5f34efSLei Wen 11468a5f34efSLei Wen /* Here, scan <= window+strstart+257 */ 11478a5f34efSLei Wen Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 11488a5f34efSLei Wen if (*scan == *match) scan++; 11498a5f34efSLei Wen 11508a5f34efSLei Wen len = (MAX_MATCH - 1) - (int)(strend-scan); 11518a5f34efSLei Wen scan = strend - (MAX_MATCH-1); 11528a5f34efSLei Wen 11538a5f34efSLei Wen #else /* UNALIGNED_OK */ 11548a5f34efSLei Wen 11558a5f34efSLei Wen if (match[best_len] != scan_end || 11568a5f34efSLei Wen match[best_len-1] != scan_end1 || 11578a5f34efSLei Wen *match != *scan || 11588a5f34efSLei Wen *++match != scan[1]) continue; 11598a5f34efSLei Wen 11608a5f34efSLei Wen /* The check at best_len-1 can be removed because it will be made 11618a5f34efSLei Wen * again later. (This heuristic is not always a win.) 11628a5f34efSLei Wen * It is not necessary to compare scan[2] and match[2] since they 11638a5f34efSLei Wen * are always equal when the other bytes match, given that 11648a5f34efSLei Wen * the hash keys are equal and that HASH_BITS >= 8. 11658a5f34efSLei Wen */ 11668a5f34efSLei Wen scan += 2, match++; 11678a5f34efSLei Wen Assert(*scan == *match, "match[2]?"); 11688a5f34efSLei Wen 11698a5f34efSLei Wen /* We check for insufficient lookahead only every 8th comparison; 11708a5f34efSLei Wen * the 256th check will be made at strstart+258. 11718a5f34efSLei Wen */ 11728a5f34efSLei Wen do { 11738a5f34efSLei Wen } while (*++scan == *++match && *++scan == *++match && 11748a5f34efSLei Wen *++scan == *++match && *++scan == *++match && 11758a5f34efSLei Wen *++scan == *++match && *++scan == *++match && 11768a5f34efSLei Wen *++scan == *++match && *++scan == *++match && 11778a5f34efSLei Wen scan < strend); 11788a5f34efSLei Wen 11798a5f34efSLei Wen Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 11808a5f34efSLei Wen 11818a5f34efSLei Wen len = MAX_MATCH - (int)(strend - scan); 11828a5f34efSLei Wen scan = strend - MAX_MATCH; 11838a5f34efSLei Wen 11848a5f34efSLei Wen #endif /* UNALIGNED_OK */ 11858a5f34efSLei Wen 11868a5f34efSLei Wen if (len > best_len) { 11878a5f34efSLei Wen s->match_start = cur_match; 11888a5f34efSLei Wen best_len = len; 11898a5f34efSLei Wen if (len >= nice_match) break; 11908a5f34efSLei Wen #ifdef UNALIGNED_OK 11918a5f34efSLei Wen scan_end = *(ushf*)(scan+best_len-1); 11928a5f34efSLei Wen #else 11938a5f34efSLei Wen scan_end1 = scan[best_len-1]; 11948a5f34efSLei Wen scan_end = scan[best_len]; 11958a5f34efSLei Wen #endif 11968a5f34efSLei Wen } 11978a5f34efSLei Wen } while ((cur_match = prev[cur_match & wmask]) > limit 11988a5f34efSLei Wen && --chain_length != 0); 11998a5f34efSLei Wen 12008a5f34efSLei Wen if ((uInt)best_len <= s->lookahead) return (uInt)best_len; 12018a5f34efSLei Wen return s->lookahead; 12028a5f34efSLei Wen } 12038a5f34efSLei Wen #endif /* ASMV */ 12048a5f34efSLei Wen 12058a5f34efSLei Wen #else /* FASTEST */ 12068a5f34efSLei Wen 12078a5f34efSLei Wen /* --------------------------------------------------------------------------- 12088a5f34efSLei Wen * Optimized version for FASTEST only 12098a5f34efSLei Wen */ 12108a5f34efSLei Wen local uInt longest_match(s, cur_match) 12118a5f34efSLei Wen deflate_state *s; 12128a5f34efSLei Wen IPos cur_match; /* current match */ 12138a5f34efSLei Wen { 12148a5f34efSLei Wen register Bytef *scan = s->window + s->strstart; /* current string */ 12158a5f34efSLei Wen register Bytef *match; /* matched string */ 12168a5f34efSLei Wen register int len; /* length of current match */ 12178a5f34efSLei Wen register Bytef *strend = s->window + s->strstart + MAX_MATCH; 12188a5f34efSLei Wen 12198a5f34efSLei Wen /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 12208a5f34efSLei Wen * It is easy to get rid of this optimization if necessary. 12218a5f34efSLei Wen */ 12228a5f34efSLei Wen Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 12238a5f34efSLei Wen 12248a5f34efSLei Wen Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 12258a5f34efSLei Wen 12268a5f34efSLei Wen Assert(cur_match < s->strstart, "no future"); 12278a5f34efSLei Wen 12288a5f34efSLei Wen match = s->window + cur_match; 12298a5f34efSLei Wen 12308a5f34efSLei Wen /* Return failure if the match length is less than 2: 12318a5f34efSLei Wen */ 12328a5f34efSLei Wen if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; 12338a5f34efSLei Wen 12348a5f34efSLei Wen /* The check at best_len-1 can be removed because it will be made 12358a5f34efSLei Wen * again later. (This heuristic is not always a win.) 12368a5f34efSLei Wen * It is not necessary to compare scan[2] and match[2] since they 12378a5f34efSLei Wen * are always equal when the other bytes match, given that 12388a5f34efSLei Wen * the hash keys are equal and that HASH_BITS >= 8. 12398a5f34efSLei Wen */ 12408a5f34efSLei Wen scan += 2, match += 2; 12418a5f34efSLei Wen Assert(*scan == *match, "match[2]?"); 12428a5f34efSLei Wen 12438a5f34efSLei Wen /* We check for insufficient lookahead only every 8th comparison; 12448a5f34efSLei Wen * the 256th check will be made at strstart+258. 12458a5f34efSLei Wen */ 12468a5f34efSLei Wen do { 12478a5f34efSLei Wen } while (*++scan == *++match && *++scan == *++match && 12488a5f34efSLei Wen *++scan == *++match && *++scan == *++match && 12498a5f34efSLei Wen *++scan == *++match && *++scan == *++match && 12508a5f34efSLei Wen *++scan == *++match && *++scan == *++match && 12518a5f34efSLei Wen scan < strend); 12528a5f34efSLei Wen 12538a5f34efSLei Wen Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 12548a5f34efSLei Wen 12558a5f34efSLei Wen len = MAX_MATCH - (int)(strend - scan); 12568a5f34efSLei Wen 12578a5f34efSLei Wen if (len < MIN_MATCH) return MIN_MATCH - 1; 12588a5f34efSLei Wen 12598a5f34efSLei Wen s->match_start = cur_match; 12608a5f34efSLei Wen return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; 12618a5f34efSLei Wen } 12628a5f34efSLei Wen 12638a5f34efSLei Wen #endif /* FASTEST */ 12648a5f34efSLei Wen 12658a5f34efSLei Wen #ifdef DEBUG 12668a5f34efSLei Wen /* =========================================================================== 12678a5f34efSLei Wen * Check that the match at match_start is indeed a match. 12688a5f34efSLei Wen */ 12698a5f34efSLei Wen local void check_match(s, start, match, length) 12708a5f34efSLei Wen deflate_state *s; 12718a5f34efSLei Wen IPos start, match; 12728a5f34efSLei Wen int length; 12738a5f34efSLei Wen { 12748a5f34efSLei Wen /* check that the match is indeed a match */ 12758a5f34efSLei Wen if (zmemcmp(s->window + match, 12768a5f34efSLei Wen s->window + start, length) != EQUAL) { 12778a5f34efSLei Wen fprintf(stderr, " start %u, match %u, length %d\n", 12788a5f34efSLei Wen start, match, length); 12798a5f34efSLei Wen do { 12808a5f34efSLei Wen fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); 12818a5f34efSLei Wen } while (--length != 0); 12828a5f34efSLei Wen z_error("invalid match"); 12838a5f34efSLei Wen } 12848a5f34efSLei Wen if (z_verbose > 1) { 12858a5f34efSLei Wen fprintf(stderr,"\\[%d,%d]", start-match, length); 12868a5f34efSLei Wen do { putc(s->window[start++], stderr); } while (--length != 0); 12878a5f34efSLei Wen } 12888a5f34efSLei Wen } 12898a5f34efSLei Wen #else 12908a5f34efSLei Wen # define check_match(s, start, match, length) 12918a5f34efSLei Wen #endif /* DEBUG */ 12928a5f34efSLei Wen 12938a5f34efSLei Wen /* =========================================================================== 12948a5f34efSLei Wen * Fill the window when the lookahead becomes insufficient. 12958a5f34efSLei Wen * Updates strstart and lookahead. 12968a5f34efSLei Wen * 12978a5f34efSLei Wen * IN assertion: lookahead < MIN_LOOKAHEAD 12988a5f34efSLei Wen * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD 12998a5f34efSLei Wen * At least one byte has been read, or avail_in == 0; reads are 13008a5f34efSLei Wen * performed for at least two bytes (required for the zip translate_eol 13018a5f34efSLei Wen * option -- not supported here). 13028a5f34efSLei Wen */ 13038a5f34efSLei Wen local void fill_window(s) 13048a5f34efSLei Wen deflate_state *s; 13058a5f34efSLei Wen { 13068a5f34efSLei Wen register unsigned n, m; 13078a5f34efSLei Wen register Posf *p; 13088a5f34efSLei Wen unsigned more; /* Amount of free space at the end of the window. */ 13098a5f34efSLei Wen uInt wsize = s->w_size; 13108a5f34efSLei Wen 13118a5f34efSLei Wen do { 13128a5f34efSLei Wen more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); 13138a5f34efSLei Wen 13148a5f34efSLei Wen /* Deal with !@#$% 64K limit: */ 13158a5f34efSLei Wen if (sizeof(int) <= 2) { 13168a5f34efSLei Wen if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 13178a5f34efSLei Wen more = wsize; 13188a5f34efSLei Wen 13198a5f34efSLei Wen } else if (more == (unsigned)(-1)) { 13208a5f34efSLei Wen /* Very unlikely, but possible on 16 bit machine if 13218a5f34efSLei Wen * strstart == 0 && lookahead == 1 (input done a byte at time) 13228a5f34efSLei Wen */ 13238a5f34efSLei Wen more--; 13248a5f34efSLei Wen } 13258a5f34efSLei Wen } 13268a5f34efSLei Wen 13278a5f34efSLei Wen /* If the window is almost full and there is insufficient lookahead, 13288a5f34efSLei Wen * move the upper half to the lower one to make room in the upper half. 13298a5f34efSLei Wen */ 13308a5f34efSLei Wen if (s->strstart >= wsize+MAX_DIST(s)) { 13318a5f34efSLei Wen 13328a5f34efSLei Wen zmemcpy(s->window, s->window+wsize, (unsigned)wsize); 13338a5f34efSLei Wen s->match_start -= wsize; 13348a5f34efSLei Wen s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ 13358a5f34efSLei Wen s->block_start -= (long) wsize; 13368a5f34efSLei Wen 13378a5f34efSLei Wen /* Slide the hash table (could be avoided with 32 bit values 13388a5f34efSLei Wen at the expense of memory usage). We slide even when level == 0 13398a5f34efSLei Wen to keep the hash table consistent if we switch back to level > 0 13408a5f34efSLei Wen later. (Using level 0 permanently is not an optimal usage of 13418a5f34efSLei Wen zlib, so we don't care about this pathological case.) 13428a5f34efSLei Wen */ 13438a5f34efSLei Wen n = s->hash_size; 13448a5f34efSLei Wen p = &s->head[n]; 13458a5f34efSLei Wen do { 13468a5f34efSLei Wen m = *--p; 13478a5f34efSLei Wen *p = (Pos)(m >= wsize ? m-wsize : NIL); 13488a5f34efSLei Wen } while (--n); 13498a5f34efSLei Wen 13508a5f34efSLei Wen n = wsize; 13518a5f34efSLei Wen #ifndef FASTEST 13528a5f34efSLei Wen p = &s->prev[n]; 13538a5f34efSLei Wen do { 13548a5f34efSLei Wen m = *--p; 13558a5f34efSLei Wen *p = (Pos)(m >= wsize ? m-wsize : NIL); 13568a5f34efSLei Wen /* If n is not on any hash chain, prev[n] is garbage but 13578a5f34efSLei Wen * its value will never be used. 13588a5f34efSLei Wen */ 13598a5f34efSLei Wen } while (--n); 13608a5f34efSLei Wen #endif 13618a5f34efSLei Wen more += wsize; 13628a5f34efSLei Wen } 13638a5f34efSLei Wen if (s->strm->avail_in == 0) return; 13648a5f34efSLei Wen 13658a5f34efSLei Wen /* If there was no sliding: 13668a5f34efSLei Wen * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && 13678a5f34efSLei Wen * more == window_size - lookahead - strstart 13688a5f34efSLei Wen * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) 13698a5f34efSLei Wen * => more >= window_size - 2*WSIZE + 2 13708a5f34efSLei Wen * In the BIG_MEM or MMAP case (not yet supported), 13718a5f34efSLei Wen * window_size == input_size + MIN_LOOKAHEAD && 13728a5f34efSLei Wen * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. 13738a5f34efSLei Wen * Otherwise, window_size == 2*WSIZE so more >= 2. 13748a5f34efSLei Wen * If there was sliding, more >= WSIZE. So in all cases, more >= 2. 13758a5f34efSLei Wen */ 13768a5f34efSLei Wen Assert(more >= 2, "more < 2"); 13778a5f34efSLei Wen 13788a5f34efSLei Wen n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); 13798a5f34efSLei Wen s->lookahead += n; 13808a5f34efSLei Wen 13818a5f34efSLei Wen /* Initialize the hash value now that we have some input: */ 13828a5f34efSLei Wen if (s->lookahead >= MIN_MATCH) { 13838a5f34efSLei Wen s->ins_h = s->window[s->strstart]; 13848a5f34efSLei Wen UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 13858a5f34efSLei Wen #if MIN_MATCH != 3 13868a5f34efSLei Wen Call UPDATE_HASH() MIN_MATCH-3 more times 13878a5f34efSLei Wen #endif 13888a5f34efSLei Wen } 13898a5f34efSLei Wen /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, 13908a5f34efSLei Wen * but this is not important since only literal bytes will be emitted. 13918a5f34efSLei Wen */ 13928a5f34efSLei Wen 13938a5f34efSLei Wen } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); 13948a5f34efSLei Wen 13958a5f34efSLei Wen /* If the WIN_INIT bytes after the end of the current data have never been 13968a5f34efSLei Wen * written, then zero those bytes in order to avoid memory check reports of 13978a5f34efSLei Wen * the use of uninitialized (or uninitialised as Julian writes) bytes by 13988a5f34efSLei Wen * the longest match routines. Update the high water mark for the next 13998a5f34efSLei Wen * time through here. WIN_INIT is set to MAX_MATCH since the longest match 14008a5f34efSLei Wen * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. 14018a5f34efSLei Wen */ 14028a5f34efSLei Wen if (s->high_water < s->window_size) { 14038a5f34efSLei Wen ulg curr = s->strstart + (ulg)(s->lookahead); 14048a5f34efSLei Wen ulg init; 14058a5f34efSLei Wen 14068a5f34efSLei Wen if (s->high_water < curr) { 14078a5f34efSLei Wen /* Previous high water mark below current data -- zero WIN_INIT 14088a5f34efSLei Wen * bytes or up to end of window, whichever is less. 14098a5f34efSLei Wen */ 14108a5f34efSLei Wen init = s->window_size - curr; 14118a5f34efSLei Wen if (init > WIN_INIT) 14128a5f34efSLei Wen init = WIN_INIT; 14138a5f34efSLei Wen zmemzero(s->window + curr, (unsigned)init); 14148a5f34efSLei Wen s->high_water = curr + init; 14158a5f34efSLei Wen } 14168a5f34efSLei Wen else if (s->high_water < (ulg)curr + WIN_INIT) { 14178a5f34efSLei Wen /* High water mark at or above current data, but below current data 14188a5f34efSLei Wen * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up 14198a5f34efSLei Wen * to end of window, whichever is less. 14208a5f34efSLei Wen */ 14218a5f34efSLei Wen init = (ulg)curr + WIN_INIT - s->high_water; 14228a5f34efSLei Wen if (init > s->window_size - s->high_water) 14238a5f34efSLei Wen init = s->window_size - s->high_water; 14248a5f34efSLei Wen zmemzero(s->window + s->high_water, (unsigned)init); 14258a5f34efSLei Wen s->high_water += init; 14268a5f34efSLei Wen } 14278a5f34efSLei Wen } 14288a5f34efSLei Wen } 14298a5f34efSLei Wen 14308a5f34efSLei Wen /* =========================================================================== 14318a5f34efSLei Wen * Flush the current block, with given end-of-file flag. 14328a5f34efSLei Wen * IN assertion: strstart is set to the end of the current match. 14338a5f34efSLei Wen */ 14348a5f34efSLei Wen #define FLUSH_BLOCK_ONLY(s, last) { \ 14358a5f34efSLei Wen _tr_flush_block(s, (s->block_start >= 0L ? \ 14368a5f34efSLei Wen (charf *)&s->window[(unsigned)s->block_start] : \ 14378a5f34efSLei Wen (charf *)Z_NULL), \ 14388a5f34efSLei Wen (ulg)((long)s->strstart - s->block_start), \ 14398a5f34efSLei Wen (last)); \ 14408a5f34efSLei Wen s->block_start = s->strstart; \ 14418a5f34efSLei Wen flush_pending(s->strm); \ 14428a5f34efSLei Wen Tracev((stderr,"[FLUSH]")); \ 14438a5f34efSLei Wen } 14448a5f34efSLei Wen 14458a5f34efSLei Wen /* Same but force premature exit if necessary. */ 14468a5f34efSLei Wen #define FLUSH_BLOCK(s, last) { \ 14478a5f34efSLei Wen FLUSH_BLOCK_ONLY(s, last); \ 14488a5f34efSLei Wen if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ 14498a5f34efSLei Wen } 14508a5f34efSLei Wen 14518a5f34efSLei Wen /* =========================================================================== 14528a5f34efSLei Wen * Copy without compression as much as possible from the input stream, return 14538a5f34efSLei Wen * the current block state. 14548a5f34efSLei Wen * This function does not insert new strings in the dictionary since 14558a5f34efSLei Wen * uncompressible data is probably not useful. This function is used 14568a5f34efSLei Wen * only for the level=0 compression option. 14578a5f34efSLei Wen * NOTE: this function should be optimized to avoid extra copying from 14588a5f34efSLei Wen * window to pending_buf. 14598a5f34efSLei Wen */ 14608a5f34efSLei Wen local block_state deflate_stored(s, flush) 14618a5f34efSLei Wen deflate_state *s; 14628a5f34efSLei Wen int flush; 14638a5f34efSLei Wen { 14648a5f34efSLei Wen /* Stored blocks are limited to 0xffff bytes, pending_buf is limited 14658a5f34efSLei Wen * to pending_buf_size, and each stored block has a 5 byte header: 14668a5f34efSLei Wen */ 14678a5f34efSLei Wen ulg max_block_size = 0xffff; 14688a5f34efSLei Wen ulg max_start; 14698a5f34efSLei Wen 14708a5f34efSLei Wen if (max_block_size > s->pending_buf_size - 5) { 14718a5f34efSLei Wen max_block_size = s->pending_buf_size - 5; 14728a5f34efSLei Wen } 14738a5f34efSLei Wen 14748a5f34efSLei Wen /* Copy as much as possible from input to output: */ 14758a5f34efSLei Wen for (;;) { 14768a5f34efSLei Wen /* Fill the window as much as possible: */ 14778a5f34efSLei Wen if (s->lookahead <= 1) { 14788a5f34efSLei Wen 14798a5f34efSLei Wen Assert(s->strstart < s->w_size+MAX_DIST(s) || 14808a5f34efSLei Wen s->block_start >= (long)s->w_size, "slide too late"); 14818a5f34efSLei Wen 14828a5f34efSLei Wen fill_window(s); 14838a5f34efSLei Wen if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more; 14848a5f34efSLei Wen 14858a5f34efSLei Wen if (s->lookahead == 0) break; /* flush the current block */ 14868a5f34efSLei Wen } 14878a5f34efSLei Wen Assert(s->block_start >= 0L, "block gone"); 14888a5f34efSLei Wen 14898a5f34efSLei Wen s->strstart += s->lookahead; 14908a5f34efSLei Wen s->lookahead = 0; 14918a5f34efSLei Wen 14928a5f34efSLei Wen /* Emit a stored block if pending_buf will be full: */ 14938a5f34efSLei Wen max_start = s->block_start + max_block_size; 14948a5f34efSLei Wen if (s->strstart == 0 || (ulg)s->strstart >= max_start) { 14958a5f34efSLei Wen /* strstart == 0 is possible when wraparound on 16-bit machine */ 14968a5f34efSLei Wen s->lookahead = (uInt)(s->strstart - max_start); 14978a5f34efSLei Wen s->strstart = (uInt)max_start; 14988a5f34efSLei Wen FLUSH_BLOCK(s, 0); 14998a5f34efSLei Wen } 15008a5f34efSLei Wen /* Flush if we may have to slide, otherwise block_start may become 15018a5f34efSLei Wen * negative and the data will be gone: 15028a5f34efSLei Wen */ 15038a5f34efSLei Wen if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { 15048a5f34efSLei Wen FLUSH_BLOCK(s, 0); 15058a5f34efSLei Wen } 15068a5f34efSLei Wen } 15078a5f34efSLei Wen FLUSH_BLOCK(s, flush == Z_FINISH); 15088a5f34efSLei Wen return flush == Z_FINISH ? finish_done : block_done; 15098a5f34efSLei Wen } 15108a5f34efSLei Wen 15118a5f34efSLei Wen /* =========================================================================== 15128a5f34efSLei Wen * Compress as much as possible from the input stream, return the current 15138a5f34efSLei Wen * block state. 15148a5f34efSLei Wen * This function does not perform lazy evaluation of matches and inserts 15158a5f34efSLei Wen * new strings in the dictionary only for unmatched strings or for short 15168a5f34efSLei Wen * matches. It is used only for the fast compression options. 15178a5f34efSLei Wen */ 15188a5f34efSLei Wen local block_state deflate_fast(s, flush) 15198a5f34efSLei Wen deflate_state *s; 15208a5f34efSLei Wen int flush; 15218a5f34efSLei Wen { 15228a5f34efSLei Wen IPos hash_head; /* head of the hash chain */ 15238a5f34efSLei Wen int bflush; /* set if current block must be flushed */ 15248a5f34efSLei Wen 15258a5f34efSLei Wen for (;;) { 15268a5f34efSLei Wen /* Make sure that we always have enough lookahead, except 15278a5f34efSLei Wen * at the end of the input file. We need MAX_MATCH bytes 15288a5f34efSLei Wen * for the next match, plus MIN_MATCH bytes to insert the 15298a5f34efSLei Wen * string following the next match. 15308a5f34efSLei Wen */ 15318a5f34efSLei Wen if (s->lookahead < MIN_LOOKAHEAD) { 15328a5f34efSLei Wen fill_window(s); 15338a5f34efSLei Wen if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 15348a5f34efSLei Wen return need_more; 15358a5f34efSLei Wen } 15368a5f34efSLei Wen if (s->lookahead == 0) break; /* flush the current block */ 15378a5f34efSLei Wen } 15388a5f34efSLei Wen 15398a5f34efSLei Wen /* Insert the string window[strstart .. strstart+2] in the 15408a5f34efSLei Wen * dictionary, and set hash_head to the head of the hash chain: 15418a5f34efSLei Wen */ 15428a5f34efSLei Wen hash_head = NIL; 15438a5f34efSLei Wen if (s->lookahead >= MIN_MATCH) { 15448a5f34efSLei Wen INSERT_STRING(s, s->strstart, hash_head); 15458a5f34efSLei Wen } 15468a5f34efSLei Wen 15478a5f34efSLei Wen /* Find the longest match, discarding those <= prev_length. 15488a5f34efSLei Wen * At this point we have always match_length < MIN_MATCH 15498a5f34efSLei Wen */ 15508a5f34efSLei Wen if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { 15518a5f34efSLei Wen /* To simplify the code, we prevent matches with the string 15528a5f34efSLei Wen * of window index 0 (in particular we have to avoid a match 15538a5f34efSLei Wen * of the string with itself at the start of the input file). 15548a5f34efSLei Wen */ 15558a5f34efSLei Wen s->match_length = longest_match (s, hash_head); 15568a5f34efSLei Wen /* longest_match() sets match_start */ 15578a5f34efSLei Wen } 15588a5f34efSLei Wen if (s->match_length >= MIN_MATCH) { 15598a5f34efSLei Wen check_match(s, s->strstart, s->match_start, s->match_length); 15608a5f34efSLei Wen 15618a5f34efSLei Wen _tr_tally_dist(s, s->strstart - s->match_start, 15628a5f34efSLei Wen s->match_length - MIN_MATCH, bflush); 15638a5f34efSLei Wen 15648a5f34efSLei Wen s->lookahead -= s->match_length; 15658a5f34efSLei Wen 15668a5f34efSLei Wen /* Insert new strings in the hash table only if the match length 15678a5f34efSLei Wen * is not too large. This saves time but degrades compression. 15688a5f34efSLei Wen */ 15698a5f34efSLei Wen #ifndef FASTEST 15708a5f34efSLei Wen if (s->match_length <= s->max_insert_length && 15718a5f34efSLei Wen s->lookahead >= MIN_MATCH) { 15728a5f34efSLei Wen s->match_length--; /* string at strstart already in table */ 15738a5f34efSLei Wen do { 15748a5f34efSLei Wen s->strstart++; 15758a5f34efSLei Wen INSERT_STRING(s, s->strstart, hash_head); 15768a5f34efSLei Wen /* strstart never exceeds WSIZE-MAX_MATCH, so there are 15778a5f34efSLei Wen * always MIN_MATCH bytes ahead. 15788a5f34efSLei Wen */ 15798a5f34efSLei Wen } while (--s->match_length != 0); 15808a5f34efSLei Wen s->strstart++; 15818a5f34efSLei Wen } else 15828a5f34efSLei Wen #endif 15838a5f34efSLei Wen { 15848a5f34efSLei Wen s->strstart += s->match_length; 15858a5f34efSLei Wen s->match_length = 0; 15868a5f34efSLei Wen s->ins_h = s->window[s->strstart]; 15878a5f34efSLei Wen UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 15888a5f34efSLei Wen #if MIN_MATCH != 3 15898a5f34efSLei Wen Call UPDATE_HASH() MIN_MATCH-3 more times 15908a5f34efSLei Wen #endif 15918a5f34efSLei Wen /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not 15928a5f34efSLei Wen * matter since it will be recomputed at next deflate call. 15938a5f34efSLei Wen */ 15948a5f34efSLei Wen } 15958a5f34efSLei Wen } else { 15968a5f34efSLei Wen /* No match, output a literal byte */ 15978a5f34efSLei Wen Tracevv((stderr,"%c", s->window[s->strstart])); 15988a5f34efSLei Wen _tr_tally_lit (s, s->window[s->strstart], bflush); 15998a5f34efSLei Wen s->lookahead--; 16008a5f34efSLei Wen s->strstart++; 16018a5f34efSLei Wen } 16028a5f34efSLei Wen if (bflush) FLUSH_BLOCK(s, 0); 16038a5f34efSLei Wen } 16048a5f34efSLei Wen FLUSH_BLOCK(s, flush == Z_FINISH); 16058a5f34efSLei Wen return flush == Z_FINISH ? finish_done : block_done; 16068a5f34efSLei Wen } 16078a5f34efSLei Wen 16088a5f34efSLei Wen #ifndef FASTEST 16098a5f34efSLei Wen /* =========================================================================== 16108a5f34efSLei Wen * Same as above, but achieves better compression. We use a lazy 16118a5f34efSLei Wen * evaluation for matches: a match is finally adopted only if there is 16128a5f34efSLei Wen * no better match at the next window position. 16138a5f34efSLei Wen */ 16148a5f34efSLei Wen local block_state deflate_slow(s, flush) 16158a5f34efSLei Wen deflate_state *s; 16168a5f34efSLei Wen int flush; 16178a5f34efSLei Wen { 16188a5f34efSLei Wen IPos hash_head; /* head of hash chain */ 16198a5f34efSLei Wen int bflush; /* set if current block must be flushed */ 16208a5f34efSLei Wen 16218a5f34efSLei Wen /* Process the input block. */ 16228a5f34efSLei Wen for (;;) { 16238a5f34efSLei Wen /* Make sure that we always have enough lookahead, except 16248a5f34efSLei Wen * at the end of the input file. We need MAX_MATCH bytes 16258a5f34efSLei Wen * for the next match, plus MIN_MATCH bytes to insert the 16268a5f34efSLei Wen * string following the next match. 16278a5f34efSLei Wen */ 16288a5f34efSLei Wen if (s->lookahead < MIN_LOOKAHEAD) { 16298a5f34efSLei Wen fill_window(s); 16308a5f34efSLei Wen if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 16318a5f34efSLei Wen return need_more; 16328a5f34efSLei Wen } 16338a5f34efSLei Wen if (s->lookahead == 0) break; /* flush the current block */ 16348a5f34efSLei Wen } 16358a5f34efSLei Wen 16368a5f34efSLei Wen /* Insert the string window[strstart .. strstart+2] in the 16378a5f34efSLei Wen * dictionary, and set hash_head to the head of the hash chain: 16388a5f34efSLei Wen */ 16398a5f34efSLei Wen hash_head = NIL; 16408a5f34efSLei Wen if (s->lookahead >= MIN_MATCH) { 16418a5f34efSLei Wen INSERT_STRING(s, s->strstart, hash_head); 16428a5f34efSLei Wen } 16438a5f34efSLei Wen 16448a5f34efSLei Wen /* Find the longest match, discarding those <= prev_length. 16458a5f34efSLei Wen */ 16468a5f34efSLei Wen s->prev_length = s->match_length, s->prev_match = s->match_start; 16478a5f34efSLei Wen s->match_length = MIN_MATCH-1; 16488a5f34efSLei Wen 16498a5f34efSLei Wen if (hash_head != NIL && s->prev_length < s->max_lazy_match && 16508a5f34efSLei Wen s->strstart - hash_head <= MAX_DIST(s)) { 16518a5f34efSLei Wen /* To simplify the code, we prevent matches with the string 16528a5f34efSLei Wen * of window index 0 (in particular we have to avoid a match 16538a5f34efSLei Wen * of the string with itself at the start of the input file). 16548a5f34efSLei Wen */ 16558a5f34efSLei Wen s->match_length = longest_match (s, hash_head); 16568a5f34efSLei Wen /* longest_match() sets match_start */ 16578a5f34efSLei Wen 16588a5f34efSLei Wen if (s->match_length <= 5 && (s->strategy == Z_FILTERED 16598a5f34efSLei Wen #if TOO_FAR <= 32767 16608a5f34efSLei Wen || (s->match_length == MIN_MATCH && 16618a5f34efSLei Wen s->strstart - s->match_start > TOO_FAR) 16628a5f34efSLei Wen #endif 16638a5f34efSLei Wen )) { 16648a5f34efSLei Wen 16658a5f34efSLei Wen /* If prev_match is also MIN_MATCH, match_start is garbage 16668a5f34efSLei Wen * but we will ignore the current match anyway. 16678a5f34efSLei Wen */ 16688a5f34efSLei Wen s->match_length = MIN_MATCH-1; 16698a5f34efSLei Wen } 16708a5f34efSLei Wen } 16718a5f34efSLei Wen /* If there was a match at the previous step and the current 16728a5f34efSLei Wen * match is not better, output the previous match: 16738a5f34efSLei Wen */ 16748a5f34efSLei Wen if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { 16758a5f34efSLei Wen uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 16768a5f34efSLei Wen /* Do not insert strings in hash table beyond this. */ 16778a5f34efSLei Wen 16788a5f34efSLei Wen check_match(s, s->strstart-1, s->prev_match, s->prev_length); 16798a5f34efSLei Wen 16808a5f34efSLei Wen _tr_tally_dist(s, s->strstart -1 - s->prev_match, 16818a5f34efSLei Wen s->prev_length - MIN_MATCH, bflush); 16828a5f34efSLei Wen 16838a5f34efSLei Wen /* Insert in hash table all strings up to the end of the match. 16848a5f34efSLei Wen * strstart-1 and strstart are already inserted. If there is not 16858a5f34efSLei Wen * enough lookahead, the last two strings are not inserted in 16868a5f34efSLei Wen * the hash table. 16878a5f34efSLei Wen */ 16888a5f34efSLei Wen s->lookahead -= s->prev_length-1; 16898a5f34efSLei Wen s->prev_length -= 2; 16908a5f34efSLei Wen do { 16918a5f34efSLei Wen if (++s->strstart <= max_insert) { 16928a5f34efSLei Wen INSERT_STRING(s, s->strstart, hash_head); 16938a5f34efSLei Wen } 16948a5f34efSLei Wen } while (--s->prev_length != 0); 16958a5f34efSLei Wen s->match_available = 0; 16968a5f34efSLei Wen s->match_length = MIN_MATCH-1; 16978a5f34efSLei Wen s->strstart++; 16988a5f34efSLei Wen 16998a5f34efSLei Wen if (bflush) FLUSH_BLOCK(s, 0); 17008a5f34efSLei Wen 17018a5f34efSLei Wen } else if (s->match_available) { 17028a5f34efSLei Wen /* If there was no match at the previous position, output a 17038a5f34efSLei Wen * single literal. If there was a match but the current match 17048a5f34efSLei Wen * is longer, truncate the previous match to a single literal. 17058a5f34efSLei Wen */ 17068a5f34efSLei Wen Tracevv((stderr,"%c", s->window[s->strstart-1])); 17078a5f34efSLei Wen _tr_tally_lit(s, s->window[s->strstart-1], bflush); 17088a5f34efSLei Wen if (bflush) { 17098a5f34efSLei Wen FLUSH_BLOCK_ONLY(s, 0); 17108a5f34efSLei Wen } 17118a5f34efSLei Wen s->strstart++; 17128a5f34efSLei Wen s->lookahead--; 17138a5f34efSLei Wen if (s->strm->avail_out == 0) return need_more; 17148a5f34efSLei Wen } else { 17158a5f34efSLei Wen /* There is no previous match to compare with, wait for 17168a5f34efSLei Wen * the next step to decide. 17178a5f34efSLei Wen */ 17188a5f34efSLei Wen s->match_available = 1; 17198a5f34efSLei Wen s->strstart++; 17208a5f34efSLei Wen s->lookahead--; 17218a5f34efSLei Wen } 17228a5f34efSLei Wen } 17238a5f34efSLei Wen Assert (flush != Z_NO_FLUSH, "no flush?"); 17248a5f34efSLei Wen if (s->match_available) { 17258a5f34efSLei Wen Tracevv((stderr,"%c", s->window[s->strstart-1])); 17268a5f34efSLei Wen _tr_tally_lit(s, s->window[s->strstart-1], bflush); 17278a5f34efSLei Wen s->match_available = 0; 17288a5f34efSLei Wen } 17298a5f34efSLei Wen FLUSH_BLOCK(s, flush == Z_FINISH); 17308a5f34efSLei Wen return flush == Z_FINISH ? finish_done : block_done; 17318a5f34efSLei Wen } 17328a5f34efSLei Wen #endif /* FASTEST */ 17338a5f34efSLei Wen 17348a5f34efSLei Wen /* =========================================================================== 17358a5f34efSLei Wen * For Z_RLE, simply look for runs of bytes, generate matches only of distance 17368a5f34efSLei Wen * one. Do not maintain a hash table. (It will be regenerated if this run of 17378a5f34efSLei Wen * deflate switches away from Z_RLE.) 17388a5f34efSLei Wen */ 17398a5f34efSLei Wen local block_state deflate_rle(s, flush) 17408a5f34efSLei Wen deflate_state *s; 17418a5f34efSLei Wen int flush; 17428a5f34efSLei Wen { 17438a5f34efSLei Wen int bflush; /* set if current block must be flushed */ 17448a5f34efSLei Wen uInt prev; /* byte at distance one to match */ 17458a5f34efSLei Wen Bytef *scan, *strend; /* scan goes up to strend for length of run */ 17468a5f34efSLei Wen 17478a5f34efSLei Wen for (;;) { 17488a5f34efSLei Wen /* Make sure that we always have enough lookahead, except 17498a5f34efSLei Wen * at the end of the input file. We need MAX_MATCH bytes 17508a5f34efSLei Wen * for the longest encodable run. 17518a5f34efSLei Wen */ 17528a5f34efSLei Wen if (s->lookahead < MAX_MATCH) { 17538a5f34efSLei Wen fill_window(s); 17548a5f34efSLei Wen if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { 17558a5f34efSLei Wen return need_more; 17568a5f34efSLei Wen } 17578a5f34efSLei Wen if (s->lookahead == 0) break; /* flush the current block */ 17588a5f34efSLei Wen } 17598a5f34efSLei Wen 17608a5f34efSLei Wen /* See how many times the previous byte repeats */ 17618a5f34efSLei Wen s->match_length = 0; 17628a5f34efSLei Wen if (s->lookahead >= MIN_MATCH && s->strstart > 0) { 17638a5f34efSLei Wen scan = s->window + s->strstart - 1; 17648a5f34efSLei Wen prev = *scan; 17658a5f34efSLei Wen if (prev == *++scan && prev == *++scan && prev == *++scan) { 17668a5f34efSLei Wen strend = s->window + s->strstart + MAX_MATCH; 17678a5f34efSLei Wen do { 17688a5f34efSLei Wen } while (prev == *++scan && prev == *++scan && 17698a5f34efSLei Wen prev == *++scan && prev == *++scan && 17708a5f34efSLei Wen prev == *++scan && prev == *++scan && 17718a5f34efSLei Wen prev == *++scan && prev == *++scan && 17728a5f34efSLei Wen scan < strend); 17738a5f34efSLei Wen s->match_length = MAX_MATCH - (int)(strend - scan); 17748a5f34efSLei Wen if (s->match_length > s->lookahead) 17758a5f34efSLei Wen s->match_length = s->lookahead; 17768a5f34efSLei Wen } 17778a5f34efSLei Wen } 17788a5f34efSLei Wen 17798a5f34efSLei Wen /* Emit match if have run of MIN_MATCH or longer, else emit literal */ 17808a5f34efSLei Wen if (s->match_length >= MIN_MATCH) { 17818a5f34efSLei Wen check_match(s, s->strstart, s->strstart - 1, s->match_length); 17828a5f34efSLei Wen 17838a5f34efSLei Wen _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); 17848a5f34efSLei Wen 17858a5f34efSLei Wen s->lookahead -= s->match_length; 17868a5f34efSLei Wen s->strstart += s->match_length; 17878a5f34efSLei Wen s->match_length = 0; 17888a5f34efSLei Wen } else { 17898a5f34efSLei Wen /* No match, output a literal byte */ 17908a5f34efSLei Wen Tracevv((stderr,"%c", s->window[s->strstart])); 17918a5f34efSLei Wen _tr_tally_lit (s, s->window[s->strstart], bflush); 17928a5f34efSLei Wen s->lookahead--; 17938a5f34efSLei Wen s->strstart++; 17948a5f34efSLei Wen } 17958a5f34efSLei Wen if (bflush) FLUSH_BLOCK(s, 0); 17968a5f34efSLei Wen } 17978a5f34efSLei Wen FLUSH_BLOCK(s, flush == Z_FINISH); 17988a5f34efSLei Wen return flush == Z_FINISH ? finish_done : block_done; 17998a5f34efSLei Wen } 18008a5f34efSLei Wen 18018a5f34efSLei Wen /* =========================================================================== 18028a5f34efSLei Wen * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. 18038a5f34efSLei Wen * (It will be regenerated if this run of deflate switches away from Huffman.) 18048a5f34efSLei Wen */ 18058a5f34efSLei Wen local block_state deflate_huff(s, flush) 18068a5f34efSLei Wen deflate_state *s; 18078a5f34efSLei Wen int flush; 18088a5f34efSLei Wen { 18098a5f34efSLei Wen int bflush; /* set if current block must be flushed */ 18108a5f34efSLei Wen 18118a5f34efSLei Wen for (;;) { 18128a5f34efSLei Wen /* Make sure that we have a literal to write. */ 18138a5f34efSLei Wen if (s->lookahead == 0) { 18148a5f34efSLei Wen fill_window(s); 18158a5f34efSLei Wen if (s->lookahead == 0) { 18168a5f34efSLei Wen if (flush == Z_NO_FLUSH) 18178a5f34efSLei Wen return need_more; 18188a5f34efSLei Wen break; /* flush the current block */ 18198a5f34efSLei Wen } 18208a5f34efSLei Wen } 18218a5f34efSLei Wen 18228a5f34efSLei Wen /* Output a literal byte */ 18238a5f34efSLei Wen s->match_length = 0; 18248a5f34efSLei Wen Tracevv((stderr,"%c", s->window[s->strstart])); 18258a5f34efSLei Wen _tr_tally_lit (s, s->window[s->strstart], bflush); 18268a5f34efSLei Wen s->lookahead--; 18278a5f34efSLei Wen s->strstart++; 18288a5f34efSLei Wen if (bflush) FLUSH_BLOCK(s, 0); 18298a5f34efSLei Wen } 18308a5f34efSLei Wen FLUSH_BLOCK(s, flush == Z_FINISH); 18318a5f34efSLei Wen return flush == Z_FINISH ? finish_done : block_done; 18328a5f34efSLei Wen } 1833