From: Nathan Moinvaziri Date: Sat, 22 Jun 2019 07:25:39 +0000 (-0700) Subject: Remove duplicate gz functions in test code (#348) X-Git-Tag: 1.9.9-b1~473 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=553761a51f0b9af2161bdbb73789b8272df58cb9;p=thirdparty%2Fzlib-ng.git Remove duplicate gz functions in test code (#348) Change CMakeLists.txt so that if WITH_GZFILEOP is OFF, gz* sources are still compiled against the tests which need them. Remove duplicate gz functions from test code. Always compile with gz functions when zlib tests enabled in makefile. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index deaae240f..e6af74114 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -709,18 +709,13 @@ set(ZLIB_SRCS uncompr.c zutil.c ) -if (WITH_GZFILEOP) - set(ZLIB_GZFILE_SRCS - gzclose.c - gzlib.c - gzread.c - gzwrite.c - ) -else (WITH_GZFILEOP) - set(ZLIB_GZFILE_SRCS - ) -endif (WITH_GZFILEOP) +set(ZLIB_GZFILE_SRCS + gzclose.c + gzlib.c + gzread.c + gzwrite.c +) if(NOT MINGW AND NOT MSYS) set(ZLIB_DLL_SRCS @@ -744,11 +739,16 @@ if(MINGW OR MSYS) set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj) endif() -add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_GZFILE_SRCS} ${ZLIB_ARCH_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) +add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ARCH_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) target_include_directories(zlib PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) -add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_GZFILE_SRCS} ${ZLIB_ARCH_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) +add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ARCH_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) target_include_directories(zlibstatic PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) +if(WITH_GZFILEOP) + target_sources(zlib PRIVATE ${ZLIB_GZFILE_SRCS}) + target_sources(zlibstatic PRIVATE ${ZLIB_GZFILE_SRCS}) +endif() + set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL) set_target_properties(zlib PROPERTIES SOVERSION 1) @@ -807,21 +807,29 @@ endif() option(ZLIB_ENABLE_TESTS "Build test binaries" ON) if (ZLIB_ENABLE_TESTS) enable_testing() + macro(configure_test_executable target) + target_link_libraries(${target} zlib) + if(NOT WITH_GZFILEOP) + target_compile_definitions(${target} PUBLIC -DWITH_GZFILEOP) + target_sources(${target} PRIVATE ${ZLIB_GZFILE_SRCS}) + endif() + endmacro() + add_executable(example test/example.c) - target_link_libraries(example zlib) + configure_test_executable(example) add_test(NAME example COMMAND example${CMAKE_EXECUTABLE_SUFFIX}) add_executable(minigzip test/minigzip.c) - target_link_libraries(minigzip zlib) + configure_test_executable(minigzip) if(HAVE_OFF64_T) add_executable(example64 test/example.c) - target_link_libraries(example64 zlib) + configure_test_executable(example64) set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64") add_test(NAME example64 COMMAND example64${CMAKE_EXECUTABLE_SUFFIX}) add_executable(minigzip64 test/minigzip.c) - target_link_libraries(minigzip64 zlib) + configure_test_executable(minigzip64) set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64") endif() @@ -830,7 +838,7 @@ if (ZLIB_ENABLE_TESTS) file(GLOB ALL_SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*") foreach(FUZZER ${FUZZERS}) add_executable(${FUZZER}_fuzzer test/fuzz/${FUZZER}_fuzzer.c test/fuzz/standalone_fuzz_target_runner.c) - target_link_libraries(${FUZZER}_fuzzer zlib) + configure_test_executable(${FUZZER}_fuzzer) add_test(${FUZZER}_fuzzer ${FUZZER}_fuzzer${CMAKE_EXECUTABLE_SUFFIX} ${ALL_SRC_FILES}) endforeach() endif() diff --git a/Makefile.in b/Makefile.in index 1e8269522..69fe01165 100644 --- a/Makefile.in +++ b/Makefile.in @@ -147,7 +147,7 @@ example_flush_fuzzer.o: example_dict_fuzzer.o: $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< minigzip_fuzzer.o: - $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< + $(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $< checksum_fuzzer$(EXE): checksum_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB) $(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) checksum_fuzzer.o $(STATICLIB) -lpthread compress_fuzzer$(EXE): compress_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB) @@ -160,8 +160,8 @@ example_flush_fuzzer$(EXE): example_flush_fuzzer.o standalone_fuzz_target_runner $(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) example_flush_fuzzer.o $(STATICLIB) -lpthread example_dict_fuzzer$(EXE): example_dict_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB) $(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) example_dict_fuzzer.o $(STATICLIB) -lpthread -minigzip_fuzzer$(EXE): minigzip_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB) - $(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) minigzip_fuzzer.o $(STATICLIB) -lpthread +minigzip_fuzzer$(EXE): minigzip_fuzzer.o standalone_fuzz_target_runner.o $(OBJG) $(STATICLIB) + $(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) minigzip_fuzzer.o $(OBJG) $(STATICLIB) -lpthread infcover.o: $(SRCDIR)/test/infcover.c $(SRCDIR)/zlib$(SUFFIX).h zconf$(SUFFIX).h $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/infcover.c @@ -182,16 +182,16 @@ $(STATICLIB): $(OBJS) -@ ($(RANLIB) $@ || true) >/dev/null 2>&1 example.o: - $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/example.c + $(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $(SRCDIR)/test/example.c minigzip.o: - $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c + $(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c example64.o: - $(CC) $(CFLAGS) -D_FILE_OFFSET_BITS=64 $(INCLUDES) -c -o $@ $(SRCDIR)/test/example.c + $(CC) $(CFLAGS) -DWITH_GZFILEOP -D_FILE_OFFSET_BITS=64 $(INCLUDES) -c -o $@ $(SRCDIR)/test/example.c minigzip64.o: - $(CC) $(CFLAGS) -D_FILE_OFFSET_BITS=64 $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c + $(CC) $(CFLAGS) -DWITH_GZFILEOP -D_FILE_OFFSET_BITS=64 $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c zlibrc.o: win32/zlib$(SUFFIX)1.rc $(RC) $(RCFLAGS) -o $@ win32/zlib$(SUFFIX)1.rc @@ -204,6 +204,9 @@ zlibrc.o: win32/zlib$(SUFFIX)1.rc %.lo: $(SRCDIR)/%.c $(CC) $(SFLAGS) -DPIC $(INCLUDES) -c -o $@ $< +$(OBJG): %.o: $(SRCDIR)/%.c + $(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $< + $(SHAREDTARGET): $(PIC_OBJS) $(DEFFILE) $(RCOBJS) ifneq ($(SHAREDTARGET),) $(LDSHARED) $(LDSHAREDFLAGS) $(LDFLAGS) -o $@ $(DEFFILE) $(PIC_OBJS) $(RCOBJS) $(LDSHAREDLIBC) @@ -217,38 +220,38 @@ ifneq ($(SHAREDLIB),$(SHAREDTARGET)) endif endif -example$(EXE): example.o $(STATICLIB) - $(CC) $(LDFLAGS) -o $@ example.o $(TEST_LIBS) $(LDSHAREDLIBC) +example$(EXE): example.o $(OBJG) $(STATICLIB) + $(CC) $(LDFLAGS) -o $@ example.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC) ifneq ($(STRIP),) $(STRIP) $@ endif -minigzip$(EXE): minigzip.o $(STATICLIB) - $(CC) $(LDFLAGS) -o $@ minigzip.o $(TEST_LIBS) $(LDSHAREDLIBC) +minigzip$(EXE): minigzip.o $(OBJG) $(STATICLIB) + $(CC) $(LDFLAGS) -o $@ minigzip.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC) ifneq ($(STRIP),) $(STRIP) $@ endif -examplesh$(EXE): example.o $(SHAREDTARGET) - $(CC) $(LDFLAGS) -o $@ example.o $(SHAREDTARGET) $(LDSHAREDLIBC) +examplesh$(EXE): example.o $(OBJG) $(SHAREDTARGET) + $(CC) $(LDFLAGS) -o $@ example.o $(OBJG) $(SHAREDTARGET) $(LDSHAREDLIBC) ifneq ($(STRIP),) $(STRIP) $@ endif -minigzipsh$(EXE): minigzip.o $(SHAREDTARGET) - $(CC) $(LDFLAGS) -o $@ minigzip.o $(SHAREDTARGET) $(LDSHAREDLIBC) +minigzipsh$(EXE): minigzip.o $(OBJG) $(SHAREDTARGET) + $(CC) $(LDFLAGS) -o $@ minigzip.o $(OBJG) $(SHAREDTARGET) $(LDSHAREDLIBC) ifneq ($(STRIP),) $(STRIP) $@ endif -example64$(EXE): example64.o $(STATICLIB) - $(CC) $(LDFLAGS) -o $@ example64.o $(TEST_LIBS) $(LDSHAREDLIBC) +example64$(EXE): example64.o $(OBJG) $(STATICLIB) + $(CC) $(LDFLAGS) -o $@ example64.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC) ifneq ($(STRIP),) $(STRIP) $@ endif -minigzip64$(EXE): minigzip64.o $(STATICLIB) - $(CC) $(LDFLAGS) -o $@ minigzip64.o $(TEST_LIBS) $(LDSHAREDLIBC) +minigzip64$(EXE): minigzip64.o $(OBJG) $(STATICLIB) + $(CC) $(LDFLAGS) -o $@ minigzip64.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC) ifneq ($(STRIP),) $(STRIP) $@ endif diff --git a/test/example.c b/test/example.c index 13a828b72..964a380d7 100644 --- a/test/example.c +++ b/test/example.c @@ -35,6 +35,9 @@ static const char hello[] = "hello, hello!"; static const char dictionary[] = "hello"; static unsigned long dictId = 0; /* Adler32 value of the dictionary */ + +void test_compress (unsigned char *compr, z_size_t comprLen,unsigned char *uncompr, z_size_t uncomprLen); +void test_gzio (const char *fname, unsigned char *uncompr, z_size_t uncomprLen); void test_deflate (unsigned char *compr, size_t comprLen); void test_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen); void test_large_deflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen); @@ -49,9 +52,6 @@ int main (int argc, char *argv[]); static alloc_func zalloc = NULL; static free_func zfree = NULL; -void test_compress (unsigned char *compr, z_size_t comprLen, - unsigned char *uncompr, z_size_t uncomprLen); - /* =========================================================================== * Test compress() and uncompress() */ @@ -76,10 +76,6 @@ void test_compress(unsigned char *compr, z_size_t comprLen, unsigned char *uncom } } -#ifdef WITH_GZFILEOP -void test_gzio (const char *fname, - unsigned char *uncompr, z_size_t uncomprLen); - /* =========================================================================== * Test read/write of .gz files */ @@ -161,8 +157,6 @@ void test_gzio(const char *fname, unsigned char *uncompr, z_size_t uncomprLen) #endif } -#endif /* WITH_GZFILEOP */ - /* =========================================================================== * Test deflate() with small buffers */ @@ -535,13 +529,8 @@ int main(int argc, char *argv[]) test_compress(compr, comprLen, uncompr, uncomprLen); -#ifdef WITH_GZFILEOP test_gzio((argc > 1 ? argv[1] : TESTFILE), uncompr, uncomprLen); -#else - (void)argc; - (void)argv; -#endif test_deflate(compr, comprLen); test_inflate(compr, comprLen, uncompr, uncomprLen); diff --git a/test/fuzz/minigzip_fuzzer.c b/test/fuzz/minigzip_fuzzer.c index 73d25bb47..d0c35571f 100644 --- a/test/fuzz/minigzip_fuzzer.c +++ b/test/fuzz/minigzip_fuzzer.c @@ -62,205 +62,6 @@ #define BUFLENW (BUFLEN * 3) /* write buffer size */ #define MAX_NAME_LEN 1024 -#ifndef WITH_GZFILEOP -/* without WITH_GZFILEOP, create simplified gz* functions using deflate and inflate */ - -#if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE) -# include /* for unlink() */ -#endif - -void *myalloc (void *, unsigned, unsigned); -void myfree (void *, void *); - -void *myalloc(void *q, unsigned n, unsigned m) -{ - (void)q; -#ifndef UNALIGNED_OK - return memalign(16, n * m); -#else - return calloc(n, m); -#endif -} - -void myfree(void *q, void *p) -{ - (void)q; - free(p); -} - -typedef struct gzFile_s { - FILE *file; - int write; - int err; - const char *msg; - PREFIX3(stream) strm; - unsigned char *buf; -} *gzFile; - -gzFile PREFIX(gzopen)(const char *, const char *); -gzFile PREFIX(gzdopen)(int, const char *); -gzFile gz_open (const char *, int, const char *); - -gzFile PREFIX(gzopen)(const char *path, const char *mode) -{ - return gz_open(path, -1, mode); -} - -gzFile PREFIX(gzdopen)(int fd, const char *mode) -{ - return gz_open(NULL, fd, mode); -} - -gzFile gz_open(const char *path, int fd, const char *mode) -{ - gzFile gz; - int ret; - int level = Z_DEFAULT_COMPRESSION; - const char *plevel = mode; - - gz = malloc(sizeof(struct gzFile_s)); - if (gz == NULL) - return NULL; - gz->write = strchr(mode, 'w') != NULL; - gz->strm.zalloc = myalloc; - gz->strm.zfree = myfree; - gz->strm.opaque = NULL; - gz->buf = malloc(gz->write ? BUFLENW : BUFLEN); - - if (gz->buf == NULL) { - free(gz); - return NULL; - } - - while (*plevel) { - if (*plevel >= '0' && *plevel <= '9') { - level = *plevel - '0'; - break; - } - plevel++; - } - if (gz->write) - ret = PREFIX(deflateInit2)(&(gz->strm), level, 8, 15 + 16, 8, 0); - else { - gz->strm.next_in = NULL; - gz->strm.avail_in = 0; - ret = PREFIX(inflateInit2)(&(gz->strm), 15 + 16); - } - if (ret != Z_OK) { - free(gz); - return NULL; - } -#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE - gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") - : fopen(path, gz->write ? "wb" : "rb"); -#else - /* fdopen is not available in C99. */ - if (path == NULL) - exit(1); - gz->file = fopen(path, gz->write ? "wb" : "rb"); -#endif - if (gz->file == NULL) { - gz->write ? PREFIX(deflateEnd)(&(gz->strm)) : PREFIX(inflateEnd)(&(gz->strm)); - free(gz); - return NULL; - } - gz->err = 0; - gz->msg = ""; - return gz; -} - -int PREFIX(gzwrite)(gzFile, const void *, unsigned); - -int PREFIX(gzwrite)(gzFile gz, const void *buf, unsigned len) -{ - PREFIX3(stream) *strm; - - if (gz == NULL || !gz->write) - return 0; - strm = &(gz->strm); - strm->next_in = (void *)buf; - strm->avail_in = len; - do { - strm->next_out = gz->buf; - strm->avail_out = BUFLENW; - (void)PREFIX(deflate)(strm, Z_NO_FLUSH); - fwrite(gz->buf, 1, BUFLENW - strm->avail_out, gz->file); - } while (strm->avail_out == 0); - return len; -} - -int PREFIX(gzread)(gzFile, void *, unsigned); - -int PREFIX(gzread)(gzFile gz, void *buf, unsigned len) -{ - PREFIX3(stream) *strm; - - if (gz == NULL || gz->write || gz->err) - return 0; - strm = &(gz->strm); - strm->next_out = buf; - strm->avail_out = len; - do { - if (strm->avail_in == 0) - { - strm->next_in = gz->buf; - strm->avail_in = (uint32_t)fread(gz->buf, 1, BUFLEN, gz->file); - } - if (strm->avail_in > 0) - { - int ret = PREFIX(inflate)(strm, Z_NO_FLUSH); - if (ret == Z_DATA_ERROR) { - gz->err = ret; - gz->msg = strm->msg; - return 0; - } - else if (ret == Z_STREAM_END) - PREFIX(inflateReset)(strm); - } - else - break; - } while (strm->avail_out); - return len - strm->avail_out; -} - -int PREFIX(gzclose)(gzFile); - -int PREFIX(gzclose)(gzFile gz) -{ - PREFIX3(stream) *strm; - - if (gz == NULL) - return Z_STREAM_ERROR; - strm = &(gz->strm); - if (gz->write) { - strm->next_in = NULL; - strm->avail_in = 0; - do { - strm->next_out = gz->buf; - strm->avail_out = BUFLENW; - (void)PREFIX(deflate)(strm, Z_FINISH); - fwrite(gz->buf, 1, BUFLENW - strm->avail_out, gz->file); - } while (strm->avail_out == 0); - PREFIX(deflateEnd)(strm); - } - else - PREFIX(inflateEnd)(strm); - free(gz->buf); - fclose(gz->file); - free(gz); - return Z_OK; -} - -const char *PREFIX(gzerror)(gzFile, int *); - -const char *PREFIX(gzerror)(gzFile gz, int *err) -{ - *err = gz->err; - return gz->msg; -} - -#endif - static char *prog; void error (const char *msg); diff --git a/test/minigzip.c b/test/minigzip.c index 67892d400..ebbed9ae3 100644 --- a/test/minigzip.c +++ b/test/minigzip.c @@ -69,198 +69,6 @@ #define BUFLENW (BUFLEN * 3) /* write buffer size */ #define MAX_NAME_LEN 1024 -#ifndef WITH_GZFILEOP -/* without WITH_GZFILEOP, create simplified gz* functions using deflate and inflate */ - -#if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE) -# include /* for unlink() */ -#endif - -void *myalloc (void *, unsigned, unsigned); -void myfree (void *, void *); - -void *myalloc(void *q, unsigned n, unsigned m) -{ - (void)q; -#ifndef UNALIGNED_OK - return memalign(16, n * m); -#else - return calloc(n, m); -#endif -} - -void myfree(void *q, void *p) -{ - (void)q; - free(p); -} - -typedef struct gzFile_s { - FILE *file; - int write; - int err; - const char *msg; - PREFIX3(stream) strm; - unsigned char *buf; -} *gzFile; - -gzFile PREFIX(gzopen)(const char *, const char *); -gzFile PREFIX(gzdopen)(int, const char *); -gzFile gz_open (const char *, int, const char *); - -gzFile PREFIX(gzopen)(const char *path, const char *mode) -{ - return gz_open(path, -1, mode); -} - -gzFile PREFIX(gzdopen)(int fd, const char *mode) -{ - return gz_open(NULL, fd, mode); -} - -gzFile gz_open(const char *path, int fd, const char *mode) -{ - gzFile gz; - int ret; - int level = Z_DEFAULT_COMPRESSION; - const char *plevel = mode; - - gz = malloc(sizeof(struct gzFile_s)); - if (gz == NULL) - return NULL; - gz->write = strchr(mode, 'w') != NULL; - gz->strm.zalloc = myalloc; - gz->strm.zfree = myfree; - gz->strm.opaque = NULL; - gz->buf = malloc(gz->write ? BUFLENW : BUFLEN); - - if (gz->buf == NULL) { - free(gz); - return NULL; - } - - while (*plevel) { - if (*plevel >= '0' && *plevel <= '9') { - level = *plevel - '0'; - break; - } - plevel++; - } - if (gz->write) - ret = PREFIX(deflateInit2)(&(gz->strm), level, 8, 15 + 16, 8, 0); - else { - gz->strm.next_in = NULL; - gz->strm.avail_in = 0; - ret = PREFIX(inflateInit2)(&(gz->strm), 15 + 16); - } - if (ret != Z_OK) { - free(gz); - return NULL; - } - gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") : - fopen(path, gz->write ? "wb" : "rb"); - if (gz->file == NULL) { - gz->write ? PREFIX(deflateEnd)(&(gz->strm)) : PREFIX(inflateEnd)(&(gz->strm)); - free(gz); - return NULL; - } - gz->err = 0; - gz->msg = ""; - return gz; -} - -int PREFIX(gzwrite)(gzFile, const void *, unsigned); - -int PREFIX(gzwrite)(gzFile gz, const void *buf, unsigned len) -{ - PREFIX3(stream) *strm; - - if (gz == NULL || !gz->write) - return 0; - strm = &(gz->strm); - strm->next_in = (void *)buf; - strm->avail_in = len; - do { - strm->next_out = gz->buf; - strm->avail_out = BUFLENW; - (void)PREFIX(deflate)(strm, Z_NO_FLUSH); - fwrite(gz->buf, 1, BUFLENW - strm->avail_out, gz->file); - } while (strm->avail_out == 0); - return len; -} - -int PREFIX(gzread)(gzFile, void *, unsigned); - -int PREFIX(gzread)(gzFile gz, void *buf, unsigned len) -{ - PREFIX3(stream) *strm; - - if (gz == NULL || gz->write || gz->err) - return 0; - strm = &(gz->strm); - strm->next_out = buf; - strm->avail_out = len; - do { - if (strm->avail_in == 0) - { - strm->next_in = gz->buf; - strm->avail_in = (uint32_t)fread(gz->buf, 1, BUFLEN, gz->file); - } - if (strm->avail_in > 0) - { - int ret = PREFIX(inflate)(strm, Z_NO_FLUSH); - if (ret == Z_DATA_ERROR) { - gz->err = ret; - gz->msg = strm->msg; - return 0; - } - else if (ret == Z_STREAM_END) - PREFIX(inflateReset)(strm); - } - else - break; - } while (strm->avail_out); - return len - strm->avail_out; -} - -int PREFIX(gzclose)(gzFile); - -int PREFIX(gzclose)(gzFile gz) -{ - PREFIX3(stream) *strm; - - if (gz == NULL) - return Z_STREAM_ERROR; - strm = &(gz->strm); - if (gz->write) { - strm->next_in = NULL; - strm->avail_in = 0; - do { - strm->next_out = gz->buf; - strm->avail_out = BUFLENW; - (void)PREFIX(deflate)(strm, Z_FINISH); - fwrite(gz->buf, 1, BUFLENW - strm->avail_out, gz->file); - } while (strm->avail_out == 0); - PREFIX(deflateEnd)(strm); - } - else - PREFIX(inflateEnd)(strm); - free(gz->buf); - fclose(gz->file); - free(gz); - return Z_OK; -} - -const char *PREFIX(gzerror)(gzFile, int *); - -const char *PREFIX(gzerror)(gzFile gz, int *err) -{ - *err = gz->err; - return gz->msg; -} - -#endif - static char *prog; void error (const char *msg);