16141cf33Sderaadt /* deflate.c -- compress data using the deflation algorithm 2f0633749Stb * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler 36141cf33Sderaadt * For conditions of distribution and use, see copyright notice in zlib.h 46141cf33Sderaadt */ 56141cf33Sderaadt 66141cf33Sderaadt /* 76141cf33Sderaadt * ALGORITHM 86141cf33Sderaadt * 96141cf33Sderaadt * The "deflation" process depends on being able to identify portions 106141cf33Sderaadt * of the input text which are identical to earlier input (within a 116141cf33Sderaadt * sliding window trailing behind the input currently being processed). 126141cf33Sderaadt * 136141cf33Sderaadt * The most straightforward technique turns out to be the fastest for 146141cf33Sderaadt * most input files: try all possible matches and select the longest. 156141cf33Sderaadt * The key feature of this algorithm is that insertions into the string 166141cf33Sderaadt * dictionary are very simple and thus fast, and deletions are avoided 176141cf33Sderaadt * completely. Insertions are performed at each input character, whereas 186141cf33Sderaadt * string matches are performed only when the previous match ends. So it 196141cf33Sderaadt * is preferable to spend more time in matches to allow very fast string 206141cf33Sderaadt * insertions and avoid deletions. The matching algorithm for small 216141cf33Sderaadt * strings is inspired from that of Rabin & Karp. A brute force approach 226141cf33Sderaadt * is used to find longer strings when a small match has been found. 236141cf33Sderaadt * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze 246141cf33Sderaadt * (by Leonid Broukhis). 256141cf33Sderaadt * A previous version of this file used a more sophisticated algorithm 266141cf33Sderaadt * (by Fiala and Greene) which is guaranteed to run in linear amortized 276141cf33Sderaadt * time, but has a larger average cost, uses more memory and is patented. 286141cf33Sderaadt * However the F&G algorithm may be faster for some highly redundant 296141cf33Sderaadt * files if the parameter max_chain_length (described below) is too large. 306141cf33Sderaadt * 316141cf33Sderaadt * ACKNOWLEDGEMENTS 326141cf33Sderaadt * 336141cf33Sderaadt * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and 346141cf33Sderaadt * I found it in 'freeze' written by Leonid Broukhis. 356141cf33Sderaadt * Thanks to many people for bug reports and testing. 366141cf33Sderaadt * 376141cf33Sderaadt * REFERENCES 386141cf33Sderaadt * 396141cf33Sderaadt * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". 4036f395ceStb * Available in http://tools.ietf.org/html/rfc1951 416141cf33Sderaadt * 426141cf33Sderaadt * A description of the Rabin and Karp algorithm is given in the book 436141cf33Sderaadt * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. 446141cf33Sderaadt * 456141cf33Sderaadt * Fiala,E.R., and Greene,D.H. 466141cf33Sderaadt * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 476141cf33Sderaadt * 486141cf33Sderaadt */ 496141cf33Sderaadt 506141cf33Sderaadt #include "deflate.h" 516141cf33Sderaadt 526141cf33Sderaadt /* 536141cf33Sderaadt If you use the zlib library in a product, an acknowledgment is welcome 546141cf33Sderaadt in the documentation of your product. If for some reason you cannot 556141cf33Sderaadt include such an acknowledgment, I would appreciate that you keep this 566141cf33Sderaadt copyright string in the executable of your product. 576141cf33Sderaadt */ 586141cf33Sderaadt 596141cf33Sderaadt typedef enum { 606141cf33Sderaadt need_more, /* block not completed, need more input or more output */ 616141cf33Sderaadt block_done, /* block flush performed */ 626141cf33Sderaadt finish_started, /* finish started, need only more output at next deflate */ 636141cf33Sderaadt finish_done /* finish done, accept no more input or output */ 646141cf33Sderaadt } block_state; 656141cf33Sderaadt 66cfac609cStb typedef block_state (*compress_func)(deflate_state *s, int flush); 676141cf33Sderaadt /* Compression function. Returns the block state after the call. */ 686141cf33Sderaadt 69cfac609cStb local block_state deflate_stored(deflate_state *s, int flush); 70cfac609cStb local block_state deflate_fast(deflate_state *s, int flush); 716141cf33Sderaadt #ifndef FASTEST 72cfac609cStb local block_state deflate_slow(deflate_state *s, int flush); 736141cf33Sderaadt #endif 74cfac609cStb local block_state deflate_rle(deflate_state *s, int flush); 75cfac609cStb local block_state deflate_huff(deflate_state *s, int flush); 766141cf33Sderaadt 776141cf33Sderaadt /* =========================================================================== 786141cf33Sderaadt * Local data 796141cf33Sderaadt */ 806141cf33Sderaadt 816141cf33Sderaadt #define NIL 0 826141cf33Sderaadt /* Tail of hash chains */ 836141cf33Sderaadt 846141cf33Sderaadt #ifndef TOO_FAR 856141cf33Sderaadt # define TOO_FAR 4096 866141cf33Sderaadt #endif 876141cf33Sderaadt /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ 886141cf33Sderaadt 896141cf33Sderaadt /* Values for max_lazy_match, good_match and max_chain_length, depending on 906141cf33Sderaadt * the desired pack level (0..9). The values given below have been tuned to 916141cf33Sderaadt * exclude worst case performance for pathological files. Better values may be 926141cf33Sderaadt * found for specific files. 936141cf33Sderaadt */ 946141cf33Sderaadt typedef struct config_s { 956141cf33Sderaadt ush good_length; /* reduce lazy search above this match length */ 966141cf33Sderaadt ush max_lazy; /* do not perform lazy search above this match length */ 976141cf33Sderaadt ush nice_length; /* quit search above this match length */ 986141cf33Sderaadt ush max_chain; 996141cf33Sderaadt compress_func func; 1006141cf33Sderaadt } config; 1016141cf33Sderaadt 1026141cf33Sderaadt #ifdef FASTEST 1036141cf33Sderaadt local const config configuration_table[2] = { 1046141cf33Sderaadt /* good lazy nice chain */ 1056141cf33Sderaadt /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 1066141cf33Sderaadt /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ 1076141cf33Sderaadt #else 1086141cf33Sderaadt local const config configuration_table[10] = { 1096141cf33Sderaadt /* good lazy nice chain */ 1106141cf33Sderaadt /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 1116141cf33Sderaadt /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ 1126141cf33Sderaadt /* 2 */ {4, 5, 16, 8, deflate_fast}, 1136141cf33Sderaadt /* 3 */ {4, 6, 32, 32, deflate_fast}, 1146141cf33Sderaadt 1156141cf33Sderaadt /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ 1166141cf33Sderaadt /* 5 */ {8, 16, 32, 32, deflate_slow}, 1176141cf33Sderaadt /* 6 */ {8, 16, 128, 128, deflate_slow}, 1186141cf33Sderaadt /* 7 */ {8, 32, 128, 256, deflate_slow}, 1196141cf33Sderaadt /* 8 */ {32, 128, 258, 1024, deflate_slow}, 1206141cf33Sderaadt /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ 1216141cf33Sderaadt #endif 1226141cf33Sderaadt 1236141cf33Sderaadt /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 1246141cf33Sderaadt * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 1256141cf33Sderaadt * meaning. 1266141cf33Sderaadt */ 1276141cf33Sderaadt 12836f395ceStb /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ 12936f395ceStb #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0)) 1306141cf33Sderaadt 1316141cf33Sderaadt /* =========================================================================== 1326141cf33Sderaadt * Update a hash value with the given input byte 13336f395ceStb * IN assertion: all calls to UPDATE_HASH are made with consecutive input 13436f395ceStb * characters, so that a running hash key can be computed from the previous 13536f395ceStb * key instead of complete recalculation each time. 1366141cf33Sderaadt */ 1376141cf33Sderaadt #define UPDATE_HASH(s,h,c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask) 1386141cf33Sderaadt 1396141cf33Sderaadt 1406141cf33Sderaadt /* =========================================================================== 1416141cf33Sderaadt * Insert string str in the dictionary and set match_head to the previous head 1426141cf33Sderaadt * of the hash chain (the most recent string with same hash key). Return 1436141cf33Sderaadt * the previous length of the hash chain. 1446141cf33Sderaadt * If this file is compiled with -DFASTEST, the compression level is forced 1456141cf33Sderaadt * to 1, and no hash chains are maintained. 14636f395ceStb * IN assertion: all calls to INSERT_STRING are made with consecutive input 14736f395ceStb * characters and the first MIN_MATCH bytes of str are valid (except for 14836f395ceStb * the last MIN_MATCH-1 bytes of the input file). 1496141cf33Sderaadt */ 1506141cf33Sderaadt #ifdef FASTEST 1516141cf33Sderaadt #define INSERT_STRING(s, str, match_head) \ 1526141cf33Sderaadt (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 1536141cf33Sderaadt match_head = s->head[s->ins_h], \ 1546141cf33Sderaadt s->head[s->ins_h] = (Pos)(str)) 1556141cf33Sderaadt #else 1566141cf33Sderaadt #define INSERT_STRING(s, str, match_head) \ 1576141cf33Sderaadt (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 1586141cf33Sderaadt match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ 1596141cf33Sderaadt s->head[s->ins_h] = (Pos)(str)) 1606141cf33Sderaadt #endif 1616141cf33Sderaadt 1626141cf33Sderaadt /* =========================================================================== 1636141cf33Sderaadt * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 1646141cf33Sderaadt * prev[] will be initialized on the fly. 1656141cf33Sderaadt */ 1666141cf33Sderaadt #define CLEAR_HASH(s) \ 167c43db38aStb do { \ 1686141cf33Sderaadt s->head[s->hash_size - 1] = NIL; \ 169c43db38aStb zmemzero((Bytef *)s->head, \ 170c43db38aStb (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \ 171c43db38aStb } while (0) 1726141cf33Sderaadt 17336f395ceStb /* =========================================================================== 17436f395ceStb * Slide the hash table when sliding the window down (could be avoided with 32 17536f395ceStb * bit values at the expense of memory usage). We slide even when level == 0 to 17636f395ceStb * keep the hash table consistent if we switch back to level > 0 later. 17736f395ceStb */ 178f22a72c1Stb #if defined(__has_feature) 179f22a72c1Stb # if __has_feature(memory_sanitizer) 180f22a72c1Stb __attribute__((no_sanitize("memory"))) 181f22a72c1Stb # endif 182f22a72c1Stb #endif 183cfac609cStb local void slide_hash(deflate_state *s) { 18436f395ceStb unsigned n, m; 18536f395ceStb Posf *p; 18636f395ceStb uInt wsize = s->w_size; 18736f395ceStb 18836f395ceStb n = s->hash_size; 18936f395ceStb p = &s->head[n]; 19036f395ceStb do { 19136f395ceStb m = *--p; 19236f395ceStb *p = (Pos)(m >= wsize ? m - wsize : NIL); 19336f395ceStb } while (--n); 19436f395ceStb n = wsize; 19536f395ceStb #ifndef FASTEST 19636f395ceStb p = &s->prev[n]; 19736f395ceStb do { 19836f395ceStb m = *--p; 19936f395ceStb *p = (Pos)(m >= wsize ? m - wsize : NIL); 20036f395ceStb /* If n is not on any hash chain, prev[n] is garbage but 20136f395ceStb * its value will never be used. 20236f395ceStb */ 20336f395ceStb } while (--n); 20436f395ceStb #endif 20536f395ceStb } 20636f395ceStb 207cfac609cStb /* =========================================================================== 208cfac609cStb * Read a new buffer from the current input stream, update the adler32 209cfac609cStb * and total number of bytes read. All deflate() input goes through 210cfac609cStb * this function so some applications may wish to modify it to avoid 211cfac609cStb * allocating a large strm->next_in buffer and copying from it. 212cfac609cStb * (See also flush_pending()). 213cfac609cStb */ 214cfac609cStb local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size) { 215cfac609cStb unsigned len = strm->avail_in; 216cfac609cStb 217cfac609cStb if (len > size) len = size; 218cfac609cStb if (len == 0) return 0; 219cfac609cStb 220cfac609cStb strm->avail_in -= len; 221cfac609cStb 222cfac609cStb zmemcpy(buf, strm->next_in, len); 223cfac609cStb if (strm->state->wrap == 1) { 224cfac609cStb strm->adler = adler32(strm->adler, buf, len); 225cfac609cStb } 226cfac609cStb #ifdef GZIP 227cfac609cStb else if (strm->state->wrap == 2) { 228cfac609cStb strm->adler = crc32(strm->adler, buf, len); 229cfac609cStb } 230cfac609cStb #endif 231cfac609cStb strm->next_in += len; 232cfac609cStb strm->total_in += len; 233cfac609cStb 234cfac609cStb return len; 235cfac609cStb } 236cfac609cStb 237cfac609cStb /* =========================================================================== 238cfac609cStb * Fill the window when the lookahead becomes insufficient. 239cfac609cStb * Updates strstart and lookahead. 240cfac609cStb * 241cfac609cStb * IN assertion: lookahead < MIN_LOOKAHEAD 242cfac609cStb * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD 243cfac609cStb * At least one byte has been read, or avail_in == 0; reads are 244cfac609cStb * performed for at least two bytes (required for the zip translate_eol 245cfac609cStb * option -- not supported here). 246cfac609cStb */ 247cfac609cStb local void fill_window(deflate_state *s) { 248cfac609cStb unsigned n; 249cfac609cStb unsigned more; /* Amount of free space at the end of the window. */ 250cfac609cStb uInt wsize = s->w_size; 251cfac609cStb 252cfac609cStb Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); 253cfac609cStb 254cfac609cStb do { 255cfac609cStb more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); 256cfac609cStb 257cfac609cStb /* Deal with !@#$% 64K limit: */ 258cfac609cStb if (sizeof(int) <= 2) { 259cfac609cStb if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 260cfac609cStb more = wsize; 261cfac609cStb 262cfac609cStb } else if (more == (unsigned)(-1)) { 263cfac609cStb /* Very unlikely, but possible on 16 bit machine if 264cfac609cStb * strstart == 0 && lookahead == 1 (input done a byte at time) 265cfac609cStb */ 266cfac609cStb more--; 267cfac609cStb } 268cfac609cStb } 269cfac609cStb 270cfac609cStb /* If the window is almost full and there is insufficient lookahead, 271cfac609cStb * move the upper half to the lower one to make room in the upper half. 272cfac609cStb */ 273cfac609cStb if (s->strstart >= wsize + MAX_DIST(s)) { 274cfac609cStb 275cfac609cStb zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more); 276cfac609cStb s->match_start -= wsize; 277cfac609cStb s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ 278cfac609cStb s->block_start -= (long) wsize; 279cfac609cStb if (s->insert > s->strstart) 280cfac609cStb s->insert = s->strstart; 281cfac609cStb slide_hash(s); 282cfac609cStb more += wsize; 283cfac609cStb } 284cfac609cStb if (s->strm->avail_in == 0) break; 285cfac609cStb 286cfac609cStb /* If there was no sliding: 287cfac609cStb * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && 288cfac609cStb * more == window_size - lookahead - strstart 289cfac609cStb * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) 290cfac609cStb * => more >= window_size - 2*WSIZE + 2 291cfac609cStb * In the BIG_MEM or MMAP case (not yet supported), 292cfac609cStb * window_size == input_size + MIN_LOOKAHEAD && 293cfac609cStb * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. 294cfac609cStb * Otherwise, window_size == 2*WSIZE so more >= 2. 295cfac609cStb * If there was sliding, more >= WSIZE. So in all cases, more >= 2. 296cfac609cStb */ 297cfac609cStb Assert(more >= 2, "more < 2"); 298cfac609cStb 299cfac609cStb n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); 300cfac609cStb s->lookahead += n; 301cfac609cStb 302cfac609cStb /* Initialize the hash value now that we have some input: */ 303cfac609cStb if (s->lookahead + s->insert >= MIN_MATCH) { 304cfac609cStb uInt str = s->strstart - s->insert; 305cfac609cStb s->ins_h = s->window[str]; 306cfac609cStb UPDATE_HASH(s, s->ins_h, s->window[str + 1]); 307cfac609cStb #if MIN_MATCH != 3 308cfac609cStb Call UPDATE_HASH() MIN_MATCH-3 more times 309cfac609cStb #endif 310cfac609cStb while (s->insert) { 311cfac609cStb UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); 312cfac609cStb #ifndef FASTEST 313cfac609cStb s->prev[str & s->w_mask] = s->head[s->ins_h]; 314cfac609cStb #endif 315cfac609cStb s->head[s->ins_h] = (Pos)str; 316cfac609cStb str++; 317cfac609cStb s->insert--; 318cfac609cStb if (s->lookahead + s->insert < MIN_MATCH) 319cfac609cStb break; 320cfac609cStb } 321cfac609cStb } 322cfac609cStb /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, 323cfac609cStb * but this is not important since only literal bytes will be emitted. 324cfac609cStb */ 325cfac609cStb 326cfac609cStb } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); 327cfac609cStb 328cfac609cStb /* If the WIN_INIT bytes after the end of the current data have never been 329cfac609cStb * written, then zero those bytes in order to avoid memory check reports of 330cfac609cStb * the use of uninitialized (or uninitialised as Julian writes) bytes by 331cfac609cStb * the longest match routines. Update the high water mark for the next 332cfac609cStb * time through here. WIN_INIT is set to MAX_MATCH since the longest match 333cfac609cStb * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. 334cfac609cStb */ 335cfac609cStb if (s->high_water < s->window_size) { 336cfac609cStb ulg curr = s->strstart + (ulg)(s->lookahead); 337cfac609cStb ulg init; 338cfac609cStb 339cfac609cStb if (s->high_water < curr) { 340cfac609cStb /* Previous high water mark below current data -- zero WIN_INIT 341cfac609cStb * bytes or up to end of window, whichever is less. 342cfac609cStb */ 343cfac609cStb init = s->window_size - curr; 344cfac609cStb if (init > WIN_INIT) 345cfac609cStb init = WIN_INIT; 346cfac609cStb zmemzero(s->window + curr, (unsigned)init); 347cfac609cStb s->high_water = curr + init; 348cfac609cStb } 349cfac609cStb else if (s->high_water < (ulg)curr + WIN_INIT) { 350cfac609cStb /* High water mark at or above current data, but below current data 351cfac609cStb * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up 352cfac609cStb * to end of window, whichever is less. 353cfac609cStb */ 354cfac609cStb init = (ulg)curr + WIN_INIT - s->high_water; 355cfac609cStb if (init > s->window_size - s->high_water) 356cfac609cStb init = s->window_size - s->high_water; 357cfac609cStb zmemzero(s->window + s->high_water, (unsigned)init); 358cfac609cStb s->high_water += init; 359cfac609cStb } 360cfac609cStb } 361cfac609cStb 362cfac609cStb Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, 363cfac609cStb "not enough room for search"); 364cfac609cStb } 365cfac609cStb 3666141cf33Sderaadt /* ========================================================================= */ 367cfac609cStb int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, 368cfac609cStb int stream_size) { 3696141cf33Sderaadt return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 3706141cf33Sderaadt Z_DEFAULT_STRATEGY, version, stream_size); 3716141cf33Sderaadt /* To do: ignore strm->next_in if we use it as window */ 3726141cf33Sderaadt } 3736141cf33Sderaadt 3746141cf33Sderaadt /* ========================================================================= */ 375cfac609cStb int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, 376cfac609cStb int windowBits, int memLevel, int strategy, 377cfac609cStb const char *version, int stream_size) { 3786141cf33Sderaadt deflate_state *s; 3796141cf33Sderaadt int wrap = 1; 3806141cf33Sderaadt static const char my_version[] = ZLIB_VERSION; 3816141cf33Sderaadt 3826141cf33Sderaadt if (version == Z_NULL || version[0] != my_version[0] || 3836141cf33Sderaadt stream_size != sizeof(z_stream)) { 3846141cf33Sderaadt return Z_VERSION_ERROR; 3856141cf33Sderaadt } 3866141cf33Sderaadt if (strm == Z_NULL) return Z_STREAM_ERROR; 3876141cf33Sderaadt 3886141cf33Sderaadt strm->msg = Z_NULL; 3896141cf33Sderaadt if (strm->zalloc == (alloc_func)0) { 39036f395ceStb #ifdef Z_SOLO 39136f395ceStb return Z_STREAM_ERROR; 39236f395ceStb #else 3936141cf33Sderaadt strm->zalloc = zcalloc; 3946141cf33Sderaadt strm->opaque = (voidpf)0; 39536f395ceStb #endif 3966141cf33Sderaadt } 39736f395ceStb if (strm->zfree == (free_func)0) 39836f395ceStb #ifdef Z_SOLO 39936f395ceStb return Z_STREAM_ERROR; 40036f395ceStb #else 40136f395ceStb strm->zfree = zcfree; 40236f395ceStb #endif 4036141cf33Sderaadt 4046141cf33Sderaadt #ifdef FASTEST 4056141cf33Sderaadt if (level != 0) level = 1; 4066141cf33Sderaadt #else 4076141cf33Sderaadt if (level == Z_DEFAULT_COMPRESSION) level = 6; 4086141cf33Sderaadt #endif 4096141cf33Sderaadt 4106141cf33Sderaadt if (windowBits < 0) { /* suppress zlib wrapper */ 4116141cf33Sderaadt wrap = 0; 412ddf65acdStb if (windowBits < -15) 413ddf65acdStb return Z_STREAM_ERROR; 4146141cf33Sderaadt windowBits = -windowBits; 4156141cf33Sderaadt } 4166141cf33Sderaadt #ifdef GZIP 4176141cf33Sderaadt else if (windowBits > 15) { 4186141cf33Sderaadt wrap = 2; /* write gzip wrapper instead */ 4196141cf33Sderaadt windowBits -= 16; 4206141cf33Sderaadt } 4216141cf33Sderaadt #endif 4226141cf33Sderaadt if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || 4236141cf33Sderaadt windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || 42436f395ceStb strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) { 4256141cf33Sderaadt return Z_STREAM_ERROR; 4266141cf33Sderaadt } 4276141cf33Sderaadt if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ 4286141cf33Sderaadt s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); 4296141cf33Sderaadt if (s == Z_NULL) return Z_MEM_ERROR; 4306141cf33Sderaadt strm->state = (struct internal_state FAR *)s; 4316141cf33Sderaadt s->strm = strm; 43236f395ceStb s->status = INIT_STATE; /* to pass state test in deflateReset() */ 4336141cf33Sderaadt 4346141cf33Sderaadt s->wrap = wrap; 4356141cf33Sderaadt s->gzhead = Z_NULL; 43636f395ceStb s->w_bits = (uInt)windowBits; 4376141cf33Sderaadt s->w_size = 1 << s->w_bits; 4386141cf33Sderaadt s->w_mask = s->w_size - 1; 4396141cf33Sderaadt 44036f395ceStb s->hash_bits = (uInt)memLevel + 7; 4416141cf33Sderaadt s->hash_size = 1 << s->hash_bits; 4426141cf33Sderaadt s->hash_mask = s->hash_size - 1; 4436141cf33Sderaadt s->hash_shift = ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH); 4446141cf33Sderaadt 4456141cf33Sderaadt s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); 4466141cf33Sderaadt s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 4476141cf33Sderaadt s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 4486141cf33Sderaadt 44936f395ceStb s->high_water = 0; /* nothing written to s->window yet */ 45036f395ceStb 4516141cf33Sderaadt s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 4526141cf33Sderaadt 45356e03632Stb /* We overlay pending_buf and sym_buf. This works since the average size 45456e03632Stb * for length/distance pairs over any compressed block is assured to be 31 45556e03632Stb * bits or less. 45656e03632Stb * 45756e03632Stb * Analysis: The longest fixed codes are a length code of 8 bits plus 5 45856e03632Stb * extra bits, for lengths 131 to 257. The longest fixed distance codes are 45956e03632Stb * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest 46056e03632Stb * possible fixed-codes length/distance pair is then 31 bits total. 46156e03632Stb * 46256e03632Stb * sym_buf starts one-fourth of the way into pending_buf. So there are 46356e03632Stb * three bytes in sym_buf for every four bytes in pending_buf. Each symbol 46456e03632Stb * in sym_buf is three bytes -- two for the distance and one for the 46556e03632Stb * literal/length. As each symbol is consumed, the pointer to the next 46656e03632Stb * sym_buf value to read moves forward three bytes. From that symbol, up to 46756e03632Stb * 31 bits are written to pending_buf. The closest the written pending_buf 46856e03632Stb * bits gets to the next sym_buf symbol to read is just before the last 46956e03632Stb * code is written. At that time, 31*(n - 2) bits have been written, just 47056e03632Stb * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at 47156e03632Stb * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1 47256e03632Stb * symbols are written.) The closest the writing gets to what is unread is 47356e03632Stb * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and 47456e03632Stb * can range from 128 to 32768. 47556e03632Stb * 47656e03632Stb * Therefore, at a minimum, there are 142 bits of space between what is 47756e03632Stb * written and what is read in the overlain buffers, so the symbols cannot 47856e03632Stb * be overwritten by the compressed data. That space is actually 139 bits, 47956e03632Stb * due to the three-bit fixed-code block header. 48056e03632Stb * 48156e03632Stb * That covers the case where either Z_FIXED is specified, forcing fixed 48256e03632Stb * codes, or when the use of fixed codes is chosen, because that choice 48356e03632Stb * results in a smaller compressed block than dynamic codes. That latter 48456e03632Stb * condition then assures that the above analysis also covers all dynamic 48556e03632Stb * blocks. A dynamic-code block will only be chosen to be emitted if it has 48656e03632Stb * fewer bits than a fixed-code block would for the same set of symbols. 48756e03632Stb * Therefore its average symbol length is assured to be less than 31. So 48856e03632Stb * the compressed data for a dynamic block also cannot overwrite the 48956e03632Stb * symbols from which it is being constructed. 49056e03632Stb */ 49156e03632Stb 492555b046eStb s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, LIT_BUFS); 49356e03632Stb s->pending_buf_size = (ulg)s->lit_bufsize * 4; 4946141cf33Sderaadt 4956141cf33Sderaadt if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 4966141cf33Sderaadt s->pending_buf == Z_NULL) { 4976141cf33Sderaadt s->status = FINISH_STATE; 49836f395ceStb strm->msg = ERR_MSG(Z_MEM_ERROR); 4996141cf33Sderaadt deflateEnd (strm); 5006141cf33Sderaadt return Z_MEM_ERROR; 5016141cf33Sderaadt } 50285d95931Stb #ifdef LIT_MEM 50385d95931Stb s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1)); 50485d95931Stb s->l_buf = s->pending_buf + (s->lit_bufsize << 2); 50585d95931Stb s->sym_end = s->lit_bufsize - 1; 50685d95931Stb #else 50756e03632Stb s->sym_buf = s->pending_buf + s->lit_bufsize; 50856e03632Stb s->sym_end = (s->lit_bufsize - 1) * 3; 50985d95931Stb #endif 51056e03632Stb /* We avoid equality with lit_bufsize*3 because of wraparound at 64K 51156e03632Stb * on 16 bit machines and because stored blocks are restricted to 51256e03632Stb * 64K-1 bytes. 51356e03632Stb */ 5146141cf33Sderaadt 5156141cf33Sderaadt s->level = level; 5166141cf33Sderaadt s->strategy = strategy; 5176141cf33Sderaadt s->method = (Byte)method; 5186141cf33Sderaadt 5196141cf33Sderaadt return deflateReset(strm); 5206141cf33Sderaadt } 5216141cf33Sderaadt 52236f395ceStb /* ========================================================================= 52336f395ceStb * Check for a valid deflate stream state. Return 0 if ok, 1 if not. 52436f395ceStb */ 525cfac609cStb local int deflateStateCheck(z_streamp strm) { 52636f395ceStb deflate_state *s; 52736f395ceStb if (strm == Z_NULL || 52836f395ceStb strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) 52936f395ceStb return 1; 53036f395ceStb s = strm->state; 53136f395ceStb if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE && 53236f395ceStb #ifdef GZIP 53336f395ceStb s->status != GZIP_STATE && 53436f395ceStb #endif 53536f395ceStb s->status != EXTRA_STATE && 53636f395ceStb s->status != NAME_STATE && 53736f395ceStb s->status != COMMENT_STATE && 53836f395ceStb s->status != HCRC_STATE && 53936f395ceStb s->status != BUSY_STATE && 54036f395ceStb s->status != FINISH_STATE)) 54136f395ceStb return 1; 54236f395ceStb return 0; 54336f395ceStb } 54436f395ceStb 5456141cf33Sderaadt /* ========================================================================= */ 546cfac609cStb int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary, 547cfac609cStb uInt dictLength) { 5486141cf33Sderaadt deflate_state *s; 54936f395ceStb uInt str, n; 55036f395ceStb int wrap; 55136f395ceStb unsigned avail; 55236f395ceStb z_const unsigned char *next; 5536141cf33Sderaadt 55436f395ceStb if (deflateStateCheck(strm) || dictionary == Z_NULL) 55536f395ceStb return Z_STREAM_ERROR; 55636f395ceStb s = strm->state; 55736f395ceStb wrap = s->wrap; 55836f395ceStb if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) 5596141cf33Sderaadt return Z_STREAM_ERROR; 5606141cf33Sderaadt 56136f395ceStb /* when using zlib wrappers, compute Adler-32 for provided dictionary */ 56236f395ceStb if (wrap == 1) 5636141cf33Sderaadt strm->adler = adler32(strm->adler, dictionary, dictLength); 56436f395ceStb s->wrap = 0; /* avoid computing Adler-32 in read_buf */ 5656141cf33Sderaadt 56636f395ceStb /* if dictionary would fill window, just replace the history */ 56736f395ceStb if (dictLength >= s->w_size) { 56836f395ceStb if (wrap == 0) { /* already empty otherwise */ 56936f395ceStb CLEAR_HASH(s); 57036f395ceStb s->strstart = 0; 57136f395ceStb s->block_start = 0L; 57236f395ceStb s->insert = 0; 5736141cf33Sderaadt } 57436f395ceStb dictionary += dictLength - s->w_size; /* use the tail */ 57536f395ceStb dictLength = s->w_size; 57636f395ceStb } 5776141cf33Sderaadt 57836f395ceStb /* insert dictionary into window and hash */ 57936f395ceStb avail = strm->avail_in; 58036f395ceStb next = strm->next_in; 58136f395ceStb strm->avail_in = dictLength; 58236f395ceStb strm->next_in = (z_const Bytef *)dictionary; 58336f395ceStb fill_window(s); 58436f395ceStb while (s->lookahead >= MIN_MATCH) { 58536f395ceStb str = s->strstart; 58636f395ceStb n = s->lookahead - (MIN_MATCH-1); 58736f395ceStb do { 58836f395ceStb UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); 58936f395ceStb #ifndef FASTEST 59036f395ceStb s->prev[str & s->w_mask] = s->head[s->ins_h]; 59136f395ceStb #endif 59236f395ceStb s->head[s->ins_h] = (Pos)str; 59336f395ceStb str++; 59436f395ceStb } while (--n); 59536f395ceStb s->strstart = str; 59636f395ceStb s->lookahead = MIN_MATCH-1; 59736f395ceStb fill_window(s); 5986141cf33Sderaadt } 59936f395ceStb s->strstart += s->lookahead; 60036f395ceStb s->block_start = (long)s->strstart; 60136f395ceStb s->insert = s->lookahead; 60236f395ceStb s->lookahead = 0; 60336f395ceStb s->match_length = s->prev_length = MIN_MATCH-1; 60436f395ceStb s->match_available = 0; 60536f395ceStb strm->next_in = next; 60636f395ceStb strm->avail_in = avail; 60736f395ceStb s->wrap = wrap; 6086141cf33Sderaadt return Z_OK; 6096141cf33Sderaadt } 6106141cf33Sderaadt 6116141cf33Sderaadt /* ========================================================================= */ 612cfac609cStb int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary, 613cfac609cStb uInt *dictLength) { 61436f395ceStb deflate_state *s; 61536f395ceStb uInt len; 61636f395ceStb 61736f395ceStb if (deflateStateCheck(strm)) 61836f395ceStb return Z_STREAM_ERROR; 61936f395ceStb s = strm->state; 62036f395ceStb len = s->strstart + s->lookahead; 62136f395ceStb if (len > s->w_size) 62236f395ceStb len = s->w_size; 62336f395ceStb if (dictionary != Z_NULL && len) 62436f395ceStb zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len); 62536f395ceStb if (dictLength != Z_NULL) 62636f395ceStb *dictLength = len; 62736f395ceStb return Z_OK; 62836f395ceStb } 62936f395ceStb 63036f395ceStb /* ========================================================================= */ 631cfac609cStb int ZEXPORT deflateResetKeep(z_streamp strm) { 6326141cf33Sderaadt deflate_state *s; 6336141cf33Sderaadt 63436f395ceStb if (deflateStateCheck(strm)) { 6356141cf33Sderaadt return Z_STREAM_ERROR; 6366141cf33Sderaadt } 6376141cf33Sderaadt 6386141cf33Sderaadt strm->total_in = strm->total_out = 0; 6396141cf33Sderaadt strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ 6406141cf33Sderaadt strm->data_type = Z_UNKNOWN; 6416141cf33Sderaadt 6426141cf33Sderaadt s = (deflate_state *)strm->state; 6436141cf33Sderaadt s->pending = 0; 6446141cf33Sderaadt s->pending_out = s->pending_buf; 6456141cf33Sderaadt 6466141cf33Sderaadt if (s->wrap < 0) { 6476141cf33Sderaadt s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ 6486141cf33Sderaadt } 64936f395ceStb s->status = 65036f395ceStb #ifdef GZIP 65136f395ceStb s->wrap == 2 ? GZIP_STATE : 65236f395ceStb #endif 653a6530658Stb INIT_STATE; 6546141cf33Sderaadt strm->adler = 6556141cf33Sderaadt #ifdef GZIP 6566141cf33Sderaadt s->wrap == 2 ? crc32(0L, Z_NULL, 0) : 6576141cf33Sderaadt #endif 6586141cf33Sderaadt adler32(0L, Z_NULL, 0); 659a6530658Stb s->last_flush = -2; 6606141cf33Sderaadt 6616141cf33Sderaadt _tr_init(s); 6626141cf33Sderaadt 6636141cf33Sderaadt return Z_OK; 6646141cf33Sderaadt } 6656141cf33Sderaadt 666cfac609cStb /* =========================================================================== 667cfac609cStb * Initialize the "longest match" routines for a new zlib stream 668cfac609cStb */ 669cfac609cStb local void lm_init(deflate_state *s) { 670cfac609cStb s->window_size = (ulg)2L*s->w_size; 671cfac609cStb 672cfac609cStb CLEAR_HASH(s); 673cfac609cStb 674cfac609cStb /* Set the default configuration parameters: 675cfac609cStb */ 676cfac609cStb s->max_lazy_match = configuration_table[s->level].max_lazy; 677cfac609cStb s->good_match = configuration_table[s->level].good_length; 678cfac609cStb s->nice_match = configuration_table[s->level].nice_length; 679cfac609cStb s->max_chain_length = configuration_table[s->level].max_chain; 680cfac609cStb 681cfac609cStb s->strstart = 0; 682cfac609cStb s->block_start = 0L; 683cfac609cStb s->lookahead = 0; 684cfac609cStb s->insert = 0; 685cfac609cStb s->match_length = s->prev_length = MIN_MATCH-1; 686cfac609cStb s->match_available = 0; 687cfac609cStb s->ins_h = 0; 688cfac609cStb } 689cfac609cStb 6906141cf33Sderaadt /* ========================================================================= */ 691cfac609cStb int ZEXPORT deflateReset(z_streamp strm) { 69236f395ceStb int ret; 69336f395ceStb 69436f395ceStb ret = deflateResetKeep(strm); 69536f395ceStb if (ret == Z_OK) 69636f395ceStb lm_init(strm->state); 69736f395ceStb return ret; 69836f395ceStb } 69936f395ceStb 70036f395ceStb /* ========================================================================= */ 701cfac609cStb int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head) { 70236f395ceStb if (deflateStateCheck(strm) || strm->state->wrap != 2) 70336f395ceStb return Z_STREAM_ERROR; 7046141cf33Sderaadt strm->state->gzhead = head; 7056141cf33Sderaadt return Z_OK; 7066141cf33Sderaadt } 7076141cf33Sderaadt 7086141cf33Sderaadt /* ========================================================================= */ 709cfac609cStb int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits) { 71036f395ceStb if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 71136f395ceStb if (pending != Z_NULL) 71236f395ceStb *pending = strm->state->pending; 71336f395ceStb if (bits != Z_NULL) 71436f395ceStb *bits = strm->state->bi_valid; 71536f395ceStb return Z_OK; 71636f395ceStb } 71736f395ceStb 71836f395ceStb /* ========================================================================= */ 719*0a218225Stb int ZEXPORT deflateUsed(z_streamp strm, int *bits) { 720*0a218225Stb if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 721*0a218225Stb if (bits != Z_NULL) 722*0a218225Stb *bits = strm->state->bi_used; 723*0a218225Stb return Z_OK; 724*0a218225Stb } 725*0a218225Stb 726*0a218225Stb /* ========================================================================= */ 727cfac609cStb int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) { 72836f395ceStb deflate_state *s; 72936f395ceStb int put; 73036f395ceStb 73136f395ceStb if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 73236f395ceStb s = strm->state; 73385d95931Stb #ifdef LIT_MEM 73485d95931Stb if (bits < 0 || bits > 16 || 73585d95931Stb (uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3)) 73685d95931Stb return Z_BUF_ERROR; 73785d95931Stb #else 73856e03632Stb if (bits < 0 || bits > 16 || 73956e03632Stb s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3)) 74036f395ceStb return Z_BUF_ERROR; 74185d95931Stb #endif 74236f395ceStb do { 74336f395ceStb put = Buf_size - s->bi_valid; 74436f395ceStb if (put > bits) 74536f395ceStb put = bits; 74636f395ceStb s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); 74736f395ceStb s->bi_valid += put; 74836f395ceStb _tr_flush_bits(s); 74936f395ceStb value >>= put; 75036f395ceStb bits -= put; 75136f395ceStb } while (bits); 7526141cf33Sderaadt return Z_OK; 7536141cf33Sderaadt } 7546141cf33Sderaadt 7556141cf33Sderaadt /* ========================================================================= */ 756cfac609cStb int ZEXPORT deflateParams(z_streamp strm, int level, int strategy) { 7576141cf33Sderaadt deflate_state *s; 7586141cf33Sderaadt compress_func func; 7596141cf33Sderaadt 76036f395ceStb if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 7616141cf33Sderaadt s = strm->state; 7626141cf33Sderaadt 7636141cf33Sderaadt #ifdef FASTEST 7646141cf33Sderaadt if (level != 0) level = 1; 7656141cf33Sderaadt #else 7666141cf33Sderaadt if (level == Z_DEFAULT_COMPRESSION) level = 6; 7676141cf33Sderaadt #endif 7686141cf33Sderaadt if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { 7696141cf33Sderaadt return Z_STREAM_ERROR; 7706141cf33Sderaadt } 7716141cf33Sderaadt func = configuration_table[s->level].func; 7726141cf33Sderaadt 77336f395ceStb if ((strategy != s->strategy || func != configuration_table[level].func) && 774a6530658Stb s->last_flush != -2) { 7756141cf33Sderaadt /* Flush the last buffer: */ 77636f395ceStb int err = deflate(strm, Z_BLOCK); 77736f395ceStb if (err == Z_STREAM_ERROR) 77836f395ceStb return err; 779a6530658Stb if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead) 78036f395ceStb return Z_BUF_ERROR; 7816141cf33Sderaadt } 7826141cf33Sderaadt if (s->level != level) { 78336f395ceStb if (s->level == 0 && s->matches != 0) { 78436f395ceStb if (s->matches == 1) 78536f395ceStb slide_hash(s); 78636f395ceStb else 78736f395ceStb CLEAR_HASH(s); 78836f395ceStb s->matches = 0; 78936f395ceStb } 7906141cf33Sderaadt s->level = level; 7916141cf33Sderaadt s->max_lazy_match = configuration_table[level].max_lazy; 7926141cf33Sderaadt s->good_match = configuration_table[level].good_length; 7936141cf33Sderaadt s->nice_match = configuration_table[level].nice_length; 7946141cf33Sderaadt s->max_chain_length = configuration_table[level].max_chain; 7956141cf33Sderaadt } 7966141cf33Sderaadt s->strategy = strategy; 79736f395ceStb return Z_OK; 7986141cf33Sderaadt } 7996141cf33Sderaadt 8006141cf33Sderaadt /* ========================================================================= */ 801cfac609cStb int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy, 802cfac609cStb int nice_length, int max_chain) { 8036141cf33Sderaadt deflate_state *s; 8046141cf33Sderaadt 80536f395ceStb if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 8066141cf33Sderaadt s = strm->state; 80736f395ceStb s->good_match = (uInt)good_length; 80836f395ceStb s->max_lazy_match = (uInt)max_lazy; 8096141cf33Sderaadt s->nice_match = nice_length; 81036f395ceStb s->max_chain_length = (uInt)max_chain; 8116141cf33Sderaadt return Z_OK; 8126141cf33Sderaadt } 8136141cf33Sderaadt 8146141cf33Sderaadt /* ========================================================================= 815ddf65acdStb * For the default windowBits of 15 and memLevel of 8, this function returns a 816ddf65acdStb * close to exact, as well as small, upper bound on the compressed size. This 817ddf65acdStb * is an expansion of ~0.03%, plus a small constant. 8186141cf33Sderaadt * 819ddf65acdStb * For any setting other than those defaults for windowBits and memLevel, one 820ddf65acdStb * of two worst case bounds is returned. This is at most an expansion of ~4% or 821ddf65acdStb * ~13%, plus a small constant. 8226141cf33Sderaadt * 823ddf65acdStb * Both the 0.03% and 4% derive from the overhead of stored blocks. The first 824ddf65acdStb * one is for stored blocks of 16383 bytes (memLevel == 8), whereas the second 825ddf65acdStb * is for stored blocks of 127 bytes (the worst case memLevel == 1). The 826ddf65acdStb * expansion results from five bytes of header for each stored block. 827ddf65acdStb * 828ddf65acdStb * The larger expansion of 13% results from a window size less than or equal to 829ddf65acdStb * the symbols buffer size (windowBits <= memLevel + 7). In that case some of 830ddf65acdStb * the data being compressed may have slid out of the sliding window, impeding 831ddf65acdStb * a stored block from being emitted. Then the only choice is a fixed or 832ddf65acdStb * dynamic block, where a fixed block limits the maximum expansion to 9 bits 833ddf65acdStb * per 8-bit byte, plus 10 bits for every block. The smallest block size for 834ddf65acdStb * which this can occur is 255 (memLevel == 2). 835ddf65acdStb * 836ddf65acdStb * Shifts are used to approximate divisions, for speed. 8376141cf33Sderaadt */ 838cfac609cStb uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen) { 8396141cf33Sderaadt deflate_state *s; 840ddf65acdStb uLong fixedlen, storelen, wraplen; 8416141cf33Sderaadt 842ddf65acdStb /* upper bound for fixed blocks with 9-bit literals and length 255 843ddf65acdStb (memLevel == 2, which is the lowest that may not use stored blocks) -- 844ddf65acdStb ~13% overhead plus a small constant */ 845ddf65acdStb fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) + 846ddf65acdStb (sourceLen >> 9) + 4; 8476141cf33Sderaadt 848ddf65acdStb /* upper bound for stored blocks with length 127 (memLevel == 1) -- 849ddf65acdStb ~4% overhead plus a small constant */ 850ddf65acdStb storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) + 851ddf65acdStb (sourceLen >> 11) + 7; 852ddf65acdStb 853a05001adStb /* if can't get parameters, return larger bound plus a wrapper */ 85436f395ceStb if (deflateStateCheck(strm)) 855a05001adStb return (fixedlen > storelen ? fixedlen : storelen) + 18; 85636f395ceStb 85736f395ceStb /* compute wrapper length */ 85836f395ceStb s = strm->state; 859a05001adStb switch (s->wrap < 0 ? -s->wrap : s->wrap) { 86036f395ceStb case 0: /* raw deflate */ 86136f395ceStb wraplen = 0; 86236f395ceStb break; 86336f395ceStb case 1: /* zlib wrapper */ 86436f395ceStb wraplen = 6 + (s->strstart ? 4 : 0); 86536f395ceStb break; 86636f395ceStb #ifdef GZIP 86736f395ceStb case 2: /* gzip wrapper */ 86836f395ceStb wraplen = 18; 86936f395ceStb if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ 87036f395ceStb Bytef *str; 87136f395ceStb if (s->gzhead->extra != Z_NULL) 87236f395ceStb wraplen += 2 + s->gzhead->extra_len; 87336f395ceStb str = s->gzhead->name; 87436f395ceStb if (str != Z_NULL) 87536f395ceStb do { 87636f395ceStb wraplen++; 87736f395ceStb } while (*str++); 87836f395ceStb str = s->gzhead->comment; 87936f395ceStb if (str != Z_NULL) 88036f395ceStb do { 88136f395ceStb wraplen++; 88236f395ceStb } while (*str++); 88336f395ceStb if (s->gzhead->hcrc) 88436f395ceStb wraplen += 2; 88536f395ceStb } 88636f395ceStb break; 88736f395ceStb #endif 88836f395ceStb default: /* for compiler happiness */ 889a05001adStb wraplen = 18; 89036f395ceStb } 8916141cf33Sderaadt 892ddf65acdStb /* if not default parameters, return one of the conservative bounds */ 8936141cf33Sderaadt if (s->w_bits != 15 || s->hash_bits != 8 + 7) 894cfac609cStb return (s->w_bits <= s->hash_bits && s->level ? fixedlen : storelen) + 895cfac609cStb wraplen; 8966141cf33Sderaadt 897ddf65acdStb /* default settings: return tight bound for that case -- ~0.03% overhead 898ddf65acdStb plus a small constant */ 89936f395ceStb return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 90036f395ceStb (sourceLen >> 25) + 13 - 6 + wraplen; 9016141cf33Sderaadt } 9026141cf33Sderaadt 9036141cf33Sderaadt /* ========================================================================= 9046141cf33Sderaadt * Put a short in the pending buffer. The 16-bit value is put in MSB order. 9056141cf33Sderaadt * IN assertion: the stream state is correct and there is enough room in 9066141cf33Sderaadt * pending_buf. 9076141cf33Sderaadt */ 908cfac609cStb local void putShortMSB(deflate_state *s, uInt b) { 9096141cf33Sderaadt put_byte(s, (Byte)(b >> 8)); 9106141cf33Sderaadt put_byte(s, (Byte)(b & 0xff)); 9116141cf33Sderaadt } 9126141cf33Sderaadt 9136141cf33Sderaadt /* ========================================================================= 91436f395ceStb * Flush as much pending output as possible. All deflate() output, except for 91536f395ceStb * some deflate_stored() output, goes through this function so some 91636f395ceStb * applications may wish to modify it to avoid allocating a large 91736f395ceStb * strm->next_out buffer and copying into it. (See also read_buf()). 9186141cf33Sderaadt */ 919cfac609cStb local void flush_pending(z_streamp strm) { 92036f395ceStb unsigned len; 92136f395ceStb deflate_state *s = strm->state; 9226141cf33Sderaadt 92336f395ceStb _tr_flush_bits(s); 92436f395ceStb len = s->pending; 9256141cf33Sderaadt if (len > strm->avail_out) len = strm->avail_out; 9266141cf33Sderaadt if (len == 0) return; 9276141cf33Sderaadt 92836f395ceStb zmemcpy(strm->next_out, s->pending_out, len); 9296141cf33Sderaadt strm->next_out += len; 93036f395ceStb s->pending_out += len; 9316141cf33Sderaadt strm->total_out += len; 9326141cf33Sderaadt strm->avail_out -= len; 93336f395ceStb s->pending -= len; 93436f395ceStb if (s->pending == 0) { 93536f395ceStb s->pending_out = s->pending_buf; 9366141cf33Sderaadt } 9376141cf33Sderaadt } 9386141cf33Sderaadt 93936f395ceStb /* =========================================================================== 94036f395ceStb * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1]. 94136f395ceStb */ 94236f395ceStb #define HCRC_UPDATE(beg) \ 94336f395ceStb do { \ 94436f395ceStb if (s->gzhead->hcrc && s->pending > (beg)) \ 94536f395ceStb strm->adler = crc32(strm->adler, s->pending_buf + (beg), \ 94636f395ceStb s->pending - (beg)); \ 94736f395ceStb } while (0) 94836f395ceStb 9496141cf33Sderaadt /* ========================================================================= */ 950cfac609cStb int ZEXPORT deflate(z_streamp strm, int flush) { 9516141cf33Sderaadt int old_flush; /* value of flush param for previous deflate call */ 9526141cf33Sderaadt deflate_state *s; 9536141cf33Sderaadt 95436f395ceStb if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) { 9556141cf33Sderaadt return Z_STREAM_ERROR; 9566141cf33Sderaadt } 9576141cf33Sderaadt s = strm->state; 9586141cf33Sderaadt 9596141cf33Sderaadt if (strm->next_out == Z_NULL || 96036f395ceStb (strm->avail_in != 0 && strm->next_in == Z_NULL) || 9616141cf33Sderaadt (s->status == FINISH_STATE && flush != Z_FINISH)) { 9626141cf33Sderaadt ERR_RETURN(strm, Z_STREAM_ERROR); 9636141cf33Sderaadt } 9646141cf33Sderaadt if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 9656141cf33Sderaadt 9666141cf33Sderaadt old_flush = s->last_flush; 9676141cf33Sderaadt s->last_flush = flush; 9686141cf33Sderaadt 9696141cf33Sderaadt /* Flush as much pending output as possible */ 9706141cf33Sderaadt if (s->pending != 0) { 9716141cf33Sderaadt flush_pending(strm); 9726141cf33Sderaadt if (strm->avail_out == 0) { 9736141cf33Sderaadt /* Since avail_out is 0, deflate will be called again with 9746141cf33Sderaadt * more output space, but possibly with both pending and 9756141cf33Sderaadt * avail_in equal to zero. There won't be anything to do, 9766141cf33Sderaadt * but this is not an error situation so make sure we 9776141cf33Sderaadt * return OK instead of BUF_ERROR at next call of deflate: 9786141cf33Sderaadt */ 9796141cf33Sderaadt s->last_flush = -1; 9806141cf33Sderaadt return Z_OK; 9816141cf33Sderaadt } 9826141cf33Sderaadt 9836141cf33Sderaadt /* Make sure there is something to do and avoid duplicate consecutive 9846141cf33Sderaadt * flushes. For repeated and useless calls with Z_FINISH, we keep 9856141cf33Sderaadt * returning Z_STREAM_END instead of Z_BUF_ERROR. 9866141cf33Sderaadt */ 98736f395ceStb } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && 9886141cf33Sderaadt flush != Z_FINISH) { 9896141cf33Sderaadt ERR_RETURN(strm, Z_BUF_ERROR); 9906141cf33Sderaadt } 9916141cf33Sderaadt 9926141cf33Sderaadt /* User must not provide more input after the first FINISH: */ 9936141cf33Sderaadt if (s->status == FINISH_STATE && strm->avail_in != 0) { 9946141cf33Sderaadt ERR_RETURN(strm, Z_BUF_ERROR); 9956141cf33Sderaadt } 9966141cf33Sderaadt 99736f395ceStb /* Write the header */ 998a6530658Stb if (s->status == INIT_STATE && s->wrap == 0) 999a6530658Stb s->status = BUSY_STATE; 100036f395ceStb if (s->status == INIT_STATE) { 100136f395ceStb /* zlib header */ 100236f395ceStb uInt header = (Z_DEFLATED + ((s->w_bits - 8) << 4)) << 8; 100336f395ceStb uInt level_flags; 100436f395ceStb 100536f395ceStb if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) 100636f395ceStb level_flags = 0; 100736f395ceStb else if (s->level < 6) 100836f395ceStb level_flags = 1; 100936f395ceStb else if (s->level == 6) 101036f395ceStb level_flags = 2; 101136f395ceStb else 101236f395ceStb level_flags = 3; 101336f395ceStb header |= (level_flags << 6); 101436f395ceStb if (s->strstart != 0) header |= PRESET_DICT; 101536f395ceStb header += 31 - (header % 31); 101636f395ceStb 101736f395ceStb putShortMSB(s, header); 101836f395ceStb 101936f395ceStb /* Save the adler32 of the preset dictionary: */ 102036f395ceStb if (s->strstart != 0) { 102136f395ceStb putShortMSB(s, (uInt)(strm->adler >> 16)); 102236f395ceStb putShortMSB(s, (uInt)(strm->adler & 0xffff)); 102336f395ceStb } 102436f395ceStb strm->adler = adler32(0L, Z_NULL, 0); 102536f395ceStb s->status = BUSY_STATE; 102636f395ceStb 102736f395ceStb /* Compression must start with an empty pending buffer */ 102836f395ceStb flush_pending(strm); 102936f395ceStb if (s->pending != 0) { 103036f395ceStb s->last_flush = -1; 103136f395ceStb return Z_OK; 103236f395ceStb } 103336f395ceStb } 103436f395ceStb #ifdef GZIP 103536f395ceStb if (s->status == GZIP_STATE) { 103636f395ceStb /* gzip header */ 103736f395ceStb strm->adler = crc32(0L, Z_NULL, 0); 103836f395ceStb put_byte(s, 31); 103936f395ceStb put_byte(s, 139); 104036f395ceStb put_byte(s, 8); 104136f395ceStb if (s->gzhead == Z_NULL) { 104236f395ceStb put_byte(s, 0); 104336f395ceStb put_byte(s, 0); 104436f395ceStb put_byte(s, 0); 104536f395ceStb put_byte(s, 0); 104636f395ceStb put_byte(s, 0); 104736f395ceStb put_byte(s, s->level == 9 ? 2 : 104836f395ceStb (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 104936f395ceStb 4 : 0)); 105036f395ceStb put_byte(s, OS_CODE); 105136f395ceStb s->status = BUSY_STATE; 105236f395ceStb 105336f395ceStb /* Compression must start with an empty pending buffer */ 105436f395ceStb flush_pending(strm); 105536f395ceStb if (s->pending != 0) { 105636f395ceStb s->last_flush = -1; 105736f395ceStb return Z_OK; 105836f395ceStb } 105936f395ceStb } 106036f395ceStb else { 106136f395ceStb put_byte(s, (s->gzhead->text ? 1 : 0) + 106236f395ceStb (s->gzhead->hcrc ? 2 : 0) + 106336f395ceStb (s->gzhead->extra == Z_NULL ? 0 : 4) + 106436f395ceStb (s->gzhead->name == Z_NULL ? 0 : 8) + 106536f395ceStb (s->gzhead->comment == Z_NULL ? 0 : 16) 106636f395ceStb ); 106736f395ceStb put_byte(s, (Byte)(s->gzhead->time & 0xff)); 106836f395ceStb put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); 106936f395ceStb put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); 107036f395ceStb put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); 107136f395ceStb put_byte(s, s->level == 9 ? 2 : 107236f395ceStb (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 107336f395ceStb 4 : 0)); 107436f395ceStb put_byte(s, s->gzhead->os & 0xff); 107536f395ceStb if (s->gzhead->extra != Z_NULL) { 107636f395ceStb put_byte(s, s->gzhead->extra_len & 0xff); 107736f395ceStb put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); 107836f395ceStb } 107936f395ceStb if (s->gzhead->hcrc) 108036f395ceStb strm->adler = crc32(strm->adler, s->pending_buf, 108136f395ceStb s->pending); 108236f395ceStb s->gzindex = 0; 108336f395ceStb s->status = EXTRA_STATE; 108436f395ceStb } 108536f395ceStb } 108636f395ceStb if (s->status == EXTRA_STATE) { 108736f395ceStb if (s->gzhead->extra != Z_NULL) { 108836f395ceStb ulg beg = s->pending; /* start of bytes to update crc */ 108936f395ceStb uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex; 109036f395ceStb while (s->pending + left > s->pending_buf_size) { 109136f395ceStb uInt copy = s->pending_buf_size - s->pending; 109236f395ceStb zmemcpy(s->pending_buf + s->pending, 109336f395ceStb s->gzhead->extra + s->gzindex, copy); 109436f395ceStb s->pending = s->pending_buf_size; 109536f395ceStb HCRC_UPDATE(beg); 109636f395ceStb s->gzindex += copy; 109736f395ceStb flush_pending(strm); 109836f395ceStb if (s->pending != 0) { 109936f395ceStb s->last_flush = -1; 110036f395ceStb return Z_OK; 110136f395ceStb } 110236f395ceStb beg = 0; 110336f395ceStb left -= copy; 110436f395ceStb } 110536f395ceStb zmemcpy(s->pending_buf + s->pending, 110636f395ceStb s->gzhead->extra + s->gzindex, left); 110736f395ceStb s->pending += left; 110836f395ceStb HCRC_UPDATE(beg); 110936f395ceStb s->gzindex = 0; 111036f395ceStb } 111136f395ceStb s->status = NAME_STATE; 111236f395ceStb } 111336f395ceStb if (s->status == NAME_STATE) { 111436f395ceStb if (s->gzhead->name != Z_NULL) { 111536f395ceStb ulg beg = s->pending; /* start of bytes to update crc */ 111636f395ceStb int val; 111736f395ceStb do { 111836f395ceStb if (s->pending == s->pending_buf_size) { 111936f395ceStb HCRC_UPDATE(beg); 112036f395ceStb flush_pending(strm); 112136f395ceStb if (s->pending != 0) { 112236f395ceStb s->last_flush = -1; 112336f395ceStb return Z_OK; 112436f395ceStb } 112536f395ceStb beg = 0; 112636f395ceStb } 112736f395ceStb val = s->gzhead->name[s->gzindex++]; 112836f395ceStb put_byte(s, val); 112936f395ceStb } while (val != 0); 113036f395ceStb HCRC_UPDATE(beg); 113136f395ceStb s->gzindex = 0; 113236f395ceStb } 113336f395ceStb s->status = COMMENT_STATE; 113436f395ceStb } 113536f395ceStb if (s->status == COMMENT_STATE) { 113636f395ceStb if (s->gzhead->comment != Z_NULL) { 113736f395ceStb ulg beg = s->pending; /* start of bytes to update crc */ 113836f395ceStb int val; 113936f395ceStb do { 114036f395ceStb if (s->pending == s->pending_buf_size) { 114136f395ceStb HCRC_UPDATE(beg); 114236f395ceStb flush_pending(strm); 114336f395ceStb if (s->pending != 0) { 114436f395ceStb s->last_flush = -1; 114536f395ceStb return Z_OK; 114636f395ceStb } 114736f395ceStb beg = 0; 114836f395ceStb } 114936f395ceStb val = s->gzhead->comment[s->gzindex++]; 115036f395ceStb put_byte(s, val); 115136f395ceStb } while (val != 0); 115236f395ceStb HCRC_UPDATE(beg); 115336f395ceStb } 115436f395ceStb s->status = HCRC_STATE; 115536f395ceStb } 115636f395ceStb if (s->status == HCRC_STATE) { 115736f395ceStb if (s->gzhead->hcrc) { 115836f395ceStb if (s->pending + 2 > s->pending_buf_size) { 115936f395ceStb flush_pending(strm); 116036f395ceStb if (s->pending != 0) { 116136f395ceStb s->last_flush = -1; 116236f395ceStb return Z_OK; 116336f395ceStb } 116436f395ceStb } 116536f395ceStb put_byte(s, (Byte)(strm->adler & 0xff)); 116636f395ceStb put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 116736f395ceStb strm->adler = crc32(0L, Z_NULL, 0); 116836f395ceStb } 116936f395ceStb s->status = BUSY_STATE; 117036f395ceStb 117136f395ceStb /* Compression must start with an empty pending buffer */ 117236f395ceStb flush_pending(strm); 117336f395ceStb if (s->pending != 0) { 117436f395ceStb s->last_flush = -1; 117536f395ceStb return Z_OK; 117636f395ceStb } 117736f395ceStb } 117836f395ceStb #endif 117936f395ceStb 11806141cf33Sderaadt /* Start a new block or continue the current one. 11816141cf33Sderaadt */ 11826141cf33Sderaadt if (strm->avail_in != 0 || s->lookahead != 0 || 11836141cf33Sderaadt (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { 11846141cf33Sderaadt block_state bstate; 11856141cf33Sderaadt 118636f395ceStb bstate = s->level == 0 ? deflate_stored(s, flush) : 118736f395ceStb s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : 118836f395ceStb s->strategy == Z_RLE ? deflate_rle(s, flush) : 118936f395ceStb (*(configuration_table[s->level].func))(s, flush); 11906141cf33Sderaadt 11916141cf33Sderaadt if (bstate == finish_started || bstate == finish_done) { 11926141cf33Sderaadt s->status = FINISH_STATE; 11936141cf33Sderaadt } 11946141cf33Sderaadt if (bstate == need_more || bstate == finish_started) { 11956141cf33Sderaadt if (strm->avail_out == 0) { 11966141cf33Sderaadt s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ 11976141cf33Sderaadt } 11986141cf33Sderaadt return Z_OK; 11996141cf33Sderaadt /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 12006141cf33Sderaadt * of deflate should use the same flush parameter to make sure 12016141cf33Sderaadt * that the flush is complete. So we don't have to output an 12026141cf33Sderaadt * empty block here, this will be done at next call. This also 12036141cf33Sderaadt * ensures that for a very small output buffer, we emit at most 12046141cf33Sderaadt * one empty block. 12056141cf33Sderaadt */ 12066141cf33Sderaadt } 12076141cf33Sderaadt if (bstate == block_done) { 12086141cf33Sderaadt if (flush == Z_PARTIAL_FLUSH) { 12096141cf33Sderaadt _tr_align(s); 121036f395ceStb } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ 121136f395ceStb _tr_stored_block(s, (char*)0, 0L, 0); 12126141cf33Sderaadt /* For a full flush, this empty block will be recognized 12136141cf33Sderaadt * as a special marker by inflate_sync(). 12146141cf33Sderaadt */ 12156141cf33Sderaadt if (flush == Z_FULL_FLUSH) { 12166141cf33Sderaadt CLEAR_HASH(s); /* forget history */ 121736f395ceStb if (s->lookahead == 0) { 121836f395ceStb s->strstart = 0; 121936f395ceStb s->block_start = 0L; 122036f395ceStb s->insert = 0; 122136f395ceStb } 12226141cf33Sderaadt } 12236141cf33Sderaadt } 12246141cf33Sderaadt flush_pending(strm); 12256141cf33Sderaadt if (strm->avail_out == 0) { 12266141cf33Sderaadt s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ 12276141cf33Sderaadt return Z_OK; 12286141cf33Sderaadt } 12296141cf33Sderaadt } 12306141cf33Sderaadt } 12316141cf33Sderaadt 12326141cf33Sderaadt if (flush != Z_FINISH) return Z_OK; 12336141cf33Sderaadt if (s->wrap <= 0) return Z_STREAM_END; 12346141cf33Sderaadt 12356141cf33Sderaadt /* Write the trailer */ 12366141cf33Sderaadt #ifdef GZIP 12376141cf33Sderaadt if (s->wrap == 2) { 12386141cf33Sderaadt put_byte(s, (Byte)(strm->adler & 0xff)); 12396141cf33Sderaadt put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 12406141cf33Sderaadt put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); 12416141cf33Sderaadt put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); 12426141cf33Sderaadt put_byte(s, (Byte)(strm->total_in & 0xff)); 12436141cf33Sderaadt put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); 12446141cf33Sderaadt put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); 12456141cf33Sderaadt put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); 12466141cf33Sderaadt } 12476141cf33Sderaadt else 12486141cf33Sderaadt #endif 12496141cf33Sderaadt { 12506141cf33Sderaadt putShortMSB(s, (uInt)(strm->adler >> 16)); 12516141cf33Sderaadt putShortMSB(s, (uInt)(strm->adler & 0xffff)); 12526141cf33Sderaadt } 12536141cf33Sderaadt flush_pending(strm); 12546141cf33Sderaadt /* If avail_out is zero, the application will call deflate again 12556141cf33Sderaadt * to flush the rest. 12566141cf33Sderaadt */ 12576141cf33Sderaadt if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ 12586141cf33Sderaadt return s->pending != 0 ? Z_OK : Z_STREAM_END; 12596141cf33Sderaadt } 12606141cf33Sderaadt 12616141cf33Sderaadt /* ========================================================================= */ 1262cfac609cStb int ZEXPORT deflateEnd(z_streamp strm) { 12636141cf33Sderaadt int status; 12646141cf33Sderaadt 126536f395ceStb if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 12666141cf33Sderaadt 12676141cf33Sderaadt status = strm->state->status; 12686141cf33Sderaadt 12696141cf33Sderaadt /* Deallocate in reverse order of allocations: */ 12700258c1b1Stb TRY_FREE(strm, strm->state->pending_buf, strm->state->pending_buf_size); 12710258c1b1Stb TRY_FREE(strm, strm->state->head, strm->state->hash_size * sizeof(Pos)); 12720258c1b1Stb TRY_FREE(strm, strm->state->prev, strm->state->w_size * sizeof(Pos)); 1273d061ccb3Stb TRY_FREE(strm, strm->state->window, strm->state->w_size * 2 * sizeof(Byte)); 12746141cf33Sderaadt 12750258c1b1Stb ZFREE(strm, strm->state, sizeof(deflate_state)); 12766141cf33Sderaadt strm->state = Z_NULL; 12776141cf33Sderaadt 12786141cf33Sderaadt return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; 12796141cf33Sderaadt } 12806141cf33Sderaadt 12816141cf33Sderaadt /* ========================================================================= 12826141cf33Sderaadt * Copy the source state to the destination state. 12836141cf33Sderaadt * To simplify the source, this is not supported for 16-bit MSDOS (which 12846141cf33Sderaadt * doesn't have enough memory anyway to duplicate compression states). 12856141cf33Sderaadt */ 1286cfac609cStb int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) { 12876141cf33Sderaadt #ifdef MAXSEG_64K 1288cfac609cStb (void)dest; 1289cfac609cStb (void)source; 12906141cf33Sderaadt return Z_STREAM_ERROR; 12916141cf33Sderaadt #else 12926141cf33Sderaadt deflate_state *ds; 12936141cf33Sderaadt deflate_state *ss; 12946141cf33Sderaadt 12956141cf33Sderaadt 129636f395ceStb if (deflateStateCheck(source) || dest == Z_NULL) { 12976141cf33Sderaadt return Z_STREAM_ERROR; 12986141cf33Sderaadt } 12996141cf33Sderaadt 13006141cf33Sderaadt ss = source->state; 13016141cf33Sderaadt 130236f395ceStb zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); 13036141cf33Sderaadt 13046141cf33Sderaadt ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); 13056141cf33Sderaadt if (ds == Z_NULL) return Z_MEM_ERROR; 13066141cf33Sderaadt dest->state = (struct internal_state FAR *) ds; 130736f395ceStb zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); 13086141cf33Sderaadt ds->strm = dest; 13096141cf33Sderaadt 13106141cf33Sderaadt ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); 13116141cf33Sderaadt ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); 13126141cf33Sderaadt ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); 1313555b046eStb ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS); 13146141cf33Sderaadt 13156141cf33Sderaadt if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || 13166141cf33Sderaadt ds->pending_buf == Z_NULL) { 13176141cf33Sderaadt deflateEnd (dest); 13186141cf33Sderaadt return Z_MEM_ERROR; 13196141cf33Sderaadt } 13206141cf33Sderaadt /* following zmemcpy do not work for 16-bit MSDOS */ 13216141cf33Sderaadt zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); 132236f395ceStb zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); 132336f395ceStb zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); 1324555b046eStb zmemcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS); 13256141cf33Sderaadt 13266141cf33Sderaadt ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); 132785d95931Stb #ifdef LIT_MEM 132885d95931Stb ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1)); 132985d95931Stb ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2); 133085d95931Stb #else 133156e03632Stb ds->sym_buf = ds->pending_buf + ds->lit_bufsize; 133285d95931Stb #endif 13336141cf33Sderaadt 13346141cf33Sderaadt ds->l_desc.dyn_tree = ds->dyn_ltree; 13356141cf33Sderaadt ds->d_desc.dyn_tree = ds->dyn_dtree; 13366141cf33Sderaadt ds->bl_desc.dyn_tree = ds->bl_tree; 13376141cf33Sderaadt 13386141cf33Sderaadt return Z_OK; 13396141cf33Sderaadt #endif /* MAXSEG_64K */ 13406141cf33Sderaadt } 13416141cf33Sderaadt 13426141cf33Sderaadt #ifndef FASTEST 13436141cf33Sderaadt /* =========================================================================== 13446141cf33Sderaadt * Set match_start to the longest match starting at the given string and 13456141cf33Sderaadt * return its length. Matches shorter or equal to prev_length are discarded, 13466141cf33Sderaadt * in which case the result is equal to prev_length and match_start is 13476141cf33Sderaadt * garbage. 13486141cf33Sderaadt * IN assertions: cur_match is the head of the hash chain for the current 13496141cf33Sderaadt * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 13506141cf33Sderaadt * OUT assertion: the match length is not greater than s->lookahead. 13516141cf33Sderaadt */ 1352cfac609cStb local uInt longest_match(deflate_state *s, IPos cur_match) { 13536141cf33Sderaadt unsigned chain_length = s->max_chain_length;/* max hash chain length */ 13546141cf33Sderaadt register Bytef *scan = s->window + s->strstart; /* current string */ 13556141cf33Sderaadt register Bytef *match; /* matched string */ 13566141cf33Sderaadt register int len; /* length of current match */ 135736f395ceStb int best_len = (int)s->prev_length; /* best match length so far */ 13586141cf33Sderaadt int nice_match = s->nice_match; /* stop if match long enough */ 13596141cf33Sderaadt IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 13606141cf33Sderaadt s->strstart - (IPos)MAX_DIST(s) : NIL; 13616141cf33Sderaadt /* Stop when cur_match becomes <= limit. To simplify the code, 13626141cf33Sderaadt * we prevent matches with the string of window index 0. 13636141cf33Sderaadt */ 13646141cf33Sderaadt Posf *prev = s->prev; 13656141cf33Sderaadt uInt wmask = s->w_mask; 13666141cf33Sderaadt 13676141cf33Sderaadt #ifdef UNALIGNED_OK 13686141cf33Sderaadt /* Compare two bytes at a time. Note: this is not always beneficial. 13696141cf33Sderaadt * Try with and without -DUNALIGNED_OK to check. 13706141cf33Sderaadt */ 13716141cf33Sderaadt register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; 13726141cf33Sderaadt register ush scan_start = *(ushf*)scan; 13736141cf33Sderaadt register ush scan_end = *(ushf*)(scan + best_len - 1); 13746141cf33Sderaadt #else 13756141cf33Sderaadt register Bytef *strend = s->window + s->strstart + MAX_MATCH; 13766141cf33Sderaadt register Byte scan_end1 = scan[best_len - 1]; 13776141cf33Sderaadt register Byte scan_end = scan[best_len]; 13786141cf33Sderaadt #endif 13796141cf33Sderaadt 13806141cf33Sderaadt /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 13816141cf33Sderaadt * It is easy to get rid of this optimization if necessary. 13826141cf33Sderaadt */ 13836141cf33Sderaadt Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 13846141cf33Sderaadt 13856141cf33Sderaadt /* Do not waste too much time if we already have a good match: */ 13866141cf33Sderaadt if (s->prev_length >= s->good_match) { 13876141cf33Sderaadt chain_length >>= 2; 13886141cf33Sderaadt } 13896141cf33Sderaadt /* Do not look for matches beyond the end of the input. This is necessary 13906141cf33Sderaadt * to make deflate deterministic. 13916141cf33Sderaadt */ 139236f395ceStb if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; 13936141cf33Sderaadt 1394ddf65acdStb Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, 1395ddf65acdStb "need lookahead"); 13966141cf33Sderaadt 13976141cf33Sderaadt do { 13986141cf33Sderaadt Assert(cur_match < s->strstart, "no future"); 13996141cf33Sderaadt match = s->window + cur_match; 14006141cf33Sderaadt 14016141cf33Sderaadt /* Skip to next match if the match length cannot increase 14026141cf33Sderaadt * or if the match length is less than 2. Note that the checks below 14036141cf33Sderaadt * for insufficient lookahead only occur occasionally for performance 14046141cf33Sderaadt * reasons. Therefore uninitialized memory will be accessed, and 14056141cf33Sderaadt * conditional jumps will be made that depend on those values. 14066141cf33Sderaadt * However the length of the match is limited to the lookahead, so 14076141cf33Sderaadt * the output of deflate is not affected by the uninitialized values. 14086141cf33Sderaadt */ 14096141cf33Sderaadt #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 14106141cf33Sderaadt /* This code assumes sizeof(unsigned short) == 2. Do not use 14116141cf33Sderaadt * UNALIGNED_OK if your compiler uses a different size. 14126141cf33Sderaadt */ 14136141cf33Sderaadt if (*(ushf*)(match + best_len - 1) != scan_end || 14146141cf33Sderaadt *(ushf*)match != scan_start) continue; 14156141cf33Sderaadt 14166141cf33Sderaadt /* It is not necessary to compare scan[2] and match[2] since they are 14176141cf33Sderaadt * always equal when the other bytes match, given that the hash keys 14186141cf33Sderaadt * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at 1419ddf65acdStb * strstart + 3, + 5, up to strstart + 257. We check for insufficient 14206141cf33Sderaadt * lookahead only every 4th comparison; the 128th check will be made 14216141cf33Sderaadt * at strstart + 257. If MAX_MATCH-2 is not a multiple of 8, it is 14226141cf33Sderaadt * necessary to put more guard bytes at the end of the window, or 14236141cf33Sderaadt * to check more often for insufficient lookahead. 14246141cf33Sderaadt */ 14256141cf33Sderaadt Assert(scan[2] == match[2], "scan[2]?"); 14266141cf33Sderaadt scan++, match++; 14276141cf33Sderaadt do { 14286141cf33Sderaadt } while (*(ushf*)(scan += 2) == *(ushf*)(match += 2) && 14296141cf33Sderaadt *(ushf*)(scan += 2) == *(ushf*)(match += 2) && 14306141cf33Sderaadt *(ushf*)(scan += 2) == *(ushf*)(match += 2) && 14316141cf33Sderaadt *(ushf*)(scan += 2) == *(ushf*)(match += 2) && 14326141cf33Sderaadt scan < strend); 14336141cf33Sderaadt /* The funny "do {}" generates better code on most compilers */ 14346141cf33Sderaadt 14356141cf33Sderaadt /* Here, scan <= window + strstart + 257 */ 1436ddf65acdStb Assert(scan <= s->window + (unsigned)(s->window_size - 1), 1437ddf65acdStb "wild scan"); 14386141cf33Sderaadt if (*scan == *match) scan++; 14396141cf33Sderaadt 14406141cf33Sderaadt len = (MAX_MATCH - 1) - (int)(strend - scan); 14416141cf33Sderaadt scan = strend - (MAX_MATCH-1); 14426141cf33Sderaadt 14436141cf33Sderaadt #else /* UNALIGNED_OK */ 14446141cf33Sderaadt 14456141cf33Sderaadt if (match[best_len] != scan_end || 14466141cf33Sderaadt match[best_len - 1] != scan_end1 || 14476141cf33Sderaadt *match != *scan || 14486141cf33Sderaadt *++match != scan[1]) continue; 14496141cf33Sderaadt 14506141cf33Sderaadt /* The check at best_len - 1 can be removed because it will be made 14516141cf33Sderaadt * again later. (This heuristic is not always a win.) 14526141cf33Sderaadt * It is not necessary to compare scan[2] and match[2] since they 14536141cf33Sderaadt * are always equal when the other bytes match, given that 14546141cf33Sderaadt * the hash keys are equal and that HASH_BITS >= 8. 14556141cf33Sderaadt */ 14566141cf33Sderaadt scan += 2, match++; 14576141cf33Sderaadt Assert(*scan == *match, "match[2]?"); 14586141cf33Sderaadt 14596141cf33Sderaadt /* We check for insufficient lookahead only every 8th comparison; 14606141cf33Sderaadt * the 256th check will be made at strstart + 258. 14616141cf33Sderaadt */ 14626141cf33Sderaadt do { 14636141cf33Sderaadt } while (*++scan == *++match && *++scan == *++match && 14646141cf33Sderaadt *++scan == *++match && *++scan == *++match && 14656141cf33Sderaadt *++scan == *++match && *++scan == *++match && 14666141cf33Sderaadt *++scan == *++match && *++scan == *++match && 14676141cf33Sderaadt scan < strend); 14686141cf33Sderaadt 1469ddf65acdStb Assert(scan <= s->window + (unsigned)(s->window_size - 1), 1470ddf65acdStb "wild scan"); 14716141cf33Sderaadt 14726141cf33Sderaadt len = MAX_MATCH - (int)(strend - scan); 14736141cf33Sderaadt scan = strend - MAX_MATCH; 14746141cf33Sderaadt 14756141cf33Sderaadt #endif /* UNALIGNED_OK */ 14766141cf33Sderaadt 14776141cf33Sderaadt if (len > best_len) { 14786141cf33Sderaadt s->match_start = cur_match; 14796141cf33Sderaadt best_len = len; 14806141cf33Sderaadt if (len >= nice_match) break; 14816141cf33Sderaadt #ifdef UNALIGNED_OK 14826141cf33Sderaadt scan_end = *(ushf*)(scan + best_len - 1); 14836141cf33Sderaadt #else 14846141cf33Sderaadt scan_end1 = scan[best_len - 1]; 14856141cf33Sderaadt scan_end = scan[best_len]; 14866141cf33Sderaadt #endif 14876141cf33Sderaadt } 14886141cf33Sderaadt } while ((cur_match = prev[cur_match & wmask]) > limit 14896141cf33Sderaadt && --chain_length != 0); 14906141cf33Sderaadt 14916141cf33Sderaadt if ((uInt)best_len <= s->lookahead) return (uInt)best_len; 14926141cf33Sderaadt return s->lookahead; 14936141cf33Sderaadt } 149436f395ceStb 149536f395ceStb #else /* FASTEST */ 14966141cf33Sderaadt 14976141cf33Sderaadt /* --------------------------------------------------------------------------- 149836f395ceStb * Optimized version for FASTEST only 14996141cf33Sderaadt */ 1500cfac609cStb local uInt longest_match(deflate_state *s, IPos cur_match) { 15016141cf33Sderaadt register Bytef *scan = s->window + s->strstart; /* current string */ 15026141cf33Sderaadt register Bytef *match; /* matched string */ 15036141cf33Sderaadt register int len; /* length of current match */ 15046141cf33Sderaadt register Bytef *strend = s->window + s->strstart + MAX_MATCH; 15056141cf33Sderaadt 15066141cf33Sderaadt /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 15076141cf33Sderaadt * It is easy to get rid of this optimization if necessary. 15086141cf33Sderaadt */ 15096141cf33Sderaadt Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 15106141cf33Sderaadt 1511ddf65acdStb Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, 1512ddf65acdStb "need lookahead"); 15136141cf33Sderaadt 15146141cf33Sderaadt Assert(cur_match < s->strstart, "no future"); 15156141cf33Sderaadt 15166141cf33Sderaadt match = s->window + cur_match; 15176141cf33Sderaadt 15186141cf33Sderaadt /* Return failure if the match length is less than 2: 15196141cf33Sderaadt */ 15206141cf33Sderaadt if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; 15216141cf33Sderaadt 15226141cf33Sderaadt /* The check at best_len - 1 can be removed because it will be made 15236141cf33Sderaadt * again later. (This heuristic is not always a win.) 15246141cf33Sderaadt * It is not necessary to compare scan[2] and match[2] since they 15256141cf33Sderaadt * are always equal when the other bytes match, given that 15266141cf33Sderaadt * the hash keys are equal and that HASH_BITS >= 8. 15276141cf33Sderaadt */ 15286141cf33Sderaadt scan += 2, match += 2; 15296141cf33Sderaadt Assert(*scan == *match, "match[2]?"); 15306141cf33Sderaadt 15316141cf33Sderaadt /* We check for insufficient lookahead only every 8th comparison; 15326141cf33Sderaadt * the 256th check will be made at strstart + 258. 15336141cf33Sderaadt */ 15346141cf33Sderaadt do { 15356141cf33Sderaadt } while (*++scan == *++match && *++scan == *++match && 15366141cf33Sderaadt *++scan == *++match && *++scan == *++match && 15376141cf33Sderaadt *++scan == *++match && *++scan == *++match && 15386141cf33Sderaadt *++scan == *++match && *++scan == *++match && 15396141cf33Sderaadt scan < strend); 15406141cf33Sderaadt 15416141cf33Sderaadt Assert(scan <= s->window + (unsigned)(s->window_size - 1), "wild scan"); 15426141cf33Sderaadt 15436141cf33Sderaadt len = MAX_MATCH - (int)(strend - scan); 15446141cf33Sderaadt 15456141cf33Sderaadt if (len < MIN_MATCH) return MIN_MATCH - 1; 15466141cf33Sderaadt 15476141cf33Sderaadt s->match_start = cur_match; 15486141cf33Sderaadt return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; 15496141cf33Sderaadt } 15506141cf33Sderaadt 155136f395ceStb #endif /* FASTEST */ 155236f395ceStb 155336f395ceStb #ifdef ZLIB_DEBUG 155436f395ceStb 155536f395ceStb #define EQUAL 0 155636f395ceStb /* result of memcmp for equal strings */ 155736f395ceStb 15586141cf33Sderaadt /* =========================================================================== 15596141cf33Sderaadt * Check that the match at match_start is indeed a match. 15606141cf33Sderaadt */ 1561cfac609cStb local void check_match(deflate_state *s, IPos start, IPos match, int length) { 15626141cf33Sderaadt /* check that the match is indeed a match */ 1563e1497126Stb Bytef *back = s->window + (int)match, *here = s->window + start; 1564e1497126Stb IPos len = length; 1565e1497126Stb if (match == (IPos)-1) { 1566e1497126Stb /* match starts one byte before the current window -- just compare the 1567e1497126Stb subsequent length-1 bytes */ 1568e1497126Stb back++; 1569e1497126Stb here++; 1570e1497126Stb len--; 1571e1497126Stb } 1572e1497126Stb if (zmemcmp(back, here, len) != EQUAL) { 1573e1497126Stb fprintf(stderr, " start %u, match %d, length %d\n", 1574e1497126Stb start, (int)match, length); 15756141cf33Sderaadt do { 1576e1497126Stb fprintf(stderr, "(%02x %02x)", *back++, *here++); 1577e1497126Stb } while (--len != 0); 15786141cf33Sderaadt z_error("invalid match"); 15796141cf33Sderaadt } 15806141cf33Sderaadt if (z_verbose > 1) { 15816141cf33Sderaadt fprintf(stderr,"\\[%d,%d]", start - match, length); 15826141cf33Sderaadt do { putc(s->window[start++], stderr); } while (--length != 0); 15836141cf33Sderaadt } 15846141cf33Sderaadt } 15856141cf33Sderaadt #else 15866141cf33Sderaadt # define check_match(s, start, match, length) 158736f395ceStb #endif /* ZLIB_DEBUG */ 15886141cf33Sderaadt 15896141cf33Sderaadt /* =========================================================================== 15906141cf33Sderaadt * Flush the current block, with given end-of-file flag. 15916141cf33Sderaadt * IN assertion: strstart is set to the end of the current match. 15926141cf33Sderaadt */ 159336f395ceStb #define FLUSH_BLOCK_ONLY(s, last) { \ 15946141cf33Sderaadt _tr_flush_block(s, (s->block_start >= 0L ? \ 15956141cf33Sderaadt (charf *)&s->window[(unsigned)s->block_start] : \ 15966141cf33Sderaadt (charf *)Z_NULL), \ 15976141cf33Sderaadt (ulg)((long)s->strstart - s->block_start), \ 159836f395ceStb (last)); \ 15996141cf33Sderaadt s->block_start = s->strstart; \ 16006141cf33Sderaadt flush_pending(s->strm); \ 16016141cf33Sderaadt Tracev((stderr,"[FLUSH]")); \ 16026141cf33Sderaadt } 16036141cf33Sderaadt 16046141cf33Sderaadt /* Same but force premature exit if necessary. */ 160536f395ceStb #define FLUSH_BLOCK(s, last) { \ 160636f395ceStb FLUSH_BLOCK_ONLY(s, last); \ 160736f395ceStb if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ 16086141cf33Sderaadt } 16096141cf33Sderaadt 161036f395ceStb /* Maximum stored block length in deflate format (not including header). */ 161136f395ceStb #define MAX_STORED 65535 161236f395ceStb 161336f395ceStb #ifndef MIN 161436f395ceStb /* Minimum of a and b. */ 161536f395ceStb #define MIN(a, b) ((a) > (b) ? (b) : (a)) 161636f395ceStb #endif 161736f395ceStb 16186141cf33Sderaadt /* =========================================================================== 16196141cf33Sderaadt * Copy without compression as much as possible from the input stream, return 16206141cf33Sderaadt * the current block state. 162136f395ceStb * 162236f395ceStb * In case deflateParams() is used to later switch to a non-zero compression 162336f395ceStb * level, s->matches (otherwise unused when storing) keeps track of the number 162436f395ceStb * of hash table slides to perform. If s->matches is 1, then one hash table 162536f395ceStb * slide will be done when switching. If s->matches is 2, the maximum value 162636f395ceStb * allowed here, then the hash table will be cleared, since two or more slides 162736f395ceStb * is the same as a clear. 162836f395ceStb * 162936f395ceStb * deflate_stored() is written to minimize the number of times an input byte is 163036f395ceStb * copied. It is most efficient with large input and output buffers, which 1631ddf65acdStb * maximizes the opportunities to have a single copy from next_in to next_out. 16326141cf33Sderaadt */ 1633cfac609cStb local block_state deflate_stored(deflate_state *s, int flush) { 163436f395ceStb /* Smallest worthy block size when not flushing or finishing. By default 163536f395ceStb * this is 32K. This can be as small as 507 bytes for memLevel == 1. For 163636f395ceStb * large input and output buffers, the stored block size will be larger. 16376141cf33Sderaadt */ 163836f395ceStb unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size); 16396141cf33Sderaadt 164036f395ceStb /* Copy as many min_block or larger stored blocks directly to next_out as 164136f395ceStb * possible. If flushing, copy the remaining available input to next_out as 164236f395ceStb * stored blocks, if there is enough space. 16436141cf33Sderaadt */ 164455dd448dStb int last = 0; 164555dd448dStb unsigned len, left, have; 164636f395ceStb unsigned used = s->strm->avail_in; 164736f395ceStb do { 164836f395ceStb /* Set len to the maximum size block that we can copy directly with the 164936f395ceStb * available input data and output space. Set left to how much of that 165036f395ceStb * would be copied from what's left in the window. 165136f395ceStb */ 165236f395ceStb len = MAX_STORED; /* maximum deflate stored block length */ 165336f395ceStb have = (s->bi_valid + 42) >> 3; /* number of header bytes */ 165436f395ceStb if (s->strm->avail_out < have) /* need room for header */ 165536f395ceStb break; 165636f395ceStb /* maximum stored block length that will fit in avail_out: */ 165736f395ceStb have = s->strm->avail_out - have; 165836f395ceStb left = s->strstart - s->block_start; /* bytes left in window */ 165936f395ceStb if (len > (ulg)left + s->strm->avail_in) 166036f395ceStb len = left + s->strm->avail_in; /* limit len to the input */ 166136f395ceStb if (len > have) 166236f395ceStb len = have; /* limit len to the output */ 166336f395ceStb 166436f395ceStb /* If the stored block would be less than min_block in length, or if 166536f395ceStb * unable to copy all of the available input when flushing, then try 166636f395ceStb * copying to the window and the pending buffer instead. Also don't 166736f395ceStb * write an empty block when flushing -- deflate() does that. 166836f395ceStb */ 166936f395ceStb if (len < min_block && ((len == 0 && flush != Z_FINISH) || 167036f395ceStb flush == Z_NO_FLUSH || 167136f395ceStb len != left + s->strm->avail_in)) 167236f395ceStb break; 167336f395ceStb 167436f395ceStb /* Make a dummy stored block in pending to get the header bytes, 167536f395ceStb * including any pending bits. This also updates the debugging counts. 167636f395ceStb */ 167736f395ceStb last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; 167836f395ceStb _tr_stored_block(s, (char *)0, 0L, last); 167936f395ceStb 168036f395ceStb /* Replace the lengths in the dummy stored block with len. */ 168176eaddfcStb s->pending_buf[s->pending - 4] = (Bytef)len; 168276eaddfcStb s->pending_buf[s->pending - 3] = (Bytef)(len >> 8); 168376eaddfcStb s->pending_buf[s->pending - 2] = (Bytef)~len; 168476eaddfcStb s->pending_buf[s->pending - 1] = (Bytef)(~len >> 8); 168536f395ceStb 168636f395ceStb /* Write the stored block header bytes. */ 168736f395ceStb flush_pending(s->strm); 168836f395ceStb 168936f395ceStb #ifdef ZLIB_DEBUG 169036f395ceStb /* Update debugging counts for the data about to be copied. */ 169136f395ceStb s->compressed_len += len << 3; 169236f395ceStb s->bits_sent += len << 3; 169336f395ceStb #endif 169436f395ceStb 169536f395ceStb /* Copy uncompressed bytes from the window to next_out. */ 169636f395ceStb if (left) { 169736f395ceStb if (left > len) 169836f395ceStb left = len; 169936f395ceStb zmemcpy(s->strm->next_out, s->window + s->block_start, left); 170036f395ceStb s->strm->next_out += left; 170136f395ceStb s->strm->avail_out -= left; 170236f395ceStb s->strm->total_out += left; 170336f395ceStb s->block_start += left; 170436f395ceStb len -= left; 17056141cf33Sderaadt } 170636f395ceStb 170736f395ceStb /* Copy uncompressed bytes directly from next_in to next_out, updating 170836f395ceStb * the check value. 170936f395ceStb */ 171036f395ceStb if (len) { 171136f395ceStb read_buf(s->strm, s->strm->next_out, len); 171236f395ceStb s->strm->next_out += len; 171336f395ceStb s->strm->avail_out -= len; 171436f395ceStb s->strm->total_out += len; 17156141cf33Sderaadt } 171636f395ceStb } while (last == 0); 171736f395ceStb 171836f395ceStb /* Update the sliding window with the last s->w_size bytes of the copied 171936f395ceStb * data, or append all of the copied data to the existing window if less 172036f395ceStb * than s->w_size bytes were copied. Also update the number of bytes to 172136f395ceStb * insert in the hash tables, in the event that deflateParams() switches to 172236f395ceStb * a non-zero compression level. 172336f395ceStb */ 172436f395ceStb used -= s->strm->avail_in; /* number of input bytes directly copied */ 172536f395ceStb if (used) { 172636f395ceStb /* If any input was used, then no unused input remains in the window, 172736f395ceStb * therefore s->block_start == s->strstart. 172836f395ceStb */ 172936f395ceStb if (used >= s->w_size) { /* supplant the previous history */ 173036f395ceStb s->matches = 2; /* clear hash */ 173136f395ceStb zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); 173236f395ceStb s->strstart = s->w_size; 1733a6530658Stb s->insert = s->strstart; 173436f395ceStb } 173536f395ceStb else { 173636f395ceStb if (s->window_size - s->strstart <= used) { 173736f395ceStb /* Slide the window down. */ 173836f395ceStb s->strstart -= s->w_size; 173936f395ceStb zmemcpy(s->window, s->window + s->w_size, s->strstart); 174036f395ceStb if (s->matches < 2) 174136f395ceStb s->matches++; /* add a pending slide_hash() */ 1742a6530658Stb if (s->insert > s->strstart) 1743a6530658Stb s->insert = s->strstart; 174436f395ceStb } 174536f395ceStb zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); 174636f395ceStb s->strstart += used; 1747a6530658Stb s->insert += MIN(used, s->w_size - s->insert); 174836f395ceStb } 174936f395ceStb s->block_start = s->strstart; 175036f395ceStb } 175136f395ceStb if (s->high_water < s->strstart) 175236f395ceStb s->high_water = s->strstart; 175336f395ceStb 175436f395ceStb /* If the last block was written to next_out, then done. */ 1755*0a218225Stb if (last) { 1756*0a218225Stb s->bi_used = 8; 175736f395ceStb return finish_done; 1758*0a218225Stb } 175936f395ceStb 176036f395ceStb /* If flushing and all input has been consumed, then done. */ 176136f395ceStb if (flush != Z_NO_FLUSH && flush != Z_FINISH && 176236f395ceStb s->strm->avail_in == 0 && (long)s->strstart == s->block_start) 176336f395ceStb return block_done; 176436f395ceStb 176536f395ceStb /* Fill the window with any remaining input. */ 1766a6530658Stb have = s->window_size - s->strstart; 176736f395ceStb if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { 176836f395ceStb /* Slide the window down. */ 176936f395ceStb s->block_start -= s->w_size; 177036f395ceStb s->strstart -= s->w_size; 177136f395ceStb zmemcpy(s->window, s->window + s->w_size, s->strstart); 177236f395ceStb if (s->matches < 2) 177336f395ceStb s->matches++; /* add a pending slide_hash() */ 177436f395ceStb have += s->w_size; /* more space now */ 1775a6530658Stb if (s->insert > s->strstart) 1776a6530658Stb s->insert = s->strstart; 177736f395ceStb } 177836f395ceStb if (have > s->strm->avail_in) 177936f395ceStb have = s->strm->avail_in; 178036f395ceStb if (have) { 178136f395ceStb read_buf(s->strm, s->window + s->strstart, have); 178236f395ceStb s->strstart += have; 1783a6530658Stb s->insert += MIN(have, s->w_size - s->insert); 178436f395ceStb } 178536f395ceStb if (s->high_water < s->strstart) 178636f395ceStb s->high_water = s->strstart; 178736f395ceStb 178836f395ceStb /* There was not enough avail_out to write a complete worthy or flushed 178936f395ceStb * stored block to next_out. Write a stored block to pending instead, if we 179036f395ceStb * have enough input for a worthy block, or if flushing and there is enough 179136f395ceStb * room for the remaining input as a stored block in the pending buffer. 179236f395ceStb */ 179336f395ceStb have = (s->bi_valid + 42) >> 3; /* number of header bytes */ 179436f395ceStb /* maximum stored block length that will fit in pending: */ 179536f395ceStb have = MIN(s->pending_buf_size - have, MAX_STORED); 179636f395ceStb min_block = MIN(have, s->w_size); 179736f395ceStb left = s->strstart - s->block_start; 179836f395ceStb if (left >= min_block || 179936f395ceStb ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && 180036f395ceStb s->strm->avail_in == 0 && left <= have)) { 180136f395ceStb len = MIN(left, have); 180236f395ceStb last = flush == Z_FINISH && s->strm->avail_in == 0 && 180336f395ceStb len == left ? 1 : 0; 180436f395ceStb _tr_stored_block(s, (charf *)s->window + s->block_start, len, last); 180536f395ceStb s->block_start += len; 180636f395ceStb flush_pending(s->strm); 180736f395ceStb } 180836f395ceStb 180936f395ceStb /* We've done all we can with the available input and output. */ 1810*0a218225Stb if (last) 1811*0a218225Stb s->bi_used = 8; 181236f395ceStb return last ? finish_started : need_more; 18136141cf33Sderaadt } 18146141cf33Sderaadt 18156141cf33Sderaadt /* =========================================================================== 18166141cf33Sderaadt * Compress as much as possible from the input stream, return the current 18176141cf33Sderaadt * block state. 18186141cf33Sderaadt * This function does not perform lazy evaluation of matches and inserts 18196141cf33Sderaadt * new strings in the dictionary only for unmatched strings or for short 18206141cf33Sderaadt * matches. It is used only for the fast compression options. 18216141cf33Sderaadt */ 1822cfac609cStb local block_state deflate_fast(deflate_state *s, int flush) { 182336f395ceStb IPos hash_head; /* head of the hash chain */ 18246141cf33Sderaadt int bflush; /* set if current block must be flushed */ 18256141cf33Sderaadt 18266141cf33Sderaadt for (;;) { 18276141cf33Sderaadt /* Make sure that we always have enough lookahead, except 18286141cf33Sderaadt * at the end of the input file. We need MAX_MATCH bytes 18296141cf33Sderaadt * for the next match, plus MIN_MATCH bytes to insert the 18306141cf33Sderaadt * string following the next match. 18316141cf33Sderaadt */ 18326141cf33Sderaadt if (s->lookahead < MIN_LOOKAHEAD) { 18336141cf33Sderaadt fill_window(s); 18346141cf33Sderaadt if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 18356141cf33Sderaadt return need_more; 18366141cf33Sderaadt } 18376141cf33Sderaadt if (s->lookahead == 0) break; /* flush the current block */ 18386141cf33Sderaadt } 18396141cf33Sderaadt 18406141cf33Sderaadt /* Insert the string window[strstart .. strstart + 2] in the 18416141cf33Sderaadt * dictionary, and set hash_head to the head of the hash chain: 18426141cf33Sderaadt */ 184336f395ceStb hash_head = NIL; 18446141cf33Sderaadt if (s->lookahead >= MIN_MATCH) { 18456141cf33Sderaadt INSERT_STRING(s, s->strstart, hash_head); 18466141cf33Sderaadt } 18476141cf33Sderaadt 18486141cf33Sderaadt /* Find the longest match, discarding those <= prev_length. 18496141cf33Sderaadt * At this point we have always match_length < MIN_MATCH 18506141cf33Sderaadt */ 18516141cf33Sderaadt if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { 18526141cf33Sderaadt /* To simplify the code, we prevent matches with the string 18536141cf33Sderaadt * of window index 0 (in particular we have to avoid a match 18546141cf33Sderaadt * of the string with itself at the start of the input file). 18556141cf33Sderaadt */ 18566141cf33Sderaadt s->match_length = longest_match (s, hash_head); 185736f395ceStb /* longest_match() sets match_start */ 18586141cf33Sderaadt } 18596141cf33Sderaadt if (s->match_length >= MIN_MATCH) { 18606141cf33Sderaadt check_match(s, s->strstart, s->match_start, s->match_length); 18616141cf33Sderaadt 18626141cf33Sderaadt _tr_tally_dist(s, s->strstart - s->match_start, 18636141cf33Sderaadt s->match_length - MIN_MATCH, bflush); 18646141cf33Sderaadt 18656141cf33Sderaadt s->lookahead -= s->match_length; 18666141cf33Sderaadt 18676141cf33Sderaadt /* Insert new strings in the hash table only if the match length 18686141cf33Sderaadt * is not too large. This saves time but degrades compression. 18696141cf33Sderaadt */ 18706141cf33Sderaadt #ifndef FASTEST 18716141cf33Sderaadt if (s->match_length <= s->max_insert_length && 18726141cf33Sderaadt s->lookahead >= MIN_MATCH) { 18736141cf33Sderaadt s->match_length--; /* string at strstart already in table */ 18746141cf33Sderaadt do { 18756141cf33Sderaadt s->strstart++; 18766141cf33Sderaadt INSERT_STRING(s, s->strstart, hash_head); 18776141cf33Sderaadt /* strstart never exceeds WSIZE-MAX_MATCH, so there are 18786141cf33Sderaadt * always MIN_MATCH bytes ahead. 18796141cf33Sderaadt */ 18806141cf33Sderaadt } while (--s->match_length != 0); 18816141cf33Sderaadt s->strstart++; 18826141cf33Sderaadt } else 18836141cf33Sderaadt #endif 18846141cf33Sderaadt { 18856141cf33Sderaadt s->strstart += s->match_length; 18866141cf33Sderaadt s->match_length = 0; 18876141cf33Sderaadt s->ins_h = s->window[s->strstart]; 18886141cf33Sderaadt UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]); 18896141cf33Sderaadt #if MIN_MATCH != 3 18906141cf33Sderaadt Call UPDATE_HASH() MIN_MATCH-3 more times 18916141cf33Sderaadt #endif 18926141cf33Sderaadt /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not 18936141cf33Sderaadt * matter since it will be recomputed at next deflate call. 18946141cf33Sderaadt */ 18956141cf33Sderaadt } 18966141cf33Sderaadt } else { 18976141cf33Sderaadt /* No match, output a literal byte */ 18986141cf33Sderaadt Tracevv((stderr,"%c", s->window[s->strstart])); 18996141cf33Sderaadt _tr_tally_lit(s, s->window[s->strstart], bflush); 19006141cf33Sderaadt s->lookahead--; 19016141cf33Sderaadt s->strstart++; 19026141cf33Sderaadt } 19036141cf33Sderaadt if (bflush) FLUSH_BLOCK(s, 0); 19046141cf33Sderaadt } 190536f395ceStb s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 190636f395ceStb if (flush == Z_FINISH) { 190736f395ceStb FLUSH_BLOCK(s, 1); 190836f395ceStb return finish_done; 190936f395ceStb } 191056e03632Stb if (s->sym_next) 191136f395ceStb FLUSH_BLOCK(s, 0); 191236f395ceStb return block_done; 19136141cf33Sderaadt } 19146141cf33Sderaadt 19156141cf33Sderaadt #ifndef FASTEST 19166141cf33Sderaadt /* =========================================================================== 19176141cf33Sderaadt * Same as above, but achieves better compression. We use a lazy 19186141cf33Sderaadt * evaluation for matches: a match is finally adopted only if there is 19196141cf33Sderaadt * no better match at the next window position. 19206141cf33Sderaadt */ 1921cfac609cStb local block_state deflate_slow(deflate_state *s, int flush) { 192236f395ceStb IPos hash_head; /* head of hash chain */ 19236141cf33Sderaadt int bflush; /* set if current block must be flushed */ 19246141cf33Sderaadt 19256141cf33Sderaadt /* Process the input block. */ 19266141cf33Sderaadt for (;;) { 19276141cf33Sderaadt /* Make sure that we always have enough lookahead, except 19286141cf33Sderaadt * at the end of the input file. We need MAX_MATCH bytes 19296141cf33Sderaadt * for the next match, plus MIN_MATCH bytes to insert the 19306141cf33Sderaadt * string following the next match. 19316141cf33Sderaadt */ 19326141cf33Sderaadt if (s->lookahead < MIN_LOOKAHEAD) { 19336141cf33Sderaadt fill_window(s); 19346141cf33Sderaadt if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 19356141cf33Sderaadt return need_more; 19366141cf33Sderaadt } 19376141cf33Sderaadt if (s->lookahead == 0) break; /* flush the current block */ 19386141cf33Sderaadt } 19396141cf33Sderaadt 19406141cf33Sderaadt /* Insert the string window[strstart .. strstart + 2] in the 19416141cf33Sderaadt * dictionary, and set hash_head to the head of the hash chain: 19426141cf33Sderaadt */ 194336f395ceStb hash_head = NIL; 19446141cf33Sderaadt if (s->lookahead >= MIN_MATCH) { 19456141cf33Sderaadt INSERT_STRING(s, s->strstart, hash_head); 19466141cf33Sderaadt } 19476141cf33Sderaadt 19486141cf33Sderaadt /* Find the longest match, discarding those <= prev_length. 19496141cf33Sderaadt */ 19506141cf33Sderaadt s->prev_length = s->match_length, s->prev_match = s->match_start; 19516141cf33Sderaadt s->match_length = MIN_MATCH-1; 19526141cf33Sderaadt 19536141cf33Sderaadt if (hash_head != NIL && s->prev_length < s->max_lazy_match && 19546141cf33Sderaadt s->strstart - hash_head <= MAX_DIST(s)) { 19556141cf33Sderaadt /* To simplify the code, we prevent matches with the string 19566141cf33Sderaadt * of window index 0 (in particular we have to avoid a match 19576141cf33Sderaadt * of the string with itself at the start of the input file). 19586141cf33Sderaadt */ 19596141cf33Sderaadt s->match_length = longest_match (s, hash_head); 196036f395ceStb /* longest_match() sets match_start */ 19616141cf33Sderaadt 19626141cf33Sderaadt if (s->match_length <= 5 && (s->strategy == Z_FILTERED 19636141cf33Sderaadt #if TOO_FAR <= 32767 19646141cf33Sderaadt || (s->match_length == MIN_MATCH && 19656141cf33Sderaadt s->strstart - s->match_start > TOO_FAR) 19666141cf33Sderaadt #endif 19676141cf33Sderaadt )) { 19686141cf33Sderaadt 19696141cf33Sderaadt /* If prev_match is also MIN_MATCH, match_start is garbage 19706141cf33Sderaadt * but we will ignore the current match anyway. 19716141cf33Sderaadt */ 19726141cf33Sderaadt s->match_length = MIN_MATCH-1; 19736141cf33Sderaadt } 19746141cf33Sderaadt } 19756141cf33Sderaadt /* If there was a match at the previous step and the current 19766141cf33Sderaadt * match is not better, output the previous match: 19776141cf33Sderaadt */ 19786141cf33Sderaadt if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { 19796141cf33Sderaadt uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 19806141cf33Sderaadt /* Do not insert strings in hash table beyond this. */ 19816141cf33Sderaadt 19826141cf33Sderaadt check_match(s, s->strstart - 1, s->prev_match, s->prev_length); 19836141cf33Sderaadt 19846141cf33Sderaadt _tr_tally_dist(s, s->strstart - 1 - s->prev_match, 19856141cf33Sderaadt s->prev_length - MIN_MATCH, bflush); 19866141cf33Sderaadt 19876141cf33Sderaadt /* Insert in hash table all strings up to the end of the match. 19886141cf33Sderaadt * strstart - 1 and strstart are already inserted. If there is not 19896141cf33Sderaadt * enough lookahead, the last two strings are not inserted in 19906141cf33Sderaadt * the hash table. 19916141cf33Sderaadt */ 19926141cf33Sderaadt s->lookahead -= s->prev_length - 1; 19936141cf33Sderaadt s->prev_length -= 2; 19946141cf33Sderaadt do { 19956141cf33Sderaadt if (++s->strstart <= max_insert) { 19966141cf33Sderaadt INSERT_STRING(s, s->strstart, hash_head); 19976141cf33Sderaadt } 19986141cf33Sderaadt } while (--s->prev_length != 0); 19996141cf33Sderaadt s->match_available = 0; 20006141cf33Sderaadt s->match_length = MIN_MATCH-1; 20016141cf33Sderaadt s->strstart++; 20026141cf33Sderaadt 20036141cf33Sderaadt if (bflush) FLUSH_BLOCK(s, 0); 20046141cf33Sderaadt 20056141cf33Sderaadt } else if (s->match_available) { 20066141cf33Sderaadt /* If there was no match at the previous position, output a 20076141cf33Sderaadt * single literal. If there was a match but the current match 20086141cf33Sderaadt * is longer, truncate the previous match to a single literal. 20096141cf33Sderaadt */ 20106141cf33Sderaadt Tracevv((stderr,"%c", s->window[s->strstart - 1])); 20116141cf33Sderaadt _tr_tally_lit(s, s->window[s->strstart - 1], bflush); 20126141cf33Sderaadt if (bflush) { 20136141cf33Sderaadt FLUSH_BLOCK_ONLY(s, 0); 20146141cf33Sderaadt } 20156141cf33Sderaadt s->strstart++; 20166141cf33Sderaadt s->lookahead--; 20176141cf33Sderaadt if (s->strm->avail_out == 0) return need_more; 20186141cf33Sderaadt } else { 20196141cf33Sderaadt /* There is no previous match to compare with, wait for 20206141cf33Sderaadt * the next step to decide. 20216141cf33Sderaadt */ 20226141cf33Sderaadt s->match_available = 1; 20236141cf33Sderaadt s->strstart++; 20246141cf33Sderaadt s->lookahead--; 20256141cf33Sderaadt } 20266141cf33Sderaadt } 20276141cf33Sderaadt Assert (flush != Z_NO_FLUSH, "no flush?"); 20286141cf33Sderaadt if (s->match_available) { 20296141cf33Sderaadt Tracevv((stderr,"%c", s->window[s->strstart - 1])); 20306141cf33Sderaadt _tr_tally_lit(s, s->window[s->strstart - 1], bflush); 20316141cf33Sderaadt s->match_available = 0; 20326141cf33Sderaadt } 203336f395ceStb s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 203436f395ceStb if (flush == Z_FINISH) { 203536f395ceStb FLUSH_BLOCK(s, 1); 203636f395ceStb return finish_done; 203736f395ceStb } 203856e03632Stb if (s->sym_next) 203936f395ceStb FLUSH_BLOCK(s, 0); 204036f395ceStb return block_done; 20416141cf33Sderaadt } 20426141cf33Sderaadt #endif /* FASTEST */ 20436141cf33Sderaadt 20446141cf33Sderaadt /* =========================================================================== 20456141cf33Sderaadt * For Z_RLE, simply look for runs of bytes, generate matches only of distance 20466141cf33Sderaadt * one. Do not maintain a hash table. (It will be regenerated if this run of 20476141cf33Sderaadt * deflate switches away from Z_RLE.) 20486141cf33Sderaadt */ 2049cfac609cStb local block_state deflate_rle(deflate_state *s, int flush) { 20506141cf33Sderaadt int bflush; /* set if current block must be flushed */ 20516141cf33Sderaadt uInt prev; /* byte at distance one to match */ 205236f395ceStb Bytef *scan, *strend; /* scan goes up to strend for length of run */ 20536141cf33Sderaadt 20546141cf33Sderaadt for (;;) { 20556141cf33Sderaadt /* Make sure that we always have enough lookahead, except 20566141cf33Sderaadt * at the end of the input file. We need MAX_MATCH bytes 205736f395ceStb * for the longest run, plus one for the unrolled loop. 20586141cf33Sderaadt */ 205936f395ceStb if (s->lookahead <= MAX_MATCH) { 20606141cf33Sderaadt fill_window(s); 206136f395ceStb if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { 20626141cf33Sderaadt return need_more; 20636141cf33Sderaadt } 20646141cf33Sderaadt if (s->lookahead == 0) break; /* flush the current block */ 20656141cf33Sderaadt } 20666141cf33Sderaadt 20676141cf33Sderaadt /* See how many times the previous byte repeats */ 206836f395ceStb s->match_length = 0; 206936f395ceStb if (s->lookahead >= MIN_MATCH && s->strstart > 0) { 20706141cf33Sderaadt scan = s->window + s->strstart - 1; 207136f395ceStb prev = *scan; 207236f395ceStb if (prev == *++scan && prev == *++scan && prev == *++scan) { 207336f395ceStb strend = s->window + s->strstart + MAX_MATCH; 20746141cf33Sderaadt do { 207536f395ceStb } while (prev == *++scan && prev == *++scan && 207636f395ceStb prev == *++scan && prev == *++scan && 207736f395ceStb prev == *++scan && prev == *++scan && 207836f395ceStb prev == *++scan && prev == *++scan && 207936f395ceStb scan < strend); 208036f395ceStb s->match_length = MAX_MATCH - (uInt)(strend - scan); 208136f395ceStb if (s->match_length > s->lookahead) 208236f395ceStb s->match_length = s->lookahead; 208336f395ceStb } 2084ddf65acdStb Assert(scan <= s->window + (uInt)(s->window_size - 1), 2085ddf65acdStb "wild scan"); 20866141cf33Sderaadt } 20876141cf33Sderaadt 20886141cf33Sderaadt /* Emit match if have run of MIN_MATCH or longer, else emit literal */ 208936f395ceStb if (s->match_length >= MIN_MATCH) { 209036f395ceStb check_match(s, s->strstart, s->strstart - 1, s->match_length); 209136f395ceStb 209236f395ceStb _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); 209336f395ceStb 209436f395ceStb s->lookahead -= s->match_length; 209536f395ceStb s->strstart += s->match_length; 209636f395ceStb s->match_length = 0; 20976141cf33Sderaadt } else { 20986141cf33Sderaadt /* No match, output a literal byte */ 20996141cf33Sderaadt Tracevv((stderr,"%c", s->window[s->strstart])); 21006141cf33Sderaadt _tr_tally_lit(s, s->window[s->strstart], bflush); 21016141cf33Sderaadt s->lookahead--; 21026141cf33Sderaadt s->strstart++; 21036141cf33Sderaadt } 21046141cf33Sderaadt if (bflush) FLUSH_BLOCK(s, 0); 21056141cf33Sderaadt } 210636f395ceStb s->insert = 0; 210736f395ceStb if (flush == Z_FINISH) { 210836f395ceStb FLUSH_BLOCK(s, 1); 210936f395ceStb return finish_done; 21106141cf33Sderaadt } 211156e03632Stb if (s->sym_next) 211236f395ceStb FLUSH_BLOCK(s, 0); 211336f395ceStb return block_done; 211436f395ceStb } 211536f395ceStb 211636f395ceStb /* =========================================================================== 211736f395ceStb * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. 211836f395ceStb * (It will be regenerated if this run of deflate switches away from Huffman.) 211936f395ceStb */ 2120cfac609cStb local block_state deflate_huff(deflate_state *s, int flush) { 212136f395ceStb int bflush; /* set if current block must be flushed */ 212236f395ceStb 212336f395ceStb for (;;) { 212436f395ceStb /* Make sure that we have a literal to write. */ 212536f395ceStb if (s->lookahead == 0) { 212636f395ceStb fill_window(s); 212736f395ceStb if (s->lookahead == 0) { 212836f395ceStb if (flush == Z_NO_FLUSH) 212936f395ceStb return need_more; 213036f395ceStb break; /* flush the current block */ 213136f395ceStb } 213236f395ceStb } 213336f395ceStb 213436f395ceStb /* Output a literal byte */ 213536f395ceStb s->match_length = 0; 213636f395ceStb Tracevv((stderr,"%c", s->window[s->strstart])); 213736f395ceStb _tr_tally_lit(s, s->window[s->strstart], bflush); 213836f395ceStb s->lookahead--; 213936f395ceStb s->strstart++; 214036f395ceStb if (bflush) FLUSH_BLOCK(s, 0); 214136f395ceStb } 214236f395ceStb s->insert = 0; 214336f395ceStb if (flush == Z_FINISH) { 214436f395ceStb FLUSH_BLOCK(s, 1); 214536f395ceStb return finish_done; 214636f395ceStb } 214756e03632Stb if (s->sym_next) 214836f395ceStb FLUSH_BLOCK(s, 0); 214936f395ceStb return block_done; 215036f395ceStb } 2151