Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 1 | /* deflate.c -- compress data using the deflation algorithm |
| 2 | * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler |
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * ALGORITHM |
| 8 | * |
| 9 | * The "deflation" process depends on being able to identify portions |
| 10 | * of the input text which are identical to earlier input (within a |
| 11 | * sliding window trailing behind the input currently being processed). |
| 12 | * |
| 13 | * The most straightforward technique turns out to be the fastest for |
| 14 | * most input files: try all possible matches and select the longest. |
| 15 | * The key feature of this algorithm is that insertions into the string |
| 16 | * dictionary are very simple and thus fast, and deletions are avoided |
| 17 | * completely. Insertions are performed at each input character, whereas |
| 18 | * string matches are performed only when the previous match ends. So it |
| 19 | * is preferable to spend more time in matches to allow very fast string |
| 20 | * insertions and avoid deletions. The matching algorithm for small |
| 21 | * strings is inspired from that of Rabin & Karp. A brute force approach |
| 22 | * is used to find longer strings when a small match has been found. |
| 23 | * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze |
| 24 | * (by Leonid Broukhis). |
| 25 | * A previous version of this file used a more sophisticated algorithm |
| 26 | * (by Fiala and Greene) which is guaranteed to run in linear amortized |
| 27 | * time, but has a larger average cost, uses more memory and is patented. |
| 28 | * However the F&G algorithm may be faster for some highly redundant |
| 29 | * files if the parameter max_chain_length (described below) is too large. |
| 30 | * |
| 31 | * ACKNOWLEDGEMENTS |
| 32 | * |
| 33 | * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and |
| 34 | * I found it in 'freeze' written by Leonid Broukhis. |
| 35 | * Thanks to many people for bug reports and testing. |
| 36 | * |
| 37 | * REFERENCES |
| 38 | * |
| 39 | * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". |
| 40 | * Available in http://www.ietf.org/rfc/rfc1951.txt |
| 41 | * |
| 42 | * A description of the Rabin and Karp algorithm is given in the book |
| 43 | * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. |
| 44 | * |
| 45 | * Fiala,E.R., and Greene,D.H. |
| 46 | * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 |
| 47 | * |
| 48 | */ |
| 49 | |
| 50 | /* @(#) $Id$ */ |
| 51 | |
| 52 | #include "deflate.h" |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 53 | #include <u-boot/crc.h> |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 54 | |
| 55 | const char deflate_copyright[] = |
| 56 | " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler "; |
| 57 | /* |
| 58 | If you use the zlib library in a product, an acknowledgment is welcome |
| 59 | in the documentation of your product. If for some reason you cannot |
| 60 | include such an acknowledgment, I would appreciate that you keep this |
| 61 | copyright string in the executable of your product. |
| 62 | */ |
| 63 | |
| 64 | /* =========================================================================== |
| 65 | * Function prototypes. |
| 66 | */ |
| 67 | typedef enum { |
| 68 | need_more, /* block not completed, need more input or more output */ |
| 69 | block_done, /* block flush performed */ |
| 70 | finish_started, /* finish started, need only more output at next deflate */ |
| 71 | finish_done /* finish done, accept no more input or output */ |
| 72 | } block_state; |
| 73 | |
| 74 | typedef block_state (*compress_func) OF((deflate_state *s, int flush)); |
| 75 | /* Compression function. Returns the block state after the call. */ |
| 76 | |
| 77 | local void fill_window OF((deflate_state *s)); |
| 78 | local block_state deflate_stored OF((deflate_state *s, int flush)); |
| 79 | local block_state deflate_fast OF((deflate_state *s, int flush)); |
| 80 | #ifndef FASTEST |
| 81 | local block_state deflate_slow OF((deflate_state *s, int flush)); |
| 82 | #endif |
| 83 | local block_state deflate_rle OF((deflate_state *s, int flush)); |
| 84 | local block_state deflate_huff OF((deflate_state *s, int flush)); |
| 85 | local void lm_init OF((deflate_state *s)); |
| 86 | local void putShortMSB OF((deflate_state *s, uInt b)); |
| 87 | local void flush_pending OF((z_streamp strm)); |
| 88 | local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); |
| 89 | #ifdef ASMV |
| 90 | void match_init OF((void)); /* asm code initialization */ |
| 91 | uInt longest_match OF((deflate_state *s, IPos cur_match)); |
| 92 | #else |
| 93 | local uInt longest_match OF((deflate_state *s, IPos cur_match)); |
| 94 | #endif |
| 95 | |
| 96 | #ifdef DEBUG |
| 97 | local void check_match OF((deflate_state *s, IPos start, IPos match, |
| 98 | int length)); |
| 99 | #endif |
| 100 | |
| 101 | /* =========================================================================== |
| 102 | * Local data |
| 103 | */ |
| 104 | |
| 105 | #define NIL 0 |
| 106 | /* Tail of hash chains */ |
| 107 | |
| 108 | #ifndef TOO_FAR |
| 109 | # define TOO_FAR 4096 |
| 110 | #endif |
| 111 | /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ |
| 112 | |
| 113 | /* Values for max_lazy_match, good_match and max_chain_length, depending on |
| 114 | * the desired pack level (0..9). The values given below have been tuned to |
| 115 | * exclude worst case performance for pathological files. Better values may be |
| 116 | * found for specific files. |
| 117 | */ |
| 118 | typedef struct config_s { |
| 119 | ush good_length; /* reduce lazy search above this match length */ |
| 120 | ush max_lazy; /* do not perform lazy search above this match length */ |
| 121 | ush nice_length; /* quit search above this match length */ |
| 122 | ush max_chain; |
| 123 | compress_func func; |
| 124 | } config; |
| 125 | |
| 126 | #ifdef FASTEST |
| 127 | local const config configuration_table[2] = { |
| 128 | /* good lazy nice chain */ |
| 129 | /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ |
| 130 | /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ |
| 131 | #else |
| 132 | local const config configuration_table[10] = { |
| 133 | /* good lazy nice chain */ |
| 134 | /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ |
| 135 | /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ |
| 136 | /* 2 */ {4, 5, 16, 8, deflate_fast}, |
| 137 | /* 3 */ {4, 6, 32, 32, deflate_fast}, |
| 138 | |
| 139 | /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ |
| 140 | /* 5 */ {8, 16, 32, 32, deflate_slow}, |
| 141 | /* 6 */ {8, 16, 128, 128, deflate_slow}, |
| 142 | /* 7 */ {8, 32, 128, 256, deflate_slow}, |
| 143 | /* 8 */ {32, 128, 258, 1024, deflate_slow}, |
| 144 | /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ |
| 145 | #endif |
| 146 | |
| 147 | /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 |
| 148 | * For deflate_fast() (levels <= 3) good is ignored and lazy has a different |
| 149 | * meaning. |
| 150 | */ |
| 151 | |
| 152 | #define EQUAL 0 |
| 153 | /* result of memcmp for equal strings */ |
| 154 | |
| 155 | #ifndef NO_DUMMY_DECL |
| 156 | struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ |
| 157 | #endif |
| 158 | |
| 159 | /* =========================================================================== |
| 160 | * Update a hash value with the given input byte |
| 161 | * IN assertion: all calls to to UPDATE_HASH are made with consecutive |
| 162 | * input characters, so that a running hash key can be computed from the |
| 163 | * previous key instead of complete recalculation each time. |
| 164 | */ |
| 165 | #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) |
| 166 | |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 167 | /* =========================================================================== |
| 168 | * Insert string str in the dictionary and set match_head to the previous head |
| 169 | * of the hash chain (the most recent string with same hash key). Return |
| 170 | * the previous length of the hash chain. |
| 171 | * If this file is compiled with -DFASTEST, the compression level is forced |
| 172 | * to 1, and no hash chains are maintained. |
| 173 | * IN assertion: all calls to to INSERT_STRING are made with consecutive |
| 174 | * input characters and the first MIN_MATCH bytes of str are valid |
| 175 | * (except for the last MIN_MATCH-1 bytes of the input file). |
| 176 | */ |
| 177 | #ifdef FASTEST |
| 178 | #define INSERT_STRING(s, str, match_head) \ |
| 179 | (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ |
| 180 | match_head = s->head[s->ins_h], \ |
| 181 | s->head[s->ins_h] = (Pos)(str)) |
| 182 | #else |
| 183 | #define INSERT_STRING(s, str, match_head) \ |
| 184 | (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ |
| 185 | match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ |
| 186 | s->head[s->ins_h] = (Pos)(str)) |
| 187 | #endif |
| 188 | |
| 189 | /* =========================================================================== |
| 190 | * Initialize the hash table (avoiding 64K overflow for 16 bit systems). |
| 191 | * prev[] will be initialized on the fly. |
| 192 | */ |
| 193 | #define CLEAR_HASH(s) \ |
| 194 | s->head[s->hash_size-1] = NIL; \ |
| 195 | zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); |
| 196 | |
| 197 | /* ========================================================================= */ |
Tom Rini | 9796bdd | 2024-07-05 09:44:54 -0600 | [diff] [blame] | 198 | int ZEXPORT deflateInit_(strm, level, stream_size) |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 199 | z_streamp strm; |
| 200 | int level; |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 201 | int stream_size; |
| 202 | { |
| 203 | return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, |
Tom Rini | 9796bdd | 2024-07-05 09:44:54 -0600 | [diff] [blame] | 204 | Z_DEFAULT_STRATEGY, stream_size); |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 205 | /* To do: ignore strm->next_in if we use it as window */ |
| 206 | } |
| 207 | |
| 208 | /* ========================================================================= */ |
| 209 | int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, |
Tom Rini | 9796bdd | 2024-07-05 09:44:54 -0600 | [diff] [blame] | 210 | stream_size) |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 211 | z_streamp strm; |
| 212 | int level; |
| 213 | int method; |
| 214 | int windowBits; |
| 215 | int memLevel; |
| 216 | int strategy; |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 217 | int stream_size; |
| 218 | { |
| 219 | deflate_state *s; |
| 220 | int wrap = 1; |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 221 | |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 222 | if (strm == Z_NULL) return Z_STREAM_ERROR; |
| 223 | |
| 224 | strm->msg = Z_NULL; |
| 225 | if (strm->zalloc == (alloc_func)0) { |
| 226 | strm->zalloc = zcalloc; |
| 227 | strm->opaque = (voidpf)0; |
| 228 | } |
| 229 | if (strm->zfree == (free_func)0) strm->zfree = zcfree; |
| 230 | |
| 231 | #ifdef FASTEST |
| 232 | if (level != 0) level = 1; |
| 233 | #else |
| 234 | if (level == Z_DEFAULT_COMPRESSION) level = 6; |
| 235 | #endif |
| 236 | |
| 237 | if (windowBits < 0) { /* suppress zlib wrapper */ |
| 238 | wrap = 0; |
| 239 | windowBits = -windowBits; |
| 240 | } |
| 241 | #ifdef GZIP |
| 242 | else if (windowBits > 15) { |
| 243 | wrap = 2; /* write gzip wrapper instead */ |
| 244 | windowBits -= 16; |
| 245 | } |
| 246 | #endif |
| 247 | if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || |
| 248 | windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || |
| 249 | strategy < 0 || strategy > Z_FIXED) { |
| 250 | return Z_STREAM_ERROR; |
| 251 | } |
| 252 | if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ |
| 253 | s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); |
| 254 | if (s == Z_NULL) return Z_MEM_ERROR; |
| 255 | strm->state = (struct internal_state FAR *)s; |
| 256 | s->strm = strm; |
| 257 | |
| 258 | s->wrap = wrap; |
| 259 | s->gzhead = Z_NULL; |
| 260 | s->w_bits = windowBits; |
| 261 | s->w_size = 1 << s->w_bits; |
| 262 | s->w_mask = s->w_size - 1; |
| 263 | |
| 264 | s->hash_bits = memLevel + 7; |
| 265 | s->hash_size = 1 << s->hash_bits; |
| 266 | s->hash_mask = s->hash_size - 1; |
| 267 | s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); |
| 268 | |
| 269 | s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); |
| 270 | s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); |
| 271 | s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); |
| 272 | |
| 273 | s->high_water = 0; /* nothing written to s->window yet */ |
| 274 | |
| 275 | s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ |
| 276 | |
Tom Rini | c3411d4 | 2022-05-10 14:36:59 -0400 | [diff] [blame] | 277 | /* We overlay pending_buf and sym_buf. This works since the average size |
| 278 | * for length/distance pairs over any compressed block is assured to be 31 |
| 279 | * bits or less. |
| 280 | * |
| 281 | * Analysis: The longest fixed codes are a length code of 8 bits plus 5 |
| 282 | * extra bits, for lengths 131 to 257. The longest fixed distance codes are |
| 283 | * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest |
| 284 | * possible fixed-codes length/distance pair is then 31 bits total. |
| 285 | * |
| 286 | * sym_buf starts one-fourth of the way into pending_buf. So there are |
| 287 | * three bytes in sym_buf for every four bytes in pending_buf. Each symbol |
| 288 | * in sym_buf is three bytes -- two for the distance and one for the |
| 289 | * literal/length. As each symbol is consumed, the pointer to the next |
| 290 | * sym_buf value to read moves forward three bytes. From that symbol, up to |
| 291 | * 31 bits are written to pending_buf. The closest the written pending_buf |
| 292 | * bits gets to the next sym_buf symbol to read is just before the last |
| 293 | * code is written. At that time, 31*(n-2) bits have been written, just |
| 294 | * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at |
| 295 | * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1 |
| 296 | * symbols are written.) The closest the writing gets to what is unread is |
| 297 | * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and |
| 298 | * can range from 128 to 32768. |
| 299 | * |
| 300 | * Therefore, at a minimum, there are 142 bits of space between what is |
| 301 | * written and what is read in the overlain buffers, so the symbols cannot |
| 302 | * be overwritten by the compressed data. That space is actually 139 bits, |
| 303 | * due to the three-bit fixed-code block header. |
| 304 | * |
| 305 | * That covers the case where either Z_FIXED is specified, forcing fixed |
| 306 | * codes, or when the use of fixed codes is chosen, because that choice |
| 307 | * results in a smaller compressed block than dynamic codes. That latter |
| 308 | * condition then assures that the above analysis also covers all dynamic |
| 309 | * blocks. A dynamic-code block will only be chosen to be emitted if it has |
| 310 | * fewer bits than a fixed-code block would for the same set of symbols. |
| 311 | * Therefore its average symbol length is assured to be less than 31. So |
| 312 | * the compressed data for a dynamic block also cannot overwrite the |
| 313 | * symbols from which it is being constructed. |
| 314 | */ |
| 315 | |
| 316 | s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4); |
| 317 | s->pending_buf_size = (ulg)s->lit_bufsize * 4; |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 318 | |
| 319 | if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || |
| 320 | s->pending_buf == Z_NULL) { |
| 321 | s->status = FINISH_STATE; |
| 322 | strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); |
| 323 | deflateEnd (strm); |
| 324 | return Z_MEM_ERROR; |
| 325 | } |
Tom Rini | c3411d4 | 2022-05-10 14:36:59 -0400 | [diff] [blame] | 326 | s->sym_buf = s->pending_buf + s->lit_bufsize; |
| 327 | s->sym_end = (s->lit_bufsize - 1) * 3; |
| 328 | /* We avoid equality with lit_bufsize*3 because of wraparound at 64K |
| 329 | * on 16 bit machines and because stored blocks are restricted to |
| 330 | * 64K-1 bytes. |
| 331 | */ |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 332 | |
| 333 | s->level = level; |
| 334 | s->strategy = strategy; |
| 335 | s->method = (Byte)method; |
| 336 | |
| 337 | return deflateReset(strm); |
| 338 | } |
| 339 | |
| 340 | /* ========================================================================= */ |
| 341 | int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) |
| 342 | z_streamp strm; |
| 343 | const Bytef *dictionary; |
| 344 | uInt dictLength; |
| 345 | { |
| 346 | deflate_state *s; |
| 347 | uInt length = dictLength; |
| 348 | uInt n; |
| 349 | IPos hash_head = 0; |
| 350 | |
| 351 | if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || |
| 352 | strm->state->wrap == 2 || |
| 353 | (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) |
| 354 | return Z_STREAM_ERROR; |
| 355 | |
| 356 | s = strm->state; |
| 357 | if (s->wrap) |
| 358 | strm->adler = adler32(strm->adler, dictionary, dictLength); |
| 359 | |
| 360 | if (length < MIN_MATCH) return Z_OK; |
| 361 | if (length > s->w_size) { |
| 362 | length = s->w_size; |
| 363 | dictionary += dictLength - length; /* use the tail of the dictionary */ |
| 364 | } |
| 365 | zmemcpy(s->window, dictionary, length); |
| 366 | s->strstart = length; |
| 367 | s->block_start = (long)length; |
| 368 | |
| 369 | /* Insert all strings in the hash table (except for the last two bytes). |
| 370 | * s->lookahead stays null, so s->ins_h will be recomputed at the next |
| 371 | * call of fill_window. |
| 372 | */ |
| 373 | s->ins_h = s->window[0]; |
| 374 | UPDATE_HASH(s, s->ins_h, s->window[1]); |
| 375 | for (n = 0; n <= length - MIN_MATCH; n++) { |
| 376 | INSERT_STRING(s, n, hash_head); |
| 377 | } |
| 378 | if (hash_head) hash_head = 0; /* to make compiler happy */ |
| 379 | return Z_OK; |
| 380 | } |
| 381 | |
| 382 | /* ========================================================================= */ |
| 383 | int ZEXPORT deflateReset (strm) |
| 384 | z_streamp strm; |
| 385 | { |
| 386 | deflate_state *s; |
| 387 | |
| 388 | if (strm == Z_NULL || strm->state == Z_NULL || |
| 389 | strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { |
| 390 | return Z_STREAM_ERROR; |
| 391 | } |
| 392 | |
| 393 | strm->total_in = strm->total_out = 0; |
| 394 | strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ |
| 395 | strm->data_type = Z_UNKNOWN; |
| 396 | |
| 397 | s = (deflate_state *)strm->state; |
| 398 | s->pending = 0; |
| 399 | s->pending_out = s->pending_buf; |
| 400 | |
| 401 | if (s->wrap < 0) { |
| 402 | s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ |
| 403 | } |
| 404 | s->status = s->wrap ? INIT_STATE : BUSY_STATE; |
| 405 | strm->adler = |
| 406 | #ifdef GZIP |
| 407 | s->wrap == 2 ? crc32(0L, Z_NULL, 0) : |
| 408 | #endif |
| 409 | adler32(0L, Z_NULL, 0); |
| 410 | s->last_flush = Z_NO_FLUSH; |
| 411 | |
| 412 | _tr_init(s); |
| 413 | lm_init(s); |
| 414 | |
| 415 | return Z_OK; |
| 416 | } |
| 417 | |
| 418 | /* ========================================================================= */ |
| 419 | int ZEXPORT deflateSetHeader (strm, head) |
| 420 | z_streamp strm; |
| 421 | gz_headerp head; |
| 422 | { |
| 423 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 424 | if (strm->state->wrap != 2) return Z_STREAM_ERROR; |
| 425 | strm->state->gzhead = head; |
| 426 | return Z_OK; |
| 427 | } |
| 428 | |
| 429 | /* ========================================================================= */ |
| 430 | int ZEXPORT deflatePrime (strm, bits, value) |
| 431 | z_streamp strm; |
| 432 | int bits; |
| 433 | int value; |
| 434 | { |
| 435 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 436 | strm->state->bi_valid = bits; |
| 437 | strm->state->bi_buf = (ush)(value & ((1 << bits) - 1)); |
| 438 | return Z_OK; |
| 439 | } |
| 440 | |
| 441 | /* ========================================================================= */ |
| 442 | int ZEXPORT deflateParams(strm, level, strategy) |
| 443 | z_streamp strm; |
| 444 | int level; |
| 445 | int strategy; |
| 446 | { |
| 447 | deflate_state *s; |
| 448 | compress_func func; |
| 449 | int err = Z_OK; |
| 450 | |
| 451 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 452 | s = strm->state; |
| 453 | |
| 454 | #ifdef FASTEST |
| 455 | if (level != 0) level = 1; |
| 456 | #else |
| 457 | if (level == Z_DEFAULT_COMPRESSION) level = 6; |
| 458 | #endif |
| 459 | if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { |
| 460 | return Z_STREAM_ERROR; |
| 461 | } |
| 462 | func = configuration_table[s->level].func; |
| 463 | |
| 464 | if ((strategy != s->strategy || func != configuration_table[level].func) && |
| 465 | strm->total_in != 0) { |
| 466 | /* Flush the last buffer: */ |
| 467 | err = deflate(strm, Z_BLOCK); |
| 468 | } |
| 469 | if (s->level != level) { |
| 470 | s->level = level; |
| 471 | s->max_lazy_match = configuration_table[level].max_lazy; |
| 472 | s->good_match = configuration_table[level].good_length; |
| 473 | s->nice_match = configuration_table[level].nice_length; |
| 474 | s->max_chain_length = configuration_table[level].max_chain; |
| 475 | } |
| 476 | s->strategy = strategy; |
| 477 | return err; |
| 478 | } |
| 479 | |
| 480 | /* ========================================================================= */ |
| 481 | int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) |
| 482 | z_streamp strm; |
| 483 | int good_length; |
| 484 | int max_lazy; |
| 485 | int nice_length; |
| 486 | int max_chain; |
| 487 | { |
| 488 | deflate_state *s; |
| 489 | |
| 490 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 491 | s = strm->state; |
| 492 | s->good_match = good_length; |
| 493 | s->max_lazy_match = max_lazy; |
| 494 | s->nice_match = nice_length; |
| 495 | s->max_chain_length = max_chain; |
| 496 | return Z_OK; |
| 497 | } |
| 498 | |
| 499 | /* ========================================================================= |
| 500 | * For the default windowBits of 15 and memLevel of 8, this function returns |
| 501 | * a close to exact, as well as small, upper bound on the compressed size. |
| 502 | * They are coded as constants here for a reason--if the #define's are |
| 503 | * changed, then this function needs to be changed as well. The return |
| 504 | * value for 15 and 8 only works for those exact settings. |
| 505 | * |
| 506 | * For any setting other than those defaults for windowBits and memLevel, |
| 507 | * the value returned is a conservative worst case for the maximum expansion |
| 508 | * resulting from using fixed blocks instead of stored blocks, which deflate |
| 509 | * can emit on compressed data for some combinations of the parameters. |
| 510 | * |
| 511 | * This function could be more sophisticated to provide closer upper bounds for |
| 512 | * every combination of windowBits and memLevel. But even the conservative |
| 513 | * upper bound of about 14% expansion does not seem onerous for output buffer |
| 514 | * allocation. |
| 515 | */ |
| 516 | uLong ZEXPORT deflateBound(strm, sourceLen) |
| 517 | z_streamp strm; |
| 518 | uLong sourceLen; |
| 519 | { |
| 520 | deflate_state *s; |
| 521 | uLong complen, wraplen; |
| 522 | Bytef *str; |
| 523 | |
| 524 | /* conservative upper bound for compressed data */ |
| 525 | complen = sourceLen + |
| 526 | ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; |
| 527 | |
| 528 | /* if can't get parameters, return conservative bound plus zlib wrapper */ |
| 529 | if (strm == Z_NULL || strm->state == Z_NULL) |
| 530 | return complen + 6; |
| 531 | |
| 532 | /* compute wrapper length */ |
| 533 | s = strm->state; |
| 534 | switch (s->wrap) { |
| 535 | case 0: /* raw deflate */ |
| 536 | wraplen = 0; |
| 537 | break; |
| 538 | case 1: /* zlib wrapper */ |
| 539 | wraplen = 6 + (s->strstart ? 4 : 0); |
| 540 | break; |
| 541 | case 2: /* gzip wrapper */ |
| 542 | wraplen = 18; |
| 543 | if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ |
| 544 | if (s->gzhead->extra != Z_NULL) |
| 545 | wraplen += 2 + s->gzhead->extra_len; |
| 546 | str = s->gzhead->name; |
| 547 | if (str != Z_NULL) |
| 548 | do { |
| 549 | wraplen++; |
| 550 | } while (*str++); |
| 551 | str = s->gzhead->comment; |
| 552 | if (str != Z_NULL) |
| 553 | do { |
| 554 | wraplen++; |
| 555 | } while (*str++); |
| 556 | if (s->gzhead->hcrc) |
| 557 | wraplen += 2; |
| 558 | } |
| 559 | break; |
| 560 | default: /* for compiler happiness */ |
| 561 | wraplen = 6; |
| 562 | } |
| 563 | |
| 564 | /* if not default parameters, return conservative bound */ |
| 565 | if (s->w_bits != 15 || s->hash_bits != 8 + 7) |
| 566 | return complen + wraplen; |
| 567 | |
| 568 | /* default settings: return tight bound for that case */ |
| 569 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + |
| 570 | (sourceLen >> 25) + 13 - 6 + wraplen; |
| 571 | } |
| 572 | |
| 573 | /* ========================================================================= |
| 574 | * Put a short in the pending buffer. The 16-bit value is put in MSB order. |
| 575 | * IN assertion: the stream state is correct and there is enough room in |
| 576 | * pending_buf. |
| 577 | */ |
| 578 | local void putShortMSB (s, b) |
| 579 | deflate_state *s; |
| 580 | uInt b; |
| 581 | { |
| 582 | put_byte(s, (Byte)(b >> 8)); |
| 583 | put_byte(s, (Byte)(b & 0xff)); |
| 584 | } |
| 585 | |
| 586 | /* ========================================================================= |
| 587 | * Flush as much pending output as possible. All deflate() output goes |
| 588 | * through this function so some applications may wish to modify it |
| 589 | * to avoid allocating a large strm->next_out buffer and copying into it. |
| 590 | * (See also read_buf()). |
| 591 | */ |
| 592 | local void flush_pending(strm) |
| 593 | z_streamp strm; |
| 594 | { |
| 595 | unsigned len = strm->state->pending; |
| 596 | |
| 597 | if (len > strm->avail_out) len = strm->avail_out; |
| 598 | if (len == 0) return; |
| 599 | |
| 600 | zmemcpy(strm->next_out, strm->state->pending_out, len); |
| 601 | strm->next_out += len; |
| 602 | strm->state->pending_out += len; |
| 603 | strm->total_out += len; |
| 604 | strm->avail_out -= len; |
| 605 | strm->state->pending -= len; |
| 606 | if (strm->state->pending == 0) { |
| 607 | strm->state->pending_out = strm->state->pending_buf; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* ========================================================================= */ |
| 612 | int ZEXPORT deflate (strm, flush) |
| 613 | z_streamp strm; |
| 614 | int flush; |
| 615 | { |
| 616 | int old_flush; /* value of flush param for previous deflate call */ |
| 617 | deflate_state *s; |
| 618 | |
| 619 | if (strm == Z_NULL || strm->state == Z_NULL || |
| 620 | flush > Z_BLOCK || flush < 0) { |
| 621 | return Z_STREAM_ERROR; |
| 622 | } |
| 623 | s = strm->state; |
| 624 | |
Lei Wen | 86a3628 | 2012-09-28 04:26:45 +0000 | [diff] [blame] | 625 | if (s->status == FINISH_STATE && flush != Z_FINISH) { |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 626 | ERR_RETURN(strm, Z_STREAM_ERROR); |
| 627 | } |
| 628 | if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); |
| 629 | |
| 630 | s->strm = strm; /* just in case */ |
| 631 | old_flush = s->last_flush; |
| 632 | s->last_flush = flush; |
| 633 | |
| 634 | /* Write the header */ |
| 635 | if (s->status == INIT_STATE) { |
| 636 | #ifdef GZIP |
| 637 | if (s->wrap == 2) { |
| 638 | strm->adler = crc32(0L, Z_NULL, 0); |
| 639 | put_byte(s, 31); |
| 640 | put_byte(s, 139); |
| 641 | put_byte(s, 8); |
| 642 | if (s->gzhead == Z_NULL) { |
| 643 | put_byte(s, 0); |
| 644 | put_byte(s, 0); |
| 645 | put_byte(s, 0); |
| 646 | put_byte(s, 0); |
| 647 | put_byte(s, 0); |
| 648 | put_byte(s, s->level == 9 ? 2 : |
| 649 | (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? |
| 650 | 4 : 0)); |
| 651 | put_byte(s, OS_CODE); |
| 652 | s->status = BUSY_STATE; |
| 653 | } |
| 654 | else { |
| 655 | put_byte(s, (s->gzhead->text ? 1 : 0) + |
| 656 | (s->gzhead->hcrc ? 2 : 0) + |
| 657 | (s->gzhead->extra == Z_NULL ? 0 : 4) + |
| 658 | (s->gzhead->name == Z_NULL ? 0 : 8) + |
| 659 | (s->gzhead->comment == Z_NULL ? 0 : 16) |
| 660 | ); |
| 661 | put_byte(s, (Byte)(s->gzhead->time & 0xff)); |
| 662 | put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); |
| 663 | put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); |
| 664 | put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); |
| 665 | put_byte(s, s->level == 9 ? 2 : |
| 666 | (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? |
| 667 | 4 : 0)); |
| 668 | put_byte(s, s->gzhead->os & 0xff); |
| 669 | if (s->gzhead->extra != Z_NULL) { |
| 670 | put_byte(s, s->gzhead->extra_len & 0xff); |
| 671 | put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); |
| 672 | } |
| 673 | if (s->gzhead->hcrc) |
| 674 | strm->adler = crc32(strm->adler, s->pending_buf, |
| 675 | s->pending); |
| 676 | s->gzindex = 0; |
| 677 | s->status = EXTRA_STATE; |
| 678 | } |
| 679 | } |
| 680 | else |
| 681 | #endif |
| 682 | { |
| 683 | uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; |
| 684 | uInt level_flags; |
| 685 | |
| 686 | if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) |
| 687 | level_flags = 0; |
| 688 | else if (s->level < 6) |
| 689 | level_flags = 1; |
| 690 | else if (s->level == 6) |
| 691 | level_flags = 2; |
| 692 | else |
| 693 | level_flags = 3; |
| 694 | header |= (level_flags << 6); |
| 695 | if (s->strstart != 0) header |= PRESET_DICT; |
| 696 | header += 31 - (header % 31); |
| 697 | |
| 698 | s->status = BUSY_STATE; |
| 699 | putShortMSB(s, header); |
| 700 | |
| 701 | /* Save the adler32 of the preset dictionary: */ |
| 702 | if (s->strstart != 0) { |
| 703 | putShortMSB(s, (uInt)(strm->adler >> 16)); |
| 704 | putShortMSB(s, (uInt)(strm->adler & 0xffff)); |
| 705 | } |
| 706 | strm->adler = adler32(0L, Z_NULL, 0); |
| 707 | } |
| 708 | } |
| 709 | #ifdef GZIP |
| 710 | if (s->status == EXTRA_STATE) { |
| 711 | if (s->gzhead->extra != Z_NULL) { |
| 712 | uInt beg = s->pending; /* start of bytes to update crc */ |
| 713 | |
| 714 | while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { |
| 715 | if (s->pending == s->pending_buf_size) { |
| 716 | if (s->gzhead->hcrc && s->pending > beg) |
| 717 | strm->adler = crc32(strm->adler, s->pending_buf + beg, |
| 718 | s->pending - beg); |
| 719 | flush_pending(strm); |
| 720 | beg = s->pending; |
| 721 | if (s->pending == s->pending_buf_size) |
| 722 | break; |
| 723 | } |
| 724 | put_byte(s, s->gzhead->extra[s->gzindex]); |
| 725 | s->gzindex++; |
| 726 | } |
| 727 | if (s->gzhead->hcrc && s->pending > beg) |
| 728 | strm->adler = crc32(strm->adler, s->pending_buf + beg, |
| 729 | s->pending - beg); |
| 730 | if (s->gzindex == s->gzhead->extra_len) { |
| 731 | s->gzindex = 0; |
| 732 | s->status = NAME_STATE; |
| 733 | } |
| 734 | } |
| 735 | else |
| 736 | s->status = NAME_STATE; |
| 737 | } |
| 738 | if (s->status == NAME_STATE) { |
| 739 | if (s->gzhead->name != Z_NULL) { |
| 740 | uInt beg = s->pending; /* start of bytes to update crc */ |
| 741 | int val; |
| 742 | |
| 743 | do { |
| 744 | if (s->pending == s->pending_buf_size) { |
| 745 | if (s->gzhead->hcrc && s->pending > beg) |
| 746 | strm->adler = crc32(strm->adler, s->pending_buf + beg, |
| 747 | s->pending - beg); |
| 748 | flush_pending(strm); |
| 749 | beg = s->pending; |
| 750 | if (s->pending == s->pending_buf_size) { |
| 751 | val = 1; |
| 752 | break; |
| 753 | } |
| 754 | } |
| 755 | val = s->gzhead->name[s->gzindex++]; |
| 756 | put_byte(s, val); |
| 757 | } while (val != 0); |
| 758 | if (s->gzhead->hcrc && s->pending > beg) |
| 759 | strm->adler = crc32(strm->adler, s->pending_buf + beg, |
| 760 | s->pending - beg); |
| 761 | if (val == 0) { |
| 762 | s->gzindex = 0; |
| 763 | s->status = COMMENT_STATE; |
| 764 | } |
| 765 | } |
| 766 | else |
| 767 | s->status = COMMENT_STATE; |
| 768 | } |
| 769 | if (s->status == COMMENT_STATE) { |
| 770 | if (s->gzhead->comment != Z_NULL) { |
| 771 | uInt beg = s->pending; /* start of bytes to update crc */ |
| 772 | int val; |
| 773 | |
| 774 | do { |
| 775 | if (s->pending == s->pending_buf_size) { |
| 776 | if (s->gzhead->hcrc && s->pending > beg) |
| 777 | strm->adler = crc32(strm->adler, s->pending_buf + beg, |
| 778 | s->pending - beg); |
| 779 | flush_pending(strm); |
| 780 | beg = s->pending; |
| 781 | if (s->pending == s->pending_buf_size) { |
| 782 | val = 1; |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | val = s->gzhead->comment[s->gzindex++]; |
| 787 | put_byte(s, val); |
| 788 | } while (val != 0); |
| 789 | if (s->gzhead->hcrc && s->pending > beg) |
| 790 | strm->adler = crc32(strm->adler, s->pending_buf + beg, |
| 791 | s->pending - beg); |
| 792 | if (val == 0) |
| 793 | s->status = HCRC_STATE; |
| 794 | } |
| 795 | else |
| 796 | s->status = HCRC_STATE; |
| 797 | } |
| 798 | if (s->status == HCRC_STATE) { |
| 799 | if (s->gzhead->hcrc) { |
| 800 | if (s->pending + 2 > s->pending_buf_size) |
| 801 | flush_pending(strm); |
| 802 | if (s->pending + 2 <= s->pending_buf_size) { |
| 803 | put_byte(s, (Byte)(strm->adler & 0xff)); |
| 804 | put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); |
| 805 | strm->adler = crc32(0L, Z_NULL, 0); |
| 806 | s->status = BUSY_STATE; |
| 807 | } |
| 808 | } |
| 809 | else |
| 810 | s->status = BUSY_STATE; |
| 811 | } |
| 812 | #endif |
| 813 | |
| 814 | /* Flush as much pending output as possible */ |
| 815 | if (s->pending != 0) { |
| 816 | flush_pending(strm); |
| 817 | if (strm->avail_out == 0) { |
| 818 | /* Since avail_out is 0, deflate will be called again with |
| 819 | * more output space, but possibly with both pending and |
| 820 | * avail_in equal to zero. There won't be anything to do, |
| 821 | * but this is not an error situation so make sure we |
| 822 | * return OK instead of BUF_ERROR at next call of deflate: |
| 823 | */ |
| 824 | s->last_flush = -1; |
| 825 | return Z_OK; |
| 826 | } |
| 827 | |
| 828 | /* Make sure there is something to do and avoid duplicate consecutive |
| 829 | * flushes. For repeated and useless calls with Z_FINISH, we keep |
| 830 | * returning Z_STREAM_END instead of Z_BUF_ERROR. |
| 831 | */ |
| 832 | } else if (strm->avail_in == 0 && flush <= old_flush && |
| 833 | flush != Z_FINISH) { |
| 834 | ERR_RETURN(strm, Z_BUF_ERROR); |
| 835 | } |
| 836 | |
| 837 | /* User must not provide more input after the first FINISH: */ |
| 838 | if (s->status == FINISH_STATE && strm->avail_in != 0) { |
| 839 | ERR_RETURN(strm, Z_BUF_ERROR); |
| 840 | } |
| 841 | |
| 842 | /* Start a new block or continue the current one. |
| 843 | */ |
| 844 | if (strm->avail_in != 0 || s->lookahead != 0 || |
| 845 | (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { |
| 846 | block_state bstate; |
| 847 | |
| 848 | bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : |
| 849 | (s->strategy == Z_RLE ? deflate_rle(s, flush) : |
| 850 | (*(configuration_table[s->level].func))(s, flush)); |
| 851 | |
| 852 | if (bstate == finish_started || bstate == finish_done) { |
| 853 | s->status = FINISH_STATE; |
| 854 | } |
| 855 | if (bstate == need_more || bstate == finish_started) { |
| 856 | if (strm->avail_out == 0) { |
| 857 | s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ |
| 858 | } |
| 859 | return Z_OK; |
| 860 | /* If flush != Z_NO_FLUSH && avail_out == 0, the next call |
| 861 | * of deflate should use the same flush parameter to make sure |
| 862 | * that the flush is complete. So we don't have to output an |
| 863 | * empty block here, this will be done at next call. This also |
| 864 | * ensures that for a very small output buffer, we emit at most |
| 865 | * one empty block. |
| 866 | */ |
| 867 | } |
| 868 | if (bstate == block_done) { |
| 869 | if (flush == Z_PARTIAL_FLUSH) { |
| 870 | _tr_align(s); |
| 871 | } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ |
| 872 | _tr_stored_block(s, (char*)0, 0L, 0); |
| 873 | /* For a full flush, this empty block will be recognized |
| 874 | * as a special marker by inflate_sync(). |
| 875 | */ |
| 876 | if (flush == Z_FULL_FLUSH) { |
| 877 | CLEAR_HASH(s); /* forget history */ |
| 878 | if (s->lookahead == 0) { |
| 879 | s->strstart = 0; |
| 880 | s->block_start = 0L; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | flush_pending(strm); |
| 885 | if (strm->avail_out == 0) { |
| 886 | s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ |
| 887 | return Z_OK; |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | Assert(strm->avail_out > 0, "bug2"); |
| 892 | |
| 893 | if (flush != Z_FINISH) return Z_OK; |
| 894 | if (s->wrap <= 0) return Z_STREAM_END; |
| 895 | |
| 896 | /* Write the trailer */ |
| 897 | #ifdef GZIP |
| 898 | if (s->wrap == 2) { |
| 899 | put_byte(s, (Byte)(strm->adler & 0xff)); |
| 900 | put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); |
| 901 | put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); |
| 902 | put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); |
| 903 | put_byte(s, (Byte)(strm->total_in & 0xff)); |
| 904 | put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); |
| 905 | put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); |
| 906 | put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); |
| 907 | } |
| 908 | else |
| 909 | #endif |
| 910 | { |
| 911 | putShortMSB(s, (uInt)(strm->adler >> 16)); |
| 912 | putShortMSB(s, (uInt)(strm->adler & 0xffff)); |
| 913 | } |
| 914 | flush_pending(strm); |
| 915 | /* If avail_out is zero, the application will call deflate again |
| 916 | * to flush the rest. |
| 917 | */ |
| 918 | if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ |
| 919 | return s->pending != 0 ? Z_OK : Z_STREAM_END; |
| 920 | } |
| 921 | |
| 922 | /* ========================================================================= */ |
| 923 | int ZEXPORT deflateEnd (strm) |
| 924 | z_streamp strm; |
| 925 | { |
| 926 | int status; |
| 927 | |
| 928 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 929 | |
| 930 | status = strm->state->status; |
| 931 | if (status != INIT_STATE && |
| 932 | status != EXTRA_STATE && |
| 933 | status != NAME_STATE && |
| 934 | status != COMMENT_STATE && |
| 935 | status != HCRC_STATE && |
| 936 | status != BUSY_STATE && |
| 937 | status != FINISH_STATE) { |
| 938 | return Z_STREAM_ERROR; |
| 939 | } |
| 940 | |
| 941 | /* Deallocate in reverse order of allocations: */ |
| 942 | TRY_FREE(strm, strm->state->pending_buf); |
| 943 | TRY_FREE(strm, strm->state->head); |
| 944 | TRY_FREE(strm, strm->state->prev); |
| 945 | TRY_FREE(strm, strm->state->window); |
| 946 | |
| 947 | ZFREE(strm, strm->state); |
| 948 | strm->state = Z_NULL; |
| 949 | |
| 950 | return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; |
| 951 | } |
| 952 | |
| 953 | /* ========================================================================= |
| 954 | * Copy the source state to the destination state. |
| 955 | * To simplify the source, this is not supported for 16-bit MSDOS (which |
| 956 | * doesn't have enough memory anyway to duplicate compression states). |
| 957 | */ |
| 958 | int ZEXPORT deflateCopy (dest, source) |
| 959 | z_streamp dest; |
| 960 | z_streamp source; |
| 961 | { |
| 962 | #ifdef MAXSEG_64K |
| 963 | return Z_STREAM_ERROR; |
| 964 | #else |
| 965 | deflate_state *ds; |
| 966 | deflate_state *ss; |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 967 | |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 968 | if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { |
| 969 | return Z_STREAM_ERROR; |
| 970 | } |
| 971 | |
| 972 | ss = source->state; |
| 973 | |
| 974 | zmemcpy(dest, source, sizeof(z_stream)); |
| 975 | |
| 976 | ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); |
| 977 | if (ds == Z_NULL) return Z_MEM_ERROR; |
| 978 | dest->state = (struct internal_state FAR *) ds; |
| 979 | zmemcpy(ds, ss, sizeof(deflate_state)); |
| 980 | ds->strm = dest; |
| 981 | |
| 982 | ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); |
| 983 | ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); |
| 984 | ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); |
Tom Rini | c3411d4 | 2022-05-10 14:36:59 -0400 | [diff] [blame] | 985 | ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4); |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 986 | |
| 987 | if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || |
| 988 | ds->pending_buf == Z_NULL) { |
| 989 | deflateEnd (dest); |
| 990 | return Z_MEM_ERROR; |
| 991 | } |
| 992 | /* following zmemcpy do not work for 16-bit MSDOS */ |
| 993 | zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); |
| 994 | zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos)); |
| 995 | zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos)); |
| 996 | zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); |
| 997 | |
| 998 | ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); |
Tom Rini | c3411d4 | 2022-05-10 14:36:59 -0400 | [diff] [blame] | 999 | ds->sym_buf = ds->pending_buf + ds->lit_bufsize; |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 1000 | |
| 1001 | ds->l_desc.dyn_tree = ds->dyn_ltree; |
| 1002 | ds->d_desc.dyn_tree = ds->dyn_dtree; |
| 1003 | ds->bl_desc.dyn_tree = ds->bl_tree; |
| 1004 | |
| 1005 | return Z_OK; |
| 1006 | #endif /* MAXSEG_64K */ |
| 1007 | } |
| 1008 | |
| 1009 | /* =========================================================================== |
| 1010 | * Read a new buffer from the current input stream, update the adler32 |
| 1011 | * and total number of bytes read. All deflate() input goes through |
| 1012 | * this function so some applications may wish to modify it to avoid |
| 1013 | * allocating a large strm->next_in buffer and copying from it. |
| 1014 | * (See also flush_pending()). |
| 1015 | */ |
| 1016 | local int read_buf(strm, buf, size) |
| 1017 | z_streamp strm; |
| 1018 | Bytef *buf; |
| 1019 | unsigned size; |
| 1020 | { |
| 1021 | unsigned len = strm->avail_in; |
| 1022 | |
| 1023 | if (len > size) len = size; |
| 1024 | if (len == 0) return 0; |
| 1025 | |
| 1026 | strm->avail_in -= len; |
| 1027 | |
| 1028 | if (strm->state->wrap == 1) { |
| 1029 | strm->adler = adler32(strm->adler, strm->next_in, len); |
| 1030 | } |
| 1031 | #ifdef GZIP |
| 1032 | else if (strm->state->wrap == 2) { |
| 1033 | strm->adler = crc32(strm->adler, strm->next_in, len); |
| 1034 | } |
| 1035 | #endif |
| 1036 | zmemcpy(buf, strm->next_in, len); |
| 1037 | strm->next_in += len; |
| 1038 | strm->total_in += len; |
| 1039 | |
| 1040 | return (int)len; |
| 1041 | } |
| 1042 | |
| 1043 | /* =========================================================================== |
| 1044 | * Initialize the "longest match" routines for a new zlib stream |
| 1045 | */ |
| 1046 | local void lm_init (s) |
| 1047 | deflate_state *s; |
| 1048 | { |
| 1049 | s->window_size = (ulg)2L*s->w_size; |
| 1050 | |
| 1051 | CLEAR_HASH(s); |
| 1052 | |
| 1053 | /* Set the default configuration parameters: |
| 1054 | */ |
| 1055 | s->max_lazy_match = configuration_table[s->level].max_lazy; |
| 1056 | s->good_match = configuration_table[s->level].good_length; |
| 1057 | s->nice_match = configuration_table[s->level].nice_length; |
| 1058 | s->max_chain_length = configuration_table[s->level].max_chain; |
| 1059 | |
| 1060 | s->strstart = 0; |
| 1061 | s->block_start = 0L; |
| 1062 | s->lookahead = 0; |
| 1063 | s->match_length = s->prev_length = MIN_MATCH-1; |
| 1064 | s->match_available = 0; |
| 1065 | s->ins_h = 0; |
| 1066 | #ifndef FASTEST |
| 1067 | #ifdef ASMV |
| 1068 | match_init(); /* initialize the asm code */ |
| 1069 | #endif |
| 1070 | #endif |
| 1071 | } |
| 1072 | |
| 1073 | #ifndef FASTEST |
| 1074 | /* =========================================================================== |
| 1075 | * Set match_start to the longest match starting at the given string and |
| 1076 | * return its length. Matches shorter or equal to prev_length are discarded, |
| 1077 | * in which case the result is equal to prev_length and match_start is |
| 1078 | * garbage. |
| 1079 | * IN assertions: cur_match is the head of the hash chain for the current |
| 1080 | * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 |
| 1081 | * OUT assertion: the match length is not greater than s->lookahead. |
| 1082 | */ |
| 1083 | #ifndef ASMV |
| 1084 | /* For 80x86 and 680x0, an optimized version will be provided in match.asm or |
| 1085 | * match.S. The code will be functionally equivalent. |
| 1086 | */ |
| 1087 | local uInt longest_match(s, cur_match) |
| 1088 | deflate_state *s; |
| 1089 | IPos cur_match; /* current match */ |
| 1090 | { |
| 1091 | unsigned chain_length = s->max_chain_length;/* max hash chain length */ |
| 1092 | register Bytef *scan = s->window + s->strstart; /* current string */ |
| 1093 | register Bytef *match; /* matched string */ |
| 1094 | register int len; /* length of current match */ |
| 1095 | int best_len = s->prev_length; /* best match length so far */ |
| 1096 | int nice_match = s->nice_match; /* stop if match long enough */ |
| 1097 | IPos limit = s->strstart > (IPos)MAX_DIST(s) ? |
| 1098 | s->strstart - (IPos)MAX_DIST(s) : NIL; |
| 1099 | /* Stop when cur_match becomes <= limit. To simplify the code, |
| 1100 | * we prevent matches with the string of window index 0. |
| 1101 | */ |
| 1102 | Posf *prev = s->prev; |
| 1103 | uInt wmask = s->w_mask; |
| 1104 | |
| 1105 | #ifdef UNALIGNED_OK |
| 1106 | /* Compare two bytes at a time. Note: this is not always beneficial. |
| 1107 | * Try with and without -DUNALIGNED_OK to check. |
| 1108 | */ |
| 1109 | register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; |
| 1110 | register ush scan_start = *(ushf*)scan; |
| 1111 | register ush scan_end = *(ushf*)(scan+best_len-1); |
| 1112 | #else |
| 1113 | register Bytef *strend = s->window + s->strstart + MAX_MATCH; |
| 1114 | register Byte scan_end1 = scan[best_len-1]; |
| 1115 | register Byte scan_end = scan[best_len]; |
| 1116 | #endif |
| 1117 | |
| 1118 | /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. |
| 1119 | * It is easy to get rid of this optimization if necessary. |
| 1120 | */ |
| 1121 | Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); |
| 1122 | |
| 1123 | /* Do not waste too much time if we already have a good match: */ |
| 1124 | if (s->prev_length >= s->good_match) { |
| 1125 | chain_length >>= 2; |
| 1126 | } |
| 1127 | /* Do not look for matches beyond the end of the input. This is necessary |
| 1128 | * to make deflate deterministic. |
| 1129 | */ |
| 1130 | if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; |
| 1131 | |
| 1132 | Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); |
| 1133 | |
| 1134 | do { |
| 1135 | Assert(cur_match < s->strstart, "no future"); |
| 1136 | match = s->window + cur_match; |
| 1137 | |
| 1138 | /* Skip to next match if the match length cannot increase |
| 1139 | * or if the match length is less than 2. Note that the checks below |
| 1140 | * for insufficient lookahead only occur occasionally for performance |
| 1141 | * reasons. Therefore uninitialized memory will be accessed, and |
| 1142 | * conditional jumps will be made that depend on those values. |
| 1143 | * However the length of the match is limited to the lookahead, so |
| 1144 | * the output of deflate is not affected by the uninitialized values. |
| 1145 | */ |
| 1146 | #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) |
| 1147 | /* This code assumes sizeof(unsigned short) == 2. Do not use |
| 1148 | * UNALIGNED_OK if your compiler uses a different size. |
| 1149 | */ |
| 1150 | if (*(ushf*)(match+best_len-1) != scan_end || |
| 1151 | *(ushf*)match != scan_start) continue; |
| 1152 | |
| 1153 | /* It is not necessary to compare scan[2] and match[2] since they are |
| 1154 | * always equal when the other bytes match, given that the hash keys |
| 1155 | * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at |
| 1156 | * strstart+3, +5, ... up to strstart+257. We check for insufficient |
| 1157 | * lookahead only every 4th comparison; the 128th check will be made |
| 1158 | * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is |
| 1159 | * necessary to put more guard bytes at the end of the window, or |
| 1160 | * to check more often for insufficient lookahead. |
| 1161 | */ |
| 1162 | Assert(scan[2] == match[2], "scan[2]?"); |
| 1163 | scan++, match++; |
| 1164 | do { |
| 1165 | } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && |
| 1166 | *(ushf*)(scan+=2) == *(ushf*)(match+=2) && |
| 1167 | *(ushf*)(scan+=2) == *(ushf*)(match+=2) && |
| 1168 | *(ushf*)(scan+=2) == *(ushf*)(match+=2) && |
| 1169 | scan < strend); |
| 1170 | /* The funny "do {}" generates better code on most compilers */ |
| 1171 | |
| 1172 | /* Here, scan <= window+strstart+257 */ |
| 1173 | Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); |
| 1174 | if (*scan == *match) scan++; |
| 1175 | |
| 1176 | len = (MAX_MATCH - 1) - (int)(strend-scan); |
| 1177 | scan = strend - (MAX_MATCH-1); |
| 1178 | |
| 1179 | #else /* UNALIGNED_OK */ |
| 1180 | |
| 1181 | if (match[best_len] != scan_end || |
| 1182 | match[best_len-1] != scan_end1 || |
| 1183 | *match != *scan || |
| 1184 | *++match != scan[1]) continue; |
| 1185 | |
| 1186 | /* The check at best_len-1 can be removed because it will be made |
| 1187 | * again later. (This heuristic is not always a win.) |
| 1188 | * It is not necessary to compare scan[2] and match[2] since they |
| 1189 | * are always equal when the other bytes match, given that |
| 1190 | * the hash keys are equal and that HASH_BITS >= 8. |
| 1191 | */ |
| 1192 | scan += 2, match++; |
| 1193 | Assert(*scan == *match, "match[2]?"); |
| 1194 | |
| 1195 | /* We check for insufficient lookahead only every 8th comparison; |
| 1196 | * the 256th check will be made at strstart+258. |
| 1197 | */ |
| 1198 | do { |
| 1199 | } while (*++scan == *++match && *++scan == *++match && |
| 1200 | *++scan == *++match && *++scan == *++match && |
| 1201 | *++scan == *++match && *++scan == *++match && |
| 1202 | *++scan == *++match && *++scan == *++match && |
| 1203 | scan < strend); |
| 1204 | |
| 1205 | Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); |
| 1206 | |
| 1207 | len = MAX_MATCH - (int)(strend - scan); |
| 1208 | scan = strend - MAX_MATCH; |
| 1209 | |
| 1210 | #endif /* UNALIGNED_OK */ |
| 1211 | |
| 1212 | if (len > best_len) { |
| 1213 | s->match_start = cur_match; |
| 1214 | best_len = len; |
| 1215 | if (len >= nice_match) break; |
| 1216 | #ifdef UNALIGNED_OK |
| 1217 | scan_end = *(ushf*)(scan+best_len-1); |
| 1218 | #else |
| 1219 | scan_end1 = scan[best_len-1]; |
| 1220 | scan_end = scan[best_len]; |
| 1221 | #endif |
| 1222 | } |
| 1223 | } while ((cur_match = prev[cur_match & wmask]) > limit |
| 1224 | && --chain_length != 0); |
| 1225 | |
| 1226 | if ((uInt)best_len <= s->lookahead) return (uInt)best_len; |
| 1227 | return s->lookahead; |
| 1228 | } |
| 1229 | #endif /* ASMV */ |
| 1230 | |
| 1231 | #else /* FASTEST */ |
| 1232 | |
| 1233 | /* --------------------------------------------------------------------------- |
| 1234 | * Optimized version for FASTEST only |
| 1235 | */ |
| 1236 | local uInt longest_match(s, cur_match) |
| 1237 | deflate_state *s; |
| 1238 | IPos cur_match; /* current match */ |
| 1239 | { |
| 1240 | register Bytef *scan = s->window + s->strstart; /* current string */ |
| 1241 | register Bytef *match; /* matched string */ |
| 1242 | register int len; /* length of current match */ |
| 1243 | register Bytef *strend = s->window + s->strstart + MAX_MATCH; |
| 1244 | |
| 1245 | /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. |
| 1246 | * It is easy to get rid of this optimization if necessary. |
| 1247 | */ |
| 1248 | Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); |
| 1249 | |
| 1250 | Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); |
| 1251 | |
| 1252 | Assert(cur_match < s->strstart, "no future"); |
| 1253 | |
| 1254 | match = s->window + cur_match; |
| 1255 | |
| 1256 | /* Return failure if the match length is less than 2: |
| 1257 | */ |
| 1258 | if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; |
| 1259 | |
| 1260 | /* The check at best_len-1 can be removed because it will be made |
| 1261 | * again later. (This heuristic is not always a win.) |
| 1262 | * It is not necessary to compare scan[2] and match[2] since they |
| 1263 | * are always equal when the other bytes match, given that |
| 1264 | * the hash keys are equal and that HASH_BITS >= 8. |
| 1265 | */ |
| 1266 | scan += 2, match += 2; |
| 1267 | Assert(*scan == *match, "match[2]?"); |
| 1268 | |
| 1269 | /* We check for insufficient lookahead only every 8th comparison; |
| 1270 | * the 256th check will be made at strstart+258. |
| 1271 | */ |
| 1272 | do { |
| 1273 | } while (*++scan == *++match && *++scan == *++match && |
| 1274 | *++scan == *++match && *++scan == *++match && |
| 1275 | *++scan == *++match && *++scan == *++match && |
| 1276 | *++scan == *++match && *++scan == *++match && |
| 1277 | scan < strend); |
| 1278 | |
| 1279 | Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); |
| 1280 | |
| 1281 | len = MAX_MATCH - (int)(strend - scan); |
| 1282 | |
| 1283 | if (len < MIN_MATCH) return MIN_MATCH - 1; |
| 1284 | |
| 1285 | s->match_start = cur_match; |
| 1286 | return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; |
| 1287 | } |
| 1288 | |
| 1289 | #endif /* FASTEST */ |
| 1290 | |
| 1291 | #ifdef DEBUG |
| 1292 | /* =========================================================================== |
| 1293 | * Check that the match at match_start is indeed a match. |
| 1294 | */ |
| 1295 | local void check_match(s, start, match, length) |
| 1296 | deflate_state *s; |
| 1297 | IPos start, match; |
| 1298 | int length; |
| 1299 | { |
| 1300 | /* check that the match is indeed a match */ |
| 1301 | if (zmemcmp(s->window + match, |
| 1302 | s->window + start, length) != EQUAL) { |
| 1303 | fprintf(stderr, " start %u, match %u, length %d\n", |
| 1304 | start, match, length); |
| 1305 | do { |
| 1306 | fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); |
| 1307 | } while (--length != 0); |
| 1308 | z_error("invalid match"); |
| 1309 | } |
| 1310 | if (z_verbose > 1) { |
| 1311 | fprintf(stderr,"\\[%d,%d]", start-match, length); |
Heinrich Schuchardt | 8239a7f | 2020-12-28 21:41:40 +0100 | [diff] [blame] | 1312 | do { putc(s->window[start++]); } while (--length != 0); |
Lei Wen | b834a39 | 2012-09-28 04:26:42 +0000 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | #else |
| 1316 | # define check_match(s, start, match, length) |
| 1317 | #endif /* DEBUG */ |
| 1318 | |
| 1319 | /* =========================================================================== |
| 1320 | * Fill the window when the lookahead becomes insufficient. |
| 1321 | * Updates strstart and lookahead. |
| 1322 | * |
| 1323 | * IN assertion: lookahead < MIN_LOOKAHEAD |
| 1324 | * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD |
| 1325 | * At least one byte has been read, or avail_in == 0; reads are |
| 1326 | * performed for at least two bytes (required for the zip translate_eol |
| 1327 | * option -- not supported here). |
| 1328 | */ |
| 1329 | local void fill_window(s) |
| 1330 | deflate_state *s; |
| 1331 | { |
| 1332 | register unsigned n, m; |
| 1333 | register Posf *p; |
| 1334 | unsigned more; /* Amount of free space at the end of the window. */ |
| 1335 | uInt wsize = s->w_size; |
| 1336 | |
| 1337 | do { |
| 1338 | more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); |
| 1339 | |
| 1340 | /* Deal with !@#$% 64K limit: */ |
| 1341 | if (sizeof(int) <= 2) { |
| 1342 | if (more == 0 && s->strstart == 0 && s->lookahead == 0) { |
| 1343 | more = wsize; |
| 1344 | |
| 1345 | } else if (more == (unsigned)(-1)) { |
| 1346 | /* Very unlikely, but possible on 16 bit machine if |
| 1347 | * strstart == 0 && lookahead == 1 (input done a byte at time) |
| 1348 | */ |
| 1349 | more--; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | /* If the window is almost full and there is insufficient lookahead, |
| 1354 | * move the upper half to the lower one to make room in the upper half. |
| 1355 | */ |
| 1356 | if (s->strstart >= wsize+MAX_DIST(s)) { |
| 1357 | |
| 1358 | zmemcpy(s->window, s->window+wsize, (unsigned)wsize); |
| 1359 | s->match_start -= wsize; |
| 1360 | s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ |
| 1361 | s->block_start -= (long) wsize; |
| 1362 | |
| 1363 | /* Slide the hash table (could be avoided with 32 bit values |
| 1364 | at the expense of memory usage). We slide even when level == 0 |
| 1365 | to keep the hash table consistent if we switch back to level > 0 |
| 1366 | later. (Using level 0 permanently is not an optimal usage of |
| 1367 | zlib, so we don't care about this pathological case.) |
| 1368 | */ |
| 1369 | n = s->hash_size; |
| 1370 | p = &s->head[n]; |
| 1371 | do { |
| 1372 | m = *--p; |
| 1373 | *p = (Pos)(m >= wsize ? m-wsize : NIL); |
| 1374 | } while (--n); |
| 1375 | |
| 1376 | n = wsize; |
| 1377 | #ifndef FASTEST |
| 1378 | p = &s->prev[n]; |
| 1379 | do { |
| 1380 | m = *--p; |
| 1381 | *p = (Pos)(m >= wsize ? m-wsize : NIL); |
| 1382 | /* If n is not on any hash chain, prev[n] is garbage but |
| 1383 | * its value will never be used. |
| 1384 | */ |
| 1385 | } while (--n); |
| 1386 | #endif |
| 1387 | more += wsize; |
| 1388 | } |
| 1389 | if (s->strm->avail_in == 0) return; |
| 1390 | |
| 1391 | /* If there was no sliding: |
| 1392 | * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && |
| 1393 | * more == window_size - lookahead - strstart |
| 1394 | * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) |
| 1395 | * => more >= window_size - 2*WSIZE + 2 |
| 1396 | * In the BIG_MEM or MMAP case (not yet supported), |
| 1397 | * window_size == input_size + MIN_LOOKAHEAD && |
| 1398 | * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. |
| 1399 | * Otherwise, window_size == 2*WSIZE so more >= 2. |
| 1400 | * If there was sliding, more >= WSIZE. So in all cases, more >= 2. |
| 1401 | */ |
| 1402 | Assert(more >= 2, "more < 2"); |
| 1403 | |
| 1404 | n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); |
| 1405 | s->lookahead += n; |
| 1406 | |
| 1407 | /* Initialize the hash value now that we have some input: */ |
| 1408 | if (s->lookahead >= MIN_MATCH) { |
| 1409 | s->ins_h = s->window[s->strstart]; |
| 1410 | UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); |
| 1411 | #if MIN_MATCH != 3 |
| 1412 | Call UPDATE_HASH() MIN_MATCH-3 more times |
| 1413 | #endif |
| 1414 | } |
| 1415 | /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, |
| 1416 | * but this is not important since only literal bytes will be emitted. |
| 1417 | */ |
| 1418 | |
| 1419 | } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); |
| 1420 | |
| 1421 | /* If the WIN_INIT bytes after the end of the current data have never been |
| 1422 | * written, then zero those bytes in order to avoid memory check reports of |
| 1423 | * the use of uninitialized (or uninitialised as Julian writes) bytes by |
| 1424 | * the longest match routines. Update the high water mark for the next |
| 1425 | * time through here. WIN_INIT is set to MAX_MATCH since the longest match |
| 1426 | * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. |
| 1427 | */ |
| 1428 | if (s->high_water < s->window_size) { |
| 1429 | ulg curr = s->strstart + (ulg)(s->lookahead); |
| 1430 | ulg init; |
| 1431 | |
| 1432 | if (s->high_water < curr) { |
| 1433 | /* Previous high water mark below current data -- zero WIN_INIT |
| 1434 | * bytes or up to end of window, whichever is less. |
| 1435 | */ |
| 1436 | init = s->window_size - curr; |
| 1437 | if (init > WIN_INIT) |
| 1438 | init = WIN_INIT; |
| 1439 | zmemzero(s->window + curr, (unsigned)init); |
| 1440 | s->high_water = curr + init; |
| 1441 | } |
| 1442 | else if (s->high_water < (ulg)curr + WIN_INIT) { |
| 1443 | /* High water mark at or above current data, but below current data |
| 1444 | * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up |
| 1445 | * to end of window, whichever is less. |
| 1446 | */ |
| 1447 | init = (ulg)curr + WIN_INIT - s->high_water; |
| 1448 | if (init > s->window_size - s->high_water) |
| 1449 | init = s->window_size - s->high_water; |
| 1450 | zmemzero(s->window + s->high_water, (unsigned)init); |
| 1451 | s->high_water += init; |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | /* =========================================================================== |
| 1457 | * Flush the current block, with given end-of-file flag. |
| 1458 | * IN assertion: strstart is set to the end of the current match. |
| 1459 | */ |
| 1460 | #define FLUSH_BLOCK_ONLY(s, last) { \ |
| 1461 | _tr_flush_block(s, (s->block_start >= 0L ? \ |
| 1462 | (charf *)&s->window[(unsigned)s->block_start] : \ |
| 1463 | (charf *)Z_NULL), \ |
| 1464 | (ulg)((long)s->strstart - s->block_start), \ |
| 1465 | (last)); \ |
| 1466 | s->block_start = s->strstart; \ |
| 1467 | flush_pending(s->strm); \ |
| 1468 | Tracev((stderr,"[FLUSH]")); \ |
| 1469 | } |
| 1470 | |
| 1471 | /* Same but force premature exit if necessary. */ |
| 1472 | #define FLUSH_BLOCK(s, last) { \ |
| 1473 | FLUSH_BLOCK_ONLY(s, last); \ |
| 1474 | if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ |
| 1475 | } |
| 1476 | |
| 1477 | /* =========================================================================== |
| 1478 | * Copy without compression as much as possible from the input stream, return |
| 1479 | * the current block state. |
| 1480 | * This function does not insert new strings in the dictionary since |
| 1481 | * uncompressible data is probably not useful. This function is used |
| 1482 | * only for the level=0 compression option. |
| 1483 | * NOTE: this function should be optimized to avoid extra copying from |
| 1484 | * window to pending_buf. |
| 1485 | */ |
| 1486 | local block_state deflate_stored(s, flush) |
| 1487 | deflate_state *s; |
| 1488 | int flush; |
| 1489 | { |
| 1490 | /* Stored blocks are limited to 0xffff bytes, pending_buf is limited |
| 1491 | * to pending_buf_size, and each stored block has a 5 byte header: |
| 1492 | */ |
| 1493 | ulg max_block_size = 0xffff; |
| 1494 | ulg max_start; |
| 1495 | |
| 1496 | if (max_block_size > s->pending_buf_size - 5) { |
| 1497 | max_block_size = s->pending_buf_size - 5; |
| 1498 | } |
| 1499 | |
| 1500 | /* Copy as much as possible from input to output: */ |
| 1501 | for (;;) { |
| 1502 | /* Fill the window as much as possible: */ |
| 1503 | if (s->lookahead <= 1) { |
| 1504 | |
| 1505 | Assert(s->strstart < s->w_size+MAX_DIST(s) || |
| 1506 | s->block_start >= (long)s->w_size, "slide too late"); |
| 1507 | |
| 1508 | fill_window(s); |
| 1509 | if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more; |
| 1510 | |
| 1511 | if (s->lookahead == 0) break; /* flush the current block */ |
| 1512 | } |
| 1513 | Assert(s->block_start >= 0L, "block gone"); |
| 1514 | |
| 1515 | s->strstart += s->lookahead; |
| 1516 | s->lookahead = 0; |
| 1517 | |
| 1518 | /* Emit a stored block if pending_buf will be full: */ |
| 1519 | max_start = s->block_start + max_block_size; |
| 1520 | if (s->strstart == 0 || (ulg)s->strstart >= max_start) { |
| 1521 | /* strstart == 0 is possible when wraparound on 16-bit machine */ |
| 1522 | s->lookahead = (uInt)(s->strstart - max_start); |
| 1523 | s->strstart = (uInt)max_start; |
| 1524 | FLUSH_BLOCK(s, 0); |
| 1525 | } |
| 1526 | /* Flush if we may have to slide, otherwise block_start may become |
| 1527 | * negative and the data will be gone: |
| 1528 | */ |
| 1529 | if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { |
| 1530 | FLUSH_BLOCK(s, 0); |
| 1531 | } |
| 1532 | } |
| 1533 | FLUSH_BLOCK(s, flush == Z_FINISH); |
| 1534 | return flush == Z_FINISH ? finish_done : block_done; |
| 1535 | } |
| 1536 | |
| 1537 | /* =========================================================================== |
| 1538 | * Compress as much as possible from the input stream, return the current |
| 1539 | * block state. |
| 1540 | * This function does not perform lazy evaluation of matches and inserts |
| 1541 | * new strings in the dictionary only for unmatched strings or for short |
| 1542 | * matches. It is used only for the fast compression options. |
| 1543 | */ |
| 1544 | local block_state deflate_fast(s, flush) |
| 1545 | deflate_state *s; |
| 1546 | int flush; |
| 1547 | { |
| 1548 | IPos hash_head; /* head of the hash chain */ |
| 1549 | int bflush; /* set if current block must be flushed */ |
| 1550 | |
| 1551 | for (;;) { |
| 1552 | /* Make sure that we always have enough lookahead, except |
| 1553 | * at the end of the input file. We need MAX_MATCH bytes |
| 1554 | * for the next match, plus MIN_MATCH bytes to insert the |
| 1555 | * string following the next match. |
| 1556 | */ |
| 1557 | if (s->lookahead < MIN_LOOKAHEAD) { |
| 1558 | fill_window(s); |
| 1559 | if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { |
| 1560 | return need_more; |
| 1561 | } |
| 1562 | if (s->lookahead == 0) break; /* flush the current block */ |
| 1563 | } |
| 1564 | |
| 1565 | /* Insert the string window[strstart .. strstart+2] in the |
| 1566 | * dictionary, and set hash_head to the head of the hash chain: |
| 1567 | */ |
| 1568 | hash_head = NIL; |
| 1569 | if (s->lookahead >= MIN_MATCH) { |
| 1570 | INSERT_STRING(s, s->strstart, hash_head); |
| 1571 | } |
| 1572 | |
| 1573 | /* Find the longest match, discarding those <= prev_length. |
| 1574 | * At this point we have always match_length < MIN_MATCH |
| 1575 | */ |
| 1576 | if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { |
| 1577 | /* To simplify the code, we prevent matches with the string |
| 1578 | * of window index 0 (in particular we have to avoid a match |
| 1579 | * of the string with itself at the start of the input file). |
| 1580 | */ |
| 1581 | s->match_length = longest_match (s, hash_head); |
| 1582 | /* longest_match() sets match_start */ |
| 1583 | } |
| 1584 | if (s->match_length >= MIN_MATCH) { |
| 1585 | check_match(s, s->strstart, s->match_start, s->match_length); |
| 1586 | |
| 1587 | _tr_tally_dist(s, s->strstart - s->match_start, |
| 1588 | s->match_length - MIN_MATCH, bflush); |
| 1589 | |
| 1590 | s->lookahead -= s->match_length; |
| 1591 | |
| 1592 | /* Insert new strings in the hash table only if the match length |
| 1593 | * is not too large. This saves time but degrades compression. |
| 1594 | */ |
| 1595 | #ifndef FASTEST |
| 1596 | if (s->match_length <= s->max_insert_length && |
| 1597 | s->lookahead >= MIN_MATCH) { |
| 1598 | s->match_length--; /* string at strstart already in table */ |
| 1599 | do { |
| 1600 | s->strstart++; |
| 1601 | INSERT_STRING(s, s->strstart, hash_head); |
| 1602 | /* strstart never exceeds WSIZE-MAX_MATCH, so there are |
| 1603 | * always MIN_MATCH bytes ahead. |
| 1604 | */ |
| 1605 | } while (--s->match_length != 0); |
| 1606 | s->strstart++; |
| 1607 | } else |
| 1608 | #endif |
| 1609 | { |
| 1610 | s->strstart += s->match_length; |
| 1611 | s->match_length = 0; |
| 1612 | s->ins_h = s->window[s->strstart]; |
| 1613 | UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); |
| 1614 | #if MIN_MATCH != 3 |
| 1615 | Call UPDATE_HASH() MIN_MATCH-3 more times |
| 1616 | #endif |
| 1617 | /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not |
| 1618 | * matter since it will be recomputed at next deflate call. |
| 1619 | */ |
| 1620 | } |
| 1621 | } else { |
| 1622 | /* No match, output a literal byte */ |
| 1623 | Tracevv((stderr,"%c", s->window[s->strstart])); |
| 1624 | _tr_tally_lit (s, s->window[s->strstart], bflush); |
| 1625 | s->lookahead--; |
| 1626 | s->strstart++; |
| 1627 | } |
| 1628 | if (bflush) FLUSH_BLOCK(s, 0); |
| 1629 | } |
| 1630 | FLUSH_BLOCK(s, flush == Z_FINISH); |
| 1631 | return flush == Z_FINISH ? finish_done : block_done; |
| 1632 | } |
| 1633 | |
| 1634 | #ifndef FASTEST |
| 1635 | /* =========================================================================== |
| 1636 | * Same as above, but achieves better compression. We use a lazy |
| 1637 | * evaluation for matches: a match is finally adopted only if there is |
| 1638 | * no better match at the next window position. |
| 1639 | */ |
| 1640 | local block_state deflate_slow(s, flush) |
| 1641 | deflate_state *s; |
| 1642 | int flush; |
| 1643 | { |
| 1644 | IPos hash_head; /* head of hash chain */ |
| 1645 | int bflush; /* set if current block must be flushed */ |
| 1646 | |
| 1647 | /* Process the input block. */ |
| 1648 | for (;;) { |
| 1649 | /* Make sure that we always have enough lookahead, except |
| 1650 | * at the end of the input file. We need MAX_MATCH bytes |
| 1651 | * for the next match, plus MIN_MATCH bytes to insert the |
| 1652 | * string following the next match. |
| 1653 | */ |
| 1654 | if (s->lookahead < MIN_LOOKAHEAD) { |
| 1655 | fill_window(s); |
| 1656 | if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { |
| 1657 | return need_more; |
| 1658 | } |
| 1659 | if (s->lookahead == 0) break; /* flush the current block */ |
| 1660 | } |
| 1661 | |
| 1662 | /* Insert the string window[strstart .. strstart+2] in the |
| 1663 | * dictionary, and set hash_head to the head of the hash chain: |
| 1664 | */ |
| 1665 | hash_head = NIL; |
| 1666 | if (s->lookahead >= MIN_MATCH) { |
| 1667 | INSERT_STRING(s, s->strstart, hash_head); |
| 1668 | } |
| 1669 | |
| 1670 | /* Find the longest match, discarding those <= prev_length. |
| 1671 | */ |
| 1672 | s->prev_length = s->match_length, s->prev_match = s->match_start; |
| 1673 | s->match_length = MIN_MATCH-1; |
| 1674 | |
| 1675 | if (hash_head != NIL && s->prev_length < s->max_lazy_match && |
| 1676 | s->strstart - hash_head <= MAX_DIST(s)) { |
| 1677 | /* To simplify the code, we prevent matches with the string |
| 1678 | * of window index 0 (in particular we have to avoid a match |
| 1679 | * of the string with itself at the start of the input file). |
| 1680 | */ |
| 1681 | s->match_length = longest_match (s, hash_head); |
| 1682 | /* longest_match() sets match_start */ |
| 1683 | |
| 1684 | if (s->match_length <= 5 && (s->strategy == Z_FILTERED |
| 1685 | #if TOO_FAR <= 32767 |
| 1686 | || (s->match_length == MIN_MATCH && |
| 1687 | s->strstart - s->match_start > TOO_FAR) |
| 1688 | #endif |
| 1689 | )) { |
| 1690 | |
| 1691 | /* If prev_match is also MIN_MATCH, match_start is garbage |
| 1692 | * but we will ignore the current match anyway. |
| 1693 | */ |
| 1694 | s->match_length = MIN_MATCH-1; |
| 1695 | } |
| 1696 | } |
| 1697 | /* If there was a match at the previous step and the current |
| 1698 | * match is not better, output the previous match: |
| 1699 | */ |
| 1700 | if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { |
| 1701 | uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; |
| 1702 | /* Do not insert strings in hash table beyond this. */ |
| 1703 | |
| 1704 | check_match(s, s->strstart-1, s->prev_match, s->prev_length); |
| 1705 | |
| 1706 | _tr_tally_dist(s, s->strstart -1 - s->prev_match, |
| 1707 | s->prev_length - MIN_MATCH, bflush); |
| 1708 | |
| 1709 | /* Insert in hash table all strings up to the end of the match. |
| 1710 | * strstart-1 and strstart are already inserted. If there is not |
| 1711 | * enough lookahead, the last two strings are not inserted in |
| 1712 | * the hash table. |
| 1713 | */ |
| 1714 | s->lookahead -= s->prev_length-1; |
| 1715 | s->prev_length -= 2; |
| 1716 | do { |
| 1717 | if (++s->strstart <= max_insert) { |
| 1718 | INSERT_STRING(s, s->strstart, hash_head); |
| 1719 | } |
| 1720 | } while (--s->prev_length != 0); |
| 1721 | s->match_available = 0; |
| 1722 | s->match_length = MIN_MATCH-1; |
| 1723 | s->strstart++; |
| 1724 | |
| 1725 | if (bflush) FLUSH_BLOCK(s, 0); |
| 1726 | |
| 1727 | } else if (s->match_available) { |
| 1728 | /* If there was no match at the previous position, output a |
| 1729 | * single literal. If there was a match but the current match |
| 1730 | * is longer, truncate the previous match to a single literal. |
| 1731 | */ |
| 1732 | Tracevv((stderr,"%c", s->window[s->strstart-1])); |
| 1733 | _tr_tally_lit(s, s->window[s->strstart-1], bflush); |
| 1734 | if (bflush) { |
| 1735 | FLUSH_BLOCK_ONLY(s, 0); |
| 1736 | } |
| 1737 | s->strstart++; |
| 1738 | s->lookahead--; |
| 1739 | if (s->strm->avail_out == 0) return need_more; |
| 1740 | } else { |
| 1741 | /* There is no previous match to compare with, wait for |
| 1742 | * the next step to decide. |
| 1743 | */ |
| 1744 | s->match_available = 1; |
| 1745 | s->strstart++; |
| 1746 | s->lookahead--; |
| 1747 | } |
| 1748 | } |
| 1749 | Assert (flush != Z_NO_FLUSH, "no flush?"); |
| 1750 | if (s->match_available) { |
| 1751 | Tracevv((stderr,"%c", s->window[s->strstart-1])); |
| 1752 | _tr_tally_lit(s, s->window[s->strstart-1], bflush); |
| 1753 | s->match_available = 0; |
| 1754 | } |
| 1755 | FLUSH_BLOCK(s, flush == Z_FINISH); |
| 1756 | return flush == Z_FINISH ? finish_done : block_done; |
| 1757 | } |
| 1758 | #endif /* FASTEST */ |
| 1759 | |
| 1760 | /* =========================================================================== |
| 1761 | * For Z_RLE, simply look for runs of bytes, generate matches only of distance |
| 1762 | * one. Do not maintain a hash table. (It will be regenerated if this run of |
| 1763 | * deflate switches away from Z_RLE.) |
| 1764 | */ |
| 1765 | local block_state deflate_rle(s, flush) |
| 1766 | deflate_state *s; |
| 1767 | int flush; |
| 1768 | { |
| 1769 | int bflush; /* set if current block must be flushed */ |
| 1770 | uInt prev; /* byte at distance one to match */ |
| 1771 | Bytef *scan, *strend; /* scan goes up to strend for length of run */ |
| 1772 | |
| 1773 | for (;;) { |
| 1774 | /* Make sure that we always have enough lookahead, except |
| 1775 | * at the end of the input file. We need MAX_MATCH bytes |
| 1776 | * for the longest encodable run. |
| 1777 | */ |
| 1778 | if (s->lookahead < MAX_MATCH) { |
| 1779 | fill_window(s); |
| 1780 | if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { |
| 1781 | return need_more; |
| 1782 | } |
| 1783 | if (s->lookahead == 0) break; /* flush the current block */ |
| 1784 | } |
| 1785 | |
| 1786 | /* See how many times the previous byte repeats */ |
| 1787 | s->match_length = 0; |
| 1788 | if (s->lookahead >= MIN_MATCH && s->strstart > 0) { |
| 1789 | scan = s->window + s->strstart - 1; |
| 1790 | prev = *scan; |
| 1791 | if (prev == *++scan && prev == *++scan && prev == *++scan) { |
| 1792 | strend = s->window + s->strstart + MAX_MATCH; |
| 1793 | do { |
| 1794 | } while (prev == *++scan && prev == *++scan && |
| 1795 | prev == *++scan && prev == *++scan && |
| 1796 | prev == *++scan && prev == *++scan && |
| 1797 | prev == *++scan && prev == *++scan && |
| 1798 | scan < strend); |
| 1799 | s->match_length = MAX_MATCH - (int)(strend - scan); |
| 1800 | if (s->match_length > s->lookahead) |
| 1801 | s->match_length = s->lookahead; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | /* Emit match if have run of MIN_MATCH or longer, else emit literal */ |
| 1806 | if (s->match_length >= MIN_MATCH) { |
| 1807 | check_match(s, s->strstart, s->strstart - 1, s->match_length); |
| 1808 | |
| 1809 | _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); |
| 1810 | |
| 1811 | s->lookahead -= s->match_length; |
| 1812 | s->strstart += s->match_length; |
| 1813 | s->match_length = 0; |
| 1814 | } else { |
| 1815 | /* No match, output a literal byte */ |
| 1816 | Tracevv((stderr,"%c", s->window[s->strstart])); |
| 1817 | _tr_tally_lit (s, s->window[s->strstart], bflush); |
| 1818 | s->lookahead--; |
| 1819 | s->strstart++; |
| 1820 | } |
| 1821 | if (bflush) FLUSH_BLOCK(s, 0); |
| 1822 | } |
| 1823 | FLUSH_BLOCK(s, flush == Z_FINISH); |
| 1824 | return flush == Z_FINISH ? finish_done : block_done; |
| 1825 | } |
| 1826 | |
| 1827 | /* =========================================================================== |
| 1828 | * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. |
| 1829 | * (It will be regenerated if this run of deflate switches away from Huffman.) |
| 1830 | */ |
| 1831 | local block_state deflate_huff(s, flush) |
| 1832 | deflate_state *s; |
| 1833 | int flush; |
| 1834 | { |
| 1835 | int bflush; /* set if current block must be flushed */ |
| 1836 | |
| 1837 | for (;;) { |
| 1838 | /* Make sure that we have a literal to write. */ |
| 1839 | if (s->lookahead == 0) { |
| 1840 | fill_window(s); |
| 1841 | if (s->lookahead == 0) { |
| 1842 | if (flush == Z_NO_FLUSH) |
| 1843 | return need_more; |
| 1844 | break; /* flush the current block */ |
| 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | /* Output a literal byte */ |
| 1849 | s->match_length = 0; |
| 1850 | Tracevv((stderr,"%c", s->window[s->strstart])); |
| 1851 | _tr_tally_lit (s, s->window[s->strstart], bflush); |
| 1852 | s->lookahead--; |
| 1853 | s->strstart++; |
| 1854 | if (bflush) FLUSH_BLOCK(s, 0); |
| 1855 | } |
| 1856 | FLUSH_BLOCK(s, flush == Z_FINISH); |
| 1857 | return flush == Z_FINISH ? finish_done : block_done; |
| 1858 | } |