michael@0: /* gzwrite.c -- zlib functions for writing gzip files michael@0: * Copyright (C) 2004, 2005, 2010 Mark Adler michael@0: * For conditions of distribution and use, see copyright notice in zlib.h michael@0: */ michael@0: michael@0: #include "gzguts.h" michael@0: michael@0: /* Local functions */ michael@0: local int gz_init OF((gz_statep)); michael@0: local int gz_comp OF((gz_statep, int)); michael@0: local int gz_zero OF((gz_statep, z_off64_t)); michael@0: michael@0: /* Initialize state for writing a gzip file. Mark initialization by setting michael@0: state->size to non-zero. Return -1 on failure or 0 on success. */ michael@0: local int gz_init(state) michael@0: gz_statep state; michael@0: { michael@0: int ret; michael@0: z_streamp strm = &(state->strm); michael@0: michael@0: /* allocate input and output buffers */ michael@0: state->in = malloc(state->want); michael@0: state->out = malloc(state->want); michael@0: if (state->in == NULL || state->out == NULL) { michael@0: if (state->out != NULL) michael@0: free(state->out); michael@0: if (state->in != NULL) michael@0: free(state->in); michael@0: gz_error(state, Z_MEM_ERROR, "out of memory"); michael@0: return -1; michael@0: } michael@0: michael@0: /* allocate deflate memory, set up for gzip compression */ michael@0: strm->zalloc = Z_NULL; michael@0: strm->zfree = Z_NULL; michael@0: strm->opaque = Z_NULL; michael@0: ret = deflateInit2(strm, state->level, Z_DEFLATED, michael@0: 15 + 16, 8, state->strategy); michael@0: if (ret != Z_OK) { michael@0: free(state->in); michael@0: gz_error(state, Z_MEM_ERROR, "out of memory"); michael@0: return -1; michael@0: } michael@0: michael@0: /* mark state as initialized */ michael@0: state->size = state->want; michael@0: michael@0: /* initialize write buffer */ michael@0: strm->avail_out = state->size; michael@0: strm->next_out = state->out; michael@0: state->next = strm->next_out; michael@0: return 0; michael@0: } michael@0: michael@0: /* Compress whatever is at avail_in and next_in and write to the output file. michael@0: Return -1 if there is an error writing to the output file, otherwise 0. michael@0: flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH, michael@0: then the deflate() state is reset to start a new gzip stream. */ michael@0: local int gz_comp(state, flush) michael@0: gz_statep state; michael@0: int flush; michael@0: { michael@0: int ret, got; michael@0: unsigned have; michael@0: z_streamp strm = &(state->strm); michael@0: michael@0: /* allocate memory if this is the first time through */ michael@0: if (state->size == 0 && gz_init(state) == -1) michael@0: return -1; michael@0: michael@0: /* run deflate() on provided input until it produces no more output */ michael@0: ret = Z_OK; michael@0: do { michael@0: /* write out current buffer contents if full, or if flushing, but if michael@0: doing Z_FINISH then don't write until we get to Z_STREAM_END */ michael@0: if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && michael@0: (flush != Z_FINISH || ret == Z_STREAM_END))) { michael@0: have = (unsigned)(strm->next_out - state->next); michael@0: if (have && ((got = write(state->fd, state->next, have)) < 0 || michael@0: (unsigned)got != have)) { michael@0: gz_error(state, Z_ERRNO, zstrerror()); michael@0: return -1; michael@0: } michael@0: if (strm->avail_out == 0) { michael@0: strm->avail_out = state->size; michael@0: strm->next_out = state->out; michael@0: } michael@0: state->next = strm->next_out; michael@0: } michael@0: michael@0: /* compress */ michael@0: have = strm->avail_out; michael@0: ret = deflate(strm, flush); michael@0: if (ret == Z_STREAM_ERROR) { michael@0: gz_error(state, Z_STREAM_ERROR, michael@0: "internal error: deflate stream corrupt"); michael@0: return -1; michael@0: } michael@0: have -= strm->avail_out; michael@0: } while (have); michael@0: michael@0: /* if that completed a deflate stream, allow another to start */ michael@0: if (flush == Z_FINISH) michael@0: deflateReset(strm); michael@0: michael@0: /* all done, no errors */ michael@0: return 0; michael@0: } michael@0: michael@0: /* Compress len zeros to output. Return -1 on error, 0 on success. */ michael@0: local int gz_zero(state, len) michael@0: gz_statep state; michael@0: z_off64_t len; michael@0: { michael@0: int first; michael@0: unsigned n; michael@0: z_streamp strm = &(state->strm); michael@0: michael@0: /* consume whatever's left in the input buffer */ michael@0: if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) michael@0: return -1; michael@0: michael@0: /* compress len zeros (len guaranteed > 0) */ michael@0: first = 1; michael@0: while (len) { michael@0: n = GT_OFF(state->size) || (z_off64_t)state->size > len ? michael@0: (unsigned)len : state->size; michael@0: if (first) { michael@0: memset(state->in, 0, n); michael@0: first = 0; michael@0: } michael@0: strm->avail_in = n; michael@0: strm->next_in = state->in; michael@0: state->pos += n; michael@0: if (gz_comp(state, Z_NO_FLUSH) == -1) michael@0: return -1; michael@0: len -= n; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORT gzwrite(file, buf, len) michael@0: gzFile file; michael@0: voidpc buf; michael@0: unsigned len; michael@0: { michael@0: unsigned put = len; michael@0: unsigned n; michael@0: gz_statep state; michael@0: z_streamp strm; michael@0: michael@0: /* get internal structure */ michael@0: if (file == NULL) michael@0: return 0; michael@0: state = (gz_statep)file; michael@0: strm = &(state->strm); michael@0: michael@0: /* check that we're writing and that there's no error */ michael@0: if (state->mode != GZ_WRITE || state->err != Z_OK) michael@0: return 0; michael@0: michael@0: /* since an int is returned, make sure len fits in one, otherwise return michael@0: with an error (this avoids the flaw in the interface) */ michael@0: if ((int)len < 0) { michael@0: gz_error(state, Z_BUF_ERROR, "requested length does not fit in int"); michael@0: return 0; michael@0: } michael@0: michael@0: /* if len is zero, avoid unnecessary operations */ michael@0: if (len == 0) michael@0: return 0; michael@0: michael@0: /* allocate memory if this is the first time through */ michael@0: if (state->size == 0 && gz_init(state) == -1) michael@0: return 0; michael@0: michael@0: /* check for seek request */ michael@0: if (state->seek) { michael@0: state->seek = 0; michael@0: if (gz_zero(state, state->skip) == -1) michael@0: return 0; michael@0: } michael@0: michael@0: /* for small len, copy to input buffer, otherwise compress directly */ michael@0: if (len < state->size) { michael@0: /* copy to input buffer, compress when full */ michael@0: do { michael@0: if (strm->avail_in == 0) michael@0: strm->next_in = state->in; michael@0: n = state->size - strm->avail_in; michael@0: if (n > len) michael@0: n = len; michael@0: memcpy(strm->next_in + strm->avail_in, buf, n); michael@0: strm->avail_in += n; michael@0: state->pos += n; michael@0: buf = (char *)buf + n; michael@0: len -= n; michael@0: if (len && gz_comp(state, Z_NO_FLUSH) == -1) michael@0: return 0; michael@0: } while (len); michael@0: } michael@0: else { michael@0: /* consume whatever's left in the input buffer */ michael@0: if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) michael@0: return 0; michael@0: michael@0: /* directly compress user buffer to file */ michael@0: strm->avail_in = len; michael@0: strm->next_in = (voidp)buf; michael@0: state->pos += len; michael@0: if (gz_comp(state, Z_NO_FLUSH) == -1) michael@0: return 0; michael@0: } michael@0: michael@0: /* input was all buffered or compressed (put will fit in int) */ michael@0: return (int)put; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORT gzputc(file, c) michael@0: gzFile file; michael@0: int c; michael@0: { michael@0: unsigned char buf[1]; michael@0: gz_statep state; michael@0: z_streamp strm; michael@0: michael@0: /* get internal structure */ michael@0: if (file == NULL) michael@0: return -1; michael@0: state = (gz_statep)file; michael@0: strm = &(state->strm); michael@0: michael@0: /* check that we're writing and that there's no error */ michael@0: if (state->mode != GZ_WRITE || state->err != Z_OK) michael@0: return -1; michael@0: michael@0: /* check for seek request */ michael@0: if (state->seek) { michael@0: state->seek = 0; michael@0: if (gz_zero(state, state->skip) == -1) michael@0: return -1; michael@0: } michael@0: michael@0: /* try writing to input buffer for speed (state->size == 0 if buffer not michael@0: initialized) */ michael@0: if (strm->avail_in < state->size) { michael@0: if (strm->avail_in == 0) michael@0: strm->next_in = state->in; michael@0: strm->next_in[strm->avail_in++] = c; michael@0: state->pos++; michael@0: return c; michael@0: } michael@0: michael@0: /* no room in buffer or not initialized, use gz_write() */ michael@0: buf[0] = c; michael@0: if (gzwrite(file, buf, 1) != 1) michael@0: return -1; michael@0: return c; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORT gzputs(file, str) michael@0: gzFile file; michael@0: const char *str; michael@0: { michael@0: int ret; michael@0: unsigned len; michael@0: michael@0: /* write string */ michael@0: len = (unsigned)strlen(str); michael@0: ret = gzwrite(file, str, len); michael@0: return ret == 0 && len != 0 ? -1 : ret; michael@0: } michael@0: michael@0: #ifdef STDC michael@0: #include michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORTVA gzprintf (gzFile file, const char *format, ...) michael@0: { michael@0: int size, len; michael@0: gz_statep state; michael@0: z_streamp strm; michael@0: va_list va; michael@0: michael@0: /* get internal structure */ michael@0: if (file == NULL) michael@0: return -1; michael@0: state = (gz_statep)file; michael@0: strm = &(state->strm); michael@0: michael@0: /* check that we're writing and that there's no error */ michael@0: if (state->mode != GZ_WRITE || state->err != Z_OK) michael@0: return 0; michael@0: michael@0: /* make sure we have some buffer space */ michael@0: if (state->size == 0 && gz_init(state) == -1) michael@0: return 0; michael@0: michael@0: /* check for seek request */ michael@0: if (state->seek) { michael@0: state->seek = 0; michael@0: if (gz_zero(state, state->skip) == -1) michael@0: return 0; michael@0: } michael@0: michael@0: /* consume whatever's left in the input buffer */ michael@0: if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) michael@0: return 0; michael@0: michael@0: /* do the printf() into the input buffer, put length in len */ michael@0: size = (int)(state->size); michael@0: state->in[size - 1] = 0; michael@0: va_start(va, format); michael@0: #ifdef NO_vsnprintf michael@0: # ifdef HAS_vsprintf_void michael@0: (void)vsprintf(state->in, format, va); michael@0: va_end(va); michael@0: for (len = 0; len < size; len++) michael@0: if (state->in[len] == 0) break; michael@0: # else michael@0: len = vsprintf(state->in, format, va); michael@0: va_end(va); michael@0: # endif michael@0: #else michael@0: # ifdef HAS_vsnprintf_void michael@0: (void)vsnprintf(state->in, size, format, va); michael@0: va_end(va); michael@0: len = strlen(state->in); michael@0: # else michael@0: len = vsnprintf((char *)(state->in), size, format, va); michael@0: va_end(va); michael@0: # endif michael@0: #endif michael@0: michael@0: /* check that printf() results fit in buffer */ michael@0: if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) michael@0: return 0; michael@0: michael@0: /* update buffer and position, defer compression until needed */ michael@0: strm->avail_in = (unsigned)len; michael@0: strm->next_in = state->in; michael@0: state->pos += len; michael@0: return len; michael@0: } michael@0: michael@0: #else /* !STDC */ michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, michael@0: a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) michael@0: gzFile file; michael@0: const char *format; michael@0: int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, michael@0: a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; michael@0: { michael@0: int size, len; michael@0: gz_statep state; michael@0: z_streamp strm; michael@0: michael@0: /* get internal structure */ michael@0: if (file == NULL) michael@0: return -1; michael@0: state = (gz_statep)file; michael@0: strm = &(state->strm); michael@0: michael@0: /* check that we're writing and that there's no error */ michael@0: if (state->mode != GZ_WRITE || state->err != Z_OK) michael@0: return 0; michael@0: michael@0: /* make sure we have some buffer space */ michael@0: if (state->size == 0 && gz_init(state) == -1) michael@0: return 0; michael@0: michael@0: /* check for seek request */ michael@0: if (state->seek) { michael@0: state->seek = 0; michael@0: if (gz_zero(state, state->skip) == -1) michael@0: return 0; michael@0: } michael@0: michael@0: /* consume whatever's left in the input buffer */ michael@0: if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) michael@0: return 0; michael@0: michael@0: /* do the printf() into the input buffer, put length in len */ michael@0: size = (int)(state->size); michael@0: state->in[size - 1] = 0; michael@0: #ifdef NO_snprintf michael@0: # ifdef HAS_sprintf_void michael@0: sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8, michael@0: a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); michael@0: for (len = 0; len < size; len++) michael@0: if (state->in[len] == 0) break; michael@0: # else michael@0: len = sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8, michael@0: a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); michael@0: # endif michael@0: #else michael@0: # ifdef HAS_snprintf_void michael@0: snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8, michael@0: a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); michael@0: len = strlen(state->in); michael@0: # else michael@0: len = snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8, michael@0: a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); michael@0: # endif michael@0: #endif michael@0: michael@0: /* check that printf() results fit in buffer */ michael@0: if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) michael@0: return 0; michael@0: michael@0: /* update buffer and position, defer compression until needed */ michael@0: strm->avail_in = (unsigned)len; michael@0: strm->next_in = state->in; michael@0: state->pos += len; michael@0: return len; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORT gzflush(file, flush) michael@0: gzFile file; michael@0: int flush; michael@0: { michael@0: gz_statep state; michael@0: michael@0: /* get internal structure */ michael@0: if (file == NULL) michael@0: return -1; michael@0: state = (gz_statep)file; michael@0: michael@0: /* check that we're writing and that there's no error */ michael@0: if (state->mode != GZ_WRITE || state->err != Z_OK) michael@0: return Z_STREAM_ERROR; michael@0: michael@0: /* check flush parameter */ michael@0: if (flush < 0 || flush > Z_FINISH) michael@0: return Z_STREAM_ERROR; michael@0: michael@0: /* check for seek request */ michael@0: if (state->seek) { michael@0: state->seek = 0; michael@0: if (gz_zero(state, state->skip) == -1) michael@0: return -1; michael@0: } michael@0: michael@0: /* compress remaining data with requested flush */ michael@0: gz_comp(state, flush); michael@0: return state->err; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORT gzsetparams(file, level, strategy) michael@0: gzFile file; michael@0: int level; michael@0: int strategy; michael@0: { michael@0: gz_statep state; michael@0: z_streamp strm; michael@0: michael@0: /* get internal structure */ michael@0: if (file == NULL) michael@0: return Z_STREAM_ERROR; michael@0: state = (gz_statep)file; michael@0: strm = &(state->strm); michael@0: michael@0: /* check that we're writing and that there's no error */ michael@0: if (state->mode != GZ_WRITE || state->err != Z_OK) michael@0: return Z_STREAM_ERROR; michael@0: michael@0: /* if no change is requested, then do nothing */ michael@0: if (level == state->level && strategy == state->strategy) michael@0: return Z_OK; michael@0: michael@0: /* check for seek request */ michael@0: if (state->seek) { michael@0: state->seek = 0; michael@0: if (gz_zero(state, state->skip) == -1) michael@0: return -1; michael@0: } michael@0: michael@0: /* change compression parameters for subsequent input */ michael@0: if (state->size) { michael@0: /* flush previous input with previous parameters before changing */ michael@0: if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1) michael@0: return state->err; michael@0: deflateParams(strm, level, strategy); michael@0: } michael@0: state->level = level; michael@0: state->strategy = strategy; michael@0: return Z_OK; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORT gzclose_w(file) michael@0: gzFile file; michael@0: { michael@0: int ret = 0; michael@0: gz_statep state; michael@0: michael@0: /* get internal structure */ michael@0: if (file == NULL) michael@0: return Z_STREAM_ERROR; michael@0: state = (gz_statep)file; michael@0: michael@0: /* check that we're writing */ michael@0: if (state->mode != GZ_WRITE) michael@0: return Z_STREAM_ERROR; michael@0: michael@0: /* check for seek request */ michael@0: if (state->seek) { michael@0: state->seek = 0; michael@0: ret += gz_zero(state, state->skip); michael@0: } michael@0: michael@0: /* flush, free memory, and close file */ michael@0: ret += gz_comp(state, Z_FINISH); michael@0: (void)deflateEnd(&(state->strm)); michael@0: free(state->out); michael@0: free(state->in); michael@0: gz_error(state, Z_OK, NULL); michael@0: free(state->path); michael@0: ret += close(state->fd); michael@0: free(state); michael@0: return ret ? Z_ERRNO : Z_OK; michael@0: }