diff options
Diffstat (limited to 'example.c')
-rw-r--r-- | example.c | 83 |
1 files changed, 41 insertions, 42 deletions
@@ -1,6 +1,6 @@ | |||
1 | /* example.c -- usage example of the zlib compression library | 1 | /* example.c -- usage example of the zlib compression library |
2 | * Copyright (C) 1995-2003 Jean-loup Gailly. | 2 | * Copyright (C) 1995-2003 Jean-loup Gailly. |
3 | * For conditions of distribution and use, see copyright notice in zlib.h | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | */ | 4 | */ |
5 | 5 | ||
6 | /* @(#) $Id$ */ | 6 | /* @(#) $Id$ */ |
@@ -37,22 +37,22 @@ const char dictionary[] = "hello"; | |||
37 | uLong dictId; /* Adler32 value of the dictionary */ | 37 | uLong dictId; /* Adler32 value of the dictionary */ |
38 | 38 | ||
39 | void test_compress OF((Byte *compr, uLong comprLen, | 39 | void test_compress OF((Byte *compr, uLong comprLen, |
40 | Byte *uncompr, uLong uncomprLen)); | 40 | Byte *uncompr, uLong uncomprLen)); |
41 | void test_gzio OF((const char *out, const char *in, | 41 | void test_gzio OF((const char *out, const char *in, |
42 | Byte *uncompr, int uncomprLen)); | 42 | Byte *uncompr, int uncomprLen)); |
43 | void test_deflate OF((Byte *compr, uLong comprLen)); | 43 | void test_deflate OF((Byte *compr, uLong comprLen)); |
44 | void test_inflate OF((Byte *compr, uLong comprLen, | 44 | void test_inflate OF((Byte *compr, uLong comprLen, |
45 | Byte *uncompr, uLong uncomprLen)); | 45 | Byte *uncompr, uLong uncomprLen)); |
46 | void test_large_deflate OF((Byte *compr, uLong comprLen, | 46 | void test_large_deflate OF((Byte *compr, uLong comprLen, |
47 | Byte *uncompr, uLong uncomprLen)); | 47 | Byte *uncompr, uLong uncomprLen)); |
48 | void test_large_inflate OF((Byte *compr, uLong comprLen, | 48 | void test_large_inflate OF((Byte *compr, uLong comprLen, |
49 | Byte *uncompr, uLong uncomprLen)); | 49 | Byte *uncompr, uLong uncomprLen)); |
50 | void test_flush OF((Byte *compr, uLong *comprLen)); | 50 | void test_flush OF((Byte *compr, uLong *comprLen)); |
51 | void test_sync OF((Byte *compr, uLong comprLen, | 51 | void test_sync OF((Byte *compr, uLong comprLen, |
52 | Byte *uncompr, uLong uncomprLen)); | 52 | Byte *uncompr, uLong uncomprLen)); |
53 | void test_dict_deflate OF((Byte *compr, uLong comprLen)); | 53 | void test_dict_deflate OF((Byte *compr, uLong comprLen)); |
54 | void test_dict_inflate OF((Byte *compr, uLong comprLen, | 54 | void test_dict_inflate OF((Byte *compr, uLong comprLen, |
55 | Byte *uncompr, uLong uncomprLen)); | 55 | Byte *uncompr, uLong uncomprLen)); |
56 | int main OF((int argc, char *argv[])); | 56 | int main OF((int argc, char *argv[])); |
57 | 57 | ||
58 | /* =========================================================================== | 58 | /* =========================================================================== |
@@ -75,7 +75,7 @@ void test_compress(compr, comprLen, uncompr, uncomprLen) | |||
75 | 75 | ||
76 | if (strcmp((char*)uncompr, hello)) { | 76 | if (strcmp((char*)uncompr, hello)) { |
77 | fprintf(stderr, "bad uncompress\n"); | 77 | fprintf(stderr, "bad uncompress\n"); |
78 | exit(1); | 78 | exit(1); |
79 | } else { | 79 | } else { |
80 | printf("uncompress(): %s\n", (char *)uncompr); | 80 | printf("uncompress(): %s\n", (char *)uncompr); |
81 | } | 81 | } |
@@ -103,11 +103,11 @@ void test_gzio(out, in, uncompr, uncomprLen) | |||
103 | gzputc(file, 'h'); | 103 | gzputc(file, 'h'); |
104 | if (gzputs(file, "ello") != 4) { | 104 | if (gzputs(file, "ello") != 4) { |
105 | fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); | 105 | fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); |
106 | exit(1); | 106 | exit(1); |
107 | } | 107 | } |
108 | if (gzprintf(file, ", %s!", "hello") != 8) { | 108 | if (gzprintf(file, ", %s!", "hello") != 8) { |
109 | fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); | 109 | fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); |
110 | exit(1); | 110 | exit(1); |
111 | } | 111 | } |
112 | gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ | 112 | gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ |
113 | gzclose(file); | 113 | gzclose(file); |
@@ -121,36 +121,36 @@ void test_gzio(out, in, uncompr, uncomprLen) | |||
121 | uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen); | 121 | uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen); |
122 | if (uncomprLen != len) { | 122 | if (uncomprLen != len) { |
123 | fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); | 123 | fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); |
124 | exit(1); | 124 | exit(1); |
125 | } | 125 | } |
126 | if (strcmp((char*)uncompr, hello)) { | 126 | if (strcmp((char*)uncompr, hello)) { |
127 | fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); | 127 | fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); |
128 | exit(1); | 128 | exit(1); |
129 | } else { | 129 | } else { |
130 | printf("gzread(): %s\n", (char *)uncompr); | 130 | printf("gzread(): %s\n", (char *)uncompr); |
131 | } | 131 | } |
132 | 132 | ||
133 | pos = gzseek(file, -8L, SEEK_CUR); | 133 | pos = gzseek(file, -8L, SEEK_CUR); |
134 | if (pos != 6 || gztell(file) != pos) { | 134 | if (pos != 6 || gztell(file) != pos) { |
135 | fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", | 135 | fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", |
136 | (long)pos, (long)gztell(file)); | 136 | (long)pos, (long)gztell(file)); |
137 | exit(1); | 137 | exit(1); |
138 | } | 138 | } |
139 | 139 | ||
140 | if (gzgetc(file) != ' ') { | 140 | if (gzgetc(file) != ' ') { |
141 | fprintf(stderr, "gzgetc error\n"); | 141 | fprintf(stderr, "gzgetc error\n"); |
142 | exit(1); | 142 | exit(1); |
143 | } | 143 | } |
144 | 144 | ||
145 | gzgets(file, (char*)uncompr, uncomprLen); | 145 | gzgets(file, (char*)uncompr, uncomprLen); |
146 | uncomprLen = strlen((char*)uncompr); | 146 | uncomprLen = strlen((char*)uncompr); |
147 | if (uncomprLen != 6) { /* "hello!" */ | 147 | if (uncomprLen != 6) { /* "hello!" */ |
148 | fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); | 148 | fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); |
149 | exit(1); | 149 | exit(1); |
150 | } | 150 | } |
151 | if (strcmp((char*)uncompr, hello+7)) { | 151 | if (strcmp((char*)uncompr, hello+7)) { |
152 | fprintf(stderr, "bad gzgets after gzseek\n"); | 152 | fprintf(stderr, "bad gzgets after gzseek\n"); |
153 | exit(1); | 153 | exit(1); |
154 | } else { | 154 | } else { |
155 | printf("gzgets() after gzseek: %s\n", (char *)uncompr); | 155 | printf("gzgets() after gzseek: %s\n", (char *)uncompr); |
156 | } | 156 | } |
@@ -231,7 +231,7 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen) | |||
231 | 231 | ||
232 | if (strcmp((char*)uncompr, hello)) { | 232 | if (strcmp((char*)uncompr, hello)) { |
233 | fprintf(stderr, "bad inflate\n"); | 233 | fprintf(stderr, "bad inflate\n"); |
234 | exit(1); | 234 | exit(1); |
235 | } else { | 235 | } else { |
236 | printf("inflate(): %s\n", (char *)uncompr); | 236 | printf("inflate(): %s\n", (char *)uncompr); |
237 | } | 237 | } |
@@ -266,7 +266,7 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen) | |||
266 | CHECK_ERR(err, "deflate"); | 266 | CHECK_ERR(err, "deflate"); |
267 | if (c_stream.avail_in != 0) { | 267 | if (c_stream.avail_in != 0) { |
268 | fprintf(stderr, "deflate not greedy\n"); | 268 | fprintf(stderr, "deflate not greedy\n"); |
269 | exit(1); | 269 | exit(1); |
270 | } | 270 | } |
271 | 271 | ||
272 | /* Feed in already compressed data and switch to no compression: */ | 272 | /* Feed in already compressed data and switch to no compression: */ |
@@ -286,7 +286,7 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen) | |||
286 | err = deflate(&c_stream, Z_FINISH); | 286 | err = deflate(&c_stream, Z_FINISH); |
287 | if (err != Z_STREAM_END) { | 287 | if (err != Z_STREAM_END) { |
288 | fprintf(stderr, "deflate should report Z_STREAM_END\n"); | 288 | fprintf(stderr, "deflate should report Z_STREAM_END\n"); |
289 | exit(1); | 289 | exit(1); |
290 | } | 290 | } |
291 | err = deflateEnd(&c_stream); | 291 | err = deflateEnd(&c_stream); |
292 | CHECK_ERR(err, "deflateEnd"); | 292 | CHECK_ERR(err, "deflateEnd"); |
@@ -316,7 +316,7 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) | |||
316 | 316 | ||
317 | for (;;) { | 317 | for (;;) { |
318 | d_stream.next_out = uncompr; /* discard the output */ | 318 | d_stream.next_out = uncompr; /* discard the output */ |
319 | d_stream.avail_out = (uInt)uncomprLen; | 319 | d_stream.avail_out = (uInt)uncomprLen; |
320 | err = inflate(&d_stream, Z_NO_FLUSH); | 320 | err = inflate(&d_stream, Z_NO_FLUSH); |
321 | if (err == Z_STREAM_END) break; | 321 | if (err == Z_STREAM_END) break; |
322 | CHECK_ERR(err, "large inflate"); | 322 | CHECK_ERR(err, "large inflate"); |
@@ -327,7 +327,7 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) | |||
327 | 327 | ||
328 | if (d_stream.total_out != 2*uncomprLen + comprLen/2) { | 328 | if (d_stream.total_out != 2*uncomprLen + comprLen/2) { |
329 | fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); | 329 | fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); |
330 | exit(1); | 330 | exit(1); |
331 | } else { | 331 | } else { |
332 | printf("large_inflate(): OK\n"); | 332 | printf("large_inflate(): OK\n"); |
333 | } | 333 | } |
@@ -407,7 +407,7 @@ void test_sync(compr, comprLen, uncompr, uncomprLen) | |||
407 | if (err != Z_DATA_ERROR) { | 407 | if (err != Z_DATA_ERROR) { |
408 | fprintf(stderr, "inflate should report DATA_ERROR\n"); | 408 | fprintf(stderr, "inflate should report DATA_ERROR\n"); |
409 | /* Because of incorrect adler32 */ | 409 | /* Because of incorrect adler32 */ |
410 | exit(1); | 410 | exit(1); |
411 | } | 411 | } |
412 | err = inflateEnd(&d_stream); | 412 | err = inflateEnd(&d_stream); |
413 | CHECK_ERR(err, "inflateEnd"); | 413 | CHECK_ERR(err, "inflateEnd"); |
@@ -433,7 +433,7 @@ void test_dict_deflate(compr, comprLen) | |||
433 | CHECK_ERR(err, "deflateInit"); | 433 | CHECK_ERR(err, "deflateInit"); |
434 | 434 | ||
435 | err = deflateSetDictionary(&c_stream, | 435 | err = deflateSetDictionary(&c_stream, |
436 | (const Bytef*)dictionary, sizeof(dictionary)); | 436 | (const Bytef*)dictionary, sizeof(dictionary)); |
437 | CHECK_ERR(err, "deflateSetDictionary"); | 437 | CHECK_ERR(err, "deflateSetDictionary"); |
438 | 438 | ||
439 | dictId = c_stream.adler; | 439 | dictId = c_stream.adler; |
@@ -446,7 +446,7 @@ void test_dict_deflate(compr, comprLen) | |||
446 | err = deflate(&c_stream, Z_FINISH); | 446 | err = deflate(&c_stream, Z_FINISH); |
447 | if (err != Z_STREAM_END) { | 447 | if (err != Z_STREAM_END) { |
448 | fprintf(stderr, "deflate should report Z_STREAM_END\n"); | 448 | fprintf(stderr, "deflate should report Z_STREAM_END\n"); |
449 | exit(1); | 449 | exit(1); |
450 | } | 450 | } |
451 | err = deflateEnd(&c_stream); | 451 | err = deflateEnd(&c_stream); |
452 | CHECK_ERR(err, "deflateEnd"); | 452 | CHECK_ERR(err, "deflateEnd"); |
@@ -480,14 +480,14 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) | |||
480 | for (;;) { | 480 | for (;;) { |
481 | err = inflate(&d_stream, Z_NO_FLUSH); | 481 | err = inflate(&d_stream, Z_NO_FLUSH); |
482 | if (err == Z_STREAM_END) break; | 482 | if (err == Z_STREAM_END) break; |
483 | if (err == Z_NEED_DICT) { | 483 | if (err == Z_NEED_DICT) { |
484 | if (d_stream.adler != dictId) { | 484 | if (d_stream.adler != dictId) { |
485 | fprintf(stderr, "unexpected dictionary"); | 485 | fprintf(stderr, "unexpected dictionary"); |
486 | exit(1); | 486 | exit(1); |
487 | } | 487 | } |
488 | err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, | 488 | err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, |
489 | sizeof(dictionary)); | 489 | sizeof(dictionary)); |
490 | } | 490 | } |
491 | CHECK_ERR(err, "inflate with dict"); | 491 | CHECK_ERR(err, "inflate with dict"); |
492 | } | 492 | } |
493 | 493 | ||
@@ -496,7 +496,7 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) | |||
496 | 496 | ||
497 | if (strcmp((char*)uncompr, hello)) { | 497 | if (strcmp((char*)uncompr, hello)) { |
498 | fprintf(stderr, "bad inflate with dict\n"); | 498 | fprintf(stderr, "bad inflate with dict\n"); |
499 | exit(1); | 499 | exit(1); |
500 | } else { | 500 | } else { |
501 | printf("inflate with dictionary: %s\n", (char *)uncompr); | 501 | printf("inflate with dictionary: %s\n", (char *)uncompr); |
502 | } | 502 | } |
@@ -530,13 +530,13 @@ int main(argc, argv) | |||
530 | */ | 530 | */ |
531 | if (compr == Z_NULL || uncompr == Z_NULL) { | 531 | if (compr == Z_NULL || uncompr == Z_NULL) { |
532 | printf("out of memory\n"); | 532 | printf("out of memory\n"); |
533 | exit(1); | 533 | exit(1); |
534 | } | 534 | } |
535 | test_compress(compr, comprLen, uncompr, uncomprLen); | 535 | test_compress(compr, comprLen, uncompr, uncomprLen); |
536 | 536 | ||
537 | test_gzio((argc > 1 ? argv[1] : TESTFILE), | 537 | test_gzio((argc > 1 ? argv[1] : TESTFILE), |
538 | (argc > 2 ? argv[2] : TESTFILE), | 538 | (argc > 2 ? argv[2] : TESTFILE), |
539 | uncompr, (int)uncomprLen); | 539 | uncompr, (int)uncomprLen); |
540 | 540 | ||
541 | test_deflate(compr, comprLen); | 541 | test_deflate(compr, comprLen); |
542 | test_inflate(compr, comprLen, uncompr, uncomprLen); | 542 | test_inflate(compr, comprLen, uncompr, uncomprLen); |
@@ -554,6 +554,5 @@ int main(argc, argv) | |||
554 | free(compr); | 554 | free(compr); |
555 | free(uncompr); | 555 | free(uncompr); |
556 | 556 | ||
557 | exit(0); | 557 | return 0; |
558 | return 0; /* to avoid warning */ | ||
559 | } | 558 | } |