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