From e473ff37e0febb0c0dda71d6d277cc833bc9f7a3 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Sun, 07 May 2023 03:52:23 -0400 Subject: gzip: Split init functions --- (limited to 'src') diff --git a/src/gzip.c b/src/gzip.c index 1263ffc..da4f6a8 100644 --- a/src/gzip.c +++ b/src/gzip.c @@ -41,10 +41,8 @@ struct opkg_opk_gzip { gz_header gz_header; }; -static struct opkg_opk_gzip * -_opkg_opk_gzip_init(enum _opkg_opk_gzip_dir dir, - opkg_opk_gzip_read_func *read_func, - opkg_opk_gzip_write_func *write_func, void *user_data) +struct opkg_opk_gzip * +opkg_opk_gzip_init_read(opkg_opk_gzip_read_func *read_func, void *user_data) { struct opkg_opk_gzip *gzip; @@ -53,61 +51,64 @@ _opkg_opk_gzip_init(enum _opkg_opk_gzip_dir dir, return NULL; } - gzip->dir = dir; + gzip->dir = _OPKG_OPK_GZIP_DIR_READ; gzip->read_func = read_func; - gzip->write_func = write_func; gzip->user_data = user_data; gzip->stream.zalloc = Z_NULL; gzip->stream.zfree = Z_NULL; gzip->stream.opaque = Z_NULL; - if (dir == _OPKG_OPK_GZIP_DIR_READ) { - gzip->stream.next_in = Z_NULL; - gzip->stream.avail_in = 0; - if (inflateInit2(&gzip->stream, OPKG_OPK_GZIP_WINDOW_BITS_) != - Z_OK) { - free(gzip); - return NULL; - } - } else { - gzip->stream.next_out = Z_NULL; - gzip->stream.avail_out = 0; - if (deflateInit2(&gzip->stream, 9, Z_DEFLATED, - OPKG_OPK_GZIP_WINDOW_BITS_, 9, - Z_DEFAULT_STRATEGY) != Z_OK) { - free(gzip); - return NULL; - } - gzip->gz_header.text = 0; - gzip->gz_header.time = 0; /* Stored as 32-bit uint */ - gzip->gz_header.os = 3; /* Unix, per RFC 1952 */ - gzip->gz_header.extra = Z_NULL; - gzip->gz_header.name = Z_NULL; - gzip->gz_header.comment = Z_NULL; - gzip->gz_header.hcrc = 0; - if (deflateSetHeader(&gzip->stream, &gzip->gz_header) != Z_OK) { - deflateEnd(&gzip->stream); - free(gzip); - return NULL; - } + gzip->stream.next_in = Z_NULL; + gzip->stream.avail_in = 0; + if (inflateInit2(&gzip->stream, OPKG_OPK_GZIP_WINDOW_BITS_) != Z_OK) { + free(gzip); + return NULL; } return gzip; } struct opkg_opk_gzip * -opkg_opk_gzip_init_read(opkg_opk_gzip_read_func *read, void *user_data) +opkg_opk_gzip_init_write(opkg_opk_gzip_write_func *write_func, void *user_data) { - return _opkg_opk_gzip_init(_OPKG_OPK_GZIP_DIR_READ, - read, NULL, user_data); -} + struct opkg_opk_gzip *gzip; -struct opkg_opk_gzip * -opkg_opk_gzip_init_write(opkg_opk_gzip_write_func *write, void *user_data) -{ - return _opkg_opk_gzip_init(_OPKG_OPK_GZIP_DIR_WRITE, - NULL, write, user_data); + gzip = malloc(sizeof(*gzip)); + if (gzip == NULL) { + return NULL; + } + + gzip->dir = _OPKG_OPK_GZIP_DIR_WRITE; + gzip->write_func = write_func; + gzip->user_data = user_data; + + gzip->stream.zalloc = Z_NULL; + gzip->stream.zfree = Z_NULL; + gzip->stream.opaque = Z_NULL; + + gzip->stream.next_out = Z_NULL; + gzip->stream.avail_out = 0; + if (deflateInit2(&gzip->stream, 9, Z_DEFLATED, + OPKG_OPK_GZIP_WINDOW_BITS_, 9, + Z_DEFAULT_STRATEGY) != Z_OK) { + free(gzip); + return NULL; + } + gzip->gz_header.text = 0; + gzip->gz_header.time = 0; /* Stored as 32-bit uint */ + gzip->gz_header.os = 3; /* Unix, per RFC 1952 */ + gzip->gz_header.extra = Z_NULL; + gzip->gz_header.name = Z_NULL; + gzip->gz_header.comment = Z_NULL; + gzip->gz_header.hcrc = 0; + if (deflateSetHeader(&gzip->stream, &gzip->gz_header) != Z_OK) { + deflateEnd(&gzip->stream); + free(gzip); + return NULL; + } + + return gzip; } int diff --git a/src/gzip.h b/src/gzip.h index ecf659a..cf017ce 100644 --- a/src/gzip.h +++ b/src/gzip.h @@ -42,10 +42,10 @@ typedef int (opkg_opk_gzip_write_func)(void *, size_t); * - NULL on memory exhaustion. */ struct opkg_opk_gzip * -opkg_opk_gzip_init_read(opkg_opk_gzip_read_func *read, void *user_data); +opkg_opk_gzip_init_read(opkg_opk_gzip_read_func *read_func, void *user_data); struct opkg_opk_gzip * -opkg_opk_gzip_init_write(opkg_opk_gzip_write_func *write, void *user_data); +opkg_opk_gzip_init_write(opkg_opk_gzip_write_func *write_func, void *user_data); int opkg_opk_gzip_set_write_buffer(struct opkg_opk_gzip *gzip, char *buffer, -- cgit v0.9.1