summaryrefslogtreecommitdiff
path: root/deflate.c
diff options
context:
space:
mode:
authorjmadill <jmadill@chromium.org>2016-06-20 06:58:52 -0700
committerCommit bot <commit-bot@chromium.org>2016-06-20 14:01:19 +0000
commitbf2aebeacfc84bf69ff964edbf212ec3aed6430c (patch)
treedfa65fc6e4ed637adcd0a4e9a13d353eef01f543 /deflate.c
parent90f7dad71a47e992425d7deda7970643990288f3 (diff)
downloadzlib-bf2aebeacfc84bf69ff964edbf212ec3aed6430c.tar.gz
Revert of Update Zlib to version 1.2.8 (patchset #10 id:220001 of https://codereview.chromium.org/1955383002/ )
Reason for revert: Broke the Windows Debug GPU FYI builder: https://build.chromium.org/p/chromium.gpu.fyi/builders/GPU%20Win%20Builder%20%28dbg%29/builds/41421 fx_zlib.gzlib.obj : error LNK2005: _gzopen_w already defined in zlib.gzlib.obj initial\chrome.dll : fatal error LNK1169: one or more multiply defined symbols found Possibly because they use shared components with a GYP build. Original issue's description: > Update Zlib to version 1.2.8 > > merge the latest open source zlib into chromium external projects > > Version 1.2.8 fixes a very rare bug in decompression. All users are encouraged to upgrade immediately. Version 1.2.8 also has these improvements: > - Add new inflateGetDictionary() function > - Fix bug where gzopen() immediately followed by gzclose() would write an empty file instead of an empty gzip stream. > - Fix bug in gzclose() when gzwrite() runs out of memory > Version 1.2.7 has many portability improvements over 1.2.6, and has these enhancements: > - Fix bug in gzclose_w() when gzwrite() fails to allocate memory > - Add "x" (O_EXCL) and "e" (O_CLOEXEC) modes support to gzopen() > - Add gzopen_w() in Windows for wide character path names > - Fix type mismatch between get_crc_table() and crc_table > Version 1.2.6 has many changes over 1.2.5, including these improvements: > - gzread() can now read a file that is being written concurrently > - gzgetc() is now a macro for increased speed > - Added a 'T' option to gzopen() for transparent writing (no compression) > - Added deflatePending() to return the amount of pending output > - Allow deflateSetDictionary() and inflateSetDictionary() at any time in raw mode > - deflatePrime() can now insert bits in the middle of the stream > - ./configure now creates a configure.log file with all of the results > - Added a ./configure --solo option to compile zlib with no dependency on any libraries > - Fixed a problem with large file support macros > - Fixed a bug in contrib/puff > - Many portability improvements > > BUG=610615 > R=agl@chromium.org > R=gavinp@chromium.org > > Committed: https://crrev.com/a50849ae9ba1b2fad47905db521463b3bf085883 > Cr-Commit-Position: refs/heads/master@{#400647} TBR=agl@chromium.org,gavinp@chromium.org,khasim.mohammed@linaro.org,jiadong.zhu@linaro.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=610615 Review-Url: https://codereview.chromium.org/2079313002 Cr-Original-Commit-Position: refs/heads/master@{#400670} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: 43770253ccc0425c4edde966e56b3e68955cc4d3
Diffstat (limited to 'deflate.c')
-rw-r--r--deflate.c263
1 files changed, 69 insertions, 194 deletions
diff --git a/deflate.c b/deflate.c
index 59645eb..55ec215 100644
--- a/deflate.c
+++ b/deflate.c
@@ -1,5 +1,5 @@
/* deflate.c -- compress data using the deflation algorithm
- * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
+ * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
@@ -37,7 +37,7 @@
* REFERENCES
*
* Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
- * Available in http://tools.ietf.org/html/rfc1951
+ * Available in http://www.ietf.org/rfc/rfc1951.txt
*
* A description of the Rabin and Karp algorithm is given in the book
* "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
@@ -48,12 +48,14 @@
*/
/* @(#) $Id$ */
+
#include <assert.h>
+
#include "deflate.h"
#include "x86.h"
const char deflate_copyright[] =
- " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
+ " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler ";
/*
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
@@ -174,9 +176,6 @@ local const config configuration_table[10] = {
struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
#endif
-/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
-#define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0))
-
/* ===========================================================================
* Update a hash value with the given input byte
* IN assertion: all calls to to UPDATE_HASH are made with consecutive
@@ -270,19 +269,10 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
strm->msg = Z_NULL;
if (strm->zalloc == (alloc_func)0) {
-#ifdef Z_SOLO
- return Z_STREAM_ERROR;
-#else
strm->zalloc = zcalloc;
strm->opaque = (voidpf)0;
-#endif
}
- if (strm->zfree == (free_func)0)
-#ifdef Z_SOLO
- return Z_STREAM_ERROR;
-#else
- strm->zfree = zcfree;
-#endif
+ if (strm->zfree == (free_func)0) strm->zfree = zcfree;
#ifdef FASTEST
if (level != 0) level = 1;
@@ -345,7 +335,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
s->pending_buf == Z_NULL) {
s->status = FINISH_STATE;
- strm->msg = ERR_MSG(Z_MEM_ERROR);
+ strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
deflateEnd (strm);
return Z_MEM_ERROR;
}
@@ -366,66 +356,43 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
uInt dictLength;
{
deflate_state *s;
- uInt str, n;
- int wrap;
- unsigned avail;
- z_const unsigned char *next;
+ uInt length = dictLength;
+ uInt n;
+ IPos hash_head = 0;
- if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
- return Z_STREAM_ERROR;
- s = strm->state;
- wrap = s->wrap;
- if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
+ if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
+ strm->state->wrap == 2 ||
+ (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
return Z_STREAM_ERROR;
- /* when using zlib wrappers, compute Adler-32 for provided dictionary */
- if (wrap == 1)
+ s = strm->state;
+ if (s->wrap)
strm->adler = adler32(strm->adler, dictionary, dictLength);
- s->wrap = 0; /* avoid computing Adler-32 in read_buf */
-
- /* if dictionary would fill window, just replace the history */
- if (dictLength >= s->w_size) {
- if (wrap == 0) { /* already empty otherwise */
- CLEAR_HASH(s);
- s->strstart = 0;
- s->block_start = 0L;
- s->insert = 0;
- }
- dictionary += dictLength - s->w_size; /* use the tail */
- dictLength = s->w_size;
+
+ if (length < MIN_MATCH) return Z_OK;
+ if (length > s->w_size) {
+ length = s->w_size;
+ dictionary += dictLength - length; /* use the tail of the dictionary */
}
+ zmemcpy(s->window, dictionary, length);
+ s->strstart = length;
+ s->block_start = (long)length;
- /* insert dictionary into window and hash */
- avail = strm->avail_in;
- next = strm->next_in;
- strm->avail_in = dictLength;
- strm->next_in = (z_const Bytef *)dictionary;
- fill_window(s);
- while (s->lookahead >= MIN_MATCH) {
- str = s->strstart;
- n = s->lookahead - (MIN_MATCH-1);
- do {
- insert_string(s, str);
- str++;
- } while (--n);
- s->strstart = str;
- s->lookahead = MIN_MATCH-1;
- fill_window(s);
+ /* Insert all strings in the hash table (except for the last two bytes).
+ * s->lookahead stays null, so s->ins_h will be recomputed at the next
+ * call of fill_window.
+ */
+ s->ins_h = s->window[0];
+ UPDATE_HASH(s, s->ins_h, s->window[1]);
+ for (n = 0; n <= length - MIN_MATCH; n++) {
+ insert_string(s, n);
}
- s->strstart += s->lookahead;
- s->block_start = (long)s->strstart;
- s->insert = s->lookahead;
- s->lookahead = 0;
- s->match_length = s->prev_length = MIN_MATCH-1;
- s->match_available = 0;
- strm->next_in = next;
- strm->avail_in = avail;
- s->wrap = wrap;
+ if (hash_head) hash_head = 0; /* to make compiler happy */
return Z_OK;
}
/* ========================================================================= */
-int ZEXPORT deflateResetKeep (strm)
+int ZEXPORT deflateReset (strm)
z_streamp strm;
{
deflate_state *s;
@@ -457,23 +424,12 @@ int ZEXPORT deflateResetKeep (strm)
s->last_flush = Z_NO_FLUSH;
_tr_init(s);
+ lm_init(s);
return Z_OK;
}
/* ========================================================================= */
-int ZEXPORT deflateReset (strm)
- z_streamp strm;
-{
- int ret;
-
- ret = deflateResetKeep(strm);
- if (ret == Z_OK)
- lm_init(strm->state);
- return ret;
-}
-
-/* ========================================================================= */
int ZEXPORT deflateSetHeader (strm, head)
z_streamp strm;
gz_headerp head;
@@ -485,42 +441,14 @@ int ZEXPORT deflateSetHeader (strm, head)
}
/* ========================================================================= */
-int ZEXPORT deflatePending (strm, pending, bits)
- unsigned *pending;
- int *bits;
- z_streamp strm;
-{
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
- if (pending != Z_NULL)
- *pending = strm->state->pending;
- if (bits != Z_NULL)
- *bits = strm->state->bi_valid;
- return Z_OK;
-}
-
-/* ========================================================================= */
int ZEXPORT deflatePrime (strm, bits, value)
z_streamp strm;
int bits;
int value;
{
- deflate_state *s;
- int put;
-
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
- s = strm->state;
- if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
- return Z_BUF_ERROR;
- do {
- put = Buf_size - s->bi_valid;
- if (put > bits)
- put = bits;
- s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
- s->bi_valid += put;
- _tr_flush_bits(s);
- value >>= put;
- bits -= put;
- } while (bits);
+ strm->state->bi_valid = bits;
+ strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
return Z_OK;
}
@@ -551,8 +479,6 @@ int ZEXPORT deflateParams(strm, level, strategy)
strm->total_in != 0) {
/* Flush the last buffer: */
err = deflate(strm, Z_BLOCK);
- if (err == Z_BUF_ERROR && s->pending == 0)
- err = Z_OK;
}
if (s->level != level) {
s->level = level;
@@ -680,22 +606,19 @@ local void putShortMSB (s, b)
local void flush_pending(strm)
z_streamp strm;
{
- unsigned len;
- deflate_state *s = strm->state;
+ unsigned len = strm->state->pending;
- _tr_flush_bits(s);
- len = s->pending;
if (len > strm->avail_out) len = strm->avail_out;
if (len == 0) return;
- zmemcpy(strm->next_out, s->pending_out, len);
+ zmemcpy(strm->next_out, strm->state->pending_out, len);
strm->next_out += len;
- s->pending_out += len;
+ strm->state->pending_out += len;
strm->total_out += len;
strm->avail_out -= len;
- s->pending -= len;
- if (s->pending == 0) {
- s->pending_out = s->pending_buf;
+ strm->state->pending -= len;
+ if (strm->state->pending == 0) {
+ strm->state->pending_out = strm->state->pending_buf;
}
}
@@ -922,7 +845,7 @@ int ZEXPORT deflate (strm, flush)
* 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 && RANK(flush) <= RANK(old_flush) &&
+ } else if (strm->avail_in == 0 && flush <= old_flush &&
flush != Z_FINISH) {
ERR_RETURN(strm, Z_BUF_ERROR);
}
@@ -988,7 +911,6 @@ int ZEXPORT deflate (strm, flush)
if (s->lookahead == 0) {
s->strstart = 0;
s->block_start = 0L;
- s->insert = 0;
}
}
}
@@ -1086,12 +1008,12 @@ int ZEXPORT deflateCopy (dest, source)
ss = source->state;
- zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
+ zmemcpy(dest, source, sizeof(z_stream));
ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
if (ds == Z_NULL) return Z_MEM_ERROR;
dest->state = (struct internal_state FAR *) ds;
- zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
+ zmemcpy(ds, ss, sizeof(deflate_state));
ds->strm = dest;
ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
@@ -1107,8 +1029,8 @@ int ZEXPORT deflateCopy (dest, source)
}
/* following zmemcpy do not work for 16-bit MSDOS */
zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
- zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
- zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
+ zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
+ zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
@@ -1143,9 +1065,10 @@ ZLIB_INTERNAL int read_buf(strm, buf, size)
strm->avail_in -= len;
#ifdef GZIP
- if (strm->state->wrap == 2)
+ if (strm->state->wrap == 2) {
copy_with_crc(strm, buf, len);
- else
+ }
+ else
#endif
{
zmemcpy(buf, strm->next_in, len);
@@ -1178,7 +1101,6 @@ local void lm_init (s)
s->strstart = 0;
s->block_start = 0L;
s->lookahead = 0;
- s->insert = 0;
s->match_length = s->prev_length = MIN_MATCH-1;
s->match_available = 0;
s->ins_h = 0;
@@ -1450,7 +1372,7 @@ local uInt cookie_match(s, start, len)
}
/* Check that we aren't matching a prefix of another cookie by ensuring
* that the final byte is either a semicolon (which cannot appear in a
- * cookie value), or non-cookie data. */
+ * cookie value), or the match is followed by non-cookie data. */
if (s->window[cookie_location+len-1] != ';' &&
class_at(s, cookie_location+len) != 0) {
return 0;
@@ -1584,8 +1506,6 @@ local void fill_window_c(s)
unsigned more; /* Amount of free space at the end of the window. */
uInt wsize = s->w_size;
- Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
-
do {
more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
@@ -1636,6 +1556,7 @@ local void fill_window_c(s)
*/
} while (--n);
#endif
+
for (n = 0; n < Z_COOKIE_HASH_SIZE; n++) {
if (s->cookie_locations[n] > wsize) {
s->cookie_locations[n] -= wsize;
@@ -1652,7 +1573,7 @@ local void fill_window_c(s)
more += wsize;
}
- if (s->strm->avail_in == 0) break;
+ if (s->strm->avail_in == 0) return;
/* If there was no sliding:
* strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
@@ -1674,24 +1595,12 @@ local void fill_window_c(s)
s->lookahead += n;
/* Initialize the hash value now that we have some input: */
- if (s->lookahead + s->insert >= MIN_MATCH) {
- uInt str = s->strstart - s->insert;
- s->ins_h = s->window[str];
- UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
+ if (s->lookahead >= MIN_MATCH) {
+ s->ins_h = s->window[s->strstart];
+ UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
#if MIN_MATCH != 3
Call UPDATE_HASH() MIN_MATCH-3 more times
#endif
- while (s->insert) {
- UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
-#ifndef FASTEST
- s->prev[str & s->w_mask] = s->head[s->ins_h];
-#endif
- s->head[s->ins_h] = (Pos)str;
- str++;
- s->insert--;
- if (s->lookahead + s->insert < MIN_MATCH)
- break;
- }
}
/* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
* but this is not important since only literal bytes will be emitted.
@@ -1732,9 +1641,6 @@ local void fill_window_c(s)
s->high_water += init;
}
}
-
- Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
- "not enough room for search");
}
/* ===========================================================================
@@ -1815,14 +1721,8 @@ local block_state deflate_stored(s, flush, clas)
FLUSH_BLOCK(s, 0);
}
}
- s->insert = 0;
- if (flush == Z_FINISH) {
- FLUSH_BLOCK(s, 1);
- return finish_done;
- }
- if ((long)s->strstart > s->block_start)
- FLUSH_BLOCK(s, 0);
- return block_done;
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
}
/* ===========================================================================
@@ -1924,14 +1824,8 @@ local block_state deflate_fast(s, flush, clas)
}
if (bflush) FLUSH_BLOCK(s, 0);
}
- s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
- if (flush == Z_FINISH) {
- FLUSH_BLOCK(s, 1);
- return finish_done;
- }
- if (s->last_lit)
- FLUSH_BLOCK(s, 0);
- return block_done;
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
}
#ifndef FASTEST
@@ -2086,14 +1980,8 @@ local block_state deflate_slow(s, flush, clas)
_tr_tally_lit(s, s->window[s->strstart-1], bflush);
s->match_available = 0;
}
- s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
- if (flush == Z_FINISH) {
- FLUSH_BLOCK(s, 1);
- return finish_done;
- }
- if (s->last_lit)
- FLUSH_BLOCK(s, 0);
- return block_done;
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
}
#endif /* FASTEST */
@@ -2113,11 +2001,11 @@ local block_state deflate_rle(s, flush)
for (;;) {
/* Make sure that we always have enough lookahead, except
* at the end of the input file. We need MAX_MATCH bytes
- * for the longest run, plus one for the unrolled loop.
+ * for the longest encodable run.
*/
- if (s->lookahead <= MAX_MATCH) {
+ if (s->lookahead < MAX_MATCH) {
fill_window(s);
- if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
+ if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
return need_more;
}
if (s->lookahead == 0) break; /* flush the current block */
@@ -2140,7 +2028,6 @@ local block_state deflate_rle(s, flush)
if (s->match_length > s->lookahead)
s->match_length = s->lookahead;
}
- Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
}
/* Emit match if have run of MIN_MATCH or longer, else emit literal */
@@ -2161,14 +2048,8 @@ local block_state deflate_rle(s, flush)
}
if (bflush) FLUSH_BLOCK(s, 0);
}
- s->insert = 0;
- if (flush == Z_FINISH) {
- FLUSH_BLOCK(s, 1);
- return finish_done;
- }
- if (s->last_lit)
- FLUSH_BLOCK(s, 0);
- return block_done;
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
}
/* ===========================================================================
@@ -2200,14 +2081,8 @@ local block_state deflate_huff(s, flush)
s->strstart++;
if (bflush) FLUSH_BLOCK(s, 0);
}
- s->insert = 0;
- if (flush == Z_FINISH) {
- FLUSH_BLOCK(s, 1);
- return finish_done;
- }
- if (s->last_lit)
- FLUSH_BLOCK(s, 0);
- return block_done;
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
}
/* Safe to inline this as GCC/clang will use inline asm and Visual Studio will