1*96c32821Schristos /* $NetBSD: deflate.c,v 1.7 2024/09/22 19:12:27 christos Exp $ */ 2aaf4ece6Schristos 3aaf4ece6Schristos /* deflate.c -- compress data using the deflation algorithm 4*96c32821Schristos * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler 5aaf4ece6Schristos * For conditions of distribution and use, see copyright notice in zlib.h 6aaf4ece6Schristos */ 7aaf4ece6Schristos 8aaf4ece6Schristos /* 9aaf4ece6Schristos * ALGORITHM 10aaf4ece6Schristos * 11aaf4ece6Schristos * The "deflation" process depends on being able to identify portions 12aaf4ece6Schristos * of the input text which are identical to earlier input (within a 13aaf4ece6Schristos * sliding window trailing behind the input currently being processed). 14aaf4ece6Schristos * 15aaf4ece6Schristos * The most straightforward technique turns out to be the fastest for 16aaf4ece6Schristos * most input files: try all possible matches and select the longest. 17aaf4ece6Schristos * The key feature of this algorithm is that insertions into the string 18aaf4ece6Schristos * dictionary are very simple and thus fast, and deletions are avoided 19aaf4ece6Schristos * completely. Insertions are performed at each input character, whereas 20aaf4ece6Schristos * string matches are performed only when the previous match ends. So it 21aaf4ece6Schristos * is preferable to spend more time in matches to allow very fast string 22aaf4ece6Schristos * insertions and avoid deletions. The matching algorithm for small 23aaf4ece6Schristos * strings is inspired from that of Rabin & Karp. A brute force approach 24aaf4ece6Schristos * is used to find longer strings when a small match has been found. 25aaf4ece6Schristos * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze 26aaf4ece6Schristos * (by Leonid Broukhis). 27aaf4ece6Schristos * A previous version of this file used a more sophisticated algorithm 28aaf4ece6Schristos * (by Fiala and Greene) which is guaranteed to run in linear amortized 29aaf4ece6Schristos * time, but has a larger average cost, uses more memory and is patented. 30aaf4ece6Schristos * However the F&G algorithm may be faster for some highly redundant 31aaf4ece6Schristos * files if the parameter max_chain_length (described below) is too large. 32aaf4ece6Schristos * 33aaf4ece6Schristos * ACKNOWLEDGEMENTS 34aaf4ece6Schristos * 35aaf4ece6Schristos * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and 36aaf4ece6Schristos * I found it in 'freeze' written by Leonid Broukhis. 37aaf4ece6Schristos * Thanks to many people for bug reports and testing. 38aaf4ece6Schristos * 39aaf4ece6Schristos * REFERENCES 40aaf4ece6Schristos * 41aaf4ece6Schristos * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". 426db8c6e9Schristos * Available in http://tools.ietf.org/html/rfc1951 43aaf4ece6Schristos * 44aaf4ece6Schristos * A description of the Rabin and Karp algorithm is given in the book 45aaf4ece6Schristos * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. 46aaf4ece6Schristos * 47aaf4ece6Schristos * Fiala,E.R., and Greene,D.H. 48aaf4ece6Schristos * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 49aaf4ece6Schristos * 50aaf4ece6Schristos */ 51aaf4ece6Schristos 523b1253e8Schristos /* @(#) Id */ 53aaf4ece6Schristos 54aaf4ece6Schristos #include "deflate.h" 55aaf4ece6Schristos 56aaf4ece6Schristos const char deflate_copyright[] = 57*96c32821Schristos " deflate 1.3.1 Copyright 1995-2024 Jean-loup Gailly and Mark Adler "; 58aaf4ece6Schristos /* 59aaf4ece6Schristos If you use the zlib library in a product, an acknowledgment is welcome 60aaf4ece6Schristos in the documentation of your product. If for some reason you cannot 61aaf4ece6Schristos include such an acknowledgment, I would appreciate that you keep this 62aaf4ece6Schristos copyright string in the executable of your product. 63aaf4ece6Schristos */ 64aaf4ece6Schristos 65aaf4ece6Schristos typedef enum { 66aaf4ece6Schristos need_more, /* block not completed, need more input or more output */ 67aaf4ece6Schristos block_done, /* block flush performed */ 68aaf4ece6Schristos finish_started, /* finish started, need only more output at next deflate */ 69aaf4ece6Schristos finish_done /* finish done, accept no more input or output */ 70aaf4ece6Schristos } block_state; 71aaf4ece6Schristos 72*96c32821Schristos typedef block_state (*compress_func)(deflate_state *s, int flush); 73aaf4ece6Schristos /* Compression function. Returns the block state after the call. */ 74aaf4ece6Schristos 75*96c32821Schristos local block_state deflate_stored(deflate_state *s, int flush); 76*96c32821Schristos local block_state deflate_fast(deflate_state *s, int flush); 77aaf4ece6Schristos #ifndef FASTEST 78*96c32821Schristos local block_state deflate_slow(deflate_state *s, int flush); 79aaf4ece6Schristos #endif 80*96c32821Schristos local block_state deflate_rle(deflate_state *s, int flush); 81*96c32821Schristos local block_state deflate_huff(deflate_state *s, int flush); 82aaf4ece6Schristos 83aaf4ece6Schristos /* =========================================================================== 84aaf4ece6Schristos * Local data 85aaf4ece6Schristos */ 86aaf4ece6Schristos 87aaf4ece6Schristos #define NIL 0 88aaf4ece6Schristos /* Tail of hash chains */ 89aaf4ece6Schristos 90aaf4ece6Schristos #ifndef TOO_FAR 91aaf4ece6Schristos # define TOO_FAR 4096 92aaf4ece6Schristos #endif 93aaf4ece6Schristos /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ 94aaf4ece6Schristos 95aaf4ece6Schristos /* Values for max_lazy_match, good_match and max_chain_length, depending on 96aaf4ece6Schristos * the desired pack level (0..9). The values given below have been tuned to 97aaf4ece6Schristos * exclude worst case performance for pathological files. Better values may be 98aaf4ece6Schristos * found for specific files. 99aaf4ece6Schristos */ 100aaf4ece6Schristos typedef struct config_s { 101aaf4ece6Schristos ush good_length; /* reduce lazy search above this match length */ 102aaf4ece6Schristos ush max_lazy; /* do not perform lazy search above this match length */ 103aaf4ece6Schristos ush nice_length; /* quit search above this match length */ 104aaf4ece6Schristos ush max_chain; 105aaf4ece6Schristos compress_func func; 106aaf4ece6Schristos } config; 107aaf4ece6Schristos 108aaf4ece6Schristos #ifdef FASTEST 109aaf4ece6Schristos local const config configuration_table[2] = { 110aaf4ece6Schristos /* good lazy nice chain */ 111aaf4ece6Schristos /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 112aaf4ece6Schristos /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ 113aaf4ece6Schristos #else 114aaf4ece6Schristos local const config configuration_table[10] = { 115aaf4ece6Schristos /* good lazy nice chain */ 116aaf4ece6Schristos /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 117aaf4ece6Schristos /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ 118aaf4ece6Schristos /* 2 */ {4, 5, 16, 8, deflate_fast}, 119aaf4ece6Schristos /* 3 */ {4, 6, 32, 32, deflate_fast}, 120aaf4ece6Schristos 121aaf4ece6Schristos /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ 122aaf4ece6Schristos /* 5 */ {8, 16, 32, 32, deflate_slow}, 123aaf4ece6Schristos /* 6 */ {8, 16, 128, 128, deflate_slow}, 124aaf4ece6Schristos /* 7 */ {8, 32, 128, 256, deflate_slow}, 125aaf4ece6Schristos /* 8 */ {32, 128, 258, 1024, deflate_slow}, 126aaf4ece6Schristos /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ 127aaf4ece6Schristos #endif 128aaf4ece6Schristos 129aaf4ece6Schristos /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 130aaf4ece6Schristos * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 131aaf4ece6Schristos * meaning. 132aaf4ece6Schristos */ 133aaf4ece6Schristos 1346db8c6e9Schristos /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ 1356db8c6e9Schristos #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0)) 136aaf4ece6Schristos 137aaf4ece6Schristos /* =========================================================================== 138aaf4ece6Schristos * Update a hash value with the given input byte 1396db8c6e9Schristos * IN assertion: all calls to UPDATE_HASH are made with consecutive input 1406db8c6e9Schristos * characters, so that a running hash key can be computed from the previous 1416db8c6e9Schristos * key instead of complete recalculation each time. 142aaf4ece6Schristos */ 143aaf4ece6Schristos #define UPDATE_HASH(s,h,c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask) 144aaf4ece6Schristos 145aaf4ece6Schristos 146aaf4ece6Schristos /* =========================================================================== 147aaf4ece6Schristos * Insert string str in the dictionary and set match_head to the previous head 148aaf4ece6Schristos * of the hash chain (the most recent string with same hash key). Return 149aaf4ece6Schristos * the previous length of the hash chain. 150aaf4ece6Schristos * If this file is compiled with -DFASTEST, the compression level is forced 151aaf4ece6Schristos * to 1, and no hash chains are maintained. 1526db8c6e9Schristos * IN assertion: all calls to INSERT_STRING are made with consecutive input 1536db8c6e9Schristos * characters and the first MIN_MATCH bytes of str are valid (except for 1546db8c6e9Schristos * the last MIN_MATCH-1 bytes of the input file). 155aaf4ece6Schristos */ 156aaf4ece6Schristos #ifdef FASTEST 157aaf4ece6Schristos #define INSERT_STRING(s, str, match_head) \ 158aaf4ece6Schristos (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 159aaf4ece6Schristos match_head = s->head[s->ins_h], \ 160aaf4ece6Schristos s->head[s->ins_h] = (Pos)(str)) 161aaf4ece6Schristos #else 162aaf4ece6Schristos #define INSERT_STRING(s, str, match_head) \ 163aaf4ece6Schristos (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 164aaf4ece6Schristos match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ 165aaf4ece6Schristos s->head[s->ins_h] = (Pos)(str)) 166aaf4ece6Schristos #endif 167aaf4ece6Schristos 168aaf4ece6Schristos /* =========================================================================== 169aaf4ece6Schristos * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 170aaf4ece6Schristos * prev[] will be initialized on the fly. 171aaf4ece6Schristos */ 172aaf4ece6Schristos #define CLEAR_HASH(s) \ 1733b1253e8Schristos do { \ 174aaf4ece6Schristos s->head[s->hash_size - 1] = NIL; \ 1753b1253e8Schristos zmemzero((Bytef *)s->head, \ 1763b1253e8Schristos (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \ 1773b1253e8Schristos } while (0) 178aaf4ece6Schristos 1796db8c6e9Schristos /* =========================================================================== 1806db8c6e9Schristos * Slide the hash table when sliding the window down (could be avoided with 32 1816db8c6e9Schristos * bit values at the expense of memory usage). We slide even when level == 0 to 1826db8c6e9Schristos * keep the hash table consistent if we switch back to level > 0 later. 1836db8c6e9Schristos */ 184*96c32821Schristos #if defined(__has_feature) 185*96c32821Schristos # if __has_feature(memory_sanitizer) 186*96c32821Schristos __attribute__((no_sanitize("memory"))) 187*96c32821Schristos # endif 188*96c32821Schristos #endif 189*96c32821Schristos local void slide_hash(deflate_state *s) { 1906db8c6e9Schristos unsigned n, m; 1916db8c6e9Schristos Posf *p; 1926db8c6e9Schristos uInt wsize = s->w_size; 1936db8c6e9Schristos 1946db8c6e9Schristos n = s->hash_size; 1956db8c6e9Schristos p = &s->head[n]; 1966db8c6e9Schristos do { 1976db8c6e9Schristos m = *--p; 1986db8c6e9Schristos *p = (Pos)(m >= wsize ? m - wsize : NIL); 1996db8c6e9Schristos } while (--n); 2006db8c6e9Schristos n = wsize; 2016db8c6e9Schristos #ifndef FASTEST 2026db8c6e9Schristos p = &s->prev[n]; 2036db8c6e9Schristos do { 2046db8c6e9Schristos m = *--p; 2056db8c6e9Schristos *p = (Pos)(m >= wsize ? m - wsize : NIL); 2066db8c6e9Schristos /* If n is not on any hash chain, prev[n] is garbage but 2076db8c6e9Schristos * its value will never be used. 2086db8c6e9Schristos */ 2096db8c6e9Schristos } while (--n); 2106db8c6e9Schristos #endif 2116db8c6e9Schristos } 2126db8c6e9Schristos 213*96c32821Schristos /* =========================================================================== 214*96c32821Schristos * Read a new buffer from the current input stream, update the adler32 215*96c32821Schristos * and total number of bytes read. All deflate() input goes through 216*96c32821Schristos * this function so some applications may wish to modify it to avoid 217*96c32821Schristos * allocating a large strm->next_in buffer and copying from it. 218*96c32821Schristos * (See also flush_pending()). 219*96c32821Schristos */ 220*96c32821Schristos local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size) { 221*96c32821Schristos unsigned len = strm->avail_in; 222*96c32821Schristos 223*96c32821Schristos if (len > size) len = size; 224*96c32821Schristos if (len == 0) return 0; 225*96c32821Schristos 226*96c32821Schristos strm->avail_in -= len; 227*96c32821Schristos 228*96c32821Schristos zmemcpy(buf, strm->next_in, len); 229*96c32821Schristos if (strm->state->wrap == 1) { 230*96c32821Schristos strm->adler = adler32(strm->adler, buf, len); 231*96c32821Schristos } 232*96c32821Schristos #ifdef GZIP 233*96c32821Schristos else if (strm->state->wrap == 2) { 234*96c32821Schristos strm->adler = crc32(strm->adler, buf, len); 235*96c32821Schristos } 236*96c32821Schristos #endif 237*96c32821Schristos strm->next_in += len; 238*96c32821Schristos strm->total_in += len; 239*96c32821Schristos 240*96c32821Schristos return len; 241*96c32821Schristos } 242*96c32821Schristos 243*96c32821Schristos /* =========================================================================== 244*96c32821Schristos * Fill the window when the lookahead becomes insufficient. 245*96c32821Schristos * Updates strstart and lookahead. 246*96c32821Schristos * 247*96c32821Schristos * IN assertion: lookahead < MIN_LOOKAHEAD 248*96c32821Schristos * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD 249*96c32821Schristos * At least one byte has been read, or avail_in == 0; reads are 250*96c32821Schristos * performed for at least two bytes (required for the zip translate_eol 251*96c32821Schristos * option -- not supported here). 252*96c32821Schristos */ 253*96c32821Schristos local void fill_window(deflate_state *s) { 254*96c32821Schristos unsigned n; 255*96c32821Schristos unsigned more; /* Amount of free space at the end of the window. */ 256*96c32821Schristos uInt wsize = s->w_size; 257*96c32821Schristos 258*96c32821Schristos Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); 259*96c32821Schristos 260*96c32821Schristos do { 261*96c32821Schristos more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); 262*96c32821Schristos 263*96c32821Schristos /* Deal with !@#$% 64K limit: */ 264*96c32821Schristos if (sizeof(int) <= 2) { 265*96c32821Schristos if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 266*96c32821Schristos more = wsize; 267*96c32821Schristos 268*96c32821Schristos } else if (more == (unsigned)(-1)) { 269*96c32821Schristos /* Very unlikely, but possible on 16 bit machine if 270*96c32821Schristos * strstart == 0 && lookahead == 1 (input done a byte at time) 271*96c32821Schristos */ 272*96c32821Schristos more--; 273*96c32821Schristos } 274*96c32821Schristos } 275*96c32821Schristos 276*96c32821Schristos /* If the window is almost full and there is insufficient lookahead, 277*96c32821Schristos * move the upper half to the lower one to make room in the upper half. 278*96c32821Schristos */ 279*96c32821Schristos if (s->strstart >= wsize + MAX_DIST(s)) { 280*96c32821Schristos 281*96c32821Schristos zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more); 282*96c32821Schristos s->match_start -= wsize; 283*96c32821Schristos s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ 284*96c32821Schristos s->block_start -= (long) wsize; 285*96c32821Schristos if (s->insert > s->strstart) 286*96c32821Schristos s->insert = s->strstart; 287*96c32821Schristos slide_hash(s); 288*96c32821Schristos more += wsize; 289*96c32821Schristos } 290*96c32821Schristos if (s->strm->avail_in == 0) break; 291*96c32821Schristos 292*96c32821Schristos /* If there was no sliding: 293*96c32821Schristos * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && 294*96c32821Schristos * more == window_size - lookahead - strstart 295*96c32821Schristos * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) 296*96c32821Schristos * => more >= window_size - 2*WSIZE + 2 297*96c32821Schristos * In the BIG_MEM or MMAP case (not yet supported), 298*96c32821Schristos * window_size == input_size + MIN_LOOKAHEAD && 299*96c32821Schristos * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. 300*96c32821Schristos * Otherwise, window_size == 2*WSIZE so more >= 2. 301*96c32821Schristos * If there was sliding, more >= WSIZE. So in all cases, more >= 2. 302*96c32821Schristos */ 303*96c32821Schristos Assert(more >= 2, "more < 2"); 304*96c32821Schristos 305*96c32821Schristos n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); 306*96c32821Schristos s->lookahead += n; 307*96c32821Schristos 308*96c32821Schristos /* Initialize the hash value now that we have some input: */ 309*96c32821Schristos if (s->lookahead + s->insert >= MIN_MATCH) { 310*96c32821Schristos uInt str = s->strstart - s->insert; 311*96c32821Schristos s->ins_h = s->window[str]; 312*96c32821Schristos UPDATE_HASH(s, s->ins_h, s->window[str + 1]); 313*96c32821Schristos #if MIN_MATCH != 3 314*96c32821Schristos Call UPDATE_HASH() MIN_MATCH-3 more times 315*96c32821Schristos #endif 316*96c32821Schristos while (s->insert) { 317*96c32821Schristos UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); 318*96c32821Schristos #ifndef FASTEST 319*96c32821Schristos s->prev[str & s->w_mask] = s->head[s->ins_h]; 320*96c32821Schristos #endif 321*96c32821Schristos s->head[s->ins_h] = (Pos)str; 322*96c32821Schristos str++; 323*96c32821Schristos s->insert--; 324*96c32821Schristos if (s->lookahead + s->insert < MIN_MATCH) 325*96c32821Schristos break; 326*96c32821Schristos } 327*96c32821Schristos } 328*96c32821Schristos /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, 329*96c32821Schristos * but this is not important since only literal bytes will be emitted. 330*96c32821Schristos */ 331*96c32821Schristos 332*96c32821Schristos } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); 333*96c32821Schristos 334*96c32821Schristos /* If the WIN_INIT bytes after the end of the current data have never been 335*96c32821Schristos * written, then zero those bytes in order to avoid memory check reports of 336*96c32821Schristos * the use of uninitialized (or uninitialised as Julian writes) bytes by 337*96c32821Schristos * the longest match routines. Update the high water mark for the next 338*96c32821Schristos * time through here. WIN_INIT is set to MAX_MATCH since the longest match 339*96c32821Schristos * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. 340*96c32821Schristos */ 341*96c32821Schristos if (s->high_water < s->window_size) { 342*96c32821Schristos ulg curr = s->strstart + (ulg)(s->lookahead); 343*96c32821Schristos ulg init; 344*96c32821Schristos 345*96c32821Schristos if (s->high_water < curr) { 346*96c32821Schristos /* Previous high water mark below current data -- zero WIN_INIT 347*96c32821Schristos * bytes or up to end of window, whichever is less. 348*96c32821Schristos */ 349*96c32821Schristos init = s->window_size - curr; 350*96c32821Schristos if (init > WIN_INIT) 351*96c32821Schristos init = WIN_INIT; 352*96c32821Schristos zmemzero(s->window + curr, (unsigned)init); 353*96c32821Schristos s->high_water = curr + init; 354*96c32821Schristos } 355*96c32821Schristos else if (s->high_water < (ulg)curr + WIN_INIT) { 356*96c32821Schristos /* High water mark at or above current data, but below current data 357*96c32821Schristos * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up 358*96c32821Schristos * to end of window, whichever is less. 359*96c32821Schristos */ 360*96c32821Schristos init = (ulg)curr + WIN_INIT - s->high_water; 361*96c32821Schristos if (init > s->window_size - s->high_water) 362*96c32821Schristos init = s->window_size - s->high_water; 363*96c32821Schristos zmemzero(s->window + s->high_water, (unsigned)init); 364*96c32821Schristos s->high_water += init; 365*96c32821Schristos } 366*96c32821Schristos } 367*96c32821Schristos 368*96c32821Schristos Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, 369*96c32821Schristos "not enough room for search"); 370*96c32821Schristos } 371*96c32821Schristos 372aaf4ece6Schristos /* ========================================================================= */ 373*96c32821Schristos int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, 374*96c32821Schristos int stream_size) { 375aaf4ece6Schristos return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 376aaf4ece6Schristos Z_DEFAULT_STRATEGY, version, stream_size); 377aaf4ece6Schristos /* To do: ignore strm->next_in if we use it as window */ 378aaf4ece6Schristos } 379aaf4ece6Schristos 380aaf4ece6Schristos /* ========================================================================= */ 381*96c32821Schristos int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, 382*96c32821Schristos int windowBits, int memLevel, int strategy, 383*96c32821Schristos const char *version, int stream_size) { 384aaf4ece6Schristos deflate_state *s; 385aaf4ece6Schristos int wrap = 1; 386aaf4ece6Schristos static const char my_version[] = ZLIB_VERSION; 387aaf4ece6Schristos 388aaf4ece6Schristos if (version == Z_NULL || version[0] != my_version[0] || 389aaf4ece6Schristos stream_size != sizeof(z_stream)) { 390aaf4ece6Schristos return Z_VERSION_ERROR; 391aaf4ece6Schristos } 392aaf4ece6Schristos if (strm == Z_NULL) return Z_STREAM_ERROR; 393aaf4ece6Schristos 394aaf4ece6Schristos strm->msg = Z_NULL; 395aaf4ece6Schristos if (strm->zalloc == (alloc_func)0) { 3966db8c6e9Schristos #ifdef Z_SOLO 3976db8c6e9Schristos return Z_STREAM_ERROR; 3986db8c6e9Schristos #else 399aaf4ece6Schristos strm->zalloc = zcalloc; 400aaf4ece6Schristos strm->opaque = (voidpf)0; 4016db8c6e9Schristos #endif 402aaf4ece6Schristos } 4036db8c6e9Schristos if (strm->zfree == (free_func)0) 4046db8c6e9Schristos #ifdef Z_SOLO 4056db8c6e9Schristos return Z_STREAM_ERROR; 4066db8c6e9Schristos #else 4076db8c6e9Schristos strm->zfree = zcfree; 4086db8c6e9Schristos #endif 409aaf4ece6Schristos 410aaf4ece6Schristos #ifdef FASTEST 411aaf4ece6Schristos if (level != 0) level = 1; 412aaf4ece6Schristos #else 413aaf4ece6Schristos if (level == Z_DEFAULT_COMPRESSION) level = 6; 414aaf4ece6Schristos #endif 415aaf4ece6Schristos 416aaf4ece6Schristos if (windowBits < 0) { /* suppress zlib wrapper */ 417aaf4ece6Schristos wrap = 0; 4183b1253e8Schristos if (windowBits < -15) 4193b1253e8Schristos return Z_STREAM_ERROR; 420aaf4ece6Schristos windowBits = -windowBits; 421aaf4ece6Schristos } 422aaf4ece6Schristos #ifdef GZIP 423aaf4ece6Schristos else if (windowBits > 15) { 424aaf4ece6Schristos wrap = 2; /* write gzip wrapper instead */ 425aaf4ece6Schristos windowBits -= 16; 426aaf4ece6Schristos } 427aaf4ece6Schristos #endif 428aaf4ece6Schristos if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || 429aaf4ece6Schristos windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || 4306db8c6e9Schristos strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) { 431aaf4ece6Schristos return Z_STREAM_ERROR; 432aaf4ece6Schristos } 433aaf4ece6Schristos if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ 434aaf4ece6Schristos s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); 435aaf4ece6Schristos if (s == Z_NULL) return Z_MEM_ERROR; 436aaf4ece6Schristos strm->state = (struct internal_state FAR *)s; 437aaf4ece6Schristos s->strm = strm; 4386db8c6e9Schristos s->status = INIT_STATE; /* to pass state test in deflateReset() */ 439aaf4ece6Schristos 440aaf4ece6Schristos s->wrap = wrap; 441aaf4ece6Schristos s->gzhead = Z_NULL; 4426db8c6e9Schristos s->w_bits = (uInt)windowBits; 443aaf4ece6Schristos s->w_size = 1 << s->w_bits; 444aaf4ece6Schristos s->w_mask = s->w_size - 1; 445aaf4ece6Schristos 4466db8c6e9Schristos s->hash_bits = (uInt)memLevel + 7; 447aaf4ece6Schristos s->hash_size = 1 << s->hash_bits; 448aaf4ece6Schristos s->hash_mask = s->hash_size - 1; 449aaf4ece6Schristos s->hash_shift = ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH); 450aaf4ece6Schristos 451aaf4ece6Schristos s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); 452aaf4ece6Schristos s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 453aaf4ece6Schristos s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 454aaf4ece6Schristos 4556db8c6e9Schristos s->high_water = 0; /* nothing written to s->window yet */ 4566db8c6e9Schristos 457aaf4ece6Schristos s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 458aaf4ece6Schristos 4590362f707Swiz /* We overlay pending_buf and sym_buf. This works since the average size 4600362f707Swiz * for length/distance pairs over any compressed block is assured to be 31 4610362f707Swiz * bits or less. 4620362f707Swiz * 4630362f707Swiz * Analysis: The longest fixed codes are a length code of 8 bits plus 5 4640362f707Swiz * extra bits, for lengths 131 to 257. The longest fixed distance codes are 4650362f707Swiz * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest 4660362f707Swiz * possible fixed-codes length/distance pair is then 31 bits total. 4670362f707Swiz * 4680362f707Swiz * sym_buf starts one-fourth of the way into pending_buf. So there are 4690362f707Swiz * three bytes in sym_buf for every four bytes in pending_buf. Each symbol 4700362f707Swiz * in sym_buf is three bytes -- two for the distance and one for the 4710362f707Swiz * literal/length. As each symbol is consumed, the pointer to the next 4720362f707Swiz * sym_buf value to read moves forward three bytes. From that symbol, up to 4730362f707Swiz * 31 bits are written to pending_buf. The closest the written pending_buf 4740362f707Swiz * bits gets to the next sym_buf symbol to read is just before the last 4750362f707Swiz * code is written. At that time, 31*(n - 2) bits have been written, just 4760362f707Swiz * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at 4770362f707Swiz * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1 4780362f707Swiz * symbols are written.) The closest the writing gets to what is unread is 4790362f707Swiz * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and 4800362f707Swiz * can range from 128 to 32768. 4810362f707Swiz * 4820362f707Swiz * Therefore, at a minimum, there are 142 bits of space between what is 4830362f707Swiz * written and what is read in the overlain buffers, so the symbols cannot 4840362f707Swiz * be overwritten by the compressed data. That space is actually 139 bits, 4850362f707Swiz * due to the three-bit fixed-code block header. 4860362f707Swiz * 4870362f707Swiz * That covers the case where either Z_FIXED is specified, forcing fixed 4880362f707Swiz * codes, or when the use of fixed codes is chosen, because that choice 4890362f707Swiz * results in a smaller compressed block than dynamic codes. That latter 4900362f707Swiz * condition then assures that the above analysis also covers all dynamic 4910362f707Swiz * blocks. A dynamic-code block will only be chosen to be emitted if it has 4920362f707Swiz * fewer bits than a fixed-code block would for the same set of symbols. 4930362f707Swiz * Therefore its average symbol length is assured to be less than 31. So 4940362f707Swiz * the compressed data for a dynamic block also cannot overwrite the 4950362f707Swiz * symbols from which it is being constructed. 4960362f707Swiz */ 4970362f707Swiz 498*96c32821Schristos s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, LIT_BUFS); 4990362f707Swiz s->pending_buf_size = (ulg)s->lit_bufsize * 4; 500aaf4ece6Schristos 501aaf4ece6Schristos if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 502aaf4ece6Schristos s->pending_buf == Z_NULL) { 503aaf4ece6Schristos s->status = FINISH_STATE; 504b85ba082Schristos strm->msg = __UNCONST(ERR_MSG(Z_MEM_ERROR)); 505aaf4ece6Schristos deflateEnd (strm); 506aaf4ece6Schristos return Z_MEM_ERROR; 507aaf4ece6Schristos } 508*96c32821Schristos #ifdef LIT_MEM 509*96c32821Schristos s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1)); 510*96c32821Schristos s->l_buf = s->pending_buf + (s->lit_bufsize << 2); 511*96c32821Schristos s->sym_end = s->lit_bufsize - 1; 512*96c32821Schristos #else 5130362f707Swiz s->sym_buf = s->pending_buf + s->lit_bufsize; 5140362f707Swiz s->sym_end = (s->lit_bufsize - 1) * 3; 515*96c32821Schristos #endif 5160362f707Swiz /* We avoid equality with lit_bufsize*3 because of wraparound at 64K 5170362f707Swiz * on 16 bit machines and because stored blocks are restricted to 5180362f707Swiz * 64K-1 bytes. 5190362f707Swiz */ 520aaf4ece6Schristos 521aaf4ece6Schristos s->level = level; 522aaf4ece6Schristos s->strategy = strategy; 523aaf4ece6Schristos s->method = (Byte)method; 524aaf4ece6Schristos 525aaf4ece6Schristos return deflateReset(strm); 526aaf4ece6Schristos } 527aaf4ece6Schristos 5286db8c6e9Schristos /* ========================================================================= 5296db8c6e9Schristos * Check for a valid deflate stream state. Return 0 if ok, 1 if not. 5306db8c6e9Schristos */ 531*96c32821Schristos local int deflateStateCheck(z_streamp strm) { 5326db8c6e9Schristos deflate_state *s; 5336db8c6e9Schristos if (strm == Z_NULL || 5346db8c6e9Schristos strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) 5356db8c6e9Schristos return 1; 5366db8c6e9Schristos s = strm->state; 5376db8c6e9Schristos if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE && 5386db8c6e9Schristos #ifdef GZIP 5396db8c6e9Schristos s->status != GZIP_STATE && 5406db8c6e9Schristos #endif 5416db8c6e9Schristos s->status != EXTRA_STATE && 5426db8c6e9Schristos s->status != NAME_STATE && 5436db8c6e9Schristos s->status != COMMENT_STATE && 5446db8c6e9Schristos s->status != HCRC_STATE && 5456db8c6e9Schristos s->status != BUSY_STATE && 5466db8c6e9Schristos s->status != FINISH_STATE)) 5476db8c6e9Schristos return 1; 5486db8c6e9Schristos return 0; 5496db8c6e9Schristos } 5506db8c6e9Schristos 551aaf4ece6Schristos /* ========================================================================= */ 552*96c32821Schristos int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary, 553*96c32821Schristos uInt dictLength) { 554aaf4ece6Schristos deflate_state *s; 5556db8c6e9Schristos uInt str, n; 5566db8c6e9Schristos int wrap; 5576db8c6e9Schristos unsigned avail; 5586db8c6e9Schristos z_const unsigned char *next; 559aaf4ece6Schristos 5606db8c6e9Schristos if (deflateStateCheck(strm) || dictionary == Z_NULL) 5616db8c6e9Schristos return Z_STREAM_ERROR; 5626db8c6e9Schristos s = strm->state; 5636db8c6e9Schristos wrap = s->wrap; 5646db8c6e9Schristos if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) 565aaf4ece6Schristos return Z_STREAM_ERROR; 566aaf4ece6Schristos 5676db8c6e9Schristos /* when using zlib wrappers, compute Adler-32 for provided dictionary */ 5686db8c6e9Schristos if (wrap == 1) 569aaf4ece6Schristos strm->adler = adler32(strm->adler, dictionary, dictLength); 5706db8c6e9Schristos s->wrap = 0; /* avoid computing Adler-32 in read_buf */ 571aaf4ece6Schristos 5726db8c6e9Schristos /* if dictionary would fill window, just replace the history */ 5736db8c6e9Schristos if (dictLength >= s->w_size) { 5746db8c6e9Schristos if (wrap == 0) { /* already empty otherwise */ 5756db8c6e9Schristos CLEAR_HASH(s); 5766db8c6e9Schristos s->strstart = 0; 5776db8c6e9Schristos s->block_start = 0L; 5786db8c6e9Schristos s->insert = 0; 579aaf4ece6Schristos } 5806db8c6e9Schristos dictionary += dictLength - s->w_size; /* use the tail */ 5816db8c6e9Schristos dictLength = s->w_size; 5826db8c6e9Schristos } 583aaf4ece6Schristos 5846db8c6e9Schristos /* insert dictionary into window and hash */ 5856db8c6e9Schristos avail = strm->avail_in; 5866db8c6e9Schristos next = strm->next_in; 5876db8c6e9Schristos strm->avail_in = dictLength; 5886db8c6e9Schristos strm->next_in = __UNCONST(dictionary); 5896db8c6e9Schristos fill_window(s); 5906db8c6e9Schristos while (s->lookahead >= MIN_MATCH) { 5916db8c6e9Schristos str = s->strstart; 5926db8c6e9Schristos n = s->lookahead - (MIN_MATCH-1); 5936db8c6e9Schristos do { 5946db8c6e9Schristos UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); 5956db8c6e9Schristos #ifndef FASTEST 5966db8c6e9Schristos s->prev[str & s->w_mask] = s->head[s->ins_h]; 5976db8c6e9Schristos #endif 5986db8c6e9Schristos s->head[s->ins_h] = (Pos)str; 5996db8c6e9Schristos str++; 6006db8c6e9Schristos } while (--n); 6016db8c6e9Schristos s->strstart = str; 6026db8c6e9Schristos s->lookahead = MIN_MATCH-1; 6036db8c6e9Schristos fill_window(s); 604aaf4ece6Schristos } 6056db8c6e9Schristos s->strstart += s->lookahead; 6066db8c6e9Schristos s->block_start = (long)s->strstart; 6076db8c6e9Schristos s->insert = s->lookahead; 6086db8c6e9Schristos s->lookahead = 0; 6096db8c6e9Schristos s->match_length = s->prev_length = MIN_MATCH-1; 6106db8c6e9Schristos s->match_available = 0; 6116db8c6e9Schristos strm->next_in = next; 6126db8c6e9Schristos strm->avail_in = avail; 6136db8c6e9Schristos s->wrap = wrap; 614aaf4ece6Schristos return Z_OK; 615aaf4ece6Schristos } 616aaf4ece6Schristos 617aaf4ece6Schristos /* ========================================================================= */ 618*96c32821Schristos int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary, 619*96c32821Schristos uInt *dictLength) { 6206db8c6e9Schristos deflate_state *s; 6216db8c6e9Schristos uInt len; 6226db8c6e9Schristos 6236db8c6e9Schristos if (deflateStateCheck(strm)) 6246db8c6e9Schristos return Z_STREAM_ERROR; 6256db8c6e9Schristos s = strm->state; 6266db8c6e9Schristos len = s->strstart + s->lookahead; 6276db8c6e9Schristos if (len > s->w_size) 6286db8c6e9Schristos len = s->w_size; 6296db8c6e9Schristos if (dictionary != Z_NULL && len) 6306db8c6e9Schristos zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len); 6316db8c6e9Schristos if (dictLength != Z_NULL) 6326db8c6e9Schristos *dictLength = len; 6336db8c6e9Schristos return Z_OK; 6346db8c6e9Schristos } 6356db8c6e9Schristos 6366db8c6e9Schristos /* ========================================================================= */ 637*96c32821Schristos int ZEXPORT deflateResetKeep(z_streamp strm) { 638aaf4ece6Schristos deflate_state *s; 639aaf4ece6Schristos 6406db8c6e9Schristos if (deflateStateCheck(strm)) { 641aaf4ece6Schristos return Z_STREAM_ERROR; 642aaf4ece6Schristos } 643aaf4ece6Schristos 644aaf4ece6Schristos strm->total_in = strm->total_out = 0; 645aaf4ece6Schristos strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ 646aaf4ece6Schristos strm->data_type = Z_UNKNOWN; 647aaf4ece6Schristos 648aaf4ece6Schristos s = (deflate_state *)strm->state; 649aaf4ece6Schristos s->pending = 0; 650aaf4ece6Schristos s->pending_out = s->pending_buf; 651aaf4ece6Schristos 652aaf4ece6Schristos if (s->wrap < 0) { 653aaf4ece6Schristos s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ 654aaf4ece6Schristos } 6556db8c6e9Schristos s->status = 6566db8c6e9Schristos #ifdef GZIP 6576db8c6e9Schristos s->wrap == 2 ? GZIP_STATE : 6586db8c6e9Schristos #endif 6593b1253e8Schristos INIT_STATE; 660aaf4ece6Schristos strm->adler = 661aaf4ece6Schristos #ifdef GZIP 662aaf4ece6Schristos s->wrap == 2 ? crc32(0L, Z_NULL, 0) : 663aaf4ece6Schristos #endif 664aaf4ece6Schristos adler32(0L, Z_NULL, 0); 6653b1253e8Schristos s->last_flush = -2; 666aaf4ece6Schristos 667aaf4ece6Schristos _tr_init(s); 668aaf4ece6Schristos 669aaf4ece6Schristos return Z_OK; 670aaf4ece6Schristos } 671aaf4ece6Schristos 672*96c32821Schristos /* =========================================================================== 673*96c32821Schristos * Initialize the "longest match" routines for a new zlib stream 674*96c32821Schristos */ 675*96c32821Schristos local void lm_init(deflate_state *s) { 676*96c32821Schristos s->window_size = (ulg)2L*s->w_size; 677*96c32821Schristos 678*96c32821Schristos CLEAR_HASH(s); 679*96c32821Schristos 680*96c32821Schristos /* Set the default configuration parameters: 681*96c32821Schristos */ 682*96c32821Schristos s->max_lazy_match = configuration_table[s->level].max_lazy; 683*96c32821Schristos s->good_match = configuration_table[s->level].good_length; 684*96c32821Schristos s->nice_match = configuration_table[s->level].nice_length; 685*96c32821Schristos s->max_chain_length = configuration_table[s->level].max_chain; 686*96c32821Schristos 687*96c32821Schristos s->strstart = 0; 688*96c32821Schristos s->block_start = 0L; 689*96c32821Schristos s->lookahead = 0; 690*96c32821Schristos s->insert = 0; 691*96c32821Schristos s->match_length = s->prev_length = MIN_MATCH-1; 692*96c32821Schristos s->match_available = 0; 693*96c32821Schristos s->ins_h = 0; 694*96c32821Schristos } 695*96c32821Schristos 696aaf4ece6Schristos /* ========================================================================= */ 697*96c32821Schristos int ZEXPORT deflateReset(z_streamp strm) { 6986db8c6e9Schristos int ret; 6996db8c6e9Schristos 7006db8c6e9Schristos ret = deflateResetKeep(strm); 7016db8c6e9Schristos if (ret == Z_OK) 7026db8c6e9Schristos lm_init(strm->state); 7036db8c6e9Schristos return ret; 7046db8c6e9Schristos } 7056db8c6e9Schristos 7066db8c6e9Schristos /* ========================================================================= */ 707*96c32821Schristos int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head) { 7086db8c6e9Schristos if (deflateStateCheck(strm) || strm->state->wrap != 2) 7096db8c6e9Schristos return Z_STREAM_ERROR; 710aaf4ece6Schristos strm->state->gzhead = head; 711aaf4ece6Schristos return Z_OK; 712aaf4ece6Schristos } 713aaf4ece6Schristos 714aaf4ece6Schristos /* ========================================================================= */ 715*96c32821Schristos int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits) { 7166db8c6e9Schristos if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 7176db8c6e9Schristos if (pending != Z_NULL) 7186db8c6e9Schristos *pending = strm->state->pending; 7196db8c6e9Schristos if (bits != Z_NULL) 7206db8c6e9Schristos *bits = strm->state->bi_valid; 7216db8c6e9Schristos return Z_OK; 7226db8c6e9Schristos } 7236db8c6e9Schristos 7246db8c6e9Schristos /* ========================================================================= */ 725*96c32821Schristos int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) { 7266db8c6e9Schristos deflate_state *s; 7276db8c6e9Schristos int put; 7286db8c6e9Schristos 7296db8c6e9Schristos if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 7306db8c6e9Schristos s = strm->state; 731*96c32821Schristos #ifdef LIT_MEM 732*96c32821Schristos if (bits < 0 || bits > 16 || 733*96c32821Schristos (uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3)) 734*96c32821Schristos return Z_BUF_ERROR; 735*96c32821Schristos #else 7363b1253e8Schristos if (bits < 0 || bits > 16 || 7373b1253e8Schristos s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3)) 7386db8c6e9Schristos return Z_BUF_ERROR; 739*96c32821Schristos #endif 7406db8c6e9Schristos do { 7416db8c6e9Schristos put = Buf_size - s->bi_valid; 7426db8c6e9Schristos if (put > bits) 7436db8c6e9Schristos put = bits; 7446db8c6e9Schristos s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); 7456db8c6e9Schristos s->bi_valid += put; 7466db8c6e9Schristos _tr_flush_bits(s); 7476db8c6e9Schristos value >>= put; 7486db8c6e9Schristos bits -= put; 7496db8c6e9Schristos } while (bits); 750aaf4ece6Schristos return Z_OK; 751aaf4ece6Schristos } 752aaf4ece6Schristos 753aaf4ece6Schristos /* ========================================================================= */ 754*96c32821Schristos int ZEXPORT deflateParams(z_streamp strm, int level, int strategy) { 755aaf4ece6Schristos deflate_state *s; 756aaf4ece6Schristos compress_func func; 757aaf4ece6Schristos 7586db8c6e9Schristos if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 759aaf4ece6Schristos s = strm->state; 760aaf4ece6Schristos 761aaf4ece6Schristos #ifdef FASTEST 762aaf4ece6Schristos if (level != 0) level = 1; 763aaf4ece6Schristos #else 764aaf4ece6Schristos if (level == Z_DEFAULT_COMPRESSION) level = 6; 765aaf4ece6Schristos #endif 766aaf4ece6Schristos if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { 767aaf4ece6Schristos return Z_STREAM_ERROR; 768aaf4ece6Schristos } 769aaf4ece6Schristos func = configuration_table[s->level].func; 770aaf4ece6Schristos 7713b1253e8Schristos if ((strategy != s->strategy || func != configuration_table[level].func) && 7723b1253e8Schristos s->last_flush != -2) { 773aaf4ece6Schristos /* Flush the last buffer: */ 7746db8c6e9Schristos int err = deflate(strm, Z_BLOCK); 7756db8c6e9Schristos if (err == Z_STREAM_ERROR) 7766db8c6e9Schristos return err; 7773b1253e8Schristos if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead) 7786db8c6e9Schristos return Z_BUF_ERROR; 779aaf4ece6Schristos } 780aaf4ece6Schristos if (s->level != level) { 7816db8c6e9Schristos if (s->level == 0 && s->matches != 0) { 7826db8c6e9Schristos if (s->matches == 1) 7836db8c6e9Schristos slide_hash(s); 7846db8c6e9Schristos else 7856db8c6e9Schristos CLEAR_HASH(s); 7866db8c6e9Schristos s->matches = 0; 7876db8c6e9Schristos } 788aaf4ece6Schristos s->level = level; 789aaf4ece6Schristos s->max_lazy_match = configuration_table[level].max_lazy; 790aaf4ece6Schristos s->good_match = configuration_table[level].good_length; 791aaf4ece6Schristos s->nice_match = configuration_table[level].nice_length; 792aaf4ece6Schristos s->max_chain_length = configuration_table[level].max_chain; 793aaf4ece6Schristos } 794aaf4ece6Schristos s->strategy = strategy; 7956db8c6e9Schristos return Z_OK; 796aaf4ece6Schristos } 797aaf4ece6Schristos 798aaf4ece6Schristos /* ========================================================================= */ 799*96c32821Schristos int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy, 800*96c32821Schristos int nice_length, int max_chain) { 801aaf4ece6Schristos deflate_state *s; 802aaf4ece6Schristos 8036db8c6e9Schristos if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 804aaf4ece6Schristos s = strm->state; 8056db8c6e9Schristos s->good_match = (uInt)good_length; 8066db8c6e9Schristos s->max_lazy_match = (uInt)max_lazy; 807aaf4ece6Schristos s->nice_match = nice_length; 8086db8c6e9Schristos s->max_chain_length = (uInt)max_chain; 809aaf4ece6Schristos return Z_OK; 810aaf4ece6Schristos } 811aaf4ece6Schristos 812aaf4ece6Schristos /* ========================================================================= 8133b1253e8Schristos * For the default windowBits of 15 and memLevel of 8, this function returns a 8143b1253e8Schristos * close to exact, as well as small, upper bound on the compressed size. This 8153b1253e8Schristos * is an expansion of ~0.03%, plus a small constant. 816aaf4ece6Schristos * 8173b1253e8Schristos * For any setting other than those defaults for windowBits and memLevel, one 8183b1253e8Schristos * of two worst case bounds is returned. This is at most an expansion of ~4% or 8193b1253e8Schristos * ~13%, plus a small constant. 820aaf4ece6Schristos * 8213b1253e8Schristos * Both the 0.03% and 4% derive from the overhead of stored blocks. The first 8223b1253e8Schristos * one is for stored blocks of 16383 bytes (memLevel == 8), whereas the second 8233b1253e8Schristos * is for stored blocks of 127 bytes (the worst case memLevel == 1). The 8243b1253e8Schristos * expansion results from five bytes of header for each stored block. 8253b1253e8Schristos * 8263b1253e8Schristos * The larger expansion of 13% results from a window size less than or equal to 8273b1253e8Schristos * the symbols buffer size (windowBits <= memLevel + 7). In that case some of 8283b1253e8Schristos * the data being compressed may have slid out of the sliding window, impeding 8293b1253e8Schristos * a stored block from being emitted. Then the only choice is a fixed or 8303b1253e8Schristos * dynamic block, where a fixed block limits the maximum expansion to 9 bits 8313b1253e8Schristos * per 8-bit byte, plus 10 bits for every block. The smallest block size for 8323b1253e8Schristos * which this can occur is 255 (memLevel == 2). 8333b1253e8Schristos * 8343b1253e8Schristos * Shifts are used to approximate divisions, for speed. 835aaf4ece6Schristos */ 836*96c32821Schristos uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen) { 837aaf4ece6Schristos deflate_state *s; 8383b1253e8Schristos uLong fixedlen, storelen, wraplen; 839aaf4ece6Schristos 8403b1253e8Schristos /* upper bound for fixed blocks with 9-bit literals and length 255 8413b1253e8Schristos (memLevel == 2, which is the lowest that may not use stored blocks) -- 8423b1253e8Schristos ~13% overhead plus a small constant */ 8433b1253e8Schristos fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) + 8443b1253e8Schristos (sourceLen >> 9) + 4; 845aaf4ece6Schristos 8463b1253e8Schristos /* upper bound for stored blocks with length 127 (memLevel == 1) -- 8473b1253e8Schristos ~4% overhead plus a small constant */ 8483b1253e8Schristos storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) + 8493b1253e8Schristos (sourceLen >> 11) + 7; 8503b1253e8Schristos 8513b1253e8Schristos /* if can't get parameters, return larger bound plus a zlib wrapper */ 8526db8c6e9Schristos if (deflateStateCheck(strm)) 8533b1253e8Schristos return (fixedlen > storelen ? fixedlen : storelen) + 6; 8546db8c6e9Schristos 8556db8c6e9Schristos /* compute wrapper length */ 8566db8c6e9Schristos s = strm->state; 8576db8c6e9Schristos switch (s->wrap) { 8586db8c6e9Schristos case 0: /* raw deflate */ 8596db8c6e9Schristos wraplen = 0; 8606db8c6e9Schristos break; 8616db8c6e9Schristos case 1: /* zlib wrapper */ 8626db8c6e9Schristos wraplen = 6 + (s->strstart ? 4 : 0); 8636db8c6e9Schristos break; 8646db8c6e9Schristos #ifdef GZIP 8656db8c6e9Schristos case 2: /* gzip wrapper */ 8666db8c6e9Schristos wraplen = 18; 8676db8c6e9Schristos if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ 8686db8c6e9Schristos Bytef *str; 8696db8c6e9Schristos if (s->gzhead->extra != Z_NULL) 8706db8c6e9Schristos wraplen += 2 + s->gzhead->extra_len; 8716db8c6e9Schristos str = s->gzhead->name; 8726db8c6e9Schristos if (str != Z_NULL) 8736db8c6e9Schristos do { 8746db8c6e9Schristos wraplen++; 8756db8c6e9Schristos } while (*str++); 8766db8c6e9Schristos str = s->gzhead->comment; 8776db8c6e9Schristos if (str != Z_NULL) 8786db8c6e9Schristos do { 8796db8c6e9Schristos wraplen++; 8806db8c6e9Schristos } while (*str++); 8816db8c6e9Schristos if (s->gzhead->hcrc) 8826db8c6e9Schristos wraplen += 2; 8836db8c6e9Schristos } 8846db8c6e9Schristos break; 8856db8c6e9Schristos #endif 8866db8c6e9Schristos default: /* for compiler happiness */ 8876db8c6e9Schristos wraplen = 6; 8886db8c6e9Schristos } 889aaf4ece6Schristos 8903b1253e8Schristos /* if not default parameters, return one of the conservative bounds */ 891aaf4ece6Schristos if (s->w_bits != 15 || s->hash_bits != 8 + 7) 892*96c32821Schristos return (s->w_bits <= s->hash_bits && s->level ? fixedlen : storelen) + 893*96c32821Schristos wraplen; 894aaf4ece6Schristos 8953b1253e8Schristos /* default settings: return tight bound for that case -- ~0.03% overhead 8963b1253e8Schristos plus a small constant */ 8976db8c6e9Schristos return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 8986db8c6e9Schristos (sourceLen >> 25) + 13 - 6 + wraplen; 899aaf4ece6Schristos } 900aaf4ece6Schristos 901aaf4ece6Schristos /* ========================================================================= 902aaf4ece6Schristos * Put a short in the pending buffer. The 16-bit value is put in MSB order. 903aaf4ece6Schristos * IN assertion: the stream state is correct and there is enough room in 904aaf4ece6Schristos * pending_buf. 905aaf4ece6Schristos */ 906*96c32821Schristos local void putShortMSB(deflate_state *s, uInt b) { 907aaf4ece6Schristos put_byte(s, (Byte)(b >> 8)); 908aaf4ece6Schristos put_byte(s, (Byte)(b & 0xff)); 909aaf4ece6Schristos } 910aaf4ece6Schristos 911aaf4ece6Schristos /* ========================================================================= 9126db8c6e9Schristos * Flush as much pending output as possible. All deflate() output, except for 9136db8c6e9Schristos * some deflate_stored() output, goes through this function so some 9146db8c6e9Schristos * applications may wish to modify it to avoid allocating a large 9156db8c6e9Schristos * strm->next_out buffer and copying into it. (See also read_buf()). 916aaf4ece6Schristos */ 917*96c32821Schristos local void flush_pending(z_streamp strm) { 9186db8c6e9Schristos unsigned len; 9196db8c6e9Schristos deflate_state *s = strm->state; 920aaf4ece6Schristos 9216db8c6e9Schristos _tr_flush_bits(s); 9226db8c6e9Schristos len = s->pending; 923aaf4ece6Schristos if (len > strm->avail_out) len = strm->avail_out; 924aaf4ece6Schristos if (len == 0) return; 925aaf4ece6Schristos 9266db8c6e9Schristos zmemcpy(strm->next_out, s->pending_out, len); 927aaf4ece6Schristos strm->next_out += len; 9286db8c6e9Schristos s->pending_out += len; 929aaf4ece6Schristos strm->total_out += len; 930aaf4ece6Schristos strm->avail_out -= len; 9316db8c6e9Schristos s->pending -= len; 9326db8c6e9Schristos if (s->pending == 0) { 9336db8c6e9Schristos s->pending_out = s->pending_buf; 934aaf4ece6Schristos } 935aaf4ece6Schristos } 936aaf4ece6Schristos 9376db8c6e9Schristos /* =========================================================================== 9386db8c6e9Schristos * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1]. 9396db8c6e9Schristos */ 9406db8c6e9Schristos #define HCRC_UPDATE(beg) \ 9416db8c6e9Schristos do { \ 9426db8c6e9Schristos if (s->gzhead->hcrc && s->pending > (beg)) \ 9436db8c6e9Schristos strm->adler = crc32(strm->adler, s->pending_buf + (beg), \ 9446db8c6e9Schristos s->pending - (beg)); \ 9456db8c6e9Schristos } while (0) 9466db8c6e9Schristos 947aaf4ece6Schristos /* ========================================================================= */ 948*96c32821Schristos int ZEXPORT deflate(z_streamp strm, int flush) { 949aaf4ece6Schristos int old_flush; /* value of flush param for previous deflate call */ 950aaf4ece6Schristos deflate_state *s; 951aaf4ece6Schristos 9526db8c6e9Schristos if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) { 953aaf4ece6Schristos return Z_STREAM_ERROR; 954aaf4ece6Schristos } 955aaf4ece6Schristos s = strm->state; 956aaf4ece6Schristos 957aaf4ece6Schristos if (strm->next_out == Z_NULL || 9586db8c6e9Schristos (strm->avail_in != 0 && strm->next_in == Z_NULL) || 959aaf4ece6Schristos (s->status == FINISH_STATE && flush != Z_FINISH)) { 960aaf4ece6Schristos ERR_RETURN(strm, Z_STREAM_ERROR); 961aaf4ece6Schristos } 962aaf4ece6Schristos if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 963aaf4ece6Schristos 964aaf4ece6Schristos old_flush = s->last_flush; 965aaf4ece6Schristos s->last_flush = flush; 966aaf4ece6Schristos 967aaf4ece6Schristos /* Flush as much pending output as possible */ 968aaf4ece6Schristos if (s->pending != 0) { 969aaf4ece6Schristos flush_pending(strm); 970aaf4ece6Schristos if (strm->avail_out == 0) { 971aaf4ece6Schristos /* Since avail_out is 0, deflate will be called again with 972aaf4ece6Schristos * more output space, but possibly with both pending and 973aaf4ece6Schristos * avail_in equal to zero. There won't be anything to do, 974aaf4ece6Schristos * but this is not an error situation so make sure we 975aaf4ece6Schristos * return OK instead of BUF_ERROR at next call of deflate: 976aaf4ece6Schristos */ 977aaf4ece6Schristos s->last_flush = -1; 978aaf4ece6Schristos return Z_OK; 979aaf4ece6Schristos } 980aaf4ece6Schristos 981aaf4ece6Schristos /* Make sure there is something to do and avoid duplicate consecutive 982aaf4ece6Schristos * flushes. For repeated and useless calls with Z_FINISH, we keep 983aaf4ece6Schristos * returning Z_STREAM_END instead of Z_BUF_ERROR. 984aaf4ece6Schristos */ 9856db8c6e9Schristos } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && 986aaf4ece6Schristos flush != Z_FINISH) { 987aaf4ece6Schristos ERR_RETURN(strm, Z_BUF_ERROR); 988aaf4ece6Schristos } 989aaf4ece6Schristos 990aaf4ece6Schristos /* User must not provide more input after the first FINISH: */ 991aaf4ece6Schristos if (s->status == FINISH_STATE && strm->avail_in != 0) { 992aaf4ece6Schristos ERR_RETURN(strm, Z_BUF_ERROR); 993aaf4ece6Schristos } 994aaf4ece6Schristos 9956db8c6e9Schristos /* Write the header */ 9963b1253e8Schristos if (s->status == INIT_STATE && s->wrap == 0) 9973b1253e8Schristos s->status = BUSY_STATE; 9986db8c6e9Schristos if (s->status == INIT_STATE) { 9996db8c6e9Schristos /* zlib header */ 10006db8c6e9Schristos uInt header = (Z_DEFLATED + ((s->w_bits - 8) << 4)) << 8; 10016db8c6e9Schristos uInt level_flags; 10026db8c6e9Schristos 10036db8c6e9Schristos if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) 10046db8c6e9Schristos level_flags = 0; 10056db8c6e9Schristos else if (s->level < 6) 10066db8c6e9Schristos level_flags = 1; 10076db8c6e9Schristos else if (s->level == 6) 10086db8c6e9Schristos level_flags = 2; 10096db8c6e9Schristos else 10106db8c6e9Schristos level_flags = 3; 10116db8c6e9Schristos header |= (level_flags << 6); 10126db8c6e9Schristos if (s->strstart != 0) header |= PRESET_DICT; 10136db8c6e9Schristos header += 31 - (header % 31); 10146db8c6e9Schristos 10156db8c6e9Schristos putShortMSB(s, header); 10166db8c6e9Schristos 10176db8c6e9Schristos /* Save the adler32 of the preset dictionary: */ 10186db8c6e9Schristos if (s->strstart != 0) { 10196db8c6e9Schristos putShortMSB(s, (uInt)(strm->adler >> 16)); 10206db8c6e9Schristos putShortMSB(s, (uInt)(strm->adler & 0xffff)); 10216db8c6e9Schristos } 10226db8c6e9Schristos strm->adler = adler32(0L, Z_NULL, 0); 10236db8c6e9Schristos s->status = BUSY_STATE; 10246db8c6e9Schristos 10256db8c6e9Schristos /* Compression must start with an empty pending buffer */ 10266db8c6e9Schristos flush_pending(strm); 10276db8c6e9Schristos if (s->pending != 0) { 10286db8c6e9Schristos s->last_flush = -1; 10296db8c6e9Schristos return Z_OK; 10306db8c6e9Schristos } 10316db8c6e9Schristos } 10326db8c6e9Schristos #ifdef GZIP 10336db8c6e9Schristos if (s->status == GZIP_STATE) { 10346db8c6e9Schristos /* gzip header */ 10356db8c6e9Schristos strm->adler = crc32(0L, Z_NULL, 0); 10366db8c6e9Schristos put_byte(s, 31); 10376db8c6e9Schristos put_byte(s, 139); 10386db8c6e9Schristos put_byte(s, 8); 10396db8c6e9Schristos if (s->gzhead == Z_NULL) { 10406db8c6e9Schristos put_byte(s, 0); 10416db8c6e9Schristos put_byte(s, 0); 10426db8c6e9Schristos put_byte(s, 0); 10436db8c6e9Schristos put_byte(s, 0); 10446db8c6e9Schristos put_byte(s, 0); 10456db8c6e9Schristos put_byte(s, s->level == 9 ? 2 : 10466db8c6e9Schristos (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 10476db8c6e9Schristos 4 : 0)); 10486db8c6e9Schristos put_byte(s, OS_CODE); 10496db8c6e9Schristos s->status = BUSY_STATE; 10506db8c6e9Schristos 10516db8c6e9Schristos /* Compression must start with an empty pending buffer */ 10526db8c6e9Schristos flush_pending(strm); 10536db8c6e9Schristos if (s->pending != 0) { 10546db8c6e9Schristos s->last_flush = -1; 10556db8c6e9Schristos return Z_OK; 10566db8c6e9Schristos } 10576db8c6e9Schristos } 10586db8c6e9Schristos else { 10596db8c6e9Schristos put_byte(s, (s->gzhead->text ? 1 : 0) + 10606db8c6e9Schristos (s->gzhead->hcrc ? 2 : 0) + 10616db8c6e9Schristos (s->gzhead->extra == Z_NULL ? 0 : 4) + 10626db8c6e9Schristos (s->gzhead->name == Z_NULL ? 0 : 8) + 10636db8c6e9Schristos (s->gzhead->comment == Z_NULL ? 0 : 16) 10646db8c6e9Schristos ); 10656db8c6e9Schristos put_byte(s, (Byte)(s->gzhead->time & 0xff)); 10666db8c6e9Schristos put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); 10676db8c6e9Schristos put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); 10686db8c6e9Schristos put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); 10696db8c6e9Schristos put_byte(s, s->level == 9 ? 2 : 10706db8c6e9Schristos (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 10716db8c6e9Schristos 4 : 0)); 10726db8c6e9Schristos put_byte(s, s->gzhead->os & 0xff); 10736db8c6e9Schristos if (s->gzhead->extra != Z_NULL) { 10746db8c6e9Schristos put_byte(s, s->gzhead->extra_len & 0xff); 10756db8c6e9Schristos put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); 10766db8c6e9Schristos } 10776db8c6e9Schristos if (s->gzhead->hcrc) 10786db8c6e9Schristos strm->adler = crc32(strm->adler, s->pending_buf, 10796db8c6e9Schristos s->pending); 10806db8c6e9Schristos s->gzindex = 0; 10816db8c6e9Schristos s->status = EXTRA_STATE; 10826db8c6e9Schristos } 10836db8c6e9Schristos } 10846db8c6e9Schristos if (s->status == EXTRA_STATE) { 10856db8c6e9Schristos if (s->gzhead->extra != Z_NULL) { 10866db8c6e9Schristos ulg beg = s->pending; /* start of bytes to update crc */ 10876db8c6e9Schristos uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex; 10886db8c6e9Schristos while (s->pending + left > s->pending_buf_size) { 10896db8c6e9Schristos uInt copy = s->pending_buf_size - s->pending; 10906db8c6e9Schristos zmemcpy(s->pending_buf + s->pending, 10916db8c6e9Schristos s->gzhead->extra + s->gzindex, copy); 10926db8c6e9Schristos s->pending = s->pending_buf_size; 10936db8c6e9Schristos HCRC_UPDATE(beg); 10946db8c6e9Schristos s->gzindex += copy; 10956db8c6e9Schristos flush_pending(strm); 10966db8c6e9Schristos if (s->pending != 0) { 10976db8c6e9Schristos s->last_flush = -1; 10986db8c6e9Schristos return Z_OK; 10996db8c6e9Schristos } 11006db8c6e9Schristos beg = 0; 11016db8c6e9Schristos left -= copy; 11026db8c6e9Schristos } 11036db8c6e9Schristos zmemcpy(s->pending_buf + s->pending, 11046db8c6e9Schristos s->gzhead->extra + s->gzindex, left); 11056db8c6e9Schristos s->pending += left; 11066db8c6e9Schristos HCRC_UPDATE(beg); 11076db8c6e9Schristos s->gzindex = 0; 11086db8c6e9Schristos } 11096db8c6e9Schristos s->status = NAME_STATE; 11106db8c6e9Schristos } 11116db8c6e9Schristos if (s->status == NAME_STATE) { 11126db8c6e9Schristos if (s->gzhead->name != Z_NULL) { 11136db8c6e9Schristos ulg beg = s->pending; /* start of bytes to update crc */ 11146db8c6e9Schristos int val; 11156db8c6e9Schristos do { 11166db8c6e9Schristos if (s->pending == s->pending_buf_size) { 11176db8c6e9Schristos HCRC_UPDATE(beg); 11186db8c6e9Schristos flush_pending(strm); 11196db8c6e9Schristos if (s->pending != 0) { 11206db8c6e9Schristos s->last_flush = -1; 11216db8c6e9Schristos return Z_OK; 11226db8c6e9Schristos } 11236db8c6e9Schristos beg = 0; 11246db8c6e9Schristos } 11256db8c6e9Schristos val = s->gzhead->name[s->gzindex++]; 11266db8c6e9Schristos put_byte(s, val); 11276db8c6e9Schristos } while (val != 0); 11286db8c6e9Schristos HCRC_UPDATE(beg); 11296db8c6e9Schristos s->gzindex = 0; 11306db8c6e9Schristos } 11316db8c6e9Schristos s->status = COMMENT_STATE; 11326db8c6e9Schristos } 11336db8c6e9Schristos if (s->status == COMMENT_STATE) { 11346db8c6e9Schristos if (s->gzhead->comment != Z_NULL) { 11356db8c6e9Schristos ulg beg = s->pending; /* start of bytes to update crc */ 11366db8c6e9Schristos int val; 11376db8c6e9Schristos do { 11386db8c6e9Schristos if (s->pending == s->pending_buf_size) { 11396db8c6e9Schristos HCRC_UPDATE(beg); 11406db8c6e9Schristos flush_pending(strm); 11416db8c6e9Schristos if (s->pending != 0) { 11426db8c6e9Schristos s->last_flush = -1; 11436db8c6e9Schristos return Z_OK; 11446db8c6e9Schristos } 11456db8c6e9Schristos beg = 0; 11466db8c6e9Schristos } 11476db8c6e9Schristos val = s->gzhead->comment[s->gzindex++]; 11486db8c6e9Schristos put_byte(s, val); 11496db8c6e9Schristos } while (val != 0); 11506db8c6e9Schristos HCRC_UPDATE(beg); 11516db8c6e9Schristos } 11526db8c6e9Schristos s->status = HCRC_STATE; 11536db8c6e9Schristos } 11546db8c6e9Schristos if (s->status == HCRC_STATE) { 11556db8c6e9Schristos if (s->gzhead->hcrc) { 11566db8c6e9Schristos if (s->pending + 2 > s->pending_buf_size) { 11576db8c6e9Schristos flush_pending(strm); 11586db8c6e9Schristos if (s->pending != 0) { 11596db8c6e9Schristos s->last_flush = -1; 11606db8c6e9Schristos return Z_OK; 11616db8c6e9Schristos } 11626db8c6e9Schristos } 11636db8c6e9Schristos put_byte(s, (Byte)(strm->adler & 0xff)); 11646db8c6e9Schristos put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 11656db8c6e9Schristos strm->adler = crc32(0L, Z_NULL, 0); 11666db8c6e9Schristos } 11676db8c6e9Schristos s->status = BUSY_STATE; 11686db8c6e9Schristos 11696db8c6e9Schristos /* Compression must start with an empty pending buffer */ 11706db8c6e9Schristos flush_pending(strm); 11716db8c6e9Schristos if (s->pending != 0) { 11726db8c6e9Schristos s->last_flush = -1; 11736db8c6e9Schristos return Z_OK; 11746db8c6e9Schristos } 11756db8c6e9Schristos } 11766db8c6e9Schristos #endif 11776db8c6e9Schristos 1178aaf4ece6Schristos /* Start a new block or continue the current one. 1179aaf4ece6Schristos */ 1180aaf4ece6Schristos if (strm->avail_in != 0 || s->lookahead != 0 || 1181aaf4ece6Schristos (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { 1182aaf4ece6Schristos block_state bstate; 1183aaf4ece6Schristos 11846db8c6e9Schristos bstate = s->level == 0 ? deflate_stored(s, flush) : 11856db8c6e9Schristos s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : 11866db8c6e9Schristos s->strategy == Z_RLE ? deflate_rle(s, flush) : 11876db8c6e9Schristos (*(configuration_table[s->level].func))(s, flush); 1188aaf4ece6Schristos 1189aaf4ece6Schristos if (bstate == finish_started || bstate == finish_done) { 1190aaf4ece6Schristos s->status = FINISH_STATE; 1191aaf4ece6Schristos } 1192aaf4ece6Schristos if (bstate == need_more || bstate == finish_started) { 1193aaf4ece6Schristos if (strm->avail_out == 0) { 1194aaf4ece6Schristos s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ 1195aaf4ece6Schristos } 1196aaf4ece6Schristos return Z_OK; 1197aaf4ece6Schristos /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 1198aaf4ece6Schristos * of deflate should use the same flush parameter to make sure 1199aaf4ece6Schristos * that the flush is complete. So we don't have to output an 1200aaf4ece6Schristos * empty block here, this will be done at next call. This also 1201aaf4ece6Schristos * ensures that for a very small output buffer, we emit at most 1202aaf4ece6Schristos * one empty block. 1203aaf4ece6Schristos */ 1204aaf4ece6Schristos } 1205aaf4ece6Schristos if (bstate == block_done) { 1206aaf4ece6Schristos if (flush == Z_PARTIAL_FLUSH) { 1207aaf4ece6Schristos _tr_align(s); 12086db8c6e9Schristos } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ 1209aaf4ece6Schristos _tr_stored_block(s, (char*)0, 0L, 0); 1210aaf4ece6Schristos /* For a full flush, this empty block will be recognized 1211aaf4ece6Schristos * as a special marker by inflate_sync(). 1212aaf4ece6Schristos */ 1213aaf4ece6Schristos if (flush == Z_FULL_FLUSH) { 1214aaf4ece6Schristos CLEAR_HASH(s); /* forget history */ 12156db8c6e9Schristos if (s->lookahead == 0) { 12166db8c6e9Schristos s->strstart = 0; 12176db8c6e9Schristos s->block_start = 0L; 12186db8c6e9Schristos s->insert = 0; 12196db8c6e9Schristos } 1220aaf4ece6Schristos } 1221aaf4ece6Schristos } 1222aaf4ece6Schristos flush_pending(strm); 1223aaf4ece6Schristos if (strm->avail_out == 0) { 1224aaf4ece6Schristos s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ 1225aaf4ece6Schristos return Z_OK; 1226aaf4ece6Schristos } 1227aaf4ece6Schristos } 1228aaf4ece6Schristos } 1229aaf4ece6Schristos 1230aaf4ece6Schristos if (flush != Z_FINISH) return Z_OK; 1231aaf4ece6Schristos if (s->wrap <= 0) return Z_STREAM_END; 1232aaf4ece6Schristos 1233aaf4ece6Schristos /* Write the trailer */ 1234aaf4ece6Schristos #ifdef GZIP 1235aaf4ece6Schristos if (s->wrap == 2) { 1236aaf4ece6Schristos put_byte(s, (Byte)(strm->adler & 0xff)); 1237aaf4ece6Schristos put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 1238aaf4ece6Schristos put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); 1239aaf4ece6Schristos put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); 1240aaf4ece6Schristos put_byte(s, (Byte)(strm->total_in & 0xff)); 1241aaf4ece6Schristos put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); 1242aaf4ece6Schristos put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); 1243aaf4ece6Schristos put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); 1244aaf4ece6Schristos } 1245aaf4ece6Schristos else 1246aaf4ece6Schristos #endif 1247aaf4ece6Schristos { 1248aaf4ece6Schristos putShortMSB(s, (uInt)(strm->adler >> 16)); 1249aaf4ece6Schristos putShortMSB(s, (uInt)(strm->adler & 0xffff)); 1250aaf4ece6Schristos } 1251aaf4ece6Schristos flush_pending(strm); 1252aaf4ece6Schristos /* If avail_out is zero, the application will call deflate again 1253aaf4ece6Schristos * to flush the rest. 1254aaf4ece6Schristos */ 1255aaf4ece6Schristos if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ 1256aaf4ece6Schristos return s->pending != 0 ? Z_OK : Z_STREAM_END; 1257aaf4ece6Schristos } 1258aaf4ece6Schristos 1259aaf4ece6Schristos /* ========================================================================= */ 1260*96c32821Schristos int ZEXPORT deflateEnd(z_streamp strm) { 1261aaf4ece6Schristos int status; 1262aaf4ece6Schristos 12636db8c6e9Schristos if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 1264aaf4ece6Schristos 1265aaf4ece6Schristos status = strm->state->status; 1266aaf4ece6Schristos 1267aaf4ece6Schristos /* Deallocate in reverse order of allocations: */ 1268aaf4ece6Schristos TRY_FREE(strm, strm->state->pending_buf); 1269aaf4ece6Schristos TRY_FREE(strm, strm->state->head); 1270aaf4ece6Schristos TRY_FREE(strm, strm->state->prev); 1271aaf4ece6Schristos TRY_FREE(strm, strm->state->window); 1272aaf4ece6Schristos 1273aaf4ece6Schristos ZFREE(strm, strm->state); 1274aaf4ece6Schristos strm->state = Z_NULL; 1275aaf4ece6Schristos 1276aaf4ece6Schristos return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; 1277aaf4ece6Schristos } 1278aaf4ece6Schristos 1279aaf4ece6Schristos /* ========================================================================= 1280aaf4ece6Schristos * Copy the source state to the destination state. 1281aaf4ece6Schristos * To simplify the source, this is not supported for 16-bit MSDOS (which 1282aaf4ece6Schristos * doesn't have enough memory anyway to duplicate compression states). 1283aaf4ece6Schristos */ 1284*96c32821Schristos int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) { 1285aaf4ece6Schristos #ifdef MAXSEG_64K 1286*96c32821Schristos (void)dest; 1287*96c32821Schristos (void)source; 1288aaf4ece6Schristos return Z_STREAM_ERROR; 1289aaf4ece6Schristos #else 1290aaf4ece6Schristos deflate_state *ds; 1291aaf4ece6Schristos deflate_state *ss; 1292aaf4ece6Schristos 1293aaf4ece6Schristos 12946db8c6e9Schristos if (deflateStateCheck(source) || dest == Z_NULL) { 1295aaf4ece6Schristos return Z_STREAM_ERROR; 1296aaf4ece6Schristos } 1297aaf4ece6Schristos 1298aaf4ece6Schristos ss = source->state; 1299aaf4ece6Schristos 13006db8c6e9Schristos zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); 1301aaf4ece6Schristos 1302aaf4ece6Schristos ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); 1303aaf4ece6Schristos if (ds == Z_NULL) return Z_MEM_ERROR; 1304aaf4ece6Schristos dest->state = (struct internal_state FAR *) ds; 13056db8c6e9Schristos zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); 1306aaf4ece6Schristos ds->strm = dest; 1307aaf4ece6Schristos 1308aaf4ece6Schristos ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); 1309aaf4ece6Schristos ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); 1310aaf4ece6Schristos ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); 1311*96c32821Schristos ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS); 1312aaf4ece6Schristos 1313aaf4ece6Schristos if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || 1314aaf4ece6Schristos ds->pending_buf == Z_NULL) { 1315aaf4ece6Schristos deflateEnd (dest); 1316aaf4ece6Schristos return Z_MEM_ERROR; 1317aaf4ece6Schristos } 1318aaf4ece6Schristos /* following zmemcpy do not work for 16-bit MSDOS */ 1319aaf4ece6Schristos zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); 13206db8c6e9Schristos zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); 13216db8c6e9Schristos zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); 1322*96c32821Schristos zmemcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS); 1323aaf4ece6Schristos 1324aaf4ece6Schristos ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); 1325*96c32821Schristos #ifdef LIT_MEM 1326*96c32821Schristos ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1)); 1327*96c32821Schristos ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2); 1328*96c32821Schristos #else 13290362f707Swiz ds->sym_buf = ds->pending_buf + ds->lit_bufsize; 1330*96c32821Schristos #endif 1331aaf4ece6Schristos 1332aaf4ece6Schristos ds->l_desc.dyn_tree = ds->dyn_ltree; 1333aaf4ece6Schristos ds->d_desc.dyn_tree = ds->dyn_dtree; 1334aaf4ece6Schristos ds->bl_desc.dyn_tree = ds->bl_tree; 1335aaf4ece6Schristos 1336aaf4ece6Schristos return Z_OK; 1337aaf4ece6Schristos #endif /* MAXSEG_64K */ 1338aaf4ece6Schristos } 1339aaf4ece6Schristos 1340aaf4ece6Schristos #ifndef FASTEST 1341aaf4ece6Schristos /* =========================================================================== 1342aaf4ece6Schristos * Set match_start to the longest match starting at the given string and 1343aaf4ece6Schristos * return its length. Matches shorter or equal to prev_length are discarded, 1344aaf4ece6Schristos * in which case the result is equal to prev_length and match_start is 1345aaf4ece6Schristos * garbage. 1346aaf4ece6Schristos * IN assertions: cur_match is the head of the hash chain for the current 1347aaf4ece6Schristos * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 1348aaf4ece6Schristos * OUT assertion: the match length is not greater than s->lookahead. 1349aaf4ece6Schristos */ 1350*96c32821Schristos local uInt longest_match(deflate_state *s, IPos cur_match) { 1351aaf4ece6Schristos unsigned chain_length = s->max_chain_length;/* max hash chain length */ 1352aaf4ece6Schristos register Bytef *scan = s->window + s->strstart; /* current string */ 1353aaf4ece6Schristos register Bytef *match; /* matched string */ 1354aaf4ece6Schristos register int len; /* length of current match */ 13556db8c6e9Schristos int best_len = (int)s->prev_length; /* best match length so far */ 1356aaf4ece6Schristos int nice_match = s->nice_match; /* stop if match long enough */ 1357aaf4ece6Schristos IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 1358aaf4ece6Schristos s->strstart - (IPos)MAX_DIST(s) : NIL; 1359aaf4ece6Schristos /* Stop when cur_match becomes <= limit. To simplify the code, 1360aaf4ece6Schristos * we prevent matches with the string of window index 0. 1361aaf4ece6Schristos */ 1362aaf4ece6Schristos Posf *prev = s->prev; 1363aaf4ece6Schristos uInt wmask = s->w_mask; 1364aaf4ece6Schristos 1365aaf4ece6Schristos #ifdef UNALIGNED_OK 1366aaf4ece6Schristos /* Compare two bytes at a time. Note: this is not always beneficial. 1367aaf4ece6Schristos * Try with and without -DUNALIGNED_OK to check. 1368aaf4ece6Schristos */ 1369aaf4ece6Schristos register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; 1370aaf4ece6Schristos register ush scan_start = *(ushf*)scan; 1371aaf4ece6Schristos register ush scan_end = *(ushf*)(scan + best_len - 1); 1372aaf4ece6Schristos #else 1373aaf4ece6Schristos register Bytef *strend = s->window + s->strstart + MAX_MATCH; 1374aaf4ece6Schristos register Byte scan_end1 = scan[best_len - 1]; 1375aaf4ece6Schristos register Byte scan_end = scan[best_len]; 1376aaf4ece6Schristos #endif 1377aaf4ece6Schristos 1378aaf4ece6Schristos /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1379aaf4ece6Schristos * It is easy to get rid of this optimization if necessary. 1380aaf4ece6Schristos */ 1381aaf4ece6Schristos Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 1382aaf4ece6Schristos 1383aaf4ece6Schristos /* Do not waste too much time if we already have a good match: */ 1384aaf4ece6Schristos if (s->prev_length >= s->good_match) { 1385aaf4ece6Schristos chain_length >>= 2; 1386aaf4ece6Schristos } 1387aaf4ece6Schristos /* Do not look for matches beyond the end of the input. This is necessary 1388aaf4ece6Schristos * to make deflate deterministic. 1389aaf4ece6Schristos */ 13906db8c6e9Schristos if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; 1391aaf4ece6Schristos 13923b1253e8Schristos Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, 13933b1253e8Schristos "need lookahead"); 1394aaf4ece6Schristos 1395aaf4ece6Schristos do { 1396aaf4ece6Schristos Assert(cur_match < s->strstart, "no future"); 1397aaf4ece6Schristos match = s->window + cur_match; 1398aaf4ece6Schristos 1399aaf4ece6Schristos /* Skip to next match if the match length cannot increase 1400aaf4ece6Schristos * or if the match length is less than 2. Note that the checks below 1401aaf4ece6Schristos * for insufficient lookahead only occur occasionally for performance 1402aaf4ece6Schristos * reasons. Therefore uninitialized memory will be accessed, and 1403aaf4ece6Schristos * conditional jumps will be made that depend on those values. 1404aaf4ece6Schristos * However the length of the match is limited to the lookahead, so 1405aaf4ece6Schristos * the output of deflate is not affected by the uninitialized values. 1406aaf4ece6Schristos */ 1407aaf4ece6Schristos #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 1408aaf4ece6Schristos /* This code assumes sizeof(unsigned short) == 2. Do not use 1409aaf4ece6Schristos * UNALIGNED_OK if your compiler uses a different size. 1410aaf4ece6Schristos */ 1411aaf4ece6Schristos if (*(ushf*)(match + best_len - 1) != scan_end || 1412aaf4ece6Schristos *(ushf*)match != scan_start) continue; 1413aaf4ece6Schristos 1414aaf4ece6Schristos /* It is not necessary to compare scan[2] and match[2] since they are 1415aaf4ece6Schristos * always equal when the other bytes match, given that the hash keys 1416aaf4ece6Schristos * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at 14173b1253e8Schristos * strstart + 3, + 5, up to strstart + 257. We check for insufficient 1418aaf4ece6Schristos * lookahead only every 4th comparison; the 128th check will be made 1419aaf4ece6Schristos * at strstart + 257. If MAX_MATCH-2 is not a multiple of 8, it is 1420aaf4ece6Schristos * necessary to put more guard bytes at the end of the window, or 1421aaf4ece6Schristos * to check more often for insufficient lookahead. 1422aaf4ece6Schristos */ 1423aaf4ece6Schristos Assert(scan[2] == match[2], "scan[2]?"); 1424aaf4ece6Schristos scan++, match++; 1425aaf4ece6Schristos do { 1426aaf4ece6Schristos } while (*(ushf*)(scan += 2) == *(ushf*)(match += 2) && 1427aaf4ece6Schristos *(ushf*)(scan += 2) == *(ushf*)(match += 2) && 1428aaf4ece6Schristos *(ushf*)(scan += 2) == *(ushf*)(match += 2) && 1429aaf4ece6Schristos *(ushf*)(scan += 2) == *(ushf*)(match += 2) && 1430aaf4ece6Schristos scan < strend); 1431aaf4ece6Schristos /* The funny "do {}" generates better code on most compilers */ 1432aaf4ece6Schristos 1433aaf4ece6Schristos /* Here, scan <= window + strstart + 257 */ 14343b1253e8Schristos Assert(scan <= s->window + (unsigned)(s->window_size - 1), 14353b1253e8Schristos "wild scan"); 1436aaf4ece6Schristos if (*scan == *match) scan++; 1437aaf4ece6Schristos 1438aaf4ece6Schristos len = (MAX_MATCH - 1) - (int)(strend - scan); 1439aaf4ece6Schristos scan = strend - (MAX_MATCH-1); 1440aaf4ece6Schristos 1441aaf4ece6Schristos #else /* UNALIGNED_OK */ 1442aaf4ece6Schristos 1443aaf4ece6Schristos if (match[best_len] != scan_end || 1444aaf4ece6Schristos match[best_len - 1] != scan_end1 || 1445aaf4ece6Schristos *match != *scan || 1446aaf4ece6Schristos *++match != scan[1]) continue; 1447aaf4ece6Schristos 1448aaf4ece6Schristos /* The check at best_len - 1 can be removed because it will be made 1449aaf4ece6Schristos * again later. (This heuristic is not always a win.) 1450aaf4ece6Schristos * It is not necessary to compare scan[2] and match[2] since they 1451aaf4ece6Schristos * are always equal when the other bytes match, given that 1452aaf4ece6Schristos * the hash keys are equal and that HASH_BITS >= 8. 1453aaf4ece6Schristos */ 1454aaf4ece6Schristos scan += 2, match++; 1455aaf4ece6Schristos Assert(*scan == *match, "match[2]?"); 1456aaf4ece6Schristos 1457aaf4ece6Schristos /* We check for insufficient lookahead only every 8th comparison; 1458aaf4ece6Schristos * the 256th check will be made at strstart + 258. 1459aaf4ece6Schristos */ 1460aaf4ece6Schristos do { 1461aaf4ece6Schristos } while (*++scan == *++match && *++scan == *++match && 1462aaf4ece6Schristos *++scan == *++match && *++scan == *++match && 1463aaf4ece6Schristos *++scan == *++match && *++scan == *++match && 1464aaf4ece6Schristos *++scan == *++match && *++scan == *++match && 1465aaf4ece6Schristos scan < strend); 1466aaf4ece6Schristos 14673b1253e8Schristos Assert(scan <= s->window + (unsigned)(s->window_size - 1), 14683b1253e8Schristos "wild scan"); 1469aaf4ece6Schristos 1470aaf4ece6Schristos len = MAX_MATCH - (int)(strend - scan); 1471aaf4ece6Schristos scan = strend - MAX_MATCH; 1472aaf4ece6Schristos 1473aaf4ece6Schristos #endif /* UNALIGNED_OK */ 1474aaf4ece6Schristos 1475aaf4ece6Schristos if (len > best_len) { 1476aaf4ece6Schristos s->match_start = cur_match; 1477aaf4ece6Schristos best_len = len; 1478aaf4ece6Schristos if (len >= nice_match) break; 1479aaf4ece6Schristos #ifdef UNALIGNED_OK 1480aaf4ece6Schristos scan_end = *(ushf*)(scan + best_len - 1); 1481aaf4ece6Schristos #else 1482aaf4ece6Schristos scan_end1 = scan[best_len - 1]; 1483aaf4ece6Schristos scan_end = scan[best_len]; 1484aaf4ece6Schristos #endif 1485aaf4ece6Schristos } 1486aaf4ece6Schristos } while ((cur_match = prev[cur_match & wmask]) > limit 1487aaf4ece6Schristos && --chain_length != 0); 1488aaf4ece6Schristos 1489aaf4ece6Schristos if ((uInt)best_len <= s->lookahead) return (uInt)best_len; 1490aaf4ece6Schristos return s->lookahead; 1491aaf4ece6Schristos } 14926db8c6e9Schristos 14936db8c6e9Schristos #else /* FASTEST */ 1494aaf4ece6Schristos 1495aaf4ece6Schristos /* --------------------------------------------------------------------------- 14966db8c6e9Schristos * Optimized version for FASTEST only 1497aaf4ece6Schristos */ 1498*96c32821Schristos local uInt longest_match(deflate_state *s, IPos cur_match) { 1499aaf4ece6Schristos register Bytef *scan = s->window + s->strstart; /* current string */ 1500aaf4ece6Schristos register Bytef *match; /* matched string */ 1501aaf4ece6Schristos register int len; /* length of current match */ 1502aaf4ece6Schristos register Bytef *strend = s->window + s->strstart + MAX_MATCH; 1503aaf4ece6Schristos 1504aaf4ece6Schristos /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1505aaf4ece6Schristos * It is easy to get rid of this optimization if necessary. 1506aaf4ece6Schristos */ 1507aaf4ece6Schristos Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 1508aaf4ece6Schristos 15093b1253e8Schristos Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, 15103b1253e8Schristos "need lookahead"); 1511aaf4ece6Schristos 1512aaf4ece6Schristos Assert(cur_match < s->strstart, "no future"); 1513aaf4ece6Schristos 1514aaf4ece6Schristos match = s->window + cur_match; 1515aaf4ece6Schristos 1516aaf4ece6Schristos /* Return failure if the match length is less than 2: 1517aaf4ece6Schristos */ 1518aaf4ece6Schristos if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; 1519aaf4ece6Schristos 1520aaf4ece6Schristos /* The check at best_len - 1 can be removed because it will be made 1521aaf4ece6Schristos * again later. (This heuristic is not always a win.) 1522aaf4ece6Schristos * It is not necessary to compare scan[2] and match[2] since they 1523aaf4ece6Schristos * are always equal when the other bytes match, given that 1524aaf4ece6Schristos * the hash keys are equal and that HASH_BITS >= 8. 1525aaf4ece6Schristos */ 1526aaf4ece6Schristos scan += 2, match += 2; 1527aaf4ece6Schristos Assert(*scan == *match, "match[2]?"); 1528aaf4ece6Schristos 1529aaf4ece6Schristos /* We check for insufficient lookahead only every 8th comparison; 1530aaf4ece6Schristos * the 256th check will be made at strstart + 258. 1531aaf4ece6Schristos */ 1532aaf4ece6Schristos do { 1533aaf4ece6Schristos } while (*++scan == *++match && *++scan == *++match && 1534aaf4ece6Schristos *++scan == *++match && *++scan == *++match && 1535aaf4ece6Schristos *++scan == *++match && *++scan == *++match && 1536aaf4ece6Schristos *++scan == *++match && *++scan == *++match && 1537aaf4ece6Schristos scan < strend); 1538aaf4ece6Schristos 1539aaf4ece6Schristos Assert(scan <= s->window + (unsigned)(s->window_size - 1), "wild scan"); 1540aaf4ece6Schristos 1541aaf4ece6Schristos len = MAX_MATCH - (int)(strend - scan); 1542aaf4ece6Schristos 1543aaf4ece6Schristos if (len < MIN_MATCH) return MIN_MATCH - 1; 1544aaf4ece6Schristos 1545aaf4ece6Schristos s->match_start = cur_match; 1546aaf4ece6Schristos return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; 1547aaf4ece6Schristos } 1548aaf4ece6Schristos 15496db8c6e9Schristos #endif /* FASTEST */ 15506db8c6e9Schristos 1551a1f9f4c0Schristos #ifdef ZLIB_DEBUG 15526db8c6e9Schristos 15536db8c6e9Schristos #define EQUAL 0 15546db8c6e9Schristos /* result of memcmp for equal strings */ 15556db8c6e9Schristos 1556aaf4ece6Schristos /* =========================================================================== 1557aaf4ece6Schristos * Check that the match at match_start is indeed a match. 1558aaf4ece6Schristos */ 1559*96c32821Schristos local void check_match(deflate_state *s, IPos start, IPos match, int length) { 1560aaf4ece6Schristos /* check that the match is indeed a match */ 1561*96c32821Schristos Bytef *back = s->window + (int)match, *here = s->window + start; 1562*96c32821Schristos IPos len = length; 1563*96c32821Schristos if (match == (IPos)-1) { 1564*96c32821Schristos /* match starts one byte before the current window -- just compare the 1565*96c32821Schristos subsequent length-1 bytes */ 1566*96c32821Schristos back++; 1567*96c32821Schristos here++; 1568*96c32821Schristos len--; 1569*96c32821Schristos } 1570*96c32821Schristos if (zmemcmp(back, here, len) != EQUAL) { 1571*96c32821Schristos fprintf(stderr, " start %u, match %d, length %d\n", 1572*96c32821Schristos start, (int)match, length); 1573aaf4ece6Schristos do { 1574*96c32821Schristos fprintf(stderr, "(%02x %02x)", *back++, *here++); 1575*96c32821Schristos } while (--len != 0); 1576aaf4ece6Schristos z_error("invalid match"); 1577aaf4ece6Schristos } 1578aaf4ece6Schristos if (z_verbose > 1) { 1579aaf4ece6Schristos fprintf(stderr,"\\[%d,%d]", start - match, length); 1580aaf4ece6Schristos do { putc(s->window[start++], stderr); } while (--length != 0); 1581aaf4ece6Schristos } 1582aaf4ece6Schristos } 1583aaf4ece6Schristos #else 1584aaf4ece6Schristos # define check_match(s, start, match, length) 1585a1f9f4c0Schristos #endif /* ZLIB_DEBUG */ 1586aaf4ece6Schristos 1587aaf4ece6Schristos /* =========================================================================== 1588aaf4ece6Schristos * Flush the current block, with given end-of-file flag. 1589aaf4ece6Schristos * IN assertion: strstart is set to the end of the current match. 1590aaf4ece6Schristos */ 15916db8c6e9Schristos #define FLUSH_BLOCK_ONLY(s, last) { \ 1592aaf4ece6Schristos _tr_flush_block(s, (s->block_start >= 0L ? \ 1593aaf4ece6Schristos (charf *)&s->window[(unsigned)s->block_start] : \ 1594aaf4ece6Schristos (charf *)Z_NULL), \ 1595aaf4ece6Schristos (ulg)((long)s->strstart - s->block_start), \ 15966db8c6e9Schristos (last)); \ 1597aaf4ece6Schristos s->block_start = s->strstart; \ 1598aaf4ece6Schristos flush_pending(s->strm); \ 1599aaf4ece6Schristos Tracev((stderr,"[FLUSH]")); \ 1600aaf4ece6Schristos } 1601aaf4ece6Schristos 1602aaf4ece6Schristos /* Same but force premature exit if necessary. */ 16036db8c6e9Schristos #define FLUSH_BLOCK(s, last) { \ 16046db8c6e9Schristos FLUSH_BLOCK_ONLY(s, last); \ 16056db8c6e9Schristos if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ 1606aaf4ece6Schristos } 1607aaf4ece6Schristos 16086db8c6e9Schristos /* Maximum stored block length in deflate format (not including header). */ 16096db8c6e9Schristos #define MAX_STORED 65535 16106db8c6e9Schristos 16116db8c6e9Schristos /* Minimum of a and b. */ 16126db8c6e9Schristos #define MIN(a, b) ((a) > (b) ? (b) : (a)) 16136db8c6e9Schristos 1614aaf4ece6Schristos /* =========================================================================== 1615aaf4ece6Schristos * Copy without compression as much as possible from the input stream, return 1616aaf4ece6Schristos * the current block state. 16176db8c6e9Schristos * 16186db8c6e9Schristos * In case deflateParams() is used to later switch to a non-zero compression 16196db8c6e9Schristos * level, s->matches (otherwise unused when storing) keeps track of the number 16206db8c6e9Schristos * of hash table slides to perform. If s->matches is 1, then one hash table 16216db8c6e9Schristos * slide will be done when switching. If s->matches is 2, the maximum value 16226db8c6e9Schristos * allowed here, then the hash table will be cleared, since two or more slides 16236db8c6e9Schristos * is the same as a clear. 16246db8c6e9Schristos * 16256db8c6e9Schristos * deflate_stored() is written to minimize the number of times an input byte is 16266db8c6e9Schristos * copied. It is most efficient with large input and output buffers, which 16273b1253e8Schristos * maximizes the opportunities to have a single copy from next_in to next_out. 1628aaf4ece6Schristos */ 1629*96c32821Schristos local block_state deflate_stored(deflate_state *s, int flush) { 16306db8c6e9Schristos /* Smallest worthy block size when not flushing or finishing. By default 16316db8c6e9Schristos * this is 32K. This can be as small as 507 bytes for memLevel == 1. For 16326db8c6e9Schristos * large input and output buffers, the stored block size will be larger. 1633aaf4ece6Schristos */ 16346db8c6e9Schristos unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size); 1635aaf4ece6Schristos 16366db8c6e9Schristos /* Copy as many min_block or larger stored blocks directly to next_out as 16376db8c6e9Schristos * possible. If flushing, copy the remaining available input to next_out as 16386db8c6e9Schristos * stored blocks, if there is enough space. 1639aaf4ece6Schristos */ 16406db8c6e9Schristos unsigned len, left, have, last = 0; 16416db8c6e9Schristos unsigned used = s->strm->avail_in; 16426db8c6e9Schristos do { 16436db8c6e9Schristos /* Set len to the maximum size block that we can copy directly with the 16446db8c6e9Schristos * available input data and output space. Set left to how much of that 16456db8c6e9Schristos * would be copied from what's left in the window. 16466db8c6e9Schristos */ 16476db8c6e9Schristos len = MAX_STORED; /* maximum deflate stored block length */ 16486db8c6e9Schristos have = (s->bi_valid + 42) >> 3; /* number of header bytes */ 16496db8c6e9Schristos if (s->strm->avail_out < have) /* need room for header */ 16506db8c6e9Schristos break; 16516db8c6e9Schristos /* maximum stored block length that will fit in avail_out: */ 16526db8c6e9Schristos have = s->strm->avail_out - have; 16536db8c6e9Schristos left = s->strstart - s->block_start; /* bytes left in window */ 16546db8c6e9Schristos if (len > (ulg)left + s->strm->avail_in) 16556db8c6e9Schristos len = left + s->strm->avail_in; /* limit len to the input */ 16566db8c6e9Schristos if (len > have) 16576db8c6e9Schristos len = have; /* limit len to the output */ 16586db8c6e9Schristos 16596db8c6e9Schristos /* If the stored block would be less than min_block in length, or if 16606db8c6e9Schristos * unable to copy all of the available input when flushing, then try 16616db8c6e9Schristos * copying to the window and the pending buffer instead. Also don't 16626db8c6e9Schristos * write an empty block when flushing -- deflate() does that. 16636db8c6e9Schristos */ 16646db8c6e9Schristos if (len < min_block && ((len == 0 && flush != Z_FINISH) || 16656db8c6e9Schristos flush == Z_NO_FLUSH || 16663b1253e8Schristos len != left + s->strm->avail_in)) 16676db8c6e9Schristos break; 16686db8c6e9Schristos 16696db8c6e9Schristos /* Make a dummy stored block in pending to get the header bytes, 16706db8c6e9Schristos * including any pending bits. This also updates the debugging counts. 16716db8c6e9Schristos */ 16723b1253e8Schristos last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; 16736db8c6e9Schristos _tr_stored_block(s, (char *)0, 0L, last); 16746db8c6e9Schristos 16756db8c6e9Schristos /* Replace the lengths in the dummy stored block with len. */ 16766db8c6e9Schristos s->pending_buf[s->pending - 4] = len; 16776db8c6e9Schristos s->pending_buf[s->pending - 3] = len >> 8; 16786db8c6e9Schristos s->pending_buf[s->pending - 2] = ~len; 16796db8c6e9Schristos s->pending_buf[s->pending - 1] = ~len >> 8; 16806db8c6e9Schristos 16816db8c6e9Schristos /* Write the stored block header bytes. */ 16826db8c6e9Schristos flush_pending(s->strm); 16836db8c6e9Schristos 16846db8c6e9Schristos #ifdef ZLIB_DEBUG 16853b1253e8Schristos /* Update debugging counts for the data about to be copied. */ 16866db8c6e9Schristos s->compressed_len += len << 3; 16876db8c6e9Schristos s->bits_sent += len << 3; 16886db8c6e9Schristos #endif 16896db8c6e9Schristos 16906db8c6e9Schristos /* Copy uncompressed bytes from the window to next_out. */ 16916db8c6e9Schristos if (left) { 16923b1253e8Schristos if (left > len) 16933b1253e8Schristos left = len; 16946db8c6e9Schristos zmemcpy(s->strm->next_out, s->window + s->block_start, left); 16956db8c6e9Schristos s->strm->next_out += left; 16966db8c6e9Schristos s->strm->avail_out -= left; 16976db8c6e9Schristos s->strm->total_out += left; 16986db8c6e9Schristos s->block_start += left; 16996db8c6e9Schristos len -= left; 1700aaf4ece6Schristos } 17016db8c6e9Schristos 17026db8c6e9Schristos /* Copy uncompressed bytes directly from next_in to next_out, updating 17036db8c6e9Schristos * the check value. 17046db8c6e9Schristos */ 17056db8c6e9Schristos if (len) { 17066db8c6e9Schristos read_buf(s->strm, s->strm->next_out, len); 17076db8c6e9Schristos s->strm->next_out += len; 17086db8c6e9Schristos s->strm->avail_out -= len; 17096db8c6e9Schristos s->strm->total_out += len; 1710aaf4ece6Schristos } 17116db8c6e9Schristos } while (last == 0); 17126db8c6e9Schristos 17136db8c6e9Schristos /* Update the sliding window with the last s->w_size bytes of the copied 17146db8c6e9Schristos * data, or append all of the copied data to the existing window if less 17156db8c6e9Schristos * than s->w_size bytes were copied. Also update the number of bytes to 17166db8c6e9Schristos * insert in the hash tables, in the event that deflateParams() switches to 17176db8c6e9Schristos * a non-zero compression level. 17186db8c6e9Schristos */ 17196db8c6e9Schristos used -= s->strm->avail_in; /* number of input bytes directly copied */ 17206db8c6e9Schristos if (used) { 17216db8c6e9Schristos /* If any input was used, then no unused input remains in the window, 17226db8c6e9Schristos * therefore s->block_start == s->strstart. 17236db8c6e9Schristos */ 17246db8c6e9Schristos if (used >= s->w_size) { /* supplant the previous history */ 17256db8c6e9Schristos s->matches = 2; /* clear hash */ 17266db8c6e9Schristos zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); 17276db8c6e9Schristos s->strstart = s->w_size; 17283b1253e8Schristos s->insert = s->strstart; 17296db8c6e9Schristos } 17306db8c6e9Schristos else { 17316db8c6e9Schristos if (s->window_size - s->strstart <= used) { 17326db8c6e9Schristos /* Slide the window down. */ 17336db8c6e9Schristos s->strstart -= s->w_size; 17346db8c6e9Schristos zmemcpy(s->window, s->window + s->w_size, s->strstart); 17356db8c6e9Schristos if (s->matches < 2) 17366db8c6e9Schristos s->matches++; /* add a pending slide_hash() */ 17373b1253e8Schristos if (s->insert > s->strstart) 17383b1253e8Schristos s->insert = s->strstart; 17396db8c6e9Schristos } 17406db8c6e9Schristos zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); 17416db8c6e9Schristos s->strstart += used; 17426db8c6e9Schristos s->insert += MIN(used, s->w_size - s->insert); 17436db8c6e9Schristos } 17443b1253e8Schristos s->block_start = s->strstart; 17453b1253e8Schristos } 17463b1253e8Schristos if (s->high_water < s->strstart) 17473b1253e8Schristos s->high_water = s->strstart; 17486db8c6e9Schristos 17496db8c6e9Schristos /* If the last block was written to next_out, then done. */ 17506db8c6e9Schristos if (last) 17516db8c6e9Schristos return finish_done; 17526db8c6e9Schristos 17536db8c6e9Schristos /* If flushing and all input has been consumed, then done. */ 17546db8c6e9Schristos if (flush != Z_NO_FLUSH && flush != Z_FINISH && 17556db8c6e9Schristos s->strm->avail_in == 0 && (long)s->strstart == s->block_start) 17566db8c6e9Schristos return block_done; 17576db8c6e9Schristos 17586db8c6e9Schristos /* Fill the window with any remaining input. */ 17593b1253e8Schristos have = s->window_size - s->strstart; 17606db8c6e9Schristos if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { 17616db8c6e9Schristos /* Slide the window down. */ 17626db8c6e9Schristos s->block_start -= s->w_size; 17636db8c6e9Schristos s->strstart -= s->w_size; 17646db8c6e9Schristos zmemcpy(s->window, s->window + s->w_size, s->strstart); 17656db8c6e9Schristos if (s->matches < 2) 17666db8c6e9Schristos s->matches++; /* add a pending slide_hash() */ 17676db8c6e9Schristos have += s->w_size; /* more space now */ 17683b1253e8Schristos if (s->insert > s->strstart) 17693b1253e8Schristos s->insert = s->strstart; 17706db8c6e9Schristos } 17716db8c6e9Schristos if (have > s->strm->avail_in) 17726db8c6e9Schristos have = s->strm->avail_in; 17736db8c6e9Schristos if (have) { 17746db8c6e9Schristos read_buf(s->strm, s->window + s->strstart, have); 17756db8c6e9Schristos s->strstart += have; 17763b1253e8Schristos s->insert += MIN(have, s->w_size - s->insert); 17776db8c6e9Schristos } 17783b1253e8Schristos if (s->high_water < s->strstart) 17793b1253e8Schristos s->high_water = s->strstart; 17806db8c6e9Schristos 17816db8c6e9Schristos /* There was not enough avail_out to write a complete worthy or flushed 17826db8c6e9Schristos * stored block to next_out. Write a stored block to pending instead, if we 17836db8c6e9Schristos * have enough input for a worthy block, or if flushing and there is enough 17846db8c6e9Schristos * room for the remaining input as a stored block in the pending buffer. 17856db8c6e9Schristos */ 17866db8c6e9Schristos have = (s->bi_valid + 42) >> 3; /* number of header bytes */ 17876db8c6e9Schristos /* maximum stored block length that will fit in pending: */ 17886db8c6e9Schristos have = MIN(s->pending_buf_size - have, MAX_STORED); 17896db8c6e9Schristos min_block = MIN(have, s->w_size); 17906db8c6e9Schristos left = s->strstart - s->block_start; 17916db8c6e9Schristos if (left >= min_block || 17926db8c6e9Schristos ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && 17936db8c6e9Schristos s->strm->avail_in == 0 && left <= have)) { 17946db8c6e9Schristos len = MIN(left, have); 17956db8c6e9Schristos last = flush == Z_FINISH && s->strm->avail_in == 0 && 17966db8c6e9Schristos len == left ? 1 : 0; 17976db8c6e9Schristos _tr_stored_block(s, (charf *)s->window + s->block_start, len, last); 17986db8c6e9Schristos s->block_start += len; 17996db8c6e9Schristos flush_pending(s->strm); 18006db8c6e9Schristos } 18016db8c6e9Schristos 18026db8c6e9Schristos /* We've done all we can with the available input and output. */ 18036db8c6e9Schristos return last ? finish_started : need_more; 1804aaf4ece6Schristos } 1805aaf4ece6Schristos 1806aaf4ece6Schristos /* =========================================================================== 1807aaf4ece6Schristos * Compress as much as possible from the input stream, return the current 1808aaf4ece6Schristos * block state. 1809aaf4ece6Schristos * This function does not perform lazy evaluation of matches and inserts 1810aaf4ece6Schristos * new strings in the dictionary only for unmatched strings or for short 1811aaf4ece6Schristos * matches. It is used only for the fast compression options. 1812aaf4ece6Schristos */ 1813*96c32821Schristos local block_state deflate_fast(deflate_state *s, int flush) { 18146db8c6e9Schristos IPos hash_head; /* head of the hash chain */ 1815aaf4ece6Schristos int bflush; /* set if current block must be flushed */ 1816aaf4ece6Schristos 1817aaf4ece6Schristos for (;;) { 1818aaf4ece6Schristos /* Make sure that we always have enough lookahead, except 1819aaf4ece6Schristos * at the end of the input file. We need MAX_MATCH bytes 1820aaf4ece6Schristos * for the next match, plus MIN_MATCH bytes to insert the 1821aaf4ece6Schristos * string following the next match. 1822aaf4ece6Schristos */ 1823aaf4ece6Schristos if (s->lookahead < MIN_LOOKAHEAD) { 1824aaf4ece6Schristos fill_window(s); 1825aaf4ece6Schristos if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1826aaf4ece6Schristos return need_more; 1827aaf4ece6Schristos } 1828aaf4ece6Schristos if (s->lookahead == 0) break; /* flush the current block */ 1829aaf4ece6Schristos } 1830aaf4ece6Schristos 1831aaf4ece6Schristos /* Insert the string window[strstart .. strstart + 2] in the 1832aaf4ece6Schristos * dictionary, and set hash_head to the head of the hash chain: 1833aaf4ece6Schristos */ 18346db8c6e9Schristos hash_head = NIL; 1835aaf4ece6Schristos if (s->lookahead >= MIN_MATCH) { 1836aaf4ece6Schristos INSERT_STRING(s, s->strstart, hash_head); 1837aaf4ece6Schristos } 1838aaf4ece6Schristos 1839aaf4ece6Schristos /* Find the longest match, discarding those <= prev_length. 1840aaf4ece6Schristos * At this point we have always match_length < MIN_MATCH 1841aaf4ece6Schristos */ 1842aaf4ece6Schristos if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { 1843aaf4ece6Schristos /* To simplify the code, we prevent matches with the string 1844aaf4ece6Schristos * of window index 0 (in particular we have to avoid a match 1845aaf4ece6Schristos * of the string with itself at the start of the input file). 1846aaf4ece6Schristos */ 1847aaf4ece6Schristos s->match_length = longest_match (s, hash_head); 18486db8c6e9Schristos /* longest_match() sets match_start */ 1849aaf4ece6Schristos } 1850aaf4ece6Schristos if (s->match_length >= MIN_MATCH) { 1851aaf4ece6Schristos check_match(s, s->strstart, s->match_start, s->match_length); 1852aaf4ece6Schristos 1853aaf4ece6Schristos _tr_tally_dist(s, s->strstart - s->match_start, 1854aaf4ece6Schristos s->match_length - MIN_MATCH, bflush); 1855aaf4ece6Schristos 1856aaf4ece6Schristos s->lookahead -= s->match_length; 1857aaf4ece6Schristos 1858aaf4ece6Schristos /* Insert new strings in the hash table only if the match length 1859aaf4ece6Schristos * is not too large. This saves time but degrades compression. 1860aaf4ece6Schristos */ 1861aaf4ece6Schristos #ifndef FASTEST 1862aaf4ece6Schristos if (s->match_length <= s->max_insert_length && 1863aaf4ece6Schristos s->lookahead >= MIN_MATCH) { 1864aaf4ece6Schristos s->match_length--; /* string at strstart already in table */ 1865aaf4ece6Schristos do { 1866aaf4ece6Schristos s->strstart++; 1867aaf4ece6Schristos INSERT_STRING(s, s->strstart, hash_head); 1868aaf4ece6Schristos /* strstart never exceeds WSIZE-MAX_MATCH, so there are 1869aaf4ece6Schristos * always MIN_MATCH bytes ahead. 1870aaf4ece6Schristos */ 1871aaf4ece6Schristos } while (--s->match_length != 0); 1872aaf4ece6Schristos s->strstart++; 1873aaf4ece6Schristos } else 1874aaf4ece6Schristos #endif 1875aaf4ece6Schristos { 1876aaf4ece6Schristos s->strstart += s->match_length; 1877aaf4ece6Schristos s->match_length = 0; 1878aaf4ece6Schristos s->ins_h = s->window[s->strstart]; 1879aaf4ece6Schristos UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]); 1880aaf4ece6Schristos #if MIN_MATCH != 3 1881aaf4ece6Schristos Call UPDATE_HASH() MIN_MATCH-3 more times 1882aaf4ece6Schristos #endif 1883aaf4ece6Schristos /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not 1884aaf4ece6Schristos * matter since it will be recomputed at next deflate call. 1885aaf4ece6Schristos */ 1886aaf4ece6Schristos } 1887aaf4ece6Schristos } else { 1888aaf4ece6Schristos /* No match, output a literal byte */ 1889aaf4ece6Schristos Tracevv((stderr,"%c", s->window[s->strstart])); 1890aaf4ece6Schristos _tr_tally_lit(s, s->window[s->strstart], bflush); 1891aaf4ece6Schristos s->lookahead--; 1892aaf4ece6Schristos s->strstart++; 1893aaf4ece6Schristos } 1894aaf4ece6Schristos if (bflush) FLUSH_BLOCK(s, 0); 1895aaf4ece6Schristos } 18966db8c6e9Schristos s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 18976db8c6e9Schristos if (flush == Z_FINISH) { 18986db8c6e9Schristos FLUSH_BLOCK(s, 1); 18996db8c6e9Schristos return finish_done; 19006db8c6e9Schristos } 19010362f707Swiz if (s->sym_next) 19026db8c6e9Schristos FLUSH_BLOCK(s, 0); 19036db8c6e9Schristos return block_done; 1904aaf4ece6Schristos } 1905aaf4ece6Schristos 1906aaf4ece6Schristos #ifndef FASTEST 1907aaf4ece6Schristos /* =========================================================================== 1908aaf4ece6Schristos * Same as above, but achieves better compression. We use a lazy 1909aaf4ece6Schristos * evaluation for matches: a match is finally adopted only if there is 1910aaf4ece6Schristos * no better match at the next window position. 1911aaf4ece6Schristos */ 1912*96c32821Schristos local block_state deflate_slow(deflate_state *s, int flush) { 19136db8c6e9Schristos IPos hash_head; /* head of hash chain */ 1914aaf4ece6Schristos int bflush; /* set if current block must be flushed */ 1915aaf4ece6Schristos 1916aaf4ece6Schristos /* Process the input block. */ 1917aaf4ece6Schristos for (;;) { 1918aaf4ece6Schristos /* Make sure that we always have enough lookahead, except 1919aaf4ece6Schristos * at the end of the input file. We need MAX_MATCH bytes 1920aaf4ece6Schristos * for the next match, plus MIN_MATCH bytes to insert the 1921aaf4ece6Schristos * string following the next match. 1922aaf4ece6Schristos */ 1923aaf4ece6Schristos if (s->lookahead < MIN_LOOKAHEAD) { 1924aaf4ece6Schristos fill_window(s); 1925aaf4ece6Schristos if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1926aaf4ece6Schristos return need_more; 1927aaf4ece6Schristos } 1928aaf4ece6Schristos if (s->lookahead == 0) break; /* flush the current block */ 1929aaf4ece6Schristos } 1930aaf4ece6Schristos 1931aaf4ece6Schristos /* Insert the string window[strstart .. strstart + 2] in the 1932aaf4ece6Schristos * dictionary, and set hash_head to the head of the hash chain: 1933aaf4ece6Schristos */ 19346db8c6e9Schristos hash_head = NIL; 1935aaf4ece6Schristos if (s->lookahead >= MIN_MATCH) { 1936aaf4ece6Schristos INSERT_STRING(s, s->strstart, hash_head); 1937aaf4ece6Schristos } 1938aaf4ece6Schristos 1939aaf4ece6Schristos /* Find the longest match, discarding those <= prev_length. 1940aaf4ece6Schristos */ 1941aaf4ece6Schristos s->prev_length = s->match_length, s->prev_match = s->match_start; 1942aaf4ece6Schristos s->match_length = MIN_MATCH-1; 1943aaf4ece6Schristos 1944aaf4ece6Schristos if (hash_head != NIL && s->prev_length < s->max_lazy_match && 1945aaf4ece6Schristos s->strstart - hash_head <= MAX_DIST(s)) { 1946aaf4ece6Schristos /* To simplify the code, we prevent matches with the string 1947aaf4ece6Schristos * of window index 0 (in particular we have to avoid a match 1948aaf4ece6Schristos * of the string with itself at the start of the input file). 1949aaf4ece6Schristos */ 1950aaf4ece6Schristos s->match_length = longest_match (s, hash_head); 19516db8c6e9Schristos /* longest_match() sets match_start */ 1952aaf4ece6Schristos 1953aaf4ece6Schristos if (s->match_length <= 5 && (s->strategy == Z_FILTERED 1954aaf4ece6Schristos #if TOO_FAR <= 32767 1955aaf4ece6Schristos || (s->match_length == MIN_MATCH && 1956aaf4ece6Schristos s->strstart - s->match_start > TOO_FAR) 1957aaf4ece6Schristos #endif 1958aaf4ece6Schristos )) { 1959aaf4ece6Schristos 1960aaf4ece6Schristos /* If prev_match is also MIN_MATCH, match_start is garbage 1961aaf4ece6Schristos * but we will ignore the current match anyway. 1962aaf4ece6Schristos */ 1963aaf4ece6Schristos s->match_length = MIN_MATCH-1; 1964aaf4ece6Schristos } 1965aaf4ece6Schristos } 1966aaf4ece6Schristos /* If there was a match at the previous step and the current 1967aaf4ece6Schristos * match is not better, output the previous match: 1968aaf4ece6Schristos */ 1969aaf4ece6Schristos if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { 1970aaf4ece6Schristos uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 1971aaf4ece6Schristos /* Do not insert strings in hash table beyond this. */ 1972aaf4ece6Schristos 1973aaf4ece6Schristos check_match(s, s->strstart - 1, s->prev_match, s->prev_length); 1974aaf4ece6Schristos 1975aaf4ece6Schristos _tr_tally_dist(s, s->strstart - 1 - s->prev_match, 1976aaf4ece6Schristos s->prev_length - MIN_MATCH, bflush); 1977aaf4ece6Schristos 1978aaf4ece6Schristos /* Insert in hash table all strings up to the end of the match. 1979aaf4ece6Schristos * strstart - 1 and strstart are already inserted. If there is not 1980aaf4ece6Schristos * enough lookahead, the last two strings are not inserted in 1981aaf4ece6Schristos * the hash table. 1982aaf4ece6Schristos */ 1983aaf4ece6Schristos s->lookahead -= s->prev_length - 1; 1984aaf4ece6Schristos s->prev_length -= 2; 1985aaf4ece6Schristos do { 1986aaf4ece6Schristos if (++s->strstart <= max_insert) { 1987aaf4ece6Schristos INSERT_STRING(s, s->strstart, hash_head); 1988aaf4ece6Schristos } 1989aaf4ece6Schristos } while (--s->prev_length != 0); 1990aaf4ece6Schristos s->match_available = 0; 1991aaf4ece6Schristos s->match_length = MIN_MATCH-1; 1992aaf4ece6Schristos s->strstart++; 1993aaf4ece6Schristos 1994aaf4ece6Schristos if (bflush) FLUSH_BLOCK(s, 0); 1995aaf4ece6Schristos 1996aaf4ece6Schristos } else if (s->match_available) { 1997aaf4ece6Schristos /* If there was no match at the previous position, output a 1998aaf4ece6Schristos * single literal. If there was a match but the current match 1999aaf4ece6Schristos * is longer, truncate the previous match to a single literal. 2000aaf4ece6Schristos */ 2001aaf4ece6Schristos Tracevv((stderr,"%c", s->window[s->strstart - 1])); 2002aaf4ece6Schristos _tr_tally_lit(s, s->window[s->strstart - 1], bflush); 2003aaf4ece6Schristos if (bflush) { 2004aaf4ece6Schristos FLUSH_BLOCK_ONLY(s, 0); 2005aaf4ece6Schristos } 2006aaf4ece6Schristos s->strstart++; 2007aaf4ece6Schristos s->lookahead--; 2008aaf4ece6Schristos if (s->strm->avail_out == 0) return need_more; 2009aaf4ece6Schristos } else { 2010aaf4ece6Schristos /* There is no previous match to compare with, wait for 2011aaf4ece6Schristos * the next step to decide. 2012aaf4ece6Schristos */ 2013aaf4ece6Schristos s->match_available = 1; 2014aaf4ece6Schristos s->strstart++; 2015aaf4ece6Schristos s->lookahead--; 2016aaf4ece6Schristos } 2017aaf4ece6Schristos } 2018aaf4ece6Schristos Assert (flush != Z_NO_FLUSH, "no flush?"); 2019aaf4ece6Schristos if (s->match_available) { 2020aaf4ece6Schristos Tracevv((stderr,"%c", s->window[s->strstart - 1])); 2021aaf4ece6Schristos _tr_tally_lit(s, s->window[s->strstart - 1], bflush); 2022aaf4ece6Schristos s->match_available = 0; 2023aaf4ece6Schristos } 20246db8c6e9Schristos s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 20256db8c6e9Schristos if (flush == Z_FINISH) { 20266db8c6e9Schristos FLUSH_BLOCK(s, 1); 20276db8c6e9Schristos return finish_done; 20286db8c6e9Schristos } 20290362f707Swiz if (s->sym_next) 20306db8c6e9Schristos FLUSH_BLOCK(s, 0); 20316db8c6e9Schristos return block_done; 2032aaf4ece6Schristos } 2033aaf4ece6Schristos #endif /* FASTEST */ 2034aaf4ece6Schristos 2035aaf4ece6Schristos /* =========================================================================== 2036aaf4ece6Schristos * For Z_RLE, simply look for runs of bytes, generate matches only of distance 2037aaf4ece6Schristos * one. Do not maintain a hash table. (It will be regenerated if this run of 2038aaf4ece6Schristos * deflate switches away from Z_RLE.) 2039aaf4ece6Schristos */ 2040*96c32821Schristos local block_state deflate_rle(deflate_state *s, int flush) { 2041aaf4ece6Schristos int bflush; /* set if current block must be flushed */ 2042aaf4ece6Schristos uInt prev; /* byte at distance one to match */ 20436db8c6e9Schristos Bytef *scan, *strend; /* scan goes up to strend for length of run */ 2044aaf4ece6Schristos 2045aaf4ece6Schristos for (;;) { 2046aaf4ece6Schristos /* Make sure that we always have enough lookahead, except 2047aaf4ece6Schristos * at the end of the input file. We need MAX_MATCH bytes 20486db8c6e9Schristos * for the longest run, plus one for the unrolled loop. 2049aaf4ece6Schristos */ 20506db8c6e9Schristos if (s->lookahead <= MAX_MATCH) { 2051aaf4ece6Schristos fill_window(s); 20526db8c6e9Schristos if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { 2053aaf4ece6Schristos return need_more; 2054aaf4ece6Schristos } 2055aaf4ece6Schristos if (s->lookahead == 0) break; /* flush the current block */ 2056aaf4ece6Schristos } 2057aaf4ece6Schristos 2058aaf4ece6Schristos /* See how many times the previous byte repeats */ 20596db8c6e9Schristos s->match_length = 0; 20606db8c6e9Schristos if (s->lookahead >= MIN_MATCH && s->strstart > 0) { 2061aaf4ece6Schristos scan = s->window + s->strstart - 1; 20626db8c6e9Schristos prev = *scan; 20636db8c6e9Schristos if (prev == *++scan && prev == *++scan && prev == *++scan) { 20646db8c6e9Schristos strend = s->window + s->strstart + MAX_MATCH; 2065aaf4ece6Schristos do { 20666db8c6e9Schristos } while (prev == *++scan && prev == *++scan && 20676db8c6e9Schristos prev == *++scan && prev == *++scan && 20686db8c6e9Schristos prev == *++scan && prev == *++scan && 20696db8c6e9Schristos prev == *++scan && prev == *++scan && 20706db8c6e9Schristos scan < strend); 20716db8c6e9Schristos s->match_length = MAX_MATCH - (uInt)(strend - scan); 20726db8c6e9Schristos if (s->match_length > s->lookahead) 20736db8c6e9Schristos s->match_length = s->lookahead; 20746db8c6e9Schristos } 20753b1253e8Schristos Assert(scan <= s->window + (uInt)(s->window_size - 1), 20763b1253e8Schristos "wild scan"); 2077aaf4ece6Schristos } 2078aaf4ece6Schristos 2079aaf4ece6Schristos /* Emit match if have run of MIN_MATCH or longer, else emit literal */ 20806db8c6e9Schristos if (s->match_length >= MIN_MATCH) { 20816db8c6e9Schristos check_match(s, s->strstart, s->strstart - 1, s->match_length); 20826db8c6e9Schristos 20836db8c6e9Schristos _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); 20846db8c6e9Schristos 20856db8c6e9Schristos s->lookahead -= s->match_length; 20866db8c6e9Schristos s->strstart += s->match_length; 20876db8c6e9Schristos s->match_length = 0; 2088aaf4ece6Schristos } else { 2089aaf4ece6Schristos /* No match, output a literal byte */ 2090aaf4ece6Schristos Tracevv((stderr,"%c", s->window[s->strstart])); 2091aaf4ece6Schristos _tr_tally_lit(s, s->window[s->strstart], bflush); 2092aaf4ece6Schristos s->lookahead--; 2093aaf4ece6Schristos s->strstart++; 2094aaf4ece6Schristos } 2095aaf4ece6Schristos if (bflush) FLUSH_BLOCK(s, 0); 2096aaf4ece6Schristos } 20976db8c6e9Schristos s->insert = 0; 20986db8c6e9Schristos if (flush == Z_FINISH) { 20996db8c6e9Schristos FLUSH_BLOCK(s, 1); 21006db8c6e9Schristos return finish_done; 2101aaf4ece6Schristos } 21020362f707Swiz if (s->sym_next) 21036db8c6e9Schristos FLUSH_BLOCK(s, 0); 21046db8c6e9Schristos return block_done; 21056db8c6e9Schristos } 21066db8c6e9Schristos 21076db8c6e9Schristos /* =========================================================================== 21086db8c6e9Schristos * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. 21096db8c6e9Schristos * (It will be regenerated if this run of deflate switches away from Huffman.) 21106db8c6e9Schristos */ 2111*96c32821Schristos local block_state deflate_huff(deflate_state *s, int flush) { 21126db8c6e9Schristos int bflush; /* set if current block must be flushed */ 21136db8c6e9Schristos 21146db8c6e9Schristos for (;;) { 21156db8c6e9Schristos /* Make sure that we have a literal to write. */ 21166db8c6e9Schristos if (s->lookahead == 0) { 21176db8c6e9Schristos fill_window(s); 21186db8c6e9Schristos if (s->lookahead == 0) { 21196db8c6e9Schristos if (flush == Z_NO_FLUSH) 21206db8c6e9Schristos return need_more; 21216db8c6e9Schristos break; /* flush the current block */ 21226db8c6e9Schristos } 21236db8c6e9Schristos } 21246db8c6e9Schristos 21256db8c6e9Schristos /* Output a literal byte */ 21266db8c6e9Schristos s->match_length = 0; 21276db8c6e9Schristos Tracevv((stderr,"%c", s->window[s->strstart])); 21286db8c6e9Schristos _tr_tally_lit(s, s->window[s->strstart], bflush); 21296db8c6e9Schristos s->lookahead--; 21306db8c6e9Schristos s->strstart++; 21316db8c6e9Schristos if (bflush) FLUSH_BLOCK(s, 0); 21326db8c6e9Schristos } 21336db8c6e9Schristos s->insert = 0; 21346db8c6e9Schristos if (flush == Z_FINISH) { 21356db8c6e9Schristos FLUSH_BLOCK(s, 1); 21366db8c6e9Schristos return finish_done; 21376db8c6e9Schristos } 21380362f707Swiz if (s->sym_next) 21396db8c6e9Schristos FLUSH_BLOCK(s, 0); 21406db8c6e9Schristos return block_done; 21416db8c6e9Schristos } 2142