Geant4 Cross Reference |
1 /* deflate.c -- compress data using the deflat 1 /* deflate.c -- compress data using the deflation algorithm 2 * Copyright (C) 1995-2022 Jean-loup Gailly an << 2 * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler 3 * For conditions of distribution and use, see 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 4 */ 5 5 6 /* 6 /* 7 * ALGORITHM 7 * ALGORITHM 8 * 8 * 9 * The "deflation" process depends on bei 9 * The "deflation" process depends on being able to identify portions 10 * of the input text which are identical 10 * of the input text which are identical to earlier input (within a 11 * sliding window trailing behind the inp 11 * sliding window trailing behind the input currently being processed). 12 * 12 * 13 * The most straightforward technique tur 13 * The most straightforward technique turns out to be the fastest for 14 * most input files: try all possible mat 14 * most input files: try all possible matches and select the longest. 15 * The key feature of this algorithm is t 15 * The key feature of this algorithm is that insertions into the string 16 * dictionary are very simple and thus fa 16 * dictionary are very simple and thus fast, and deletions are avoided 17 * completely. Insertions are performed a 17 * completely. Insertions are performed at each input character, whereas 18 * string matches are performed only when 18 * string matches are performed only when the previous match ends. So it 19 * is preferable to spend more time in ma 19 * is preferable to spend more time in matches to allow very fast string 20 * insertions and avoid deletions. The ma 20 * insertions and avoid deletions. The matching algorithm for small 21 * strings is inspired from that of Rabin 21 * strings is inspired from that of Rabin & Karp. A brute force approach 22 * is used to find longer strings when a 22 * is used to find longer strings when a small match has been found. 23 * A similar algorithm is used in comic ( 23 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze 24 * (by Leonid Broukhis). 24 * (by Leonid Broukhis). 25 * A previous version of this file use 25 * A previous version of this file used a more sophisticated algorithm 26 * (by Fiala and Greene) which is guarant 26 * (by Fiala and Greene) which is guaranteed to run in linear amortized 27 * time, but has a larger average cost, u 27 * time, but has a larger average cost, uses more memory and is patented. 28 * However the F&G algorithm may be faste 28 * However the F&G algorithm may be faster for some highly redundant 29 * files if the parameter max_chain_lengt 29 * files if the parameter max_chain_length (described below) is too large. 30 * 30 * 31 * ACKNOWLEDGEMENTS 31 * ACKNOWLEDGEMENTS 32 * 32 * 33 * The idea of lazy evaluation of matches 33 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and 34 * I found it in 'freeze' written by Leon 34 * I found it in 'freeze' written by Leonid Broukhis. 35 * Thanks to many people for bug reports 35 * Thanks to many people for bug reports and testing. 36 * 36 * 37 * REFERENCES 37 * REFERENCES 38 * 38 * 39 * Deutsch, L.P.,"DEFLATE Compressed Data 39 * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". 40 * Available in http://tools.ietf.org/htm 40 * Available in http://tools.ietf.org/html/rfc1951 41 * 41 * 42 * A description of the Rabin and Karp al 42 * A description of the Rabin and Karp algorithm is given in the book 43 * "Algorithms" by R. Sedgewick, Addis 43 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. 44 * 44 * 45 * Fiala,E.R., and Greene,D.H. 45 * Fiala,E.R., and Greene,D.H. 46 * Data Compression with Finite Window 46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 47 * 47 * 48 */ 48 */ 49 49 50 /* @(#) $Id$ */ << 51 50 52 #include "deflate.h" 51 #include "deflate.h" 53 52 54 const char deflate_copyright[] = 53 const char deflate_copyright[] = 55 " deflate 1.2.13 Copyright 1995-2022 Jean-l << 54 " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler "; 56 /* 55 /* 57 If you use the zlib library in a product, an 56 If you use the zlib library in a product, an acknowledgment is welcome 58 in the documentation of your product. If for 57 in the documentation of your product. If for some reason you cannot 59 include such an acknowledgment, I would appr 58 include such an acknowledgment, I would appreciate that you keep this 60 copyright string in the executable of your p 59 copyright string in the executable of your product. 61 */ 60 */ 62 61 63 /* =========================================== 62 /* =========================================================================== 64 * Function prototypes. 63 * Function prototypes. 65 */ 64 */ 66 typedef enum { 65 typedef enum { 67 need_more, /* block not completed, ne 66 need_more, /* block not completed, need more input or more output */ 68 block_done, /* block flush performed * 67 block_done, /* block flush performed */ 69 finish_started, /* finish started, need on 68 finish_started, /* finish started, need only more output at next deflate */ 70 finish_done /* finish done, accept no 69 finish_done /* finish done, accept no more input or output */ 71 } block_state; 70 } block_state; 72 71 73 typedef block_state (*compress_func) OF((defla 72 typedef block_state (*compress_func) OF((deflate_state *s, int flush)); 74 /* Compression function. Returns the block sta 73 /* Compression function. Returns the block state after the call. */ 75 74 76 local int deflateStateCheck OF((z_streamp 75 local int deflateStateCheck OF((z_streamp strm)); 77 local void slide_hash OF((deflate_state *s 76 local void slide_hash OF((deflate_state *s)); 78 local void fill_window OF((deflate_state *s 77 local void fill_window OF((deflate_state *s)); 79 local block_state deflate_stored OF((deflate_s 78 local block_state deflate_stored OF((deflate_state *s, int flush)); 80 local block_state deflate_fast OF((deflate_s 79 local block_state deflate_fast OF((deflate_state *s, int flush)); 81 #ifndef FASTEST 80 #ifndef FASTEST 82 local block_state deflate_slow OF((deflate_s 81 local block_state deflate_slow OF((deflate_state *s, int flush)); 83 #endif 82 #endif 84 local block_state deflate_rle OF((deflate_s 83 local block_state deflate_rle OF((deflate_state *s, int flush)); 85 local block_state deflate_huff OF((deflate_s 84 local block_state deflate_huff OF((deflate_state *s, int flush)); 86 local void lm_init OF((deflate_state *s 85 local void lm_init OF((deflate_state *s)); 87 local void putShortMSB OF((deflate_state *s 86 local void putShortMSB OF((deflate_state *s, uInt b)); 88 local void flush_pending OF((z_streamp strm)) 87 local void flush_pending OF((z_streamp strm)); 89 local unsigned read_buf OF((z_streamp strm, 88 local unsigned read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); >> 89 #ifdef ASMV >> 90 # pragma message("Assembler code may have bugs -- use at your own risk") >> 91 void match_init OF((void)); /* asm code initialization */ >> 92 uInt longest_match OF((deflate_state *s, IPos cur_match)); >> 93 #else 90 local uInt longest_match OF((deflate_state *s 94 local uInt longest_match OF((deflate_state *s, IPos cur_match)); >> 95 #endif 91 96 92 #ifdef ZLIB_DEBUG 97 #ifdef ZLIB_DEBUG 93 local void check_match OF((deflate_state *s, 98 local void check_match OF((deflate_state *s, IPos start, IPos match, 94 int length)); 99 int length)); 95 #endif 100 #endif 96 101 97 /* =========================================== 102 /* =========================================================================== 98 * Local data 103 * Local data 99 */ 104 */ 100 105 101 #define NIL 0 106 #define NIL 0 102 /* Tail of hash chains */ 107 /* Tail of hash chains */ 103 108 104 #ifndef TOO_FAR 109 #ifndef TOO_FAR 105 # define TOO_FAR 4096 110 # define TOO_FAR 4096 106 #endif 111 #endif 107 /* Matches of length 3 are discarded if their 112 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ 108 113 109 /* Values for max_lazy_match, good_match and m 114 /* Values for max_lazy_match, good_match and max_chain_length, depending on 110 * the desired pack level (0..9). The values g 115 * the desired pack level (0..9). The values given below have been tuned to 111 * exclude worst case performance for patholog 116 * exclude worst case performance for pathological files. Better values may be 112 * found for specific files. 117 * found for specific files. 113 */ 118 */ 114 typedef struct config_s { 119 typedef struct config_s { 115 ush good_length; /* reduce lazy search abov 120 ush good_length; /* reduce lazy search above this match length */ 116 ush max_lazy; /* do not perform lazy sea 121 ush max_lazy; /* do not perform lazy search above this match length */ 117 ush nice_length; /* quit search above this 122 ush nice_length; /* quit search above this match length */ 118 ush max_chain; 123 ush max_chain; 119 compress_func func; 124 compress_func func; 120 } config; 125 } config; 121 126 122 #ifdef FASTEST 127 #ifdef FASTEST 123 local const config configuration_table[2] = { 128 local const config configuration_table[2] = { 124 /* good lazy nice chain */ 129 /* good lazy nice chain */ 125 /* 0 */ {0, 0, 0, 0, deflate_stored}, 130 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 126 /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* 131 /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ 127 #else 132 #else 128 local const config configuration_table[10] = { 133 local const config configuration_table[10] = { 129 /* good lazy nice chain */ 134 /* good lazy nice chain */ 130 /* 0 */ {0, 0, 0, 0, deflate_stored}, 135 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 131 /* 1 */ {4, 4, 8, 4, deflate_fast}, /* 136 /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ 132 /* 2 */ {4, 5, 16, 8, deflate_fast}, 137 /* 2 */ {4, 5, 16, 8, deflate_fast}, 133 /* 3 */ {4, 6, 32, 32, deflate_fast}, 138 /* 3 */ {4, 6, 32, 32, deflate_fast}, 134 139 135 /* 4 */ {4, 4, 16, 16, deflate_slow}, /* 140 /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ 136 /* 5 */ {8, 16, 32, 32, deflate_slow}, 141 /* 5 */ {8, 16, 32, 32, deflate_slow}, 137 /* 6 */ {8, 16, 128, 128, deflate_slow}, 142 /* 6 */ {8, 16, 128, 128, deflate_slow}, 138 /* 7 */ {8, 32, 128, 256, deflate_slow}, 143 /* 7 */ {8, 32, 128, 256, deflate_slow}, 139 /* 8 */ {32, 128, 258, 1024, deflate_slow}, 144 /* 8 */ {32, 128, 258, 1024, deflate_slow}, 140 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; / 145 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ 141 #endif 146 #endif 142 147 143 /* Note: the deflate() code requires max_lazy 148 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 144 * For deflate_fast() (levels <= 3) good is ig 149 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 145 * meaning. 150 * meaning. 146 */ 151 */ 147 152 148 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTI 153 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ 149 #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0) 154 #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0)) 150 155 151 /* =========================================== 156 /* =========================================================================== 152 * Update a hash value with the given input by 157 * Update a hash value with the given input byte 153 * IN assertion: all calls to UPDATE_HASH are 158 * IN assertion: all calls to UPDATE_HASH are made with consecutive input 154 * characters, so that a running hash key c 159 * characters, so that a running hash key can be computed from the previous 155 * key instead of complete recalculation ea 160 * key instead of complete recalculation each time. 156 */ 161 */ 157 #define UPDATE_HASH(s,h,c) (h = (((h) << s->ha << 162 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) 158 163 159 164 160 /* =========================================== 165 /* =========================================================================== 161 * Insert string str in the dictionary and set 166 * Insert string str in the dictionary and set match_head to the previous head 162 * of the hash chain (the most recent string w 167 * of the hash chain (the most recent string with same hash key). Return 163 * the previous length of the hash chain. 168 * the previous length of the hash chain. 164 * If this file is compiled with -DFASTEST, th 169 * If this file is compiled with -DFASTEST, the compression level is forced 165 * to 1, and no hash chains are maintained. 170 * to 1, and no hash chains are maintained. 166 * IN assertion: all calls to INSERT_STRING a 171 * IN assertion: all calls to INSERT_STRING are made with consecutive input 167 * characters and the first MIN_MATCH bytes 172 * characters and the first MIN_MATCH bytes of str are valid (except for 168 * the last MIN_MATCH-1 bytes of the input 173 * the last MIN_MATCH-1 bytes of the input file). 169 */ 174 */ 170 #ifdef FASTEST 175 #ifdef FASTEST 171 #define INSERT_STRING(s, str, match_head) \ 176 #define INSERT_STRING(s, str, match_head) \ 172 (UPDATE_HASH(s, s->ins_h, s->window[(str) + 177 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 173 match_head = s->head[s->ins_h], \ 178 match_head = s->head[s->ins_h], \ 174 s->head[s->ins_h] = (Pos)(str)) 179 s->head[s->ins_h] = (Pos)(str)) 175 #else 180 #else 176 #define INSERT_STRING(s, str, match_head) \ 181 #define INSERT_STRING(s, str, match_head) \ 177 (UPDATE_HASH(s, s->ins_h, s->window[(str) + 182 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 178 match_head = s->prev[(str) & s->w_mask] = 183 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ 179 s->head[s->ins_h] = (Pos)(str)) 184 s->head[s->ins_h] = (Pos)(str)) 180 #endif 185 #endif 181 186 182 /* =========================================== 187 /* =========================================================================== 183 * Initialize the hash table (avoiding 64K ove 188 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 184 * prev[] will be initialized on the fly. 189 * prev[] will be initialized on the fly. 185 */ 190 */ 186 #define CLEAR_HASH(s) \ 191 #define CLEAR_HASH(s) \ 187 do { \ << 192 s->head[s->hash_size-1] = NIL; \ 188 s->head[s->hash_size - 1] = NIL; \ << 193 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 189 zmemzero((Bytef *)s->head, \ << 190 (unsigned)(s->hash_size - 1)* << 191 } while (0) << 192 194 193 /* =========================================== 195 /* =========================================================================== 194 * Slide the hash table when sliding the windo 196 * Slide the hash table when sliding the window down (could be avoided with 32 195 * bit values at the expense of memory usage). 197 * bit values at the expense of memory usage). We slide even when level == 0 to 196 * keep the hash table consistent if we switch 198 * keep the hash table consistent if we switch back to level > 0 later. 197 */ 199 */ 198 local void slide_hash(s) 200 local void slide_hash(s) 199 deflate_state *s; 201 deflate_state *s; 200 { 202 { 201 unsigned n, m; 203 unsigned n, m; 202 Posf *p; 204 Posf *p; 203 uInt wsize = s->w_size; 205 uInt wsize = s->w_size; 204 206 205 n = s->hash_size; 207 n = s->hash_size; 206 p = &s->head[n]; 208 p = &s->head[n]; 207 do { 209 do { 208 m = *--p; 210 m = *--p; 209 *p = (Pos)(m >= wsize ? m - wsize : NI 211 *p = (Pos)(m >= wsize ? m - wsize : NIL); 210 } while (--n); 212 } while (--n); 211 n = wsize; 213 n = wsize; 212 #ifndef FASTEST 214 #ifndef FASTEST 213 p = &s->prev[n]; 215 p = &s->prev[n]; 214 do { 216 do { 215 m = *--p; 217 m = *--p; 216 *p = (Pos)(m >= wsize ? m - wsize : NI 218 *p = (Pos)(m >= wsize ? m - wsize : NIL); 217 /* If n is not on any hash chain, prev 219 /* If n is not on any hash chain, prev[n] is garbage but 218 * its value will never be used. 220 * its value will never be used. 219 */ 221 */ 220 } while (--n); 222 } while (--n); 221 #endif 223 #endif 222 } 224 } 223 225 224 /* =========================================== 226 /* ========================================================================= */ 225 int ZEXPORT deflateInit_(strm, level, version, 227 int ZEXPORT deflateInit_(strm, level, version, stream_size) 226 z_streamp strm; 228 z_streamp strm; 227 int level; 229 int level; 228 const char *version; 230 const char *version; 229 int stream_size; 231 int stream_size; 230 { 232 { 231 return deflateInit2_(strm, level, Z_DEFLAT 233 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 232 Z_DEFAULT_STRATEGY, v 234 Z_DEFAULT_STRATEGY, version, stream_size); 233 /* To do: ignore strm->next_in if we use i 235 /* To do: ignore strm->next_in if we use it as window */ 234 } 236 } 235 237 236 /* =========================================== 238 /* ========================================================================= */ 237 int ZEXPORT deflateInit2_(strm, level, method, 239 int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, 238 version, stream_size) 240 version, stream_size) 239 z_streamp strm; 241 z_streamp strm; 240 int level; 242 int level; 241 int method; 243 int method; 242 int windowBits; 244 int windowBits; 243 int memLevel; 245 int memLevel; 244 int strategy; 246 int strategy; 245 const char *version; 247 const char *version; 246 int stream_size; 248 int stream_size; 247 { 249 { 248 deflate_state *s; 250 deflate_state *s; 249 int wrap = 1; 251 int wrap = 1; 250 static const char my_version[] = ZLIB_VERS 252 static const char my_version[] = ZLIB_VERSION; 251 253 >> 254 ushf *overlay; >> 255 /* We overlay pending_buf and d_buf+l_buf. This works since the average >> 256 * output size for (length,distance) codes is <= 24 bits. >> 257 */ >> 258 252 if (version == Z_NULL || version[0] != my_ 259 if (version == Z_NULL || version[0] != my_version[0] || 253 stream_size != sizeof(z_stream)) { 260 stream_size != sizeof(z_stream)) { 254 return Z_VERSION_ERROR; 261 return Z_VERSION_ERROR; 255 } 262 } 256 if (strm == Z_NULL) return Z_STREAM_ERROR; 263 if (strm == Z_NULL) return Z_STREAM_ERROR; 257 264 258 strm->msg = Z_NULL; 265 strm->msg = Z_NULL; 259 if (strm->zalloc == (alloc_func)0) { 266 if (strm->zalloc == (alloc_func)0) { 260 #ifdef Z_SOLO 267 #ifdef Z_SOLO 261 return Z_STREAM_ERROR; 268 return Z_STREAM_ERROR; 262 #else 269 #else 263 strm->zalloc = zcalloc; 270 strm->zalloc = zcalloc; 264 strm->opaque = (voidpf)0; 271 strm->opaque = (voidpf)0; 265 #endif 272 #endif 266 } 273 } 267 if (strm->zfree == (free_func)0) 274 if (strm->zfree == (free_func)0) 268 #ifdef Z_SOLO 275 #ifdef Z_SOLO 269 return Z_STREAM_ERROR; 276 return Z_STREAM_ERROR; 270 #else 277 #else 271 strm->zfree = zcfree; 278 strm->zfree = zcfree; 272 #endif 279 #endif 273 280 274 #ifdef FASTEST 281 #ifdef FASTEST 275 if (level != 0) level = 1; 282 if (level != 0) level = 1; 276 #else 283 #else 277 if (level == Z_DEFAULT_COMPRESSION) level 284 if (level == Z_DEFAULT_COMPRESSION) level = 6; 278 #endif 285 #endif 279 286 280 if (windowBits < 0) { /* suppress zlib wra 287 if (windowBits < 0) { /* suppress zlib wrapper */ 281 wrap = 0; 288 wrap = 0; 282 if (windowBits < -15) << 283 return Z_STREAM_ERROR; << 284 windowBits = -windowBits; 289 windowBits = -windowBits; 285 } 290 } 286 #ifdef GZIP 291 #ifdef GZIP 287 else if (windowBits > 15) { 292 else if (windowBits > 15) { 288 wrap = 2; /* write gzip wrapper 293 wrap = 2; /* write gzip wrapper instead */ 289 windowBits -= 16; 294 windowBits -= 16; 290 } 295 } 291 #endif 296 #endif 292 if (memLevel < 1 || memLevel > MAX_MEM_LEV 297 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || 293 windowBits < 8 || windowBits > 15 || l 298 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || 294 strategy < 0 || strategy > Z_FIXED || 299 strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) { 295 return Z_STREAM_ERROR; 300 return Z_STREAM_ERROR; 296 } 301 } 297 if (windowBits == 8) windowBits = 9; /* u 302 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ 298 s = (deflate_state *) ZALLOC(strm, 1, size 303 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); 299 if (s == Z_NULL) return Z_MEM_ERROR; 304 if (s == Z_NULL) return Z_MEM_ERROR; 300 strm->state = (struct internal_state FAR * 305 strm->state = (struct internal_state FAR *)s; 301 s->strm = strm; 306 s->strm = strm; 302 s->status = INIT_STATE; /* to pass sta 307 s->status = INIT_STATE; /* to pass state test in deflateReset() */ 303 308 304 s->wrap = wrap; 309 s->wrap = wrap; 305 s->gzhead = Z_NULL; 310 s->gzhead = Z_NULL; 306 s->w_bits = (uInt)windowBits; 311 s->w_bits = (uInt)windowBits; 307 s->w_size = 1 << s->w_bits; 312 s->w_size = 1 << s->w_bits; 308 s->w_mask = s->w_size - 1; 313 s->w_mask = s->w_size - 1; 309 314 310 s->hash_bits = (uInt)memLevel + 7; 315 s->hash_bits = (uInt)memLevel + 7; 311 s->hash_size = 1 << s->hash_bits; 316 s->hash_size = 1 << s->hash_bits; 312 s->hash_mask = s->hash_size - 1; 317 s->hash_mask = s->hash_size - 1; 313 s->hash_shift = ((s->hash_bits + MIN_MATC << 318 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); 314 319 315 s->window = (Bytef *) ZALLOC(strm, s->w_si 320 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); 316 s->prev = (Posf *) ZALLOC(strm, s->w_si 321 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 317 /* Avoid use of uninitialized value, see: << 318 * https://bugs.chromium.org/p/oss-fuzz/is << 319 */ << 320 zmemzero(s->prev, s->w_size * sizeof(Pos)) << 321 s->head = (Posf *) ZALLOC(strm, s->hash 322 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 322 323 323 s->high_water = 0; /* nothing written 324 s->high_water = 0; /* nothing written to s->window yet */ 324 325 325 s->lit_bufsize = 1 << (memLevel + 6); /* 1 326 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 326 327 327 /* We overlay pending_buf and sym_buf. Thi << 328 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); 328 * for length/distance pairs over any comp << 329 s->pending_buf = (uchf *) overlay; 329 * bits or less. << 330 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); 330 * << 331 * Analysis: The longest fixed codes are a << 332 * extra bits, for lengths 131 to 257. The << 333 * 5 bits plus 13 extra bits, for distance << 334 * possible fixed-codes length/distance pa << 335 * << 336 * sym_buf starts one-fourth of the way in << 337 * three bytes in sym_buf for every four b << 338 * in sym_buf is three bytes -- two for th << 339 * literal/length. As each symbol is consu << 340 * sym_buf value to read moves forward thr << 341 * 31 bits are written to pending_buf. The << 342 * bits gets to the next sym_buf symbol to << 343 * code is written. At that time, 31*(n - << 344 * after 24*(n - 2) bits have been consume << 345 * 8*n bits into pending_buf. (Note that t << 346 * symbols are written.) The closest the w << 347 * then n + 14 bits. Here n is lit_bufsize << 348 * can range from 128 to 32768. << 349 * << 350 * Therefore, at a minimum, there are 142 << 351 * written and what is read in the overlai << 352 * be overwritten by the compressed data. << 353 * due to the three-bit fixed-code block h << 354 * << 355 * That covers the case where either Z_FIX << 356 * codes, or when the use of fixed codes i << 357 * results in a smaller compressed block t << 358 * condition then assures that the above a << 359 * blocks. A dynamic-code block will only << 360 * fewer bits than a fixed-code block woul << 361 * Therefore its average symbol length is << 362 * the compressed data for a dynamic block << 363 * symbols from which it is being construc << 364 */ << 365 << 366 s->pending_buf = (uchf *) ZALLOC(strm, s-> << 367 s->pending_buf_size = (ulg)s->lit_bufsize << 368 331 369 if (s->window == Z_NULL || s->prev == Z_NU 332 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 370 s->pending_buf == Z_NULL) { 333 s->pending_buf == Z_NULL) { 371 s->status = FINISH_STATE; 334 s->status = FINISH_STATE; 372 strm->msg = ERR_MSG(Z_MEM_ERROR); 335 strm->msg = ERR_MSG(Z_MEM_ERROR); 373 deflateEnd (strm); 336 deflateEnd (strm); 374 return Z_MEM_ERROR; 337 return Z_MEM_ERROR; 375 } 338 } 376 s->sym_buf = s->pending_buf + s->lit_bufsi << 339 s->d_buf = overlay + s->lit_bufsize/sizeof(ush); 377 s->sym_end = (s->lit_bufsize - 1) * 3; << 340 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; 378 /* We avoid equality with lit_bufsize*3 be << 379 * on 16 bit machines and because stored b << 380 * 64K-1 bytes. << 381 */ << 382 341 383 s->level = level; 342 s->level = level; 384 s->strategy = strategy; 343 s->strategy = strategy; 385 s->method = (Byte)method; 344 s->method = (Byte)method; 386 345 387 return deflateReset(strm); 346 return deflateReset(strm); 388 } 347 } 389 348 390 /* =========================================== 349 /* ========================================================================= 391 * Check for a valid deflate stream state. Ret 350 * Check for a valid deflate stream state. Return 0 if ok, 1 if not. 392 */ 351 */ 393 local int deflateStateCheck(strm) << 352 local int deflateStateCheck (strm) 394 z_streamp strm; 353 z_streamp strm; 395 { 354 { 396 deflate_state *s; 355 deflate_state *s; 397 if (strm == Z_NULL || 356 if (strm == Z_NULL || 398 strm->zalloc == (alloc_func)0 || strm- 357 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) 399 return 1; 358 return 1; 400 s = strm->state; 359 s = strm->state; 401 if (s == Z_NULL || s->strm != strm || (s-> 360 if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE && 402 #ifdef GZIP 361 #ifdef GZIP 403 s-> 362 s->status != GZIP_STATE && 404 #endif 363 #endif 405 s-> 364 s->status != EXTRA_STATE && 406 s-> 365 s->status != NAME_STATE && 407 s-> 366 s->status != COMMENT_STATE && 408 s-> 367 s->status != HCRC_STATE && 409 s-> 368 s->status != BUSY_STATE && 410 s-> 369 s->status != FINISH_STATE)) 411 return 1; 370 return 1; 412 return 0; 371 return 0; 413 } 372 } 414 373 415 /* =========================================== 374 /* ========================================================================= */ 416 int ZEXPORT deflateSetDictionary(strm, diction << 375 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) 417 z_streamp strm; 376 z_streamp strm; 418 const Bytef *dictionary; 377 const Bytef *dictionary; 419 uInt dictLength; 378 uInt dictLength; 420 { 379 { 421 deflate_state *s; 380 deflate_state *s; 422 uInt str, n; 381 uInt str, n; 423 int wrap; 382 int wrap; 424 unsigned avail; 383 unsigned avail; 425 z_const unsigned char *next; 384 z_const unsigned char *next; 426 385 427 if (deflateStateCheck(strm) || dictionary 386 if (deflateStateCheck(strm) || dictionary == Z_NULL) 428 return Z_STREAM_ERROR; 387 return Z_STREAM_ERROR; 429 s = strm->state; 388 s = strm->state; 430 wrap = s->wrap; 389 wrap = s->wrap; 431 if (wrap == 2 || (wrap == 1 && s->status ! 390 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) 432 return Z_STREAM_ERROR; 391 return Z_STREAM_ERROR; 433 392 434 /* when using zlib wrappers, compute Adler 393 /* when using zlib wrappers, compute Adler-32 for provided dictionary */ 435 if (wrap == 1) 394 if (wrap == 1) 436 strm->adler = adler32(strm->adler, dic 395 strm->adler = adler32(strm->adler, dictionary, dictLength); 437 s->wrap = 0; /* avoid c 396 s->wrap = 0; /* avoid computing Adler-32 in read_buf */ 438 397 439 /* if dictionary would fill window, just r 398 /* if dictionary would fill window, just replace the history */ 440 if (dictLength >= s->w_size) { 399 if (dictLength >= s->w_size) { 441 if (wrap == 0) { /* already 400 if (wrap == 0) { /* already empty otherwise */ 442 CLEAR_HASH(s); 401 CLEAR_HASH(s); 443 s->strstart = 0; 402 s->strstart = 0; 444 s->block_start = 0L; 403 s->block_start = 0L; 445 s->insert = 0; 404 s->insert = 0; 446 } 405 } 447 dictionary += dictLength - s->w_size; 406 dictionary += dictLength - s->w_size; /* use the tail */ 448 dictLength = s->w_size; 407 dictLength = s->w_size; 449 } 408 } 450 409 451 /* insert dictionary into window and hash 410 /* insert dictionary into window and hash */ 452 avail = strm->avail_in; 411 avail = strm->avail_in; 453 next = strm->next_in; 412 next = strm->next_in; 454 strm->avail_in = dictLength; 413 strm->avail_in = dictLength; 455 strm->next_in = (z_const Bytef *)dictionar 414 strm->next_in = (z_const Bytef *)dictionary; 456 fill_window(s); 415 fill_window(s); 457 while (s->lookahead >= MIN_MATCH) { 416 while (s->lookahead >= MIN_MATCH) { 458 str = s->strstart; 417 str = s->strstart; 459 n = s->lookahead - (MIN_MATCH-1); 418 n = s->lookahead - (MIN_MATCH-1); 460 do { 419 do { 461 UPDATE_HASH(s, s->ins_h, s->window 420 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); 462 #ifndef FASTEST 421 #ifndef FASTEST 463 s->prev[str & s->w_mask] = s->head 422 s->prev[str & s->w_mask] = s->head[s->ins_h]; 464 #endif 423 #endif 465 s->head[s->ins_h] = (Pos)str; 424 s->head[s->ins_h] = (Pos)str; 466 str++; 425 str++; 467 } while (--n); 426 } while (--n); 468 s->strstart = str; 427 s->strstart = str; 469 s->lookahead = MIN_MATCH-1; 428 s->lookahead = MIN_MATCH-1; 470 fill_window(s); 429 fill_window(s); 471 } 430 } 472 s->strstart += s->lookahead; 431 s->strstart += s->lookahead; 473 s->block_start = (long)s->strstart; 432 s->block_start = (long)s->strstart; 474 s->insert = s->lookahead; 433 s->insert = s->lookahead; 475 s->lookahead = 0; 434 s->lookahead = 0; 476 s->match_length = s->prev_length = MIN_MAT 435 s->match_length = s->prev_length = MIN_MATCH-1; 477 s->match_available = 0; 436 s->match_available = 0; 478 strm->next_in = next; 437 strm->next_in = next; 479 strm->avail_in = avail; 438 strm->avail_in = avail; 480 s->wrap = wrap; 439 s->wrap = wrap; 481 return Z_OK; 440 return Z_OK; 482 } 441 } 483 442 484 /* =========================================== 443 /* ========================================================================= */ 485 int ZEXPORT deflateGetDictionary(strm, diction << 444 int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength) 486 z_streamp strm; 445 z_streamp strm; 487 Bytef *dictionary; 446 Bytef *dictionary; 488 uInt *dictLength; 447 uInt *dictLength; 489 { 448 { 490 deflate_state *s; 449 deflate_state *s; 491 uInt len; 450 uInt len; 492 451 493 if (deflateStateCheck(strm)) 452 if (deflateStateCheck(strm)) 494 return Z_STREAM_ERROR; 453 return Z_STREAM_ERROR; 495 s = strm->state; 454 s = strm->state; 496 len = s->strstart + s->lookahead; 455 len = s->strstart + s->lookahead; 497 if (len > s->w_size) 456 if (len > s->w_size) 498 len = s->w_size; 457 len = s->w_size; 499 if (dictionary != Z_NULL && len) 458 if (dictionary != Z_NULL && len) 500 zmemcpy(dictionary, s->window + s->str 459 zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len); 501 if (dictLength != Z_NULL) 460 if (dictLength != Z_NULL) 502 *dictLength = len; 461 *dictLength = len; 503 return Z_OK; 462 return Z_OK; 504 } 463 } 505 464 506 /* =========================================== 465 /* ========================================================================= */ 507 int ZEXPORT deflateResetKeep(strm) << 466 int ZEXPORT deflateResetKeep (strm) 508 z_streamp strm; 467 z_streamp strm; 509 { 468 { 510 deflate_state *s; 469 deflate_state *s; 511 470 512 if (deflateStateCheck(strm)) { 471 if (deflateStateCheck(strm)) { 513 return Z_STREAM_ERROR; 472 return Z_STREAM_ERROR; 514 } 473 } 515 474 516 strm->total_in = strm->total_out = 0; 475 strm->total_in = strm->total_out = 0; 517 strm->msg = Z_NULL; /* use zfree if we eve 476 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ 518 strm->data_type = Z_UNKNOWN; 477 strm->data_type = Z_UNKNOWN; 519 478 520 s = (deflate_state *)strm->state; 479 s = (deflate_state *)strm->state; 521 s->pending = 0; 480 s->pending = 0; 522 s->pending_out = s->pending_buf; 481 s->pending_out = s->pending_buf; 523 482 524 if (s->wrap < 0) { 483 if (s->wrap < 0) { 525 s->wrap = -s->wrap; /* was made negati 484 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ 526 } 485 } 527 s->status = 486 s->status = 528 #ifdef GZIP 487 #ifdef GZIP 529 s->wrap == 2 ? GZIP_STATE : 488 s->wrap == 2 ? GZIP_STATE : 530 #endif 489 #endif 531 INIT_STATE; << 490 s->wrap ? INIT_STATE : BUSY_STATE; 532 strm->adler = 491 strm->adler = 533 #ifdef GZIP 492 #ifdef GZIP 534 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : 493 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : 535 #endif 494 #endif 536 adler32(0L, Z_NULL, 0); 495 adler32(0L, Z_NULL, 0); 537 s->last_flush = -2; << 496 s->last_flush = Z_NO_FLUSH; 538 497 539 _tr_init(s); 498 _tr_init(s); 540 499 541 return Z_OK; 500 return Z_OK; 542 } 501 } 543 502 544 /* =========================================== 503 /* ========================================================================= */ 545 int ZEXPORT deflateReset(strm) << 504 int ZEXPORT deflateReset (strm) 546 z_streamp strm; 505 z_streamp strm; 547 { 506 { 548 int ret; 507 int ret; 549 508 550 ret = deflateResetKeep(strm); 509 ret = deflateResetKeep(strm); 551 if (ret == Z_OK) 510 if (ret == Z_OK) 552 lm_init(strm->state); 511 lm_init(strm->state); 553 return ret; 512 return ret; 554 } 513 } 555 514 556 /* =========================================== 515 /* ========================================================================= */ 557 int ZEXPORT deflateSetHeader(strm, head) << 516 int ZEXPORT deflateSetHeader (strm, head) 558 z_streamp strm; 517 z_streamp strm; 559 gz_headerp head; 518 gz_headerp head; 560 { 519 { 561 if (deflateStateCheck(strm) || strm->state 520 if (deflateStateCheck(strm) || strm->state->wrap != 2) 562 return Z_STREAM_ERROR; 521 return Z_STREAM_ERROR; 563 strm->state->gzhead = head; 522 strm->state->gzhead = head; 564 return Z_OK; 523 return Z_OK; 565 } 524 } 566 525 567 /* =========================================== 526 /* ========================================================================= */ 568 int ZEXPORT deflatePending(strm, pending, bits << 527 int ZEXPORT deflatePending (strm, pending, bits) 569 unsigned *pending; 528 unsigned *pending; 570 int *bits; 529 int *bits; 571 z_streamp strm; 530 z_streamp strm; 572 { 531 { 573 if (deflateStateCheck(strm)) return Z_STRE 532 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 574 if (pending != Z_NULL) 533 if (pending != Z_NULL) 575 *pending = strm->state->pending; 534 *pending = strm->state->pending; 576 if (bits != Z_NULL) 535 if (bits != Z_NULL) 577 *bits = strm->state->bi_valid; 536 *bits = strm->state->bi_valid; 578 return Z_OK; 537 return Z_OK; 579 } 538 } 580 539 581 /* =========================================== 540 /* ========================================================================= */ 582 int ZEXPORT deflatePrime(strm, bits, value) << 541 int ZEXPORT deflatePrime (strm, bits, value) 583 z_streamp strm; 542 z_streamp strm; 584 int bits; 543 int bits; 585 int value; 544 int value; 586 { 545 { 587 deflate_state *s; 546 deflate_state *s; 588 int put; 547 int put; 589 548 590 if (deflateStateCheck(strm)) return Z_STRE 549 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 591 s = strm->state; 550 s = strm->state; 592 if (bits < 0 || bits > 16 || << 551 if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) 593 s->sym_buf < s->pending_out + ((Buf_si << 594 return Z_BUF_ERROR; 552 return Z_BUF_ERROR; 595 do { 553 do { 596 put = Buf_size - s->bi_valid; 554 put = Buf_size - s->bi_valid; 597 if (put > bits) 555 if (put > bits) 598 put = bits; 556 put = bits; 599 s->bi_buf |= (ush)((value & ((1 << put 557 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); 600 s->bi_valid += put; 558 s->bi_valid += put; 601 _tr_flush_bits(s); 559 _tr_flush_bits(s); 602 value >>= put; 560 value >>= put; 603 bits -= put; 561 bits -= put; 604 } while (bits); 562 } while (bits); 605 return Z_OK; 563 return Z_OK; 606 } 564 } 607 565 608 /* =========================================== 566 /* ========================================================================= */ 609 int ZEXPORT deflateParams(strm, level, strateg 567 int ZEXPORT deflateParams(strm, level, strategy) 610 z_streamp strm; 568 z_streamp strm; 611 int level; 569 int level; 612 int strategy; 570 int strategy; 613 { 571 { 614 deflate_state *s; 572 deflate_state *s; 615 compress_func func; 573 compress_func func; 616 574 617 if (deflateStateCheck(strm)) return Z_STRE 575 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 618 s = strm->state; 576 s = strm->state; 619 577 620 #ifdef FASTEST 578 #ifdef FASTEST 621 if (level != 0) level = 1; 579 if (level != 0) level = 1; 622 #else 580 #else 623 if (level == Z_DEFAULT_COMPRESSION) level 581 if (level == Z_DEFAULT_COMPRESSION) level = 6; 624 #endif 582 #endif 625 if (level < 0 || level > 9 || strategy < 0 583 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { 626 return Z_STREAM_ERROR; 584 return Z_STREAM_ERROR; 627 } 585 } 628 func = configuration_table[s->level].func; 586 func = configuration_table[s->level].func; 629 587 630 if ((strategy != s->strategy || func != co 588 if ((strategy != s->strategy || func != configuration_table[level].func) && 631 s->last_flush != -2) { << 589 s->high_water) { 632 /* Flush the last buffer: */ 590 /* Flush the last buffer: */ 633 int err = deflate(strm, Z_BLOCK); 591 int err = deflate(strm, Z_BLOCK); 634 if (err == Z_STREAM_ERROR) 592 if (err == Z_STREAM_ERROR) 635 return err; 593 return err; 636 if (strm->avail_in || (s->strstart - s << 594 if (strm->avail_out == 0) 637 return Z_BUF_ERROR; 595 return Z_BUF_ERROR; 638 } 596 } 639 if (s->level != level) { 597 if (s->level != level) { 640 if (s->level == 0 && s->matches != 0) 598 if (s->level == 0 && s->matches != 0) { 641 if (s->matches == 1) 599 if (s->matches == 1) 642 slide_hash(s); 600 slide_hash(s); 643 else 601 else 644 CLEAR_HASH(s); 602 CLEAR_HASH(s); 645 s->matches = 0; 603 s->matches = 0; 646 } 604 } 647 s->level = level; 605 s->level = level; 648 s->max_lazy_match = configuration_ta 606 s->max_lazy_match = configuration_table[level].max_lazy; 649 s->good_match = configuration_ta 607 s->good_match = configuration_table[level].good_length; 650 s->nice_match = configuration_ta 608 s->nice_match = configuration_table[level].nice_length; 651 s->max_chain_length = configuration_ta 609 s->max_chain_length = configuration_table[level].max_chain; 652 } 610 } 653 s->strategy = strategy; 611 s->strategy = strategy; 654 return Z_OK; 612 return Z_OK; 655 } 613 } 656 614 657 /* =========================================== 615 /* ========================================================================= */ 658 int ZEXPORT deflateTune(strm, good_length, max 616 int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) 659 z_streamp strm; 617 z_streamp strm; 660 int good_length; 618 int good_length; 661 int max_lazy; 619 int max_lazy; 662 int nice_length; 620 int nice_length; 663 int max_chain; 621 int max_chain; 664 { 622 { 665 deflate_state *s; 623 deflate_state *s; 666 624 667 if (deflateStateCheck(strm)) return Z_STRE 625 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 668 s = strm->state; 626 s = strm->state; 669 s->good_match = (uInt)good_length; 627 s->good_match = (uInt)good_length; 670 s->max_lazy_match = (uInt)max_lazy; 628 s->max_lazy_match = (uInt)max_lazy; 671 s->nice_match = nice_length; 629 s->nice_match = nice_length; 672 s->max_chain_length = (uInt)max_chain; 630 s->max_chain_length = (uInt)max_chain; 673 return Z_OK; 631 return Z_OK; 674 } 632 } 675 633 676 /* =========================================== 634 /* ========================================================================= 677 * For the default windowBits of 15 and memLev << 635 * For the default windowBits of 15 and memLevel of 8, this function returns 678 * close to exact, as well as small, upper bou << 636 * a close to exact, as well as small, upper bound on the compressed size. 679 * is an expansion of ~0.03%, plus a small con << 637 * They are coded as constants here for a reason--if the #define's are >> 638 * changed, then this function needs to be changed as well. The return >> 639 * value for 15 and 8 only works for those exact settings. 680 * 640 * 681 * For any setting other than those defaults f << 641 * For any setting other than those defaults for windowBits and memLevel, 682 * of two worst case bounds is returned. This << 642 * the value returned is a conservative worst case for the maximum expansion 683 * ~13%, plus a small constant. << 643 * resulting from using fixed blocks instead of stored blocks, which deflate >> 644 * can emit on compressed data for some combinations of the parameters. 684 * 645 * 685 * Both the 0.03% and 4% derive from the overh << 646 * This function could be more sophisticated to provide closer upper bounds for 686 * one is for stored blocks of 16383 bytes (me << 647 * every combination of windowBits and memLevel. But even the conservative 687 * is for stored blocks of 127 bytes (the wors << 648 * upper bound of about 14% expansion does not seem onerous for output buffer 688 * expansion results from five bytes of header << 649 * allocation. 689 * << 690 * The larger expansion of 13% results from a << 691 * the symbols buffer size (windowBits <= memL << 692 * the data being compressed may have slid out << 693 * a stored block from being emitted. Then the << 694 * dynamic block, where a fixed block limits t << 695 * per 8-bit byte, plus 10 bits for every bloc << 696 * which this can occur is 255 (memLevel == 2) << 697 * << 698 * Shifts are used to approximate divisions, f << 699 */ 650 */ 700 uLong ZEXPORT deflateBound(strm, sourceLen) 651 uLong ZEXPORT deflateBound(strm, sourceLen) 701 z_streamp strm; 652 z_streamp strm; 702 uLong sourceLen; 653 uLong sourceLen; 703 { 654 { 704 deflate_state *s; 655 deflate_state *s; 705 uLong fixedlen, storelen, wraplen; << 656 uLong complen, wraplen; 706 657 707 /* upper bound for fixed blocks with 9-bit << 658 /* conservative upper bound for compressed data */ 708 (memLevel == 2, which is the lowest tha << 659 complen = sourceLen + 709 ~13% overhead plus a small constant */ << 660 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; 710 fixedlen = sourceLen + (sourceLen >> 3) + << 711 (sourceLen >> 9) + 4; << 712 << 713 /* upper bound for stored blocks with leng << 714 ~4% overhead plus a small constant */ << 715 storelen = sourceLen + (sourceLen >> 5) + << 716 (sourceLen >> 11) + 7; << 717 661 718 /* if can't get parameters, return larger << 662 /* if can't get parameters, return conservative bound plus zlib wrapper */ 719 if (deflateStateCheck(strm)) 663 if (deflateStateCheck(strm)) 720 return (fixedlen > storelen ? fixedlen << 664 return complen + 6; 721 665 722 /* compute wrapper length */ 666 /* compute wrapper length */ 723 s = strm->state; 667 s = strm->state; 724 switch (s->wrap) { 668 switch (s->wrap) { 725 case 0: /* 669 case 0: /* raw deflate */ 726 wraplen = 0; 670 wraplen = 0; 727 break; 671 break; 728 case 1: /* 672 case 1: /* zlib wrapper */ 729 wraplen = 6 + (s->strstart ? 4 : 0); 673 wraplen = 6 + (s->strstart ? 4 : 0); 730 break; 674 break; 731 #ifdef GZIP 675 #ifdef GZIP 732 case 2: /* 676 case 2: /* gzip wrapper */ 733 wraplen = 18; 677 wraplen = 18; 734 if (s->gzhead != Z_NULL) { /* 678 if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ 735 Bytef *str; 679 Bytef *str; 736 if (s->gzhead->extra != Z_NULL) 680 if (s->gzhead->extra != Z_NULL) 737 wraplen += 2 + s->gzhead->extr 681 wraplen += 2 + s->gzhead->extra_len; 738 str = s->gzhead->name; 682 str = s->gzhead->name; 739 if (str != Z_NULL) 683 if (str != Z_NULL) 740 do { 684 do { 741 wraplen++; 685 wraplen++; 742 } while (*str++); 686 } while (*str++); 743 str = s->gzhead->comment; 687 str = s->gzhead->comment; 744 if (str != Z_NULL) 688 if (str != Z_NULL) 745 do { 689 do { 746 wraplen++; 690 wraplen++; 747 } while (*str++); 691 } while (*str++); 748 if (s->gzhead->hcrc) 692 if (s->gzhead->hcrc) 749 wraplen += 2; 693 wraplen += 2; 750 } 694 } 751 break; 695 break; 752 #endif 696 #endif 753 default: /* 697 default: /* for compiler happiness */ 754 wraplen = 6; 698 wraplen = 6; 755 } 699 } 756 700 757 /* if not default parameters, return one o << 701 /* if not default parameters, return conservative bound */ 758 if (s->w_bits != 15 || s->hash_bits != 8 + 702 if (s->w_bits != 15 || s->hash_bits != 8 + 7) 759 return (s->w_bits <= s->hash_bits ? fi << 703 return complen + wraplen; 760 704 761 /* default settings: return tight bound fo << 705 /* default settings: return tight bound for that case */ 762 plus a small constant */ << 763 return sourceLen + (sourceLen >> 12) + (so 706 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 764 (sourceLen >> 25) + 13 - 6 + wraple 707 (sourceLen >> 25) + 13 - 6 + wraplen; 765 } 708 } 766 709 767 /* =========================================== 710 /* ========================================================================= 768 * Put a short in the pending buffer. The 16-b 711 * Put a short in the pending buffer. The 16-bit value is put in MSB order. 769 * IN assertion: the stream state is correct a 712 * IN assertion: the stream state is correct and there is enough room in 770 * pending_buf. 713 * pending_buf. 771 */ 714 */ 772 local void putShortMSB(s, b) << 715 local void putShortMSB (s, b) 773 deflate_state *s; 716 deflate_state *s; 774 uInt b; 717 uInt b; 775 { 718 { 776 put_byte(s, (Byte)(b >> 8)); 719 put_byte(s, (Byte)(b >> 8)); 777 put_byte(s, (Byte)(b & 0xff)); 720 put_byte(s, (Byte)(b & 0xff)); 778 } 721 } 779 722 780 /* =========================================== 723 /* ========================================================================= 781 * Flush as much pending output as possible. A 724 * Flush as much pending output as possible. All deflate() output, except for 782 * some deflate_stored() output, goes through 725 * some deflate_stored() output, goes through this function so some 783 * applications may wish to modify it to avoid 726 * applications may wish to modify it to avoid allocating a large 784 * strm->next_out buffer and copying into it. 727 * strm->next_out buffer and copying into it. (See also read_buf()). 785 */ 728 */ 786 local void flush_pending(strm) 729 local void flush_pending(strm) 787 z_streamp strm; 730 z_streamp strm; 788 { 731 { 789 unsigned len; 732 unsigned len; 790 deflate_state *s = strm->state; 733 deflate_state *s = strm->state; 791 734 792 _tr_flush_bits(s); 735 _tr_flush_bits(s); 793 len = s->pending; 736 len = s->pending; 794 if (len > strm->avail_out) len = strm->ava 737 if (len > strm->avail_out) len = strm->avail_out; 795 if (len == 0) return; 738 if (len == 0) return; 796 739 797 zmemcpy(strm->next_out, s->pending_out, le 740 zmemcpy(strm->next_out, s->pending_out, len); 798 strm->next_out += len; 741 strm->next_out += len; 799 s->pending_out += len; 742 s->pending_out += len; 800 strm->total_out += len; 743 strm->total_out += len; 801 strm->avail_out -= len; 744 strm->avail_out -= len; 802 s->pending -= len; 745 s->pending -= len; 803 if (s->pending == 0) { 746 if (s->pending == 0) { 804 s->pending_out = s->pending_buf; 747 s->pending_out = s->pending_buf; 805 } 748 } 806 } 749 } 807 750 808 /* =========================================== 751 /* =========================================================================== 809 * Update the header CRC with the bytes s->pen 752 * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1]. 810 */ 753 */ 811 #define HCRC_UPDATE(beg) \ 754 #define HCRC_UPDATE(beg) \ 812 do { \ 755 do { \ 813 if (s->gzhead->hcrc && s->pending > (b 756 if (s->gzhead->hcrc && s->pending > (beg)) \ 814 strm->adler = crc32(strm->adler, s 757 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \ 815 s->pending - ( 758 s->pending - (beg)); \ 816 } while (0) 759 } while (0) 817 760 818 /* =========================================== 761 /* ========================================================================= */ 819 int ZEXPORT deflate(strm, flush) << 762 int ZEXPORT deflate (strm, flush) 820 z_streamp strm; 763 z_streamp strm; 821 int flush; 764 int flush; 822 { 765 { 823 int old_flush; /* value of flush param for 766 int old_flush; /* value of flush param for previous deflate call */ 824 deflate_state *s; 767 deflate_state *s; 825 768 826 if (deflateStateCheck(strm) || flush > Z_B 769 if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) { 827 return Z_STREAM_ERROR; 770 return Z_STREAM_ERROR; 828 } 771 } 829 s = strm->state; 772 s = strm->state; 830 773 831 if (strm->next_out == Z_NULL || 774 if (strm->next_out == Z_NULL || 832 (strm->avail_in != 0 && strm->next_in 775 (strm->avail_in != 0 && strm->next_in == Z_NULL) || 833 (s->status == FINISH_STATE && flush != 776 (s->status == FINISH_STATE && flush != Z_FINISH)) { 834 ERR_RETURN(strm, Z_STREAM_ERROR); 777 ERR_RETURN(strm, Z_STREAM_ERROR); 835 } 778 } 836 if (strm->avail_out == 0) ERR_RETURN(strm, 779 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 837 780 838 old_flush = s->last_flush; 781 old_flush = s->last_flush; 839 s->last_flush = flush; 782 s->last_flush = flush; 840 783 841 /* Flush as much pending output as possibl 784 /* Flush as much pending output as possible */ 842 if (s->pending != 0) { 785 if (s->pending != 0) { 843 flush_pending(strm); 786 flush_pending(strm); 844 if (strm->avail_out == 0) { 787 if (strm->avail_out == 0) { 845 /* Since avail_out is 0, deflate w 788 /* Since avail_out is 0, deflate will be called again with 846 * more output space, but possibly 789 * more output space, but possibly with both pending and 847 * avail_in equal to zero. There w 790 * avail_in equal to zero. There won't be anything to do, 848 * but this is not an error situat 791 * but this is not an error situation so make sure we 849 * return OK instead of BUF_ERROR 792 * return OK instead of BUF_ERROR at next call of deflate: 850 */ 793 */ 851 s->last_flush = -1; 794 s->last_flush = -1; 852 return Z_OK; 795 return Z_OK; 853 } 796 } 854 797 855 /* Make sure there is something to do and 798 /* Make sure there is something to do and avoid duplicate consecutive 856 * flushes. For repeated and useless calls 799 * flushes. For repeated and useless calls with Z_FINISH, we keep 857 * returning Z_STREAM_END instead of Z_BUF 800 * returning Z_STREAM_END instead of Z_BUF_ERROR. 858 */ 801 */ 859 } else if (strm->avail_in == 0 && RANK(flu 802 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && 860 flush != Z_FINISH) { 803 flush != Z_FINISH) { 861 ERR_RETURN(strm, Z_BUF_ERROR); 804 ERR_RETURN(strm, Z_BUF_ERROR); 862 } 805 } 863 806 864 /* User must not provide more input after 807 /* User must not provide more input after the first FINISH: */ 865 if (s->status == FINISH_STATE && strm->ava 808 if (s->status == FINISH_STATE && strm->avail_in != 0) { 866 ERR_RETURN(strm, Z_BUF_ERROR); 809 ERR_RETURN(strm, Z_BUF_ERROR); 867 } 810 } 868 811 869 /* Write the header */ 812 /* Write the header */ 870 if (s->status == INIT_STATE && s->wrap == << 871 s->status = BUSY_STATE; << 872 if (s->status == INIT_STATE) { 813 if (s->status == INIT_STATE) { 873 /* zlib header */ 814 /* zlib header */ 874 uInt header = (Z_DEFLATED + ((s->w_bit << 815 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; 875 uInt level_flags; 816 uInt level_flags; 876 817 877 if (s->strategy >= Z_HUFFMAN_ONLY || s 818 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) 878 level_flags = 0; 819 level_flags = 0; 879 else if (s->level < 6) 820 else if (s->level < 6) 880 level_flags = 1; 821 level_flags = 1; 881 else if (s->level == 6) 822 else if (s->level == 6) 882 level_flags = 2; 823 level_flags = 2; 883 else 824 else 884 level_flags = 3; 825 level_flags = 3; 885 header |= (level_flags << 6); 826 header |= (level_flags << 6); 886 if (s->strstart != 0) header |= PRESET 827 if (s->strstart != 0) header |= PRESET_DICT; 887 header += 31 - (header % 31); 828 header += 31 - (header % 31); 888 829 889 putShortMSB(s, header); 830 putShortMSB(s, header); 890 831 891 /* Save the adler32 of the preset dict 832 /* Save the adler32 of the preset dictionary: */ 892 if (s->strstart != 0) { 833 if (s->strstart != 0) { 893 putShortMSB(s, (uInt)(strm->adler 834 putShortMSB(s, (uInt)(strm->adler >> 16)); 894 putShortMSB(s, (uInt)(strm->adler 835 putShortMSB(s, (uInt)(strm->adler & 0xffff)); 895 } 836 } 896 strm->adler = adler32(0L, Z_NULL, 0); 837 strm->adler = adler32(0L, Z_NULL, 0); 897 s->status = BUSY_STATE; 838 s->status = BUSY_STATE; 898 839 899 /* Compression must start with an empt 840 /* Compression must start with an empty pending buffer */ 900 flush_pending(strm); 841 flush_pending(strm); 901 if (s->pending != 0) { 842 if (s->pending != 0) { 902 s->last_flush = -1; 843 s->last_flush = -1; 903 return Z_OK; 844 return Z_OK; 904 } 845 } 905 } 846 } 906 #ifdef GZIP 847 #ifdef GZIP 907 if (s->status == GZIP_STATE) { 848 if (s->status == GZIP_STATE) { 908 /* gzip header */ 849 /* gzip header */ 909 strm->adler = crc32(0L, Z_NULL, 0); 850 strm->adler = crc32(0L, Z_NULL, 0); 910 put_byte(s, 31); 851 put_byte(s, 31); 911 put_byte(s, 139); 852 put_byte(s, 139); 912 put_byte(s, 8); 853 put_byte(s, 8); 913 if (s->gzhead == Z_NULL) { 854 if (s->gzhead == Z_NULL) { 914 put_byte(s, 0); 855 put_byte(s, 0); 915 put_byte(s, 0); 856 put_byte(s, 0); 916 put_byte(s, 0); 857 put_byte(s, 0); 917 put_byte(s, 0); 858 put_byte(s, 0); 918 put_byte(s, 0); 859 put_byte(s, 0); 919 put_byte(s, s->level == 9 ? 2 : 860 put_byte(s, s->level == 9 ? 2 : 920 (s->strategy >= Z_HUFFMAN 861 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 921 4 : 0)); 862 4 : 0)); 922 put_byte(s, OS_CODE); 863 put_byte(s, OS_CODE); 923 s->status = BUSY_STATE; 864 s->status = BUSY_STATE; 924 865 925 /* Compression must start with an 866 /* Compression must start with an empty pending buffer */ 926 flush_pending(strm); 867 flush_pending(strm); 927 if (s->pending != 0) { 868 if (s->pending != 0) { 928 s->last_flush = -1; 869 s->last_flush = -1; 929 return Z_OK; 870 return Z_OK; 930 } 871 } 931 } 872 } 932 else { 873 else { 933 put_byte(s, (s->gzhead->text ? 1 : 874 put_byte(s, (s->gzhead->text ? 1 : 0) + 934 (s->gzhead->hcrc ? 2 : 0) 875 (s->gzhead->hcrc ? 2 : 0) + 935 (s->gzhead->extra == Z_NU 876 (s->gzhead->extra == Z_NULL ? 0 : 4) + 936 (s->gzhead->name == Z_NUL 877 (s->gzhead->name == Z_NULL ? 0 : 8) + 937 (s->gzhead->comment == Z_ 878 (s->gzhead->comment == Z_NULL ? 0 : 16) 938 ); 879 ); 939 put_byte(s, (Byte)(s->gzhead->time 880 put_byte(s, (Byte)(s->gzhead->time & 0xff)); 940 put_byte(s, (Byte)((s->gzhead->tim 881 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); 941 put_byte(s, (Byte)((s->gzhead->tim 882 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); 942 put_byte(s, (Byte)((s->gzhead->tim 883 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); 943 put_byte(s, s->level == 9 ? 2 : 884 put_byte(s, s->level == 9 ? 2 : 944 (s->strategy >= Z_HUFFMAN 885 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 945 4 : 0)); 886 4 : 0)); 946 put_byte(s, s->gzhead->os & 0xff); 887 put_byte(s, s->gzhead->os & 0xff); 947 if (s->gzhead->extra != Z_NULL) { 888 if (s->gzhead->extra != Z_NULL) { 948 put_byte(s, s->gzhead->extra_l 889 put_byte(s, s->gzhead->extra_len & 0xff); 949 put_byte(s, (s->gzhead->extra_ 890 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); 950 } 891 } 951 if (s->gzhead->hcrc) 892 if (s->gzhead->hcrc) 952 strm->adler = crc32(strm->adle 893 strm->adler = crc32(strm->adler, s->pending_buf, 953 s->pending 894 s->pending); 954 s->gzindex = 0; 895 s->gzindex = 0; 955 s->status = EXTRA_STATE; 896 s->status = EXTRA_STATE; 956 } 897 } 957 } 898 } 958 if (s->status == EXTRA_STATE) { 899 if (s->status == EXTRA_STATE) { 959 if (s->gzhead->extra != Z_NULL) { 900 if (s->gzhead->extra != Z_NULL) { 960 ulg beg = s->pending; /* start o 901 ulg beg = s->pending; /* start of bytes to update crc */ 961 uInt left = (s->gzhead->extra_len 902 uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex; 962 while (s->pending + left > s->pend 903 while (s->pending + left > s->pending_buf_size) { 963 uInt copy = s->pending_buf_siz 904 uInt copy = s->pending_buf_size - s->pending; 964 zmemcpy(s->pending_buf + s->pe 905 zmemcpy(s->pending_buf + s->pending, 965 s->gzhead->extra + s-> 906 s->gzhead->extra + s->gzindex, copy); 966 s->pending = s->pending_buf_si 907 s->pending = s->pending_buf_size; 967 HCRC_UPDATE(beg); 908 HCRC_UPDATE(beg); 968 s->gzindex += copy; 909 s->gzindex += copy; 969 flush_pending(strm); 910 flush_pending(strm); 970 if (s->pending != 0) { 911 if (s->pending != 0) { 971 s->last_flush = -1; 912 s->last_flush = -1; 972 return Z_OK; 913 return Z_OK; 973 } 914 } 974 beg = 0; 915 beg = 0; 975 left -= copy; 916 left -= copy; 976 } 917 } 977 zmemcpy(s->pending_buf + s->pendin 918 zmemcpy(s->pending_buf + s->pending, 978 s->gzhead->extra + s->gzin 919 s->gzhead->extra + s->gzindex, left); 979 s->pending += left; 920 s->pending += left; 980 HCRC_UPDATE(beg); 921 HCRC_UPDATE(beg); 981 s->gzindex = 0; 922 s->gzindex = 0; 982 } 923 } 983 s->status = NAME_STATE; 924 s->status = NAME_STATE; 984 } 925 } 985 if (s->status == NAME_STATE) { 926 if (s->status == NAME_STATE) { 986 if (s->gzhead->name != Z_NULL) { 927 if (s->gzhead->name != Z_NULL) { 987 ulg beg = s->pending; /* start o 928 ulg beg = s->pending; /* start of bytes to update crc */ 988 int val; 929 int val; 989 do { 930 do { 990 if (s->pending == s->pending_b 931 if (s->pending == s->pending_buf_size) { 991 HCRC_UPDATE(beg); 932 HCRC_UPDATE(beg); 992 flush_pending(strm); 933 flush_pending(strm); 993 if (s->pending != 0) { 934 if (s->pending != 0) { 994 s->last_flush = -1; 935 s->last_flush = -1; 995 return Z_OK; 936 return Z_OK; 996 } 937 } 997 beg = 0; 938 beg = 0; 998 } 939 } 999 val = s->gzhead->name[s->gzind 940 val = s->gzhead->name[s->gzindex++]; 1000 put_byte(s, val); 941 put_byte(s, val); 1001 } while (val != 0); 942 } while (val != 0); 1002 HCRC_UPDATE(beg); 943 HCRC_UPDATE(beg); 1003 s->gzindex = 0; 944 s->gzindex = 0; 1004 } 945 } 1005 s->status = COMMENT_STATE; 946 s->status = COMMENT_STATE; 1006 } 947 } 1007 if (s->status == COMMENT_STATE) { 948 if (s->status == COMMENT_STATE) { 1008 if (s->gzhead->comment != Z_NULL) { 949 if (s->gzhead->comment != Z_NULL) { 1009 ulg beg = s->pending; /* start 950 ulg beg = s->pending; /* start of bytes to update crc */ 1010 int val; 951 int val; 1011 do { 952 do { 1012 if (s->pending == s->pending_ 953 if (s->pending == s->pending_buf_size) { 1013 HCRC_UPDATE(beg); 954 HCRC_UPDATE(beg); 1014 flush_pending(strm); 955 flush_pending(strm); 1015 if (s->pending != 0) { 956 if (s->pending != 0) { 1016 s->last_flush = -1; 957 s->last_flush = -1; 1017 return Z_OK; 958 return Z_OK; 1018 } 959 } 1019 beg = 0; 960 beg = 0; 1020 } 961 } 1021 val = s->gzhead->comment[s->g 962 val = s->gzhead->comment[s->gzindex++]; 1022 put_byte(s, val); 963 put_byte(s, val); 1023 } while (val != 0); 964 } while (val != 0); 1024 HCRC_UPDATE(beg); 965 HCRC_UPDATE(beg); 1025 } 966 } 1026 s->status = HCRC_STATE; 967 s->status = HCRC_STATE; 1027 } 968 } 1028 if (s->status == HCRC_STATE) { 969 if (s->status == HCRC_STATE) { 1029 if (s->gzhead->hcrc) { 970 if (s->gzhead->hcrc) { 1030 if (s->pending + 2 > s->pending_b 971 if (s->pending + 2 > s->pending_buf_size) { 1031 flush_pending(strm); 972 flush_pending(strm); 1032 if (s->pending != 0) { 973 if (s->pending != 0) { 1033 s->last_flush = -1; 974 s->last_flush = -1; 1034 return Z_OK; 975 return Z_OK; 1035 } 976 } 1036 } 977 } 1037 put_byte(s, (Byte)(strm->adler & 978 put_byte(s, (Byte)(strm->adler & 0xff)); 1038 put_byte(s, (Byte)((strm->adler > 979 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 1039 strm->adler = crc32(0L, Z_NULL, 0 980 strm->adler = crc32(0L, Z_NULL, 0); 1040 } 981 } 1041 s->status = BUSY_STATE; 982 s->status = BUSY_STATE; 1042 983 1043 /* Compression must start with an emp 984 /* Compression must start with an empty pending buffer */ 1044 flush_pending(strm); 985 flush_pending(strm); 1045 if (s->pending != 0) { 986 if (s->pending != 0) { 1046 s->last_flush = -1; 987 s->last_flush = -1; 1047 return Z_OK; 988 return Z_OK; 1048 } 989 } 1049 } 990 } 1050 #endif 991 #endif 1051 992 1052 /* Start a new block or continue the curr 993 /* Start a new block or continue the current one. 1053 */ 994 */ 1054 if (strm->avail_in != 0 || s->lookahead ! 995 if (strm->avail_in != 0 || s->lookahead != 0 || 1055 (flush != Z_NO_FLUSH && s->status != 996 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { 1056 block_state bstate; 997 block_state bstate; 1057 998 1058 bstate = s->level == 0 ? deflate_stor 999 bstate = s->level == 0 ? deflate_stored(s, flush) : 1059 s->strategy == Z_HUFFMAN_ONL 1000 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : 1060 s->strategy == Z_RLE ? defla 1001 s->strategy == Z_RLE ? deflate_rle(s, flush) : 1061 (*(configuration_table[s->le 1002 (*(configuration_table[s->level].func))(s, flush); 1062 1003 1063 if (bstate == finish_started || bstat 1004 if (bstate == finish_started || bstate == finish_done) { 1064 s->status = FINISH_STATE; 1005 s->status = FINISH_STATE; 1065 } 1006 } 1066 if (bstate == need_more || bstate == 1007 if (bstate == need_more || bstate == finish_started) { 1067 if (strm->avail_out == 0) { 1008 if (strm->avail_out == 0) { 1068 s->last_flush = -1; /* avoid 1009 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ 1069 } 1010 } 1070 return Z_OK; 1011 return Z_OK; 1071 /* If flush != Z_NO_FLUSH && avai 1012 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 1072 * of deflate should use the same 1013 * of deflate should use the same flush parameter to make sure 1073 * that the flush is complete. So 1014 * that the flush is complete. So we don't have to output an 1074 * empty block here, this will be 1015 * empty block here, this will be done at next call. This also 1075 * ensures that for a very small 1016 * ensures that for a very small output buffer, we emit at most 1076 * one empty block. 1017 * one empty block. 1077 */ 1018 */ 1078 } 1019 } 1079 if (bstate == block_done) { 1020 if (bstate == block_done) { 1080 if (flush == Z_PARTIAL_FLUSH) { 1021 if (flush == Z_PARTIAL_FLUSH) { 1081 _tr_align(s); 1022 _tr_align(s); 1082 } else if (flush != Z_BLOCK) { /* 1023 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ 1083 _tr_stored_block(s, (char*)0, 1024 _tr_stored_block(s, (char*)0, 0L, 0); 1084 /* For a full flush, this emp 1025 /* For a full flush, this empty block will be recognized 1085 * as a special marker by inf 1026 * as a special marker by inflate_sync(). 1086 */ 1027 */ 1087 if (flush == Z_FULL_FLUSH) { 1028 if (flush == Z_FULL_FLUSH) { 1088 CLEAR_HASH(s); 1029 CLEAR_HASH(s); /* forget history */ 1089 if (s->lookahead == 0) { 1030 if (s->lookahead == 0) { 1090 s->strstart = 0; 1031 s->strstart = 0; 1091 s->block_start = 0L; 1032 s->block_start = 0L; 1092 s->insert = 0; 1033 s->insert = 0; 1093 } 1034 } 1094 } 1035 } 1095 } 1036 } 1096 flush_pending(strm); 1037 flush_pending(strm); 1097 if (strm->avail_out == 0) { 1038 if (strm->avail_out == 0) { 1098 s->last_flush = -1; /* avoid BU 1039 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ 1099 return Z_OK; 1040 return Z_OK; 1100 } 1041 } 1101 } 1042 } 1102 } 1043 } 1103 1044 1104 if (flush != Z_FINISH) return Z_OK; 1045 if (flush != Z_FINISH) return Z_OK; 1105 if (s->wrap <= 0) return Z_STREAM_END; 1046 if (s->wrap <= 0) return Z_STREAM_END; 1106 1047 1107 /* Write the trailer */ 1048 /* Write the trailer */ 1108 #ifdef GZIP 1049 #ifdef GZIP 1109 if (s->wrap == 2) { 1050 if (s->wrap == 2) { 1110 put_byte(s, (Byte)(strm->adler & 0xff 1051 put_byte(s, (Byte)(strm->adler & 0xff)); 1111 put_byte(s, (Byte)((strm->adler >> 8) 1052 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 1112 put_byte(s, (Byte)((strm->adler >> 16 1053 put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); 1113 put_byte(s, (Byte)((strm->adler >> 24 1054 put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); 1114 put_byte(s, (Byte)(strm->total_in & 0 1055 put_byte(s, (Byte)(strm->total_in & 0xff)); 1115 put_byte(s, (Byte)((strm->total_in >> 1056 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); 1116 put_byte(s, (Byte)((strm->total_in >> 1057 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); 1117 put_byte(s, (Byte)((strm->total_in >> 1058 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); 1118 } 1059 } 1119 else 1060 else 1120 #endif 1061 #endif 1121 { 1062 { 1122 putShortMSB(s, (uInt)(strm->adler >> 1063 putShortMSB(s, (uInt)(strm->adler >> 16)); 1123 putShortMSB(s, (uInt)(strm->adler & 0 1064 putShortMSB(s, (uInt)(strm->adler & 0xffff)); 1124 } 1065 } 1125 flush_pending(strm); 1066 flush_pending(strm); 1126 /* If avail_out is zero, the application 1067 /* If avail_out is zero, the application will call deflate again 1127 * to flush the rest. 1068 * to flush the rest. 1128 */ 1069 */ 1129 if (s->wrap > 0) s->wrap = -s->wrap; /* w 1070 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ 1130 return s->pending != 0 ? Z_OK : Z_STREAM_ 1071 return s->pending != 0 ? Z_OK : Z_STREAM_END; 1131 } 1072 } 1132 1073 1133 /* ========================================== 1074 /* ========================================================================= */ 1134 int ZEXPORT deflateEnd(strm) << 1075 int ZEXPORT deflateEnd (strm) 1135 z_streamp strm; 1076 z_streamp strm; 1136 { 1077 { 1137 int status; 1078 int status; 1138 1079 1139 if (deflateStateCheck(strm)) return Z_STR 1080 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 1140 1081 1141 status = strm->state->status; 1082 status = strm->state->status; 1142 1083 1143 /* Deallocate in reverse order of allocat 1084 /* Deallocate in reverse order of allocations: */ 1144 TRY_FREE(strm, strm->state->pending_buf); 1085 TRY_FREE(strm, strm->state->pending_buf); 1145 TRY_FREE(strm, strm->state->head); 1086 TRY_FREE(strm, strm->state->head); 1146 TRY_FREE(strm, strm->state->prev); 1087 TRY_FREE(strm, strm->state->prev); 1147 TRY_FREE(strm, strm->state->window); 1088 TRY_FREE(strm, strm->state->window); 1148 1089 1149 ZFREE(strm, strm->state); 1090 ZFREE(strm, strm->state); 1150 strm->state = Z_NULL; 1091 strm->state = Z_NULL; 1151 1092 1152 return status == BUSY_STATE ? Z_DATA_ERRO 1093 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; 1153 } 1094 } 1154 1095 1155 /* ========================================== 1096 /* ========================================================================= 1156 * Copy the source state to the destination s 1097 * Copy the source state to the destination state. 1157 * To simplify the source, this is not suppor 1098 * To simplify the source, this is not supported for 16-bit MSDOS (which 1158 * doesn't have enough memory anyway to dupli 1099 * doesn't have enough memory anyway to duplicate compression states). 1159 */ 1100 */ 1160 int ZEXPORT deflateCopy(dest, source) << 1101 int ZEXPORT deflateCopy (dest, source) 1161 z_streamp dest; 1102 z_streamp dest; 1162 z_streamp source; 1103 z_streamp source; 1163 { 1104 { 1164 #ifdef MAXSEG_64K 1105 #ifdef MAXSEG_64K 1165 return Z_STREAM_ERROR; 1106 return Z_STREAM_ERROR; 1166 #else 1107 #else 1167 deflate_state *ds; 1108 deflate_state *ds; 1168 deflate_state *ss; 1109 deflate_state *ss; >> 1110 ushf *overlay; 1169 1111 1170 1112 1171 if (deflateStateCheck(source) || dest == 1113 if (deflateStateCheck(source) || dest == Z_NULL) { 1172 return Z_STREAM_ERROR; 1114 return Z_STREAM_ERROR; 1173 } 1115 } 1174 1116 1175 ss = source->state; 1117 ss = source->state; 1176 1118 1177 zmemcpy((voidpf)dest, (voidpf)source, siz 1119 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); 1178 1120 1179 ds = (deflate_state *) ZALLOC(dest, 1, si 1121 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); 1180 if (ds == Z_NULL) return Z_MEM_ERROR; 1122 if (ds == Z_NULL) return Z_MEM_ERROR; 1181 dest->state = (struct internal_state FAR 1123 dest->state = (struct internal_state FAR *) ds; 1182 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(de 1124 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); 1183 ds->strm = dest; 1125 ds->strm = dest; 1184 1126 1185 ds->window = (Bytef *) ZALLOC(dest, ds->w 1127 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); 1186 ds->prev = (Posf *) ZALLOC(dest, ds->w 1128 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); 1187 ds->head = (Posf *) ZALLOC(dest, ds->h 1129 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); 1188 ds->pending_buf = (uchf *) ZALLOC(dest, d << 1130 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); >> 1131 ds->pending_buf = (uchf *) overlay; 1189 1132 1190 if (ds->window == Z_NULL || ds->prev == Z 1133 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || 1191 ds->pending_buf == Z_NULL) { 1134 ds->pending_buf == Z_NULL) { 1192 deflateEnd (dest); 1135 deflateEnd (dest); 1193 return Z_MEM_ERROR; 1136 return Z_MEM_ERROR; 1194 } 1137 } 1195 /* following zmemcpy do not work for 16-b 1138 /* following zmemcpy do not work for 16-bit MSDOS */ 1196 zmemcpy(ds->window, ss->window, ds->w_siz 1139 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); 1197 zmemcpy((voidpf)ds->prev, (voidpf)ss->pre 1140 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); 1198 zmemcpy((voidpf)ds->head, (voidpf)ss->hea 1141 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); 1199 zmemcpy(ds->pending_buf, ss->pending_buf, 1142 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); 1200 1143 1201 ds->pending_out = ds->pending_buf + (ss-> 1144 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); 1202 ds->sym_buf = ds->pending_buf + ds->lit_b << 1145 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); >> 1146 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; 1203 1147 1204 ds->l_desc.dyn_tree = ds->dyn_ltree; 1148 ds->l_desc.dyn_tree = ds->dyn_ltree; 1205 ds->d_desc.dyn_tree = ds->dyn_dtree; 1149 ds->d_desc.dyn_tree = ds->dyn_dtree; 1206 ds->bl_desc.dyn_tree = ds->bl_tree; 1150 ds->bl_desc.dyn_tree = ds->bl_tree; 1207 1151 1208 return Z_OK; 1152 return Z_OK; 1209 #endif /* MAXSEG_64K */ 1153 #endif /* MAXSEG_64K */ 1210 } 1154 } 1211 1155 1212 /* ========================================== 1156 /* =========================================================================== 1213 * Read a new buffer from the current input s 1157 * Read a new buffer from the current input stream, update the adler32 1214 * and total number of bytes read. All defla 1158 * and total number of bytes read. All deflate() input goes through 1215 * this function so some applications may wis 1159 * this function so some applications may wish to modify it to avoid 1216 * allocating a large strm->next_in buffer an 1160 * allocating a large strm->next_in buffer and copying from it. 1217 * (See also flush_pending()). 1161 * (See also flush_pending()). 1218 */ 1162 */ 1219 local unsigned read_buf(strm, buf, size) 1163 local unsigned read_buf(strm, buf, size) 1220 z_streamp strm; 1164 z_streamp strm; 1221 Bytef *buf; 1165 Bytef *buf; 1222 unsigned size; 1166 unsigned size; 1223 { 1167 { 1224 unsigned len = strm->avail_in; 1168 unsigned len = strm->avail_in; 1225 1169 1226 if (len > size) len = size; 1170 if (len > size) len = size; 1227 if (len == 0) return 0; 1171 if (len == 0) return 0; 1228 1172 1229 strm->avail_in -= len; 1173 strm->avail_in -= len; 1230 1174 1231 zmemcpy(buf, strm->next_in, len); 1175 zmemcpy(buf, strm->next_in, len); 1232 if (strm->state->wrap == 1) { 1176 if (strm->state->wrap == 1) { 1233 strm->adler = adler32(strm->adler, bu 1177 strm->adler = adler32(strm->adler, buf, len); 1234 } 1178 } 1235 #ifdef GZIP 1179 #ifdef GZIP 1236 else if (strm->state->wrap == 2) { 1180 else if (strm->state->wrap == 2) { 1237 strm->adler = crc32(strm->adler, buf, 1181 strm->adler = crc32(strm->adler, buf, len); 1238 } 1182 } 1239 #endif 1183 #endif 1240 strm->next_in += len; 1184 strm->next_in += len; 1241 strm->total_in += len; 1185 strm->total_in += len; 1242 1186 1243 return len; 1187 return len; 1244 } 1188 } 1245 1189 1246 /* ========================================== 1190 /* =========================================================================== 1247 * Initialize the "longest match" routines fo 1191 * Initialize the "longest match" routines for a new zlib stream 1248 */ 1192 */ 1249 local void lm_init(s) << 1193 local void lm_init (s) 1250 deflate_state *s; 1194 deflate_state *s; 1251 { 1195 { 1252 s->window_size = (ulg)2L*s->w_size; 1196 s->window_size = (ulg)2L*s->w_size; 1253 1197 1254 CLEAR_HASH(s); 1198 CLEAR_HASH(s); 1255 1199 1256 /* Set the default configuration paramete 1200 /* Set the default configuration parameters: 1257 */ 1201 */ 1258 s->max_lazy_match = configuration_table 1202 s->max_lazy_match = configuration_table[s->level].max_lazy; 1259 s->good_match = configuration_table 1203 s->good_match = configuration_table[s->level].good_length; 1260 s->nice_match = configuration_table 1204 s->nice_match = configuration_table[s->level].nice_length; 1261 s->max_chain_length = configuration_table 1205 s->max_chain_length = configuration_table[s->level].max_chain; 1262 1206 1263 s->strstart = 0; 1207 s->strstart = 0; 1264 s->block_start = 0L; 1208 s->block_start = 0L; 1265 s->lookahead = 0; 1209 s->lookahead = 0; 1266 s->insert = 0; 1210 s->insert = 0; 1267 s->match_length = s->prev_length = MIN_MA 1211 s->match_length = s->prev_length = MIN_MATCH-1; 1268 s->match_available = 0; 1212 s->match_available = 0; 1269 s->ins_h = 0; 1213 s->ins_h = 0; >> 1214 #ifndef FASTEST >> 1215 #ifdef ASMV >> 1216 match_init(); /* initialize the asm code */ >> 1217 #endif >> 1218 #endif 1270 } 1219 } 1271 1220 1272 #ifndef FASTEST 1221 #ifndef FASTEST 1273 /* ========================================== 1222 /* =========================================================================== 1274 * Set match_start to the longest match start 1223 * Set match_start to the longest match starting at the given string and 1275 * return its length. Matches shorter or equa 1224 * return its length. Matches shorter or equal to prev_length are discarded, 1276 * in which case the result is equal to prev_ 1225 * in which case the result is equal to prev_length and match_start is 1277 * garbage. 1226 * garbage. 1278 * IN assertions: cur_match is the head of th 1227 * IN assertions: cur_match is the head of the hash chain for the current 1279 * string (strstart) and its distance is <= 1228 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 1280 * OUT assertion: the match length is not gre 1229 * OUT assertion: the match length is not greater than s->lookahead. 1281 */ 1230 */ >> 1231 #ifndef ASMV >> 1232 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or >> 1233 * match.S. The code will be functionally equivalent. >> 1234 */ 1282 local uInt longest_match(s, cur_match) 1235 local uInt longest_match(s, cur_match) 1283 deflate_state *s; 1236 deflate_state *s; 1284 IPos cur_match; 1237 IPos cur_match; /* current match */ 1285 { 1238 { 1286 unsigned chain_length = s->max_chain_leng 1239 unsigned chain_length = s->max_chain_length;/* max hash chain length */ 1287 register Bytef *scan = s->window + s->str 1240 register Bytef *scan = s->window + s->strstart; /* current string */ 1288 register Bytef *match; 1241 register Bytef *match; /* matched string */ 1289 register int len; 1242 register int len; /* length of current match */ 1290 int best_len = (int)s->prev_length; 1243 int best_len = (int)s->prev_length; /* best match length so far */ 1291 int nice_match = s->nice_match; 1244 int nice_match = s->nice_match; /* stop if match long enough */ 1292 IPos limit = s->strstart > (IPos)MAX_DIST 1245 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 1293 s->strstart - (IPos)MAX_DIST(s) : NIL 1246 s->strstart - (IPos)MAX_DIST(s) : NIL; 1294 /* Stop when cur_match becomes <= limit. 1247 /* Stop when cur_match becomes <= limit. To simplify the code, 1295 * we prevent matches with the string of 1248 * we prevent matches with the string of window index 0. 1296 */ 1249 */ 1297 Posf *prev = s->prev; 1250 Posf *prev = s->prev; 1298 uInt wmask = s->w_mask; 1251 uInt wmask = s->w_mask; 1299 1252 1300 #ifdef UNALIGNED_OK 1253 #ifdef UNALIGNED_OK 1301 /* Compare two bytes at a time. Note: thi 1254 /* Compare two bytes at a time. Note: this is not always beneficial. 1302 * Try with and without -DUNALIGNED_OK to 1255 * Try with and without -DUNALIGNED_OK to check. 1303 */ 1256 */ 1304 register Bytef *strend = s->window + s->s 1257 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; 1305 register ush scan_start = *(ushf*)scan; 1258 register ush scan_start = *(ushf*)scan; 1306 register ush scan_end = *(ushf*)(scan + << 1259 register ush scan_end = *(ushf*)(scan+best_len-1); 1307 #else 1260 #else 1308 register Bytef *strend = s->window + s->s 1261 register Bytef *strend = s->window + s->strstart + MAX_MATCH; 1309 register Byte scan_end1 = scan[best_len << 1262 register Byte scan_end1 = scan[best_len-1]; 1310 register Byte scan_end = scan[best_len] 1263 register Byte scan_end = scan[best_len]; 1311 #endif 1264 #endif 1312 1265 1313 /* The code is optimized for HASH_BITS >= 1266 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1314 * It is easy to get rid of this optimiza 1267 * It is easy to get rid of this optimization if necessary. 1315 */ 1268 */ 1316 Assert(s->hash_bits >= 8 && MAX_MATCH == 1269 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 1317 1270 1318 /* Do not waste too much time if we alrea 1271 /* Do not waste too much time if we already have a good match: */ 1319 if (s->prev_length >= s->good_match) { 1272 if (s->prev_length >= s->good_match) { 1320 chain_length >>= 2; 1273 chain_length >>= 2; 1321 } 1274 } 1322 /* Do not look for matches beyond the end 1275 /* Do not look for matches beyond the end of the input. This is necessary 1323 * to make deflate deterministic. 1276 * to make deflate deterministic. 1324 */ 1277 */ 1325 if ((uInt)nice_match > s->lookahead) nice 1278 if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; 1326 1279 1327 Assert((ulg)s->strstart <= s->window_size << 1280 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 1328 "need lookahead"); << 1329 1281 1330 do { 1282 do { 1331 Assert(cur_match < s->strstart, "no f 1283 Assert(cur_match < s->strstart, "no future"); 1332 match = s->window + cur_match; 1284 match = s->window + cur_match; 1333 1285 1334 /* Skip to next match if the match le 1286 /* Skip to next match if the match length cannot increase 1335 * or if the match length is less tha 1287 * or if the match length is less than 2. Note that the checks below 1336 * for insufficient lookahead only oc 1288 * for insufficient lookahead only occur occasionally for performance 1337 * reasons. Therefore uninitialized 1289 * reasons. Therefore uninitialized memory will be accessed, and 1338 * conditional jumps will be made tha 1290 * conditional jumps will be made that depend on those values. 1339 * However the length of the match is 1291 * However the length of the match is limited to the lookahead, so 1340 * the output of deflate is not affec 1292 * the output of deflate is not affected by the uninitialized values. 1341 */ 1293 */ 1342 #if (defined(UNALIGNED_OK) && MAX_MATCH == 25 1294 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 1343 /* This code assumes sizeof(unsigned 1295 /* This code assumes sizeof(unsigned short) == 2. Do not use 1344 * UNALIGNED_OK if your compiler uses 1296 * UNALIGNED_OK if your compiler uses a different size. 1345 */ 1297 */ 1346 if (*(ushf*)(match + best_len - 1) != << 1298 if (*(ushf*)(match+best_len-1) != scan_end || 1347 *(ushf*)match != scan_start) cont 1299 *(ushf*)match != scan_start) continue; 1348 1300 1349 /* It is not necessary to compare sca 1301 /* It is not necessary to compare scan[2] and match[2] since they are 1350 * always equal when the other bytes 1302 * always equal when the other bytes match, given that the hash keys 1351 * are equal and that HASH_BITS >= 8. 1303 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at 1352 * strstart + 3, + 5, up to strstart << 1304 * strstart+3, +5, ... up to strstart+257. We check for insufficient 1353 * lookahead only every 4th compariso 1305 * lookahead only every 4th comparison; the 128th check will be made 1354 * at strstart + 257. If MAX_MATCH-2 << 1306 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is 1355 * necessary to put more guard bytes 1307 * necessary to put more guard bytes at the end of the window, or 1356 * to check more often for insufficie 1308 * to check more often for insufficient lookahead. 1357 */ 1309 */ 1358 Assert(scan[2] == match[2], "scan[2]? 1310 Assert(scan[2] == match[2], "scan[2]?"); 1359 scan++, match++; 1311 scan++, match++; 1360 do { 1312 do { 1361 } while (*(ushf*)(scan += 2) == *(ush << 1313 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1362 *(ushf*)(scan += 2) == *(ush << 1314 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1363 *(ushf*)(scan += 2) == *(ush << 1315 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1364 *(ushf*)(scan += 2) == *(ush << 1316 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1365 scan < strend); 1317 scan < strend); 1366 /* The funny "do {}" generates better 1318 /* The funny "do {}" generates better code on most compilers */ 1367 1319 1368 /* Here, scan <= window + strstart + << 1320 /* Here, scan <= window+strstart+257 */ 1369 Assert(scan <= s->window + (unsigned) << 1321 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1370 "wild scan"); << 1371 if (*scan == *match) scan++; 1322 if (*scan == *match) scan++; 1372 1323 1373 len = (MAX_MATCH - 1) - (int)(strend << 1324 len = (MAX_MATCH - 1) - (int)(strend-scan); 1374 scan = strend - (MAX_MATCH-1); 1325 scan = strend - (MAX_MATCH-1); 1375 1326 1376 #else /* UNALIGNED_OK */ 1327 #else /* UNALIGNED_OK */ 1377 1328 1378 if (match[best_len] != scan_end << 1329 if (match[best_len] != scan_end || 1379 match[best_len - 1] != scan_end1 << 1330 match[best_len-1] != scan_end1 || 1380 *match != *scan << 1331 *match != *scan || 1381 *++match != scan[1]) << 1332 *++match != scan[1]) continue; 1382 1333 1383 /* The check at best_len - 1 can be r << 1334 /* The check at best_len-1 can be removed because it will be made 1384 * again later. (This heuristic is no 1335 * again later. (This heuristic is not always a win.) 1385 * It is not necessary to compare sca 1336 * It is not necessary to compare scan[2] and match[2] since they 1386 * are always equal when the other by 1337 * are always equal when the other bytes match, given that 1387 * the hash keys are equal and that H 1338 * the hash keys are equal and that HASH_BITS >= 8. 1388 */ 1339 */ 1389 scan += 2, match++; 1340 scan += 2, match++; 1390 Assert(*scan == *match, "match[2]?"); 1341 Assert(*scan == *match, "match[2]?"); 1391 1342 1392 /* We check for insufficient lookahea 1343 /* We check for insufficient lookahead only every 8th comparison; 1393 * the 256th check will be made at st << 1344 * the 256th check will be made at strstart+258. 1394 */ 1345 */ 1395 do { 1346 do { 1396 } while (*++scan == *++match && *++sc 1347 } while (*++scan == *++match && *++scan == *++match && 1397 *++scan == *++match && *++sc 1348 *++scan == *++match && *++scan == *++match && 1398 *++scan == *++match && *++sc 1349 *++scan == *++match && *++scan == *++match && 1399 *++scan == *++match && *++sc 1350 *++scan == *++match && *++scan == *++match && 1400 scan < strend); 1351 scan < strend); 1401 1352 1402 Assert(scan <= s->window + (unsigned) << 1353 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1403 "wild scan"); << 1404 1354 1405 len = MAX_MATCH - (int)(strend - scan 1355 len = MAX_MATCH - (int)(strend - scan); 1406 scan = strend - MAX_MATCH; 1356 scan = strend - MAX_MATCH; 1407 1357 1408 #endif /* UNALIGNED_OK */ 1358 #endif /* UNALIGNED_OK */ 1409 1359 1410 if (len > best_len) { 1360 if (len > best_len) { 1411 s->match_start = cur_match; 1361 s->match_start = cur_match; 1412 best_len = len; 1362 best_len = len; 1413 if (len >= nice_match) break; 1363 if (len >= nice_match) break; 1414 #ifdef UNALIGNED_OK 1364 #ifdef UNALIGNED_OK 1415 scan_end = *(ushf*)(scan + best_l << 1365 scan_end = *(ushf*)(scan+best_len-1); 1416 #else 1366 #else 1417 scan_end1 = scan[best_len - 1]; << 1367 scan_end1 = scan[best_len-1]; 1418 scan_end = scan[best_len]; 1368 scan_end = scan[best_len]; 1419 #endif 1369 #endif 1420 } 1370 } 1421 } while ((cur_match = prev[cur_match & wm 1371 } while ((cur_match = prev[cur_match & wmask]) > limit 1422 && --chain_length != 0); 1372 && --chain_length != 0); 1423 1373 1424 if ((uInt)best_len <= s->lookahead) retur 1374 if ((uInt)best_len <= s->lookahead) return (uInt)best_len; 1425 return s->lookahead; 1375 return s->lookahead; 1426 } 1376 } >> 1377 #endif /* ASMV */ 1427 1378 1428 #else /* FASTEST */ 1379 #else /* FASTEST */ 1429 1380 1430 /* ------------------------------------------ 1381 /* --------------------------------------------------------------------------- 1431 * Optimized version for FASTEST only 1382 * Optimized version for FASTEST only 1432 */ 1383 */ 1433 local uInt longest_match(s, cur_match) 1384 local uInt longest_match(s, cur_match) 1434 deflate_state *s; 1385 deflate_state *s; 1435 IPos cur_match; 1386 IPos cur_match; /* current match */ 1436 { 1387 { 1437 register Bytef *scan = s->window + s->str 1388 register Bytef *scan = s->window + s->strstart; /* current string */ 1438 register Bytef *match; 1389 register Bytef *match; /* matched string */ 1439 register int len; 1390 register int len; /* length of current match */ 1440 register Bytef *strend = s->window + s->s 1391 register Bytef *strend = s->window + s->strstart + MAX_MATCH; 1441 1392 1442 /* The code is optimized for HASH_BITS >= 1393 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1443 * It is easy to get rid of this optimiza 1394 * It is easy to get rid of this optimization if necessary. 1444 */ 1395 */ 1445 Assert(s->hash_bits >= 8 && MAX_MATCH == 1396 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 1446 1397 1447 Assert((ulg)s->strstart <= s->window_size << 1398 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 1448 "need lookahead"); << 1449 1399 1450 Assert(cur_match < s->strstart, "no futur 1400 Assert(cur_match < s->strstart, "no future"); 1451 1401 1452 match = s->window + cur_match; 1402 match = s->window + cur_match; 1453 1403 1454 /* Return failure if the match length is 1404 /* Return failure if the match length is less than 2: 1455 */ 1405 */ 1456 if (match[0] != scan[0] || match[1] != sc 1406 if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; 1457 1407 1458 /* The check at best_len - 1 can be remov << 1408 /* The check at best_len-1 can be removed because it will be made 1459 * again later. (This heuristic is not al 1409 * again later. (This heuristic is not always a win.) 1460 * It is not necessary to compare scan[2] 1410 * It is not necessary to compare scan[2] and match[2] since they 1461 * are always equal when the other bytes 1411 * are always equal when the other bytes match, given that 1462 * the hash keys are equal and that HASH_ 1412 * the hash keys are equal and that HASH_BITS >= 8. 1463 */ 1413 */ 1464 scan += 2, match += 2; 1414 scan += 2, match += 2; 1465 Assert(*scan == *match, "match[2]?"); 1415 Assert(*scan == *match, "match[2]?"); 1466 1416 1467 /* We check for insufficient lookahead on 1417 /* We check for insufficient lookahead only every 8th comparison; 1468 * the 256th check will be made at strsta << 1418 * the 256th check will be made at strstart+258. 1469 */ 1419 */ 1470 do { 1420 do { 1471 } while (*++scan == *++match && *++scan = 1421 } while (*++scan == *++match && *++scan == *++match && 1472 *++scan == *++match && *++scan = 1422 *++scan == *++match && *++scan == *++match && 1473 *++scan == *++match && *++scan = 1423 *++scan == *++match && *++scan == *++match && 1474 *++scan == *++match && *++scan = 1424 *++scan == *++match && *++scan == *++match && 1475 scan < strend); 1425 scan < strend); 1476 1426 1477 Assert(scan <= s->window + (unsigned)(s-> << 1427 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1478 1428 1479 len = MAX_MATCH - (int)(strend - scan); 1429 len = MAX_MATCH - (int)(strend - scan); 1480 1430 1481 if (len < MIN_MATCH) return MIN_MATCH - 1 1431 if (len < MIN_MATCH) return MIN_MATCH - 1; 1482 1432 1483 s->match_start = cur_match; 1433 s->match_start = cur_match; 1484 return (uInt)len <= s->lookahead ? (uInt) 1434 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; 1485 } 1435 } 1486 1436 1487 #endif /* FASTEST */ 1437 #endif /* FASTEST */ 1488 1438 1489 #ifdef ZLIB_DEBUG 1439 #ifdef ZLIB_DEBUG 1490 1440 1491 #define EQUAL 0 1441 #define EQUAL 0 1492 /* result of memcmp for equal strings */ 1442 /* result of memcmp for equal strings */ 1493 1443 1494 /* ========================================== 1444 /* =========================================================================== 1495 * Check that the match at match_start is ind 1445 * Check that the match at match_start is indeed a match. 1496 */ 1446 */ 1497 local void check_match(s, start, match, lengt 1447 local void check_match(s, start, match, length) 1498 deflate_state *s; 1448 deflate_state *s; 1499 IPos start, match; 1449 IPos start, match; 1500 int length; 1450 int length; 1501 { 1451 { 1502 /* check that the match is indeed a match 1452 /* check that the match is indeed a match */ 1503 if (zmemcmp(s->window + match, 1453 if (zmemcmp(s->window + match, 1504 s->window + start, length) != 1454 s->window + start, length) != EQUAL) { 1505 fprintf(stderr, " start %u, match %u, 1455 fprintf(stderr, " start %u, match %u, length %d\n", 1506 start, match, length); 1456 start, match, length); 1507 do { 1457 do { 1508 fprintf(stderr, "%c%c", s->window 1458 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); 1509 } while (--length != 0); 1459 } while (--length != 0); 1510 z_error("invalid match"); 1460 z_error("invalid match"); 1511 } 1461 } 1512 if (z_verbose > 1) { 1462 if (z_verbose > 1) { 1513 fprintf(stderr,"\\[%d,%d]", start - m << 1463 fprintf(stderr,"\\[%d,%d]", start-match, length); 1514 do { putc(s->window[start++], stderr) 1464 do { putc(s->window[start++], stderr); } while (--length != 0); 1515 } 1465 } 1516 } 1466 } 1517 #else 1467 #else 1518 # define check_match(s, start, match, length 1468 # define check_match(s, start, match, length) 1519 #endif /* ZLIB_DEBUG */ 1469 #endif /* ZLIB_DEBUG */ 1520 1470 1521 /* ========================================== 1471 /* =========================================================================== 1522 * Fill the window when the lookahead becomes 1472 * Fill the window when the lookahead becomes insufficient. 1523 * Updates strstart and lookahead. 1473 * Updates strstart and lookahead. 1524 * 1474 * 1525 * IN assertion: lookahead < MIN_LOOKAHEAD 1475 * IN assertion: lookahead < MIN_LOOKAHEAD 1526 * OUT assertions: strstart <= window_size-MI 1476 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD 1527 * At least one byte has been read, or ava 1477 * At least one byte has been read, or avail_in == 0; reads are 1528 * performed for at least two bytes (requi 1478 * performed for at least two bytes (required for the zip translate_eol 1529 * option -- not supported here). 1479 * option -- not supported here). 1530 */ 1480 */ 1531 local void fill_window(s) 1481 local void fill_window(s) 1532 deflate_state *s; 1482 deflate_state *s; 1533 { 1483 { 1534 unsigned n; 1484 unsigned n; 1535 unsigned more; /* Amount of free space 1485 unsigned more; /* Amount of free space at the end of the window. */ 1536 uInt wsize = s->w_size; 1486 uInt wsize = s->w_size; 1537 1487 1538 Assert(s->lookahead < MIN_LOOKAHEAD, "alr 1488 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); 1539 1489 1540 do { 1490 do { 1541 more = (unsigned)(s->window_size -(ul 1491 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); 1542 1492 1543 /* Deal with !@#$% 64K limit: */ 1493 /* Deal with !@#$% 64K limit: */ 1544 if (sizeof(int) <= 2) { 1494 if (sizeof(int) <= 2) { 1545 if (more == 0 && s->strstart == 0 1495 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 1546 more = wsize; 1496 more = wsize; 1547 1497 1548 } else if (more == (unsigned)(-1) 1498 } else if (more == (unsigned)(-1)) { 1549 /* Very unlikely, but possibl 1499 /* Very unlikely, but possible on 16 bit machine if 1550 * strstart == 0 && lookahead 1500 * strstart == 0 && lookahead == 1 (input done a byte at time) 1551 */ 1501 */ 1552 more--; 1502 more--; 1553 } 1503 } 1554 } 1504 } 1555 1505 1556 /* If the window is almost full and t 1506 /* If the window is almost full and there is insufficient lookahead, 1557 * move the upper half to the lower o 1507 * move the upper half to the lower one to make room in the upper half. 1558 */ 1508 */ 1559 if (s->strstart >= wsize + MAX_DIST(s << 1509 if (s->strstart >= wsize+MAX_DIST(s)) { 1560 1510 1561 zmemcpy(s->window, s->window + ws << 1511 zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more); 1562 s->match_start -= wsize; 1512 s->match_start -= wsize; 1563 s->strstart -= wsize; /* we no 1513 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ 1564 s->block_start -= (long) wsize; 1514 s->block_start -= (long) wsize; 1565 if (s->insert > s->strstart) << 1566 s->insert = s->strstart; << 1567 slide_hash(s); 1515 slide_hash(s); 1568 more += wsize; 1516 more += wsize; 1569 } 1517 } 1570 if (s->strm->avail_in == 0) break; 1518 if (s->strm->avail_in == 0) break; 1571 1519 1572 /* If there was no sliding: 1520 /* If there was no sliding: 1573 * strstart <= WSIZE+MAX_DIST-1 && 1521 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && 1574 * more == window_size - lookahead 1522 * more == window_size - lookahead - strstart 1575 * => more >= window_size - (MIN_LOOK 1523 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) 1576 * => more >= window_size - 2*WSIZE + 1524 * => more >= window_size - 2*WSIZE + 2 1577 * In the BIG_MEM or MMAP case (not y 1525 * In the BIG_MEM or MMAP case (not yet supported), 1578 * window_size == input_size + MIN_ 1526 * window_size == input_size + MIN_LOOKAHEAD && 1579 * strstart + s->lookahead <= input 1527 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. 1580 * Otherwise, window_size == 2*WSIZE 1528 * Otherwise, window_size == 2*WSIZE so more >= 2. 1581 * If there was sliding, more >= WSIZ 1529 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. 1582 */ 1530 */ 1583 Assert(more >= 2, "more < 2"); 1531 Assert(more >= 2, "more < 2"); 1584 1532 1585 n = read_buf(s->strm, s->window + s-> 1533 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); 1586 s->lookahead += n; 1534 s->lookahead += n; 1587 1535 1588 /* Initialize the hash value now that 1536 /* Initialize the hash value now that we have some input: */ 1589 if (s->lookahead + s->insert >= MIN_M 1537 if (s->lookahead + s->insert >= MIN_MATCH) { 1590 uInt str = s->strstart - s->inser 1538 uInt str = s->strstart - s->insert; 1591 s->ins_h = s->window[str]; 1539 s->ins_h = s->window[str]; 1592 UPDATE_HASH(s, s->ins_h, s->windo 1540 UPDATE_HASH(s, s->ins_h, s->window[str + 1]); 1593 #if MIN_MATCH != 3 1541 #if MIN_MATCH != 3 1594 Call UPDATE_HASH() MIN_MATCH-3 mo 1542 Call UPDATE_HASH() MIN_MATCH-3 more times 1595 #endif 1543 #endif 1596 while (s->insert) { 1544 while (s->insert) { 1597 UPDATE_HASH(s, s->ins_h, s->w 1545 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); 1598 #ifndef FASTEST 1546 #ifndef FASTEST 1599 s->prev[str & s->w_mask] = s- 1547 s->prev[str & s->w_mask] = s->head[s->ins_h]; 1600 #endif 1548 #endif 1601 s->head[s->ins_h] = (Pos)str; 1549 s->head[s->ins_h] = (Pos)str; 1602 str++; 1550 str++; 1603 s->insert--; 1551 s->insert--; 1604 if (s->lookahead + s->insert 1552 if (s->lookahead + s->insert < MIN_MATCH) 1605 break; 1553 break; 1606 } 1554 } 1607 } 1555 } 1608 /* If the whole input has less than M 1556 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, 1609 * but this is not important since on 1557 * but this is not important since only literal bytes will be emitted. 1610 */ 1558 */ 1611 1559 1612 } while (s->lookahead < MIN_LOOKAHEAD && 1560 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); 1613 1561 1614 /* If the WIN_INIT bytes after the end of 1562 /* If the WIN_INIT bytes after the end of the current data have never been 1615 * written, then zero those bytes in orde 1563 * written, then zero those bytes in order to avoid memory check reports of 1616 * the use of uninitialized (or uninitial 1564 * the use of uninitialized (or uninitialised as Julian writes) bytes by 1617 * the longest match routines. Update th 1565 * the longest match routines. Update the high water mark for the next 1618 * time through here. WIN_INIT is set to 1566 * time through here. WIN_INIT is set to MAX_MATCH since the longest match 1619 * routines allow scanning to strstart + 1567 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. 1620 */ 1568 */ 1621 if (s->high_water < s->window_size) { 1569 if (s->high_water < s->window_size) { 1622 ulg curr = s->strstart + (ulg)(s->loo 1570 ulg curr = s->strstart + (ulg)(s->lookahead); 1623 ulg init; 1571 ulg init; 1624 1572 1625 if (s->high_water < curr) { 1573 if (s->high_water < curr) { 1626 /* Previous high water mark below 1574 /* Previous high water mark below current data -- zero WIN_INIT 1627 * bytes or up to end of window, 1575 * bytes or up to end of window, whichever is less. 1628 */ 1576 */ 1629 init = s->window_size - curr; 1577 init = s->window_size - curr; 1630 if (init > WIN_INIT) 1578 if (init > WIN_INIT) 1631 init = WIN_INIT; 1579 init = WIN_INIT; 1632 zmemzero(s->window + curr, (unsig 1580 zmemzero(s->window + curr, (unsigned)init); 1633 s->high_water = curr + init; 1581 s->high_water = curr + init; 1634 } 1582 } 1635 else if (s->high_water < (ulg)curr + 1583 else if (s->high_water < (ulg)curr + WIN_INIT) { 1636 /* High water mark at or above cu 1584 /* High water mark at or above current data, but below current data 1637 * plus WIN_INIT -- zero out to c 1585 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up 1638 * to end of window, whichever is 1586 * to end of window, whichever is less. 1639 */ 1587 */ 1640 init = (ulg)curr + WIN_INIT - s-> 1588 init = (ulg)curr + WIN_INIT - s->high_water; 1641 if (init > s->window_size - s->hi 1589 if (init > s->window_size - s->high_water) 1642 init = s->window_size - s->hi 1590 init = s->window_size - s->high_water; 1643 zmemzero(s->window + s->high_wate 1591 zmemzero(s->window + s->high_water, (unsigned)init); 1644 s->high_water += init; 1592 s->high_water += init; 1645 } 1593 } 1646 } 1594 } 1647 1595 1648 Assert((ulg)s->strstart <= s->window_size 1596 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, 1649 "not enough room for search"); 1597 "not enough room for search"); 1650 } 1598 } 1651 1599 1652 /* ========================================== 1600 /* =========================================================================== 1653 * Flush the current block, with given end-of 1601 * Flush the current block, with given end-of-file flag. 1654 * IN assertion: strstart is set to the end o 1602 * IN assertion: strstart is set to the end of the current match. 1655 */ 1603 */ 1656 #define FLUSH_BLOCK_ONLY(s, last) { \ 1604 #define FLUSH_BLOCK_ONLY(s, last) { \ 1657 _tr_flush_block(s, (s->block_start >= 0L ? 1605 _tr_flush_block(s, (s->block_start >= 0L ? \ 1658 (charf *)&s->window[(unsig 1606 (charf *)&s->window[(unsigned)s->block_start] : \ 1659 (charf *)Z_NULL), \ 1607 (charf *)Z_NULL), \ 1660 (ulg)((long)s->strstart - s-> 1608 (ulg)((long)s->strstart - s->block_start), \ 1661 (last)); \ 1609 (last)); \ 1662 s->block_start = s->strstart; \ 1610 s->block_start = s->strstart; \ 1663 flush_pending(s->strm); \ 1611 flush_pending(s->strm); \ 1664 Tracev((stderr,"[FLUSH]")); \ 1612 Tracev((stderr,"[FLUSH]")); \ 1665 } 1613 } 1666 1614 1667 /* Same but force premature exit if necessary 1615 /* Same but force premature exit if necessary. */ 1668 #define FLUSH_BLOCK(s, last) { \ 1616 #define FLUSH_BLOCK(s, last) { \ 1669 FLUSH_BLOCK_ONLY(s, last); \ 1617 FLUSH_BLOCK_ONLY(s, last); \ 1670 if (s->strm->avail_out == 0) return (last) 1618 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ 1671 } 1619 } 1672 1620 1673 /* Maximum stored block length in deflate for 1621 /* Maximum stored block length in deflate format (not including header). */ 1674 #define MAX_STORED 65535 1622 #define MAX_STORED 65535 1675 1623 1676 /* Minimum of a and b. */ 1624 /* Minimum of a and b. */ 1677 #define MIN(a, b) ((a) > (b) ? (b) : (a)) 1625 #define MIN(a, b) ((a) > (b) ? (b) : (a)) 1678 1626 1679 /* ========================================== 1627 /* =========================================================================== 1680 * Copy without compression as much as possib 1628 * Copy without compression as much as possible from the input stream, return 1681 * the current block state. 1629 * the current block state. 1682 * 1630 * 1683 * In case deflateParams() is used to later s 1631 * In case deflateParams() is used to later switch to a non-zero compression 1684 * level, s->matches (otherwise unused when s 1632 * level, s->matches (otherwise unused when storing) keeps track of the number 1685 * of hash table slides to perform. If s->mat 1633 * of hash table slides to perform. If s->matches is 1, then one hash table 1686 * slide will be done when switching. If s->m 1634 * slide will be done when switching. If s->matches is 2, the maximum value 1687 * allowed here, then the hash table will be 1635 * allowed here, then the hash table will be cleared, since two or more slides 1688 * is the same as a clear. 1636 * is the same as a clear. 1689 * 1637 * 1690 * deflate_stored() is written to minimize th 1638 * deflate_stored() is written to minimize the number of times an input byte is 1691 * copied. It is most efficient with large in 1639 * copied. It is most efficient with large input and output buffers, which 1692 * maximizes the opportunities to have a sing << 1640 * maximizes the opportunites to have a single copy from next_in to next_out. 1693 */ 1641 */ 1694 local block_state deflate_stored(s, flush) 1642 local block_state deflate_stored(s, flush) 1695 deflate_state *s; 1643 deflate_state *s; 1696 int flush; 1644 int flush; 1697 { 1645 { 1698 /* Smallest worthy block size when not fl 1646 /* Smallest worthy block size when not flushing or finishing. By default 1699 * this is 32K. This can be as small as 5 1647 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For 1700 * large input and output buffers, the st 1648 * large input and output buffers, the stored block size will be larger. 1701 */ 1649 */ 1702 unsigned min_block = MIN(s->pending_buf_s 1650 unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size); 1703 1651 1704 /* Copy as many min_block or larger store 1652 /* Copy as many min_block or larger stored blocks directly to next_out as 1705 * possible. If flushing, copy the remain 1653 * possible. If flushing, copy the remaining available input to next_out as 1706 * stored blocks, if there is enough spac 1654 * stored blocks, if there is enough space. 1707 */ 1655 */ 1708 unsigned len, left, have, last = 0; 1656 unsigned len, left, have, last = 0; 1709 unsigned used = s->strm->avail_in; 1657 unsigned used = s->strm->avail_in; 1710 do { 1658 do { 1711 /* Set len to the maximum size block 1659 /* Set len to the maximum size block that we can copy directly with the 1712 * available input data and output sp 1660 * available input data and output space. Set left to how much of that 1713 * would be copied from what's left i 1661 * would be copied from what's left in the window. 1714 */ 1662 */ 1715 len = MAX_STORED; /* maximum de 1663 len = MAX_STORED; /* maximum deflate stored block length */ 1716 have = (s->bi_valid + 42) >> 3; 1664 have = (s->bi_valid + 42) >> 3; /* number of header bytes */ 1717 if (s->strm->avail_out < have) 1665 if (s->strm->avail_out < have) /* need room for header */ 1718 break; 1666 break; 1719 /* maximum stored block length th 1667 /* maximum stored block length that will fit in avail_out: */ 1720 have = s->strm->avail_out - have; 1668 have = s->strm->avail_out - have; 1721 left = s->strstart - s->block_start; 1669 left = s->strstart - s->block_start; /* bytes left in window */ 1722 if (len > (ulg)left + s->strm->avail_ 1670 if (len > (ulg)left + s->strm->avail_in) 1723 len = left + s->strm->avail_in; 1671 len = left + s->strm->avail_in; /* limit len to the input */ 1724 if (len > have) 1672 if (len > have) 1725 len = have; 1673 len = have; /* limit len to the output */ 1726 1674 1727 /* If the stored block would be less 1675 /* If the stored block would be less than min_block in length, or if 1728 * unable to copy all of the availabl 1676 * unable to copy all of the available input when flushing, then try 1729 * copying to the window and the pend 1677 * copying to the window and the pending buffer instead. Also don't 1730 * write an empty block when flushing 1678 * write an empty block when flushing -- deflate() does that. 1731 */ 1679 */ 1732 if (len < min_block && ((len == 0 && 1680 if (len < min_block && ((len == 0 && flush != Z_FINISH) || 1733 flush == Z_NO 1681 flush == Z_NO_FLUSH || 1734 len != left + 1682 len != left + s->strm->avail_in)) 1735 break; 1683 break; 1736 1684 1737 /* Make a dummy stored block in pendi 1685 /* Make a dummy stored block in pending to get the header bytes, 1738 * including any pending bits. This a 1686 * including any pending bits. This also updates the debugging counts. 1739 */ 1687 */ 1740 last = flush == Z_FINISH && len == le 1688 last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; 1741 _tr_stored_block(s, (char *)0, 0L, la 1689 _tr_stored_block(s, (char *)0, 0L, last); 1742 1690 1743 /* Replace the lengths in the dummy s 1691 /* Replace the lengths in the dummy stored block with len. */ 1744 s->pending_buf[s->pending - 4] = len; 1692 s->pending_buf[s->pending - 4] = len; 1745 s->pending_buf[s->pending - 3] = len 1693 s->pending_buf[s->pending - 3] = len >> 8; 1746 s->pending_buf[s->pending - 2] = ~len 1694 s->pending_buf[s->pending - 2] = ~len; 1747 s->pending_buf[s->pending - 1] = ~len 1695 s->pending_buf[s->pending - 1] = ~len >> 8; 1748 1696 1749 /* Write the stored block header byte 1697 /* Write the stored block header bytes. */ 1750 flush_pending(s->strm); 1698 flush_pending(s->strm); 1751 1699 1752 #ifdef ZLIB_DEBUG 1700 #ifdef ZLIB_DEBUG 1753 /* Update debugging counts for the da 1701 /* Update debugging counts for the data about to be copied. */ 1754 s->compressed_len += len << 3; 1702 s->compressed_len += len << 3; 1755 s->bits_sent += len << 3; 1703 s->bits_sent += len << 3; 1756 #endif 1704 #endif 1757 1705 1758 /* Copy uncompressed bytes from the w 1706 /* Copy uncompressed bytes from the window to next_out. */ 1759 if (left) { 1707 if (left) { 1760 if (left > len) 1708 if (left > len) 1761 left = len; 1709 left = len; 1762 zmemcpy(s->strm->next_out, s->win 1710 zmemcpy(s->strm->next_out, s->window + s->block_start, left); 1763 s->strm->next_out += left; 1711 s->strm->next_out += left; 1764 s->strm->avail_out -= left; 1712 s->strm->avail_out -= left; 1765 s->strm->total_out += left; 1713 s->strm->total_out += left; 1766 s->block_start += left; 1714 s->block_start += left; 1767 len -= left; 1715 len -= left; 1768 } 1716 } 1769 1717 1770 /* Copy uncompressed bytes directly f 1718 /* Copy uncompressed bytes directly from next_in to next_out, updating 1771 * the check value. 1719 * the check value. 1772 */ 1720 */ 1773 if (len) { 1721 if (len) { 1774 read_buf(s->strm, s->strm->next_o 1722 read_buf(s->strm, s->strm->next_out, len); 1775 s->strm->next_out += len; 1723 s->strm->next_out += len; 1776 s->strm->avail_out -= len; 1724 s->strm->avail_out -= len; 1777 s->strm->total_out += len; 1725 s->strm->total_out += len; 1778 } 1726 } 1779 } while (last == 0); 1727 } while (last == 0); 1780 1728 1781 /* Update the sliding window with the las 1729 /* Update the sliding window with the last s->w_size bytes of the copied 1782 * data, or append all of the copied data 1730 * data, or append all of the copied data to the existing window if less 1783 * than s->w_size bytes were copied. Also 1731 * than s->w_size bytes were copied. Also update the number of bytes to 1784 * insert in the hash tables, in the even 1732 * insert in the hash tables, in the event that deflateParams() switches to 1785 * a non-zero compression level. 1733 * a non-zero compression level. 1786 */ 1734 */ 1787 used -= s->strm->avail_in; /* number 1735 used -= s->strm->avail_in; /* number of input bytes directly copied */ 1788 if (used) { 1736 if (used) { 1789 /* If any input was used, then no unu 1737 /* If any input was used, then no unused input remains in the window, 1790 * therefore s->block_start == s->str 1738 * therefore s->block_start == s->strstart. 1791 */ 1739 */ 1792 if (used >= s->w_size) { /* suppla 1740 if (used >= s->w_size) { /* supplant the previous history */ 1793 s->matches = 2; /* clear 1741 s->matches = 2; /* clear hash */ 1794 zmemcpy(s->window, s->strm->next_ 1742 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); 1795 s->strstart = s->w_size; 1743 s->strstart = s->w_size; 1796 s->insert = s->strstart; << 1797 } 1744 } 1798 else { 1745 else { 1799 if (s->window_size - s->strstart 1746 if (s->window_size - s->strstart <= used) { 1800 /* Slide the window down. */ 1747 /* Slide the window down. */ 1801 s->strstart -= s->w_size; 1748 s->strstart -= s->w_size; 1802 zmemcpy(s->window, s->window 1749 zmemcpy(s->window, s->window + s->w_size, s->strstart); 1803 if (s->matches < 2) 1750 if (s->matches < 2) 1804 s->matches++; /* add a 1751 s->matches++; /* add a pending slide_hash() */ 1805 if (s->insert > s->strstart) << 1806 s->insert = s->strstart; << 1807 } 1752 } 1808 zmemcpy(s->window + s->strstart, 1753 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); 1809 s->strstart += used; 1754 s->strstart += used; 1810 s->insert += MIN(used, s->w_size << 1811 } 1755 } 1812 s->block_start = s->strstart; 1756 s->block_start = s->strstart; >> 1757 s->insert += MIN(used, s->w_size - s->insert); 1813 } 1758 } 1814 if (s->high_water < s->strstart) 1759 if (s->high_water < s->strstart) 1815 s->high_water = s->strstart; 1760 s->high_water = s->strstart; 1816 1761 1817 /* If the last block was written to next_ 1762 /* If the last block was written to next_out, then done. */ 1818 if (last) 1763 if (last) 1819 return finish_done; 1764 return finish_done; 1820 1765 1821 /* If flushing and all input has been con 1766 /* If flushing and all input has been consumed, then done. */ 1822 if (flush != Z_NO_FLUSH && flush != Z_FIN 1767 if (flush != Z_NO_FLUSH && flush != Z_FINISH && 1823 s->strm->avail_in == 0 && (long)s->st 1768 s->strm->avail_in == 0 && (long)s->strstart == s->block_start) 1824 return block_done; 1769 return block_done; 1825 1770 1826 /* Fill the window with any remaining inp 1771 /* Fill the window with any remaining input. */ 1827 have = s->window_size - s->strstart; << 1772 have = s->window_size - s->strstart - 1; 1828 if (s->strm->avail_in > have && s->block_ 1773 if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { 1829 /* Slide the window down. */ 1774 /* Slide the window down. */ 1830 s->block_start -= s->w_size; 1775 s->block_start -= s->w_size; 1831 s->strstart -= s->w_size; 1776 s->strstart -= s->w_size; 1832 zmemcpy(s->window, s->window + s->w_s 1777 zmemcpy(s->window, s->window + s->w_size, s->strstart); 1833 if (s->matches < 2) 1778 if (s->matches < 2) 1834 s->matches++; /* add a 1779 s->matches++; /* add a pending slide_hash() */ 1835 have += s->w_size; /* more s 1780 have += s->w_size; /* more space now */ 1836 if (s->insert > s->strstart) << 1837 s->insert = s->strstart; << 1838 } 1781 } 1839 if (have > s->strm->avail_in) 1782 if (have > s->strm->avail_in) 1840 have = s->strm->avail_in; 1783 have = s->strm->avail_in; 1841 if (have) { 1784 if (have) { 1842 read_buf(s->strm, s->window + s->strs 1785 read_buf(s->strm, s->window + s->strstart, have); 1843 s->strstart += have; 1786 s->strstart += have; 1844 s->insert += MIN(have, s->w_size - s- << 1845 } 1787 } 1846 if (s->high_water < s->strstart) 1788 if (s->high_water < s->strstart) 1847 s->high_water = s->strstart; 1789 s->high_water = s->strstart; 1848 1790 1849 /* There was not enough avail_out to writ 1791 /* There was not enough avail_out to write a complete worthy or flushed 1850 * stored block to next_out. Write a stor 1792 * stored block to next_out. Write a stored block to pending instead, if we 1851 * have enough input for a worthy block, 1793 * have enough input for a worthy block, or if flushing and there is enough 1852 * room for the remaining input as a stor 1794 * room for the remaining input as a stored block in the pending buffer. 1853 */ 1795 */ 1854 have = (s->bi_valid + 42) >> 3; / 1796 have = (s->bi_valid + 42) >> 3; /* number of header bytes */ 1855 /* maximum stored block length that w 1797 /* maximum stored block length that will fit in pending: */ 1856 have = MIN(s->pending_buf_size - have, MA 1798 have = MIN(s->pending_buf_size - have, MAX_STORED); 1857 min_block = MIN(have, s->w_size); 1799 min_block = MIN(have, s->w_size); 1858 left = s->strstart - s->block_start; 1800 left = s->strstart - s->block_start; 1859 if (left >= min_block || 1801 if (left >= min_block || 1860 ((left || flush == Z_FINISH) && flush 1802 ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && 1861 s->strm->avail_in == 0 && left <= ha 1803 s->strm->avail_in == 0 && left <= have)) { 1862 len = MIN(left, have); 1804 len = MIN(left, have); 1863 last = flush == Z_FINISH && s->strm-> 1805 last = flush == Z_FINISH && s->strm->avail_in == 0 && 1864 len == left ? 1 : 0; 1806 len == left ? 1 : 0; 1865 _tr_stored_block(s, (charf *)s->windo 1807 _tr_stored_block(s, (charf *)s->window + s->block_start, len, last); 1866 s->block_start += len; 1808 s->block_start += len; 1867 flush_pending(s->strm); 1809 flush_pending(s->strm); 1868 } 1810 } 1869 1811 1870 /* We've done all we can with the availab 1812 /* We've done all we can with the available input and output. */ 1871 return last ? finish_started : need_more; 1813 return last ? finish_started : need_more; 1872 } 1814 } 1873 1815 1874 /* ========================================== 1816 /* =========================================================================== 1875 * Compress as much as possible from the inpu 1817 * Compress as much as possible from the input stream, return the current 1876 * block state. 1818 * block state. 1877 * This function does not perform lazy evalua 1819 * This function does not perform lazy evaluation of matches and inserts 1878 * new strings in the dictionary only for unm 1820 * new strings in the dictionary only for unmatched strings or for short 1879 * matches. It is used only for the fast comp 1821 * matches. It is used only for the fast compression options. 1880 */ 1822 */ 1881 local block_state deflate_fast(s, flush) 1823 local block_state deflate_fast(s, flush) 1882 deflate_state *s; 1824 deflate_state *s; 1883 int flush; 1825 int flush; 1884 { 1826 { 1885 IPos hash_head; /* head of the hash 1827 IPos hash_head; /* head of the hash chain */ 1886 int bflush; /* set if current b 1828 int bflush; /* set if current block must be flushed */ 1887 1829 1888 for (;;) { 1830 for (;;) { 1889 /* Make sure that we always have enou 1831 /* Make sure that we always have enough lookahead, except 1890 * at the end of the input file. We n 1832 * at the end of the input file. We need MAX_MATCH bytes 1891 * for the next match, plus MIN_MATCH 1833 * for the next match, plus MIN_MATCH bytes to insert the 1892 * string following the next match. 1834 * string following the next match. 1893 */ 1835 */ 1894 if (s->lookahead < MIN_LOOKAHEAD) { 1836 if (s->lookahead < MIN_LOOKAHEAD) { 1895 fill_window(s); 1837 fill_window(s); 1896 if (s->lookahead < MIN_LOOKAHEAD 1838 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1897 return need_more; 1839 return need_more; 1898 } 1840 } 1899 if (s->lookahead == 0) break; /* 1841 if (s->lookahead == 0) break; /* flush the current block */ 1900 } 1842 } 1901 1843 1902 /* Insert the string window[strstart << 1844 /* Insert the string window[strstart .. strstart+2] in the 1903 * dictionary, and set hash_head to t 1845 * dictionary, and set hash_head to the head of the hash chain: 1904 */ 1846 */ 1905 hash_head = NIL; 1847 hash_head = NIL; 1906 if (s->lookahead >= MIN_MATCH) { 1848 if (s->lookahead >= MIN_MATCH) { 1907 INSERT_STRING(s, s->strstart, has 1849 INSERT_STRING(s, s->strstart, hash_head); 1908 } 1850 } 1909 1851 1910 /* Find the longest match, discarding 1852 /* Find the longest match, discarding those <= prev_length. 1911 * At this point we have always match 1853 * At this point we have always match_length < MIN_MATCH 1912 */ 1854 */ 1913 if (hash_head != NIL && s->strstart - 1855 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { 1914 /* To simplify the code, we preve 1856 /* To simplify the code, we prevent matches with the string 1915 * of window index 0 (in particul 1857 * of window index 0 (in particular we have to avoid a match 1916 * of the string with itself at t 1858 * of the string with itself at the start of the input file). 1917 */ 1859 */ 1918 s->match_length = longest_match ( 1860 s->match_length = longest_match (s, hash_head); 1919 /* longest_match() sets match_sta 1861 /* longest_match() sets match_start */ 1920 } 1862 } 1921 if (s->match_length >= MIN_MATCH) { 1863 if (s->match_length >= MIN_MATCH) { 1922 check_match(s, s->strstart, s->ma 1864 check_match(s, s->strstart, s->match_start, s->match_length); 1923 1865 1924 _tr_tally_dist(s, s->strstart - s 1866 _tr_tally_dist(s, s->strstart - s->match_start, 1925 s->match_length - 1867 s->match_length - MIN_MATCH, bflush); 1926 1868 1927 s->lookahead -= s->match_length; 1869 s->lookahead -= s->match_length; 1928 1870 1929 /* Insert new strings in the hash 1871 /* Insert new strings in the hash table only if the match length 1930 * is not too large. This saves t 1872 * is not too large. This saves time but degrades compression. 1931 */ 1873 */ 1932 #ifndef FASTEST 1874 #ifndef FASTEST 1933 if (s->match_length <= s->max_ins 1875 if (s->match_length <= s->max_insert_length && 1934 s->lookahead >= MIN_MATCH) { 1876 s->lookahead >= MIN_MATCH) { 1935 s->match_length--; /* string 1877 s->match_length--; /* string at strstart already in table */ 1936 do { 1878 do { 1937 s->strstart++; 1879 s->strstart++; 1938 INSERT_STRING(s, s->strst 1880 INSERT_STRING(s, s->strstart, hash_head); 1939 /* strstart never exceeds 1881 /* strstart never exceeds WSIZE-MAX_MATCH, so there are 1940 * always MIN_MATCH bytes 1882 * always MIN_MATCH bytes ahead. 1941 */ 1883 */ 1942 } while (--s->match_length != 1884 } while (--s->match_length != 0); 1943 s->strstart++; 1885 s->strstart++; 1944 } else 1886 } else 1945 #endif 1887 #endif 1946 { 1888 { 1947 s->strstart += s->match_lengt 1889 s->strstart += s->match_length; 1948 s->match_length = 0; 1890 s->match_length = 0; 1949 s->ins_h = s->window[s->strst 1891 s->ins_h = s->window[s->strstart]; 1950 UPDATE_HASH(s, s->ins_h, s->w << 1892 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 1951 #if MIN_MATCH != 3 1893 #if MIN_MATCH != 3 1952 Call UPDATE_HASH() MIN_MATCH- 1894 Call UPDATE_HASH() MIN_MATCH-3 more times 1953 #endif 1895 #endif 1954 /* If lookahead < MIN_MATCH, 1896 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not 1955 * matter since it will be re 1897 * matter since it will be recomputed at next deflate call. 1956 */ 1898 */ 1957 } 1899 } 1958 } else { 1900 } else { 1959 /* No match, output a literal byt 1901 /* No match, output a literal byte */ 1960 Tracevv((stderr,"%c", s->window[s 1902 Tracevv((stderr,"%c", s->window[s->strstart])); 1961 _tr_tally_lit(s, s->window[s->str << 1903 _tr_tally_lit (s, s->window[s->strstart], bflush); 1962 s->lookahead--; 1904 s->lookahead--; 1963 s->strstart++; 1905 s->strstart++; 1964 } 1906 } 1965 if (bflush) FLUSH_BLOCK(s, 0); 1907 if (bflush) FLUSH_BLOCK(s, 0); 1966 } 1908 } 1967 s->insert = s->strstart < MIN_MATCH-1 ? s 1909 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 1968 if (flush == Z_FINISH) { 1910 if (flush == Z_FINISH) { 1969 FLUSH_BLOCK(s, 1); 1911 FLUSH_BLOCK(s, 1); 1970 return finish_done; 1912 return finish_done; 1971 } 1913 } 1972 if (s->sym_next) << 1914 if (s->last_lit) 1973 FLUSH_BLOCK(s, 0); 1915 FLUSH_BLOCK(s, 0); 1974 return block_done; 1916 return block_done; 1975 } 1917 } 1976 1918 1977 #ifndef FASTEST 1919 #ifndef FASTEST 1978 /* ========================================== 1920 /* =========================================================================== 1979 * Same as above, but achieves better compres 1921 * Same as above, but achieves better compression. We use a lazy 1980 * evaluation for matches: a match is finally 1922 * evaluation for matches: a match is finally adopted only if there is 1981 * no better match at the next window positio 1923 * no better match at the next window position. 1982 */ 1924 */ 1983 local block_state deflate_slow(s, flush) 1925 local block_state deflate_slow(s, flush) 1984 deflate_state *s; 1926 deflate_state *s; 1985 int flush; 1927 int flush; 1986 { 1928 { 1987 IPos hash_head; /* head of hash 1929 IPos hash_head; /* head of hash chain */ 1988 int bflush; /* set if curren 1930 int bflush; /* set if current block must be flushed */ 1989 1931 1990 /* Process the input block. */ 1932 /* Process the input block. */ 1991 for (;;) { 1933 for (;;) { 1992 /* Make sure that we always have enou 1934 /* Make sure that we always have enough lookahead, except 1993 * at the end of the input file. We n 1935 * at the end of the input file. We need MAX_MATCH bytes 1994 * for the next match, plus MIN_MATCH 1936 * for the next match, plus MIN_MATCH bytes to insert the 1995 * string following the next match. 1937 * string following the next match. 1996 */ 1938 */ 1997 if (s->lookahead < MIN_LOOKAHEAD) { 1939 if (s->lookahead < MIN_LOOKAHEAD) { 1998 fill_window(s); 1940 fill_window(s); 1999 if (s->lookahead < MIN_LOOKAHEAD 1941 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 2000 return need_more; 1942 return need_more; 2001 } 1943 } 2002 if (s->lookahead == 0) break; /* 1944 if (s->lookahead == 0) break; /* flush the current block */ 2003 } 1945 } 2004 1946 2005 /* Insert the string window[strstart << 1947 /* Insert the string window[strstart .. strstart+2] in the 2006 * dictionary, and set hash_head to t 1948 * dictionary, and set hash_head to the head of the hash chain: 2007 */ 1949 */ 2008 hash_head = NIL; 1950 hash_head = NIL; 2009 if (s->lookahead >= MIN_MATCH) { 1951 if (s->lookahead >= MIN_MATCH) { 2010 INSERT_STRING(s, s->strstart, has 1952 INSERT_STRING(s, s->strstart, hash_head); 2011 } 1953 } 2012 1954 2013 /* Find the longest match, discarding 1955 /* Find the longest match, discarding those <= prev_length. 2014 */ 1956 */ 2015 s->prev_length = s->match_length, s-> 1957 s->prev_length = s->match_length, s->prev_match = s->match_start; 2016 s->match_length = MIN_MATCH-1; 1958 s->match_length = MIN_MATCH-1; 2017 1959 2018 if (hash_head != NIL && s->prev_lengt 1960 if (hash_head != NIL && s->prev_length < s->max_lazy_match && 2019 s->strstart - hash_head <= MAX_DI 1961 s->strstart - hash_head <= MAX_DIST(s)) { 2020 /* To simplify the code, we preve 1962 /* To simplify the code, we prevent matches with the string 2021 * of window index 0 (in particul 1963 * of window index 0 (in particular we have to avoid a match 2022 * of the string with itself at t 1964 * of the string with itself at the start of the input file). 2023 */ 1965 */ 2024 s->match_length = longest_match ( 1966 s->match_length = longest_match (s, hash_head); 2025 /* longest_match() sets match_sta 1967 /* longest_match() sets match_start */ 2026 1968 2027 if (s->match_length <= 5 && (s->s 1969 if (s->match_length <= 5 && (s->strategy == Z_FILTERED 2028 #if TOO_FAR <= 32767 1970 #if TOO_FAR <= 32767 2029 || (s->match_length == MIN_MA 1971 || (s->match_length == MIN_MATCH && 2030 s->strstart - s->match_st 1972 s->strstart - s->match_start > TOO_FAR) 2031 #endif 1973 #endif 2032 )) { 1974 )) { 2033 1975 2034 /* If prev_match is also MIN_ 1976 /* If prev_match is also MIN_MATCH, match_start is garbage 2035 * but we will ignore the cur 1977 * but we will ignore the current match anyway. 2036 */ 1978 */ 2037 s->match_length = MIN_MATCH-1 1979 s->match_length = MIN_MATCH-1; 2038 } 1980 } 2039 } 1981 } 2040 /* If there was a match at the previo 1982 /* If there was a match at the previous step and the current 2041 * match is not better, output the pr 1983 * match is not better, output the previous match: 2042 */ 1984 */ 2043 if (s->prev_length >= MIN_MATCH && s- 1985 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { 2044 uInt max_insert = s->strstart + s 1986 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 2045 /* Do not insert strings in hash 1987 /* Do not insert strings in hash table beyond this. */ 2046 1988 2047 check_match(s, s->strstart - 1, s << 1989 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 2048 1990 2049 _tr_tally_dist(s, s->strstart - 1 << 1991 _tr_tally_dist(s, s->strstart -1 - s->prev_match, 2050 s->prev_length - M 1992 s->prev_length - MIN_MATCH, bflush); 2051 1993 2052 /* Insert in hash table all strin 1994 /* Insert in hash table all strings up to the end of the match. 2053 * strstart - 1 and strstart are << 1995 * strstart-1 and strstart are already inserted. If there is not 2054 * enough lookahead, the last two 1996 * enough lookahead, the last two strings are not inserted in 2055 * the hash table. 1997 * the hash table. 2056 */ 1998 */ 2057 s->lookahead -= s->prev_length - << 1999 s->lookahead -= s->prev_length-1; 2058 s->prev_length -= 2; 2000 s->prev_length -= 2; 2059 do { 2001 do { 2060 if (++s->strstart <= max_inse 2002 if (++s->strstart <= max_insert) { 2061 INSERT_STRING(s, s->strst 2003 INSERT_STRING(s, s->strstart, hash_head); 2062 } 2004 } 2063 } while (--s->prev_length != 0); 2005 } while (--s->prev_length != 0); 2064 s->match_available = 0; 2006 s->match_available = 0; 2065 s->match_length = MIN_MATCH-1; 2007 s->match_length = MIN_MATCH-1; 2066 s->strstart++; 2008 s->strstart++; 2067 2009 2068 if (bflush) FLUSH_BLOCK(s, 0); 2010 if (bflush) FLUSH_BLOCK(s, 0); 2069 2011 2070 } else if (s->match_available) { 2012 } else if (s->match_available) { 2071 /* If there was no match at the p 2013 /* If there was no match at the previous position, output a 2072 * single literal. If there was a 2014 * single literal. If there was a match but the current match 2073 * is longer, truncate the previo 2015 * is longer, truncate the previous match to a single literal. 2074 */ 2016 */ 2075 Tracevv((stderr,"%c", s->window[s << 2017 Tracevv((stderr,"%c", s->window[s->strstart-1])); 2076 _tr_tally_lit(s, s->window[s->str << 2018 _tr_tally_lit(s, s->window[s->strstart-1], bflush); 2077 if (bflush) { 2019 if (bflush) { 2078 FLUSH_BLOCK_ONLY(s, 0); 2020 FLUSH_BLOCK_ONLY(s, 0); 2079 } 2021 } 2080 s->strstart++; 2022 s->strstart++; 2081 s->lookahead--; 2023 s->lookahead--; 2082 if (s->strm->avail_out == 0) retu 2024 if (s->strm->avail_out == 0) return need_more; 2083 } else { 2025 } else { 2084 /* There is no previous match to 2026 /* There is no previous match to compare with, wait for 2085 * the next step to decide. 2027 * the next step to decide. 2086 */ 2028 */ 2087 s->match_available = 1; 2029 s->match_available = 1; 2088 s->strstart++; 2030 s->strstart++; 2089 s->lookahead--; 2031 s->lookahead--; 2090 } 2032 } 2091 } 2033 } 2092 Assert (flush != Z_NO_FLUSH, "no flush?") 2034 Assert (flush != Z_NO_FLUSH, "no flush?"); 2093 if (s->match_available) { 2035 if (s->match_available) { 2094 Tracevv((stderr,"%c", s->window[s->st << 2036 Tracevv((stderr,"%c", s->window[s->strstart-1])); 2095 _tr_tally_lit(s, s->window[s->strstar << 2037 _tr_tally_lit(s, s->window[s->strstart-1], bflush); 2096 s->match_available = 0; 2038 s->match_available = 0; 2097 } 2039 } 2098 s->insert = s->strstart < MIN_MATCH-1 ? s 2040 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 2099 if (flush == Z_FINISH) { 2041 if (flush == Z_FINISH) { 2100 FLUSH_BLOCK(s, 1); 2042 FLUSH_BLOCK(s, 1); 2101 return finish_done; 2043 return finish_done; 2102 } 2044 } 2103 if (s->sym_next) << 2045 if (s->last_lit) 2104 FLUSH_BLOCK(s, 0); 2046 FLUSH_BLOCK(s, 0); 2105 return block_done; 2047 return block_done; 2106 } 2048 } 2107 #endif /* FASTEST */ 2049 #endif /* FASTEST */ 2108 2050 2109 /* ========================================== 2051 /* =========================================================================== 2110 * For Z_RLE, simply look for runs of bytes, 2052 * For Z_RLE, simply look for runs of bytes, generate matches only of distance 2111 * one. Do not maintain a hash table. (It w 2053 * one. Do not maintain a hash table. (It will be regenerated if this run of 2112 * deflate switches away from Z_RLE.) 2054 * deflate switches away from Z_RLE.) 2113 */ 2055 */ 2114 local block_state deflate_rle(s, flush) 2056 local block_state deflate_rle(s, flush) 2115 deflate_state *s; 2057 deflate_state *s; 2116 int flush; 2058 int flush; 2117 { 2059 { 2118 int bflush; /* set if current 2060 int bflush; /* set if current block must be flushed */ 2119 uInt prev; /* byte at distan 2061 uInt prev; /* byte at distance one to match */ 2120 Bytef *scan, *strend; /* scan goes up t 2062 Bytef *scan, *strend; /* scan goes up to strend for length of run */ 2121 2063 2122 for (;;) { 2064 for (;;) { 2123 /* Make sure that we always have enou 2065 /* Make sure that we always have enough lookahead, except 2124 * at the end of the input file. We n 2066 * at the end of the input file. We need MAX_MATCH bytes 2125 * for the longest run, plus one for 2067 * for the longest run, plus one for the unrolled loop. 2126 */ 2068 */ 2127 if (s->lookahead <= MAX_MATCH) { 2069 if (s->lookahead <= MAX_MATCH) { 2128 fill_window(s); 2070 fill_window(s); 2129 if (s->lookahead <= MAX_MATCH && 2071 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { 2130 return need_more; 2072 return need_more; 2131 } 2073 } 2132 if (s->lookahead == 0) break; /* 2074 if (s->lookahead == 0) break; /* flush the current block */ 2133 } 2075 } 2134 2076 2135 /* See how many times the previous by 2077 /* See how many times the previous byte repeats */ 2136 s->match_length = 0; 2078 s->match_length = 0; 2137 if (s->lookahead >= MIN_MATCH && s->s 2079 if (s->lookahead >= MIN_MATCH && s->strstart > 0) { 2138 scan = s->window + s->strstart - 2080 scan = s->window + s->strstart - 1; 2139 prev = *scan; 2081 prev = *scan; 2140 if (prev == *++scan && prev == *+ 2082 if (prev == *++scan && prev == *++scan && prev == *++scan) { 2141 strend = s->window + s->strst 2083 strend = s->window + s->strstart + MAX_MATCH; 2142 do { 2084 do { 2143 } while (prev == *++scan && p 2085 } while (prev == *++scan && prev == *++scan && 2144 prev == *++scan && p 2086 prev == *++scan && prev == *++scan && 2145 prev == *++scan && p 2087 prev == *++scan && prev == *++scan && 2146 prev == *++scan && p 2088 prev == *++scan && prev == *++scan && 2147 scan < strend); 2089 scan < strend); 2148 s->match_length = MAX_MATCH - 2090 s->match_length = MAX_MATCH - (uInt)(strend - scan); 2149 if (s->match_length > s->look 2091 if (s->match_length > s->lookahead) 2150 s->match_length = s->look 2092 s->match_length = s->lookahead; 2151 } 2093 } 2152 Assert(scan <= s->window + (uInt) << 2094 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); 2153 "wild scan"); << 2154 } 2095 } 2155 2096 2156 /* Emit match if have run of MIN_MATC 2097 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ 2157 if (s->match_length >= MIN_MATCH) { 2098 if (s->match_length >= MIN_MATCH) { 2158 check_match(s, s->strstart, s->st 2099 check_match(s, s->strstart, s->strstart - 1, s->match_length); 2159 2100 2160 _tr_tally_dist(s, 1, s->match_len 2101 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); 2161 2102 2162 s->lookahead -= s->match_length; 2103 s->lookahead -= s->match_length; 2163 s->strstart += s->match_length; 2104 s->strstart += s->match_length; 2164 s->match_length = 0; 2105 s->match_length = 0; 2165 } else { 2106 } else { 2166 /* No match, output a literal byt 2107 /* No match, output a literal byte */ 2167 Tracevv((stderr,"%c", s->window[s 2108 Tracevv((stderr,"%c", s->window[s->strstart])); 2168 _tr_tally_lit(s, s->window[s->str << 2109 _tr_tally_lit (s, s->window[s->strstart], bflush); 2169 s->lookahead--; 2110 s->lookahead--; 2170 s->strstart++; 2111 s->strstart++; 2171 } 2112 } 2172 if (bflush) FLUSH_BLOCK(s, 0); 2113 if (bflush) FLUSH_BLOCK(s, 0); 2173 } 2114 } 2174 s->insert = 0; 2115 s->insert = 0; 2175 if (flush == Z_FINISH) { 2116 if (flush == Z_FINISH) { 2176 FLUSH_BLOCK(s, 1); 2117 FLUSH_BLOCK(s, 1); 2177 return finish_done; 2118 return finish_done; 2178 } 2119 } 2179 if (s->sym_next) << 2120 if (s->last_lit) 2180 FLUSH_BLOCK(s, 0); 2121 FLUSH_BLOCK(s, 0); 2181 return block_done; 2122 return block_done; 2182 } 2123 } 2183 2124 2184 /* ========================================== 2125 /* =========================================================================== 2185 * For Z_HUFFMAN_ONLY, do not look for matche 2126 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. 2186 * (It will be regenerated if this run of def 2127 * (It will be regenerated if this run of deflate switches away from Huffman.) 2187 */ 2128 */ 2188 local block_state deflate_huff(s, flush) 2129 local block_state deflate_huff(s, flush) 2189 deflate_state *s; 2130 deflate_state *s; 2190 int flush; 2131 int flush; 2191 { 2132 { 2192 int bflush; /* set if current 2133 int bflush; /* set if current block must be flushed */ 2193 2134 2194 for (;;) { 2135 for (;;) { 2195 /* Make sure that we have a literal t 2136 /* Make sure that we have a literal to write. */ 2196 if (s->lookahead == 0) { 2137 if (s->lookahead == 0) { 2197 fill_window(s); 2138 fill_window(s); 2198 if (s->lookahead == 0) { 2139 if (s->lookahead == 0) { 2199 if (flush == Z_NO_FLUSH) 2140 if (flush == Z_NO_FLUSH) 2200 return need_more; 2141 return need_more; 2201 break; /* flush the curr 2142 break; /* flush the current block */ 2202 } 2143 } 2203 } 2144 } 2204 2145 2205 /* Output a literal byte */ 2146 /* Output a literal byte */ 2206 s->match_length = 0; 2147 s->match_length = 0; 2207 Tracevv((stderr,"%c", s->window[s->st 2148 Tracevv((stderr,"%c", s->window[s->strstart])); 2208 _tr_tally_lit(s, s->window[s->strstar << 2149 _tr_tally_lit (s, s->window[s->strstart], bflush); 2209 s->lookahead--; 2150 s->lookahead--; 2210 s->strstart++; 2151 s->strstart++; 2211 if (bflush) FLUSH_BLOCK(s, 0); 2152 if (bflush) FLUSH_BLOCK(s, 0); 2212 } 2153 } 2213 s->insert = 0; 2154 s->insert = 0; 2214 if (flush == Z_FINISH) { 2155 if (flush == Z_FINISH) { 2215 FLUSH_BLOCK(s, 1); 2156 FLUSH_BLOCK(s, 1); 2216 return finish_done; 2157 return finish_done; 2217 } 2158 } 2218 if (s->sym_next) << 2159 if (s->last_lit) 2219 FLUSH_BLOCK(s, 0); 2160 FLUSH_BLOCK(s, 0); 2220 return block_done; 2161 return block_done; 2221 } 2162 } 2222 2163