From 5d5b0feb712d5dc8a7e40d08e93a5d5e3ab35305 Mon Sep 17 00:00:00 2001 From: Hans Kristian Rosbach Date: Fri, 4 Jul 2025 21:15:35 +0200 Subject: [PATCH] Split out gz_read_init() from gzlook(), and rename gz_init() to gz_write_init(). This makes gzread.c more like gzwrite.c, and fits in with the new code in gzlib.c. --- gzread.c.in | 31 ++++++++++++++++++------------- gzwrite.c | 12 ++++++------ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/gzread.c.in b/gzread.c.in index e725d54a..0951d597 100644 --- a/gzread.c.in +++ b/gzread.c.in @@ -8,6 +8,7 @@ #include "gzguts.h" /* Local functions */ +static int gz_read_init(gz_state *state); static int gz_load(gz_state *, unsigned char *, unsigned, unsigned *); static int gz_avail(gz_state *); static int gz_look(gz_state *); @@ -16,6 +17,20 @@ static int gz_fetch(gz_state *); static int gz_skip(gz_state *, z_off64_t); static size_t gz_read(gz_state *, void *, size_t); +static int gz_read_init(gz_state *state) { + /* Allocate gz buffers */ + if (gz_buffer_alloc(state) != 0) + return -1; + + /* Initialize inflate state */ + if (PREFIX(inflateInit2)(&(state->strm), MAX_WBITS + 16) != Z_OK) { + gz_buffer_free(state); + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + return 0; +} + /* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from state->fd, and update state->eof, state->err, and state->msg as appropriate. This function needs to loop on read(), since read() is not guaranteed to @@ -81,19 +96,9 @@ static int gz_avail(gz_state *state) { static int gz_look(gz_state *state) { PREFIX3(stream) *strm = &(state->strm); - if (state->size == 0) { - /* Allocate gz buffers */ - if (gz_buffer_alloc(state) != 0) { - return -1; - } - - /* Initialize inflate state */ - if (PREFIX(inflateInit2)(&(state->strm), MAX_WBITS + 16) != Z_OK) { /* gunzip */ - gz_buffer_free(state); - gz_error(state, Z_MEM_ERROR, "out of memory"); - return -1; - } - } + /* allocate memory if this is the first time through */ + if (state->size == 0 && gz_read_init(state) == -1) + return -1; /* get at least the magic bytes in the input buffer */ if (strm->avail_in < 2) { diff --git a/gzwrite.c b/gzwrite.c index 7bac0f77..78c9fbda 100644 --- a/gzwrite.c +++ b/gzwrite.c @@ -9,7 +9,7 @@ #include "gzguts.h" /* Local functions */ -static int gz_init(gz_state *); +static int gz_write_init(gz_state *); static int gz_comp(gz_state *, int); static int gz_zero(gz_state *, z_off64_t); static size_t gz_write(gz_state *, void const *, size_t); @@ -17,7 +17,7 @@ static size_t gz_write(gz_state *, void const *, size_t); /* 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 success. */ -static int gz_init(gz_state *state) { +static int gz_write_init(gz_state *state) { int ret; PREFIX3(stream) *strm = &(state->strm); @@ -45,7 +45,7 @@ static int gz_init(gz_state *state) { } /* 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 or if gz_init() + Return -1 if there is an error writing to the output file or if gz_write_init() fails to allocate memory, 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 @@ -57,7 +57,7 @@ static int gz_comp(gz_state *state, int flush) { PREFIX3(stream) *strm = &(state->strm); /* allocate memory if this is the first time through */ - if (state->size == 0 && gz_init(state) == -1) + if (state->size == 0 && gz_write_init(state) == -1) return -1; /* write directly if requested */ @@ -155,7 +155,7 @@ static size_t gz_write(gz_state *state, void const *buf, size_t len) { return 0; /* allocate memory if this is the first time through */ - if (state->size == 0 && gz_init(state) == -1) + if (state->size == 0 && gz_write_init(state) == -1) return 0; /* check for seek request */ @@ -350,7 +350,7 @@ int Z_EXPORTVA PREFIX(gzvprintf)(gzFile file, const char *format, va_list va) { return Z_STREAM_ERROR; /* make sure we have some buffer space */ - if (state->size == 0 && gz_init(state) == -1) + if (state->size == 0 && gz_write_init(state) == -1) return state->err; /* check for seek request */ -- 2.47.2