Simplify zlib-ng native API by removing version and struct size checks.

This should be backwards compatible with applications compiled for 2.0.x.
This commit is contained in:
Hans Kristian Rosbach 2022-02-11 11:11:04 +01:00 committed by Hans Kristian Rosbach
parent c62b35ffac
commit 2f4e2372a2
9 changed files with 131 additions and 90 deletions

View File

@ -53,6 +53,12 @@
#include "deflate_p.h"
#include "functable.h"
/* Avoid conflicts with zlib.h macros */
#ifdef ZLIB_COMPAT
# undef deflateInit
# undef deflateInit2
#endif
const char PREFIX(deflate_copyright)[] = " deflate 1.2.11.f Copyright 1995-2016 Jean-loup Gailly and Mark Adler ";
/*
If you use the zlib library in a product, an acknowledgment is welcome
@ -181,24 +187,16 @@ static const config configuration_table[10] = {
} while (0)
/* ========================================================================= */
int32_t Z_EXPORT PREFIX(deflateInit_)(PREFIX3(stream) *strm, int32_t level, const char *version, int32_t stream_size) {
return PREFIX(deflateInit2_)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size);
/* This function is hidden in ZLIB_COMPAT builds. */
int32_t ZNG_CONDEXPORT PREFIX(deflateInit2)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
int32_t memLevel, int32_t strategy) {
/* Todo: ignore strm->next_in if we use it as window */
}
/* ========================================================================= */
int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
int32_t memLevel, int32_t strategy, const char *version, int32_t stream_size) {
uint32_t window_padding = 0;
deflate_state *s;
int wrap = 1;
static const char my_version[] = PREFIX2(VERSION);
cpu_check_features();
if (version == NULL || version[0] != my_version[0] || stream_size != sizeof(PREFIX3(stream))) {
return Z_VERSION_ERROR;
}
if (strm == NULL)
return Z_STREAM_ERROR;
@ -319,6 +317,29 @@ int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int
return PREFIX(deflateReset)(strm);
}
#ifndef ZLIB_COMPAT
int32_t Z_EXPORT PREFIX(deflateInit)(PREFIX3(stream) *strm, int32_t level) {
return PREFIX(deflateInit2)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
}
#endif
/* Function used by zlib.h and zlib-ng version 2.0 macros */
int32_t Z_EXPORT PREFIX(deflateInit_)(PREFIX3(stream) *strm, int32_t level, const char *version, int32_t stream_size) {
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != sizeof(PREFIX3(stream))) {
return Z_VERSION_ERROR;
}
return PREFIX(deflateInit2)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
}
/* Function used by zlib.h and zlib-ng version 2.0 macros */
int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
int32_t memLevel, int32_t strategy, const char *version, int32_t stream_size) {
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != sizeof(PREFIX3(stream))) {
return Z_VERSION_ERROR;
}
return PREFIX(deflateInit2)(strm, level, method, windowBits, memLevel, strategy);
}
/* =========================================================================
* Check for a valid deflate stream state. Return 0 if ok, 1 if not.
*/

View File

@ -18,19 +18,23 @@
#include "inflate_p.h"
#include "functable.h"
/* Avoid conflicts with zlib.h macros */
#ifdef ZLIB_COMPAT
# undef inflateBackInit
#endif
/*
strm provides memory allocation functions in zalloc and zfree, or
NULL to use the library memory allocation functions.
windowBits is in the range 8..15, and window is a user-supplied
window and output buffer that is 2**windowBits bytes.
This function is hidden in ZLIB_COMPAT builds.
*/
int32_t Z_EXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window,
const char *version, int32_t stream_size) {
int32_t ZNG_CONDEXPORT PREFIX(inflateBackInit)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window) {
struct inflate_state *state;
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
return Z_VERSION_ERROR;
if (strm == NULL || window == NULL || windowBits < 8 || windowBits > 15)
return Z_STREAM_ERROR;
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
return Z_OK;
}
/* Function used by zlib.h and zlib-ng version 2.0 macros */
int32_t Z_EXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window,
const char *version, int32_t stream_size) {
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream)))) {
return Z_VERSION_ERROR;
}
return PREFIX(inflateBackInit)(strm, windowBits, window);
}
/*
Private macros for inflateBack()
Look in inflate_p.h for macros shared with inflate()

View File

@ -13,6 +13,12 @@
#include "inffixed_tbl.h"
#include "functable.h"
/* Avoid conflicts with zlib.h macros */
#ifdef ZLIB_COMPAT
# undef inflateInit
# undef inflateInit2
#endif
/* function prototypes */
static int inflateStateCheck(PREFIX3(stream) *strm);
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
return PREFIX(inflateReset)(strm);
}
int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
/* This function is hidden in ZLIB_COMPAT builds. */
int32_t ZNG_CONDEXPORT PREFIX(inflateInit2)(PREFIX3(stream) *strm, int32_t windowBits) {
int32_t ret;
struct inflate_state *state;
cpu_check_features();
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
return Z_VERSION_ERROR;
if (strm == NULL)
return Z_STREAM_ERROR;
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
return ret;
}
#ifndef ZLIB_COMPAT
int32_t Z_EXPORT PREFIX(inflateInit)(PREFIX3(stream) *strm) {
return PREFIX(inflateInit2)(strm, DEF_WBITS);
}
#endif
/* Function used by zlib.h and zlib-ng version 2.0 macros */
int32_t Z_EXPORT PREFIX(inflateInit_)(PREFIX3(stream) *strm, const char *version, int32_t stream_size) {
return PREFIX(inflateInit2_)(strm, DEF_WBITS, version, stream_size);
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
return Z_VERSION_ERROR;
return PREFIX(inflateInit2)(strm, DEF_WBITS);
}
/* Function used by zlib.h and zlib-ng version 2.0 macros */
int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
return Z_VERSION_ERROR;
return PREFIX(inflateInit2)(strm, windowBits);
}
int32_t Z_EXPORT PREFIX(inflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_t value) {

View File

@ -368,12 +368,14 @@ static void cover_support(void) {
inf("3 0", "use fixed blocks", 0, -15, 1, Z_STREAM_END);
inf("", "bad window size", 0, 1, 0, Z_STREAM_ERROR);
#ifdef ZLIB_COMPAT
mem_setup(&strm);
strm.avail_in = 0;
strm.next_in = NULL;
ret = PREFIX(inflateInit_)(&strm, &PREFIX2(VERSION)[1], (int)sizeof(PREFIX3(stream)));
assert(ret == Z_VERSION_ERROR);
mem_done(&strm, "wrong version");
#endif
strm.avail_in = 0;
strm.next_in = NULL;
@ -474,8 +476,11 @@ static void cover_back(void) {
PREFIX3(stream) strm;
unsigned char win[32768];
#ifdef ZLIB_COMPAT
ret = PREFIX(inflateBackInit_)(NULL, 0, win, 0, 0);
assert(ret == Z_VERSION_ERROR);
#endif
ret = PREFIX(inflateBackInit)(NULL, 0, win);
assert(ret == Z_STREAM_ERROR);
ret = PREFIX(inflateBack)(NULL, NULL, NULL, NULL, NULL);

View File

@ -4,8 +4,13 @@ EXPORTS
@ZLIB_SYMBOL_PREFIX@zlibng_version
@ZLIB_SYMBOL_PREFIX@zng_deflate
@ZLIB_SYMBOL_PREFIX@zng_deflateEnd
@ZLIB_SYMBOL_PREFIX@zng_deflateInit
@ZLIB_SYMBOL_PREFIX@zng_deflateInit2
@ZLIB_SYMBOL_PREFIX@zng_inflate
@ZLIB_SYMBOL_PREFIX@zng_inflateEnd
@ZLIB_SYMBOL_PREFIX@zng_inflateInit
@ZLIB_SYMBOL_PREFIX@zng_inflateInit2
@ZLIB_SYMBOL_PREFIX@zng_inflateBackInit
; advanced functions
@ZLIB_SYMBOL_PREFIX@zng_deflateSetDictionary
@ZLIB_SYMBOL_PREFIX@zng_deflateGetDictionary
@ -45,11 +50,6 @@ EXPORTS
@ZLIB_SYMBOL_PREFIX@zng_adler32_combine
@ZLIB_SYMBOL_PREFIX@zng_crc32_combine
; various hacks, don't look :)
@ZLIB_SYMBOL_PREFIX@zng_deflateInit_
@ZLIB_SYMBOL_PREFIX@zng_deflateInit2_
@ZLIB_SYMBOL_PREFIX@zng_inflateInit_
@ZLIB_SYMBOL_PREFIX@zng_inflateInit2_
@ZLIB_SYMBOL_PREFIX@zng_inflateBackInit_
@ZLIB_SYMBOL_PREFIX@zng_zError
@ZLIB_SYMBOL_PREFIX@zng_inflateSyncPoint
@ZLIB_SYMBOL_PREFIX@zng_get_crc_table

View File

@ -88,6 +88,9 @@
# define Z_EXPORTVA
#endif
/* Conditional exports */
#define ZNG_CONDEXPORT Z_EXPORT
/* Fallback for something that includes us. */
typedef unsigned char Byte;
typedef Byte Bytef;

View File

@ -96,6 +96,9 @@
# define Z_EXPORTVA
#endif
/* Conditional exports */
#define ZNG_CONDEXPORT Z_INTERNAL
/* For backwards compatibility */
#ifndef ZEXTERN

View File

@ -219,14 +219,12 @@ Z_EXTERN Z_EXPORT
const char *zlibng_version(void);
/* The application can compare zlibng_version and ZLIBNG_VERSION for consistency.
If the first character differs, the library code actually used is not
compatible with the zlib-ng.h header file used by the application. This check
is automatically made by deflateInit and inflateInit.
compatible with the zlib-ng.h header file used by the application.
*/
/*
Z_EXTERN Z_EXPORT
int zng_deflateInit(zng_stream *strm, int level);
int32_t zng_deflateInit(zng_stream *strm, int32_t level);
/*
Initializes the internal stream state for compression. The fields
zalloc, zfree and opaque must be initialized before by the caller. If
zalloc and zfree are set to NULL, deflateInit updates them to use default
@ -239,11 +237,9 @@ int zng_deflateInit(zng_stream *strm, int level);
equivalent to level 6).
deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_STREAM_ERROR if level is not a valid compression level, or
Z_VERSION_ERROR if the zlib library version (zng_version) is incompatible
with the version assumed by the caller (ZLIBNG_VERSION). msg is set to null
if there is no error message. deflateInit does not perform any compression:
this will be done by deflate().
memory, Z_STREAM_ERROR if level is not a valid compression level.
msg is set to null if there is no error message. deflateInit does not perform
any compression: this will be done by deflate().
*/
@ -376,10 +372,9 @@ int32_t zng_deflateEnd(zng_stream *strm);
*/
/*
Z_EXTERN Z_EXPORT
int zng_inflateInit(zng_stream *strm);
int32_t zng_inflateInit(zng_stream *strm);
/*
Initializes the internal stream state for decompression. The fields
next_in, avail_in, zalloc, zfree and opaque must be initialized before by
the caller. In the current version of inflate, the provided input is not
@ -389,14 +384,13 @@ int zng_inflateInit(zng_stream *strm);
them to use default allocation functions.
inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
version assumed by the caller, or Z_STREAM_ERROR if the parameters are
invalid, such as a null pointer to the structure. msg is set to null if
there is no error message. inflateInit does not perform any decompression.
Actual decompression will be done by inflate(). So next_in, and avail_in,
next_out, and avail_out are unused and unchanged. The current
implementation of inflateInit() does not process any header information --
that is deferred until inflate() is called.
memory, or Z_STREAM_ERROR if the parameters are invalid, such as a null
pointer to the structure. msg is set to null if there is no error message.
inflateInit does not perform any decompression. Actual decompression will
be done by inflate(). So next_in, and avail_in, next_out, and avail_out
are unused and unchanged. The current implementation of inflateInit()
does not process any header information -- that is deferred until inflate()
is called.
*/
@ -539,10 +533,9 @@ int32_t zng_inflateEnd(zng_stream *strm);
The following functions are needed only in some special applications.
*/
/*
Z_EXTERN Z_EXPORT
int zng_deflateInit2(zng_stream *strm, int level, int method, int windowBits, int memLevel, int strategy);
int32_t zng_deflateInit2(zng_stream *strm, int32_t level, int32_t method, int32_t windowBits, int32_t memLevel, int32_t strategy);
/*
This is another version of deflateInit with more compression options. The
fields zalloc, zfree and opaque must be initialized before by the caller.
@ -601,11 +594,9 @@ int zng_deflateInit2(zng_stream *strm, int level, int method, int windowBits,
decoder for special applications.
deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid
method), or Z_VERSION_ERROR if the zlib library version (zng_version) is
incompatible with the version assumed by the caller (ZLIBNG_VERSION). msg is
set to null if there is no error message. deflateInit2 does not perform any
compression: this will be done by deflate().
memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid method).
msg is set to null if there is no error message. deflateInit2 does not perform
any compression: this will be done by deflate().
*/
Z_EXTERN Z_EXPORT
@ -822,10 +813,9 @@ int32_t zng_deflateSetHeader(zng_stream *strm, zng_gz_headerp head);
stream state was inconsistent.
*/
/*
Z_EXTERN Z_EXPORT
int zng_inflateInit2(zng_stream *strm, int windowBits);
int32_t zng_inflateInit2(zng_stream *strm, int32_t windowBits);
/*
This is another version of inflateInit with an extra parameter. The
fields next_in, avail_in, zalloc, zfree and opaque must be initialized
before by the caller.
@ -866,15 +856,13 @@ int zng_inflateInit2(zng_stream *strm, int windowBits);
decompression to be compliant with the gzip standard (RFC 1952).
inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
version assumed by the caller, or Z_STREAM_ERROR if the parameters are
invalid, such as a null pointer to the structure. msg is set to null if
there is no error message. inflateInit2 does not perform any decompression
apart from possibly reading the zlib header if present: actual decompression
will be done by inflate(). (So next_in and avail_in may be modified, but
next_out and avail_out are unused and unchanged.) The current implementation
of inflateInit2() does not process any header information -- that is
deferred until inflate() is called.
memory, or Z_STREAM_ERROR if the parameters are invalid, such as a null
pointer to the structure. msg is set to null if there is no error message.
inflateInit2 does not perform any decompression apart from possibly reading
the zlib header if present: actual decompression will be done by inflate().
(So next_in and avail_in may be modified, but next_out and avail_out are
unused and unchanged.) The current implementation of inflateInit2() does not
process any header information -- that is deferred until inflate() is called.
*/
Z_EXTERN Z_EXPORT
@ -1063,10 +1051,9 @@ int32_t zng_inflateGetHeader(zng_stream *strm, zng_gz_headerp head);
stream state was inconsistent.
*/
/*
Z_EXTERN Z_EXPORT
int zng_inflateBackInit(zng_stream *strm, int windowBits, unsigned char *window);
int32_t zng_inflateBackInit(zng_stream *strm, int32_t windowBits, uint8_t *window);
/*
Initialize the internal stream state for decompression using inflateBack()
calls. The fields zalloc, zfree and opaque in strm must be initialized
before the call. If zalloc and zfree are NULL, then the default library-
@ -1081,8 +1068,7 @@ int zng_inflateBackInit(zng_stream *strm, int windowBits, unsigned char *window)
inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
the parameters are invalid, Z_MEM_ERROR if the internal state could not be
allocated, or Z_VERSION_ERROR if the version of the library does not match
the version of the header file.
allocated.
*/
typedef uint32_t (*in_func) (void *, const uint8_t * *);
@ -1791,26 +1777,6 @@ uint32_t zng_crc32_combine_op(uint32_t crc1, uint32_t crc2, const uint32_t op);
/* various hacks, don't look :) */
/* zng_deflateInit and zng_inflateInit are macros to allow checking the zlib version
* and the compiler's view of zng_stream:
*/
Z_EXTERN Z_EXPORT int32_t zng_deflateInit_(zng_stream *strm, int32_t level, const char *version, int32_t stream_size);
Z_EXTERN Z_EXPORT int32_t zng_inflateInit_(zng_stream *strm, const char *version, int32_t stream_size);
Z_EXTERN Z_EXPORT int32_t zng_deflateInit2_(zng_stream *strm, int32_t level, int32_t method, int32_t windowBits, int32_t memLevel,
int32_t strategy, const char *version, int32_t stream_size);
Z_EXTERN Z_EXPORT int32_t zng_inflateInit2_(zng_stream *strm, int32_t windowBits, const char *version, int32_t stream_size);
Z_EXTERN Z_EXPORT int32_t zng_inflateBackInit_(zng_stream *strm, int32_t windowBits, uint8_t *window,
const char *version, int32_t stream_size);
#define @ZLIB_SYMBOL_PREFIX@zng_deflateInit(strm, level) zng_deflateInit_((strm), (level), ZLIBNG_VERSION, (int32_t)sizeof(zng_stream))
#define @ZLIB_SYMBOL_PREFIX@zng_inflateInit(strm) zng_inflateInit_((strm), ZLIBNG_VERSION, (int32_t)sizeof(zng_stream))
#define @ZLIB_SYMBOL_PREFIX@zng_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
zng_deflateInit2_((strm), (level), (method), (windowBits), (memLevel), \
(strategy), ZLIBNG_VERSION, (int32_t)sizeof(zng_stream))
#define @ZLIB_SYMBOL_PREFIX@zng_inflateInit2(strm, windowBits) zng_inflateInit2_((strm), (windowBits), ZLIBNG_VERSION, (int32_t)sizeof(zng_stream))
#define @ZLIB_SYMBOL_PREFIX@zng_inflateBackInit(strm, windowBits, window) \
zng_inflateBackInit_((strm), (windowBits), (window), ZLIBNG_VERSION, (int32_t)sizeof(zng_stream))
#ifdef WITH_GZFILEOP
/* gzgetc() macro and its supporting function and exposed data structure. Note

View File

@ -1,3 +1,12 @@
ZLIB_NG_2.1.0 {
global:
zng_deflateInit;
zng_deflateInit2;
zng_inflateBackInit;
zng_inflateInit;
zng_inflateInit2;
};
ZLIB_NG_2.0.0 {
global:
zng_adler32;