Fix definition of z_size_t to match documentation of legacy zlib API.

This commit is contained in:
Mika T. Lindqvist 2022-12-07 21:48:31 +02:00 committed by Hans Kristian Rosbach
parent 135641be1c
commit c970422caa
8 changed files with 35 additions and 23 deletions

View File

@ -43,7 +43,8 @@ certain value will need to be updated.
- Static library is *libz.a* on Unix and macOS, or *zlib.lib* on Windows - Static library is *libz.a* on Unix and macOS, or *zlib.lib* on Windows
- Shared library is *libz.so* on Unix, *libz.dylib* on macOS, or *zlib1.dll* - Shared library is *libz.so* on Unix, *libz.dylib* on macOS, or *zlib1.dll*
on Windows on Windows
- Type `z_size_t` is *unsigned long* - Type `z_size_t` is *unsigned __int64* on 64-bit Windows, and *unsigned long* on 32-bit Windows, Unix and macOS
- Type `z_uintmax_t` is *unsigned long* in zlib-compat mode, and *size_t* with zlib-ng API
zlib-ng native mode zlib-ng native mode
------------------- -------------------

View File

@ -28,8 +28,8 @@
memory, Z_BUF_ERROR if there was not enough room in the output buffer, memory, Z_BUF_ERROR if there was not enough room in the output buffer,
Z_STREAM_ERROR if the level parameter is invalid. Z_STREAM_ERROR if the level parameter is invalid.
*/ */
int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source,
z_size_t sourceLen, int level) { z_uintmax_t sourceLen, int level) {
PREFIX3(stream) stream; PREFIX3(stream) stream;
int err; int err;
const unsigned int max = (unsigned int)-1; const unsigned int max = (unsigned int)-1;
@ -63,14 +63,14 @@ int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_size_t *destLen, const uns
err = PREFIX(deflate)(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH); err = PREFIX(deflate)(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
} while (err == Z_OK); } while (err == Z_OK);
*destLen = (z_size_t)stream.total_out; *destLen = stream.total_out;
PREFIX(deflateEnd)(&stream); PREFIX(deflateEnd)(&stream);
return err == Z_STREAM_END ? Z_OK : err; return err == Z_STREAM_END ? Z_OK : err;
} }
/* =========================================================================== /* ===========================================================================
*/ */
int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, z_size_t sourceLen) { int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) {
return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
} }
@ -78,8 +78,8 @@ int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_size_t *destLen, const unsi
If the default memLevel or windowBits for deflateInit() is changed, then If the default memLevel or windowBits for deflateInit() is changed, then
this function needs to be updated. this function needs to be updated.
*/ */
z_size_t Z_EXPORT PREFIX(compressBound)(z_size_t sourceLen) { z_uintmax_t Z_EXPORT PREFIX(compressBound)(z_uintmax_t sourceLen) {
z_size_t complen = DEFLATE_BOUND_COMPLEN(sourceLen); z_uintmax_t complen = DEFLATE_BOUND_COMPLEN(sourceLen);
if (complen > 0) if (complen > 0)
/* Architecture-specific code provided an upper bound. */ /* Architecture-specific code provided an upper bound. */

View File

@ -1292,8 +1292,8 @@ int32_t Z_EXPORT PREFIX(inflateSync)(PREFIX3(stream) *strm) {
in = strm->total_in; in = strm->total_in;
out = strm->total_out; out = strm->total_out;
PREFIX(inflateReset)(strm); PREFIX(inflateReset)(strm);
strm->total_in = (z_size_t)in; strm->total_in = (z_uintmax_t)in; /* Can't use z_size_t here as it will overflow on 64-bit Windows */
strm->total_out = (z_size_t)out; strm->total_out = (z_uintmax_t)out;
state->flags = flags; state->flags = flags;
state->mode = TYPE; state->mode = TYPE;
return Z_OK; return Z_OK;

View File

@ -26,13 +26,13 @@ static unsigned long dictId = 0; /* Adler32 value of the dictionary */
#define MAX_DICTIONARY_SIZE 32768 #define MAX_DICTIONARY_SIZE 32768
void test_compress (unsigned char *compr, z_size_t comprLen,unsigned char *uncompr, z_size_t uncomprLen); void test_compress (unsigned char *compr, z_uintmax_t comprLen, unsigned char *uncompr, z_uintmax_t uncomprLen);
void test_gzio (const char *fname, 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_deflate (unsigned char *compr, size_t comprLen);
void test_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen); 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, int zng_params); void test_large_deflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen, int zng_params);
void test_large_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen); void test_large_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
void test_flush (unsigned char *compr, z_size_t *comprLen); void test_flush (unsigned char *compr, z_uintmax_t *comprLen);
void test_sync (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen); void test_sync (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
void test_dict_deflate (unsigned char *compr, size_t comprLen); void test_dict_deflate (unsigned char *compr, size_t comprLen);
void test_dict_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen); void test_dict_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
@ -63,11 +63,11 @@ void error(const char *format, ...) {
/* =========================================================================== /* ===========================================================================
* Test compress() and uncompress() * Test compress() and uncompress()
*/ */
void test_compress(unsigned char *compr, z_size_t comprLen, unsigned char *uncompr, z_size_t uncomprLen) { void test_compress(unsigned char *compr, z_uintmax_t comprLen, unsigned char *uncompr, z_uintmax_t uncomprLen) {
int err; int err;
size_t len = strlen(hello)+1; unsigned int len = (unsigned int)strlen(hello)+1;
err = PREFIX(compress)(compr, &comprLen, (const unsigned char*)hello, (z_size_t)len); err = PREFIX(compress)(compr, &comprLen, (const unsigned char*)hello, len);
CHECK_ERR(err, "compress"); CHECK_ERR(err, "compress");
strcpy((char*)uncompr, "garbage"); strcpy((char*)uncompr, "garbage");
@ -402,7 +402,7 @@ void test_large_inflate(unsigned char *compr, size_t comprLen, unsigned char *un
/* =========================================================================== /* ===========================================================================
* Test deflate() with full flush * Test deflate() with full flush
*/ */
void test_flush(unsigned char *compr, z_size_t *comprLen) { void test_flush(unsigned char *compr, z_uintmax_t *comprLen) {
PREFIX3(stream) c_stream; /* compression stream */ PREFIX3(stream) c_stream; /* compression stream */
int err; int err;
unsigned int len = (unsigned int)strlen(hello)+1; unsigned int len = (unsigned int)strlen(hello)+1;
@ -953,8 +953,8 @@ void test_deflate_tune(unsigned char *compr, size_t comprLen) {
*/ */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
unsigned char *compr, *uncompr; unsigned char *compr, *uncompr;
z_size_t comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */ z_uintmax_t comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
z_size_t uncomprLen = comprLen; z_uintmax_t uncomprLen = comprLen;
static const char* myVersion = PREFIX2(VERSION); static const char* myVersion = PREFIX2(VERSION);
if (zVersion()[0] != myVersion[0]) { if (zVersion()[0] != myVersion[0]) {

View File

@ -18,7 +18,7 @@
TEST(compress, basic) { TEST(compress, basic) {
uint8_t compr[128], uncompr[128]; uint8_t compr[128], uncompr[128];
z_size_t compr_len = sizeof(compr), uncompr_len = sizeof(uncompr); z_uintmax_t compr_len = sizeof(compr), uncompr_len = sizeof(uncompr);
int err; int err;
err = PREFIX(compress)(compr, &compr_len, (const unsigned char *)hello, hello_len); err = PREFIX(compress)(compr, &compr_len, (const unsigned char *)hello, hello_len);

View File

@ -35,7 +35,7 @@ public:
} }
for (z_size_t i = 0; i < MAX_LENGTH; i++) { for (z_size_t i = 0; i < MAX_LENGTH; i++) {
z_size_t dest_len = sizeof(dest); z_uintmax_t dest_len = sizeof(dest);
/* calculate actual output length */ /* calculate actual output length */
estimate_len = PREFIX(compressBound)(i); estimate_len = PREFIX(compressBound)(i);

View File

@ -22,11 +22,11 @@
Z_DATA_ERROR if the input data was corrupted, including if the input data is Z_DATA_ERROR if the input data was corrupted, including if the input data is
an incomplete zlib stream. an incomplete zlib stream.
*/ */
int Z_EXPORT PREFIX(uncompress2)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, z_size_t *sourceLen) { int Z_EXPORT PREFIX(uncompress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t *sourceLen) {
PREFIX3(stream) stream; PREFIX3(stream) stream;
int err; int err;
const unsigned int max = (unsigned int)-1; const unsigned int max = (unsigned int)-1;
z_size_t len, left; z_uintmax_t len, left;
unsigned char buf[1]; /* for detection of incomplete stream when *destLen == 0 */ unsigned char buf[1]; /* for detection of incomplete stream when *destLen == 0 */
len = *sourceLen; len = *sourceLen;
@ -75,6 +75,6 @@ int Z_EXPORT PREFIX(uncompress2)(unsigned char *dest, z_size_t *destLen, const u
err; err;
} }
int Z_EXPORT PREFIX(uncompress)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, z_size_t sourceLen) { int Z_EXPORT PREFIX(uncompress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) {
return PREFIX(uncompress2)(dest, destLen, source, &sourceLen); return PREFIX(uncompress2)(dest, destLen, source, &sourceLen);
} }

View File

@ -85,7 +85,11 @@
# define PREFIX3(x) z_ ## x # define PREFIX3(x) z_ ## x
# define PREFIX4(x) x ## 64 # define PREFIX4(x) x ## 64
# define zVersion zlibVersion # define zVersion zlibVersion
# define z_size_t unsigned long # if defined(_WIN64)
# define z_size_t unsigned __int64
# else
# define z_size_t unsigned long
# endif
#else #else
# define PREFIX(x) zng_ ## x # define PREFIX(x) zng_ ## x
# define PREFIX2(x) ZLIBNG_ ## x # define PREFIX2(x) ZLIBNG_ ## x
@ -95,6 +99,13 @@
# define z_size_t size_t # define z_size_t size_t
#endif #endif
/* In zlib-compat some functions and types use unsigned long, but zlib-ng use size_t */
#if defined(ZLIB_COMPAT)
# define z_uintmax_t unsigned long
#else
# define z_uintmax_t size_t
#endif
/* Minimum of a and b. */ /* Minimum of a and b. */
#define MIN(a, b) ((a) > (b) ? (b) : (a)) #define MIN(a, b) ((a) > (b) ? (b) : (a))
/* Maximum of a and b. */ /* Maximum of a and b. */