Index: third_party/zlib/gzwrite.c |
diff --git a/third_party/zlib/gzwrite.c b/third_party/zlib/gzwrite.c |
index aa767fbf63ec7ddd2f5863d05c03f647303a101f..e8defc6887a186055f17597968c4fc5791332811 100644 |
--- a/third_party/zlib/gzwrite.c |
+++ b/third_party/zlib/gzwrite.c |
@@ -1,5 +1,5 @@ |
/* gzwrite.c -- zlib functions for writing gzip files |
- * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler |
+ * Copyright (C) 2004, 2005, 2010 Mark Adler |
* For conditions of distribution and use, see copyright notice in zlib.h |
*/ |
@@ -18,55 +18,44 @@ |
int ret; |
z_streamp strm = &(state->strm); |
- /* allocate input buffer */ |
- state->in = (unsigned char *)malloc(state->want); |
- if (state->in == NULL) { |
+ /* allocate input and output buffers */ |
+ state->in = malloc(state->want); |
+ state->out = malloc(state->want); |
+ if (state->in == NULL || state->out == NULL) { |
+ if (state->out != NULL) |
+ free(state->out); |
+ if (state->in != NULL) |
+ free(state->in); |
gz_error(state, Z_MEM_ERROR, "out of memory"); |
return -1; |
} |
- /* only need output buffer and deflate state if compressing */ |
- if (!state->direct) { |
- /* allocate output buffer */ |
- state->out = (unsigned char *)malloc(state->want); |
- if (state->out == NULL) { |
- free(state->in); |
- gz_error(state, Z_MEM_ERROR, "out of memory"); |
- return -1; |
- } |
- |
- /* allocate deflate memory, set up for gzip compression */ |
- strm->zalloc = Z_NULL; |
- strm->zfree = Z_NULL; |
- strm->opaque = Z_NULL; |
- ret = deflateInit2(strm, state->level, Z_DEFLATED, |
- MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); |
- if (ret != Z_OK) { |
- free(state->out); |
- free(state->in); |
- gz_error(state, Z_MEM_ERROR, "out of memory"); |
- return -1; |
- } |
+ /* allocate deflate memory, set up for gzip compression */ |
+ strm->zalloc = Z_NULL; |
+ strm->zfree = Z_NULL; |
+ strm->opaque = Z_NULL; |
+ ret = deflateInit2(strm, state->level, Z_DEFLATED, |
+ 15 + 16, 8, state->strategy); |
+ if (ret != Z_OK) { |
+ free(state->in); |
+ gz_error(state, Z_MEM_ERROR, "out of memory"); |
+ return -1; |
} |
/* mark state as initialized */ |
state->size = state->want; |
- /* initialize write buffer if compressing */ |
- if (!state->direct) { |
- strm->avail_out = state->size; |
- strm->next_out = state->out; |
- state->x.next = strm->next_out; |
- } |
+ /* initialize write buffer */ |
+ strm->avail_out = state->size; |
+ strm->next_out = state->out; |
+ state->next = strm->next_out; |
return 0; |
} |
/* Compress whatever is at avail_in and next_in and write to the output file. |
Return -1 if there is an error writing to the output file, otherwise 0. |
flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH, |
- then the deflate() state is reset to start a new gzip stream. If gz->direct |
- is true, then simply write to the output file without compressing, and |
- ignore flush. */ |
+ then the deflate() state is reset to start a new gzip stream. */ |
local int gz_comp(state, flush) |
gz_statep state; |
int flush; |
@@ -78,17 +67,6 @@ |
/* allocate memory if this is the first time through */ |
if (state->size == 0 && gz_init(state) == -1) |
return -1; |
- |
- /* write directly if requested */ |
- if (state->direct) { |
- got = write(state->fd, strm->next_in, strm->avail_in); |
- if (got < 0 || (unsigned)got != strm->avail_in) { |
- gz_error(state, Z_ERRNO, zstrerror()); |
- return -1; |
- } |
- strm->avail_in = 0; |
- return 0; |
- } |
/* run deflate() on provided input until it produces no more output */ |
ret = Z_OK; |
@@ -97,8 +75,8 @@ |
doing Z_FINISH then don't write until we get to Z_STREAM_END */ |
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && |
(flush != Z_FINISH || ret == Z_STREAM_END))) { |
- have = (unsigned)(strm->next_out - state->x.next); |
- if (have && ((got = write(state->fd, state->x.next, have)) < 0 || |
+ have = (unsigned)(strm->next_out - state->next); |
+ if (have && ((got = write(state->fd, state->next, have)) < 0 || |
(unsigned)got != have)) { |
gz_error(state, Z_ERRNO, zstrerror()); |
return -1; |
@@ -107,7 +85,7 @@ |
strm->avail_out = state->size; |
strm->next_out = state->out; |
} |
- state->x.next = strm->next_out; |
+ state->next = strm->next_out; |
} |
/* compress */ |
@@ -153,7 +131,7 @@ |
} |
strm->avail_in = n; |
strm->next_in = state->in; |
- state->x.pos += n; |
+ state->pos += n; |
if (gz_comp(state, Z_NO_FLUSH) == -1) |
return -1; |
len -= n; |
@@ -168,6 +146,7 @@ |
unsigned len; |
{ |
unsigned put = len; |
+ unsigned n; |
gz_statep state; |
z_streamp strm; |
@@ -184,7 +163,7 @@ |
/* since an int is returned, make sure len fits in one, otherwise return |
with an error (this avoids the flaw in the interface) */ |
if ((int)len < 0) { |
- gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); |
+ gz_error(state, Z_BUF_ERROR, "requested length does not fit in int"); |
return 0; |
} |
@@ -207,19 +186,16 @@ |
if (len < state->size) { |
/* copy to input buffer, compress when full */ |
do { |
- unsigned have, copy; |
- |
if (strm->avail_in == 0) |
strm->next_in = state->in; |
- have = (unsigned)((strm->next_in + strm->avail_in) - state->in); |
- copy = state->size - have; |
- if (copy > len) |
- copy = len; |
- memcpy(state->in + have, buf, copy); |
- strm->avail_in += copy; |
- state->x.pos += copy; |
- buf = (const char *)buf + copy; |
- len -= copy; |
+ n = state->size - strm->avail_in; |
+ if (n > len) |
+ n = len; |
+ memcpy(strm->next_in + strm->avail_in, buf, n); |
+ strm->avail_in += n; |
+ state->pos += n; |
+ buf = (char *)buf + n; |
+ len -= n; |
if (len && gz_comp(state, Z_NO_FLUSH) == -1) |
return 0; |
} while (len); |
@@ -231,8 +207,8 @@ |
/* directly compress user buffer to file */ |
strm->avail_in = len; |
- strm->next_in = (z_const Bytef *)buf; |
- state->x.pos += len; |
+ strm->next_in = (voidp)buf; |
+ state->pos += len; |
if (gz_comp(state, Z_NO_FLUSH) == -1) |
return 0; |
} |
@@ -246,7 +222,6 @@ |
gzFile file; |
int c; |
{ |
- unsigned have; |
unsigned char buf[1]; |
gz_statep state; |
z_streamp strm; |
@@ -270,23 +245,19 @@ |
/* try writing to input buffer for speed (state->size == 0 if buffer not |
initialized) */ |
- if (state->size) { |
+ if (strm->avail_in < state->size) { |
if (strm->avail_in == 0) |
strm->next_in = state->in; |
- have = (unsigned)((strm->next_in + strm->avail_in) - state->in); |
- if (have < state->size) { |
- state->in[have] = c; |
- strm->avail_in++; |
- state->x.pos++; |
- return c & 0xff; |
- } |
+ strm->next_in[strm->avail_in++] = c; |
+ state->pos++; |
+ return c; |
} |
/* no room in buffer or not initialized, use gz_write() */ |
buf[0] = c; |
if (gzwrite(file, buf, 1) != 1) |
return -1; |
- return c & 0xff; |
+ return c; |
} |
/* -- see zlib.h -- */ |
@@ -303,15 +274,16 @@ |
return ret == 0 && len != 0 ? -1 : ret; |
} |
-#if defined(STDC) || defined(Z_HAVE_STDARG_H) |
+#ifdef STDC |
#include <stdarg.h> |
/* -- see zlib.h -- */ |
-int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) |
+int ZEXPORTVA gzprintf (gzFile file, const char *format, ...) |
{ |
int size, len; |
gz_statep state; |
z_streamp strm; |
+ va_list va; |
/* get internal structure */ |
if (file == NULL) |
@@ -341,20 +313,25 @@ |
/* do the printf() into the input buffer, put length in len */ |
size = (int)(state->size); |
state->in[size - 1] = 0; |
+ va_start(va, format); |
#ifdef NO_vsnprintf |
# ifdef HAS_vsprintf_void |
- (void)vsprintf((char *)(state->in), format, va); |
+ (void)vsprintf(state->in, format, va); |
+ va_end(va); |
for (len = 0; len < size; len++) |
if (state->in[len] == 0) break; |
# else |
- len = vsprintf((char *)(state->in), format, va); |
+ len = vsprintf(state->in, format, va); |
+ va_end(va); |
# endif |
#else |
# ifdef HAS_vsnprintf_void |
- (void)vsnprintf((char *)(state->in), size, format, va); |
- len = strlen((char *)(state->in)); |
+ (void)vsnprintf(state->in, size, format, va); |
+ va_end(va); |
+ len = strlen(state->in); |
# else |
len = vsnprintf((char *)(state->in), size, format, va); |
+ va_end(va); |
# endif |
#endif |
@@ -365,22 +342,11 @@ |
/* update buffer and position, defer compression until needed */ |
strm->avail_in = (unsigned)len; |
strm->next_in = state->in; |
- state->x.pos += len; |
+ state->pos += len; |
return len; |
} |
-int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) |
-{ |
- va_list va; |
- int ret; |
- |
- va_start(va, format); |
- ret = gzvprintf(file, format, va); |
- va_end(va); |
- return ret; |
-} |
- |
-#else /* !STDC && !Z_HAVE_STDARG_H */ |
+#else /* !STDC */ |
/* -- see zlib.h -- */ |
int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
@@ -399,10 +365,6 @@ |
return -1; |
state = (gz_statep)file; |
strm = &(state->strm); |
- |
- /* check that can really pass pointer in ints */ |
- if (sizeof(int) != sizeof(void *)) |
- return 0; |
/* check that we're writing and that there's no error */ |
if (state->mode != GZ_WRITE || state->err != Z_OK) |
@@ -428,23 +390,22 @@ |
state->in[size - 1] = 0; |
#ifdef NO_snprintf |
# ifdef HAS_sprintf_void |
- sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, |
+ sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8, |
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
for (len = 0; len < size; len++) |
if (state->in[len] == 0) break; |
# else |
- len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, |
- a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
+ len = sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8, |
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
# endif |
#else |
# ifdef HAS_snprintf_void |
- snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8, |
+ snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8, |
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
- len = strlen((char *)(state->in)); |
+ len = strlen(state->in); |
# else |
- len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, |
- a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, |
- a19, a20); |
+ len = snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8, |
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
# endif |
#endif |
@@ -455,7 +416,7 @@ |
/* update buffer and position, defer compression until needed */ |
strm->avail_in = (unsigned)len; |
strm->next_in = state->in; |
- state->x.pos += len; |
+ state->pos += len; |
return len; |
} |
@@ -539,7 +500,7 @@ |
int ZEXPORT gzclose_w(file) |
gzFile file; |
{ |
- int ret = Z_OK; |
+ int ret = 0; |
gz_statep state; |
/* get internal structure */ |
@@ -554,24 +515,17 @@ |
/* check for seek request */ |
if (state->seek) { |
state->seek = 0; |
- if (gz_zero(state, state->skip) == -1) |
- ret = state->err; |
+ ret += gz_zero(state, state->skip); |
} |
/* flush, free memory, and close file */ |
- if (gz_comp(state, Z_FINISH) == -1) |
- ret = state->err; |
- if (state->size) { |
- if (!state->direct) { |
- (void)deflateEnd(&(state->strm)); |
- free(state->out); |
- } |
- free(state->in); |
- } |
+ ret += gz_comp(state, Z_FINISH); |
+ (void)deflateEnd(&(state->strm)); |
+ free(state->out); |
+ free(state->in); |
gz_error(state, Z_OK, NULL); |
free(state->path); |
- if (close(state->fd) == -1) |
- ret = Z_ERRNO; |
+ ret += close(state->fd); |
free(state); |
- return ret; |
-} |
+ return ret ? Z_ERRNO : Z_OK; |
+} |