diff options
Diffstat (limited to 'example.c')
-rw-r--r-- | example.c | 170 |
1 files changed, 132 insertions, 38 deletions
@@ -1,9 +1,9 @@ | |||
1 | /* example.c -- usage example of the zlib compression library | 1 | /* example.c -- usage example of the zlib compression library |
2 | * Copyright (C) 1995 Jean-loup Gailly. | 2 | * Copyright (C) 1995-1996 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: example.c,v 1.9 1995/05/03 17:27:09 jloup Exp $ */ | 6 | /* $Id: example.c,v 1.13 1996/01/30 21:59:13 me Exp $ */ |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include "zlib.h" | 9 | #include "zlib.h" |
@@ -22,38 +22,44 @@ | |||
22 | } \ | 22 | } \ |
23 | } | 23 | } |
24 | 24 | ||
25 | char *hello = "hello, hello!"; | 25 | const char hello[] = "hello, hello!"; |
26 | /* "hello world" would be more standard, but the repeated "hello" | 26 | /* "hello world" would be more standard, but the repeated "hello" |
27 | * stresses the compression code better, sorry... | 27 | * stresses the compression code better, sorry... |
28 | */ | 28 | */ |
29 | 29 | ||
30 | void test_compress OF((Bytef *compr, uLong comprLen, | 30 | const char dictionary[] = "hello"; |
31 | Bytef *uncompr, uLong uncomprLen)); | 31 | uLong dictId; /* Adler32 value of the dictionary */ |
32 | void test_gzio OF((char *out, char *in, | 32 | |
33 | Bytef *uncompr, int uncomprLen)); | 33 | void test_compress OF((Byte *compr, uLong comprLen, |
34 | void test_deflate OF((Bytef *compr, uLong comprLen)); | 34 | Byte *uncompr, uLong uncomprLen)); |
35 | void test_inflate OF((Bytef *compr, uLong comprLen, | 35 | void test_gzio OF((const char *out, const char *in, |
36 | Bytef *uncompr, uLong uncomprLen)); | 36 | Byte *uncompr, int uncomprLen)); |
37 | void test_large_deflate OF((Bytef *compr, uLong comprLen, | 37 | void test_deflate OF((Byte *compr, uLong comprLen)); |
38 | Bytef *uncompr, uLong uncomprLen)); | 38 | void test_inflate OF((Byte *compr, uLong comprLen, |
39 | void test_large_inflate OF((Bytef *compr, uLong comprLen, | 39 | Byte *uncompr, uLong uncomprLen)); |
40 | Bytef *uncompr, uLong uncomprLen)); | 40 | void test_large_deflate OF((Byte *compr, uLong comprLen, |
41 | void test_flush OF((Bytef *compr, uLong comprLen)); | 41 | Byte *uncompr, uLong uncomprLen)); |
42 | void test_sync OF((Bytef *compr, uLong comprLen, | 42 | void test_large_inflate OF((Byte *compr, uLong comprLen, |
43 | Bytef *uncompr, uLong uncomprLen)); | 43 | Byte *uncompr, uLong uncomprLen)); |
44 | void test_flush OF((Byte *compr, uLong comprLen)); | ||
45 | void test_sync OF((Byte *compr, uLong comprLen, | ||
46 | Byte *uncompr, uLong uncomprLen)); | ||
47 | void test_dict_deflate OF((Byte *compr, uLong comprLen)); | ||
48 | void test_dict_inflate OF((Byte *compr, uLong comprLen, | ||
49 | Byte *uncompr, uLong uncomprLen)); | ||
44 | int main OF((int argc, char *argv[])); | 50 | int main OF((int argc, char *argv[])); |
45 | 51 | ||
46 | /* =========================================================================== | 52 | /* =========================================================================== |
47 | * Test compress() and uncompress() | 53 | * Test compress() and uncompress() |
48 | */ | 54 | */ |
49 | void test_compress(compr, comprLen, uncompr, uncomprLen) | 55 | void test_compress(compr, comprLen, uncompr, uncomprLen) |
50 | Bytef *compr, *uncompr; | 56 | Byte *compr, *uncompr; |
51 | uLong comprLen, uncomprLen; | 57 | uLong comprLen, uncomprLen; |
52 | { | 58 | { |
53 | int err; | 59 | int err; |
54 | uLong len = strlen(hello)+1; | 60 | uLong len = strlen(hello)+1; |
55 | 61 | ||
56 | err = compress(compr, &comprLen, (Byte*)hello, len); | 62 | err = compress(compr, &comprLen, (const Bytef*)hello, len); |
57 | CHECK_ERR(err, "compress"); | 63 | CHECK_ERR(err, "compress"); |
58 | 64 | ||
59 | strcpy((char*)uncompr, "garbage"); | 65 | strcpy((char*)uncompr, "garbage"); |
@@ -72,9 +78,9 @@ void test_compress(compr, comprLen, uncompr, uncomprLen) | |||
72 | * Test read/write of .gz files | 78 | * Test read/write of .gz files |
73 | */ | 79 | */ |
74 | void test_gzio(out, in, uncompr, uncomprLen) | 80 | void test_gzio(out, in, uncompr, uncomprLen) |
75 | char *out; /* output file */ | 81 | const char *out; /* output file */ |
76 | char *in; /* input file */ | 82 | const char *in; /* input file */ |
77 | Bytef *uncompr; | 83 | Byte *uncompr; |
78 | int uncomprLen; | 84 | int uncomprLen; |
79 | { | 85 | { |
80 | int err; | 86 | int err; |
@@ -87,7 +93,7 @@ void test_gzio(out, in, uncompr, uncomprLen) | |||
87 | exit(1); | 93 | exit(1); |
88 | } | 94 | } |
89 | 95 | ||
90 | if (gzwrite(file, hello, len) != len) { | 96 | if (gzwrite(file, (const voidp)hello, (unsigned)len) != len) { |
91 | fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); | 97 | fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); |
92 | } | 98 | } |
93 | gzclose(file); | 99 | gzclose(file); |
@@ -98,7 +104,7 @@ void test_gzio(out, in, uncompr, uncomprLen) | |||
98 | } | 104 | } |
99 | strcpy((char*)uncompr, "garbage"); | 105 | strcpy((char*)uncompr, "garbage"); |
100 | 106 | ||
101 | uncomprLen = gzread(file, uncompr, uncomprLen); | 107 | uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen); |
102 | if (uncomprLen != len) { | 108 | if (uncomprLen != len) { |
103 | fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); | 109 | fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); |
104 | } | 110 | } |
@@ -115,7 +121,7 @@ void test_gzio(out, in, uncompr, uncomprLen) | |||
115 | * Test deflate() with small buffers | 121 | * Test deflate() with small buffers |
116 | */ | 122 | */ |
117 | void test_deflate(compr, comprLen) | 123 | void test_deflate(compr, comprLen) |
118 | Bytef *compr; | 124 | Byte *compr; |
119 | uLong comprLen; | 125 | uLong comprLen; |
120 | { | 126 | { |
121 | z_stream c_stream; /* compression stream */ | 127 | z_stream c_stream; /* compression stream */ |
@@ -153,7 +159,7 @@ void test_deflate(compr, comprLen) | |||
153 | * Test inflate() with small buffers | 159 | * Test inflate() with small buffers |
154 | */ | 160 | */ |
155 | void test_inflate(compr, comprLen, uncompr, uncomprLen) | 161 | void test_inflate(compr, comprLen, uncompr, uncomprLen) |
156 | Bytef *compr, *uncompr; | 162 | Byte *compr, *uncompr; |
157 | uLong comprLen, uncomprLen; | 163 | uLong comprLen, uncomprLen; |
158 | { | 164 | { |
159 | int err; | 165 | int err; |
@@ -171,7 +177,7 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen) | |||
171 | d_stream.next_in = compr; | 177 | d_stream.next_in = compr; |
172 | d_stream.next_out = uncompr; | 178 | d_stream.next_out = uncompr; |
173 | 179 | ||
174 | while (d_stream.total_out < uncomprLen) { | 180 | while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { |
175 | d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ | 181 | d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ |
176 | err = inflate(&d_stream, Z_NO_FLUSH); | 182 | err = inflate(&d_stream, Z_NO_FLUSH); |
177 | if (err == Z_STREAM_END) break; | 183 | if (err == Z_STREAM_END) break; |
@@ -192,7 +198,7 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen) | |||
192 | * Test deflate() with large buffers and dynamic change of compression level | 198 | * Test deflate() with large buffers and dynamic change of compression level |
193 | */ | 199 | */ |
194 | void test_large_deflate(compr, comprLen, uncompr, uncomprLen) | 200 | void test_large_deflate(compr, comprLen, uncompr, uncomprLen) |
195 | Bytef *compr, *uncompr; | 201 | Byte *compr, *uncompr; |
196 | uLong comprLen, uncomprLen; | 202 | uLong comprLen, uncomprLen; |
197 | { | 203 | { |
198 | z_stream c_stream; /* compression stream */ | 204 | z_stream c_stream; /* compression stream */ |
@@ -206,7 +212,7 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen) | |||
206 | CHECK_ERR(err, "deflateInit"); | 212 | CHECK_ERR(err, "deflateInit"); |
207 | 213 | ||
208 | c_stream.next_out = compr; | 214 | c_stream.next_out = compr; |
209 | c_stream.avail_out = comprLen; | 215 | c_stream.avail_out = (uInt)comprLen; |
210 | 216 | ||
211 | /* At this point, uncompr is still mostly zeroes, so it should compress | 217 | /* At this point, uncompr is still mostly zeroes, so it should compress |
212 | * very well: | 218 | * very well: |
@@ -245,7 +251,7 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen) | |||
245 | * Test inflate() with large buffers | 251 | * Test inflate() with large buffers |
246 | */ | 252 | */ |
247 | void test_large_inflate(compr, comprLen, uncompr, uncomprLen) | 253 | void test_large_inflate(compr, comprLen, uncompr, uncomprLen) |
248 | Bytef *compr, *uncompr; | 254 | Byte *compr, *uncompr; |
249 | uLong comprLen, uncomprLen; | 255 | uLong comprLen, uncomprLen; |
250 | { | 256 | { |
251 | int err; | 257 | int err; |
@@ -265,7 +271,7 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) | |||
265 | 271 | ||
266 | for (;;) { | 272 | for (;;) { |
267 | d_stream.next_out = uncompr; /* discard the output */ | 273 | d_stream.next_out = uncompr; /* discard the output */ |
268 | d_stream.avail_out = uncomprLen; | 274 | d_stream.avail_out = (uInt)uncomprLen; |
269 | err = inflate(&d_stream, Z_NO_FLUSH); | 275 | err = inflate(&d_stream, Z_NO_FLUSH); |
270 | if (err == Z_STREAM_END) break; | 276 | if (err == Z_STREAM_END) break; |
271 | CHECK_ERR(err, "large inflate"); | 277 | CHECK_ERR(err, "large inflate"); |
@@ -285,7 +291,7 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) | |||
285 | * Test deflate() with full flush | 291 | * Test deflate() with full flush |
286 | */ | 292 | */ |
287 | void test_flush(compr, comprLen) | 293 | void test_flush(compr, comprLen) |
288 | Bytef *compr; | 294 | Byte *compr; |
289 | uLong comprLen; | 295 | uLong comprLen; |
290 | { | 296 | { |
291 | z_stream c_stream; /* compression stream */ | 297 | z_stream c_stream; /* compression stream */ |
@@ -321,7 +327,7 @@ void test_flush(compr, comprLen) | |||
321 | * Test inflateSync() | 327 | * Test inflateSync() |
322 | */ | 328 | */ |
323 | void test_sync(compr, comprLen, uncompr, uncomprLen) | 329 | void test_sync(compr, comprLen, uncompr, uncomprLen) |
324 | Bytef *compr, *uncompr; | 330 | Byte *compr, *uncompr; |
325 | uLong comprLen, uncomprLen; | 331 | uLong comprLen, uncomprLen; |
326 | { | 332 | { |
327 | int err; | 333 | int err; |
@@ -344,7 +350,7 @@ void test_sync(compr, comprLen, uncompr, uncomprLen) | |||
344 | inflate(&d_stream, Z_NO_FLUSH); | 350 | inflate(&d_stream, Z_NO_FLUSH); |
345 | CHECK_ERR(err, "inflate"); | 351 | CHECK_ERR(err, "inflate"); |
346 | 352 | ||
347 | d_stream.avail_in = (uInt)uncomprLen-2; /* read all compressed data */ | 353 | d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */ |
348 | err = inflateSync(&d_stream); /* but skip the damaged part */ | 354 | err = inflateSync(&d_stream); /* but skip the damaged part */ |
349 | CHECK_ERR(err, "inflateSync"); | 355 | CHECK_ERR(err, "inflateSync"); |
350 | 356 | ||
@@ -360,6 +366,91 @@ void test_sync(compr, comprLen, uncompr, uncomprLen) | |||
360 | } | 366 | } |
361 | 367 | ||
362 | /* =========================================================================== | 368 | /* =========================================================================== |
369 | * Test deflate() with preset dictionary | ||
370 | */ | ||
371 | void test_dict_deflate(compr, comprLen) | ||
372 | Byte *compr; | ||
373 | uLong comprLen; | ||
374 | { | ||
375 | z_stream c_stream; /* compression stream */ | ||
376 | int err; | ||
377 | |||
378 | c_stream.zalloc = (alloc_func)0; | ||
379 | c_stream.zfree = (free_func)0; | ||
380 | c_stream.opaque = (voidpf)0; | ||
381 | |||
382 | err = deflateInit(&c_stream, Z_BEST_COMPRESSION); | ||
383 | CHECK_ERR(err, "deflateInit"); | ||
384 | |||
385 | err = deflateSetDictionary(&c_stream, | ||
386 | (const Bytef*)dictionary, sizeof(dictionary)); | ||
387 | CHECK_ERR(err, "deflateSetDictionary"); | ||
388 | |||
389 | dictId = c_stream.adler; | ||
390 | c_stream.next_out = compr; | ||
391 | c_stream.avail_out = (uInt)comprLen; | ||
392 | |||
393 | c_stream.next_in = (Bytef*)hello; | ||
394 | c_stream.avail_in = (uInt)strlen(hello)+1; | ||
395 | |||
396 | err = deflate(&c_stream, Z_FINISH); | ||
397 | if (err != Z_STREAM_END) { | ||
398 | fprintf(stderr, "deflate should report Z_STREAM_END\n"); | ||
399 | } | ||
400 | err = deflateEnd(&c_stream); | ||
401 | CHECK_ERR(err, "deflateEnd"); | ||
402 | } | ||
403 | |||
404 | /* =========================================================================== | ||
405 | * Test inflate() with a preset dictionary | ||
406 | */ | ||
407 | void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) | ||
408 | Byte *compr, *uncompr; | ||
409 | uLong comprLen, uncomprLen; | ||
410 | { | ||
411 | int err; | ||
412 | z_stream d_stream; /* decompression stream */ | ||
413 | |||
414 | strcpy((char*)uncompr, "garbage"); | ||
415 | |||
416 | d_stream.zalloc = (alloc_func)0; | ||
417 | d_stream.zfree = (free_func)0; | ||
418 | d_stream.opaque = (voidpf)0; | ||
419 | |||
420 | err = inflateInit(&d_stream); | ||
421 | CHECK_ERR(err, "inflateInit"); | ||
422 | |||
423 | d_stream.next_in = compr; | ||
424 | d_stream.avail_in = (uInt)comprLen; | ||
425 | |||
426 | d_stream.next_out = uncompr; | ||
427 | d_stream.avail_out = (uInt)uncomprLen; | ||
428 | |||
429 | for (;;) { | ||
430 | err = inflate(&d_stream, Z_NO_FLUSH); | ||
431 | if (err == Z_STREAM_END) break; | ||
432 | if (err == Z_NEED_DICT) { | ||
433 | if (d_stream.adler != dictId) { | ||
434 | fprintf(stderr, "unexpected dictionary"); | ||
435 | exit(1); | ||
436 | } | ||
437 | err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, | ||
438 | sizeof(dictionary)); | ||
439 | } | ||
440 | CHECK_ERR(err, "inflate with dict"); | ||
441 | } | ||
442 | |||
443 | err = inflateEnd(&d_stream); | ||
444 | CHECK_ERR(err, "inflateEnd"); | ||
445 | |||
446 | if (strcmp((char*)uncompr, hello)) { | ||
447 | fprintf(stderr, "bad inflate with dict\n"); | ||
448 | } else { | ||
449 | printf("inflate with dictionary: %s\n", uncompr); | ||
450 | } | ||
451 | } | ||
452 | |||
453 | /* =========================================================================== | ||
363 | * Usage: example [output.gz [input.gz]] | 454 | * Usage: example [output.gz [input.gz]] |
364 | */ | 455 | */ |
365 | 456 | ||
@@ -367,8 +458,8 @@ int main(argc, argv) | |||
367 | int argc; | 458 | int argc; |
368 | char *argv[]; | 459 | char *argv[]; |
369 | { | 460 | { |
370 | Bytef *compr, *uncompr; | 461 | Byte *compr, *uncompr; |
371 | uLong comprLen = 32750*sizeof(int); /* don't overflow on MSDOS */ | 462 | uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */ |
372 | uLong uncomprLen = comprLen; | 463 | uLong uncomprLen = comprLen; |
373 | 464 | ||
374 | if (zlib_version[0] != ZLIB_VERSION[0]) { | 465 | if (zlib_version[0] != ZLIB_VERSION[0]) { |
@@ -379,8 +470,8 @@ int main(argc, argv) | |||
379 | fprintf(stderr, "warning: different zlib version\n"); | 470 | fprintf(stderr, "warning: different zlib version\n"); |
380 | } | 471 | } |
381 | 472 | ||
382 | compr = (Bytef*)malloc(comprLen); | 473 | compr = (Byte*)malloc((uInt)comprLen); |
383 | uncompr = (Bytef*)calloc(uncomprLen, 1); /* must be cleared initially */ | 474 | uncompr = (Byte*)calloc((uInt)uncomprLen, 1); /* must be cleared */ |
384 | if (compr == Z_NULL || uncompr == Z_NULL) { | 475 | if (compr == Z_NULL || uncompr == Z_NULL) { |
385 | printf("out of memory\n"); | 476 | printf("out of memory\n"); |
386 | exit(1); | 477 | exit(1); |
@@ -401,6 +492,9 @@ int main(argc, argv) | |||
401 | test_flush(compr, comprLen); | 492 | test_flush(compr, comprLen); |
402 | test_sync(compr, comprLen, uncompr, uncomprLen); | 493 | test_sync(compr, comprLen, uncompr, uncomprLen); |
403 | 494 | ||
495 | test_dict_deflate(compr, comprLen); | ||
496 | test_dict_inflate(compr, comprLen, uncompr, uncomprLen); | ||
497 | |||
404 | exit(0); | 498 | exit(0); |
405 | return 0; /* to avoid warning */ | 499 | return 0; /* to avoid warning */ |
406 | } | 500 | } |