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