3
* Copyright (C) 2002-2010 Mark Adler
4
* For conditions of distribution and use, see copyright notice in puff.h
5
* version 2.2, 25 Apr 2010
8
/* Example of how to use puff().
10
Usage: puff [-w] [-f] [-nnn] file
11
... | puff [-w] [-f] [-nnn]
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). */
23
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
26
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
28
# define SET_BINARY_MODE(file)
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)
51
return m > size ? m : (size_t)(-1);
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)
67
buf = malloc(size = 4096);
70
in = name == NULL ? stdin : fopen(name, "rb");
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) {
88
int main(int argc, char **argv)
90
int ret, put = 0, fail = 0;
92
char *arg, *name = NULL;
93
unsigned char *source = NULL, *dest;
95
unsigned long sourcelen, destlen;
97
/* process arguments */
98
while (arg = *++argv, --argc)
100
if (arg[1] == 'w' && arg[2] == 0)
102
else if (arg[1] == 'f' && arg[2] == 0)
104
else if (arg[1] >= '0' && arg[1] <= '9')
105
skip = (unsigned)atoi(arg + 1);
107
fprintf(stderr, "invalid option %s\n", arg);
111
else if (name != NULL) {
112
fprintf(stderr, "only one file name allowed\n");
117
source = load(name, &len);
118
if (source == NULL) {
119
fprintf(stderr, "memory allocation failure\n");
123
fprintf(stderr, "could not read %s, or it was empty\n",
124
name == NULL ? "<stdin>" : name);
129
fprintf(stderr, "skip request of %d leaves no input\n", skip);
134
/* test inflate data with offset skip */
136
sourcelen = (unsigned long)len;
137
ret = puff(NIL, &destlen, source + skip, &sourcelen);
139
fprintf(stderr, "puff() failed with return code %d\n", ret);
141
fprintf(stderr, "puff() succeeded uncompressing %lu bytes\n", destlen);
142
if (sourcelen < len) fprintf(stderr, "%lu compressed bytes unused\n",
146
/* if requested, inflate again and write decompressd data to stdout */
147
if (put && ret == 0) {
150
dest = malloc(destlen);
152
fprintf(stderr, "memory allocation failure\n");
156
puff(dest, &destlen, source + skip, &sourcelen);
157
SET_BINARY_MODE(stdout);
158
fwrite(dest, 1, destlen, stdout);