1 /*
   2  * pufftest.c
   3  * Copyright (C) 2002-2013 Mark Adler
   4  * For conditions of distribution and use, see copyright notice in puff.h
   5  * version 2.3, 21 Jan 2013
   6  */
   7 
   8 /* Example of how to use puff().
   9 
  10    Usage: puff [-w] [-f] [-nnn] file
  11           ... | puff [-w] [-f] [-nnn]
  12 
  13    where file is the input file with deflate data, nnn is the number of bytes
  14    of input to skip before inflating (e.g. to skip a zlib or gzip header), and
  15    -w is used to write the decompressed data to stdout.  -f is for coverage
  16    testing, and causes pufftest to fail with not enough output space (-f does
  17    a write like -w, so -w is not required). */
  18 
  19 #include <stdio.h>
  20 #include <stdlib.h>
  21 #include "puff.h"
  22 
  23 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  24 #  include <fcntl.h>
  25 #  include <io.h>
  26 #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  27 #else
  28 #  define SET_BINARY_MODE(file)
  29 #endif
  30 
  31 #define local static
  32 
  33 /* Return size times approximately the cube root of 2, keeping the result as 1,
  34    3, or 5 times a power of 2 -- the result is always > size, until the result
  35    is the maximum value of an unsigned long, where it remains.  This is useful
  36    to keep reallocations less than ~33% over the actual data. */
  37 local size_t bythirds(size_t size)
  38 {
  39     int n;
  40     size_t m;
  41 
  42     m = size;
  43     for (n = 0; m; n++)
  44         m >>= 1;
  45     if (n < 3)
  46         return size + 1;
  47     n -= 3;
  48     m = size >> n;
  49     m += m == 6 ? 2 : 1;
  50     m <<= n;
  51     return m > size ? m : (size_t)(-1);
  52 }
  53 
  54 /* Read the input file *name, or stdin if name is NULL, into allocated memory.
  55    Reallocate to larger buffers until the entire file is read in.  Return a
  56    pointer to the allocated data, or NULL if there was a memory allocation
  57    failure.  *len is the number of bytes of data read from the input file (even
  58    if load() returns NULL).  If the input file was empty or could not be opened
  59    or read, *len is zero. */
  60 local void *load(const char *name, size_t *len)
  61 {
  62     size_t size;
  63     void *buf, *swap;
  64     FILE *in;
  65 
  66     *len = 0;
  67     buf = malloc(size = 4096);
  68     if (buf == NULL)
  69         return NULL;
  70     in = name == NULL ? stdin : fopen(name, "rb");
  71     if (in != NULL) {
  72         for (;;) {
  73             *len += fread((char *)buf + *len, 1, size - *len, in);
  74             if (*len < size) break;
  75             size = bythirds(size);
  76             if (size == *len || (swap = realloc(buf, size)) == NULL) {
  77                 free(buf);
  78                 buf = NULL;
  79                 break;
  80             }
  81             buf = swap;
  82         }
  83         fclose(in);
  84     }
  85     return buf;
  86 }
  87 
  88 int main(int argc, char **argv)
  89 {
  90     int ret, put = 0, fail = 0;
  91     unsigned skip = 0;
  92     char *arg, *name = NULL;
  93     unsigned char *source = NULL, *dest;
  94     size_t len = 0;
  95     unsigned long sourcelen, destlen;
  96 
  97     /* process arguments */
  98     while (arg = *++argv, --argc)
  99         if (arg[0] == '-') {
 100             if (arg[1] == 'w' && arg[2] == 0)
 101                 put = 1;
 102             else if (arg[1] == 'f' && arg[2] == 0)
 103                 fail = 1, put = 1;
 104             else if (arg[1] >= '0' && arg[1] <= '9')
 105                 skip = (unsigned)atoi(arg + 1);
 106             else {
 107                 fprintf(stderr, "invalid option %s\n", arg);
 108                 return 3;
 109             }
 110         }
 111         else if (name != NULL) {
 112             fprintf(stderr, "only one file name allowed\n");
 113             return 3;
 114         }
 115         else
 116             name = arg;
 117     source = load(name, &len);
 118     if (source == NULL) {
 119         fprintf(stderr, "memory allocation failure\n");
 120         return 4;
 121     }
 122     if (len == 0) {
 123         fprintf(stderr, "could not read %s, or it was empty\n",
 124                 name == NULL ? "<stdin>" : name);
 125         free(source);
 126         return 3;
 127     }
 128     if (skip >= len) {
 129         fprintf(stderr, "skip request of %d leaves no input\n", skip);
 130         free(source);
 131         return 3;
 132     }
 133 
 134     /* test inflate data with offset skip */
 135     len -= skip;
 136     sourcelen = (unsigned long)len;
 137     ret = puff(NIL, &destlen, source + skip, &sourcelen);
 138     if (ret)
 139         fprintf(stderr, "puff() failed with return code %d\n", ret);
 140     else {
 141         fprintf(stderr, "puff() succeeded uncompressing %lu bytes\n", destlen);
 142         if (sourcelen < len) fprintf(stderr, "%lu compressed bytes unused\n",
 143                                      len - sourcelen);
 144     }
 145 
 146     /* if requested, inflate again and write decompressd data to stdout */
 147     if (put && ret == 0) {
 148         if (fail)
 149             destlen >>= 1;
 150         dest = malloc(destlen);
 151         if (dest == NULL) {
 152             fprintf(stderr, "memory allocation failure\n");
 153             free(source);
 154             return 4;
 155         }
 156         puff(dest, &destlen, source + skip, &sourcelen);
 157         SET_BINARY_MODE(stdout);
 158         fwrite(dest, 1, destlen, stdout);
 159         free(dest);
 160     }
 161 
 162     /* clean up */
 163     free(source);
 164     return ret;
 165 }