]>
git.saurik.com Git - wxWidgets.git/blob - src/zlib/example.c
1 /* example.c -- usage example of the zlib compression library
2 * Copyright (C) 1995-2004 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
15 #if defined(VMS) || defined(RISCOS)
16 # define TESTFILE "foo-gz"
18 # define TESTFILE "foo.gz"
21 #define CHECK_ERR(err, msg) { \
23 fprintf(stderr, "%s error: %d\n", msg, err); \
28 const char hello
[] = "hello, hello!";
29 /* "hello world" would be more standard, but the repeated "hello"
30 * stresses the compression code better, sorry...
33 const char dictionary
[] = "hello";
34 uLong dictId
; /* Adler32 value of the dictionary */
36 void test_compress
OF((Byte
*compr
, uLong comprLen
,
37 Byte
*uncompr
, uLong uncomprLen
));
38 void test_gzio
OF((const char *fname
,
39 Byte
*uncompr
, uLong uncomprLen
));
40 void test_deflate
OF((Byte
*compr
, uLong comprLen
));
41 void test_inflate
OF((Byte
*compr
, uLong comprLen
,
42 Byte
*uncompr
, uLong uncomprLen
));
43 void test_large_deflate
OF((Byte
*compr
, uLong comprLen
,
44 Byte
*uncompr
, uLong uncomprLen
));
45 void test_large_inflate
OF((Byte
*compr
, uLong comprLen
,
46 Byte
*uncompr
, uLong uncomprLen
));
47 void test_flush
OF((Byte
*compr
, uLong
*comprLen
));
48 void test_sync
OF((Byte
*compr
, uLong comprLen
,
49 Byte
*uncompr
, uLong uncomprLen
));
50 void test_dict_deflate
OF((Byte
*compr
, uLong comprLen
));
51 void test_dict_inflate
OF((Byte
*compr
, uLong comprLen
,
52 Byte
*uncompr
, uLong uncomprLen
));
53 int main
OF((int argc
, char *argv
[]));
55 /* ===========================================================================
56 * Test compress() and uncompress()
58 void test_compress(compr
, comprLen
, uncompr
, uncomprLen
)
59 Byte
*compr
, *uncompr
;
60 uLong comprLen
, uncomprLen
;
63 uLong len
= (uLong
)strlen(hello
)+1;
65 err
= compress(compr
, &comprLen
, (const Bytef
*)hello
, len
);
66 CHECK_ERR(err
, "compress");
68 strcpy((char*)uncompr
, "garbage");
70 err
= uncompress(uncompr
, &uncomprLen
, compr
, comprLen
);
71 CHECK_ERR(err
, "uncompress");
73 if (strcmp((char*)uncompr
, hello
)) {
74 fprintf(stderr
, "bad uncompress\n");
77 printf("uncompress(): %s\n", (char *)uncompr
);
81 /* ===========================================================================
82 * Test read/write of .gz files
84 void test_gzio(fname
, uncompr
, uncomprLen
)
85 const char *fname
; /* compressed file name */
90 fprintf(stderr
, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
93 int len
= (int)strlen(hello
)+1;
97 file
= gzopen(fname
, "wb");
99 fprintf(stderr
, "gzopen error\n");
103 if (gzputs(file
, "ello") != 4) {
104 fprintf(stderr
, "gzputs err: %s\n", gzerror(file
, &err
));
107 if (gzprintf(file
, ", %s!", "hello") != 8) {
108 fprintf(stderr
, "gzprintf err: %s\n", gzerror(file
, &err
));
111 gzseek(file
, 1L, SEEK_CUR
); /* add one zero byte */
114 file
= gzopen(fname
, "rb");
116 fprintf(stderr
, "gzopen error\n");
119 strcpy((char*)uncompr
, "garbage");
121 if (gzread(file
, uncompr
, (unsigned)uncomprLen
) != len
) {
122 fprintf(stderr
, "gzread err: %s\n", gzerror(file
, &err
));
125 if (strcmp((char*)uncompr
, hello
)) {
126 fprintf(stderr
, "bad gzread: %s\n", (char*)uncompr
);
129 printf("gzread(): %s\n", (char*)uncompr
);
132 pos
= gzseek(file
, -8L, SEEK_CUR
);
133 if (pos
!= 6 || gztell(file
) != pos
) {
134 fprintf(stderr
, "gzseek error, pos=%ld, gztell=%ld\n",
135 (long)pos
, (long)gztell(file
));
139 if (gzgetc(file
) != ' ') {
140 fprintf(stderr
, "gzgetc error\n");
144 if (gzungetc(' ', file
) != ' ') {
145 fprintf(stderr
, "gzungetc error\n");
149 gzgets(file
, (char*)uncompr
, (int)uncomprLen
);
150 if (strlen((char*)uncompr
) != 7) { /* " hello!" */
151 fprintf(stderr
, "gzgets err after gzseek: %s\n", gzerror(file
, &err
));
154 if (strcmp((char*)uncompr
, hello
+ 6)) {
155 fprintf(stderr
, "bad gzgets after gzseek\n");
158 printf("gzgets() after gzseek: %s\n", (char*)uncompr
);
165 /* ===========================================================================
166 * Test deflate() with small buffers
168 void test_deflate(compr
, comprLen
)
172 z_stream c_stream
; /* compression stream */
174 uLong len
= (uLong
)strlen(hello
)+1;
176 c_stream
.zalloc
= (alloc_func
)0;
177 c_stream
.zfree
= (free_func
)0;
178 c_stream
.opaque
= (voidpf
)0;
180 err
= deflateInit(&c_stream
, Z_DEFAULT_COMPRESSION
);
181 CHECK_ERR(err
, "deflateInit");
183 c_stream
.next_in
= (Bytef
*)hello
;
184 c_stream
.next_out
= compr
;
186 while (c_stream
.total_in
!= len
&& c_stream
.total_out
< comprLen
) {
187 c_stream
.avail_in
= c_stream
.avail_out
= 1; /* force small buffers */
188 err
= deflate(&c_stream
, Z_NO_FLUSH
);
189 CHECK_ERR(err
, "deflate");
191 /* Finish the stream, still forcing small buffers: */
193 c_stream
.avail_out
= 1;
194 err
= deflate(&c_stream
, Z_FINISH
);
195 if (err
== Z_STREAM_END
) break;
196 CHECK_ERR(err
, "deflate");
199 err
= deflateEnd(&c_stream
);
200 CHECK_ERR(err
, "deflateEnd");
203 /* ===========================================================================
204 * Test inflate() with small buffers
206 void test_inflate(compr
, comprLen
, uncompr
, uncomprLen
)
207 Byte
*compr
, *uncompr
;
208 uLong comprLen
, uncomprLen
;
211 z_stream d_stream
; /* decompression stream */
213 strcpy((char*)uncompr
, "garbage");
215 d_stream
.zalloc
= (alloc_func
)0;
216 d_stream
.zfree
= (free_func
)0;
217 d_stream
.opaque
= (voidpf
)0;
219 d_stream
.next_in
= compr
;
220 d_stream
.avail_in
= 0;
221 d_stream
.next_out
= uncompr
;
223 err
= inflateInit(&d_stream
);
224 CHECK_ERR(err
, "inflateInit");
226 while (d_stream
.total_out
< uncomprLen
&& d_stream
.total_in
< comprLen
) {
227 d_stream
.avail_in
= d_stream
.avail_out
= 1; /* force small buffers */
228 err
= inflate(&d_stream
, Z_NO_FLUSH
);
229 if (err
== Z_STREAM_END
) break;
230 CHECK_ERR(err
, "inflate");
233 err
= inflateEnd(&d_stream
);
234 CHECK_ERR(err
, "inflateEnd");
236 if (strcmp((char*)uncompr
, hello
)) {
237 fprintf(stderr
, "bad inflate\n");
240 printf("inflate(): %s\n", (char *)uncompr
);
244 /* ===========================================================================
245 * Test deflate() with large buffers and dynamic change of compression level
247 void test_large_deflate(compr
, comprLen
, uncompr
, uncomprLen
)
248 Byte
*compr
, *uncompr
;
249 uLong comprLen
, uncomprLen
;
251 z_stream c_stream
; /* compression stream */
254 c_stream
.zalloc
= (alloc_func
)0;
255 c_stream
.zfree
= (free_func
)0;
256 c_stream
.opaque
= (voidpf
)0;
258 err
= deflateInit(&c_stream
, Z_BEST_SPEED
);
259 CHECK_ERR(err
, "deflateInit");
261 c_stream
.next_out
= compr
;
262 c_stream
.avail_out
= (uInt
)comprLen
;
264 /* At this point, uncompr is still mostly zeroes, so it should compress
267 c_stream
.next_in
= uncompr
;
268 c_stream
.avail_in
= (uInt
)uncomprLen
;
269 err
= deflate(&c_stream
, Z_NO_FLUSH
);
270 CHECK_ERR(err
, "deflate");
271 if (c_stream
.avail_in
!= 0) {
272 fprintf(stderr
, "deflate not greedy\n");
276 /* Feed in already compressed data and switch to no compression: */
277 deflateParams(&c_stream
, Z_NO_COMPRESSION
, Z_DEFAULT_STRATEGY
);
278 c_stream
.next_in
= compr
;
279 c_stream
.avail_in
= (uInt
)comprLen
/2;
280 err
= deflate(&c_stream
, Z_NO_FLUSH
);
281 CHECK_ERR(err
, "deflate");
283 /* Switch back to compressing mode: */
284 deflateParams(&c_stream
, Z_BEST_COMPRESSION
, Z_FILTERED
);
285 c_stream
.next_in
= uncompr
;
286 c_stream
.avail_in
= (uInt
)uncomprLen
;
287 err
= deflate(&c_stream
, Z_NO_FLUSH
);
288 CHECK_ERR(err
, "deflate");
290 err
= deflate(&c_stream
, Z_FINISH
);
291 if (err
!= Z_STREAM_END
) {
292 fprintf(stderr
, "deflate should report Z_STREAM_END\n");
295 err
= deflateEnd(&c_stream
);
296 CHECK_ERR(err
, "deflateEnd");
299 /* ===========================================================================
300 * Test inflate() with large buffers
302 void test_large_inflate(compr
, comprLen
, uncompr
, uncomprLen
)
303 Byte
*compr
, *uncompr
;
304 uLong comprLen
, uncomprLen
;
307 z_stream d_stream
; /* decompression stream */
309 strcpy((char*)uncompr
, "garbage");
311 d_stream
.zalloc
= (alloc_func
)0;
312 d_stream
.zfree
= (free_func
)0;
313 d_stream
.opaque
= (voidpf
)0;
315 d_stream
.next_in
= compr
;
316 d_stream
.avail_in
= (uInt
)comprLen
;
318 err
= inflateInit(&d_stream
);
319 CHECK_ERR(err
, "inflateInit");
322 d_stream
.next_out
= uncompr
; /* discard the output */
323 d_stream
.avail_out
= (uInt
)uncomprLen
;
324 err
= inflate(&d_stream
, Z_NO_FLUSH
);
325 if (err
== Z_STREAM_END
) break;
326 CHECK_ERR(err
, "large inflate");
329 err
= inflateEnd(&d_stream
);
330 CHECK_ERR(err
, "inflateEnd");
332 if (d_stream
.total_out
!= 2*uncomprLen
+ comprLen
/2) {
333 fprintf(stderr
, "bad large inflate: %ld\n", d_stream
.total_out
);
336 printf("large_inflate(): OK\n");
340 /* ===========================================================================
341 * Test deflate() with full flush
343 void test_flush(compr
, comprLen
)
347 z_stream c_stream
; /* compression stream */
349 uInt len
= (uInt
)strlen(hello
)+1;
351 c_stream
.zalloc
= (alloc_func
)0;
352 c_stream
.zfree
= (free_func
)0;
353 c_stream
.opaque
= (voidpf
)0;
355 err
= deflateInit(&c_stream
, Z_DEFAULT_COMPRESSION
);
356 CHECK_ERR(err
, "deflateInit");
358 c_stream
.next_in
= (Bytef
*)hello
;
359 c_stream
.next_out
= compr
;
360 c_stream
.avail_in
= 3;
361 c_stream
.avail_out
= (uInt
)*comprLen
;
362 err
= deflate(&c_stream
, Z_FULL_FLUSH
);
363 CHECK_ERR(err
, "deflate");
365 compr
[3]++; /* force an error in first compressed block */
366 c_stream
.avail_in
= len
- 3;
368 err
= deflate(&c_stream
, Z_FINISH
);
369 if (err
!= Z_STREAM_END
) {
370 CHECK_ERR(err
, "deflate");
372 err
= deflateEnd(&c_stream
);
373 CHECK_ERR(err
, "deflateEnd");
375 *comprLen
= c_stream
.total_out
;
378 /* ===========================================================================
381 void test_sync(compr
, comprLen
, uncompr
, uncomprLen
)
382 Byte
*compr
, *uncompr
;
383 uLong comprLen
, uncomprLen
;
386 z_stream d_stream
; /* decompression stream */
388 strcpy((char*)uncompr
, "garbage");
390 d_stream
.zalloc
= (alloc_func
)0;
391 d_stream
.zfree
= (free_func
)0;
392 d_stream
.opaque
= (voidpf
)0;
394 d_stream
.next_in
= compr
;
395 d_stream
.avail_in
= 2; /* just read the zlib header */
397 err
= inflateInit(&d_stream
);
398 CHECK_ERR(err
, "inflateInit");
400 d_stream
.next_out
= uncompr
;
401 d_stream
.avail_out
= (uInt
)uncomprLen
;
403 inflate(&d_stream
, Z_NO_FLUSH
);
404 CHECK_ERR(err
, "inflate");
406 d_stream
.avail_in
= (uInt
)comprLen
-2; /* read all compressed data */
407 err
= inflateSync(&d_stream
); /* but skip the damaged part */
408 CHECK_ERR(err
, "inflateSync");
410 err
= inflate(&d_stream
, Z_FINISH
);
411 if (err
!= Z_DATA_ERROR
) {
412 fprintf(stderr
, "inflate should report DATA_ERROR\n");
413 /* Because of incorrect adler32 */
416 err
= inflateEnd(&d_stream
);
417 CHECK_ERR(err
, "inflateEnd");
419 printf("after inflateSync(): hel%s\n", (char *)uncompr
);
422 /* ===========================================================================
423 * Test deflate() with preset dictionary
425 void test_dict_deflate(compr
, comprLen
)
429 z_stream c_stream
; /* compression stream */
432 c_stream
.zalloc
= (alloc_func
)0;
433 c_stream
.zfree
= (free_func
)0;
434 c_stream
.opaque
= (voidpf
)0;
436 err
= deflateInit(&c_stream
, Z_BEST_COMPRESSION
);
437 CHECK_ERR(err
, "deflateInit");
439 err
= deflateSetDictionary(&c_stream
,
440 (const Bytef
*)dictionary
, sizeof(dictionary
));
441 CHECK_ERR(err
, "deflateSetDictionary");
443 dictId
= c_stream
.adler
;
444 c_stream
.next_out
= compr
;
445 c_stream
.avail_out
= (uInt
)comprLen
;
447 c_stream
.next_in
= (Bytef
*)hello
;
448 c_stream
.avail_in
= (uInt
)strlen(hello
)+1;
450 err
= deflate(&c_stream
, Z_FINISH
);
451 if (err
!= Z_STREAM_END
) {
452 fprintf(stderr
, "deflate should report Z_STREAM_END\n");
455 err
= deflateEnd(&c_stream
);
456 CHECK_ERR(err
, "deflateEnd");
459 /* ===========================================================================
460 * Test inflate() with a preset dictionary
462 void test_dict_inflate(compr
, comprLen
, uncompr
, uncomprLen
)
463 Byte
*compr
, *uncompr
;
464 uLong comprLen
, uncomprLen
;
467 z_stream d_stream
; /* decompression stream */
469 strcpy((char*)uncompr
, "garbage");
471 d_stream
.zalloc
= (alloc_func
)0;
472 d_stream
.zfree
= (free_func
)0;
473 d_stream
.opaque
= (voidpf
)0;
475 d_stream
.next_in
= compr
;
476 d_stream
.avail_in
= (uInt
)comprLen
;
478 err
= inflateInit(&d_stream
);
479 CHECK_ERR(err
, "inflateInit");
481 d_stream
.next_out
= uncompr
;
482 d_stream
.avail_out
= (uInt
)uncomprLen
;
485 err
= inflate(&d_stream
, Z_NO_FLUSH
);
486 if (err
== Z_STREAM_END
) break;
487 if (err
== Z_NEED_DICT
) {
488 if (d_stream
.adler
!= dictId
) {
489 fprintf(stderr
, "unexpected dictionary");
492 err
= inflateSetDictionary(&d_stream
, (const Bytef
*)dictionary
,
495 CHECK_ERR(err
, "inflate with dict");
498 err
= inflateEnd(&d_stream
);
499 CHECK_ERR(err
, "inflateEnd");
501 if (strcmp((char*)uncompr
, hello
)) {
502 fprintf(stderr
, "bad inflate with dict\n");
505 printf("inflate with dictionary: %s\n", (char *)uncompr
);
509 /* ===========================================================================
510 * Usage: example [output.gz [input.gz]]
517 Byte
*compr
, *uncompr
;
518 uLong comprLen
= 10000*sizeof(int); /* don't overflow on MSDOS */
519 uLong uncomprLen
= comprLen
;
520 static const char* myVersion
= ZLIB_VERSION
;
522 if (zlibVersion()[0] != myVersion
[0]) {
523 fprintf(stderr
, "incompatible zlib version\n");
526 } else if (strcmp(zlibVersion(), ZLIB_VERSION
) != 0) {
527 fprintf(stderr
, "warning: different zlib version\n");
530 printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
531 ZLIB_VERSION
, ZLIB_VERNUM
, zlibCompileFlags());
533 compr
= (Byte
*)calloc((uInt
)comprLen
, 1);
534 uncompr
= (Byte
*)calloc((uInt
)uncomprLen
, 1);
535 /* compr and uncompr are cleared to avoid reading uninitialized
536 * data and to ensure that uncompr compresses well.
538 if (compr
== Z_NULL
|| uncompr
== Z_NULL
) {
539 printf("out of memory\n");
542 test_compress(compr
, comprLen
, uncompr
, uncomprLen
);
544 test_gzio((argc
> 1 ? argv
[1] : TESTFILE
),
545 uncompr
, uncomprLen
);
547 test_deflate(compr
, comprLen
);
548 test_inflate(compr
, comprLen
, uncompr
, uncomprLen
);
550 test_large_deflate(compr
, comprLen
, uncompr
, uncomprLen
);
551 test_large_inflate(compr
, comprLen
, uncompr
, uncomprLen
);
553 test_flush(compr
, &comprLen
);
554 test_sync(compr
, comprLen
, uncompr
, uncomprLen
);
555 comprLen
= uncomprLen
;
557 test_dict_deflate(compr
, comprLen
);
558 test_dict_inflate(compr
, comprLen
, uncompr
, uncomprLen
);