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