Skip to content

Latest commit

 

History

History
165 lines (150 loc) · 4.8 KB

pufftest.c

File metadata and controls

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