Skip to content

Commit

Permalink
Avoid use of stdint.h in contrib/minizip.
Browse files Browse the repository at this point in the history
  • Loading branch information
madler committed Aug 1, 2024
1 parent 164b8e3 commit be24a8f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 51 deletions.
57 changes: 57 additions & 0 deletions contrib/minizip/ints.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* ints.h -- create integer types for 8, 16, 32, and 64 bits
* Copyright (C) 2024 Mark Adler
* For conditions of distribution and use, see the copyright notice in zlib.h
*
* There exist compilers with limits.h, but not stdint.h or inttypes.h.
*/

#ifndef INTS_H
#define INTS_H
#include <limits.h>
#if defined(UCHAR_MAX) && UCHAR_MAX == 0xff
typedef signed char i8_t;
typedef unsigned char ui8_t;
#else
# error "no 8-bit integer"
#endif
#if defined(USHRT_MAX) && USHRT_MAX == 0xffff
typedef short i16_t;
typedef unsigned short ui16_t;
#elif defined(UINT_MAX) && UINT_MAX == 0xffff
typedef int i16_t;
typedef unsigned ui16_t;
#else
# error "no 16-bit integer"
#endif
#if defined(UINT_MAX) && UINT_MAX == 0xffffffff
typedef int i32_t;
typedef unsigned ui32_t;
# define PI32 "d"
# define PUI32 "u"
#elif defined(ULONG_MAX) && ULONG_MAX == 0xffffffff
typedef long i32_t;
typedef unsigned long ui32_t;
# define PI32 "ld"
# define PUI32 "lu"
#else
# error "no 32-bit integer"
#endif
#if defined(ULONG_MAX) && ULONG_MAX == 0xffffffffffffffff
typedef long i64_t;
typedef unsigned long ui64_t;
# define PI64 "ld"
# define PUI64 "lu"
#elif defined(ULLONG_MAX) && ULLONG_MAX == 0xffffffffffffffff
typedef long long i64_t;
typedef unsigned long long ui64_t;
# define PI64 "lld"
# define PUI64 "llu"
#elif defined(ULONG_LONG_MAX) && ULONG_LONG_MAX == 0xffffffffffffffff
typedef long long i64_t;
typedef unsigned long long ui64_t;
# define PI64 "lld"
# define PUI64 "llu"
#else
# error "no 64-bit integer"
#endif
#endif
31 changes: 2 additions & 29 deletions contrib/minizip/ioapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,39 +67,12 @@
#endif
#endif

/*
#ifndef ZPOS64_T
#ifdef _WIN32
#define ZPOS64_T fpos_t
#else
#include <stdint.h>
#define ZPOS64_T uint64_t
#endif
#endif
*/

#ifdef HAVE_MINIZIP64_CONF_H
#include "mz64conf.h"
#endif

/* a type chosen by DEFINE */
#ifdef HAVE_64BIT_INT_CUSTOM
typedef 64BIT_INT_CUSTOM_TYPE ZPOS64_T;
#else
#ifdef HAS_STDINT_H
#include "stdint.h"
typedef uint64_t ZPOS64_T;
#else



#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef unsigned __int64 ZPOS64_T;
#else
typedef unsigned long long int ZPOS64_T;
#endif
#endif
#endif
#include "ints.h"
typedef ui64_t ZPOS64_T;

/* Maximum unsigned 32-bit value used as placeholder for zip64 */
#ifndef MAXU32
Expand Down
3 changes: 2 additions & 1 deletion contrib/minizip/minizip.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#endif

#include "zip.h"
#include "ints.h"

#ifdef _WIN32
#define USEWIN32IOAPI
Expand Down Expand Up @@ -226,7 +227,7 @@ static int isLargeFile(const char* filename) {
FSEEKO_FUNC(pFile, 0, SEEK_END);
pos = (ZPOS64_T)FTELLO_FUNC(pFile);

printf("File : %s is %llu bytes\n", filename, pos);
printf("File : %s is %"PUI64" bytes\n", filename, pos);

if(pos >= 0xffffffff)
largeFile = 1;
Expand Down
33 changes: 17 additions & 16 deletions contrib/minizip/skipset.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@
#define SKIPSET_H

#include <stdlib.h> // realloc(), free(), NULL, size_t
#include <stddef.h> // ptrdiff_t
#include <setjmp.h> // jmp_buf, longjmp()
#include <errno.h> // ENOMEM
#include <stdint.h> // int16_t, uint32_t, uint64_t
#include <time.h> // time(), clock()
#include <assert.h> // assert.h
#include "ints.h" // i16_t, ui32_t, ui64_t

// Structures and functions below noted as "--private--" should not be used by
// the application. set_t is partially private and partially public -- see the
Expand All @@ -74,20 +75,20 @@
// Licensed under Apache License 2.0 (NO WARRANTY, etc. see website)
// --private-- Random number generator state.
typedef struct {
uint64_t state; // 64-bit generator state
uint64_t inc; // 63-bit sequence id
ui64_t state; // 64-bit generator state
ui64_t inc; // 63-bit sequence id
} set_rand_t;
// --private-- Initialize the state *gen using seed and seq. seed seeds the
// advancing 64-bit state. seq is a sequence selection constant.
void set_seed(set_rand_t *gen, uint64_t seed, uint64_t seq) {
void set_seed(set_rand_t *gen, ui64_t seed, ui64_t seq) {
gen->inc = (seq << 1) | 1;
gen->state = (seed + gen->inc) * 6364136223846793005ULL + gen->inc;
}
// Return 32 random bits, advancing the state *gen.
uint32_t set_rand(set_rand_t *gen) {
uint64_t state = gen->state;
ui32_t set_rand(set_rand_t *gen) {
ui64_t state = gen->state;
gen->state = state * 6364136223846793005ULL + gen->inc;
uint32_t mix = (uint32_t)(((state >> 18) ^ state) >> 27);
ui32_t mix = (ui32_t)(((state >> 18) ^ state) >> 27);
int rot = state >> 59;
return (mix >> rot) | (mix << ((-rot) & 31));
}
Expand All @@ -97,8 +98,8 @@ uint32_t set_rand(set_rand_t *gen) {
typedef struct set_node_s set_node_t;
struct set_node_s {
set_key_t key; // the key (not used for head or path)
int16_t size; // number of allocated pointers in right[]
int16_t fill; // number of pointers in right[] filled in
i16_t size; // number of allocated pointers in right[]
i16_t fill; // number of pointers in right[] filled in
set_node_t **right; // pointer for each level, each to the right
};

Expand All @@ -108,8 +109,8 @@ typedef struct set_s {
set_node_t *head; // skiplist head -- no key, just links
set_node_t *path; // right[] is path to key from set_found()
set_node_t *node; // node under construction, in case of longjmp()
int16_t depth; // maximum depth of the skiplist
uint64_t ran; // a precious trove of random bits
i16_t depth; // maximum depth of the skiplist
ui64_t ran; // a precious trove of random bits
set_rand_t gen; // random number generator state
jmp_buf env; // setjmp() environment for allocation errors
#ifdef SET_TRACK
Expand Down Expand Up @@ -184,13 +185,13 @@ void set_grow(set_t *set, set_node_t *node, int want, int fill) {
while (more < want)
more <<= 1;
node->right = set_alloc(set, node->right, more * sizeof(set_node_t *));
node->size = (int16_t)more;
node->size = (i16_t)more;
}
int i;
if (fill)
for (i = node->fill; i < want; i++)
node->right[i] = set->head;
node->fill = (int16_t)want;
node->fill = (i16_t)want;
}

// --private-- Return a new node. key is left uninitialized.
Expand Down Expand Up @@ -231,8 +232,8 @@ void set_start(set_t *set) {
set_grow(set, set->head, 1, 1); // one link back to head for an empty set
*(unsigned char *)&set->head->key = 137; // set id
set->depth = 0;
set_seed(&set->gen, ((uint64_t)(uintptr_t)set << 32) ^
((uint64_t)time(NULL) << 12) ^ clock(), 0);
set_seed(&set->gen, ((ui64_t)(ptrdiff_t)set << 32) ^
((ui64_t)time(NULL) << 12) ^ clock(), 0);
set->ran = 1;
}

Expand Down Expand Up @@ -337,7 +338,7 @@ int set_insert(set_t *set, set_key_t key) {
// The maximum depth is now deeper. Update the structures.
set_grow(set, set->path, level + 1, 1);
set_grow(set, set->head, level + 1, 1);
set->depth = (int16_t)level;
set->depth = (i16_t)level;
}

// Make a new node for the provided key, and insert it in the lists up to
Expand Down
17 changes: 12 additions & 5 deletions contrib/minizip/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#ifndef ZLIB_CONST
# define ZLIB_CONST
#endif
#include "zlib.h"
#include "zip.h"

Expand Down Expand Up @@ -125,9 +127,9 @@ typedef struct linkedlist_data_s

// zipAlreadyThere() set functions for a set of zero-terminated strings, and
// a block_t type for reading the central directory datablocks.
typedef char const *set_key_t;
typedef char *set_key_t;
#define set_cmp(a, b) strcmp(a, b)
#define set_drop(s, k) set_free(s, (void *)(intptr_t)(k))
#define set_drop(s, k) set_free(s, k)
#include "skipset.h"
typedef struct {
unsigned char *next; // next byte in datablock data
Expand Down Expand Up @@ -496,7 +498,12 @@ extern int ZEXPORT zipAlreadyThere(zipFile file, char const *name) {
}

// Return true if name is in the central directory.
return set_found(&zip->set, name);
size_t len = strlen(name);
char *copy = set_alloc(&zip->set, NULL, len + 1);
strcpy(copy, name);
int found = set_found(&zip->set, copy);
set_free(&zip->set, copy);
return found;
}


Expand Down Expand Up @@ -1646,7 +1653,7 @@ extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void* buf, unsigned i
else
#endif
{
zi->ci.stream.next_in = (Bytef*)(uintptr_t)buf;
zi->ci.stream.next_in = buf;
zi->ci.stream.avail_in = len;

while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
Expand Down

0 comments on commit be24a8f

Please sign in to comment.