diff options
Diffstat (limited to 'tinySIGCOMP/src/deflate.c')
-rwxr-xr-x | tinySIGCOMP/src/deflate.c | 580 |
1 files changed, 357 insertions, 223 deletions
diff --git a/tinySIGCOMP/src/deflate.c b/tinySIGCOMP/src/deflate.c index a3c208e..feffb39 100755 --- a/tinySIGCOMP/src/deflate.c +++ b/tinySIGCOMP/src/deflate.c @@ -53,7 +53,7 @@ #include "deflate.h" const char deflate_copyright[] = - " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly "; + " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly "; /* If you use the zlib library in a product, an acknowledgment is welcome in the documentation of your product. If for some reason you cannot @@ -86,8 +86,8 @@ local void flush_pending OF((z_streamp strm)); local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); #ifndef FASTEST #ifdef ASMV - void match_init OF((void)); /* asm code initialization */ - uInt longest_match OF((deflate_state *s, IPos cur_match)); +void match_init OF((void)); /* asm code initialization */ +uInt longest_match OF((deflate_state *s, IPos cur_match)); #else local uInt longest_match OF((deflate_state *s, IPos cur_match)); #endif @@ -122,32 +122,34 @@ local void check_match OF((deflate_state *s, IPos start, IPos match, * found for specific files. */ typedef struct config_s { - ush good_length; /* reduce lazy search above this match length */ - ush max_lazy; /* do not perform lazy search above this match length */ - ush nice_length; /* quit search above this match length */ - ush max_chain; - compress_func func; + ush good_length; /* reduce lazy search above this match length */ + ush max_lazy; /* do not perform lazy search above this match length */ + ush nice_length; /* quit search above this match length */ + ush max_chain; + compress_func func; } config; #ifdef FASTEST local const config configuration_table[2] = { -/* good lazy nice chain */ -/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ -/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ + /* good lazy nice chain */ + /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ + /* 1 */ {4, 4, 8, 4, deflate_fast} +}; /* max speed, no lazy matches */ #else local const config configuration_table[10] = { -/* good lazy nice chain */ -/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ -/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ -/* 2 */ {4, 5, 16, 8, deflate_fast}, -/* 3 */ {4, 6, 32, 32, deflate_fast}, - -/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ -/* 5 */ {8, 16, 32, 32, deflate_slow}, -/* 6 */ {8, 16, 128, 128, deflate_slow}, -/* 7 */ {8, 32, 128, 256, deflate_slow}, -/* 8 */ {32, 128, 258, 1024, deflate_slow}, -/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ + /* good lazy nice chain */ + /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ + /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ + /* 2 */ {4, 5, 16, 8, deflate_fast}, + /* 3 */ {4, 6, 32, 32, deflate_fast}, + + /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ + /* 5 */ {8, 16, 32, 32, deflate_slow}, + /* 6 */ {8, 16, 128, 128, deflate_slow}, + /* 7 */ {8, 32, 128, 256, deflate_slow}, + /* 8 */ {32, 128, 258, 1024, deflate_slow}, + /* 9 */ {32, 258, 258, 4096, deflate_slow} +}; /* max compression */ #endif /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 @@ -159,7 +161,9 @@ local const config configuration_table[10] = { /* result of memcmp for equal strings */ #ifndef NO_DUMMY_DECL -struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ +struct static_tree_desc_s { + int dummy; +}; /* for buggy compilers */ #endif /* =========================================================================== @@ -203,10 +207,10 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ /* ========================================================================= */ int ZEXPORT deflateInit_(strm, level, version, stream_size) - z_streamp strm; - int level; - const char *version; - int stream_size; +z_streamp strm; +int level; +const char *version; +int stream_size; { return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size); @@ -215,15 +219,15 @@ int ZEXPORT deflateInit_(strm, level, version, stream_size) /* ========================================================================= */ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, - version, stream_size) - z_streamp strm; - int level; - int method; - int windowBits; - int memLevel; - int strategy; - const char *version; - int stream_size; + version, stream_size) +z_streamp strm; +int level; +int method; +int windowBits; +int memLevel; +int strategy; +const char *version; +int stream_size; { deflate_state *s; int wrap = 1; @@ -235,22 +239,30 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, */ if (version == Z_NULL || version[0] != my_version[0] || - stream_size != sizeof(z_stream)) { + stream_size != sizeof(z_stream)) { return Z_VERSION_ERROR; } - if (strm == Z_NULL) return Z_STREAM_ERROR; + if (strm == Z_NULL) { + return Z_STREAM_ERROR; + } strm->msg = Z_NULL; if (strm->zalloc == (alloc_func)0) { strm->zalloc = zcalloc; strm->opaque = (voidpf)0; } - if (strm->zfree == (free_func)0) strm->zfree = zcfree; + if (strm->zfree == (free_func)0) { + strm->zfree = zcfree; + } #ifdef FASTEST - if (level != 0) level = 1; + if (level != 0) { + level = 1; + } #else - if (level == Z_DEFAULT_COMPRESSION) level = 6; + if (level == Z_DEFAULT_COMPRESSION) { + level = 6; + } #endif if (windowBits < 0) { /* suppress zlib wrapper */ @@ -264,13 +276,17 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, } #endif if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || - windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || - strategy < 0 || strategy > Z_FIXED) { + windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || + strategy < 0 || strategy > Z_FIXED) { return Z_STREAM_ERROR; } - if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ + if (windowBits == 8) { + windowBits = 9; /* until 256-byte window bug fixed */ + } s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); - if (s == Z_NULL) return Z_MEM_ERROR; + if (s == Z_NULL) { + return Z_MEM_ERROR; + } strm->state = (struct internal_state FAR *)s; s->strm = strm; @@ -296,7 +312,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || - s->pending_buf == Z_NULL) { + s->pending_buf == Z_NULL) { s->status = FINISH_STATE; strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); deflateEnd (strm); @@ -314,9 +330,9 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, /* ========================================================================= */ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) - z_streamp strm; - const Bytef *dictionary; - uInt dictLength; +z_streamp strm; +const Bytef *dictionary; +uInt dictLength; { deflate_state *s; uInt length = dictLength; @@ -324,15 +340,19 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) IPos hash_head = 0; if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || - strm->state->wrap == 2 || - (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) + strm->state->wrap == 2 || + (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) { return Z_STREAM_ERROR; + } s = strm->state; - if (s->wrap) + if (s->wrap) { strm->adler = adler32(strm->adler, dictionary, dictLength); + } - if (length < MIN_MATCH) return Z_OK; + if (length < MIN_MATCH) { + return Z_OK; + } if (length > MAX_DIST(s)) { length = MAX_DIST(s); dictionary += dictLength - length; /* use the tail of the dictionary */ @@ -350,18 +370,20 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) for (n = 0; n <= length - MIN_MATCH; n++) { INSERT_STRING(s, n, hash_head); } - if (hash_head) hash_head = 0; /* to make compiler happy */ + if (hash_head) { + hash_head = 0; /* to make compiler happy */ + } return Z_OK; } /* ========================================================================= */ int ZEXPORT deflateReset (strm) - z_streamp strm; +z_streamp strm; { deflate_state *s; if (strm == Z_NULL || strm->state == Z_NULL || - strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { + strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { return Z_STREAM_ERROR; } @@ -392,22 +414,28 @@ int ZEXPORT deflateReset (strm) /* ========================================================================= */ int ZEXPORT deflateSetHeader (strm, head) - z_streamp strm; - gz_headerp head; +z_streamp strm; +gz_headerp head; { - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; - if (strm->state->wrap != 2) return Z_STREAM_ERROR; + if (strm == Z_NULL || strm->state == Z_NULL) { + return Z_STREAM_ERROR; + } + if (strm->state->wrap != 2) { + return Z_STREAM_ERROR; + } strm->state->gzhead = head; return Z_OK; } /* ========================================================================= */ int ZEXPORT deflatePrime (strm, bits, value) - z_streamp strm; - int bits; - int value; +z_streamp strm; +int bits; +int value; { - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (strm == Z_NULL || strm->state == Z_NULL) { + return Z_STREAM_ERROR; + } strm->state->bi_valid = bits; strm->state->bi_buf = (ush)(value & ((1 << bits) - 1)); return Z_OK; @@ -415,21 +443,27 @@ int ZEXPORT deflatePrime (strm, bits, value) /* ========================================================================= */ int ZEXPORT deflateParams(strm, level, strategy) - z_streamp strm; - int level; - int strategy; +z_streamp strm; +int level; +int strategy; { deflate_state *s; compress_func func; int err = Z_OK; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (strm == Z_NULL || strm->state == Z_NULL) { + return Z_STREAM_ERROR; + } s = strm->state; #ifdef FASTEST - if (level != 0) level = 1; + if (level != 0) { + level = 1; + } #else - if (level == Z_DEFAULT_COMPRESSION) level = 6; + if (level == Z_DEFAULT_COMPRESSION) { + level = 6; + } #endif if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { return Z_STREAM_ERROR; @@ -453,15 +487,17 @@ int ZEXPORT deflateParams(strm, level, strategy) /* ========================================================================= */ int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) - z_streamp strm; - int good_length; - int max_lazy; - int nice_length; - int max_chain; +z_streamp strm; +int good_length; +int max_lazy; +int nice_length; +int max_chain; { deflate_state *s; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (strm == Z_NULL || strm->state == Z_NULL) { + return Z_STREAM_ERROR; + } s = strm->state; s->good_match = good_length; s->max_lazy_match = max_lazy; @@ -488,8 +524,8 @@ int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) * seem onerous for output buffer allocation. */ uLong ZEXPORT deflateBound(strm, sourceLen) - z_streamp strm; - uLong sourceLen; +z_streamp strm; +uLong sourceLen; { deflate_state *s; uLong destLen; @@ -499,13 +535,15 @@ uLong ZEXPORT deflateBound(strm, sourceLen) ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11; /* if can't get parameters, return conservative bound */ - if (strm == Z_NULL || strm->state == Z_NULL) + if (strm == Z_NULL || strm->state == Z_NULL) { return destLen; + } /* if not default parameters, return conservative bound */ s = strm->state; - if (s->w_bits != 15 || s->hash_bits != 8 + 7) + if (s->w_bits != 15 || s->hash_bits != 8 + 7) { return destLen; + } /* default settings: return tight bound for that case */ return compressBound(sourceLen); @@ -517,8 +555,8 @@ uLong ZEXPORT deflateBound(strm, sourceLen) * pending_buf. */ local void putShortMSB (s, b) - deflate_state *s; - uInt b; +deflate_state *s; +uInt b; { put_byte(s, (Byte)(b >> 8)); put_byte(s, (Byte)(b & 0xff)); @@ -531,12 +569,16 @@ local void putShortMSB (s, b) * (See also read_buf()). */ local void flush_pending(strm) - z_streamp strm; +z_streamp strm; { unsigned len = strm->state->pending; - if (len > strm->avail_out) len = strm->avail_out; - if (len == 0) return; + if (len > strm->avail_out) { + len = strm->avail_out; + } + if (len == 0) { + return; + } zmemcpy(strm->next_out, strm->state->pending_out, len); strm->next_out += len; @@ -551,24 +593,26 @@ local void flush_pending(strm) /* ========================================================================= */ int ZEXPORT deflate (strm, flush) - z_streamp strm; - int flush; +z_streamp strm; +int flush; { int old_flush; /* value of flush param for previous deflate call */ deflate_state *s; if (strm == Z_NULL || strm->state == Z_NULL || - flush > Z_FINISH || flush < 0) { + flush > Z_FINISH || flush < 0) { return Z_STREAM_ERROR; } s = strm->state; if (strm->next_out == Z_NULL || - (strm->next_in == Z_NULL && strm->avail_in != 0) || - (s->status == FINISH_STATE && flush != Z_FINISH)) { + (strm->next_in == Z_NULL && strm->avail_in != 0) || + (s->status == FINISH_STATE && flush != Z_FINISH)) { ERR_RETURN(strm, Z_STREAM_ERROR); } - if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); + if (strm->avail_out == 0) { + ERR_RETURN(strm, Z_BUF_ERROR); + } s->strm = strm; /* just in case */ old_flush = s->last_flush; @@ -589,25 +633,25 @@ int ZEXPORT deflate (strm, flush) put_byte(s, 0); put_byte(s, 0); put_byte(s, s->level == 9 ? 2 : - (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? - 4 : 0)); + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); put_byte(s, OS_CODE); s->status = BUSY_STATE; } else { put_byte(s, (s->gzhead->text ? 1 : 0) + - (s->gzhead->hcrc ? 2 : 0) + - (s->gzhead->extra == Z_NULL ? 0 : 4) + - (s->gzhead->name == Z_NULL ? 0 : 8) + - (s->gzhead->comment == Z_NULL ? 0 : 16) + (s->gzhead->hcrc ? 2 : 0) + + (s->gzhead->extra == Z_NULL ? 0 : 4) + + (s->gzhead->name == Z_NULL ? 0 : 8) + + (s->gzhead->comment == Z_NULL ? 0 : 16) ); put_byte(s, (Byte)(s->gzhead->time & 0xff)); put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); put_byte(s, s->level == 9 ? 2 : - (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? - 4 : 0)); + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); put_byte(s, s->gzhead->os & 0xff); if (s->gzhead->extra != NULL) { put_byte(s, s->gzhead->extra_len & 0xff); @@ -626,16 +670,22 @@ int ZEXPORT deflate (strm, flush) uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; uInt level_flags; - if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) + if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) { level_flags = 0; - else if (s->level < 6) + } + else if (s->level < 6) { level_flags = 1; - else if (s->level == 6) + } + else if (s->level == 6) { level_flags = 2; - else + } + else { level_flags = 3; + } header |= (level_flags << 6); - if (s->strstart != 0) header |= PRESET_DICT; + if (s->strstart != 0) { + header |= PRESET_DICT; + } header += 31 - (header % 31); s->status = BUSY_STATE; @@ -661,8 +711,9 @@ int ZEXPORT deflate (strm, flush) s->pending - beg); flush_pending(strm); beg = s->pending; - if (s->pending == s->pending_buf_size) + if (s->pending == s->pending_buf_size) { break; + } } put_byte(s, s->gzhead->extra[s->gzindex]); s->gzindex++; @@ -675,8 +726,9 @@ int ZEXPORT deflate (strm, flush) s->status = NAME_STATE; } } - else + else { s->status = NAME_STATE; + } } if (s->status == NAME_STATE) { if (s->gzhead->name != NULL) { @@ -697,7 +749,8 @@ int ZEXPORT deflate (strm, flush) } val = s->gzhead->name[s->gzindex++]; put_byte(s, val); - } while (val != 0); + } + while (val != 0); if (s->gzhead->hcrc && s->pending > beg) strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg); @@ -706,8 +759,9 @@ int ZEXPORT deflate (strm, flush) s->status = COMMENT_STATE; } } - else + else { s->status = COMMENT_STATE; + } } if (s->status == COMMENT_STATE) { if (s->gzhead->comment != NULL) { @@ -728,20 +782,24 @@ int ZEXPORT deflate (strm, flush) } val = s->gzhead->comment[s->gzindex++]; put_byte(s, val); - } while (val != 0); + } + while (val != 0); if (s->gzhead->hcrc && s->pending > beg) strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg); - if (val == 0) + if (val == 0) { s->status = HCRC_STATE; + } } - else + else { s->status = HCRC_STATE; + } } if (s->status == HCRC_STATE) { if (s->gzhead->hcrc) { - if (s->pending + 2 > s->pending_buf_size) + if (s->pending + 2 > s->pending_buf_size) { flush_pending(strm); + } if (s->pending + 2 <= s->pending_buf_size) { put_byte(s, (Byte)(strm->adler & 0xff)); put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); @@ -749,8 +807,9 @@ int ZEXPORT deflate (strm, flush) s->status = BUSY_STATE; } } - else + else { s->status = BUSY_STATE; + } } #endif @@ -768,12 +827,13 @@ int ZEXPORT deflate (strm, flush) return Z_OK; } - /* Make sure there is something to do and avoid duplicate consecutive - * flushes. For repeated and useless calls with Z_FINISH, we keep - * returning Z_STREAM_END instead of Z_BUF_ERROR. - */ - } else if (strm->avail_in == 0 && flush <= old_flush && - flush != Z_FINISH) { + /* Make sure there is something to do and avoid duplicate consecutive + * flushes. For repeated and useless calls with Z_FINISH, we keep + * returning Z_STREAM_END instead of Z_BUF_ERROR. + */ + } + else if (strm->avail_in == 0 && flush <= old_flush && + flush != Z_FINISH) { ERR_RETURN(strm, Z_BUF_ERROR); } @@ -785,7 +845,7 @@ int ZEXPORT deflate (strm, flush) /* Start a new block or continue the current one. */ if (strm->avail_in != 0 || s->lookahead != 0 || - (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { + (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { block_state bstate; bstate = (*(configuration_table[s->level].func))(s, flush); @@ -809,7 +869,8 @@ int ZEXPORT deflate (strm, flush) if (bstate == block_done) { if (flush == Z_PARTIAL_FLUSH) { _tr_align(s); - } else { /* FULL_FLUSH or SYNC_FLUSH */ + } + else { /* FULL_FLUSH or SYNC_FLUSH */ _tr_stored_block(s, (char*)0, 0L, 0); /* For a full flush, this empty block will be recognized * as a special marker by inflate_sync(). @@ -820,15 +881,19 @@ int ZEXPORT deflate (strm, flush) } flush_pending(strm); if (strm->avail_out == 0) { - s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ - return Z_OK; + s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ + return Z_OK; } } } Assert(strm->avail_out > 0, "bug2"); - if (flush != Z_FINISH) return Z_OK; - if (s->wrap <= 0) return Z_STREAM_END; + if (flush != Z_FINISH) { + return Z_OK; + } + if (s->wrap <= 0) { + return Z_STREAM_END; + } /* Write the trailer */ #ifdef GZIP @@ -852,27 +917,31 @@ int ZEXPORT deflate (strm, flush) /* If avail_out is zero, the application will call deflate again * to flush the rest. */ - if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ + if (s->wrap > 0) { + s->wrap = -s->wrap; /* write the trailer only once! */ + } return s->pending != 0 ? Z_OK : Z_STREAM_END; } /* ========================================================================= */ int ZEXPORT deflateEnd (strm) - z_streamp strm; +z_streamp strm; { int status; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (strm == Z_NULL || strm->state == Z_NULL) { + return Z_STREAM_ERROR; + } status = strm->state->status; if (status != INIT_STATE && - status != EXTRA_STATE && - status != NAME_STATE && - status != COMMENT_STATE && - status != HCRC_STATE && - status != BUSY_STATE && - status != FINISH_STATE) { - return Z_STREAM_ERROR; + status != EXTRA_STATE && + status != NAME_STATE && + status != COMMENT_STATE && + status != HCRC_STATE && + status != BUSY_STATE && + status != FINISH_STATE) { + return Z_STREAM_ERROR; } /* Deallocate in reverse order of allocations: */ @@ -893,8 +962,8 @@ int ZEXPORT deflateEnd (strm) * doesn't have enough memory anyway to duplicate compression states). */ int ZEXPORT deflateCopy (dest, source) - z_streamp dest; - z_streamp source; +z_streamp dest; +z_streamp source; { #ifdef MAXSEG_64K return Z_STREAM_ERROR; @@ -913,7 +982,9 @@ int ZEXPORT deflateCopy (dest, source) zmemcpy(dest, source, sizeof(z_stream)); ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); - if (ds == Z_NULL) return Z_MEM_ERROR; + if (ds == Z_NULL) { + return Z_MEM_ERROR; + } dest->state = (struct internal_state FAR *) ds; zmemcpy(ds, ss, sizeof(deflate_state)); ds->strm = dest; @@ -925,7 +996,7 @@ int ZEXPORT deflateCopy (dest, source) ds->pending_buf = (uchf *) overlay; if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || - ds->pending_buf == Z_NULL) { + ds->pending_buf == Z_NULL) { deflateEnd (dest); return Z_MEM_ERROR; } @@ -955,14 +1026,18 @@ int ZEXPORT deflateCopy (dest, source) * (See also flush_pending()). */ local int read_buf(strm, buf, size) - z_streamp strm; - Bytef *buf; - unsigned size; +z_streamp strm; +Bytef *buf; +unsigned size; { unsigned len = strm->avail_in; - if (len > size) len = size; - if (len == 0) return 0; + if (len > size) { + len = size; + } + if (len == 0) { + return 0; + } strm->avail_in -= len; @@ -985,7 +1060,7 @@ local int read_buf(strm, buf, size) * Initialize the "longest match" routines for a new zlib stream */ local void lm_init (s) - deflate_state *s; +deflate_state *s; { s->window_size = (ulg)2L*s->w_size; @@ -1026,8 +1101,8 @@ local void lm_init (s) * match.S. The code will be functionally equivalent. */ local uInt longest_match(s, cur_match) - deflate_state *s; - IPos cur_match; /* current match */ +deflate_state *s; +IPos cur_match; /* current match */ { unsigned chain_length = s->max_chain_length;/* max hash chain length */ register Bytef *scan = s->window + s->strstart; /* current string */ @@ -1036,7 +1111,7 @@ local uInt longest_match(s, cur_match) int best_len = s->prev_length; /* best match length so far */ int nice_match = s->nice_match; /* stop if match long enough */ IPos limit = s->strstart > (IPos)MAX_DIST(s) ? - s->strstart - (IPos)MAX_DIST(s) : NIL; + s->strstart - (IPos)MAX_DIST(s) : NIL; /* Stop when cur_match becomes <= limit. To simplify the code, * we prevent matches with the string of window index 0. */ @@ -1068,7 +1143,9 @@ local uInt longest_match(s, cur_match) /* Do not look for matches beyond the end of the input. This is necessary * to make deflate deterministic. */ - if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; + if ((uInt)nice_match > s->lookahead) { + nice_match = s->lookahead; + } Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); @@ -1089,7 +1166,9 @@ local uInt longest_match(s, cur_match) * UNALIGNED_OK if your compiler uses a different size. */ if (*(ushf*)(match+best_len-1) != scan_end || - *(ushf*)match != scan_start) continue; + *(ushf*)match != scan_start) { + continue; + } /* It is not necessary to compare scan[2] and match[2] since they are * always equal when the other bytes match, given that the hash keys @@ -1103,16 +1182,19 @@ local uInt longest_match(s, cur_match) Assert(scan[2] == match[2], "scan[2]?"); scan++, match++; do { - } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && - *(ushf*)(scan+=2) == *(ushf*)(match+=2) && - *(ushf*)(scan+=2) == *(ushf*)(match+=2) && - *(ushf*)(scan+=2) == *(ushf*)(match+=2) && - scan < strend); + } + while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + scan < strend); /* The funny "do {}" generates better code on most compilers */ /* Here, scan <= window+strstart+257 */ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); - if (*scan == *match) scan++; + if (*scan == *match) { + scan++; + } len = (MAX_MATCH - 1) - (int)(strend-scan); scan = strend - (MAX_MATCH-1); @@ -1120,9 +1202,11 @@ local uInt longest_match(s, cur_match) #else /* UNALIGNED_OK */ if (match[best_len] != scan_end || - match[best_len-1] != scan_end1 || - *match != *scan || - *++match != scan[1]) continue; + match[best_len-1] != scan_end1 || + *match != *scan || + *++match != scan[1]) { + continue; + } /* The check at best_len-1 can be removed because it will be made * again later. (This heuristic is not always a win.) @@ -1137,11 +1221,12 @@ local uInt longest_match(s, cur_match) * the 256th check will be made at strstart+258. */ do { - } while (*++scan == *++match && *++scan == *++match && - *++scan == *++match && *++scan == *++match && - *++scan == *++match && *++scan == *++match && - *++scan == *++match && *++scan == *++match && - scan < strend); + } + while (*++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + scan < strend); Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); @@ -1153,7 +1238,9 @@ local uInt longest_match(s, cur_match) if (len > best_len) { s->match_start = cur_match; best_len = len; - if (len >= nice_match) break; + if (len >= nice_match) { + break; + } #ifdef UNALIGNED_OK scan_end = *(ushf*)(scan+best_len-1); #else @@ -1161,10 +1248,13 @@ local uInt longest_match(s, cur_match) scan_end = scan[best_len]; #endif } - } while ((cur_match = prev[cur_match & wmask]) > limit - && --chain_length != 0); + } + while ((cur_match = prev[cur_match & wmask]) > limit + && --chain_length != 0); - if ((uInt)best_len <= s->lookahead) return (uInt)best_len; + if ((uInt)best_len <= s->lookahead) { + return (uInt)best_len; + } return s->lookahead; } #endif /* ASMV */ @@ -1174,8 +1264,8 @@ local uInt longest_match(s, cur_match) * Optimized version for level == 1 or strategy == Z_RLE only */ local uInt longest_match_fast(s, cur_match) - deflate_state *s; - IPos cur_match; /* current match */ +deflate_state *s; +IPos cur_match; /* current match */ { register Bytef *scan = s->window + s->strstart; /* current string */ register Bytef *match; /* matched string */ @@ -1195,7 +1285,9 @@ local uInt longest_match_fast(s, cur_match) /* Return failure if the match length is less than 2: */ - if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; + if (match[0] != scan[0] || match[1] != scan[1]) { + return MIN_MATCH-1; + } /* The check at best_len-1 can be removed because it will be made * again later. (This heuristic is not always a win.) @@ -1210,17 +1302,20 @@ local uInt longest_match_fast(s, cur_match) * the 256th check will be made at strstart+258. */ do { - } while (*++scan == *++match && *++scan == *++match && - *++scan == *++match && *++scan == *++match && - *++scan == *++match && *++scan == *++match && - *++scan == *++match && *++scan == *++match && - scan < strend); + } + while (*++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + scan < strend); Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); len = MAX_MATCH - (int)(strend - scan); - if (len < MIN_MATCH) return MIN_MATCH - 1; + if (len < MIN_MATCH) { + return MIN_MATCH - 1; + } s->match_start = cur_match; return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; @@ -1231,9 +1326,9 @@ local uInt longest_match_fast(s, cur_match) * Check that the match at match_start is indeed a match. */ local void check_match(s, start, match, length) - deflate_state *s; - IPos start, match; - int length; +deflate_state *s; +IPos start, match; +int length; { /* check that the match is indeed a match */ if (zmemcmp(s->window + match, @@ -1242,12 +1337,16 @@ local void check_match(s, start, match, length) start, match, length); do { fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); - } while (--length != 0); + } + while (--length != 0); z_error("invalid match"); } if (z_verbose > 1) { fprintf(stderr,"\\[%d,%d]", start-match, length); - do { putc(s->window[start++], stderr); } while (--length != 0); + do { + putc(s->window[start++], stderr); + } + while (--length != 0); } } #else @@ -1265,7 +1364,7 @@ local void check_match(s, start, match, length) * option -- not supported here). */ local void fill_window(s) - deflate_state *s; +deflate_state *s; { register unsigned n, m; register Posf *p; @@ -1280,7 +1379,8 @@ local void fill_window(s) if (more == 0 && s->strstart == 0 && s->lookahead == 0) { more = wsize; - } else if (more == (unsigned)(-1)) { + } + else if (more == (unsigned)(-1)) { /* Very unlikely, but possible on 16 bit machine if * strstart == 0 && lookahead == 1 (input done a byte at time) */ @@ -1310,7 +1410,8 @@ local void fill_window(s) do { m = *--p; *p = (Pos)(m >= wsize ? m-wsize : NIL); - } while (--n); + } + while (--n); n = wsize; #ifndef FASTEST @@ -1321,11 +1422,14 @@ local void fill_window(s) /* If n is not on any hash chain, prev[n] is garbage but * its value will never be used. */ - } while (--n); + } + while (--n); #endif more += wsize; } - if (s->strm->avail_in == 0) return; + if (s->strm->avail_in == 0) { + return; + } /* If there was no sliding: * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && @@ -1355,7 +1459,8 @@ local void fill_window(s) * but this is not important since only literal bytes will be emitted. */ - } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); + } + while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); } /* =========================================================================== @@ -1389,8 +1494,8 @@ local void fill_window(s) * window to pending_buf. */ local block_state deflate_stored(s, flush) - deflate_state *s; - int flush; +deflate_state *s; +int flush; { /* Stored blocks are limited to 0xffff bytes, pending_buf is limited * to pending_buf_size, and each stored block has a 5 byte header: @@ -1411,9 +1516,13 @@ local block_state deflate_stored(s, flush) s->block_start >= (long)s->w_size, "slide too late"); fill_window(s); - if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more; + if (s->lookahead == 0 && flush == Z_NO_FLUSH) { + return need_more; + } - if (s->lookahead == 0) break; /* flush the current block */ + if (s->lookahead == 0) { + break; /* flush the current block */ + } } Assert(s->block_start >= 0L, "block gone"); @@ -1447,8 +1556,8 @@ local block_state deflate_stored(s, flush) * matches. It is used only for the fast compression options. */ local block_state deflate_fast(s, flush) - deflate_state *s; - int flush; +deflate_state *s; +int flush; { IPos hash_head = NIL; /* head of the hash chain */ int bflush; /* set if current block must be flushed */ @@ -1464,7 +1573,9 @@ local block_state deflate_fast(s, flush) if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { return need_more; } - if (s->lookahead == 0) break; /* flush the current block */ + if (s->lookahead == 0) { + break; /* flush the current block */ + } } /* Insert the string window[strstart .. strstart+2] in the @@ -1484,13 +1595,14 @@ local block_state deflate_fast(s, flush) */ #ifdef FASTEST if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) || - (s->strategy == Z_RLE && s->strstart - hash_head == 1)) { + (s->strategy == Z_RLE && s->strstart - hash_head == 1)) { s->match_length = longest_match_fast (s, hash_head); } #else if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) { s->match_length = longest_match (s, hash_head); - } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { + } + else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { s->match_length = longest_match_fast (s, hash_head); } #endif @@ -1509,7 +1621,7 @@ local block_state deflate_fast(s, flush) */ #ifndef FASTEST if (s->match_length <= s->max_insert_length && - s->lookahead >= MIN_MATCH) { + s->lookahead >= MIN_MATCH) { s->match_length--; /* string at strstart already in table */ do { s->strstart++; @@ -1517,9 +1629,11 @@ local block_state deflate_fast(s, flush) /* strstart never exceeds WSIZE-MAX_MATCH, so there are * always MIN_MATCH bytes ahead. */ - } while (--s->match_length != 0); + } + while (--s->match_length != 0); s->strstart++; - } else + } + else #endif { s->strstart += s->match_length; @@ -1533,14 +1647,17 @@ local block_state deflate_fast(s, flush) * matter since it will be recomputed at next deflate call. */ } - } else { + } + else { /* No match, output a literal byte */ Tracevv((stderr,"%c", s->window[s->strstart])); _tr_tally_lit (s, s->window[s->strstart], bflush); s->lookahead--; s->strstart++; } - if (bflush) FLUSH_BLOCK(s, 0); + if (bflush) { + FLUSH_BLOCK(s, 0); + } } FLUSH_BLOCK(s, flush == Z_FINISH); return flush == Z_FINISH ? finish_done : block_done; @@ -1553,8 +1670,8 @@ local block_state deflate_fast(s, flush) * no better match at the next window position. */ local block_state deflate_slow(s, flush) - deflate_state *s; - int flush; +deflate_state *s; +int flush; { IPos hash_head = NIL; /* head of hash chain */ int bflush; /* set if current block must be flushed */ @@ -1571,7 +1688,9 @@ local block_state deflate_slow(s, flush) if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { return need_more; } - if (s->lookahead == 0) break; /* flush the current block */ + if (s->lookahead == 0) { + break; /* flush the current block */ + } } /* Insert the string window[strstart .. strstart+2] in the @@ -1587,24 +1706,25 @@ local block_state deflate_slow(s, flush) s->match_length = MIN_MATCH-1; if (hash_head != NIL && s->prev_length < s->max_lazy_match && - s->strstart - hash_head <= MAX_DIST(s)) { + s->strstart - hash_head <= MAX_DIST(s)) { /* To simplify the code, we prevent matches with the string * of window index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) { s->match_length = longest_match (s, hash_head); - } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { + } + else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { s->match_length = longest_match_fast (s, hash_head); } /* longest_match() or longest_match_fast() sets match_start */ if (s->match_length <= 5 && (s->strategy == Z_FILTERED #if TOO_FAR <= 32767 - || (s->match_length == MIN_MATCH && - s->strstart - s->match_start > TOO_FAR) + || (s->match_length == MIN_MATCH && + s->strstart - s->match_start > TOO_FAR) #endif - )) { + )) { /* If prev_match is also MIN_MATCH, match_start is garbage * but we will ignore the current match anyway. @@ -1635,14 +1755,18 @@ local block_state deflate_slow(s, flush) if (++s->strstart <= max_insert) { INSERT_STRING(s, s->strstart, hash_head); } - } while (--s->prev_length != 0); + } + while (--s->prev_length != 0); s->match_available = 0; s->match_length = MIN_MATCH-1; s->strstart++; - if (bflush) FLUSH_BLOCK(s, 0); + if (bflush) { + FLUSH_BLOCK(s, 0); + } - } else if (s->match_available) { + } + else if (s->match_available) { /* If there was no match at the previous position, output a * single literal. If there was a match but the current match * is longer, truncate the previous match to a single literal. @@ -1654,8 +1778,11 @@ local block_state deflate_slow(s, flush) } s->strstart++; s->lookahead--; - if (s->strm->avail_out == 0) return need_more; - } else { + if (s->strm->avail_out == 0) { + return need_more; + } + } + else { /* There is no previous match to compare with, wait for * the next step to decide. */ @@ -1682,8 +1809,8 @@ local block_state deflate_slow(s, flush) * deflate switches away from Z_RLE.) */ local block_state deflate_rle(s, flush) - deflate_state *s; - int flush; +deflate_state *s; +int flush; { int bflush; /* set if current block must be flushed */ uInt run; /* length of run */ @@ -1701,7 +1828,9 @@ local block_state deflate_rle(s, flush) if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { return need_more; } - if (s->lookahead == 0) break; /* flush the current block */ + if (s->lookahead == 0) { + break; /* flush the current block */ + } } /* See how many times the previous byte repeats */ @@ -1711,9 +1840,11 @@ local block_state deflate_rle(s, flush) scan = s->window + s->strstart - 1; prev = *scan++; do { - if (*scan++ != prev) + if (*scan++ != prev) { break; - } while (++run < max); + } + } + while (++run < max); } /* Emit match if have run of MIN_MATCH or longer, else emit literal */ @@ -1722,14 +1853,17 @@ local block_state deflate_rle(s, flush) _tr_tally_dist(s, 1, run - MIN_MATCH, bflush); s->lookahead -= run; s->strstart += run; - } else { + } + else { /* No match, output a literal byte */ Tracevv((stderr,"%c", s->window[s->strstart])); _tr_tally_lit (s, s->window[s->strstart], bflush); s->lookahead--; s->strstart++; } - if (bflush) FLUSH_BLOCK(s, 0); + if (bflush) { + FLUSH_BLOCK(s, 0); + } } FLUSH_BLOCK(s, flush == Z_FINISH); return flush == Z_FINISH ? finish_done : block_done; |