]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Split out gz_read_init() from gzlook(), and rename gz_init() to gz_write_init(). gzalloc 1928/head
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Fri, 4 Jul 2025 19:15:35 +0000 (21:15 +0200)
committerHans Kristian Rosbach <hk-git@circlestorm.org>
Fri, 4 Jul 2025 20:15:55 +0000 (22:15 +0200)
This makes gzread.c more like gzwrite.c, and fits in with the new code in gzlib.c.

gzread.c.in
gzwrite.c

index e725d54a2c4cd352ad1345845184211a6c7c5c03..0951d597f17d4aa578b1568d6d2875c9e2666015 100644 (file)
@@ -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) {
index 7bac0f77563714eb398c3b02ac93a42656200668..78c9fbda6f41b53c6f5218c52486171e967683e4 100644 (file)
--- 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 */