Skip to content

Commit 2f4e237

Browse files
committed
Simplify zlib-ng native API by removing version and struct size checks.
This should be backwards compatible with applications compiled for 2.0.x.
1 parent c62b35f commit 2f4e237

File tree

9 files changed

+131
-90
lines changed

9 files changed

+131
-90
lines changed

deflate.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
#include "deflate_p.h"
5454
#include "functable.h"
5555

56+
/* Avoid conflicts with zlib.h macros */
57+
#ifdef ZLIB_COMPAT
58+
# undef deflateInit
59+
# undef deflateInit2
60+
#endif
61+
5662
const char PREFIX(deflate_copyright)[] = " deflate 1.2.11.f Copyright 1995-2016 Jean-loup Gailly and Mark Adler ";
5763
/*
5864
If you use the zlib library in a product, an acknowledgment is welcome
@@ -181,24 +187,16 @@ static const config configuration_table[10] = {
181187
} while (0)
182188

183189
/* ========================================================================= */
184-
int32_t Z_EXPORT PREFIX(deflateInit_)(PREFIX3(stream) *strm, int32_t level, const char *version, int32_t stream_size) {
185-
return PREFIX(deflateInit2_)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size);
190+
/* This function is hidden in ZLIB_COMPAT builds. */
191+
int32_t ZNG_CONDEXPORT PREFIX(deflateInit2)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
192+
int32_t memLevel, int32_t strategy) {
186193
/* Todo: ignore strm->next_in if we use it as window */
187-
}
188-
189-
/* ========================================================================= */
190-
int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
191-
int32_t memLevel, int32_t strategy, const char *version, int32_t stream_size) {
192194
uint32_t window_padding = 0;
193195
deflate_state *s;
194196
int wrap = 1;
195-
static const char my_version[] = PREFIX2(VERSION);
196197

197198
cpu_check_features();
198199

199-
if (version == NULL || version[0] != my_version[0] || stream_size != sizeof(PREFIX3(stream))) {
200-
return Z_VERSION_ERROR;
201-
}
202200
if (strm == NULL)
203201
return Z_STREAM_ERROR;
204202

@@ -319,6 +317,29 @@ int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int
319317
return PREFIX(deflateReset)(strm);
320318
}
321319

320+
#ifndef ZLIB_COMPAT
321+
int32_t Z_EXPORT PREFIX(deflateInit)(PREFIX3(stream) *strm, int32_t level) {
322+
return PREFIX(deflateInit2)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
323+
}
324+
#endif
325+
326+
/* Function used by zlib.h and zlib-ng version 2.0 macros */
327+
int32_t Z_EXPORT PREFIX(deflateInit_)(PREFIX3(stream) *strm, int32_t level, const char *version, int32_t stream_size) {
328+
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != sizeof(PREFIX3(stream))) {
329+
return Z_VERSION_ERROR;
330+
}
331+
return PREFIX(deflateInit2)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
332+
}
333+
334+
/* Function used by zlib.h and zlib-ng version 2.0 macros */
335+
int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
336+
int32_t memLevel, int32_t strategy, const char *version, int32_t stream_size) {
337+
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != sizeof(PREFIX3(stream))) {
338+
return Z_VERSION_ERROR;
339+
}
340+
return PREFIX(deflateInit2)(strm, level, method, windowBits, memLevel, strategy);
341+
}
342+
322343
/* =========================================================================
323344
* Check for a valid deflate stream state. Return 0 if ok, 1 if not.
324345
*/

infback.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@
1818
#include "inflate_p.h"
1919
#include "functable.h"
2020

21+
/* Avoid conflicts with zlib.h macros */
22+
#ifdef ZLIB_COMPAT
23+
# undef inflateBackInit
24+
#endif
25+
2126
/*
2227
strm provides memory allocation functions in zalloc and zfree, or
2328
NULL to use the library memory allocation functions.
2429
2530
windowBits is in the range 8..15, and window is a user-supplied
2631
window and output buffer that is 2**windowBits bytes.
32+
33+
This function is hidden in ZLIB_COMPAT builds.
2734
*/
28-
int32_t Z_EXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window,
29-
const char *version, int32_t stream_size) {
35+
int32_t ZNG_CONDEXPORT PREFIX(inflateBackInit)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window) {
3036
struct inflate_state *state;
3137

32-
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
33-
return Z_VERSION_ERROR;
3438
if (strm == NULL || window == NULL || windowBits < 8 || windowBits > 15)
3539
return Z_STREAM_ERROR;
3640
strm->msg = NULL; /* in case we return an error */
@@ -55,6 +59,15 @@ int32_t Z_EXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int32_t windowB
5559
return Z_OK;
5660
}
5761

62+
/* Function used by zlib.h and zlib-ng version 2.0 macros */
63+
int32_t Z_EXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window,
64+
const char *version, int32_t stream_size) {
65+
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream)))) {
66+
return Z_VERSION_ERROR;
67+
}
68+
return PREFIX(inflateBackInit)(strm, windowBits, window);
69+
}
70+
5871
/*
5972
Private macros for inflateBack()
6073
Look in inflate_p.h for macros shared with inflate()

inflate.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
#include "inffixed_tbl.h"
1414
#include "functable.h"
1515

16+
/* Avoid conflicts with zlib.h macros */
17+
#ifdef ZLIB_COMPAT
18+
# undef inflateInit
19+
# undef inflateInit2
20+
#endif
21+
1622
/* function prototypes */
1723
static int inflateStateCheck(PREFIX3(stream) *strm);
1824
static int updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t len, int32_t cksum);
@@ -128,14 +134,13 @@ int32_t Z_EXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int32_t windowBits
128134
return PREFIX(inflateReset)(strm);
129135
}
130136

131-
int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
137+
/* This function is hidden in ZLIB_COMPAT builds. */
138+
int32_t ZNG_CONDEXPORT PREFIX(inflateInit2)(PREFIX3(stream) *strm, int32_t windowBits) {
132139
int32_t ret;
133140
struct inflate_state *state;
134141

135142
cpu_check_features();
136143

137-
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
138-
return Z_VERSION_ERROR;
139144
if (strm == NULL)
140145
return Z_STREAM_ERROR;
141146
strm->msg = NULL; /* in case we return an error */
@@ -162,8 +167,24 @@ int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits
162167
return ret;
163168
}
164169

170+
#ifndef ZLIB_COMPAT
171+
int32_t Z_EXPORT PREFIX(inflateInit)(PREFIX3(stream) *strm) {
172+
return PREFIX(inflateInit2)(strm, DEF_WBITS);
173+
}
174+
#endif
175+
176+
/* Function used by zlib.h and zlib-ng version 2.0 macros */
165177
int32_t Z_EXPORT PREFIX(inflateInit_)(PREFIX3(stream) *strm, const char *version, int32_t stream_size) {
166-
return PREFIX(inflateInit2_)(strm, DEF_WBITS, version, stream_size);
178+
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
179+
return Z_VERSION_ERROR;
180+
return PREFIX(inflateInit2)(strm, DEF_WBITS);
181+
}
182+
183+
/* Function used by zlib.h and zlib-ng version 2.0 macros */
184+
int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
185+
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
186+
return Z_VERSION_ERROR;
187+
return PREFIX(inflateInit2)(strm, windowBits);
167188
}
168189

169190
int32_t Z_EXPORT PREFIX(inflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_t value) {

test/infcover.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,14 @@ static void cover_support(void) {
368368
inf("3 0", "use fixed blocks", 0, -15, 1, Z_STREAM_END);
369369
inf("", "bad window size", 0, 1, 0, Z_STREAM_ERROR);
370370

371+
#ifdef ZLIB_COMPAT
371372
mem_setup(&strm);
372373
strm.avail_in = 0;
373374
strm.next_in = NULL;
374375
ret = PREFIX(inflateInit_)(&strm, &PREFIX2(VERSION)[1], (int)sizeof(PREFIX3(stream)));
375376
assert(ret == Z_VERSION_ERROR);
376377
mem_done(&strm, "wrong version");
378+
#endif
377379

378380
strm.avail_in = 0;
379381
strm.next_in = NULL;
@@ -474,8 +476,11 @@ static void cover_back(void) {
474476
PREFIX3(stream) strm;
475477
unsigned char win[32768];
476478

479+
#ifdef ZLIB_COMPAT
477480
ret = PREFIX(inflateBackInit_)(NULL, 0, win, 0, 0);
478481
assert(ret == Z_VERSION_ERROR);
482+
#endif
483+
479484
ret = PREFIX(inflateBackInit)(NULL, 0, win);
480485
assert(ret == Z_STREAM_ERROR);
481486
ret = PREFIX(inflateBack)(NULL, NULL, NULL, NULL, NULL);

win32/zlib-ng.def.in

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ EXPORTS
44
@ZLIB_SYMBOL_PREFIX@zlibng_version
55
@ZLIB_SYMBOL_PREFIX@zng_deflate
66
@ZLIB_SYMBOL_PREFIX@zng_deflateEnd
7+
@ZLIB_SYMBOL_PREFIX@zng_deflateInit
8+
@ZLIB_SYMBOL_PREFIX@zng_deflateInit2
79
@ZLIB_SYMBOL_PREFIX@zng_inflate
810
@ZLIB_SYMBOL_PREFIX@zng_inflateEnd
11+
@ZLIB_SYMBOL_PREFIX@zng_inflateInit
12+
@ZLIB_SYMBOL_PREFIX@zng_inflateInit2
13+
@ZLIB_SYMBOL_PREFIX@zng_inflateBackInit
914
; advanced functions
1015
@ZLIB_SYMBOL_PREFIX@zng_deflateSetDictionary
1116
@ZLIB_SYMBOL_PREFIX@zng_deflateGetDictionary
@@ -45,11 +50,6 @@ EXPORTS
4550
@ZLIB_SYMBOL_PREFIX@zng_adler32_combine
4651
@ZLIB_SYMBOL_PREFIX@zng_crc32_combine
4752
; various hacks, don't look :)
48-
@ZLIB_SYMBOL_PREFIX@zng_deflateInit_
49-
@ZLIB_SYMBOL_PREFIX@zng_deflateInit2_
50-
@ZLIB_SYMBOL_PREFIX@zng_inflateInit_
51-
@ZLIB_SYMBOL_PREFIX@zng_inflateInit2_
52-
@ZLIB_SYMBOL_PREFIX@zng_inflateBackInit_
5353
@ZLIB_SYMBOL_PREFIX@zng_zError
5454
@ZLIB_SYMBOL_PREFIX@zng_inflateSyncPoint
5555
@ZLIB_SYMBOL_PREFIX@zng_get_crc_table

zconf-ng.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
# define Z_EXPORTVA
8989
#endif
9090

91+
/* Conditional exports */
92+
#define ZNG_CONDEXPORT Z_EXPORT
93+
9194
/* Fallback for something that includes us. */
9295
typedef unsigned char Byte;
9396
typedef Byte Bytef;

zconf.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
# define Z_EXPORTVA
9797
#endif
9898

99+
/* Conditional exports */
100+
#define ZNG_CONDEXPORT Z_INTERNAL
101+
99102
/* For backwards compatibility */
100103

101104
#ifndef ZEXTERN

0 commit comments

Comments
 (0)