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