diff options
Diffstat (limited to '')
| -rw-r--r-- | example.c | 122 |
1 files changed, 14 insertions, 108 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* example.c -- usage example of the zlib compression library | 1 | /* example.c -- usage example of the zlib compression library |
| 2 | * Copyright (C) 1995-1996 Jean-loup Gailly. | 2 | * Copyright (C) 1995 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 | ||
| @@ -22,17 +22,14 @@ | |||
| 22 | } \ | 22 | } \ |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | const char hello[] = "hello, hello!"; | 25 | 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 | const char dictionary[] = "hello"; | ||
| 31 | uLong dictId; /* Adler32 value of the dictionary */ | ||
| 32 | |||
| 33 | void test_compress OF((Bytef *compr, uLong comprLen, | 30 | void test_compress OF((Bytef *compr, uLong comprLen, |
| 34 | Bytef *uncompr, uLong uncomprLen)); | 31 | Bytef *uncompr, uLong uncomprLen)); |
| 35 | void test_gzio OF((const char *out, const char *in, | 32 | void test_gzio OF((char *out, char *in, |
| 36 | Bytef *uncompr, int uncomprLen)); | 33 | Bytef *uncompr, int uncomprLen)); |
| 37 | void test_deflate OF((Bytef *compr, uLong comprLen)); | 34 | void test_deflate OF((Bytef *compr, uLong comprLen)); |
| 38 | void test_inflate OF((Bytef *compr, uLong comprLen, | 35 | void test_inflate OF((Bytef *compr, uLong comprLen, |
| @@ -44,9 +41,6 @@ void test_large_inflate OF((Bytef *compr, uLong comprLen, | |||
| 44 | void test_flush OF((Bytef *compr, uLong comprLen)); | 41 | void test_flush OF((Bytef *compr, uLong comprLen)); |
| 45 | void test_sync OF((Bytef *compr, uLong comprLen, | 42 | void test_sync OF((Bytef *compr, uLong comprLen, |
| 46 | Bytef *uncompr, uLong uncomprLen)); | 43 | Bytef *uncompr, uLong uncomprLen)); |
| 47 | void test_dict_deflate OF((Bytef *compr, uLong comprLen)); | ||
| 48 | void test_dict_inflate OF((Bytef *compr, uLong comprLen, | ||
| 49 | Bytef *uncompr, uLong uncomprLen)); | ||
| 50 | int main OF((int argc, char *argv[])); | 44 | int main OF((int argc, char *argv[])); |
| 51 | 45 | ||
| 52 | /* =========================================================================== | 46 | /* =========================================================================== |
| @@ -59,7 +53,7 @@ void test_compress(compr, comprLen, uncompr, uncomprLen) | |||
| 59 | int err; | 53 | int err; |
| 60 | uLong len = strlen(hello)+1; | 54 | uLong len = strlen(hello)+1; |
| 61 | 55 | ||
| 62 | err = compress(compr, &comprLen, (const Bytef*)hello, len); | 56 | err = compress(compr, &comprLen, (Byte*)hello, len); |
| 63 | CHECK_ERR(err, "compress"); | 57 | CHECK_ERR(err, "compress"); |
| 64 | 58 | ||
| 65 | strcpy((char*)uncompr, "garbage"); | 59 | strcpy((char*)uncompr, "garbage"); |
| @@ -78,8 +72,8 @@ void test_compress(compr, comprLen, uncompr, uncomprLen) | |||
| 78 | * Test read/write of .gz files | 72 | * Test read/write of .gz files |
| 79 | */ | 73 | */ |
| 80 | void test_gzio(out, in, uncompr, uncomprLen) | 74 | void test_gzio(out, in, uncompr, uncomprLen) |
| 81 | const char *out; /* output file */ | 75 | char *out; /* output file */ |
| 82 | const char *in; /* input file */ | 76 | char *in; /* input file */ |
| 83 | Bytef *uncompr; | 77 | Bytef *uncompr; |
| 84 | int uncomprLen; | 78 | int uncomprLen; |
| 85 | { | 79 | { |
| @@ -93,7 +87,7 @@ void test_gzio(out, in, uncompr, uncomprLen) | |||
| 93 | exit(1); | 87 | exit(1); |
| 94 | } | 88 | } |
| 95 | 89 | ||
| 96 | if (gzwrite(file, (const voidp)hello, (unsigned)len) != len) { | 90 | if (gzwrite(file, hello, len) != len) { |
| 97 | fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); | 91 | fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); |
| 98 | } | 92 | } |
| 99 | gzclose(file); | 93 | gzclose(file); |
| @@ -104,7 +98,7 @@ void test_gzio(out, in, uncompr, uncomprLen) | |||
| 104 | } | 98 | } |
| 105 | strcpy((char*)uncompr, "garbage"); | 99 | strcpy((char*)uncompr, "garbage"); |
| 106 | 100 | ||
| 107 | uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen); | 101 | uncomprLen = gzread(file, uncompr, uncomprLen); |
| 108 | if (uncomprLen != len) { | 102 | if (uncomprLen != len) { |
| 109 | fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); | 103 | fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); |
| 110 | } | 104 | } |
| @@ -177,7 +171,7 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen) | |||
| 177 | d_stream.next_in = compr; | 171 | d_stream.next_in = compr; |
| 178 | d_stream.next_out = uncompr; | 172 | d_stream.next_out = uncompr; |
| 179 | 173 | ||
| 180 | while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { | 174 | while (d_stream.total_out < uncomprLen) { |
| 181 | d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ | 175 | d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ |
| 182 | err = inflate(&d_stream, Z_NO_FLUSH); | 176 | err = inflate(&d_stream, Z_NO_FLUSH); |
| 183 | if (err == Z_STREAM_END) break; | 177 | if (err == Z_STREAM_END) break; |
| @@ -212,7 +206,7 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen) | |||
| 212 | CHECK_ERR(err, "deflateInit"); | 206 | CHECK_ERR(err, "deflateInit"); |
| 213 | 207 | ||
| 214 | c_stream.next_out = compr; | 208 | c_stream.next_out = compr; |
| 215 | c_stream.avail_out = (uInt)comprLen; | 209 | c_stream.avail_out = comprLen; |
| 216 | 210 | ||
| 217 | /* At this point, uncompr is still mostly zeroes, so it should compress | 211 | /* At this point, uncompr is still mostly zeroes, so it should compress |
| 218 | * very well: | 212 | * very well: |
| @@ -271,7 +265,7 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) | |||
| 271 | 265 | ||
| 272 | for (;;) { | 266 | for (;;) { |
| 273 | d_stream.next_out = uncompr; /* discard the output */ | 267 | d_stream.next_out = uncompr; /* discard the output */ |
| 274 | d_stream.avail_out = (uInt)uncomprLen; | 268 | d_stream.avail_out = uncomprLen; |
| 275 | err = inflate(&d_stream, Z_NO_FLUSH); | 269 | err = inflate(&d_stream, Z_NO_FLUSH); |
| 276 | if (err == Z_STREAM_END) break; | 270 | if (err == Z_STREAM_END) break; |
| 277 | CHECK_ERR(err, "large inflate"); | 271 | CHECK_ERR(err, "large inflate"); |
| @@ -350,7 +344,7 @@ void test_sync(compr, comprLen, uncompr, uncomprLen) | |||
| 350 | inflate(&d_stream, Z_NO_FLUSH); | 344 | inflate(&d_stream, Z_NO_FLUSH); |
| 351 | CHECK_ERR(err, "inflate"); | 345 | CHECK_ERR(err, "inflate"); |
| 352 | 346 | ||
| 353 | d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */ | 347 | d_stream.avail_in = (uInt)uncomprLen-2; /* read all compressed data */ |
| 354 | err = inflateSync(&d_stream); /* but skip the damaged part */ | 348 | err = inflateSync(&d_stream); /* but skip the damaged part */ |
| 355 | CHECK_ERR(err, "inflateSync"); | 349 | CHECK_ERR(err, "inflateSync"); |
| 356 | 350 | ||
| @@ -366,91 +360,6 @@ void test_sync(compr, comprLen, uncompr, uncomprLen) | |||
| 366 | } | 360 | } |
| 367 | 361 | ||
| 368 | /* =========================================================================== | 362 | /* =========================================================================== |
| 369 | * Test deflate() with preset dictionary | ||
| 370 | */ | ||
| 371 | void test_dict_deflate(compr, comprLen) | ||
| 372 | Bytef *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 | Bytef *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 | /* =========================================================================== | ||
| 454 | * Usage: example [output.gz [input.gz]] | 363 | * Usage: example [output.gz [input.gz]] |
| 455 | */ | 364 | */ |
| 456 | 365 | ||
| @@ -470,8 +379,8 @@ int main(argc, argv) | |||
| 470 | fprintf(stderr, "warning: different zlib version\n"); | 379 | fprintf(stderr, "warning: different zlib version\n"); |
| 471 | } | 380 | } |
| 472 | 381 | ||
| 473 | compr = (Bytef*)malloc((uInt)comprLen); | 382 | compr = (Bytef*)malloc(comprLen); |
| 474 | uncompr = (Bytef*)calloc((uInt)uncomprLen, 1); /* must be cleared */ | 383 | uncompr = (Bytef*)calloc(uncomprLen, 1); /* must be cleared initially */ |
| 475 | if (compr == Z_NULL || uncompr == Z_NULL) { | 384 | if (compr == Z_NULL || uncompr == Z_NULL) { |
| 476 | printf("out of memory\n"); | 385 | printf("out of memory\n"); |
| 477 | exit(1); | 386 | exit(1); |
| @@ -492,9 +401,6 @@ int main(argc, argv) | |||
| 492 | test_flush(compr, comprLen); | 401 | test_flush(compr, comprLen); |
| 493 | test_sync(compr, comprLen, uncompr, uncomprLen); | 402 | test_sync(compr, comprLen, uncompr, uncomprLen); |
| 494 | 403 | ||
| 495 | test_dict_deflate(compr, comprLen); | ||
| 496 | test_dict_inflate(compr, comprLen, uncompr, uncomprLen); | ||
| 497 | |||
| 498 | exit(0); | 404 | exit(0); |
| 499 | return 0; /* to avoid warning */ | 405 | return 0; /* to avoid warning */ |
| 500 | } | 406 | } |
