diff options
-rw-r--r-- | test/minigzip.c | 56 |
1 files changed, 27 insertions, 29 deletions
diff --git a/test/minigzip.c b/test/minigzip.c index 974a98d..aa787fc 100644 --- a/test/minigzip.c +++ b/test/minigzip.c | |||
@@ -44,10 +44,6 @@ | |||
44 | # define SET_BINARY_MODE(file) | 44 | # define SET_BINARY_MODE(file) |
45 | #endif | 45 | #endif |
46 | 46 | ||
47 | #if defined(_MSC_VER) && _MSC_VER < 1900 | ||
48 | # define snprintf _snprintf | ||
49 | #endif | ||
50 | |||
51 | #ifdef VMS | 47 | #ifdef VMS |
52 | # define unlink delete | 48 | # define unlink delete |
53 | # define GZ_SUFFIX "-gz" | 49 | # define GZ_SUFFIX "-gz" |
@@ -148,6 +144,25 @@ static void pwinerror (s) | |||
148 | # define local | 144 | # define local |
149 | #endif | 145 | #endif |
150 | 146 | ||
147 | /* =========================================================================== | ||
148 | * Safe string copy. Copy up to len bytes from src to dst, if src terminates | ||
149 | * with a null by then. If not, copy len-1 bytes from src, terminating it with | ||
150 | * a null in dst[len-1], cutting src short. Return a pointer to the terminating | ||
151 | * null. If len is zero, nothing is written to *dst and NULL is returned. | ||
152 | */ | ||
153 | static char *string_copy(char *dst, char const *src, z_size_t len) { | ||
154 | if (len == 0) | ||
155 | return NULL; | ||
156 | while (--len) { | ||
157 | *dst = *src++; | ||
158 | if (*dst == 0) | ||
159 | return dst; | ||
160 | dst++; | ||
161 | } | ||
162 | *dst = 0; | ||
163 | return dst; | ||
164 | } | ||
165 | |||
151 | #ifdef Z_SOLO | 166 | #ifdef Z_SOLO |
152 | /* for Z_SOLO, create simplified gz* functions using deflate and inflate */ | 167 | /* for Z_SOLO, create simplified gz* functions using deflate and inflate */ |
153 | 168 | ||
@@ -397,7 +412,7 @@ static void gz_uncompress(gzFile in, FILE *out) { | |||
397 | * original. | 412 | * original. |
398 | */ | 413 | */ |
399 | static void file_compress(char *file, char *mode) { | 414 | static void file_compress(char *file, char *mode) { |
400 | local char outfile[MAX_NAME_LEN]; | 415 | local char outfile[MAX_NAME_LEN+1], *end; |
401 | FILE *in; | 416 | FILE *in; |
402 | gzFile out; | 417 | gzFile out; |
403 | 418 | ||
@@ -406,12 +421,8 @@ static void file_compress(char *file, char *mode) { | |||
406 | exit(1); | 421 | exit(1); |
407 | } | 422 | } |
408 | 423 | ||
409 | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) | 424 | end = string_copy(outfile, file, sizeof(outfile)); |
410 | snprintf(outfile, sizeof(outfile), "%s%s", file, GZ_SUFFIX); | 425 | string_copy(end, GZ_SUFFIX, (outfile + sizeof(outfile)) - end); |
411 | #else | ||
412 | strcpy(outfile, file); | ||
413 | strcat(outfile, GZ_SUFFIX); | ||
414 | #endif | ||
415 | 426 | ||
416 | in = fopen(file, "rb"); | 427 | in = fopen(file, "rb"); |
417 | if (in == NULL) { | 428 | if (in == NULL) { |
@@ -433,7 +444,7 @@ static void file_compress(char *file, char *mode) { | |||
433 | * Uncompress the given file and remove the original. | 444 | * Uncompress the given file and remove the original. |
434 | */ | 445 | */ |
435 | static void file_uncompress(char *file) { | 446 | static void file_uncompress(char *file) { |
436 | local char buf[MAX_NAME_LEN]; | 447 | local char buf[MAX_NAME_LEN+1]; |
437 | char *infile, *outfile; | 448 | char *infile, *outfile; |
438 | FILE *out; | 449 | FILE *out; |
439 | gzFile in; | 450 | gzFile in; |
@@ -444,11 +455,7 @@ static void file_uncompress(char *file) { | |||
444 | exit(1); | 455 | exit(1); |
445 | } | 456 | } |
446 | 457 | ||
447 | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) | 458 | string_copy(buf, file, sizeof(buf)); |
448 | snprintf(buf, sizeof(buf), "%s", file); | ||
449 | #else | ||
450 | strcpy(buf, file); | ||
451 | #endif | ||
452 | 459 | ||
453 | if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) { | 460 | if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) { |
454 | infile = file; | 461 | infile = file; |
@@ -457,11 +464,7 @@ static void file_uncompress(char *file) { | |||
457 | } else { | 464 | } else { |
458 | outfile = file; | 465 | outfile = file; |
459 | infile = buf; | 466 | infile = buf; |
460 | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) | 467 | string_copy(buf + len, GZ_SUFFIX, sizeof(buf) - len); |
461 | snprintf(buf + len, sizeof(buf) - len, "%s", GZ_SUFFIX); | ||
462 | #else | ||
463 | strcat(infile, GZ_SUFFIX); | ||
464 | #endif | ||
465 | } | 468 | } |
466 | in = gzopen(infile, "rb"); | 469 | in = gzopen(infile, "rb"); |
467 | if (in == NULL) { | 470 | if (in == NULL) { |
@@ -494,14 +497,9 @@ int main(int argc, char *argv[]) { | |||
494 | int copyout = 0; | 497 | int copyout = 0; |
495 | int uncompr = 0; | 498 | int uncompr = 0; |
496 | gzFile file; | 499 | gzFile file; |
497 | char *bname, outmode[20]; | 500 | char *bname, outmode[5]; |
498 | |||
499 | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) | ||
500 | snprintf(outmode, sizeof(outmode), "%s", "wb6 "); | ||
501 | #else | ||
502 | strcpy(outmode, "wb6 "); | ||
503 | #endif | ||
504 | 501 | ||
502 | string_copy(outmode, "wb6 ", sizeof(outmode)); | ||
505 | prog = argv[0]; | 503 | prog = argv[0]; |
506 | bname = strrchr(argv[0], '/'); | 504 | bname = strrchr(argv[0], '/'); |
507 | if (bname) | 505 | if (bname) |