Chris@43: /* Chris@43: * pufftest.c Chris@43: * Copyright (C) 2002-2013 Mark Adler Chris@43: * For conditions of distribution and use, see copyright notice in puff.h Chris@43: * version 2.3, 21 Jan 2013 Chris@43: */ Chris@43: Chris@43: /* Example of how to use puff(). Chris@43: Chris@43: Usage: puff [-w] [-f] [-nnn] file Chris@43: ... | puff [-w] [-f] [-nnn] Chris@43: Chris@43: where file is the input file with deflate data, nnn is the number of bytes Chris@43: of input to skip before inflating (e.g. to skip a zlib or gzip header), and Chris@43: -w is used to write the decompressed data to stdout. -f is for coverage Chris@43: testing, and causes pufftest to fail with not enough output space (-f does Chris@43: a write like -w, so -w is not required). */ Chris@43: Chris@43: #include Chris@43: #include Chris@43: #include "puff.h" Chris@43: Chris@43: #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) Chris@43: # include Chris@43: # include Chris@43: # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) Chris@43: #else Chris@43: # define SET_BINARY_MODE(file) Chris@43: #endif Chris@43: Chris@43: #define local static Chris@43: Chris@43: /* Return size times approximately the cube root of 2, keeping the result as 1, Chris@43: 3, or 5 times a power of 2 -- the result is always > size, until the result Chris@43: is the maximum value of an unsigned long, where it remains. This is useful Chris@43: to keep reallocations less than ~33% over the actual data. */ Chris@43: local size_t bythirds(size_t size) Chris@43: { Chris@43: int n; Chris@43: size_t m; Chris@43: Chris@43: m = size; Chris@43: for (n = 0; m; n++) Chris@43: m >>= 1; Chris@43: if (n < 3) Chris@43: return size + 1; Chris@43: n -= 3; Chris@43: m = size >> n; Chris@43: m += m == 6 ? 2 : 1; Chris@43: m <<= n; Chris@43: return m > size ? m : (size_t)(-1); Chris@43: } Chris@43: Chris@43: /* Read the input file *name, or stdin if name is NULL, into allocated memory. Chris@43: Reallocate to larger buffers until the entire file is read in. Return a Chris@43: pointer to the allocated data, or NULL if there was a memory allocation Chris@43: failure. *len is the number of bytes of data read from the input file (even Chris@43: if load() returns NULL). If the input file was empty or could not be opened Chris@43: or read, *len is zero. */ Chris@43: local void *load(const char *name, size_t *len) Chris@43: { Chris@43: size_t size; Chris@43: void *buf, *swap; Chris@43: FILE *in; Chris@43: Chris@43: *len = 0; Chris@43: buf = malloc(size = 4096); Chris@43: if (buf == NULL) Chris@43: return NULL; Chris@43: in = name == NULL ? stdin : fopen(name, "rb"); Chris@43: if (in != NULL) { Chris@43: for (;;) { Chris@43: *len += fread((char *)buf + *len, 1, size - *len, in); Chris@43: if (*len < size) break; Chris@43: size = bythirds(size); Chris@43: if (size == *len || (swap = realloc(buf, size)) == NULL) { Chris@43: free(buf); Chris@43: buf = NULL; Chris@43: break; Chris@43: } Chris@43: buf = swap; Chris@43: } Chris@43: fclose(in); Chris@43: } Chris@43: return buf; Chris@43: } Chris@43: Chris@43: int main(int argc, char **argv) Chris@43: { Chris@43: int ret, put = 0, fail = 0; Chris@43: unsigned skip = 0; Chris@43: char *arg, *name = NULL; Chris@43: unsigned char *source = NULL, *dest; Chris@43: size_t len = 0; Chris@43: unsigned long sourcelen, destlen; Chris@43: Chris@43: /* process arguments */ Chris@43: while (arg = *++argv, --argc) Chris@43: if (arg[0] == '-') { Chris@43: if (arg[1] == 'w' && arg[2] == 0) Chris@43: put = 1; Chris@43: else if (arg[1] == 'f' && arg[2] == 0) Chris@43: fail = 1, put = 1; Chris@43: else if (arg[1] >= '0' && arg[1] <= '9') Chris@43: skip = (unsigned)atoi(arg + 1); Chris@43: else { Chris@43: fprintf(stderr, "invalid option %s\n", arg); Chris@43: return 3; Chris@43: } Chris@43: } Chris@43: else if (name != NULL) { Chris@43: fprintf(stderr, "only one file name allowed\n"); Chris@43: return 3; Chris@43: } Chris@43: else Chris@43: name = arg; Chris@43: source = load(name, &len); Chris@43: if (source == NULL) { Chris@43: fprintf(stderr, "memory allocation failure\n"); Chris@43: return 4; Chris@43: } Chris@43: if (len == 0) { Chris@43: fprintf(stderr, "could not read %s, or it was empty\n", Chris@43: name == NULL ? "" : name); Chris@43: free(source); Chris@43: return 3; Chris@43: } Chris@43: if (skip >= len) { Chris@43: fprintf(stderr, "skip request of %d leaves no input\n", skip); Chris@43: free(source); Chris@43: return 3; Chris@43: } Chris@43: Chris@43: /* test inflate data with offset skip */ Chris@43: len -= skip; Chris@43: sourcelen = (unsigned long)len; Chris@43: ret = puff(NIL, &destlen, source + skip, &sourcelen); Chris@43: if (ret) Chris@43: fprintf(stderr, "puff() failed with return code %d\n", ret); Chris@43: else { Chris@43: fprintf(stderr, "puff() succeeded uncompressing %lu bytes\n", destlen); Chris@43: if (sourcelen < len) fprintf(stderr, "%lu compressed bytes unused\n", Chris@43: len - sourcelen); Chris@43: } Chris@43: Chris@43: /* if requested, inflate again and write decompressd data to stdout */ Chris@43: if (put && ret == 0) { Chris@43: if (fail) Chris@43: destlen >>= 1; Chris@43: dest = malloc(destlen); Chris@43: if (dest == NULL) { Chris@43: fprintf(stderr, "memory allocation failure\n"); Chris@43: free(source); Chris@43: return 4; Chris@43: } Chris@43: puff(dest, &destlen, source + skip, &sourcelen); Chris@43: SET_BINARY_MODE(stdout); Chris@43: fwrite(dest, 1, destlen, stdout); Chris@43: free(dest); Chris@43: } Chris@43: Chris@43: /* clean up */ Chris@43: free(source); Chris@43: return ret; Chris@43: }