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