Add gzfwrite(), duplicating the interface of fwrite().
This commit is contained in:
parent
44dfd831d2
commit
77bc4f8944
140
gzwrite.c
140
gzwrite.c
@ -9,6 +9,7 @@
|
|||||||
local int gz_init OF((gz_statep));
|
local int gz_init OF((gz_statep));
|
||||||
local int gz_comp OF((gz_statep, int));
|
local int gz_comp OF((gz_statep, int));
|
||||||
local int gz_zero OF((gz_statep, z_off64_t));
|
local int gz_zero OF((gz_statep, z_off64_t));
|
||||||
|
local z_size_t gz_write OF((gz_statep, voidpc, z_size_t));
|
||||||
|
|
||||||
/* Initialize state for writing a gzip file. Mark initialization by setting
|
/* Initialize state for writing a gzip file. Mark initialization by setting
|
||||||
state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
|
state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
|
||||||
@ -170,32 +171,14 @@ local int gz_zero(state, len)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -- see zlib.h -- */
|
/* Write len bytes from buf to file. Return the number of bytes written. If
|
||||||
int ZEXPORT gzwrite(file, buf, len)
|
the returned value is less than len, then there was an error. */
|
||||||
gzFile file;
|
local z_size_t gz_write(state, buf, len)
|
||||||
voidpc buf;
|
|
||||||
unsigned len;
|
|
||||||
{
|
|
||||||
unsigned put = len;
|
|
||||||
gz_statep state;
|
gz_statep state;
|
||||||
z_streamp strm;
|
voidpc buf;
|
||||||
|
z_size_t len;
|
||||||
/* get internal structure */
|
{
|
||||||
if (file == NULL)
|
z_size_t put = len;
|
||||||
return 0;
|
|
||||||
state = (gz_statep)file;
|
|
||||||
strm = &(state->strm);
|
|
||||||
|
|
||||||
/* check that we're writing and that there's no error */
|
|
||||||
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
/* 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");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if len is zero, avoid unnecessary operations */
|
/* if len is zero, avoid unnecessary operations */
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
@ -218,14 +201,15 @@ int ZEXPORT gzwrite(file, buf, len)
|
|||||||
do {
|
do {
|
||||||
unsigned have, copy;
|
unsigned have, copy;
|
||||||
|
|
||||||
if (strm->avail_in == 0)
|
if (state->strm.avail_in == 0)
|
||||||
strm->next_in = state->in;
|
state->strm.next_in = state->in;
|
||||||
have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
|
have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
|
||||||
|
state->in);
|
||||||
copy = state->size - have;
|
copy = state->size - have;
|
||||||
if (copy > len)
|
if (copy > len)
|
||||||
copy = len;
|
copy = len;
|
||||||
memcpy(state->in + have, buf, copy);
|
memcpy(state->in + have, buf, copy);
|
||||||
strm->avail_in += copy;
|
state->strm.avail_in += copy;
|
||||||
state->x.pos += copy;
|
state->x.pos += copy;
|
||||||
buf = (const char *)buf + copy;
|
buf = (const char *)buf + copy;
|
||||||
len -= copy;
|
len -= copy;
|
||||||
@ -235,19 +219,83 @@ int ZEXPORT gzwrite(file, buf, len)
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* consume whatever's left in the input buffer */
|
/* consume whatever's left in the input buffer */
|
||||||
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* directly compress user buffer to file */
|
/* directly compress user buffer to file */
|
||||||
strm->avail_in = len;
|
state->strm.next_in = (z_const Bytef *)buf;
|
||||||
strm->next_in = (z_const Bytef *)buf;
|
do {
|
||||||
state->x.pos += len;
|
unsigned n = -1;
|
||||||
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
if (n > len)
|
||||||
return 0;
|
n = len;
|
||||||
|
state->strm.avail_in = n;
|
||||||
|
state->x.pos += n;
|
||||||
|
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||||
|
return 0;
|
||||||
|
len -= n;
|
||||||
|
} while (len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* input was all buffered or compressed (put will fit in int) */
|
/* input was all buffered or compressed */
|
||||||
return (int)put;
|
return put;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -- see zlib.h -- */
|
||||||
|
int ZEXPORT gzwrite(file, buf, len)
|
||||||
|
gzFile file;
|
||||||
|
voidpc buf;
|
||||||
|
unsigned len;
|
||||||
|
{
|
||||||
|
gz_statep state;
|
||||||
|
|
||||||
|
/* get internal structure */
|
||||||
|
if (file == NULL)
|
||||||
|
return 0;
|
||||||
|
state = (gz_statep)file;
|
||||||
|
|
||||||
|
/* check that we're writing and that there's no error */
|
||||||
|
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* since an int is returned, make sure len fits in one, otherwise return
|
||||||
|
with an error (this avoids a flaw in the interface) */
|
||||||
|
if ((int)len < 0) {
|
||||||
|
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* write len bytes from buf (the return value will fit in an int) */
|
||||||
|
return (int)gz_write(state, buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -- see zlib.h -- */
|
||||||
|
z_size_t ZEXPORT gzfwrite(buf, size, nitems, file)
|
||||||
|
voidpc buf;
|
||||||
|
z_size_t size;
|
||||||
|
z_size_t nitems;
|
||||||
|
gzFile file;
|
||||||
|
{
|
||||||
|
z_size_t len;
|
||||||
|
gz_statep state;
|
||||||
|
|
||||||
|
/* get internal structure */
|
||||||
|
if (file == NULL)
|
||||||
|
return 0;
|
||||||
|
state = (gz_statep)file;
|
||||||
|
|
||||||
|
/* check that we're writing and that there's no error */
|
||||||
|
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* compute bytes to read -- error on overflow */
|
||||||
|
len = nitems * size;
|
||||||
|
if (size && len / size != nitems) {
|
||||||
|
gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* write len bytes to buf, return the number of full items written */
|
||||||
|
return len ? gz_write(state, buf, len) / size : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -- see zlib.h -- */
|
/* -- see zlib.h -- */
|
||||||
@ -293,7 +341,7 @@ int ZEXPORT gzputc(file, c)
|
|||||||
|
|
||||||
/* no room in buffer or not initialized, use gz_write() */
|
/* no room in buffer or not initialized, use gz_write() */
|
||||||
buf[0] = (unsigned char)c;
|
buf[0] = (unsigned char)c;
|
||||||
if (gzwrite(file, buf, 1) != 1)
|
if (gz_write(state, buf, 1) != 1)
|
||||||
return -1;
|
return -1;
|
||||||
return c & 0xff;
|
return c & 0xff;
|
||||||
}
|
}
|
||||||
@ -304,11 +352,21 @@ int ZEXPORT gzputs(file, str)
|
|||||||
const char *str;
|
const char *str;
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
unsigned len;
|
z_size_t len;
|
||||||
|
gz_statep state;
|
||||||
|
|
||||||
|
/* get internal structure */
|
||||||
|
if (file == NULL)
|
||||||
|
return -1;
|
||||||
|
state = (gz_statep)file;
|
||||||
|
|
||||||
|
/* check that we're writing and that there's no error */
|
||||||
|
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
||||||
|
return -1;
|
||||||
|
|
||||||
/* write string */
|
/* write string */
|
||||||
len = (unsigned)strlen(str);
|
len = strlen(str);
|
||||||
ret = gzwrite(file, str, len);
|
ret = gz_write(state, str, len);
|
||||||
return ret == 0 && len != 0 ? -1 : ret;
|
return ret == 0 && len != 0 ? -1 : ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
zlib.h
14
zlib.h
@ -1427,6 +1427,20 @@ ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
|
|||||||
error.
|
error.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size,
|
||||||
|
z_size_t nitems, gzFile file));
|
||||||
|
/*
|
||||||
|
gzfwrite() writes nitems items of size size from buf to file, duplicating
|
||||||
|
the interface of stdio's fwrite(), with size_t request and return types. If
|
||||||
|
the library defines size_t, then z_size_t is identical to size_t. If not,
|
||||||
|
then z_size_t is an unsigned integer type that can contain a pointer.
|
||||||
|
|
||||||
|
gzfwrite() returns the number of full items written of size size, or zero
|
||||||
|
if there was an error. If the multiplication of size and nitems overflows,
|
||||||
|
i.e. the product does not fit in a z_size_t, then nothing is written, zero
|
||||||
|
is returned, and the error state is set to Z_STREAM_ERROR.
|
||||||
|
*/
|
||||||
|
|
||||||
ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...));
|
ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...));
|
||||||
/*
|
/*
|
||||||
Converts, formats, and writes the arguments to the compressed file under
|
Converts, formats, and writes the arguments to the compressed file under
|
||||||
|
Loading…
Reference in New Issue
Block a user