version 1.1.1.1, 2012/02/21 16:23:02
|
version 1.1.1.2, 2013/07/22 10:46:14
|
Line 1
|
Line 1
|
/* gzwrite.c -- zlib functions for writing gzip files |
/* gzwrite.c -- zlib functions for writing gzip files |
* Copyright (C) 2004, 2005, 2010 Mark Adler | * Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler |
* For conditions of distribution and use, see copyright notice in zlib.h |
* For conditions of distribution and use, see copyright notice in zlib.h |
*/ |
*/ |
|
|
Line 18 local int gz_init(state)
|
Line 18 local int gz_init(state)
|
int ret; |
int ret; |
z_streamp strm = &(state->strm); |
z_streamp strm = &(state->strm); |
|
|
/* allocate input and output buffers */ | /* allocate input buffer */ |
state->in = malloc(state->want); |
state->in = malloc(state->want); |
state->out = malloc(state->want); | if (state->in == NULL) { |
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"); |
gz_error(state, Z_MEM_ERROR, "out of memory"); |
return -1; |
return -1; |
} |
} |
|
|
/* allocate deflate memory, set up for gzip compression */ | /* only need output buffer and deflate state if compressing */ |
strm->zalloc = Z_NULL; | if (!state->direct) { |
strm->zfree = Z_NULL; | /* allocate output buffer */ |
strm->opaque = Z_NULL; | state->out = malloc(state->want); |
ret = deflateInit2(strm, state->level, Z_DEFLATED, | if (state->out == NULL) { |
15 + 16, 8, state->strategy); | free(state->in); |
if (ret != Z_OK) { | gz_error(state, Z_MEM_ERROR, "out of memory"); |
free(state->in); | return -1; |
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; |
| } |
} |
} |
|
|
/* mark state as initialized */ |
/* mark state as initialized */ |
state->size = state->want; |
state->size = state->want; |
|
|
/* initialize write buffer */ | /* initialize write buffer if compressing */ |
strm->avail_out = state->size; | if (!state->direct) { |
strm->next_out = state->out; | strm->avail_out = state->size; |
state->next = strm->next_out; | strm->next_out = state->out; |
| state->x.next = strm->next_out; |
| } |
return 0; |
return 0; |
} |
} |
|
|
/* Compress whatever is at avail_in and next_in and write to the output file. |
/* 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. |
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, |
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. */ | 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. */ |
local int gz_comp(state, flush) |
local int gz_comp(state, flush) |
gz_statep state; |
gz_statep state; |
int flush; |
int flush; |
Line 68 local int gz_comp(state, flush)
|
Line 79 local int gz_comp(state, flush)
|
if (state->size == 0 && gz_init(state) == -1) |
if (state->size == 0 && gz_init(state) == -1) |
return -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 */ |
/* run deflate() on provided input until it produces no more output */ |
ret = Z_OK; |
ret = Z_OK; |
do { |
do { |
Line 75 local int gz_comp(state, flush)
|
Line 97 local int gz_comp(state, flush)
|
doing Z_FINISH then don't write until we get to Z_STREAM_END */ |
doing Z_FINISH then don't write until we get to Z_STREAM_END */ |
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && |
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && |
(flush != Z_FINISH || ret == Z_STREAM_END))) { |
(flush != Z_FINISH || ret == Z_STREAM_END))) { |
have = (unsigned)(strm->next_out - state->next); | have = (unsigned)(strm->next_out - state->x.next); |
if (have && ((got = write(state->fd, state->next, have)) < 0 || | if (have && ((got = write(state->fd, state->x.next, have)) < 0 || |
(unsigned)got != have)) { |
(unsigned)got != have)) { |
gz_error(state, Z_ERRNO, zstrerror()); |
gz_error(state, Z_ERRNO, zstrerror()); |
return -1; |
return -1; |
Line 85 local int gz_comp(state, flush)
|
Line 107 local int gz_comp(state, flush)
|
strm->avail_out = state->size; |
strm->avail_out = state->size; |
strm->next_out = state->out; |
strm->next_out = state->out; |
} |
} |
state->next = strm->next_out; | state->x.next = strm->next_out; |
} |
} |
|
|
/* compress */ |
/* compress */ |
Line 131 local int gz_zero(state, len)
|
Line 153 local int gz_zero(state, len)
|
} |
} |
strm->avail_in = n; |
strm->avail_in = n; |
strm->next_in = state->in; |
strm->next_in = state->in; |
state->pos += n; | state->x.pos += n; |
if (gz_comp(state, Z_NO_FLUSH) == -1) |
if (gz_comp(state, Z_NO_FLUSH) == -1) |
return -1; |
return -1; |
len -= n; |
len -= n; |
Line 163 int ZEXPORT gzwrite(file, buf, len)
|
Line 185 int ZEXPORT gzwrite(file, buf, len)
|
/* since an int is returned, make sure len fits in one, otherwise return |
/* since an int is returned, make sure len fits in one, otherwise return |
with an error (this avoids the flaw in the interface) */ |
with an error (this avoids the flaw in the interface) */ |
if ((int)len < 0) { |
if ((int)len < 0) { |
gz_error(state, Z_BUF_ERROR, "requested length does not fit in int"); | gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); |
return 0; |
return 0; |
} |
} |
|
|
Line 193 int ZEXPORT gzwrite(file, buf, len)
|
Line 215 int ZEXPORT gzwrite(file, buf, len)
|
n = len; |
n = len; |
memcpy(strm->next_in + strm->avail_in, buf, n); |
memcpy(strm->next_in + strm->avail_in, buf, n); |
strm->avail_in += n; |
strm->avail_in += n; |
state->pos += n; | state->x.pos += n; |
buf = (char *)buf + n; |
buf = (char *)buf + n; |
len -= n; |
len -= n; |
if (len && gz_comp(state, Z_NO_FLUSH) == -1) |
if (len && gz_comp(state, Z_NO_FLUSH) == -1) |
Line 208 int ZEXPORT gzwrite(file, buf, len)
|
Line 230 int ZEXPORT gzwrite(file, buf, len)
|
/* directly compress user buffer to file */ |
/* directly compress user buffer to file */ |
strm->avail_in = len; |
strm->avail_in = len; |
strm->next_in = (voidp)buf; |
strm->next_in = (voidp)buf; |
state->pos += len; | state->x.pos += len; |
if (gz_comp(state, Z_NO_FLUSH) == -1) |
if (gz_comp(state, Z_NO_FLUSH) == -1) |
return 0; |
return 0; |
} |
} |
Line 249 int ZEXPORT gzputc(file, c)
|
Line 271 int ZEXPORT gzputc(file, c)
|
if (strm->avail_in == 0) |
if (strm->avail_in == 0) |
strm->next_in = state->in; |
strm->next_in = state->in; |
strm->next_in[strm->avail_in++] = c; |
strm->next_in[strm->avail_in++] = c; |
state->pos++; | state->x.pos++; |
return c; | return c & 0xff; |
} |
} |
|
|
/* no room in buffer or not initialized, use gz_write() */ |
/* no room in buffer or not initialized, use gz_write() */ |
buf[0] = c; |
buf[0] = c; |
if (gzwrite(file, buf, 1) != 1) |
if (gzwrite(file, buf, 1) != 1) |
return -1; |
return -1; |
return c; | return c & 0xff; |
} |
} |
|
|
/* -- see zlib.h -- */ |
/* -- see zlib.h -- */ |
Line 274 int ZEXPORT gzputs(file, str)
|
Line 296 int ZEXPORT gzputs(file, str)
|
return ret == 0 && len != 0 ? -1 : ret; |
return ret == 0 && len != 0 ? -1 : ret; |
} |
} |
|
|
#ifdef STDC | #if defined(STDC) || defined(Z_HAVE_STDARG_H) |
#include <stdarg.h> |
#include <stdarg.h> |
|
|
/* -- see zlib.h -- */ |
/* -- see zlib.h -- */ |
Line 342 int ZEXPORTVA gzprintf (gzFile file, const char *forma
|
Line 364 int ZEXPORTVA gzprintf (gzFile file, const char *forma
|
/* update buffer and position, defer compression until needed */ |
/* update buffer and position, defer compression until needed */ |
strm->avail_in = (unsigned)len; |
strm->avail_in = (unsigned)len; |
strm->next_in = state->in; |
strm->next_in = state->in; |
state->pos += len; | state->x.pos += len; |
return len; |
return len; |
} |
} |
|
|
#else /* !STDC */ | #else /* !STDC && !Z_HAVE_STDARG_H */ |
|
|
/* -- see zlib.h -- */ |
/* -- see zlib.h -- */ |
int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
Line 366 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4,
|
Line 388 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4,
|
state = (gz_statep)file; |
state = (gz_statep)file; |
strm = &(state->strm); |
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 */ |
/* check that we're writing and that there's no error */ |
if (state->mode != GZ_WRITE || state->err != Z_OK) |
if (state->mode != GZ_WRITE || state->err != Z_OK) |
return 0; |
return 0; |
Line 416 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4,
|
Line 442 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4,
|
/* update buffer and position, defer compression until needed */ |
/* update buffer and position, defer compression until needed */ |
strm->avail_in = (unsigned)len; |
strm->avail_in = (unsigned)len; |
strm->next_in = state->in; |
strm->next_in = state->in; |
state->pos += len; | state->x.pos += len; |
return len; |
return len; |
} |
} |
|
|
Line 500 int ZEXPORT gzsetparams(file, level, strategy)
|
Line 526 int ZEXPORT gzsetparams(file, level, strategy)
|
int ZEXPORT gzclose_w(file) |
int ZEXPORT gzclose_w(file) |
gzFile file; |
gzFile file; |
{ |
{ |
int ret = 0; | int ret = Z_OK; |
gz_statep state; |
gz_statep state; |
|
|
/* get internal structure */ |
/* get internal structure */ |
Line 515 int ZEXPORT gzclose_w(file)
|
Line 541 int ZEXPORT gzclose_w(file)
|
/* check for seek request */ |
/* check for seek request */ |
if (state->seek) { |
if (state->seek) { |
state->seek = 0; |
state->seek = 0; |
ret += gz_zero(state, state->skip); | if (gz_zero(state, state->skip) == -1) |
| ret = state->err; |
} |
} |
|
|
/* flush, free memory, and close file */ |
/* flush, free memory, and close file */ |
ret += gz_comp(state, Z_FINISH); | if (gz_comp(state, Z_FINISH) == -1) |
(void)deflateEnd(&(state->strm)); | ret = state->err; |
free(state->out); | if (!state->direct) { |
| (void)deflateEnd(&(state->strm)); |
| free(state->out); |
| } |
free(state->in); |
free(state->in); |
gz_error(state, Z_OK, NULL); |
gz_error(state, Z_OK, NULL); |
free(state->path); |
free(state->path); |
ret += close(state->fd); | if (close(state->fd) == -1) |
| ret = Z_ERRNO; |
free(state); |
free(state); |
return ret ? Z_ERRNO : Z_OK; | return ret; |
| } |
| |
| /* used by zlibVersion() to get the vsnprintf story from the horse's mouth */ |
| unsigned long ZEXPORT gzflags() |
| { |
| unsigned long flags = 0; |
| #if defined(STDC) || defined(Z_HAVE_STDARG_H) |
| # ifdef NO_vsnprintf |
| flags += 1L << 25; |
| # ifdef HAS_vsprintf_void |
| flags += 1L << 26; |
| # endif |
| # else |
| # ifdef HAS_vsnprintf_void |
| flags += 1L << 26; |
| # endif |
| # endif |
| #else |
| flags += 1L << 24; |
| # ifdef NO_snprintf |
| flags += 1L << 25; |
| # ifdef HAS_sprintf_void |
| flags += 1L << 26; |
| # endif |
| # else |
| # ifdef HAS_snprintf_void |
| flags += 1L << 26; |
| # endif |
| # endif |
| #endif |
| return flags; |
} |
} |