annotate src/zlib-1.2.8/deflate.c @ 83:ae30d91d2ffe

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