Skip to content

Commit

Permalink
Create z_size_t and z_ssize_t types.
Browse files Browse the repository at this point in the history
Normally these are set to size_t and ssize_t. But if they do not
exist, then they are set to the smallest integer type that can
contain a pointer. size_t is unsigned and ssize_t is signed.
  • Loading branch information
madler authored and kornelski committed Oct 13, 2023
1 parent 443eab2 commit a3ba995
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 22 deletions.
106 changes: 94 additions & 12 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ show $cc -c $test.c
if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
echo ... using gcc >> configure.log
CC="$cc"
CFLAGS="${CFLAGS--O3} ${ARCHS}"
CFLAGS="${CFLAGS--O3}"
SFLAGS="${CFLAGS--O3} -fPIC"
LDFLAGS="${LDFLAGS} ${ARCHS}"
if test "$ARCHS"; then
CFLAGS="${CFLAGS} ${ARCHS}"
LDFLAGS="${LDFLAGS} ${ARCHS}"
fi
if test $build64 -eq 1; then
CFLAGS="${CFLAGS} -m64"
SFLAGS="${SFLAGS} -m64"
Expand Down Expand Up @@ -366,16 +369,16 @@ if ($CC -c $CFLAGS $test.c) 2>/dev/null; then
}
echo - using any output from compiler to indicate an error >> configure.log
else
try()
{
show $*
( $* ) >> configure.log 2>&1
ret=$?
if test $ret -ne 0; then
echo "(exit code "$ret")" >> configure.log
fi
return $ret
}
try()
{
show $*
( $* ) >> configure.log 2>&1
ret=$?
if test $ret -ne 0; then
echo "(exit code "$ret")" >> configure.log
fi
return $ret
}
fi

tryboth()
Expand Down Expand Up @@ -451,6 +454,85 @@ esac

echo >> configure.log

# check for size_t
cat > $test.c <<EOF
#include <stdio.h>
#include <stdlib.h>
size_t dummy = 0;
EOF
if try $CC -c $CFLAGS $test.c; then
echo "Checking for size_t... Yes." | tee -a configure.log
need_sizet=0
else
echo "Checking for size_t... No." | tee -a configure.log
need_sizet=1
fi

echo >> configure.log

# check for ssize_t
cat > $test.c <<EOF
#include <sys/types.h>
ssize_t dummy = 0;
EOF
if try $CC -c $CFLAGS $test.c; then
echo "Checking for ssize_t... Yes." | tee -a configure.log
need_ssizet=0
else
echo "Checking for ssize_t... No." | tee -a configure.log
need_ssizet=1
fi

echo >> configure.log

# find the size_t integer type, if needed
if test $need_sizet -eq 1 -o $need_ssizet -eq 1; then
cat > $test.c <<EOF
long long dummy = 0;
EOF
if try $CC -c $CFLAGS $test.c; then
echo "Checking for long long... Yes." | tee -a configure.log
cat > $test.c <<EOF
#include <stdio.h>
int main(void) {
if (sizeof(void *) <= sizeof(int)) puts("int");
else if (sizeof(void *) <= sizeof(long)) puts("long");
else puts("long long");
return 0;
}
EOF
else
echo "Checking for long long... No." | tee -a configure.log
cat > $test.c <<EOF
#include <stdio.h>
int main(void) {
if (sizeof(void *) <= sizeof(int)) puts("int");
else puts("long");
return 0;
}
EOF
fi
if try $CC $CFLAGS -o $test $test.c; then
sizet=`./$test`
echo "Checking for a pointer-size integer type..." $sizet"." | tee -a configure.log
else
echo "Failed to find a pointer-size integer type." | tee -a configure.log
leave 1
fi
fi

if test $need_sizet -eq 1; then
CFLAGS="${CFLAGS} -DNO_SIZE_T=${sizet}"
SFLAGS="${SFLAGS} -DNO_SIZE_T=${sizet}"
fi

if test $need_ssizet -eq 1; then
CFLAGS="${CFLAGS} -DNO_SSIZE_T=${sizet}"
SFLAGS="${SFLAGS} -DNO_SSIZE_T=${sizet}"
fi

echo >> configure.log

# check for large file support, and if none, check for fseeko()
cat > $test.c <<EOF
#include <sys/types.h>
Expand Down
4 changes: 2 additions & 2 deletions gzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static void gz_reset(gz_statep state) {
static gzFile gz_open(const void *path, int fd, const char *mode)
{
gz_statep state;
size_t len;
z_size_t len;
int oflag;
#ifdef O_CLOEXEC
int cloexec = 0;
Expand Down Expand Up @@ -186,7 +186,7 @@ static gzFile gz_open(const void *path, int fd, const char *mode)
#ifdef _WIN32
if (fd == -2) {
len = wcstombs(NULL, path, 0);
if (len == (size_t)-1)
if (len == (z_size_t)-1)
len = 0;
}
else
Expand Down
2 changes: 1 addition & 1 deletion gzread.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local int gz_skip(gz_statep, z_off64_t);
read the number of bytes requested, depending on the type of descriptor. */
static int gz_load(gz_statep state, unsigned char *buf, unsigned len,
unsigned *have) {
int ret;
z_ssize_t ret;

*have = 0;
do {
Expand Down
13 changes: 7 additions & 6 deletions gzwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ static int gz_init(gz_statep state) {
}

/* Compress whatever is at avail_in and next_in and write to the output file.
Return -1 if there is an error writing to the output file, otherwise 0.
flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
then the deflate() state is reset to start a new gzip stream. If gz->direct
is true, then simply write to the output file without compressing, and
ignore flush. */
Return -1 if there is an error writing to the output file or if gz_init()
fails to allocate memory, otherwise 0. flush is assumed to be a valid
deflate() flush value. If flush is Z_FINISH, then the deflate() state is
reset to start a new gzip stream. If gz->direct is true, then simply write
to the output file without compressing, and ignore flush. */
static int gz_comp(gz_statep state, int flush) {
int ret, got;
int ret;
z_ssize_t got;
unsigned have;
z_streamp strm = &(state->strm);

Expand Down
2 changes: 1 addition & 1 deletion test/minigzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ void file_uncompress(char *file, int keep)
char *infile, *outfile;
FILE *out;
gzFile in;
size_t len = strlen(file);
z_size_t len = strlen(file);

if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
fprintf(stderr, "%s: filename too long\n", prog);
Expand Down
15 changes: 15 additions & 0 deletions zconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@
# define z_const
#endif

#ifndef Z_SOLO
# ifdef NO_SIZE_T
typedef unsigned NO_SIZE_T z_size_t;
# else
# include <stddef.h>
typedef size_t z_size_t;
# endif
# ifdef NO_SSIZE_T
typedef NO_SSIZE_T z_ssize_t;
# else
# include <sys/types.h>
typedef ssize_t z_ssize_t;
# endif
#endif

/* Maximum value for memLevel in deflateInit2 */
#ifndef MAX_MEM_LEVEL
# ifdef MAXSEG_64K
Expand Down
15 changes: 15 additions & 0 deletions zconf.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@
# define z_const
#endif

#ifndef Z_SOLO
# ifdef NO_SIZE_T
typedef unsigned NO_SIZE_T z_size_t;
# else
# include <stddef.h>
typedef size_t z_size_t;
# endif
# ifdef NO_SSIZE_T
typedef NO_SSIZE_T z_ssize_t;
# else
# include <sys/types.h>
typedef ssize_t z_ssize_t;
# endif
#endif

/* Maximum value for memLevel in deflateInit2 */
#ifndef MAX_MEM_LEVEL
# ifdef MAXSEG_64K
Expand Down
15 changes: 15 additions & 0 deletions zconf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@
# define z_const
#endif

#ifndef Z_SOLO
# ifdef NO_SIZE_T
typedef unsigned NO_SIZE_T z_size_t;
# else
# include <stddef.h>
typedef size_t z_size_t;
# endif
# ifdef NO_SSIZE_T
typedef NO_SSIZE_T z_ssize_t;
# else
# include <sys/types.h>
typedef ssize_t z_ssize_t;
# endif
#endif

/* Maximum value for memLevel in deflateInit2 */
#ifndef MAX_MEM_LEVEL
# ifdef MAXSEG_64K
Expand Down

0 comments on commit a3ba995

Please sign in to comment.