sl@0
|
1 |
/* example.c -- usage example of the zlib compression library
|
sl@0
|
2 |
* Copyright (C) 1995-1998 Jean-loup Gailly.
|
sl@0
|
3 |
* For conditions of distribution and use, see copyright notice in zlib.h
|
sl@0
|
4 |
*/
|
sl@0
|
5 |
|
sl@0
|
6 |
/* @(#) $Id$ */
|
sl@0
|
7 |
|
sl@0
|
8 |
#include <stdio.h>
|
sl@0
|
9 |
#include "zlib.h"
|
sl@0
|
10 |
|
sl@0
|
11 |
#ifdef STDC
|
sl@0
|
12 |
# include <string.h>
|
sl@0
|
13 |
# include <stdlib.h>
|
sl@0
|
14 |
#else
|
sl@0
|
15 |
extern void exit OF((int));
|
sl@0
|
16 |
#endif
|
sl@0
|
17 |
|
sl@0
|
18 |
#if defined(VMS) || defined(RISCOS)
|
sl@0
|
19 |
# define TESTFILE "foo-gz"
|
sl@0
|
20 |
#else
|
sl@0
|
21 |
# define TESTFILE "foo.gz"
|
sl@0
|
22 |
#endif
|
sl@0
|
23 |
|
sl@0
|
24 |
#define CHECK_ERR(err, msg) { \
|
sl@0
|
25 |
if (err != Z_OK) { \
|
sl@0
|
26 |
fprintf(stderr, "%s error: %d\n", msg, err); \
|
sl@0
|
27 |
exit(1); \
|
sl@0
|
28 |
} \
|
sl@0
|
29 |
}
|
sl@0
|
30 |
|
sl@0
|
31 |
const char hello[] = "hello, hello!";
|
sl@0
|
32 |
/* "hello world" would be more standard, but the repeated "hello"
|
sl@0
|
33 |
* stresses the compression code better, sorry...
|
sl@0
|
34 |
*/
|
sl@0
|
35 |
|
sl@0
|
36 |
const char dictionary[] = "hello";
|
sl@0
|
37 |
uLong dictId; /* Adler32 value of the dictionary */
|
sl@0
|
38 |
|
sl@0
|
39 |
void test_compress OF((Byte *compr, uLong comprLen,
|
sl@0
|
40 |
Byte *uncompr, uLong uncomprLen));
|
sl@0
|
41 |
void test_gzio OF((const char *out, const char *in,
|
sl@0
|
42 |
Byte *uncompr, int uncomprLen));
|
sl@0
|
43 |
void test_deflate OF((Byte *compr, uLong comprLen));
|
sl@0
|
44 |
void test_inflate OF((Byte *compr, uLong comprLen,
|
sl@0
|
45 |
Byte *uncompr, uLong uncomprLen));
|
sl@0
|
46 |
void test_large_deflate OF((Byte *compr, uLong comprLen,
|
sl@0
|
47 |
Byte *uncompr, uLong uncomprLen));
|
sl@0
|
48 |
void test_large_inflate OF((Byte *compr, uLong comprLen,
|
sl@0
|
49 |
Byte *uncompr, uLong uncomprLen));
|
sl@0
|
50 |
void test_flush OF((Byte *compr, uLong *comprLen));
|
sl@0
|
51 |
void test_sync OF((Byte *compr, uLong comprLen,
|
sl@0
|
52 |
Byte *uncompr, uLong uncomprLen));
|
sl@0
|
53 |
void test_dict_deflate OF((Byte *compr, uLong comprLen));
|
sl@0
|
54 |
void test_dict_inflate OF((Byte *compr, uLong comprLen,
|
sl@0
|
55 |
Byte *uncompr, uLong uncomprLen));
|
sl@0
|
56 |
int main OF((int argc, char *argv[]));
|
sl@0
|
57 |
|
sl@0
|
58 |
/* ===========================================================================
|
sl@0
|
59 |
* Test compress() and uncompress()
|
sl@0
|
60 |
*/
|
sl@0
|
61 |
void test_compress(compr, comprLen, uncompr, uncomprLen)
|
sl@0
|
62 |
Byte *compr, *uncompr;
|
sl@0
|
63 |
uLong comprLen, uncomprLen;
|
sl@0
|
64 |
{
|
sl@0
|
65 |
int err;
|
sl@0
|
66 |
uLong len = strlen(hello)+1;
|
sl@0
|
67 |
|
sl@0
|
68 |
err = compress(compr, &comprLen, (const Bytef*)hello, len);
|
sl@0
|
69 |
CHECK_ERR(err, "compress");
|
sl@0
|
70 |
|
sl@0
|
71 |
strcpy((char*)uncompr, "garbage");
|
sl@0
|
72 |
|
sl@0
|
73 |
err = uncompress(uncompr, &uncomprLen, compr, comprLen);
|
sl@0
|
74 |
CHECK_ERR(err, "uncompress");
|
sl@0
|
75 |
|
sl@0
|
76 |
if (strcmp((char*)uncompr, hello)) {
|
sl@0
|
77 |
fprintf(stderr, "bad uncompress\n");
|
sl@0
|
78 |
exit(1);
|
sl@0
|
79 |
} else {
|
sl@0
|
80 |
printf("uncompress(): %s\n", (char *)uncompr);
|
sl@0
|
81 |
}
|
sl@0
|
82 |
}
|
sl@0
|
83 |
|
sl@0
|
84 |
/* ===========================================================================
|
sl@0
|
85 |
* Test read/write of .gz files
|
sl@0
|
86 |
*/
|
sl@0
|
87 |
void test_gzio(out, in, uncompr, uncomprLen)
|
sl@0
|
88 |
const char *out; /* compressed output file */
|
sl@0
|
89 |
const char *in; /* compressed input file */
|
sl@0
|
90 |
Byte *uncompr;
|
sl@0
|
91 |
int uncomprLen;
|
sl@0
|
92 |
{
|
sl@0
|
93 |
int err;
|
sl@0
|
94 |
int len = strlen(hello)+1;
|
sl@0
|
95 |
gzFile file;
|
sl@0
|
96 |
z_off_t pos;
|
sl@0
|
97 |
|
sl@0
|
98 |
file = gzopen(out, "wb");
|
sl@0
|
99 |
if (file == NULL) {
|
sl@0
|
100 |
fprintf(stderr, "gzopen error\n");
|
sl@0
|
101 |
exit(1);
|
sl@0
|
102 |
}
|
sl@0
|
103 |
gzputc(file, 'h');
|
sl@0
|
104 |
if (gzputs(file, "ello") != 4) {
|
sl@0
|
105 |
fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
|
sl@0
|
106 |
exit(1);
|
sl@0
|
107 |
}
|
sl@0
|
108 |
if (gzprintf(file, ", %s!", "hello") != 8) {
|
sl@0
|
109 |
fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
|
sl@0
|
110 |
exit(1);
|
sl@0
|
111 |
}
|
sl@0
|
112 |
gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
|
sl@0
|
113 |
gzclose(file);
|
sl@0
|
114 |
|
sl@0
|
115 |
file = gzopen(in, "rb");
|
sl@0
|
116 |
if (file == NULL) {
|
sl@0
|
117 |
fprintf(stderr, "gzopen error\n");
|
sl@0
|
118 |
}
|
sl@0
|
119 |
strcpy((char*)uncompr, "garbage");
|
sl@0
|
120 |
|
sl@0
|
121 |
uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
|
sl@0
|
122 |
if (uncomprLen != len) {
|
sl@0
|
123 |
fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
|
sl@0
|
124 |
exit(1);
|
sl@0
|
125 |
}
|
sl@0
|
126 |
if (strcmp((char*)uncompr, hello)) {
|
sl@0
|
127 |
fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
|
sl@0
|
128 |
exit(1);
|
sl@0
|
129 |
} else {
|
sl@0
|
130 |
printf("gzread(): %s\n", (char *)uncompr);
|
sl@0
|
131 |
}
|
sl@0
|
132 |
|
sl@0
|
133 |
pos = gzseek(file, -8L, SEEK_CUR);
|
sl@0
|
134 |
if (pos != 6 || gztell(file) != pos) {
|
sl@0
|
135 |
fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
|
sl@0
|
136 |
(long)pos, (long)gztell(file));
|
sl@0
|
137 |
exit(1);
|
sl@0
|
138 |
}
|
sl@0
|
139 |
|
sl@0
|
140 |
if (gzgetc(file) != ' ') {
|
sl@0
|
141 |
fprintf(stderr, "gzgetc error\n");
|
sl@0
|
142 |
exit(1);
|
sl@0
|
143 |
}
|
sl@0
|
144 |
|
sl@0
|
145 |
gzgets(file, (char*)uncompr, uncomprLen);
|
sl@0
|
146 |
uncomprLen = strlen((char*)uncompr);
|
sl@0
|
147 |
if (uncomprLen != 6) { /* "hello!" */
|
sl@0
|
148 |
fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
|
sl@0
|
149 |
exit(1);
|
sl@0
|
150 |
}
|
sl@0
|
151 |
if (strcmp((char*)uncompr, hello+7)) {
|
sl@0
|
152 |
fprintf(stderr, "bad gzgets after gzseek\n");
|
sl@0
|
153 |
exit(1);
|
sl@0
|
154 |
} else {
|
sl@0
|
155 |
printf("gzgets() after gzseek: %s\n", (char *)uncompr);
|
sl@0
|
156 |
}
|
sl@0
|
157 |
|
sl@0
|
158 |
gzclose(file);
|
sl@0
|
159 |
}
|
sl@0
|
160 |
|
sl@0
|
161 |
/* ===========================================================================
|
sl@0
|
162 |
* Test deflate() with small buffers
|
sl@0
|
163 |
*/
|
sl@0
|
164 |
void test_deflate(compr, comprLen)
|
sl@0
|
165 |
Byte *compr;
|
sl@0
|
166 |
uLong comprLen;
|
sl@0
|
167 |
{
|
sl@0
|
168 |
z_stream c_stream; /* compression stream */
|
sl@0
|
169 |
int err;
|
sl@0
|
170 |
int len = strlen(hello)+1;
|
sl@0
|
171 |
|
sl@0
|
172 |
c_stream.zalloc = (alloc_func)0;
|
sl@0
|
173 |
c_stream.zfree = (free_func)0;
|
sl@0
|
174 |
c_stream.opaque = (voidpf)0;
|
sl@0
|
175 |
|
sl@0
|
176 |
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
|
sl@0
|
177 |
CHECK_ERR(err, "deflateInit");
|
sl@0
|
178 |
|
sl@0
|
179 |
c_stream.next_in = (Bytef*)hello;
|
sl@0
|
180 |
c_stream.next_out = compr;
|
sl@0
|
181 |
|
sl@0
|
182 |
while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
|
sl@0
|
183 |
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
|
sl@0
|
184 |
err = deflate(&c_stream, Z_NO_FLUSH);
|
sl@0
|
185 |
CHECK_ERR(err, "deflate");
|
sl@0
|
186 |
}
|
sl@0
|
187 |
/* Finish the stream, still forcing small buffers: */
|
sl@0
|
188 |
for (;;) {
|
sl@0
|
189 |
c_stream.avail_out = 1;
|
sl@0
|
190 |
err = deflate(&c_stream, Z_FINISH);
|
sl@0
|
191 |
if (err == Z_STREAM_END) break;
|
sl@0
|
192 |
CHECK_ERR(err, "deflate");
|
sl@0
|
193 |
}
|
sl@0
|
194 |
|
sl@0
|
195 |
err = deflateEnd(&c_stream);
|
sl@0
|
196 |
CHECK_ERR(err, "deflateEnd");
|
sl@0
|
197 |
}
|
sl@0
|
198 |
|
sl@0
|
199 |
/* ===========================================================================
|
sl@0
|
200 |
* Test inflate() with small buffers
|
sl@0
|
201 |
*/
|
sl@0
|
202 |
void test_inflate(compr, comprLen, uncompr, uncomprLen)
|
sl@0
|
203 |
Byte *compr, *uncompr;
|
sl@0
|
204 |
uLong comprLen, uncomprLen;
|
sl@0
|
205 |
{
|
sl@0
|
206 |
int err;
|
sl@0
|
207 |
z_stream d_stream; /* decompression stream */
|
sl@0
|
208 |
|
sl@0
|
209 |
strcpy((char*)uncompr, "garbage");
|
sl@0
|
210 |
|
sl@0
|
211 |
d_stream.zalloc = (alloc_func)0;
|
sl@0
|
212 |
d_stream.zfree = (free_func)0;
|
sl@0
|
213 |
d_stream.opaque = (voidpf)0;
|
sl@0
|
214 |
|
sl@0
|
215 |
d_stream.next_in = compr;
|
sl@0
|
216 |
d_stream.avail_in = 0;
|
sl@0
|
217 |
d_stream.next_out = uncompr;
|
sl@0
|
218 |
|
sl@0
|
219 |
err = inflateInit(&d_stream);
|
sl@0
|
220 |
CHECK_ERR(err, "inflateInit");
|
sl@0
|
221 |
|
sl@0
|
222 |
while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
|
sl@0
|
223 |
d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
|
sl@0
|
224 |
err = inflate(&d_stream, Z_NO_FLUSH);
|
sl@0
|
225 |
if (err == Z_STREAM_END) break;
|
sl@0
|
226 |
CHECK_ERR(err, "inflate");
|
sl@0
|
227 |
}
|
sl@0
|
228 |
|
sl@0
|
229 |
err = inflateEnd(&d_stream);
|
sl@0
|
230 |
CHECK_ERR(err, "inflateEnd");
|
sl@0
|
231 |
|
sl@0
|
232 |
if (strcmp((char*)uncompr, hello)) {
|
sl@0
|
233 |
fprintf(stderr, "bad inflate\n");
|
sl@0
|
234 |
exit(1);
|
sl@0
|
235 |
} else {
|
sl@0
|
236 |
printf("inflate(): %s\n", (char *)uncompr);
|
sl@0
|
237 |
}
|
sl@0
|
238 |
}
|
sl@0
|
239 |
|
sl@0
|
240 |
/* ===========================================================================
|
sl@0
|
241 |
* Test deflate() with large buffers and dynamic change of compression level
|
sl@0
|
242 |
*/
|
sl@0
|
243 |
void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
|
sl@0
|
244 |
Byte *compr, *uncompr;
|
sl@0
|
245 |
uLong comprLen, uncomprLen;
|
sl@0
|
246 |
{
|
sl@0
|
247 |
z_stream c_stream; /* compression stream */
|
sl@0
|
248 |
int err;
|
sl@0
|
249 |
|
sl@0
|
250 |
c_stream.zalloc = (alloc_func)0;
|
sl@0
|
251 |
c_stream.zfree = (free_func)0;
|
sl@0
|
252 |
c_stream.opaque = (voidpf)0;
|
sl@0
|
253 |
|
sl@0
|
254 |
err = deflateInit(&c_stream, Z_BEST_SPEED);
|
sl@0
|
255 |
CHECK_ERR(err, "deflateInit");
|
sl@0
|
256 |
|
sl@0
|
257 |
c_stream.next_out = compr;
|
sl@0
|
258 |
c_stream.avail_out = (uInt)comprLen;
|
sl@0
|
259 |
|
sl@0
|
260 |
/* At this point, uncompr is still mostly zeroes, so it should compress
|
sl@0
|
261 |
* very well:
|
sl@0
|
262 |
*/
|
sl@0
|
263 |
c_stream.next_in = uncompr;
|
sl@0
|
264 |
c_stream.avail_in = (uInt)uncomprLen;
|
sl@0
|
265 |
err = deflate(&c_stream, Z_NO_FLUSH);
|
sl@0
|
266 |
CHECK_ERR(err, "deflate");
|
sl@0
|
267 |
if (c_stream.avail_in != 0) {
|
sl@0
|
268 |
fprintf(stderr, "deflate not greedy\n");
|
sl@0
|
269 |
exit(1);
|
sl@0
|
270 |
}
|
sl@0
|
271 |
|
sl@0
|
272 |
/* Feed in already compressed data and switch to no compression: */
|
sl@0
|
273 |
deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
|
sl@0
|
274 |
c_stream.next_in = compr;
|
sl@0
|
275 |
c_stream.avail_in = (uInt)comprLen/2;
|
sl@0
|
276 |
err = deflate(&c_stream, Z_NO_FLUSH);
|
sl@0
|
277 |
CHECK_ERR(err, "deflate");
|
sl@0
|
278 |
|
sl@0
|
279 |
/* Switch back to compressing mode: */
|
sl@0
|
280 |
deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
|
sl@0
|
281 |
c_stream.next_in = uncompr;
|
sl@0
|
282 |
c_stream.avail_in = (uInt)uncomprLen;
|
sl@0
|
283 |
err = deflate(&c_stream, Z_NO_FLUSH);
|
sl@0
|
284 |
CHECK_ERR(err, "deflate");
|
sl@0
|
285 |
|
sl@0
|
286 |
err = deflate(&c_stream, Z_FINISH);
|
sl@0
|
287 |
if (err != Z_STREAM_END) {
|
sl@0
|
288 |
fprintf(stderr, "deflate should report Z_STREAM_END\n");
|
sl@0
|
289 |
exit(1);
|
sl@0
|
290 |
}
|
sl@0
|
291 |
err = deflateEnd(&c_stream);
|
sl@0
|
292 |
CHECK_ERR(err, "deflateEnd");
|
sl@0
|
293 |
}
|
sl@0
|
294 |
|
sl@0
|
295 |
/* ===========================================================================
|
sl@0
|
296 |
* Test inflate() with large buffers
|
sl@0
|
297 |
*/
|
sl@0
|
298 |
void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
|
sl@0
|
299 |
Byte *compr, *uncompr;
|
sl@0
|
300 |
uLong comprLen, uncomprLen;
|
sl@0
|
301 |
{
|
sl@0
|
302 |
int err;
|
sl@0
|
303 |
z_stream d_stream; /* decompression stream */
|
sl@0
|
304 |
|
sl@0
|
305 |
strcpy((char*)uncompr, "garbage");
|
sl@0
|
306 |
|
sl@0
|
307 |
d_stream.zalloc = (alloc_func)0;
|
sl@0
|
308 |
d_stream.zfree = (free_func)0;
|
sl@0
|
309 |
d_stream.opaque = (voidpf)0;
|
sl@0
|
310 |
|
sl@0
|
311 |
d_stream.next_in = compr;
|
sl@0
|
312 |
d_stream.avail_in = (uInt)comprLen;
|
sl@0
|
313 |
|
sl@0
|
314 |
err = inflateInit(&d_stream);
|
sl@0
|
315 |
CHECK_ERR(err, "inflateInit");
|
sl@0
|
316 |
|
sl@0
|
317 |
for (;;) {
|
sl@0
|
318 |
d_stream.next_out = uncompr; /* discard the output */
|
sl@0
|
319 |
d_stream.avail_out = (uInt)uncomprLen;
|
sl@0
|
320 |
err = inflate(&d_stream, Z_NO_FLUSH);
|
sl@0
|
321 |
if (err == Z_STREAM_END) break;
|
sl@0
|
322 |
CHECK_ERR(err, "large inflate");
|
sl@0
|
323 |
}
|
sl@0
|
324 |
|
sl@0
|
325 |
err = inflateEnd(&d_stream);
|
sl@0
|
326 |
CHECK_ERR(err, "inflateEnd");
|
sl@0
|
327 |
|
sl@0
|
328 |
if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
|
sl@0
|
329 |
fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
|
sl@0
|
330 |
exit(1);
|
sl@0
|
331 |
} else {
|
sl@0
|
332 |
printf("large_inflate(): OK\n");
|
sl@0
|
333 |
}
|
sl@0
|
334 |
}
|
sl@0
|
335 |
|
sl@0
|
336 |
/* ===========================================================================
|
sl@0
|
337 |
* Test deflate() with full flush
|
sl@0
|
338 |
*/
|
sl@0
|
339 |
void test_flush(compr, comprLen)
|
sl@0
|
340 |
Byte *compr;
|
sl@0
|
341 |
uLong *comprLen;
|
sl@0
|
342 |
{
|
sl@0
|
343 |
z_stream c_stream; /* compression stream */
|
sl@0
|
344 |
int err;
|
sl@0
|
345 |
int len = strlen(hello)+1;
|
sl@0
|
346 |
|
sl@0
|
347 |
c_stream.zalloc = (alloc_func)0;
|
sl@0
|
348 |
c_stream.zfree = (free_func)0;
|
sl@0
|
349 |
c_stream.opaque = (voidpf)0;
|
sl@0
|
350 |
|
sl@0
|
351 |
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
|
sl@0
|
352 |
CHECK_ERR(err, "deflateInit");
|
sl@0
|
353 |
|
sl@0
|
354 |
c_stream.next_in = (Bytef*)hello;
|
sl@0
|
355 |
c_stream.next_out = compr;
|
sl@0
|
356 |
c_stream.avail_in = 3;
|
sl@0
|
357 |
c_stream.avail_out = (uInt)*comprLen;
|
sl@0
|
358 |
err = deflate(&c_stream, Z_FULL_FLUSH);
|
sl@0
|
359 |
CHECK_ERR(err, "deflate");
|
sl@0
|
360 |
|
sl@0
|
361 |
compr[3]++; /* force an error in first compressed block */
|
sl@0
|
362 |
c_stream.avail_in = len - 3;
|
sl@0
|
363 |
|
sl@0
|
364 |
err = deflate(&c_stream, Z_FINISH);
|
sl@0
|
365 |
if (err != Z_STREAM_END) {
|
sl@0
|
366 |
CHECK_ERR(err, "deflate");
|
sl@0
|
367 |
}
|
sl@0
|
368 |
err = deflateEnd(&c_stream);
|
sl@0
|
369 |
CHECK_ERR(err, "deflateEnd");
|
sl@0
|
370 |
|
sl@0
|
371 |
*comprLen = c_stream.total_out;
|
sl@0
|
372 |
}
|
sl@0
|
373 |
|
sl@0
|
374 |
/* ===========================================================================
|
sl@0
|
375 |
* Test inflateSync()
|
sl@0
|
376 |
*/
|
sl@0
|
377 |
void test_sync(compr, comprLen, uncompr, uncomprLen)
|
sl@0
|
378 |
Byte *compr, *uncompr;
|
sl@0
|
379 |
uLong comprLen, uncomprLen;
|
sl@0
|
380 |
{
|
sl@0
|
381 |
int err;
|
sl@0
|
382 |
z_stream d_stream; /* decompression stream */
|
sl@0
|
383 |
|
sl@0
|
384 |
strcpy((char*)uncompr, "garbage");
|
sl@0
|
385 |
|
sl@0
|
386 |
d_stream.zalloc = (alloc_func)0;
|
sl@0
|
387 |
d_stream.zfree = (free_func)0;
|
sl@0
|
388 |
d_stream.opaque = (voidpf)0;
|
sl@0
|
389 |
|
sl@0
|
390 |
d_stream.next_in = compr;
|
sl@0
|
391 |
d_stream.avail_in = 2; /* just read the zlib header */
|
sl@0
|
392 |
|
sl@0
|
393 |
err = inflateInit(&d_stream);
|
sl@0
|
394 |
CHECK_ERR(err, "inflateInit");
|
sl@0
|
395 |
|
sl@0
|
396 |
d_stream.next_out = uncompr;
|
sl@0
|
397 |
d_stream.avail_out = (uInt)uncomprLen;
|
sl@0
|
398 |
|
sl@0
|
399 |
inflate(&d_stream, Z_NO_FLUSH);
|
sl@0
|
400 |
CHECK_ERR(err, "inflate");
|
sl@0
|
401 |
|
sl@0
|
402 |
d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
|
sl@0
|
403 |
err = inflateSync(&d_stream); /* but skip the damaged part */
|
sl@0
|
404 |
CHECK_ERR(err, "inflateSync");
|
sl@0
|
405 |
|
sl@0
|
406 |
err = inflate(&d_stream, Z_FINISH);
|
sl@0
|
407 |
if (err != Z_DATA_ERROR) {
|
sl@0
|
408 |
fprintf(stderr, "inflate should report DATA_ERROR\n");
|
sl@0
|
409 |
/* Because of incorrect adler32 */
|
sl@0
|
410 |
exit(1);
|
sl@0
|
411 |
}
|
sl@0
|
412 |
err = inflateEnd(&d_stream);
|
sl@0
|
413 |
CHECK_ERR(err, "inflateEnd");
|
sl@0
|
414 |
|
sl@0
|
415 |
printf("after inflateSync(): hel%s\n", (char *)uncompr);
|
sl@0
|
416 |
}
|
sl@0
|
417 |
|
sl@0
|
418 |
/* ===========================================================================
|
sl@0
|
419 |
* Test deflate() with preset dictionary
|
sl@0
|
420 |
*/
|
sl@0
|
421 |
void test_dict_deflate(compr, comprLen)
|
sl@0
|
422 |
Byte *compr;
|
sl@0
|
423 |
uLong comprLen;
|
sl@0
|
424 |
{
|
sl@0
|
425 |
z_stream c_stream; /* compression stream */
|
sl@0
|
426 |
int err;
|
sl@0
|
427 |
|
sl@0
|
428 |
c_stream.zalloc = (alloc_func)0;
|
sl@0
|
429 |
c_stream.zfree = (free_func)0;
|
sl@0
|
430 |
c_stream.opaque = (voidpf)0;
|
sl@0
|
431 |
|
sl@0
|
432 |
err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
|
sl@0
|
433 |
CHECK_ERR(err, "deflateInit");
|
sl@0
|
434 |
|
sl@0
|
435 |
err = deflateSetDictionary(&c_stream,
|
sl@0
|
436 |
(const Bytef*)dictionary, sizeof(dictionary));
|
sl@0
|
437 |
CHECK_ERR(err, "deflateSetDictionary");
|
sl@0
|
438 |
|
sl@0
|
439 |
dictId = c_stream.adler;
|
sl@0
|
440 |
c_stream.next_out = compr;
|
sl@0
|
441 |
c_stream.avail_out = (uInt)comprLen;
|
sl@0
|
442 |
|
sl@0
|
443 |
c_stream.next_in = (Bytef*)hello;
|
sl@0
|
444 |
c_stream.avail_in = (uInt)strlen(hello)+1;
|
sl@0
|
445 |
|
sl@0
|
446 |
err = deflate(&c_stream, Z_FINISH);
|
sl@0
|
447 |
if (err != Z_STREAM_END) {
|
sl@0
|
448 |
fprintf(stderr, "deflate should report Z_STREAM_END\n");
|
sl@0
|
449 |
exit(1);
|
sl@0
|
450 |
}
|
sl@0
|
451 |
err = deflateEnd(&c_stream);
|
sl@0
|
452 |
CHECK_ERR(err, "deflateEnd");
|
sl@0
|
453 |
}
|
sl@0
|
454 |
|
sl@0
|
455 |
/* ===========================================================================
|
sl@0
|
456 |
* Test inflate() with a preset dictionary
|
sl@0
|
457 |
*/
|
sl@0
|
458 |
void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
|
sl@0
|
459 |
Byte *compr, *uncompr;
|
sl@0
|
460 |
uLong comprLen, uncomprLen;
|
sl@0
|
461 |
{
|
sl@0
|
462 |
int err;
|
sl@0
|
463 |
z_stream d_stream; /* decompression stream */
|
sl@0
|
464 |
|
sl@0
|
465 |
strcpy((char*)uncompr, "garbage");
|
sl@0
|
466 |
|
sl@0
|
467 |
d_stream.zalloc = (alloc_func)0;
|
sl@0
|
468 |
d_stream.zfree = (free_func)0;
|
sl@0
|
469 |
d_stream.opaque = (voidpf)0;
|
sl@0
|
470 |
|
sl@0
|
471 |
d_stream.next_in = compr;
|
sl@0
|
472 |
d_stream.avail_in = (uInt)comprLen;
|
sl@0
|
473 |
|
sl@0
|
474 |
err = inflateInit(&d_stream);
|
sl@0
|
475 |
CHECK_ERR(err, "inflateInit");
|
sl@0
|
476 |
|
sl@0
|
477 |
d_stream.next_out = uncompr;
|
sl@0
|
478 |
d_stream.avail_out = (uInt)uncomprLen;
|
sl@0
|
479 |
|
sl@0
|
480 |
for (;;) {
|
sl@0
|
481 |
err = inflate(&d_stream, Z_NO_FLUSH);
|
sl@0
|
482 |
if (err == Z_STREAM_END) break;
|
sl@0
|
483 |
if (err == Z_NEED_DICT) {
|
sl@0
|
484 |
if (d_stream.adler != dictId) {
|
sl@0
|
485 |
fprintf(stderr, "unexpected dictionary");
|
sl@0
|
486 |
exit(1);
|
sl@0
|
487 |
}
|
sl@0
|
488 |
err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
|
sl@0
|
489 |
sizeof(dictionary));
|
sl@0
|
490 |
}
|
sl@0
|
491 |
CHECK_ERR(err, "inflate with dict");
|
sl@0
|
492 |
}
|
sl@0
|
493 |
|
sl@0
|
494 |
err = inflateEnd(&d_stream);
|
sl@0
|
495 |
CHECK_ERR(err, "inflateEnd");
|
sl@0
|
496 |
|
sl@0
|
497 |
if (strcmp((char*)uncompr, hello)) {
|
sl@0
|
498 |
fprintf(stderr, "bad inflate with dict\n");
|
sl@0
|
499 |
exit(1);
|
sl@0
|
500 |
} else {
|
sl@0
|
501 |
printf("inflate with dictionary: %s\n", (char *)uncompr);
|
sl@0
|
502 |
}
|
sl@0
|
503 |
}
|
sl@0
|
504 |
|
sl@0
|
505 |
/* ===========================================================================
|
sl@0
|
506 |
* Usage: example [output.gz [input.gz]]
|
sl@0
|
507 |
*/
|
sl@0
|
508 |
|
sl@0
|
509 |
int main(argc, argv)
|
sl@0
|
510 |
int argc;
|
sl@0
|
511 |
char *argv[];
|
sl@0
|
512 |
{
|
sl@0
|
513 |
Byte *compr, *uncompr;
|
sl@0
|
514 |
uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
|
sl@0
|
515 |
uLong uncomprLen = comprLen;
|
sl@0
|
516 |
static const char* myVersion = ZLIB_VERSION;
|
sl@0
|
517 |
|
sl@0
|
518 |
if (zlibVersion()[0] != myVersion[0]) {
|
sl@0
|
519 |
fprintf(stderr, "incompatible zlib version\n");
|
sl@0
|
520 |
exit(1);
|
sl@0
|
521 |
|
sl@0
|
522 |
} else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
|
sl@0
|
523 |
fprintf(stderr, "warning: different zlib version\n");
|
sl@0
|
524 |
}
|
sl@0
|
525 |
|
sl@0
|
526 |
compr = (Byte*)calloc((uInt)comprLen, 1);
|
sl@0
|
527 |
uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
|
sl@0
|
528 |
/* compr and uncompr are cleared to avoid reading uninitialized
|
sl@0
|
529 |
* data and to ensure that uncompr compresses well.
|
sl@0
|
530 |
*/
|
sl@0
|
531 |
if (compr == Z_NULL || uncompr == Z_NULL) {
|
sl@0
|
532 |
printf("out of memory\n");
|
sl@0
|
533 |
exit(1);
|
sl@0
|
534 |
}
|
sl@0
|
535 |
test_compress(compr, comprLen, uncompr, uncomprLen);
|
sl@0
|
536 |
|
sl@0
|
537 |
test_gzio((argc > 1 ? argv[1] : TESTFILE),
|
sl@0
|
538 |
(argc > 2 ? argv[2] : TESTFILE),
|
sl@0
|
539 |
uncompr, (int)uncomprLen);
|
sl@0
|
540 |
|
sl@0
|
541 |
test_deflate(compr, comprLen);
|
sl@0
|
542 |
test_inflate(compr, comprLen, uncompr, uncomprLen);
|
sl@0
|
543 |
|
sl@0
|
544 |
test_large_deflate(compr, comprLen, uncompr, uncomprLen);
|
sl@0
|
545 |
test_large_inflate(compr, comprLen, uncompr, uncomprLen);
|
sl@0
|
546 |
|
sl@0
|
547 |
test_flush(compr, &comprLen);
|
sl@0
|
548 |
test_sync(compr, comprLen, uncompr, uncomprLen);
|
sl@0
|
549 |
comprLen = uncomprLen;
|
sl@0
|
550 |
|
sl@0
|
551 |
test_dict_deflate(compr, comprLen);
|
sl@0
|
552 |
test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
|
sl@0
|
553 |
|
sl@0
|
554 |
exit(0);
|
sl@0
|
555 |
return 0; /* to avoid warning */
|
sl@0
|
556 |
}
|