| OLD | NEW |
| 1 /* deflate.c -- compress data using the deflation algorithm | 1 /* deflate.c -- compress data using the deflation algorithm |
| 2 * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler | 2 * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler |
| 3 * For conditions of distribution and use, see copyright notice in zlib.h | 3 * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 /* | 6 /* |
| 7 * ALGORITHM | 7 * ALGORITHM |
| 8 * | 8 * |
| 9 * The "deflation" process depends on being able to identify portions | 9 * The "deflation" process depends on being able to identify portions |
| 10 * of the input text which are identical to earlier input (within a | 10 * of the input text which are identical to earlier input (within a |
| 11 * sliding window trailing behind the input currently being processed). | 11 * sliding window trailing behind the input currently being processed). |
| 12 * | 12 * |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 | 46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 |
| 47 * | 47 * |
| 48 */ | 48 */ |
| 49 | 49 |
| 50 /* @(#) $Id$ */ | 50 /* @(#) $Id$ */ |
| 51 #include <assert.h> | 51 #include <assert.h> |
| 52 #include "deflate.h" | 52 #include "deflate.h" |
| 53 #include "x86.h" | 53 #include "x86.h" |
| 54 | 54 |
| 55 const char deflate_copyright[] = | 55 const char deflate_copyright[] = |
| 56 " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler "; | 56 " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler "; |
| 57 /* | 57 /* |
| 58 If you use the zlib library in a product, an acknowledgment is welcome | 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 | 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 | 60 include such an acknowledgment, I would appreciate that you keep this |
| 61 copyright string in the executable of your product. | 61 copyright string in the executable of your product. |
| 62 */ | 62 */ |
| 63 | 63 |
| 64 /* =========================================================================== | 64 /* =========================================================================== |
| 65 * Function prototypes. | 65 * Function prototypes. |
| 66 */ | 66 */ |
| 67 typedef enum { | 67 typedef enum { |
| 68 need_more, /* block not completed, need more input or more output */ | 68 need_more, /* block not completed, need more input or more output */ |
| 69 block_done, /* block flush performed */ | 69 block_done, /* block flush performed */ |
| 70 finish_started, /* finish started, need only more output at next deflate */ | 70 finish_started, /* finish started, need only more output at next deflate */ |
| 71 finish_done /* finish done, accept no more input or output */ | 71 finish_done /* finish done, accept no more input or output */ |
| 72 } block_state; | 72 } block_state; |
| 73 | 73 |
| 74 typedef block_state (*compress_func) OF((deflate_state *s, int flush)); | 74 typedef block_state (*compress_func) OF((deflate_state *s, int flush)); |
| 75 /* Compression function. Returns the block state after the call. */ | 75 /* Compression function. Returns the block state after the call. */ |
| 76 | 76 |
| 77 local int deflateStateCheck OF((z_streamp strm)); |
| 78 local void slide_hash OF((deflate_state *s)); |
| 77 local void fill_window OF((deflate_state *s)); | 79 local void fill_window OF((deflate_state *s)); |
| 78 local block_state deflate_stored OF((deflate_state *s, int flush)); | 80 local block_state deflate_stored OF((deflate_state *s, int flush)); |
| 79 local block_state deflate_fast OF((deflate_state *s, int flush)); | 81 local block_state deflate_fast OF((deflate_state *s, int flush)); |
| 80 #ifndef FASTEST | 82 #ifndef FASTEST |
| 81 local block_state deflate_slow OF((deflate_state *s, int flush)); | 83 local block_state deflate_slow OF((deflate_state *s, int flush)); |
| 82 #endif | 84 #endif |
| 83 local block_state deflate_rle OF((deflate_state *s, int flush)); | 85 local block_state deflate_rle OF((deflate_state *s, int flush)); |
| 84 local block_state deflate_huff OF((deflate_state *s, int flush)); | 86 local block_state deflate_huff OF((deflate_state *s, int flush)); |
| 85 local void lm_init OF((deflate_state *s)); | 87 local void lm_init OF((deflate_state *s)); |
| 86 local void putShortMSB OF((deflate_state *s, uInt b)); | 88 local void putShortMSB OF((deflate_state *s, uInt b)); |
| 87 local void flush_pending OF((z_streamp strm)); | 89 local void flush_pending OF((z_streamp strm)); |
| 88 | 90 unsigned ZLIB_INTERNAL read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); |
| 89 #ifdef ASMV | 91 #ifdef ASMV |
| 92 # pragma message("Assembler code may have bugs -- use at your own risk") |
| 90 void match_init OF((void)); /* asm code initialization */ | 93 void match_init OF((void)); /* asm code initialization */ |
| 91 uInt longest_match OF((deflate_state *s, IPos cur_match)); | 94 uInt longest_match OF((deflate_state *s, IPos cur_match)); |
| 92 #else | 95 #else |
| 93 local uInt longest_match OF((deflate_state *s, IPos cur_match)); | 96 local uInt longest_match OF((deflate_state *s, IPos cur_match)); |
| 94 #endif | 97 #endif |
| 95 | 98 |
| 96 #ifdef DEBUG | 99 #ifdef ZLIB_DEBUG |
| 97 local void check_match OF((deflate_state *s, IPos start, IPos match, | 100 local void check_match OF((deflate_state *s, IPos start, IPos match, |
| 98 int length)); | 101 int length)); |
| 99 #endif | 102 #endif |
| 100 | 103 |
| 101 /* For fill_window_sse.c to use */ | |
| 102 ZLIB_INTERNAL int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); | |
| 103 | |
| 104 /* From crc32.c */ | 104 /* From crc32.c */ |
| 105 extern void ZLIB_INTERNAL crc_reset(deflate_state *const s); | 105 extern void ZLIB_INTERNAL crc_reset(deflate_state *const s); |
| 106 extern void ZLIB_INTERNAL crc_finalize(deflate_state *const s); | 106 extern void ZLIB_INTERNAL crc_finalize(deflate_state *const s); |
| 107 extern void ZLIB_INTERNAL copy_with_crc(z_streamp strm, Bytef *dst, long size); | 107 extern void ZLIB_INTERNAL copy_with_crc(z_streamp strm, Bytef *dst, long size); |
| 108 | 108 |
| 109 #ifdef _MSC_VER | 109 #ifdef _MSC_VER |
| 110 #define INLINE __inline | 110 #define INLINE __inline |
| 111 #else | 111 #else |
| 112 #define INLINE inline | 112 #define INLINE inline |
| 113 #endif | 113 #endif |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 /* 7 */ {8, 32, 128, 256, deflate_slow}, | 159 /* 7 */ {8, 32, 128, 256, deflate_slow}, |
| 160 /* 8 */ {32, 128, 258, 1024, deflate_slow}, | 160 /* 8 */ {32, 128, 258, 1024, deflate_slow}, |
| 161 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ | 161 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ |
| 162 #endif | 162 #endif |
| 163 | 163 |
| 164 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 | 164 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 |
| 165 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different | 165 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different |
| 166 * meaning. | 166 * meaning. |
| 167 */ | 167 */ |
| 168 | 168 |
| 169 #define EQUAL 0 | |
| 170 /* result of memcmp for equal strings */ | |
| 171 | |
| 172 #ifndef NO_DUMMY_DECL | |
| 173 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ | |
| 174 #endif | |
| 175 | |
| 176 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ | 169 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ |
| 177 #define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0)) | 170 #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0)) |
| 178 | 171 |
| 179 /* =========================================================================== | 172 /* =========================================================================== |
| 180 * Update a hash value with the given input byte | 173 * Update a hash value with the given input byte |
| 181 * IN assertion: all calls to to UPDATE_HASH are made with consecutive | 174 * IN assertion: all calls to UPDATE_HASH are made with consecutive input |
| 182 * input characters, so that a running hash key can be computed from the | 175 * characters, so that a running hash key can be computed from the previous |
| 183 * previous key instead of complete recalculation each time. | 176 * key instead of complete recalculation each time. |
| 184 */ | 177 */ |
| 185 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) | 178 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) |
| 186 | 179 |
| 187 /* =========================================================================== | 180 /* =========================================================================== |
| 188 * Insert string str in the dictionary and set match_head to the previous head | 181 * Insert string str in the dictionary and set match_head to the previous head |
| 189 * of the hash chain (the most recent string with same hash key). Return | 182 * of the hash chain (the most recent string with same hash key). Return |
| 190 * the previous length of the hash chain. | 183 * the previous length of the hash chain. |
| 191 * If this file is compiled with -DFASTEST, the compression level is forced | 184 * If this file is compiled with -DFASTEST, the compression level is forced |
| 192 * to 1, and no hash chains are maintained. | 185 * to 1, and no hash chains are maintained. |
| 193 * IN assertion: all calls to to INSERT_STRING are made with consecutive | 186 * IN assertion: all calls to INSERT_STRING are made with consecutive input |
| 194 * input characters and the first MIN_MATCH bytes of str are valid | 187 * characters and the first MIN_MATCH bytes of str are valid (except for |
| 195 * (except for the last MIN_MATCH-1 bytes of the input file). | 188 * the last MIN_MATCH-1 bytes of the input file). |
| 196 */ | 189 */ |
| 197 local INLINE Pos insert_string_c(deflate_state *const s, const Pos str) | 190 local INLINE Pos insert_string_c(deflate_state *const s, const Pos str) |
| 198 { | 191 { |
| 199 Pos ret; | 192 Pos ret; |
| 200 | 193 |
| 201 UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]); | 194 UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]); |
| 202 #ifdef FASTEST | 195 #ifdef FASTEST |
| 203 ret = s->head[s->ins_h]; | 196 ret = s->head[s->ins_h]; |
| 204 #else | 197 #else |
| 205 ret = s->prev[str & s->w_mask] = s->head[s->ins_h]; | 198 ret = s->prev[str & s->w_mask] = s->head[s->ins_h]; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 218 | 211 |
| 219 | 212 |
| 220 /* =========================================================================== | 213 /* =========================================================================== |
| 221 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). | 214 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). |
| 222 * prev[] will be initialized on the fly. | 215 * prev[] will be initialized on the fly. |
| 223 */ | 216 */ |
| 224 #define CLEAR_HASH(s) \ | 217 #define CLEAR_HASH(s) \ |
| 225 s->head[s->hash_size-1] = NIL; \ | 218 s->head[s->hash_size-1] = NIL; \ |
| 226 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); | 219 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); |
| 227 | 220 |
| 221 /* =========================================================================== |
| 222 * Slide the hash table when sliding the window down (could be avoided with 32 |
| 223 * bit values at the expense of memory usage). We slide even when level == 0 to |
| 224 * keep the hash table consistent if we switch back to level > 0 later. |
| 225 */ |
| 226 local void slide_hash(s) |
| 227 deflate_state *s; |
| 228 { |
| 229 unsigned n, m; |
| 230 Posf *p; |
| 231 uInt wsize = s->w_size; |
| 232 |
| 233 n = s->hash_size; |
| 234 p = &s->head[n]; |
| 235 do { |
| 236 m = *--p; |
| 237 *p = (Pos)(m >= wsize ? m - wsize : NIL); |
| 238 } while (--n); |
| 239 n = wsize; |
| 240 #ifndef FASTEST |
| 241 p = &s->prev[n]; |
| 242 do { |
| 243 m = *--p; |
| 244 *p = (Pos)(m >= wsize ? m - wsize : NIL); |
| 245 /* If n is not on any hash chain, prev[n] is garbage but |
| 246 * its value will never be used. |
| 247 */ |
| 248 } while (--n); |
| 249 #endif |
| 250 } |
| 251 |
| 228 /* ========================================================================= */ | 252 /* ========================================================================= */ |
| 229 int ZEXPORT deflateInit_(strm, level, version, stream_size) | 253 int ZEXPORT deflateInit_(strm, level, version, stream_size) |
| 230 z_streamp strm; | 254 z_streamp strm; |
| 231 int level; | 255 int level; |
| 232 const char *version; | 256 const char *version; |
| 233 int stream_size; | 257 int stream_size; |
| 234 { | 258 { |
| 235 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, | 259 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, |
| 236 Z_DEFAULT_STRATEGY, version, stream_size); | 260 Z_DEFAULT_STRATEGY, version, stream_size); |
| 237 /* To do: ignore strm->next_in if we use it as window */ | 261 /* To do: ignore strm->next_in if we use it as window */ |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 windowBits = -windowBits; | 318 windowBits = -windowBits; |
| 295 } | 319 } |
| 296 #ifdef GZIP | 320 #ifdef GZIP |
| 297 else if (windowBits > 15) { | 321 else if (windowBits > 15) { |
| 298 wrap = 2; /* write gzip wrapper instead */ | 322 wrap = 2; /* write gzip wrapper instead */ |
| 299 windowBits -= 16; | 323 windowBits -= 16; |
| 300 } | 324 } |
| 301 #endif | 325 #endif |
| 302 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || | 326 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || |
| 303 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || | 327 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || |
| 304 strategy < 0 || strategy > Z_FIXED) { | 328 strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) { |
| 305 return Z_STREAM_ERROR; | 329 return Z_STREAM_ERROR; |
| 306 } | 330 } |
| 307 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ | 331 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ |
| 308 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); | 332 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); |
| 309 if (s == Z_NULL) return Z_MEM_ERROR; | 333 if (s == Z_NULL) return Z_MEM_ERROR; |
| 310 strm->state = (struct internal_state FAR *)s; | 334 strm->state = (struct internal_state FAR *)s; |
| 311 s->strm = strm; | 335 s->strm = strm; |
| 336 s->status = INIT_STATE; /* to pass state test in deflateReset() */ |
| 312 | 337 |
| 313 s->wrap = wrap; | 338 s->wrap = wrap; |
| 314 s->gzhead = Z_NULL; | 339 s->gzhead = Z_NULL; |
| 315 s->w_bits = windowBits; | 340 s->w_bits = (uInt)windowBits; |
| 316 s->w_size = 1 << s->w_bits; | 341 s->w_size = 1 << s->w_bits; |
| 317 s->w_mask = s->w_size - 1; | 342 s->w_mask = s->w_size - 1; |
| 318 | 343 |
| 319 if (x86_cpu_enable_simd) { | 344 if (x86_cpu_enable_simd) { |
| 320 s->hash_bits = 15; | 345 s->hash_bits = 15; |
| 321 } else { | 346 } else { |
| 322 s->hash_bits = memLevel + 7; | 347 s->hash_bits = memLevel + 7; |
| 323 } | 348 } |
| 324 | 349 |
| 325 s->hash_size = 1 << s->hash_bits; | 350 s->hash_size = 1 << s->hash_bits; |
| 326 s->hash_mask = s->hash_size - 1; | 351 s->hash_mask = s->hash_size - 1; |
| 327 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); | 352 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); |
| 328 | 353 |
| 329 s->window = (Bytef *) ZALLOC(strm, s->w_size + window_padding, 2*sizeof(Byte
)); | 354 s->window = (Bytef *) ZALLOC(strm, |
| 355 s->w_size + window_padding, |
| 356 2*sizeof(Byte)); |
| 330 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); | 357 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); |
| 331 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); | 358 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); |
| 332 | 359 |
| 333 s->high_water = 0; /* nothing written to s->window yet */ | 360 s->high_water = 0; /* nothing written to s->window yet */ |
| 334 | 361 |
| 335 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ | 362 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ |
| 336 | 363 |
| 337 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); | 364 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); |
| 338 s->pending_buf = (uchf *) overlay; | 365 s->pending_buf = (uchf *) overlay; |
| 339 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); | 366 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); |
| 340 | 367 |
| 341 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || | 368 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || |
| 342 s->pending_buf == Z_NULL) { | 369 s->pending_buf == Z_NULL) { |
| 343 s->status = FINISH_STATE; | 370 s->status = FINISH_STATE; |
| 344 strm->msg = ERR_MSG(Z_MEM_ERROR); | 371 strm->msg = ERR_MSG(Z_MEM_ERROR); |
| 345 deflateEnd (strm); | 372 deflateEnd (strm); |
| 346 return Z_MEM_ERROR; | 373 return Z_MEM_ERROR; |
| 347 } | 374 } |
| 348 s->d_buf = overlay + s->lit_bufsize/sizeof(ush); | 375 s->d_buf = overlay + s->lit_bufsize/sizeof(ush); |
| 349 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; | 376 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; |
| 350 | 377 |
| 351 s->level = level; | 378 s->level = level; |
| 352 s->strategy = strategy; | 379 s->strategy = strategy; |
| 353 s->method = (Byte)method; | 380 s->method = (Byte)method; |
| 354 | 381 |
| 355 return deflateReset(strm); | 382 return deflateReset(strm); |
| 356 } | 383 } |
| 357 | 384 |
| 385 /* ========================================================================= |
| 386 * Check for a valid deflate stream state. Return 0 if ok, 1 if not. |
| 387 */ |
| 388 local int deflateStateCheck (strm) |
| 389 z_streamp strm; |
| 390 { |
| 391 deflate_state *s; |
| 392 if (strm == Z_NULL || |
| 393 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) |
| 394 return 1; |
| 395 s = strm->state; |
| 396 if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE && |
| 397 #ifdef GZIP |
| 398 s->status != GZIP_STATE && |
| 399 #endif |
| 400 s->status != EXTRA_STATE && |
| 401 s->status != NAME_STATE && |
| 402 s->status != COMMENT_STATE && |
| 403 s->status != HCRC_STATE && |
| 404 s->status != BUSY_STATE && |
| 405 s->status != FINISH_STATE)) |
| 406 return 1; |
| 407 return 0; |
| 408 } |
| 409 |
| 358 /* ========================================================================= */ | 410 /* ========================================================================= */ |
| 359 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) | 411 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) |
| 360 z_streamp strm; | 412 z_streamp strm; |
| 361 const Bytef *dictionary; | 413 const Bytef *dictionary; |
| 362 uInt dictLength; | 414 uInt dictLength; |
| 363 { | 415 { |
| 364 deflate_state *s; | 416 deflate_state *s; |
| 365 uInt str, n; | 417 uInt str, n; |
| 366 int wrap; | 418 int wrap; |
| 367 unsigned avail; | 419 unsigned avail; |
| 368 z_const unsigned char *next; | 420 z_const unsigned char *next; |
| 369 | 421 |
| 370 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL) | 422 if (deflateStateCheck(strm) || dictionary == Z_NULL) |
| 371 return Z_STREAM_ERROR; | 423 return Z_STREAM_ERROR; |
| 372 s = strm->state; | 424 s = strm->state; |
| 373 wrap = s->wrap; | 425 wrap = s->wrap; |
| 374 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) | 426 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) |
| 375 return Z_STREAM_ERROR; | 427 return Z_STREAM_ERROR; |
| 376 | 428 |
| 377 /* when using zlib wrappers, compute Adler-32 for provided dictionary */ | 429 /* when using zlib wrappers, compute Adler-32 for provided dictionary */ |
| 378 if (wrap == 1) | 430 if (wrap == 1) |
| 379 strm->adler = adler32(strm->adler, dictionary, dictLength); | 431 strm->adler = adler32(strm->adler, dictionary, dictLength); |
| 380 s->wrap = 0; /* avoid computing Adler-32 in read_buf */ | 432 s->wrap = 0; /* avoid computing Adler-32 in read_buf */ |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 s->lookahead = 0; | 466 s->lookahead = 0; |
| 415 s->match_length = s->prev_length = MIN_MATCH-1; | 467 s->match_length = s->prev_length = MIN_MATCH-1; |
| 416 s->match_available = 0; | 468 s->match_available = 0; |
| 417 strm->next_in = next; | 469 strm->next_in = next; |
| 418 strm->avail_in = avail; | 470 strm->avail_in = avail; |
| 419 s->wrap = wrap; | 471 s->wrap = wrap; |
| 420 return Z_OK; | 472 return Z_OK; |
| 421 } | 473 } |
| 422 | 474 |
| 423 /* ========================================================================= */ | 475 /* ========================================================================= */ |
| 476 int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength) |
| 477 z_streamp strm; |
| 478 Bytef *dictionary; |
| 479 uInt *dictLength; |
| 480 { |
| 481 deflate_state *s; |
| 482 uInt len; |
| 483 |
| 484 if (deflateStateCheck(strm)) |
| 485 return Z_STREAM_ERROR; |
| 486 s = strm->state; |
| 487 len = s->strstart + s->lookahead; |
| 488 if (len > s->w_size) |
| 489 len = s->w_size; |
| 490 if (dictionary != Z_NULL && len) |
| 491 zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len); |
| 492 if (dictLength != Z_NULL) |
| 493 *dictLength = len; |
| 494 return Z_OK; |
| 495 } |
| 496 |
| 497 /* ========================================================================= */ |
| 424 int ZEXPORT deflateResetKeep (strm) | 498 int ZEXPORT deflateResetKeep (strm) |
| 425 z_streamp strm; | 499 z_streamp strm; |
| 426 { | 500 { |
| 427 deflate_state *s; | 501 deflate_state *s; |
| 428 | 502 |
| 429 if (strm == Z_NULL || strm->state == Z_NULL || | 503 if (deflateStateCheck(strm)) { |
| 430 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { | |
| 431 return Z_STREAM_ERROR; | 504 return Z_STREAM_ERROR; |
| 432 } | 505 } |
| 433 | 506 |
| 434 strm->total_in = strm->total_out = 0; | 507 strm->total_in = strm->total_out = 0; |
| 435 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ | 508 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ |
| 436 strm->data_type = Z_UNKNOWN; | 509 strm->data_type = Z_UNKNOWN; |
| 437 | 510 |
| 438 s = (deflate_state *)strm->state; | 511 s = (deflate_state *)strm->state; |
| 439 s->pending = 0; | 512 s->pending = 0; |
| 440 s->pending_out = s->pending_buf; | 513 s->pending_out = s->pending_buf; |
| 441 | 514 |
| 442 if (s->wrap < 0) { | 515 if (s->wrap < 0) { |
| 443 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ | 516 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ |
| 444 } | 517 } |
| 445 s->status = s->wrap ? INIT_STATE : BUSY_STATE; | 518 s->status = |
| 519 #ifdef GZIP |
| 520 s->wrap == 2 ? GZIP_STATE : |
| 521 #endif |
| 522 s->wrap ? INIT_STATE : BUSY_STATE; |
| 446 strm->adler = | 523 strm->adler = |
| 447 #ifdef GZIP | 524 #ifdef GZIP |
| 448 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : | 525 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : |
| 449 #endif | 526 #endif |
| 450 adler32(0L, Z_NULL, 0); | 527 adler32(0L, Z_NULL, 0); |
| 451 s->last_flush = Z_NO_FLUSH; | 528 s->last_flush = Z_NO_FLUSH; |
| 452 | 529 |
| 453 _tr_init(s); | 530 _tr_init(s); |
| 454 | 531 |
| 455 return Z_OK; | 532 return Z_OK; |
| 456 } | 533 } |
| 457 | 534 |
| 458 /* ========================================================================= */ | 535 /* ========================================================================= */ |
| 459 int ZEXPORT deflateReset (strm) | 536 int ZEXPORT deflateReset (strm) |
| 460 z_streamp strm; | 537 z_streamp strm; |
| 461 { | 538 { |
| 462 int ret; | 539 int ret; |
| 463 | 540 |
| 464 ret = deflateResetKeep(strm); | 541 ret = deflateResetKeep(strm); |
| 465 if (ret == Z_OK) | 542 if (ret == Z_OK) |
| 466 lm_init(strm->state); | 543 lm_init(strm->state); |
| 467 return ret; | 544 return ret; |
| 468 } | 545 } |
| 469 | 546 |
| 470 /* ========================================================================= */ | 547 /* ========================================================================= */ |
| 471 int ZEXPORT deflateSetHeader (strm, head) | 548 int ZEXPORT deflateSetHeader (strm, head) |
| 472 z_streamp strm; | 549 z_streamp strm; |
| 473 gz_headerp head; | 550 gz_headerp head; |
| 474 { | 551 { |
| 475 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 552 if (deflateStateCheck(strm) || strm->state->wrap != 2) |
| 476 if (strm->state->wrap != 2) return Z_STREAM_ERROR; | 553 return Z_STREAM_ERROR; |
| 477 strm->state->gzhead = head; | 554 strm->state->gzhead = head; |
| 478 return Z_OK; | 555 return Z_OK; |
| 479 } | 556 } |
| 480 | 557 |
| 481 /* ========================================================================= */ | 558 /* ========================================================================= */ |
| 482 int ZEXPORT deflatePending (strm, pending, bits) | 559 int ZEXPORT deflatePending (strm, pending, bits) |
| 483 unsigned *pending; | 560 unsigned *pending; |
| 484 int *bits; | 561 int *bits; |
| 485 z_streamp strm; | 562 z_streamp strm; |
| 486 { | 563 { |
| 487 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 564 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; |
| 488 if (pending != Z_NULL) | 565 if (pending != Z_NULL) |
| 489 *pending = strm->state->pending; | 566 *pending = strm->state->pending; |
| 490 if (bits != Z_NULL) | 567 if (bits != Z_NULL) |
| 491 *bits = strm->state->bi_valid; | 568 *bits = strm->state->bi_valid; |
| 492 return Z_OK; | 569 return Z_OK; |
| 493 } | 570 } |
| 494 | 571 |
| 495 /* ========================================================================= */ | 572 /* ========================================================================= */ |
| 496 int ZEXPORT deflatePrime (strm, bits, value) | 573 int ZEXPORT deflatePrime (strm, bits, value) |
| 497 z_streamp strm; | 574 z_streamp strm; |
| 498 int bits; | 575 int bits; |
| 499 int value; | 576 int value; |
| 500 { | 577 { |
| 501 deflate_state *s; | 578 deflate_state *s; |
| 502 int put; | 579 int put; |
| 503 | 580 |
| 504 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 581 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; |
| 505 s = strm->state; | 582 s = strm->state; |
| 506 if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) | 583 if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) |
| 507 return Z_BUF_ERROR; | 584 return Z_BUF_ERROR; |
| 508 do { | 585 do { |
| 509 put = Buf_size - s->bi_valid; | 586 put = Buf_size - s->bi_valid; |
| 510 if (put > bits) | 587 if (put > bits) |
| 511 put = bits; | 588 put = bits; |
| 512 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); | 589 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); |
| 513 s->bi_valid += put; | 590 s->bi_valid += put; |
| 514 _tr_flush_bits(s); | 591 _tr_flush_bits(s); |
| 515 value >>= put; | 592 value >>= put; |
| 516 bits -= put; | 593 bits -= put; |
| 517 } while (bits); | 594 } while (bits); |
| 518 return Z_OK; | 595 return Z_OK; |
| 519 } | 596 } |
| 520 | 597 |
| 521 /* ========================================================================= */ | 598 /* ========================================================================= */ |
| 522 int ZEXPORT deflateParams(strm, level, strategy) | 599 int ZEXPORT deflateParams(strm, level, strategy) |
| 523 z_streamp strm; | 600 z_streamp strm; |
| 524 int level; | 601 int level; |
| 525 int strategy; | 602 int strategy; |
| 526 { | 603 { |
| 527 deflate_state *s; | 604 deflate_state *s; |
| 528 compress_func func; | 605 compress_func func; |
| 529 int err = Z_OK; | |
| 530 | 606 |
| 531 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 607 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; |
| 532 s = strm->state; | 608 s = strm->state; |
| 533 | 609 |
| 534 #ifdef FASTEST | 610 #ifdef FASTEST |
| 535 if (level != 0) level = 1; | 611 if (level != 0) level = 1; |
| 536 #else | 612 #else |
| 537 if (level == Z_DEFAULT_COMPRESSION) level = 6; | 613 if (level == Z_DEFAULT_COMPRESSION) level = 6; |
| 538 #endif | 614 #endif |
| 539 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { | 615 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { |
| 540 return Z_STREAM_ERROR; | 616 return Z_STREAM_ERROR; |
| 541 } | 617 } |
| 542 func = configuration_table[s->level].func; | 618 func = configuration_table[s->level].func; |
| 543 | 619 |
| 544 if ((strategy != s->strategy || func != configuration_table[level].func) && | 620 if ((strategy != s->strategy || func != configuration_table[level].func) && |
| 545 strm->total_in != 0) { | 621 s->high_water) { |
| 546 /* Flush the last buffer: */ | 622 /* Flush the last buffer: */ |
| 547 err = deflate(strm, Z_BLOCK); | 623 int err = deflate(strm, Z_BLOCK); |
| 548 if (err == Z_BUF_ERROR && s->pending == 0) | 624 if (err == Z_STREAM_ERROR) |
| 549 err = Z_OK; | 625 return err; |
| 626 if (strm->avail_out == 0) |
| 627 return Z_BUF_ERROR; |
| 550 } | 628 } |
| 551 if (s->level != level) { | 629 if (s->level != level) { |
| 630 if (s->level == 0 && s->matches != 0) { |
| 631 if (s->matches == 1) |
| 632 slide_hash(s); |
| 633 else |
| 634 CLEAR_HASH(s); |
| 635 s->matches = 0; |
| 636 } |
| 552 s->level = level; | 637 s->level = level; |
| 553 s->max_lazy_match = configuration_table[level].max_lazy; | 638 s->max_lazy_match = configuration_table[level].max_lazy; |
| 554 s->good_match = configuration_table[level].good_length; | 639 s->good_match = configuration_table[level].good_length; |
| 555 s->nice_match = configuration_table[level].nice_length; | 640 s->nice_match = configuration_table[level].nice_length; |
| 556 s->max_chain_length = configuration_table[level].max_chain; | 641 s->max_chain_length = configuration_table[level].max_chain; |
| 557 } | 642 } |
| 558 s->strategy = strategy; | 643 s->strategy = strategy; |
| 559 return err; | 644 return Z_OK; |
| 560 } | 645 } |
| 561 | 646 |
| 562 /* ========================================================================= */ | 647 /* ========================================================================= */ |
| 563 int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) | 648 int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) |
| 564 z_streamp strm; | 649 z_streamp strm; |
| 565 int good_length; | 650 int good_length; |
| 566 int max_lazy; | 651 int max_lazy; |
| 567 int nice_length; | 652 int nice_length; |
| 568 int max_chain; | 653 int max_chain; |
| 569 { | 654 { |
| 570 deflate_state *s; | 655 deflate_state *s; |
| 571 | 656 |
| 572 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 657 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; |
| 573 s = strm->state; | 658 s = strm->state; |
| 574 s->good_match = good_length; | 659 s->good_match = (uInt)good_length; |
| 575 s->max_lazy_match = max_lazy; | 660 s->max_lazy_match = (uInt)max_lazy; |
| 576 s->nice_match = nice_length; | 661 s->nice_match = nice_length; |
| 577 s->max_chain_length = max_chain; | 662 s->max_chain_length = (uInt)max_chain; |
| 578 return Z_OK; | 663 return Z_OK; |
| 579 } | 664 } |
| 580 | 665 |
| 581 /* ========================================================================= | 666 /* ========================================================================= |
| 582 * For the default windowBits of 15 and memLevel of 8, this function returns | 667 * For the default windowBits of 15 and memLevel of 8, this function returns |
| 583 * a close to exact, as well as small, upper bound on the compressed size. | 668 * a close to exact, as well as small, upper bound on the compressed size. |
| 584 * They are coded as constants here for a reason--if the #define's are | 669 * They are coded as constants here for a reason--if the #define's are |
| 585 * changed, then this function needs to be changed as well. The return | 670 * changed, then this function needs to be changed as well. The return |
| 586 * value for 15 and 8 only works for those exact settings. | 671 * value for 15 and 8 only works for those exact settings. |
| 587 * | 672 * |
| 588 * For any setting other than those defaults for windowBits and memLevel, | 673 * For any setting other than those defaults for windowBits and memLevel, |
| 589 * the value returned is a conservative worst case for the maximum expansion | 674 * the value returned is a conservative worst case for the maximum expansion |
| 590 * resulting from using fixed blocks instead of stored blocks, which deflate | 675 * resulting from using fixed blocks instead of stored blocks, which deflate |
| 591 * can emit on compressed data for some combinations of the parameters. | 676 * can emit on compressed data for some combinations of the parameters. |
| 592 * | 677 * |
| 593 * This function could be more sophisticated to provide closer upper bounds for | 678 * This function could be more sophisticated to provide closer upper bounds for |
| 594 * every combination of windowBits and memLevel. But even the conservative | 679 * every combination of windowBits and memLevel. But even the conservative |
| 595 * upper bound of about 14% expansion does not seem onerous for output buffer | 680 * upper bound of about 14% expansion does not seem onerous for output buffer |
| 596 * allocation. | 681 * allocation. |
| 597 */ | 682 */ |
| 598 uLong ZEXPORT deflateBound(strm, sourceLen) | 683 uLong ZEXPORT deflateBound(strm, sourceLen) |
| 599 z_streamp strm; | 684 z_streamp strm; |
| 600 uLong sourceLen; | 685 uLong sourceLen; |
| 601 { | 686 { |
| 602 deflate_state *s; | 687 deflate_state *s; |
| 603 uLong complen, wraplen; | 688 uLong complen, wraplen; |
| 604 Bytef *str; | |
| 605 | 689 |
| 606 /* conservative upper bound for compressed data */ | 690 /* conservative upper bound for compressed data */ |
| 607 complen = sourceLen + | 691 complen = sourceLen + |
| 608 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; | 692 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; |
| 609 | 693 |
| 610 /* if can't get parameters, return conservative bound plus zlib wrapper */ | 694 /* if can't get parameters, return conservative bound plus zlib wrapper */ |
| 611 if (strm == Z_NULL || strm->state == Z_NULL) | 695 if (deflateStateCheck(strm)) |
| 612 return complen + 6; | 696 return complen + 6; |
| 613 | 697 |
| 614 /* compute wrapper length */ | 698 /* compute wrapper length */ |
| 615 s = strm->state; | 699 s = strm->state; |
| 616 switch (s->wrap) { | 700 switch (s->wrap) { |
| 617 case 0: /* raw deflate */ | 701 case 0: /* raw deflate */ |
| 618 wraplen = 0; | 702 wraplen = 0; |
| 619 break; | 703 break; |
| 620 case 1: /* zlib wrapper */ | 704 case 1: /* zlib wrapper */ |
| 621 wraplen = 6 + (s->strstart ? 4 : 0); | 705 wraplen = 6 + (s->strstart ? 4 : 0); |
| 622 break; | 706 break; |
| 707 #ifdef GZIP |
| 623 case 2: /* gzip wrapper */ | 708 case 2: /* gzip wrapper */ |
| 624 wraplen = 18; | 709 wraplen = 18; |
| 625 if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ | 710 if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ |
| 711 Bytef *str; |
| 626 if (s->gzhead->extra != Z_NULL) | 712 if (s->gzhead->extra != Z_NULL) |
| 627 wraplen += 2 + s->gzhead->extra_len; | 713 wraplen += 2 + s->gzhead->extra_len; |
| 628 str = s->gzhead->name; | 714 str = s->gzhead->name; |
| 629 if (str != Z_NULL) | 715 if (str != Z_NULL) |
| 630 do { | 716 do { |
| 631 wraplen++; | 717 wraplen++; |
| 632 } while (*str++); | 718 } while (*str++); |
| 633 str = s->gzhead->comment; | 719 str = s->gzhead->comment; |
| 634 if (str != Z_NULL) | 720 if (str != Z_NULL) |
| 635 do { | 721 do { |
| 636 wraplen++; | 722 wraplen++; |
| 637 } while (*str++); | 723 } while (*str++); |
| 638 if (s->gzhead->hcrc) | 724 if (s->gzhead->hcrc) |
| 639 wraplen += 2; | 725 wraplen += 2; |
| 640 } | 726 } |
| 641 break; | 727 break; |
| 728 #endif |
| 642 default: /* for compiler happiness */ | 729 default: /* for compiler happiness */ |
| 643 wraplen = 6; | 730 wraplen = 6; |
| 644 } | 731 } |
| 645 | 732 |
| 646 /* if not default parameters, return conservative bound */ | 733 /* if not default parameters, return conservative bound */ |
| 647 if (s->w_bits != 15 || s->hash_bits != 8 + 7) | 734 if (s->w_bits != 15 || s->hash_bits != 8 + 7) |
| 648 return complen + wraplen; | 735 return complen + wraplen; |
| 649 | 736 |
| 650 /* default settings: return tight bound for that case */ | 737 /* default settings: return tight bound for that case */ |
| 651 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + | 738 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + |
| 652 (sourceLen >> 25) + 13 - 6 + wraplen; | 739 (sourceLen >> 25) + 13 - 6 + wraplen; |
| 653 } | 740 } |
| 654 | 741 |
| 655 /* ========================================================================= | 742 /* ========================================================================= |
| 656 * Put a short in the pending buffer. The 16-bit value is put in MSB order. | 743 * Put a short in the pending buffer. The 16-bit value is put in MSB order. |
| 657 * IN assertion: the stream state is correct and there is enough room in | 744 * IN assertion: the stream state is correct and there is enough room in |
| 658 * pending_buf. | 745 * pending_buf. |
| 659 */ | 746 */ |
| 660 local void putShortMSB (s, b) | 747 local void putShortMSB (s, b) |
| 661 deflate_state *s; | 748 deflate_state *s; |
| 662 uInt b; | 749 uInt b; |
| 663 { | 750 { |
| 664 put_byte(s, (Byte)(b >> 8)); | 751 put_byte(s, (Byte)(b >> 8)); |
| 665 put_byte(s, (Byte)(b & 0xff)); | 752 put_byte(s, (Byte)(b & 0xff)); |
| 666 } | 753 } |
| 667 | 754 |
| 668 /* ========================================================================= | 755 /* ========================================================================= |
| 669 * Flush as much pending output as possible. All deflate() output goes | 756 * Flush as much pending output as possible. All deflate() output, except for |
| 670 * through this function so some applications may wish to modify it | 757 * some deflate_stored() output, goes through this function so some |
| 671 * to avoid allocating a large strm->next_out buffer and copying into it. | 758 * applications may wish to modify it to avoid allocating a large |
| 672 * (See also read_buf()). | 759 * strm->next_out buffer and copying into it. (See also read_buf()). |
| 673 */ | 760 */ |
| 674 local void flush_pending(strm) | 761 local void flush_pending(strm) |
| 675 z_streamp strm; | 762 z_streamp strm; |
| 676 { | 763 { |
| 677 unsigned len; | 764 unsigned len; |
| 678 deflate_state *s = strm->state; | 765 deflate_state *s = strm->state; |
| 679 | 766 |
| 680 _tr_flush_bits(s); | 767 _tr_flush_bits(s); |
| 681 len = s->pending; | 768 len = s->pending; |
| 682 if (len > strm->avail_out) len = strm->avail_out; | 769 if (len > strm->avail_out) len = strm->avail_out; |
| 683 if (len == 0) return; | 770 if (len == 0) return; |
| 684 | 771 |
| 685 zmemcpy(strm->next_out, s->pending_out, len); | 772 zmemcpy(strm->next_out, s->pending_out, len); |
| 686 strm->next_out += len; | 773 strm->next_out += len; |
| 687 s->pending_out += len; | 774 s->pending_out += len; |
| 688 strm->total_out += len; | 775 strm->total_out += len; |
| 689 strm->avail_out -= len; | 776 strm->avail_out -= len; |
| 690 s->pending -= len; | 777 s->pending -= len; |
| 691 if (s->pending == 0) { | 778 if (s->pending == 0) { |
| 692 s->pending_out = s->pending_buf; | 779 s->pending_out = s->pending_buf; |
| 693 } | 780 } |
| 694 } | 781 } |
| 695 | 782 |
| 783 /* =========================================================================== |
| 784 * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1]. |
| 785 */ |
| 786 #define HCRC_UPDATE(beg) \ |
| 787 do { \ |
| 788 if (s->gzhead->hcrc && s->pending > (beg)) \ |
| 789 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \ |
| 790 s->pending - (beg)); \ |
| 791 } while (0) |
| 792 |
| 696 /* ========================================================================= */ | 793 /* ========================================================================= */ |
| 697 int ZEXPORT deflate (strm, flush) | 794 int ZEXPORT deflate (strm, flush) |
| 698 z_streamp strm; | 795 z_streamp strm; |
| 699 int flush; | 796 int flush; |
| 700 { | 797 { |
| 701 int old_flush; /* value of flush param for previous deflate call */ | 798 int old_flush; /* value of flush param for previous deflate call */ |
| 702 deflate_state *s; | 799 deflate_state *s; |
| 703 | 800 |
| 704 if (strm == Z_NULL || strm->state == Z_NULL || | 801 if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) { |
| 705 flush > Z_BLOCK || flush < 0) { | |
| 706 return Z_STREAM_ERROR; | 802 return Z_STREAM_ERROR; |
| 707 } | 803 } |
| 708 s = strm->state; | 804 s = strm->state; |
| 709 | 805 |
| 710 if (strm->next_out == Z_NULL || | 806 if (strm->next_out == Z_NULL || |
| 711 (strm->next_in == Z_NULL && strm->avail_in != 0) || | 807 (strm->avail_in != 0 && strm->next_in == Z_NULL) || |
| 712 (s->status == FINISH_STATE && flush != Z_FINISH)) { | 808 (s->status == FINISH_STATE && flush != Z_FINISH)) { |
| 713 ERR_RETURN(strm, Z_STREAM_ERROR); | 809 ERR_RETURN(strm, Z_STREAM_ERROR); |
| 714 } | 810 } |
| 715 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); | 811 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); |
| 716 | 812 |
| 717 s->strm = strm; /* just in case */ | |
| 718 old_flush = s->last_flush; | 813 old_flush = s->last_flush; |
| 719 s->last_flush = flush; | 814 s->last_flush = flush; |
| 720 | 815 |
| 721 /* Write the header */ | |
| 722 if (s->status == INIT_STATE) { | |
| 723 #ifdef GZIP | |
| 724 if (s->wrap == 2) { | |
| 725 crc_reset(s); | |
| 726 put_byte(s, 31); | |
| 727 put_byte(s, 139); | |
| 728 put_byte(s, 8); | |
| 729 if (s->gzhead == Z_NULL) { | |
| 730 put_byte(s, 0); | |
| 731 put_byte(s, 0); | |
| 732 put_byte(s, 0); | |
| 733 put_byte(s, 0); | |
| 734 put_byte(s, 0); | |
| 735 put_byte(s, s->level == 9 ? 2 : | |
| 736 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? | |
| 737 4 : 0)); | |
| 738 put_byte(s, OS_CODE); | |
| 739 s->status = BUSY_STATE; | |
| 740 } | |
| 741 else { | |
| 742 put_byte(s, (s->gzhead->text ? 1 : 0) + | |
| 743 (s->gzhead->hcrc ? 2 : 0) + | |
| 744 (s->gzhead->extra == Z_NULL ? 0 : 4) + | |
| 745 (s->gzhead->name == Z_NULL ? 0 : 8) + | |
| 746 (s->gzhead->comment == Z_NULL ? 0 : 16) | |
| 747 ); | |
| 748 put_byte(s, (Byte)(s->gzhead->time & 0xff)); | |
| 749 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); | |
| 750 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); | |
| 751 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); | |
| 752 put_byte(s, s->level == 9 ? 2 : | |
| 753 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? | |
| 754 4 : 0)); | |
| 755 put_byte(s, s->gzhead->os & 0xff); | |
| 756 if (s->gzhead->extra != Z_NULL) { | |
| 757 put_byte(s, s->gzhead->extra_len & 0xff); | |
| 758 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); | |
| 759 } | |
| 760 if (s->gzhead->hcrc) | |
| 761 strm->adler = crc32(strm->adler, s->pending_buf, | |
| 762 s->pending); | |
| 763 s->gzindex = 0; | |
| 764 s->status = EXTRA_STATE; | |
| 765 } | |
| 766 } | |
| 767 else | |
| 768 #endif | |
| 769 { | |
| 770 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; | |
| 771 uInt level_flags; | |
| 772 | |
| 773 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) | |
| 774 level_flags = 0; | |
| 775 else if (s->level < 6) | |
| 776 level_flags = 1; | |
| 777 else if (s->level == 6) | |
| 778 level_flags = 2; | |
| 779 else | |
| 780 level_flags = 3; | |
| 781 header |= (level_flags << 6); | |
| 782 if (s->strstart != 0) header |= PRESET_DICT; | |
| 783 header += 31 - (header % 31); | |
| 784 | |
| 785 s->status = BUSY_STATE; | |
| 786 putShortMSB(s, header); | |
| 787 | |
| 788 /* Save the adler32 of the preset dictionary: */ | |
| 789 if (s->strstart != 0) { | |
| 790 putShortMSB(s, (uInt)(strm->adler >> 16)); | |
| 791 putShortMSB(s, (uInt)(strm->adler & 0xffff)); | |
| 792 } | |
| 793 strm->adler = adler32(0L, Z_NULL, 0); | |
| 794 } | |
| 795 } | |
| 796 #ifdef GZIP | |
| 797 if (s->status == EXTRA_STATE) { | |
| 798 if (s->gzhead->extra != Z_NULL) { | |
| 799 uInt beg = s->pending; /* start of bytes to update crc */ | |
| 800 | |
| 801 while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { | |
| 802 if (s->pending == s->pending_buf_size) { | |
| 803 if (s->gzhead->hcrc && s->pending > beg) | |
| 804 strm->adler = crc32(strm->adler, s->pending_buf + beg, | |
| 805 s->pending - beg); | |
| 806 flush_pending(strm); | |
| 807 beg = s->pending; | |
| 808 if (s->pending == s->pending_buf_size) | |
| 809 break; | |
| 810 } | |
| 811 put_byte(s, s->gzhead->extra[s->gzindex]); | |
| 812 s->gzindex++; | |
| 813 } | |
| 814 if (s->gzhead->hcrc && s->pending > beg) | |
| 815 strm->adler = crc32(strm->adler, s->pending_buf + beg, | |
| 816 s->pending - beg); | |
| 817 if (s->gzindex == s->gzhead->extra_len) { | |
| 818 s->gzindex = 0; | |
| 819 s->status = NAME_STATE; | |
| 820 } | |
| 821 } | |
| 822 else | |
| 823 s->status = NAME_STATE; | |
| 824 } | |
| 825 if (s->status == NAME_STATE) { | |
| 826 if (s->gzhead->name != Z_NULL) { | |
| 827 uInt beg = s->pending; /* start of bytes to update crc */ | |
| 828 int val; | |
| 829 | |
| 830 do { | |
| 831 if (s->pending == s->pending_buf_size) { | |
| 832 if (s->gzhead->hcrc && s->pending > beg) | |
| 833 strm->adler = crc32(strm->adler, s->pending_buf + beg, | |
| 834 s->pending - beg); | |
| 835 flush_pending(strm); | |
| 836 beg = s->pending; | |
| 837 if (s->pending == s->pending_buf_size) { | |
| 838 val = 1; | |
| 839 break; | |
| 840 } | |
| 841 } | |
| 842 val = s->gzhead->name[s->gzindex++]; | |
| 843 put_byte(s, val); | |
| 844 } while (val != 0); | |
| 845 if (s->gzhead->hcrc && s->pending > beg) | |
| 846 strm->adler = crc32(strm->adler, s->pending_buf + beg, | |
| 847 s->pending - beg); | |
| 848 if (val == 0) { | |
| 849 s->gzindex = 0; | |
| 850 s->status = COMMENT_STATE; | |
| 851 } | |
| 852 } | |
| 853 else | |
| 854 s->status = COMMENT_STATE; | |
| 855 } | |
| 856 if (s->status == COMMENT_STATE) { | |
| 857 if (s->gzhead->comment != Z_NULL) { | |
| 858 uInt beg = s->pending; /* start of bytes to update crc */ | |
| 859 int val; | |
| 860 | |
| 861 do { | |
| 862 if (s->pending == s->pending_buf_size) { | |
| 863 if (s->gzhead->hcrc && s->pending > beg) | |
| 864 strm->adler = crc32(strm->adler, s->pending_buf + beg, | |
| 865 s->pending - beg); | |
| 866 flush_pending(strm); | |
| 867 beg = s->pending; | |
| 868 if (s->pending == s->pending_buf_size) { | |
| 869 val = 1; | |
| 870 break; | |
| 871 } | |
| 872 } | |
| 873 val = s->gzhead->comment[s->gzindex++]; | |
| 874 put_byte(s, val); | |
| 875 } while (val != 0); | |
| 876 if (s->gzhead->hcrc && s->pending > beg) | |
| 877 strm->adler = crc32(strm->adler, s->pending_buf + beg, | |
| 878 s->pending - beg); | |
| 879 if (val == 0) | |
| 880 s->status = HCRC_STATE; | |
| 881 } | |
| 882 else | |
| 883 s->status = HCRC_STATE; | |
| 884 } | |
| 885 if (s->status == HCRC_STATE) { | |
| 886 if (s->gzhead->hcrc) { | |
| 887 if (s->pending + 2 > s->pending_buf_size) | |
| 888 flush_pending(strm); | |
| 889 if (s->pending + 2 <= s->pending_buf_size) { | |
| 890 put_byte(s, (Byte)(strm->adler & 0xff)); | |
| 891 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); | |
| 892 strm->adler = crc32(0L, Z_NULL, 0); | |
| 893 s->status = BUSY_STATE; | |
| 894 } | |
| 895 } | |
| 896 else | |
| 897 s->status = BUSY_STATE; | |
| 898 } | |
| 899 #endif | |
| 900 | |
| 901 /* Flush as much pending output as possible */ | 816 /* Flush as much pending output as possible */ |
| 902 if (s->pending != 0) { | 817 if (s->pending != 0) { |
| 903 flush_pending(strm); | 818 flush_pending(strm); |
| 904 if (strm->avail_out == 0) { | 819 if (strm->avail_out == 0) { |
| 905 /* Since avail_out is 0, deflate will be called again with | 820 /* Since avail_out is 0, deflate will be called again with |
| 906 * more output space, but possibly with both pending and | 821 * more output space, but possibly with both pending and |
| 907 * avail_in equal to zero. There won't be anything to do, | 822 * avail_in equal to zero. There won't be anything to do, |
| 908 * but this is not an error situation so make sure we | 823 * but this is not an error situation so make sure we |
| 909 * return OK instead of BUF_ERROR at next call of deflate: | 824 * return OK instead of BUF_ERROR at next call of deflate: |
| 910 */ | 825 */ |
| 911 s->last_flush = -1; | 826 s->last_flush = -1; |
| 912 return Z_OK; | 827 return Z_OK; |
| 913 } | 828 } |
| 914 | 829 |
| 915 /* Make sure there is something to do and avoid duplicate consecutive | 830 /* Make sure there is something to do and avoid duplicate consecutive |
| 916 * flushes. For repeated and useless calls with Z_FINISH, we keep | 831 * flushes. For repeated and useless calls with Z_FINISH, we keep |
| 917 * returning Z_STREAM_END instead of Z_BUF_ERROR. | 832 * returning Z_STREAM_END instead of Z_BUF_ERROR. |
| 918 */ | 833 */ |
| 919 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && | 834 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && |
| 920 flush != Z_FINISH) { | 835 flush != Z_FINISH) { |
| 921 ERR_RETURN(strm, Z_BUF_ERROR); | 836 ERR_RETURN(strm, Z_BUF_ERROR); |
| 922 } | 837 } |
| 923 | 838 |
| 924 /* User must not provide more input after the first FINISH: */ | 839 /* User must not provide more input after the first FINISH: */ |
| 925 if (s->status == FINISH_STATE && strm->avail_in != 0) { | 840 if (s->status == FINISH_STATE && strm->avail_in != 0) { |
| 926 ERR_RETURN(strm, Z_BUF_ERROR); | 841 ERR_RETURN(strm, Z_BUF_ERROR); |
| 927 } | 842 } |
| 928 | 843 |
| 844 /* Write the header */ |
| 845 if (s->status == INIT_STATE) { |
| 846 /* zlib header */ |
| 847 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; |
| 848 uInt level_flags; |
| 849 |
| 850 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) |
| 851 level_flags = 0; |
| 852 else if (s->level < 6) |
| 853 level_flags = 1; |
| 854 else if (s->level == 6) |
| 855 level_flags = 2; |
| 856 else |
| 857 level_flags = 3; |
| 858 header |= (level_flags << 6); |
| 859 if (s->strstart != 0) header |= PRESET_DICT; |
| 860 header += 31 - (header % 31); |
| 861 |
| 862 putShortMSB(s, header); |
| 863 |
| 864 /* Save the adler32 of the preset dictionary: */ |
| 865 if (s->strstart != 0) { |
| 866 putShortMSB(s, (uInt)(strm->adler >> 16)); |
| 867 putShortMSB(s, (uInt)(strm->adler & 0xffff)); |
| 868 } |
| 869 strm->adler = adler32(0L, Z_NULL, 0); |
| 870 s->status = BUSY_STATE; |
| 871 |
| 872 /* Compression must start with an empty pending buffer */ |
| 873 flush_pending(strm); |
| 874 if (s->pending != 0) { |
| 875 s->last_flush = -1; |
| 876 return Z_OK; |
| 877 } |
| 878 } |
| 879 #ifdef GZIP |
| 880 if (s->status == GZIP_STATE) { |
| 881 /* gzip header */ |
| 882 crc_reset(s); |
| 883 put_byte(s, 31); |
| 884 put_byte(s, 139); |
| 885 put_byte(s, 8); |
| 886 if (s->gzhead == Z_NULL) { |
| 887 put_byte(s, 0); |
| 888 put_byte(s, 0); |
| 889 put_byte(s, 0); |
| 890 put_byte(s, 0); |
| 891 put_byte(s, 0); |
| 892 put_byte(s, s->level == 9 ? 2 : |
| 893 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? |
| 894 4 : 0)); |
| 895 put_byte(s, OS_CODE); |
| 896 s->status = BUSY_STATE; |
| 897 |
| 898 /* Compression must start with an empty pending buffer */ |
| 899 flush_pending(strm); |
| 900 if (s->pending != 0) { |
| 901 s->last_flush = -1; |
| 902 return Z_OK; |
| 903 } |
| 904 } |
| 905 else { |
| 906 put_byte(s, (s->gzhead->text ? 1 : 0) + |
| 907 (s->gzhead->hcrc ? 2 : 0) + |
| 908 (s->gzhead->extra == Z_NULL ? 0 : 4) + |
| 909 (s->gzhead->name == Z_NULL ? 0 : 8) + |
| 910 (s->gzhead->comment == Z_NULL ? 0 : 16) |
| 911 ); |
| 912 put_byte(s, (Byte)(s->gzhead->time & 0xff)); |
| 913 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); |
| 914 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); |
| 915 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); |
| 916 put_byte(s, s->level == 9 ? 2 : |
| 917 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? |
| 918 4 : 0)); |
| 919 put_byte(s, s->gzhead->os & 0xff); |
| 920 if (s->gzhead->extra != Z_NULL) { |
| 921 put_byte(s, s->gzhead->extra_len & 0xff); |
| 922 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); |
| 923 } |
| 924 if (s->gzhead->hcrc) |
| 925 strm->adler = crc32(strm->adler, s->pending_buf, |
| 926 s->pending); |
| 927 s->gzindex = 0; |
| 928 s->status = EXTRA_STATE; |
| 929 } |
| 930 } |
| 931 if (s->status == EXTRA_STATE) { |
| 932 if (s->gzhead->extra != Z_NULL) { |
| 933 ulg beg = s->pending; /* start of bytes to update crc */ |
| 934 uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex; |
| 935 while (s->pending + left > s->pending_buf_size) { |
| 936 uInt copy = s->pending_buf_size - s->pending; |
| 937 zmemcpy(s->pending_buf + s->pending, |
| 938 s->gzhead->extra + s->gzindex, copy); |
| 939 s->pending = s->pending_buf_size; |
| 940 HCRC_UPDATE(beg); |
| 941 s->gzindex += copy; |
| 942 flush_pending(strm); |
| 943 if (s->pending != 0) { |
| 944 s->last_flush = -1; |
| 945 return Z_OK; |
| 946 } |
| 947 beg = 0; |
| 948 left -= copy; |
| 949 } |
| 950 zmemcpy(s->pending_buf + s->pending, |
| 951 s->gzhead->extra + s->gzindex, left); |
| 952 s->pending += left; |
| 953 HCRC_UPDATE(beg); |
| 954 s->gzindex = 0; |
| 955 } |
| 956 s->status = NAME_STATE; |
| 957 } |
| 958 if (s->status == NAME_STATE) { |
| 959 if (s->gzhead->name != Z_NULL) { |
| 960 ulg beg = s->pending; /* start of bytes to update crc */ |
| 961 int val; |
| 962 do { |
| 963 if (s->pending == s->pending_buf_size) { |
| 964 HCRC_UPDATE(beg); |
| 965 flush_pending(strm); |
| 966 if (s->pending != 0) { |
| 967 s->last_flush = -1; |
| 968 return Z_OK; |
| 969 } |
| 970 beg = 0; |
| 971 } |
| 972 val = s->gzhead->name[s->gzindex++]; |
| 973 put_byte(s, val); |
| 974 } while (val != 0); |
| 975 HCRC_UPDATE(beg); |
| 976 s->gzindex = 0; |
| 977 } |
| 978 s->status = COMMENT_STATE; |
| 979 } |
| 980 if (s->status == COMMENT_STATE) { |
| 981 if (s->gzhead->comment != Z_NULL) { |
| 982 ulg beg = s->pending; /* start of bytes to update crc */ |
| 983 int val; |
| 984 do { |
| 985 if (s->pending == s->pending_buf_size) { |
| 986 HCRC_UPDATE(beg); |
| 987 flush_pending(strm); |
| 988 if (s->pending != 0) { |
| 989 s->last_flush = -1; |
| 990 return Z_OK; |
| 991 } |
| 992 beg = 0; |
| 993 } |
| 994 val = s->gzhead->comment[s->gzindex++]; |
| 995 put_byte(s, val); |
| 996 } while (val != 0); |
| 997 HCRC_UPDATE(beg); |
| 998 } |
| 999 s->status = HCRC_STATE; |
| 1000 } |
| 1001 if (s->status == HCRC_STATE) { |
| 1002 if (s->gzhead->hcrc) { |
| 1003 if (s->pending + 2 > s->pending_buf_size) { |
| 1004 flush_pending(strm); |
| 1005 if (s->pending != 0) { |
| 1006 s->last_flush = -1; |
| 1007 return Z_OK; |
| 1008 } |
| 1009 } |
| 1010 put_byte(s, (Byte)(strm->adler & 0xff)); |
| 1011 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); |
| 1012 strm->adler = crc32(0L, Z_NULL, 0); |
| 1013 } |
| 1014 s->status = BUSY_STATE; |
| 1015 |
| 1016 /* Compression must start with an empty pending buffer */ |
| 1017 flush_pending(strm); |
| 1018 if (s->pending != 0) { |
| 1019 s->last_flush = -1; |
| 1020 return Z_OK; |
| 1021 } |
| 1022 } |
| 1023 #endif |
| 1024 |
| 929 /* Start a new block or continue the current one. | 1025 /* Start a new block or continue the current one. |
| 930 */ | 1026 */ |
| 931 if (strm->avail_in != 0 || s->lookahead != 0 || | 1027 if (strm->avail_in != 0 || s->lookahead != 0 || |
| 932 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { | 1028 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { |
| 933 block_state bstate; | 1029 block_state bstate; |
| 934 | 1030 |
| 935 bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : | 1031 bstate = s->level == 0 ? deflate_stored(s, flush) : |
| 936 (s->strategy == Z_RLE ? deflate_rle(s, flush) : | 1032 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : |
| 937 (*(configuration_table[s->level].func))(s, flush)); | 1033 s->strategy == Z_RLE ? deflate_rle(s, flush) : |
| 1034 (*(configuration_table[s->level].func))(s, flush); |
| 938 | 1035 |
| 939 if (bstate == finish_started || bstate == finish_done) { | 1036 if (bstate == finish_started || bstate == finish_done) { |
| 940 s->status = FINISH_STATE; | 1037 s->status = FINISH_STATE; |
| 941 } | 1038 } |
| 942 if (bstate == need_more || bstate == finish_started) { | 1039 if (bstate == need_more || bstate == finish_started) { |
| 943 if (strm->avail_out == 0) { | 1040 if (strm->avail_out == 0) { |
| 944 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ | 1041 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ |
| 945 } | 1042 } |
| 946 return Z_OK; | 1043 return Z_OK; |
| 947 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call | 1044 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call |
| (...skipping 21 matching lines...) Expand all Loading... |
| 969 } | 1066 } |
| 970 } | 1067 } |
| 971 } | 1068 } |
| 972 flush_pending(strm); | 1069 flush_pending(strm); |
| 973 if (strm->avail_out == 0) { | 1070 if (strm->avail_out == 0) { |
| 974 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ | 1071 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ |
| 975 return Z_OK; | 1072 return Z_OK; |
| 976 } | 1073 } |
| 977 } | 1074 } |
| 978 } | 1075 } |
| 979 Assert(strm->avail_out > 0, "bug2"); | |
| 980 | 1076 |
| 981 if (flush != Z_FINISH) return Z_OK; | 1077 if (flush != Z_FINISH) return Z_OK; |
| 982 if (s->wrap <= 0) return Z_STREAM_END; | 1078 if (s->wrap <= 0) return Z_STREAM_END; |
| 983 | 1079 |
| 984 /* Write the trailer */ | 1080 /* Write the trailer */ |
| 985 #ifdef GZIP | 1081 #ifdef GZIP |
| 986 if (s->wrap == 2) { | 1082 if (s->wrap == 2) { |
| 987 crc_finalize(s); | 1083 crc_finalize(s); |
| 988 put_byte(s, (Byte)(strm->adler & 0xff)); | 1084 put_byte(s, (Byte)(strm->adler & 0xff)); |
| 989 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); | 1085 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1007 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ | 1103 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ |
| 1008 return s->pending != 0 ? Z_OK : Z_STREAM_END; | 1104 return s->pending != 0 ? Z_OK : Z_STREAM_END; |
| 1009 } | 1105 } |
| 1010 | 1106 |
| 1011 /* ========================================================================= */ | 1107 /* ========================================================================= */ |
| 1012 int ZEXPORT deflateEnd (strm) | 1108 int ZEXPORT deflateEnd (strm) |
| 1013 z_streamp strm; | 1109 z_streamp strm; |
| 1014 { | 1110 { |
| 1015 int status; | 1111 int status; |
| 1016 | 1112 |
| 1017 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 1113 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; |
| 1018 | 1114 |
| 1019 status = strm->state->status; | 1115 status = strm->state->status; |
| 1020 if (status != INIT_STATE && | |
| 1021 status != EXTRA_STATE && | |
| 1022 status != NAME_STATE && | |
| 1023 status != COMMENT_STATE && | |
| 1024 status != HCRC_STATE && | |
| 1025 status != BUSY_STATE && | |
| 1026 status != FINISH_STATE) { | |
| 1027 return Z_STREAM_ERROR; | |
| 1028 } | |
| 1029 | 1116 |
| 1030 /* Deallocate in reverse order of allocations: */ | 1117 /* Deallocate in reverse order of allocations: */ |
| 1031 TRY_FREE(strm, strm->state->pending_buf); | 1118 TRY_FREE(strm, strm->state->pending_buf); |
| 1032 TRY_FREE(strm, strm->state->head); | 1119 TRY_FREE(strm, strm->state->head); |
| 1033 TRY_FREE(strm, strm->state->prev); | 1120 TRY_FREE(strm, strm->state->prev); |
| 1034 TRY_FREE(strm, strm->state->window); | 1121 TRY_FREE(strm, strm->state->window); |
| 1035 | 1122 |
| 1036 ZFREE(strm, strm->state); | 1123 ZFREE(strm, strm->state); |
| 1037 strm->state = Z_NULL; | 1124 strm->state = Z_NULL; |
| 1038 | 1125 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1049 z_streamp source; | 1136 z_streamp source; |
| 1050 { | 1137 { |
| 1051 #ifdef MAXSEG_64K | 1138 #ifdef MAXSEG_64K |
| 1052 return Z_STREAM_ERROR; | 1139 return Z_STREAM_ERROR; |
| 1053 #else | 1140 #else |
| 1054 deflate_state *ds; | 1141 deflate_state *ds; |
| 1055 deflate_state *ss; | 1142 deflate_state *ss; |
| 1056 ushf *overlay; | 1143 ushf *overlay; |
| 1057 | 1144 |
| 1058 | 1145 |
| 1059 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { | 1146 if (deflateStateCheck(source) || dest == Z_NULL) { |
| 1060 return Z_STREAM_ERROR; | 1147 return Z_STREAM_ERROR; |
| 1061 } | 1148 } |
| 1062 | 1149 |
| 1063 ss = source->state; | 1150 ss = source->state; |
| 1064 | 1151 |
| 1065 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); | 1152 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); |
| 1066 | 1153 |
| 1067 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); | 1154 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); |
| 1068 if (ds == Z_NULL) return Z_MEM_ERROR; | 1155 if (ds == Z_NULL) return Z_MEM_ERROR; |
| 1069 dest->state = (struct internal_state FAR *) ds; | 1156 dest->state = (struct internal_state FAR *) ds; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1099 #endif /* MAXSEG_64K */ | 1186 #endif /* MAXSEG_64K */ |
| 1100 } | 1187 } |
| 1101 | 1188 |
| 1102 /* =========================================================================== | 1189 /* =========================================================================== |
| 1103 * Read a new buffer from the current input stream, update the adler32 | 1190 * Read a new buffer from the current input stream, update the adler32 |
| 1104 * and total number of bytes read. All deflate() input goes through | 1191 * and total number of bytes read. All deflate() input goes through |
| 1105 * this function so some applications may wish to modify it to avoid | 1192 * this function so some applications may wish to modify it to avoid |
| 1106 * allocating a large strm->next_in buffer and copying from it. | 1193 * allocating a large strm->next_in buffer and copying from it. |
| 1107 * (See also flush_pending()). | 1194 * (See also flush_pending()). |
| 1108 */ | 1195 */ |
| 1109 ZLIB_INTERNAL int read_buf(strm, buf, size) | 1196 ZLIB_INTERNAL unsigned read_buf(strm, buf, size) |
| 1110 z_streamp strm; | 1197 z_streamp strm; |
| 1111 Bytef *buf; | 1198 Bytef *buf; |
| 1112 unsigned size; | 1199 unsigned size; |
| 1113 { | 1200 { |
| 1114 unsigned len = strm->avail_in; | 1201 unsigned len = strm->avail_in; |
| 1115 | 1202 |
| 1116 if (len > size) len = size; | 1203 if (len > size) len = size; |
| 1117 if (len == 0) return 0; | 1204 if (len == 0) return 0; |
| 1118 | 1205 |
| 1119 strm->avail_in -= len; | 1206 strm->avail_in -= len; |
| 1120 | 1207 |
| 1121 #ifdef GZIP | 1208 #ifdef GZIP |
| 1122 if (strm->state->wrap == 2) | 1209 if (strm->state->wrap == 2) |
| 1123 copy_with_crc(strm, buf, len); | 1210 copy_with_crc(strm, buf, len); |
| 1124 else | 1211 else |
| 1125 #endif | 1212 #endif |
| 1126 { | 1213 { |
| 1127 zmemcpy(buf, strm->next_in, len); | 1214 zmemcpy(buf, strm->next_in, len); |
| 1128 if (strm->state->wrap == 1) | 1215 if (strm->state->wrap == 1) |
| 1129 strm->adler = adler32(strm->adler, buf, len); | 1216 strm->adler = adler32(strm->adler, buf, len); |
| 1130 } | 1217 } |
| 1131 strm->next_in += len; | 1218 strm->next_in += len; |
| 1132 strm->total_in += len; | 1219 strm->total_in += len; |
| 1133 | 1220 |
| 1134 return (int)len; | 1221 return len; |
| 1135 } | 1222 } |
| 1136 | 1223 |
| 1137 /* =========================================================================== | 1224 /* =========================================================================== |
| 1138 * Initialize the "longest match" routines for a new zlib stream | 1225 * Initialize the "longest match" routines for a new zlib stream |
| 1139 */ | 1226 */ |
| 1140 local void lm_init (s) | 1227 local void lm_init (s) |
| 1141 deflate_state *s; | 1228 deflate_state *s; |
| 1142 { | 1229 { |
| 1143 s->window_size = (ulg)2L*s->w_size; | 1230 s->window_size = (ulg)2L*s->w_size; |
| 1144 | 1231 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1178 #ifndef ASMV | 1265 #ifndef ASMV |
| 1179 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or | 1266 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or |
| 1180 * match.S. The code will be functionally equivalent. | 1267 * match.S. The code will be functionally equivalent. |
| 1181 */ | 1268 */ |
| 1182 local uInt longest_match(s, cur_match) | 1269 local uInt longest_match(s, cur_match) |
| 1183 deflate_state *s; | 1270 deflate_state *s; |
| 1184 IPos cur_match; /* current match */ | 1271 IPos cur_match; /* current match */ |
| 1185 { | 1272 { |
| 1186 unsigned chain_length = s->max_chain_length;/* max hash chain length */ | 1273 unsigned chain_length = s->max_chain_length;/* max hash chain length */ |
| 1187 register Bytef *scan = s->window + s->strstart; /* current string */ | 1274 register Bytef *scan = s->window + s->strstart; /* current string */ |
| 1188 register Bytef *match; /* matched string */ | 1275 register Bytef *match; /* matched string */ |
| 1189 register int len; /* length of current match */ | 1276 register int len; /* length of current match */ |
| 1190 int best_len = s->prev_length; /* best match length so far */ | 1277 int best_len = (int)s->prev_length; /* best match length so far */ |
| 1191 int nice_match = s->nice_match; /* stop if match long enough */ | 1278 int nice_match = s->nice_match; /* stop if match long enough */ |
| 1192 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? | 1279 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? |
| 1193 s->strstart - (IPos)MAX_DIST(s) : NIL; | 1280 s->strstart - (IPos)MAX_DIST(s) : NIL; |
| 1194 /* Stop when cur_match becomes <= limit. To simplify the code, | 1281 /* Stop when cur_match becomes <= limit. To simplify the code, |
| 1195 * we prevent matches with the string of window index 0. | 1282 * we prevent matches with the string of window index 0. |
| 1196 */ | 1283 */ |
| 1197 Posf *prev = s->prev; | 1284 Posf *prev = s->prev; |
| 1198 uInt wmask = s->w_mask; | 1285 uInt wmask = s->w_mask; |
| 1199 | 1286 |
| 1200 #ifdef UNALIGNED_OK | 1287 #ifdef UNALIGNED_OK |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1215 */ | 1302 */ |
| 1216 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); | 1303 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); |
| 1217 | 1304 |
| 1218 /* Do not waste too much time if we already have a good match: */ | 1305 /* Do not waste too much time if we already have a good match: */ |
| 1219 if (s->prev_length >= s->good_match) { | 1306 if (s->prev_length >= s->good_match) { |
| 1220 chain_length >>= 2; | 1307 chain_length >>= 2; |
| 1221 } | 1308 } |
| 1222 /* Do not look for matches beyond the end of the input. This is necessary | 1309 /* Do not look for matches beyond the end of the input. This is necessary |
| 1223 * to make deflate deterministic. | 1310 * to make deflate deterministic. |
| 1224 */ | 1311 */ |
| 1225 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; | 1312 if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; |
| 1226 | 1313 |
| 1227 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); | 1314 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); |
| 1228 | 1315 |
| 1229 do { | 1316 do { |
| 1230 Assert(cur_match < s->strstart, "no future"); | 1317 Assert(cur_match < s->strstart, "no future"); |
| 1231 match = s->window + cur_match; | 1318 match = s->window + cur_match; |
| 1232 | 1319 |
| 1233 /* Skip to next match if the match length cannot increase | 1320 /* Skip to next match if the match length cannot increase |
| 1234 * or if the match length is less than 2. Note that the checks below | 1321 * or if the match length is less than 2. Note that the checks below |
| 1235 * for insufficient lookahead only occur occasionally for performance | 1322 * for insufficient lookahead only occur occasionally for performance |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1376 len = MAX_MATCH - (int)(strend - scan); | 1463 len = MAX_MATCH - (int)(strend - scan); |
| 1377 | 1464 |
| 1378 if (len < MIN_MATCH) return MIN_MATCH - 1; | 1465 if (len < MIN_MATCH) return MIN_MATCH - 1; |
| 1379 | 1466 |
| 1380 s->match_start = cur_match; | 1467 s->match_start = cur_match; |
| 1381 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; | 1468 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; |
| 1382 } | 1469 } |
| 1383 | 1470 |
| 1384 #endif /* FASTEST */ | 1471 #endif /* FASTEST */ |
| 1385 | 1472 |
| 1386 #ifdef DEBUG | 1473 #ifdef ZLIB_DEBUG |
| 1474 |
| 1475 #define EQUAL 0 |
| 1476 /* result of memcmp for equal strings */ |
| 1477 |
| 1387 /* =========================================================================== | 1478 /* =========================================================================== |
| 1388 * Check that the match at match_start is indeed a match. | 1479 * Check that the match at match_start is indeed a match. |
| 1389 */ | 1480 */ |
| 1390 local void check_match(s, start, match, length) | 1481 local void check_match(s, start, match, length) |
| 1391 deflate_state *s; | 1482 deflate_state *s; |
| 1392 IPos start, match; | 1483 IPos start, match; |
| 1393 int length; | 1484 int length; |
| 1394 { | 1485 { |
| 1395 /* check that the match is indeed a match */ | 1486 /* check that the match is indeed a match */ |
| 1396 if (zmemcmp(s->window + match, | 1487 if (zmemcmp(s->window + match, |
| 1397 s->window + start, length) != EQUAL) { | 1488 s->window + start, length) != EQUAL) { |
| 1398 fprintf(stderr, " start %u, match %u, length %d\n", | 1489 fprintf(stderr, " start %u, match %u, length %d\n", |
| 1399 start, match, length); | 1490 start, match, length); |
| 1400 do { | 1491 do { |
| 1401 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); | 1492 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); |
| 1402 } while (--length != 0); | 1493 } while (--length != 0); |
| 1403 z_error("invalid match"); | 1494 z_error("invalid match"); |
| 1404 } | 1495 } |
| 1405 if (z_verbose > 1) { | 1496 if (z_verbose > 1) { |
| 1406 fprintf(stderr,"\\[%d,%d]", start-match, length); | 1497 fprintf(stderr,"\\[%d,%d]", start-match, length); |
| 1407 do { putc(s->window[start++], stderr); } while (--length != 0); | 1498 do { putc(s->window[start++], stderr); } while (--length != 0); |
| 1408 } | 1499 } |
| 1409 } | 1500 } |
| 1410 #else | 1501 #else |
| 1411 # define check_match(s, start, match, length) | 1502 # define check_match(s, start, match, length) |
| 1412 #endif /* DEBUG */ | 1503 #endif /* ZLIB_DEBUG */ |
| 1413 | 1504 |
| 1414 /* =========================================================================== | 1505 /* =========================================================================== |
| 1415 * Fill the window when the lookahead becomes insufficient. | 1506 * Fill the window when the lookahead becomes insufficient. |
| 1416 * Updates strstart and lookahead. | 1507 * Updates strstart and lookahead. |
| 1417 * | 1508 * |
| 1418 * IN assertion: lookahead < MIN_LOOKAHEAD | 1509 * IN assertion: lookahead < MIN_LOOKAHEAD |
| 1419 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD | 1510 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD |
| 1420 * At least one byte has been read, or avail_in == 0; reads are | 1511 * At least one byte has been read, or avail_in == 0; reads are |
| 1421 * performed for at least two bytes (required for the zip translate_eol | 1512 * performed for at least two bytes (required for the zip translate_eol |
| 1422 * option -- not supported here). | 1513 * option -- not supported here). |
| 1423 */ | 1514 */ |
| 1424 local void fill_window_c(deflate_state *s); | 1515 local void fill_window_c(deflate_state *s); |
| 1425 | 1516 |
| 1426 local void fill_window(deflate_state *s) | 1517 local void fill_window(deflate_state *s) |
| 1427 { | 1518 { |
| 1428 if (x86_cpu_enable_simd) { | 1519 if (x86_cpu_enable_simd) { |
| 1429 fill_window_sse(s); | 1520 fill_window_sse(s); |
| 1430 return; | 1521 return; |
| 1431 } | 1522 } |
| 1432 | 1523 |
| 1433 fill_window_c(s); | 1524 fill_window_c(s); |
| 1434 } | 1525 } |
| 1435 | 1526 |
| 1436 local void fill_window_c(s) | 1527 local void fill_window_c(s) |
| 1437 deflate_state *s; | 1528 deflate_state *s; |
| 1438 { | 1529 { |
| 1439 register unsigned n, m; | 1530 unsigned n; |
| 1440 register Posf *p; | |
| 1441 unsigned more; /* Amount of free space at the end of the window. */ | 1531 unsigned more; /* Amount of free space at the end of the window. */ |
| 1442 uInt wsize = s->w_size; | 1532 uInt wsize = s->w_size; |
| 1443 | 1533 |
| 1444 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); | 1534 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); |
| 1445 | 1535 |
| 1446 do { | 1536 do { |
| 1447 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); | 1537 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); |
| 1448 | 1538 |
| 1449 /* Deal with !@#$% 64K limit: */ | 1539 /* Deal with !@#$% 64K limit: */ |
| 1450 if (sizeof(int) <= 2) { | 1540 if (sizeof(int) <= 2) { |
| 1451 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { | 1541 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { |
| 1452 more = wsize; | 1542 more = wsize; |
| 1453 | 1543 |
| 1454 } else if (more == (unsigned)(-1)) { | 1544 } else if (more == (unsigned)(-1)) { |
| 1455 /* Very unlikely, but possible on 16 bit machine if | 1545 /* Very unlikely, but possible on 16 bit machine if |
| 1456 * strstart == 0 && lookahead == 1 (input done a byte at time) | 1546 * strstart == 0 && lookahead == 1 (input done a byte at time) |
| 1457 */ | 1547 */ |
| 1458 more--; | 1548 more--; |
| 1459 } | 1549 } |
| 1460 } | 1550 } |
| 1461 | 1551 |
| 1462 /* If the window is almost full and there is insufficient lookahead, | 1552 /* If the window is almost full and there is insufficient lookahead, |
| 1463 * move the upper half to the lower one to make room in the upper half. | 1553 * move the upper half to the lower one to make room in the upper half. |
| 1464 */ | 1554 */ |
| 1465 if (s->strstart >= wsize+MAX_DIST(s)) { | 1555 if (s->strstart >= wsize+MAX_DIST(s)) { |
| 1466 | 1556 |
| 1467 zmemcpy(s->window, s->window+wsize, (unsigned)wsize); | 1557 zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more); |
| 1468 s->match_start -= wsize; | 1558 s->match_start -= wsize; |
| 1469 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ | 1559 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ |
| 1470 s->block_start -= (long) wsize; | 1560 s->block_start -= (long) wsize; |
| 1471 | 1561 slide_hash(s); |
| 1472 /* Slide the hash table (could be avoided with 32 bit values | |
| 1473 at the expense of memory usage). We slide even when level == 0 | |
| 1474 to keep the hash table consistent if we switch back to level > 0 | |
| 1475 later. (Using level 0 permanently is not an optimal usage of | |
| 1476 zlib, so we don't care about this pathological case.) | |
| 1477 */ | |
| 1478 n = s->hash_size; | |
| 1479 p = &s->head[n]; | |
| 1480 do { | |
| 1481 m = *--p; | |
| 1482 *p = (Pos)(m >= wsize ? m-wsize : NIL); | |
| 1483 } while (--n); | |
| 1484 | |
| 1485 n = wsize; | |
| 1486 #ifndef FASTEST | |
| 1487 p = &s->prev[n]; | |
| 1488 do { | |
| 1489 m = *--p; | |
| 1490 *p = (Pos)(m >= wsize ? m-wsize : NIL); | |
| 1491 /* If n is not on any hash chain, prev[n] is garbage but | |
| 1492 * its value will never be used. | |
| 1493 */ | |
| 1494 } while (--n); | |
| 1495 #endif | |
| 1496 more += wsize; | 1562 more += wsize; |
| 1497 } | 1563 } |
| 1498 if (s->strm->avail_in == 0) break; | 1564 if (s->strm->avail_in == 0) break; |
| 1499 | 1565 |
| 1500 /* If there was no sliding: | 1566 /* If there was no sliding: |
| 1501 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && | 1567 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && |
| 1502 * more == window_size - lookahead - strstart | 1568 * more == window_size - lookahead - strstart |
| 1503 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) | 1569 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) |
| 1504 * => more >= window_size - 2*WSIZE + 2 | 1570 * => more >= window_size - 2*WSIZE + 2 |
| 1505 * In the BIG_MEM or MMAP case (not yet supported), | 1571 * In the BIG_MEM or MMAP case (not yet supported), |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1591 flush_pending(s->strm); \ | 1657 flush_pending(s->strm); \ |
| 1592 Tracev((stderr,"[FLUSH]")); \ | 1658 Tracev((stderr,"[FLUSH]")); \ |
| 1593 } | 1659 } |
| 1594 | 1660 |
| 1595 /* Same but force premature exit if necessary. */ | 1661 /* Same but force premature exit if necessary. */ |
| 1596 #define FLUSH_BLOCK(s, last) { \ | 1662 #define FLUSH_BLOCK(s, last) { \ |
| 1597 FLUSH_BLOCK_ONLY(s, last); \ | 1663 FLUSH_BLOCK_ONLY(s, last); \ |
| 1598 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ | 1664 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ |
| 1599 } | 1665 } |
| 1600 | 1666 |
| 1667 /* Maximum stored block length in deflate format (not including header). */ |
| 1668 #define MAX_STORED 65535 |
| 1669 |
| 1670 /* Minimum of a and b. */ |
| 1671 #define MIN(a, b) ((a) > (b) ? (b) : (a)) |
| 1672 |
| 1601 /* =========================================================================== | 1673 /* =========================================================================== |
| 1602 * Copy without compression as much as possible from the input stream, return | 1674 * Copy without compression as much as possible from the input stream, return |
| 1603 * the current block state. | 1675 * the current block state. |
| 1604 * This function does not insert new strings in the dictionary since | 1676 * |
| 1605 * uncompressible data is probably not useful. This function is used | 1677 * In case deflateParams() is used to later switch to a non-zero compression |
| 1606 * only for the level=0 compression option. | 1678 * level, s->matches (otherwise unused when storing) keeps track of the number |
| 1607 * NOTE: this function should be optimized to avoid extra copying from | 1679 * of hash table slides to perform. If s->matches is 1, then one hash table |
| 1608 * window to pending_buf. | 1680 * slide will be done when switching. If s->matches is 2, the maximum value |
| 1681 * allowed here, then the hash table will be cleared, since two or more slides |
| 1682 * is the same as a clear. |
| 1683 * |
| 1684 * deflate_stored() is written to minimize the number of times an input byte is |
| 1685 * copied. It is most efficient with large input and output buffers, which |
| 1686 * maximizes the opportunites to have a single copy from next_in to next_out. |
| 1609 */ | 1687 */ |
| 1610 local block_state deflate_stored(s, flush) | 1688 local block_state deflate_stored(s, flush) |
| 1611 deflate_state *s; | 1689 deflate_state *s; |
| 1612 int flush; | 1690 int flush; |
| 1613 { | 1691 { |
| 1614 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited | 1692 /* Smallest worthy block size when not flushing or finishing. By default |
| 1615 * to pending_buf_size, and each stored block has a 5 byte header: | 1693 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For |
| 1694 * large input and output buffers, the stored block size will be larger. |
| 1616 */ | 1695 */ |
| 1617 ulg max_block_size = 0xffff; | 1696 unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size); |
| 1618 ulg max_start; | |
| 1619 | 1697 |
| 1620 if (max_block_size > s->pending_buf_size - 5) { | 1698 /* Copy as many min_block or larger stored blocks directly to next_out as |
| 1621 max_block_size = s->pending_buf_size - 5; | 1699 * possible. If flushing, copy the remaining available input to next_out as |
| 1700 * stored blocks, if there is enough space. |
| 1701 */ |
| 1702 unsigned len, left, have, last = 0; |
| 1703 unsigned used = s->strm->avail_in; |
| 1704 do { |
| 1705 /* Set len to the maximum size block that we can copy directly with the |
| 1706 * available input data and output space. Set left to how much of that |
| 1707 * would be copied from what's left in the window. |
| 1708 */ |
| 1709 len = MAX_STORED; /* maximum deflate stored block length */ |
| 1710 have = (s->bi_valid + 42) >> 3; /* number of header bytes */ |
| 1711 if (s->strm->avail_out < have) /* need room for header */ |
| 1712 break; |
| 1713 /* maximum stored block length that will fit in avail_out: */ |
| 1714 have = s->strm->avail_out - have; |
| 1715 left = s->strstart - s->block_start; /* bytes left in window */ |
| 1716 if (len > (ulg)left + s->strm->avail_in) |
| 1717 len = left + s->strm->avail_in; /* limit len to the input */ |
| 1718 if (len > have) |
| 1719 len = have; /* limit len to the output */ |
| 1720 |
| 1721 /* If the stored block would be less than min_block in length, or if |
| 1722 * unable to copy all of the available input when flushing, then try |
| 1723 * copying to the window and the pending buffer instead. Also don't |
| 1724 * write an empty block when flushing -- deflate() does that. |
| 1725 */ |
| 1726 if (len < min_block && ((len == 0 && flush != Z_FINISH) || |
| 1727 flush == Z_NO_FLUSH || |
| 1728 len != left + s->strm->avail_in)) |
| 1729 break; |
| 1730 |
| 1731 /* Make a dummy stored block in pending to get the header bytes, |
| 1732 * including any pending bits. This also updates the debugging counts. |
| 1733 */ |
| 1734 last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; |
| 1735 _tr_stored_block(s, (char *)0, 0L, last); |
| 1736 |
| 1737 /* Replace the lengths in the dummy stored block with len. */ |
| 1738 s->pending_buf[s->pending - 4] = len; |
| 1739 s->pending_buf[s->pending - 3] = len >> 8; |
| 1740 s->pending_buf[s->pending - 2] = ~len; |
| 1741 s->pending_buf[s->pending - 1] = ~len >> 8; |
| 1742 |
| 1743 /* Write the stored block header bytes. */ |
| 1744 flush_pending(s->strm); |
| 1745 |
| 1746 #ifdef ZLIB_DEBUG |
| 1747 /* Update debugging counts for the data about to be copied. */ |
| 1748 s->compressed_len += len << 3; |
| 1749 s->bits_sent += len << 3; |
| 1750 #endif |
| 1751 |
| 1752 /* Copy uncompressed bytes from the window to next_out. */ |
| 1753 if (left) { |
| 1754 if (left > len) |
| 1755 left = len; |
| 1756 zmemcpy(s->strm->next_out, s->window + s->block_start, left); |
| 1757 s->strm->next_out += left; |
| 1758 s->strm->avail_out -= left; |
| 1759 s->strm->total_out += left; |
| 1760 s->block_start += left; |
| 1761 len -= left; |
| 1762 } |
| 1763 |
| 1764 /* Copy uncompressed bytes directly from next_in to next_out, updating |
| 1765 * the check value. |
| 1766 */ |
| 1767 if (len) { |
| 1768 read_buf(s->strm, s->strm->next_out, len); |
| 1769 s->strm->next_out += len; |
| 1770 s->strm->avail_out -= len; |
| 1771 s->strm->total_out += len; |
| 1772 } |
| 1773 } while (last == 0); |
| 1774 |
| 1775 /* Update the sliding window with the last s->w_size bytes of the copied |
| 1776 * data, or append all of the copied data to the existing window if less |
| 1777 * than s->w_size bytes were copied. Also update the number of bytes to |
| 1778 * insert in the hash tables, in the event that deflateParams() switches to |
| 1779 * a non-zero compression level. |
| 1780 */ |
| 1781 used -= s->strm->avail_in; /* number of input bytes directly copied */ |
| 1782 if (used) { |
| 1783 /* If any input was used, then no unused input remains in the window, |
| 1784 * therefore s->block_start == s->strstart. |
| 1785 */ |
| 1786 if (used >= s->w_size) { /* supplant the previous history */ |
| 1787 s->matches = 2; /* clear hash */ |
| 1788 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); |
| 1789 s->strstart = s->w_size; |
| 1790 } |
| 1791 else { |
| 1792 if (s->window_size - s->strstart <= used) { |
| 1793 /* Slide the window down. */ |
| 1794 s->strstart -= s->w_size; |
| 1795 zmemcpy(s->window, s->window + s->w_size, s->strstart); |
| 1796 if (s->matches < 2) |
| 1797 s->matches++; /* add a pending slide_hash() */ |
| 1798 } |
| 1799 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); |
| 1800 s->strstart += used; |
| 1801 } |
| 1802 s->block_start = s->strstart; |
| 1803 s->insert += MIN(used, s->w_size - s->insert); |
| 1804 } |
| 1805 if (s->high_water < s->strstart) |
| 1806 s->high_water = s->strstart; |
| 1807 |
| 1808 /* If the last block was written to next_out, then done. */ |
| 1809 if (last) |
| 1810 return finish_done; |
| 1811 |
| 1812 /* If flushing and all input has been consumed, then done. */ |
| 1813 if (flush != Z_NO_FLUSH && flush != Z_FINISH && |
| 1814 s->strm->avail_in == 0 && (long)s->strstart == s->block_start) |
| 1815 return block_done; |
| 1816 |
| 1817 /* Fill the window with any remaining input. */ |
| 1818 have = s->window_size - s->strstart - 1; |
| 1819 if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { |
| 1820 /* Slide the window down. */ |
| 1821 s->block_start -= s->w_size; |
| 1822 s->strstart -= s->w_size; |
| 1823 zmemcpy(s->window, s->window + s->w_size, s->strstart); |
| 1824 if (s->matches < 2) |
| 1825 s->matches++; /* add a pending slide_hash() */ |
| 1826 have += s->w_size; /* more space now */ |
| 1827 } |
| 1828 if (have > s->strm->avail_in) |
| 1829 have = s->strm->avail_in; |
| 1830 if (have) { |
| 1831 read_buf(s->strm, s->window + s->strstart, have); |
| 1832 s->strstart += have; |
| 1833 } |
| 1834 if (s->high_water < s->strstart) |
| 1835 s->high_water = s->strstart; |
| 1836 |
| 1837 /* There was not enough avail_out to write a complete worthy or flushed |
| 1838 * stored block to next_out. Write a stored block to pending instead, if we |
| 1839 * have enough input for a worthy block, or if flushing and there is enough |
| 1840 * room for the remaining input as a stored block in the pending buffer. |
| 1841 */ |
| 1842 have = (s->bi_valid + 42) >> 3; /* number of header bytes */ |
| 1843 /* maximum stored block length that will fit in pending: */ |
| 1844 have = MIN(s->pending_buf_size - have, MAX_STORED); |
| 1845 min_block = MIN(have, s->w_size); |
| 1846 left = s->strstart - s->block_start; |
| 1847 if (left >= min_block || |
| 1848 ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && |
| 1849 s->strm->avail_in == 0 && left <= have)) { |
| 1850 len = MIN(left, have); |
| 1851 last = flush == Z_FINISH && s->strm->avail_in == 0 && |
| 1852 len == left ? 1 : 0; |
| 1853 _tr_stored_block(s, (charf *)s->window + s->block_start, len, last); |
| 1854 s->block_start += len; |
| 1855 flush_pending(s->strm); |
| 1622 } | 1856 } |
| 1623 | 1857 |
| 1624 /* Copy as much as possible from input to output: */ | 1858 /* We've done all we can with the available input and output. */ |
| 1625 for (;;) { | 1859 return last ? finish_started : need_more; |
| 1626 /* Fill the window as much as possible: */ | |
| 1627 if (s->lookahead <= 1) { | |
| 1628 | |
| 1629 Assert(s->strstart < s->w_size+MAX_DIST(s) || | |
| 1630 s->block_start >= (long)s->w_size, "slide too late"); | |
| 1631 | |
| 1632 fill_window(s); | |
| 1633 if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more; | |
| 1634 | |
| 1635 if (s->lookahead == 0) break; /* flush the current block */ | |
| 1636 } | |
| 1637 Assert(s->block_start >= 0L, "block gone"); | |
| 1638 | |
| 1639 s->strstart += s->lookahead; | |
| 1640 s->lookahead = 0; | |
| 1641 | |
| 1642 /* Emit a stored block if pending_buf will be full: */ | |
| 1643 max_start = s->block_start + max_block_size; | |
| 1644 if (s->strstart == 0 || (ulg)s->strstart >= max_start) { | |
| 1645 /* strstart == 0 is possible when wraparound on 16-bit machine */ | |
| 1646 s->lookahead = (uInt)(s->strstart - max_start); | |
| 1647 s->strstart = (uInt)max_start; | |
| 1648 FLUSH_BLOCK(s, 0); | |
| 1649 } | |
| 1650 /* Flush if we may have to slide, otherwise block_start may become | |
| 1651 * negative and the data will be gone: | |
| 1652 */ | |
| 1653 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { | |
| 1654 FLUSH_BLOCK(s, 0); | |
| 1655 } | |
| 1656 } | |
| 1657 s->insert = 0; | |
| 1658 if (flush == Z_FINISH) { | |
| 1659 FLUSH_BLOCK(s, 1); | |
| 1660 return finish_done; | |
| 1661 } | |
| 1662 if ((long)s->strstart > s->block_start) | |
| 1663 FLUSH_BLOCK(s, 0); | |
| 1664 return block_done; | |
| 1665 } | 1860 } |
| 1666 | 1861 |
| 1667 /* =========================================================================== | 1862 /* =========================================================================== |
| 1668 * Compress as much as possible from the input stream, return the current | 1863 * Compress as much as possible from the input stream, return the current |
| 1669 * block state. | 1864 * block state. |
| 1670 * This function does not perform lazy evaluation of matches and inserts | 1865 * This function does not perform lazy evaluation of matches and inserts |
| 1671 * new strings in the dictionary only for unmatched strings or for short | 1866 * new strings in the dictionary only for unmatched strings or for short |
| 1672 * matches. It is used only for the fast compression options. | 1867 * matches. It is used only for the fast compression options. |
| 1673 */ | 1868 */ |
| 1674 local block_state deflate_fast(s, flush) | 1869 local block_state deflate_fast(s, flush) |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1931 scan = s->window + s->strstart - 1; | 2126 scan = s->window + s->strstart - 1; |
| 1932 prev = *scan; | 2127 prev = *scan; |
| 1933 if (prev == *++scan && prev == *++scan && prev == *++scan) { | 2128 if (prev == *++scan && prev == *++scan && prev == *++scan) { |
| 1934 strend = s->window + s->strstart + MAX_MATCH; | 2129 strend = s->window + s->strstart + MAX_MATCH; |
| 1935 do { | 2130 do { |
| 1936 } while (prev == *++scan && prev == *++scan && | 2131 } while (prev == *++scan && prev == *++scan && |
| 1937 prev == *++scan && prev == *++scan && | 2132 prev == *++scan && prev == *++scan && |
| 1938 prev == *++scan && prev == *++scan && | 2133 prev == *++scan && prev == *++scan && |
| 1939 prev == *++scan && prev == *++scan && | 2134 prev == *++scan && prev == *++scan && |
| 1940 scan < strend); | 2135 scan < strend); |
| 1941 s->match_length = MAX_MATCH - (int)(strend - scan); | 2136 s->match_length = MAX_MATCH - (uInt)(strend - scan); |
| 1942 if (s->match_length > s->lookahead) | 2137 if (s->match_length > s->lookahead) |
| 1943 s->match_length = s->lookahead; | 2138 s->match_length = s->lookahead; |
| 1944 } | 2139 } |
| 1945 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); | 2140 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); |
| 1946 } | 2141 } |
| 1947 | 2142 |
| 1948 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ | 2143 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ |
| 1949 if (s->match_length >= MIN_MATCH) { | 2144 if (s->match_length >= MIN_MATCH) { |
| 1950 check_match(s, s->strstart, s->strstart - 1, s->match_length); | 2145 check_match(s, s->strstart, s->strstart - 1, s->match_length); |
| 1951 | 2146 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2038 #else | 2233 #else |
| 2039 /* This should never happen */ | 2234 /* This should never happen */ |
| 2040 assert(0); | 2235 assert(0); |
| 2041 #endif | 2236 #endif |
| 2042 | 2237 |
| 2043 ret = s->head[h & s->hash_mask]; | 2238 ret = s->head[h & s->hash_mask]; |
| 2044 s->head[h & s->hash_mask] = str; | 2239 s->head[h & s->hash_mask] = str; |
| 2045 s->prev[str & s->w_mask] = ret; | 2240 s->prev[str & s->w_mask] = ret; |
| 2046 return ret; | 2241 return ret; |
| 2047 } | 2242 } |
| OLD | NEW |