aboutsummaryrefslogtreecommitdiff
path: root/test/minigzip.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/minigzip.c')
-rw-r--r--test/minigzip.c89
1 files changed, 51 insertions, 38 deletions
diff --git a/test/minigzip.c b/test/minigzip.c
index 134e10e6c3e9..40da8736fbd2 100644
--- a/test/minigzip.c
+++ b/test/minigzip.c
@@ -1,5 +1,5 @@
/* minigzip.c -- simulate gzip using the zlib compression library
- * Copyright (C) 1995-2006, 2010, 2011, 2016 Jean-loup Gailly
+ * Copyright (C) 1995-2026 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h
*/
@@ -15,6 +15,17 @@
/* @(#) $Id$ */
+#ifndef _POSIX_C_SOURCE
+# define _POSIX_C_SOURCE 200112L
+#endif
+
+#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS)
+# define _CRT_SECURE_NO_WARNINGS
+#endif
+#if defined(_WIN32) && !defined(_CRT_NONSTDC_NO_DEPRECATE)
+# define _CRT_NONSTDC_NO_DEPRECATE
+#endif
+
#include "zlib.h"
#include <stdio.h>
@@ -40,18 +51,16 @@
# define SET_BINARY_MODE(file)
#endif
-#if defined(_MSC_VER) && _MSC_VER < 1900
-# define snprintf _snprintf
-#endif
-
#ifdef VMS
# define unlink delete
# define GZ_SUFFIX "-gz"
#endif
-#ifdef RISCOS
-# define unlink remove
-# define GZ_SUFFIX "-gz"
-# define fileno(file) file->__file
+#if defined(__riscos) && !defined(__TARGET_UNIXLIB__)
+# define GZ_SUFFIX "/gz"
+# ifndef __GNUC__
+# define unlink remove
+# define fileno(file) file->__file
+# endif
#endif
#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
# include <unix.h> /* for fileno */
@@ -109,7 +118,7 @@ static char *strwinerror (error)
LocalFree(msgbuf);
}
else {
- sprintf(buf, "unknown win32 error (%ld)", error);
+ sprintf(buf, "unknown win32 error (%lu)", error);
}
SetLastError(lasterr);
@@ -142,6 +151,25 @@ static void pwinerror (s)
# define local
#endif
+/* ===========================================================================
+ * Safe string copy. Copy up to len bytes from src to dst, if src terminates
+ * with a null by then. If not, copy len-1 bytes from src, terminating it with
+ * a null in dst[len-1], cutting src short. Return a pointer to the terminating
+ * null. If len is zero, nothing is written to *dst and NULL is returned.
+ */
+static char *string_copy(char *dst, char const *src, z_size_t len) {
+ if (len == 0)
+ return NULL;
+ while (--len) {
+ *dst = *src++;
+ if (*dst == 0)
+ return dst;
+ dst++;
+ }
+ *dst = 0;
+ return dst;
+}
+
#ifdef Z_SOLO
/* for Z_SOLO, create simplified gz* functions using deflate and inflate */
@@ -224,7 +252,7 @@ static int gzwrite(gzFile gz, const void *buf, unsigned len) {
(void)deflate(strm, Z_NO_FLUSH);
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
} while (strm->avail_out == 0);
- return len;
+ return (int)len;
}
static int gzread(gzFile gz, void *buf, unsigned len) {
@@ -241,7 +269,7 @@ static int gzread(gzFile gz, void *buf, unsigned len) {
strm->next_out = (void *)buf;
strm->avail_out = len;
do {
- got = fread(in, 1, 1, gz->file);
+ got = (unsigned)fread(in, 1, 1, gz->file);
if (got == 0)
break;
strm->next_in = in;
@@ -255,7 +283,7 @@ static int gzread(gzFile gz, void *buf, unsigned len) {
if (ret == Z_STREAM_END)
inflateReset(strm);
} while (strm->avail_out);
- return len - strm->avail_out;
+ return (int)(len - strm->avail_out);
}
static int gzclose(gzFile gz) {
@@ -391,7 +419,7 @@ static void gz_uncompress(gzFile in, FILE *out) {
* original.
*/
static void file_compress(char *file, char *mode) {
- local char outfile[MAX_NAME_LEN];
+ local char outfile[MAX_NAME_LEN+1], *end;
FILE *in;
gzFile out;
@@ -400,12 +428,8 @@ static void file_compress(char *file, char *mode) {
exit(1);
}
-#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
- snprintf(outfile, sizeof(outfile), "%s%s", file, GZ_SUFFIX);
-#else
- strcpy(outfile, file);
- strcat(outfile, GZ_SUFFIX);
-#endif
+ end = string_copy(outfile, file, sizeof(outfile));
+ string_copy(end, GZ_SUFFIX, sizeof(outfile) - (z_size_t)(end - outfile));
in = fopen(file, "rb");
if (in == NULL) {
@@ -414,6 +438,7 @@ static void file_compress(char *file, char *mode) {
}
out = gzopen(outfile, mode);
if (out == NULL) {
+ fclose(in);
fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
exit(1);
}
@@ -427,7 +452,7 @@ static void file_compress(char *file, char *mode) {
* Uncompress the given file and remove the original.
*/
static void file_uncompress(char *file) {
- local char buf[MAX_NAME_LEN];
+ local char buf[MAX_NAME_LEN+1];
char *infile, *outfile;
FILE *out;
gzFile in;
@@ -438,11 +463,7 @@ static void file_uncompress(char *file) {
exit(1);
}
-#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
- snprintf(buf, sizeof(buf), "%s", file);
-#else
- strcpy(buf, file);
-#endif
+ string_copy(buf, file, sizeof(buf));
if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
infile = file;
@@ -451,11 +472,7 @@ static void file_uncompress(char *file) {
} else {
outfile = file;
infile = buf;
-#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
- snprintf(buf + len, sizeof(buf) - len, "%s", GZ_SUFFIX);
-#else
- strcat(infile, GZ_SUFFIX);
-#endif
+ string_copy(buf + len, GZ_SUFFIX, sizeof(buf) - len);
}
in = gzopen(infile, "rb");
if (in == NULL) {
@@ -464,6 +481,7 @@ static void file_uncompress(char *file) {
}
out = fopen(outfile, "wb");
if (out == NULL) {
+ gzclose(in);
perror(file);
exit(1);
}
@@ -488,14 +506,9 @@ int main(int argc, char *argv[]) {
int copyout = 0;
int uncompr = 0;
gzFile file;
- char *bname, outmode[20];
-
-#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
- snprintf(outmode, sizeof(outmode), "%s", "wb6 ");
-#else
- strcpy(outmode, "wb6 ");
-#endif
+ char *bname, outmode[5];
+ string_copy(outmode, "wb6 ", sizeof(outmode));
prog = argv[0];
bname = strrchr(argv[0], '/');
if (bname)