diff options
Diffstat (limited to 'test/minideflate.c')
-rw-r--r-- | test/minideflate.c | 85 |
1 files changed, 66 insertions, 19 deletions
diff --git a/test/minideflate.c b/test/minideflate.c index ae04a29..be2110a 100644 --- a/test/minideflate.c +++ b/test/minideflate.c @@ -15,26 +15,21 @@ #include <inttypes.h> #include "zbuild.h" -#ifdef ZLIB_COMPAT -# include "zlib.h" -#else -# include "zlib-ng.h" -#endif +#include "zutil.h" #if defined(_WIN32) || defined(__CYGWIN__) # include <fcntl.h> # include <io.h> +# include <string.h> # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) +# ifdef _MSC_VER +# define strcasecmp _stricmp +# endif #else +# include <strings.h> # define SET_BINARY_MODE(file) #endif -#if MAX_MEM_LEVEL >= 8 -# define DEF_MEM_LEVEL 8 -#else -# define DEF_MEM_LEVEL MAX_MEM_LEVEL -#endif - #define CHECK_ERR(err, msg) { \ if (err != Z_OK) { \ fprintf(stderr, "%s error: %d\n", msg, err); \ @@ -42,6 +37,9 @@ } \ } +/* Default read/write i/o buffer size based on GZBUFSIZE */ +#define BUFSIZE 131072 + /* =========================================================================== * deflate() using specialized parameters */ @@ -175,7 +173,7 @@ void inflate_params(FILE *fin, FILE *fout, int32_t read_buf_size, int32_t write_ do { err = PREFIX(inflate)(&d_stream, flush); if (err == Z_STREAM_END) break; - CHECK_ERR(err, "deflate"); + CHECK_ERR(err, "inflate"); if (d_stream.next_out == write_buf + write_buf_size) { fwrite(write_buf, 1, write_buf_size, fout); @@ -214,15 +212,19 @@ void inflate_params(FILE *fin, FILE *fout, int32_t read_buf_size, int32_t write_ } void show_help(void) { - printf("Usage: minideflate [-c] [-f|-h|-R|-F] [-m level] [-r/-t size] [-s flush] [-w bits] [-0 to -9] [input file]\n\n" \ + printf("Usage: minideflate [-c][-d][-k] [-f|-h|-R|-F] [-m level] [-r/-t size] [-s flush] [-w bits] [-0 to -9] [input file]\n\n" \ " -c : write to standard output\n" \ " -d : decompress\n" \ + " -k : keep input file\n" \ " -f : compress with Z_FILTERED\n" \ " -h : compress with Z_HUFFMAN_ONLY\n" \ " -R : compress with Z_RLE\n" \ " -F : compress with Z_FIXED\n" \ " -m : memory level (1 to 8)\n" \ - " -w : window bits (8 to 15 for gzip, -8 to -15 for zlib)\n" \ + " -w : window bits..\n" \ + " : -1 to -15 for raw deflate\n" + " : 0 to 15 for deflate (adler32)\n" + " : 16 to 31 for gzip (crc32)\n" " -s : flush type (0 to 5)\n" \ " -r : read buffer size\n" \ " -t : write buffer size\n" \ @@ -232,18 +234,24 @@ void show_help(void) { int main(int argc, char **argv) { int32_t i; int32_t mem_level = DEF_MEM_LEVEL; - int32_t window_bits = MAX_WBITS; + int32_t window_bits = INT32_MAX; int32_t strategy = Z_DEFAULT_STRATEGY; int32_t level = Z_DEFAULT_COMPRESSION; - int32_t read_buf_size = 4096; - int32_t write_buf_size = 4096; + int32_t read_buf_size = BUFSIZE; + int32_t write_buf_size = BUFSIZE; int32_t flush = Z_NO_FLUSH; uint8_t copyout = 0; uint8_t uncompr = 0; - char out_file[320]; + uint8_t keep = 0; FILE *fin = stdin; FILE *fout = stdout; + + if (argc == 1) { + show_help(); + return 64; /* EX_USAGE */ + } + for (i = 1; i < argc; i++) { if ((strcmp(argv[i], "-m") == 0) && (i + 1 < argc)) mem_level = atoi(argv[++i]); @@ -259,8 +267,12 @@ int main(int argc, char **argv) { copyout = 1; else if (strcmp(argv[i], "-d") == 0) uncompr = 1; + else if (strcmp(argv[i], "-k") == 0) + keep = 1; else if (strcmp(argv[i], "-f") == 0) strategy = Z_FILTERED; + else if (strcmp(argv[i], "-F") == 0) + strategy = Z_FIXED; else if (strcmp(argv[i], "-h") == 0) strategy = Z_HUFFMAN_ONLY; else if (strcmp(argv[i], "-R") == 0) @@ -279,6 +291,7 @@ int main(int argc, char **argv) { SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdout); + if (i != argc) { fin = fopen(argv[i], "rb+"); if (fin == NULL) { @@ -286,14 +299,45 @@ int main(int argc, char **argv) { exit(1); } if (!copyout) { - snprintf(out_file, sizeof(out_file), "%s%s", argv[i], (window_bits < 0) ? ".zz" : ".gz"); + char *out_file = (char *)calloc(1, strlen(argv[i]) + 6); + if (out_file == NULL) { + fprintf(stderr, "Not enough memory\n"); + exit(1); + } + strcat(out_file, argv[i]); + if (!uncompr) { + if (window_bits < 0) { + strcat(out_file, ".zraw"); + } else if (window_bits > MAX_WBITS) { + strcat(out_file, ".gz"); + } else { + strcat(out_file, ".z"); + } + } else { + char *out_ext = strrchr(out_file, '.'); + if (out_ext != NULL) { + if (strcasecmp(out_ext, ".zraw") == 0 && window_bits == INT32_MAX) { + fprintf(stderr, "Must specify window bits for raw deflate stream\n"); + exit(1); + } + *out_ext = 0; + } + } fout = fopen(out_file, "wb"); if (fout == NULL) { fprintf(stderr, "Failed to open file: %s\n", out_file); exit(1); } + free(out_file); } } + + if (window_bits == INT32_MAX) { + window_bits = MAX_WBITS; + /* Auto-detect wrapper for inflateInit */ + if (uncompr) + window_bits += 32; + } if (uncompr) { inflate_params(fin, fout, read_buf_size, write_buf_size, window_bits, flush); @@ -303,6 +347,9 @@ int main(int argc, char **argv) { if (fin != stdin) { fclose(fin); + if (!copyout && !keep) { + unlink(argv[i]); + } } if (fout != stdout) { fclose(fout); |