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