/* Maximum dictionary size, according to inflateGetDictionary() description. */
#define MAX_DICTIONARY_SIZE 32768
-
-void test_compress (unsigned char *compr, z_uintmax_t comprLen, unsigned char *uncompr, z_uintmax_t uncomprLen);
-void test_gzio (const char *fname, unsigned char *uncompr, z_size_t uncomprLen);
-void test_deflate (unsigned char *compr, size_t comprLen);
-void test_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
-void test_large_deflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen, int zng_params);
-void test_large_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
-void test_flush (unsigned char *compr, z_uintmax_t *comprLen);
-void test_sync (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
-void test_dict_deflate (unsigned char *compr, size_t comprLen);
-void test_dict_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
-int main (int argc, char *argv[]);
-
-
static alloc_func zalloc = NULL;
static free_func zfree = NULL;
/* ===========================================================================
* Test compress() and uncompress()
*/
-void test_compress(unsigned char *compr, z_uintmax_t comprLen, unsigned char *uncompr, z_uintmax_t uncomprLen) {
+static void test_compress(unsigned char *compr, z_uintmax_t comprLen, unsigned char *uncompr, z_uintmax_t uncomprLen) {
int err;
unsigned int len = (unsigned int)strlen(hello)+1;
/* ===========================================================================
* Test read/write of .gz files
*/
-void test_gzio(const char *fname, unsigned char *uncompr, z_size_t uncomprLen) {
+static void test_gzio(const char *fname, unsigned char *uncompr, z_size_t uncomprLen) {
#ifdef NO_GZCOMPRESS
fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
#else
/* ===========================================================================
* Test deflate() with small buffers
*/
-void test_deflate(unsigned char *compr, size_t comprLen) {
+static void test_deflate(unsigned char *compr, size_t comprLen) {
PREFIX3(stream) c_stream; /* compression stream */
int err;
size_t len = strlen(hello)+1;
/* ===========================================================================
* Test inflate() with small buffers
*/
-void test_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
+static void test_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
int err;
PREFIX3(stream) d_stream; /* decompression stream */
/* ===========================================================================
* Test deflate() with large buffers and dynamic change of compression level
*/
-void test_large_deflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen, int zng_params) {
+static void test_large_deflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen, int zng_params) {
PREFIX3(stream) c_stream; /* compression stream */
int err;
#ifndef ZLIB_COMPAT
/* ===========================================================================
* Test inflate() with large buffers
*/
-void test_large_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
+static void test_large_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
int err;
PREFIX3(stream) d_stream; /* decompression stream */
/* ===========================================================================
* Test deflate() with full flush
*/
-void test_flush(unsigned char *compr, z_uintmax_t *comprLen) {
+static void test_flush(unsigned char *compr, z_uintmax_t *comprLen) {
PREFIX3(stream) c_stream; /* compression stream */
int err;
unsigned int len = (unsigned int)strlen(hello)+1;
* Test inflateSync()
* We expect a certain compressed block layout, so skip this with the original zlib.
*/
-void test_sync(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
+static void test_sync(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
int err;
PREFIX3(stream) d_stream; /* decompression stream */
/* ===========================================================================
* Test deflate() with preset dictionary
*/
-void test_dict_deflate(unsigned char *compr, size_t comprLen) {
+static void test_dict_deflate(unsigned char *compr, size_t comprLen) {
PREFIX3(stream) c_stream; /* compression stream */
int err;
/* ===========================================================================
* Test inflate() with a preset dictionary
*/
-void test_dict_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
+static void test_dict_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
int err;
uint8_t check_dictionary[MAX_DICTIONARY_SIZE];
uint32_t check_dictionary_len = 0;
/* ===========================================================================
* Test deflateBound() with small buffers
*/
-void test_deflate_bound(void) {
+static void test_deflate_bound(void) {
PREFIX3(stream) c_stream; /* compression stream */
int err;
unsigned int len = (unsigned int)strlen(hello)+1;
/* ===========================================================================
* Test deflateCopy() with small buffers
*/
-void test_deflate_copy(unsigned char *compr, size_t comprLen) {
+static void test_deflate_copy(unsigned char *compr, size_t comprLen) {
PREFIX3(stream) c_stream, c_stream_copy; /* compression stream */
int err;
size_t len = strlen(hello)+1;
/* ===========================================================================
* Test deflateGetDictionary() with small buffers
*/
-void test_deflate_get_dict(unsigned char *compr, size_t comprLen) {
+static void test_deflate_get_dict(unsigned char *compr, size_t comprLen) {
PREFIX3(stream) c_stream; /* compression stream */
int err;
unsigned char *dictNew = NULL;
/* ===========================================================================
* Test deflatePending() with small buffers
*/
-void test_deflate_pending(unsigned char *compr, size_t comprLen) {
+static void test_deflate_pending(unsigned char *compr, size_t comprLen) {
PREFIX3(stream) c_stream; /* compression stream */
int err;
int *bits = calloc(256, 1);
/* ===========================================================================
* Test deflatePrime() wrapping gzip around deflate stream
*/
-void test_deflate_prime(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
+static void test_deflate_prime(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
PREFIX3(stream) c_stream; /* compression stream */
PREFIX3(stream) d_stream; /* decompression stream */
int err;
/* ===========================================================================
* Test deflateSetHeader() with small buffers
*/
-void test_deflate_set_header(unsigned char *compr, size_t comprLen) {
+static void test_deflate_set_header(unsigned char *compr, size_t comprLen) {
PREFIX(gz_header) *head = calloc(1, sizeof(PREFIX(gz_header)));
PREFIX3(stream) c_stream; /* compression stream */
int err;
/* ===========================================================================
* Test deflateTune() with small buffers
*/
-void test_deflate_tune(unsigned char *compr, size_t comprLen) {
+static void test_deflate_tune(unsigned char *compr, size_t comprLen) {
PREFIX3(stream) c_stream; /* compression stream */
int err;
int good_length = 3;
static const char *prog = "minigzip_fuzzer";
-void error (const char *msg);
-void gz_compress (FILE *in, gzFile out);
-#ifdef USE_MMAP
-int gz_compress_mmap (FILE *in, gzFile out);
-#endif
-void gz_uncompress (gzFile in, FILE *out);
-void file_compress (char *file, char *mode);
-void file_uncompress (char *file);
-int main (int argc, char *argv[]);
-
/* ===========================================================================
* Display error message and exit
*/
-void error(const char *msg) {
+static void error(const char *msg) {
fprintf(stderr, "%s: %s\n", prog, msg);
exit(1);
}
-/* ===========================================================================
- * Compress input to output then close both files.
- */
-
-void gz_compress(FILE *in, gzFile out) {
- char buf[BUFLEN];
- int len;
- int err;
-
-#ifdef USE_MMAP
- /* Try first compressing with mmap. If mmap fails (minigzip used in a
- * pipe), use the normal fread loop.
- */
- if (gz_compress_mmap(in, out) == Z_OK) return;
-#endif
- /* Clear out the contents of buf before reading from the file to avoid
- MemorySanitizer: use-of-uninitialized-value warnings. */
- memset(buf, 0, sizeof(buf));
- for (;;) {
- len = (int)fread(buf, 1, sizeof(buf), in);
- if (ferror(in)) {
- perror("fread");
- exit(1);
- }
- if (len == 0) break;
-
- if (PREFIX(gzwrite)(out, buf, (unsigned)len) != len) error(PREFIX(gzerror)(out, &err));
- }
- fclose(in);
- if (PREFIX(gzclose)(out) != Z_OK) error("failed gzclose");
-}
-
#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
-
-/* Try compressing the input file at once using mmap. Return Z_OK if
+/* ===========================================================================
+ * Try compressing the input file at once using mmap. Return Z_OK if
* success, Z_ERRNO otherwise.
*/
-int gz_compress_mmap(FILE *in, gzFile out) {
+static int gz_compress_mmap(FILE *in, gzFile out) {
int len;
int err;
int ifd = fileno(in);
}
#endif /* USE_MMAP */
+/* ===========================================================================
+ * Compress input to output then close both files.
+ */
+
+static void gz_compress(FILE *in, gzFile out) {
+ char buf[BUFLEN];
+ int len;
+ int err;
+
+#ifdef USE_MMAP
+ /* Try first compressing with mmap. If mmap fails (minigzip used in a
+ * pipe), use the normal fread loop.
+ */
+ if (gz_compress_mmap(in, out) == Z_OK) return;
+#endif
+ /* Clear out the contents of buf before reading from the file to avoid
+ MemorySanitizer: use-of-uninitialized-value warnings. */
+ memset(buf, 0, sizeof(buf));
+ for (;;) {
+ len = (int)fread(buf, 1, sizeof(buf), in);
+ if (ferror(in)) {
+ perror("fread");
+ exit(1);
+ }
+ if (len == 0) break;
+
+ if (PREFIX(gzwrite)(out, buf, (unsigned)len) != len) error(PREFIX(gzerror)(out, &err));
+ }
+ fclose(in);
+ if (PREFIX(gzclose)(out) != Z_OK) error("failed gzclose");
+}
+
/* ===========================================================================
* Uncompress input to output then close both files.
*/
-void gz_uncompress(gzFile in, FILE *out) {
+static void gz_uncompress(gzFile in, FILE *out) {
char buf[BUFLENW];
int len;
int err;
* Compress the given file: create a corresponding .gz file and remove the
* original.
*/
-void file_compress(char *file, char *mode) {
+static void file_compress(char *file, char *mode) {
char outfile[MAX_NAME_LEN];
FILE *in;
gzFile out;
unlink(file);
}
-
/* ===========================================================================
* Uncompress the given file and remove the original.
*/
-void file_uncompress(char *file) {
+static void file_uncompress(char *file) {
char buf[MAX_NAME_LEN];
char *infile, *outfile;
FILE *out;
/* ===========================================================================
* deflate() using specialized parameters
*/
-void deflate_params(FILE *fin, FILE *fout, int32_t read_buf_size, int32_t write_buf_size, int32_t level,
+static void deflate_params(FILE *fin, FILE *fout, int32_t read_buf_size, int32_t write_buf_size, int32_t level,
int32_t window_bits, int32_t mem_level, int32_t strategy, int32_t flush) {
PREFIX3(stream) c_stream; /* compression stream */
uint8_t *read_buf;
/* ===========================================================================
* inflate() using specialized parameters
*/
-void inflate_params(FILE *fin, FILE *fout, int32_t read_buf_size, int32_t write_buf_size, int32_t window_bits,
+static void inflate_params(FILE *fin, FILE *fout, int32_t read_buf_size, int32_t write_buf_size, int32_t window_bits,
int32_t flush) {
PREFIX3(stream) d_stream; /* decompression stream */
uint8_t *read_buf;
free(write_buf);
}
-void show_help(void) {
+static void show_help(void) {
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"
static char *prog;
-void error (const char *msg);
-void gz_fatal (gzFile file);
-void gz_compress (FILE *in, gzFile out);
-#ifdef USE_MMAP
-int gz_compress_mmap (FILE *in, gzFile out);
-#endif
-void gz_uncompress (gzFile in, FILE *out);
-void file_compress (char *file, char *mode, int keep);
-void file_uncompress (char *file, int keep);
-int main (int argc, char *argv[]);
-
/* ===========================================================================
* Display error message and exit
*/
-void error(const char *msg) {
+static void error(const char *msg) {
fprintf(stderr, "%s: %s\n", prog, msg);
exit(1);
}
* Display last error message of gzFile, close it and exit
*/
-void gz_fatal(gzFile file) {
+static void gz_fatal(gzFile file) {
int err;
fprintf(stderr, "%s: %s\n", prog, PREFIX(gzerror)(file, &err));
PREFIX(gzclose)(file);
exit(1);
}
+#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
+/* ===========================================================================
+ * Try compressing the input file at once using mmap. Return Z_OK if
+ * success, Z_ERRNO otherwise.
+ */
+static int gz_compress_mmap(FILE *in, gzFile out) {
+ int len;
+ int ifd = fileno(in);
+ char *buf; /* mmap'ed buffer for the entire input file */
+ off_t buf_len; /* length of the input file */
+ struct stat sb;
+
+ /* Determine the size of the file, needed for mmap: */
+ if (fstat(ifd, &sb) < 0) return Z_ERRNO;
+ buf_len = sb.st_size;
+ if (buf_len <= 0) return Z_ERRNO;
+
+ /* Now do the actual mmap: */
+ buf = mmap((void *)0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
+ if (buf == (char *)(-1)) return Z_ERRNO;
+
+ /* Compress the whole file at once: */
+ len = PREFIX(gzwrite)(out, buf, (unsigned)buf_len);
+
+ if (len != (int)buf_len) gz_fatal(out);
+
+ munmap(buf, buf_len);
+ fclose(in);
+ if (PREFIX(gzclose)(out) != Z_OK) error("failed gzclose");
+ return Z_OK;
+}
+#endif /* USE_MMAP */
+
/* ===========================================================================
* Compress input to output then close both files.
*/
-void gz_compress(FILE *in, gzFile out) {
+static void gz_compress(FILE *in, gzFile out) {
char *buf;
int len;
if (PREFIX(gzclose)(out) != Z_OK) error("failed gzclose");
}
-#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
-
-/* Try compressing the input file at once using mmap. Return Z_OK if
- * success, Z_ERRNO otherwise.
- */
-int gz_compress_mmap(FILE *in, gzFile out) {
- int len;
- int ifd = fileno(in);
- char *buf; /* mmap'ed buffer for the entire input file */
- off_t buf_len; /* length of the input file */
- struct stat sb;
-
- /* Determine the size of the file, needed for mmap: */
- if (fstat(ifd, &sb) < 0) return Z_ERRNO;
- buf_len = sb.st_size;
- if (buf_len <= 0) return Z_ERRNO;
-
- /* Now do the actual mmap: */
- buf = mmap((void *)0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
- if (buf == (char *)(-1)) return Z_ERRNO;
-
- /* Compress the whole file at once: */
- len = PREFIX(gzwrite)(out, buf, (unsigned)buf_len);
-
- if (len != (int)buf_len) gz_fatal(out);
-
- munmap(buf, buf_len);
- fclose(in);
- if (PREFIX(gzclose)(out) != Z_OK) error("failed gzclose");
- return Z_OK;
-}
-#endif /* USE_MMAP */
-
/* ===========================================================================
* Uncompress input to output then close both files.
*/
-void gz_uncompress(gzFile in, FILE *out) {
+static void gz_uncompress(gzFile in, FILE *out) {
char *buf = (char *)malloc(BUFLENW);
int len;
* Compress the given file: create a corresponding .gz file and remove the
* original.
*/
-void file_compress(char *file, char *mode, int keep) {
+static void file_compress(char *file, char *mode, int keep) {
char outfile[MAX_NAME_LEN];
FILE *in;
gzFile out;
/* ===========================================================================
* Uncompress the given file and remove the original.
*/
-void file_uncompress(char *file, int keep) {
+static void file_uncompress(char *file, int keep) {
char buf[MAX_NAME_LEN];
char *infile, *outfile;
FILE *out;
unlink(infile);
}
-void show_help(void) {
+static void show_help(void) {
printf("Usage: minigzip [-c] [-d] [-k] [-f|-h|-R|-F|-T] [-A] [-0 to -9] [files...]\n\n"
" -c : write to standard output\n"
" -d : decompress\n"