Skip to content

Commit f5e888a

Browse files
committed
Add function prefix (zng_) to all exported functions to allow zlib-ng
to co-exist in an application that has been linked to something that depends on stock zlib. Previously, that would cause random problems since there is no way to guarantee what zlib version is being used for each dynamically linked function. Add the corresponding zlib-ng.h. Tests, example and minigzip will not compile before they have been adapted to use the correct functions as well. Either duplicate them, so we have minigzip-ng.c for example, or add compile-time detection in the source code.
1 parent dfdb082 commit f5e888a

19 files changed

+1941
-104
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
506506

507507
set(ZLIB_PUBLIC_HDRS
508508
${CMAKE_CURRENT_BINARY_DIR}/zconf.h
509-
zlib.h
509+
zlib${SUFFIX}.h
510510
)
511511
set(ZLIB_PRIVATE_HDRS
512512
crc32.h
@@ -558,7 +558,7 @@ if(NOT MINGW AND NOT MSYS)
558558
endif()
559559

560560
# parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
561-
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
561+
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib${SUFFIX}.h _zlib_h_contents)
562562
string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
563563
"\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})
564564

@@ -617,7 +617,7 @@ if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
617617
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
618618
endif()
619619
if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
620-
install(FILES zlib.h DESTINATION "${INSTALL_INC_DIR}" RENAME zlib${SUFFIX}.h)
620+
install(FILES zlib${SUFFIX}.h DESTINATION "${INSTALL_INC_DIR}" RENAME zlib${SUFFIX}.h)
621621
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/zconf.h DESTINATION "${INSTALL_INC_DIR}" RENAME zconf${SUFFIX}.h)
622622
endif()
623623
if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )

Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ install-libs: install-shared install-static
239239
install: install-libs
240240
-@if [ ! -d $(DESTDIR)$(includedir) ]; then mkdir -p $(DESTDIR)$(includedir); fi
241241
rm -f $(DESTDIR)$(includedir)/zlib$(SUFFIX).h $(DESTDIR)$(includedir)/zconf$(SUFFIX).h
242-
cp $(SRCDIR)/zlib.h $(DESTDIR)$(includedir)/zlib$(SUFFIX).h
242+
cp $(SRCDIR)/zlib$(SUFFIX).h $(DESTDIR)$(includedir)/zlib$(SUFFIX).h
243243
cp zconf.h $(DESTDIR)$(includedir)/zconf$(SUFFIX).h
244244
chmod 644 $(DESTDIR)$(includedir)/zlib$(SUFFIX).h $(DESTDIR)$(includedir)/zconf$(SUFFIX).h
245245

adler32.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ uint32_t adler32_c(uint32_t adler, const unsigned char *buf, size_t len) {
144144
return adler | (sum2 << 16);
145145
}
146146

147-
uint32_t ZEXPORT adler32_z(uint32_t adler, const unsigned char *buf, size_t len) {
147+
uint32_t ZEXPORT PREFIX(adler32_z)(uint32_t adler, const unsigned char *buf, size_t len) {
148148
return functable.adler32(adler, buf, len);
149149
}
150150

151151
/* ========================================================================= */
152-
uint32_t ZEXPORT adler32(uint32_t adler, const unsigned char *buf, uint32_t len) {
152+
uint32_t ZEXPORT PREFIX(adler32)(uint32_t adler, const unsigned char *buf, uint32_t len) {
153153
return functable.adler32(adler, buf, len);
154154
}
155155

@@ -179,10 +179,10 @@ static uint32_t adler32_combine_(uint32_t adler1, uint32_t adler2, z_off64_t len
179179
}
180180

181181
/* ========================================================================= */
182-
uint32_t ZEXPORT adler32_combine(uint32_t adler1, uint32_t adler2, z_off_t len2) {
182+
uint32_t ZEXPORT PREFIX(adler32_combine)(uint32_t adler1, uint32_t adler2, z_off_t len2) {
183183
return adler32_combine_(adler1, adler2, len2);
184184
}
185185

186-
uint32_t ZEXPORT adler32_combine64(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
186+
uint32_t ZEXPORT PREFIX(adler32_combine64)(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
187187
return adler32_combine_(adler1, adler2, len2);
188188
}

compress.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
/* @(#) $Id$ */
77

88
#define ZLIB_INTERNAL
9-
#include "zlib.h"
9+
#if defined(ZLIB_COMPAT)
10+
# include "zlib.h"
11+
#else
12+
# include "zlib-ng.h"
13+
#endif
1014

1115
/* ===========================================================================
1216
Compresses the source buffer into the destination buffer. The level
@@ -19,7 +23,7 @@
1923
memory, Z_BUF_ERROR if there was not enough room in the output buffer,
2024
Z_STREAM_ERROR if the level parameter is invalid.
2125
*/
22-
int ZEXPORT compress2(unsigned char *dest, size_t *destLen, const unsigned char *source,
26+
int ZEXPORT PREFIX(compress2)(unsigned char *dest, size_t *destLen, const unsigned char *source,
2327
size_t sourceLen, int level) {
2428
z_stream stream;
2529
int err;
@@ -61,14 +65,14 @@ int ZEXPORT compress2(unsigned char *dest, size_t *destLen, const unsigned char
6165

6266
/* ===========================================================================
6367
*/
64-
int ZEXPORT compress(unsigned char *dest, size_t *destLen, const unsigned char *source, size_t sourceLen) {
65-
return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
68+
int ZEXPORT PREFIX(compress)(unsigned char *dest, size_t *destLen, const unsigned char *source, size_t sourceLen) {
69+
return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
6670
}
6771

6872
/* ===========================================================================
6973
If the default memLevel or windowBits for deflateInit() is changed, then
7074
this function needs to be updated.
7175
*/
72-
size_t ZEXPORT compressBound(size_t sourceLen) {
76+
size_t ZEXPORT PREFIX(compressBound)(size_t sourceLen) {
7377
return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + (sourceLen >> 25) + 13;
7478
}

configure

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ MAPNAME=$LIBNAME2.map
234234

235235
# extract zlib version numbers from zlib.h
236236
if test $compat -eq 0; then
237-
VER=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\(.*\)".*/\1/p' < ${SRCDIR}/zlib.h`
238-
VER3=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\([0-9]*\\.[0-9]*\\.[0-9]*\).*/\1/p' < ${SRCDIR}/zlib.h`
239-
VER2=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\([0-9]*\\.[0-9]*\)\\..*/\1/p' < ${SRCDIR}/zlib.h`
240-
VER1=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\([0-9]*\)\\..*/\1/p' < ${SRCDIR}/zlib.h`
237+
VER=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\(.*\)".*/\1/p' < ${SRCDIR}/zlib-ng.h`
238+
VER3=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\([0-9]*\\.[0-9]*\\.[0-9]*\).*/\1/p' < ${SRCDIR}/zlib-ng.h`
239+
VER2=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\([0-9]*\\.[0-9]*\)\\..*/\1/p' < ${SRCDIR}/zlib-ng.h`
240+
VER1=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\([0-9]*\)\\..*/\1/p' < ${SRCDIR}/zlib-ng.h`
241241
else
242242
VER=`sed -n -e '/ZLIB_VERSION "/s/.*"\(.*\)".*/\1/p' < ${SRCDIR}/zlib.h`
243243
VER3=`sed -n -e '/ZLIB_VERSION "/s/.*"\([0-9]*\\.[0-9]*\\.[0-9]*\).*/\1/p' < ${SRCDIR}/zlib.h`

crc32.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,15 @@ static void write_table(FILE *out, const uint32_t *table) {
205205
/* =========================================================================
206206
* This function can be used by asm versions of crc32()
207207
*/
208-
const uint32_t * ZEXPORT get_crc_table(void) {
208+
const uint32_t * ZEXPORT PREFIX(get_crc_table)(void) {
209209
#ifdef DYNAMIC_CRC_TABLE
210210
if (crc_table_empty)
211211
make_crc_table();
212212
#endif /* DYNAMIC_CRC_TABLE */
213213
return (const uint32_t *)crc_table;
214214
}
215215

216-
uint32_t ZEXPORT crc32_z(uint32_t crc, const unsigned char *buf, size_t len) {
216+
uint32_t ZEXPORT PREFIX(crc32_z)(uint32_t crc, const unsigned char *buf, size_t len) {
217217
if (buf == NULL) return 0;
218218

219219
#ifdef DYNAMIC_CRC_TABLE
@@ -264,8 +264,8 @@ ZLIB_INTERNAL uint32_t crc32_generic(uint32_t crc, const unsigned char *buf, z_o
264264
return crc ^ 0xffffffff;
265265
}
266266

267-
uint32_t ZEXPORT crc32(uint32_t crc, const unsigned char *buf, uint32_t len) {
268-
return crc32_z(crc, buf, len);
267+
uint32_t ZEXPORT PREFIX(crc32)(uint32_t crc, const unsigned char *buf, uint32_t len) {
268+
return PREFIX(crc32_z)(crc, buf, len);
269269
}
270270

271271
/*
@@ -442,11 +442,11 @@ static uint32_t crc32_combine_(uint32_t crc1, uint32_t crc2, z_off64_t len2) {
442442
}
443443

444444
/* ========================================================================= */
445-
uint32_t ZEXPORT crc32_combine(uint32_t crc1, uint32_t crc2, z_off_t len2) {
445+
uint32_t ZEXPORT PREFIX(crc32_combine)(uint32_t crc1, uint32_t crc2, z_off_t len2) {
446446
return crc32_combine_(crc1, crc2, len2);
447447
}
448448

449-
uint32_t ZEXPORT crc32_combine64(uint32_t crc1, uint32_t crc2, z_off64_t len2) {
449+
uint32_t ZEXPORT PREFIX(crc32_combine64)(uint32_t crc1, uint32_t crc2, z_off64_t len2) {
450450
return crc32_combine_(crc1, crc2, len2);
451451
}
452452

deflate.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ static void slide_hash(deflate_state *s) {
219219
}
220220

221221
/* ========================================================================= */
222-
int ZEXPORT deflateInit_(z_stream *strm, int level, const char *version, int stream_size) {
223-
return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size);
222+
int ZEXPORT PREFIX(deflateInit_)(z_stream *strm, int level, const char *version, int stream_size) {
223+
return PREFIX(deflateInit2_)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size);
224224
/* Todo: ignore strm->next_in if we use it as window */
225225
}
226226

227227
/* ========================================================================= */
228-
int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
228+
int ZEXPORT PREFIX(deflateInit2_)(z_stream *strm, int level, int method, int windowBits,
229229
int memLevel, int strategy, const char *version, int stream_size) {
230230
unsigned window_padding = 0;
231231
deflate_state *s;
@@ -364,7 +364,7 @@ static int deflateStateCheck (z_stream *strm) {
364364
}
365365

366366
/* ========================================================================= */
367-
int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary, unsigned int dictLength) {
367+
int ZEXPORT PREFIX(deflateSetDictionary)(z_stream *strm, const unsigned char *dictionary, unsigned int dictLength) {
368368
deflate_state *s;
369369
unsigned int str, n;
370370
int wrap;
@@ -422,7 +422,7 @@ int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary
422422
}
423423

424424
/* ========================================================================= */
425-
int ZEXPORT deflateGetDictionary (z_stream *strm, unsigned char *dictionary, unsigned int *dictLength) {
425+
int ZEXPORT PREFIX(deflateGetDictionary)(z_stream *strm, unsigned char *dictionary, unsigned int *dictLength) {
426426
deflate_state *s;
427427
unsigned int len;
428428

@@ -440,7 +440,7 @@ int ZEXPORT deflateGetDictionary (z_stream *strm, unsigned char *dictionary, uns
440440
}
441441

442442
/* ========================================================================= */
443-
int ZEXPORT deflateResetKeep(z_stream *strm) {
443+
int ZEXPORT PREFIX(deflateResetKeep)(z_stream *strm) {
444444
deflate_state *s;
445445

446446
if (deflateStateCheck(strm)) {
@@ -478,7 +478,7 @@ int ZEXPORT deflateResetKeep(z_stream *strm) {
478478
}
479479

480480
/* ========================================================================= */
481-
int ZEXPORT deflateReset(z_stream *strm) {
481+
int ZEXPORT PREFIX(deflateReset)(z_stream *strm) {
482482
int ret;
483483

484484
ret = deflateResetKeep(strm);
@@ -488,15 +488,15 @@ int ZEXPORT deflateReset(z_stream *strm) {
488488
}
489489

490490
/* ========================================================================= */
491-
int ZEXPORT deflateSetHeader(z_stream *strm, gz_headerp head) {
491+
int ZEXPORT PREFIX(deflateSetHeader)(z_stream *strm, gz_headerp head) {
492492
if (deflateStateCheck(strm) || strm->state->wrap != 2)
493493
return Z_STREAM_ERROR;
494494
strm->state->gzhead = head;
495495
return Z_OK;
496496
}
497497

498498
/* ========================================================================= */
499-
int ZEXPORT deflatePending(z_stream *strm, uint32_t *pending, int *bits) {
499+
int ZEXPORT PREFIX(deflatePending)(z_stream *strm, uint32_t *pending, int *bits) {
500500
if (deflateStateCheck(strm))
501501
return Z_STREAM_ERROR;
502502
if (pending != NULL)
@@ -507,7 +507,7 @@ int ZEXPORT deflatePending(z_stream *strm, uint32_t *pending, int *bits) {
507507
}
508508

509509
/* ========================================================================= */
510-
int ZEXPORT deflatePrime(z_stream *strm, int bits, int value) {
510+
int ZEXPORT PREFIX(deflatePrime)(z_stream *strm, int bits, int value) {
511511
deflate_state *s;
512512
int put;
513513

@@ -530,7 +530,7 @@ int ZEXPORT deflatePrime(z_stream *strm, int bits, int value) {
530530
}
531531

532532
/* ========================================================================= */
533-
int ZEXPORT deflateParams(z_stream *strm, int level, int strategy) {
533+
int ZEXPORT PREFIX(deflateParams)(z_stream *strm, int level, int strategy) {
534534
deflate_state *s;
535535
compress_func func;
536536

@@ -573,7 +573,7 @@ int ZEXPORT deflateParams(z_stream *strm, int level, int strategy) {
573573
}
574574

575575
/* ========================================================================= */
576-
int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_length, int max_chain) {
576+
int ZEXPORT PREFIX(deflateTune)(z_stream *strm, int good_length, int max_lazy, int nice_length, int max_chain) {
577577
deflate_state *s;
578578

579579
if (deflateStateCheck(strm))
@@ -603,7 +603,7 @@ int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_
603603
* upper bound of about 14% expansion does not seem onerous for output buffer
604604
* allocation.
605605
*/
606-
unsigned long ZEXPORT deflateBound(z_stream *strm, unsigned long sourceLen) {
606+
unsigned long ZEXPORT PREFIX(deflateBound)(z_stream *strm, unsigned long sourceLen) {
607607
deflate_state *s;
608608
unsigned long complen, wraplen;
609609

@@ -708,7 +708,7 @@ ZLIB_INTERNAL void flush_pending(z_stream *strm) {
708708
} while (0)
709709

710710
/* ========================================================================= */
711-
int ZEXPORT deflate(z_stream *strm, int flush) {
711+
int ZEXPORT PREFIX(deflate)(z_stream *strm, int flush) {
712712
int old_flush; /* value of flush param for previous deflate call */
713713
deflate_state *s;
714714

@@ -1025,7 +1025,7 @@ int ZEXPORT deflate(z_stream *strm, int flush) {
10251025
}
10261026

10271027
/* ========================================================================= */
1028-
int ZEXPORT deflateEnd(z_stream *strm) {
1028+
int ZEXPORT PREFIX(deflateEnd)(z_stream *strm) {
10291029
int status;
10301030

10311031
if (deflateStateCheck(strm))
@@ -1048,7 +1048,7 @@ int ZEXPORT deflateEnd(z_stream *strm) {
10481048
/* =========================================================================
10491049
* Copy the source state to the destination state.
10501050
*/
1051-
int ZEXPORT deflateCopy(z_stream *dest, z_stream *source) {
1051+
int ZEXPORT PREFIX(deflateCopy)(z_stream *dest, z_stream *source) {
10521052
deflate_state *ds;
10531053
deflate_state *ss;
10541054
uint16_t *overlay;

gzclose.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/* gzclose() is in a separate file so that it is linked in only if it is used.
99
That way the other gzclose functions can be used instead to avoid linking in
1010
unneeded compression or decompression routines. */
11-
int ZEXPORT gzclose(gzFile file) {
11+
int ZEXPORT PREFIX(gzclose)(gzFile file) {
1212
#ifndef NO_GZCOMPRESS
1313
gz_state *state;
1414

gzguts.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
#include <stdlib.h>
2828
#include <limits.h>
2929
#include <fcntl.h>
30-
#include "zlib.h"
30+
31+
#if defined(ZLIB_COMPAT)
32+
# include "zlib.h"
33+
#else
34+
# include "zlib-ng.h"
35+
#endif
3136

3237
#ifdef WIN32
3338
# include <stddef.h>
@@ -74,10 +79,10 @@
7479

7580
/* provide prototypes for these when building zlib without LFS */
7681
#if (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0) && defined(WITH_GZFILEOP)
77-
ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *);
78-
ZEXTERN z_off64_t ZEXPORT gzseek64(gzFile, z_off64_t, int);
79-
ZEXTERN z_off64_t ZEXPORT gztell64(gzFile);
80-
ZEXTERN z_off64_t ZEXPORT gzoffset64(gzFile);
82+
ZEXTERN gzFile ZEXPORT PREFIX(gzopen64)(const char *, const char *);
83+
ZEXTERN z_off64_t ZEXPORT PREFIX(gzseek64)(gzFile, z_off64_t, int);
84+
ZEXTERN z_off64_t ZEXPORT PREFIX(gztell64)(gzFile);
85+
ZEXTERN z_off64_t ZEXPORT PREFIX(gzoffset64)(gzFile);
8186
#endif
8287

8388
/* default memLevel */

0 commit comments

Comments
 (0)