example.c (e37bb444aa945ed0725766e986698a09bd61b1b2) | example.c (4717628ed859513a3262ea68259d0605f39de0b3) |
---|---|
1/* example.c -- usage example of the zlib compression library 2 * Copyright (C) 1995-2006, 2011, 2016 Jean-loup Gailly 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 5 6/* @(#) $Id$ */ 7 8#include "zlib.h" --- 20 unchanged lines hidden (view full) --- 29static z_const char hello[] = "hello, hello!"; 30/* "hello world" would be more standard, but the repeated "hello" 31 * stresses the compression code better, sorry... 32 */ 33 34static const char dictionary[] = "hello"; 35static uLong dictId; /* Adler32 value of the dictionary */ 36 | 1/* example.c -- usage example of the zlib compression library 2 * Copyright (C) 1995-2006, 2011, 2016 Jean-loup Gailly 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 5 6/* @(#) $Id$ */ 7 8#include "zlib.h" --- 20 unchanged lines hidden (view full) --- 29static z_const char hello[] = "hello, hello!"; 30/* "hello world" would be more standard, but the repeated "hello" 31 * stresses the compression code better, sorry... 32 */ 33 34static const char dictionary[] = "hello"; 35static uLong dictId; /* Adler32 value of the dictionary */ 36 |
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#ifdef Z_SOLO 54 | 37#ifdef Z_SOLO 38 |
55void *myalloc OF((void *, unsigned, unsigned)); 56void myfree OF((void *, void *)); 57 58void *myalloc(q, n, m) 59 void *q; 60 unsigned n, m; 61{ | 39void *myalloc(void *q, unsigned n, unsigned m) { |
62 (void)q; 63 return calloc(n, m); 64} 65 | 40 (void)q; 41 return calloc(n, m); 42} 43 |
66void myfree(void *q, void *p) 67{ | 44void myfree(void *q, void *p) { |
68 (void)q; 69 free(p); 70} 71 72static alloc_func zalloc = myalloc; 73static free_func zfree = myfree; 74 75#else /* !Z_SOLO */ 76 77static alloc_func zalloc = (alloc_func)0; 78static free_func zfree = (free_func)0; 79 | 45 (void)q; 46 free(p); 47} 48 49static alloc_func zalloc = myalloc; 50static free_func zfree = myfree; 51 52#else /* !Z_SOLO */ 53 54static alloc_func zalloc = (alloc_func)0; 55static free_func zfree = (free_func)0; 56 |
80void test_compress OF((Byte *compr, uLong comprLen, 81 Byte *uncompr, uLong uncomprLen)); 82void test_gzio OF((const char *fname, 83 Byte *uncompr, uLong uncomprLen)); 84 | |
85/* =========================================================================== 86 * Test compress() and uncompress() 87 */ | 57/* =========================================================================== 58 * Test compress() and uncompress() 59 */ |
88void test_compress(compr, comprLen, uncompr, uncomprLen) 89 Byte *compr, *uncompr; 90 uLong comprLen, uncomprLen; 91{ | 60void test_compress(Byte *compr, uLong comprLen, Byte *uncompr, 61 uLong uncomprLen) { |
92 int err; 93 uLong len = (uLong)strlen(hello)+1; 94 95 err = compress(compr, &comprLen, (const Bytef*)hello, len); 96 CHECK_ERR(err, "compress"); 97 98 strcpy((char*)uncompr, "garbage"); 99 --- 6 unchanged lines hidden (view full) --- 106 } else { 107 printf("uncompress(): %s\n", (char *)uncompr); 108 } 109} 110 111/* =========================================================================== 112 * Test read/write of .gz files 113 */ | 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 --- 6 unchanged lines hidden (view full) --- 76 } else { 77 printf("uncompress(): %s\n", (char *)uncompr); 78 } 79} 80 81/* =========================================================================== 82 * Test read/write of .gz files 83 */ |
114void test_gzio(fname, uncompr, uncomprLen) 115 const char *fname; /* compressed file name */ 116 Byte *uncompr; 117 uLong uncomprLen; 118{ | 84void test_gzio(const char *fname, Byte *uncompr, uLong uncomprLen) { |
119#ifdef NO_GZCOMPRESS 120 fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n"); 121#else 122 int err; 123 int len = (int)strlen(hello)+1; 124 gzFile file; 125 z_off_t pos; 126 --- 65 unchanged lines hidden (view full) --- 192#endif 193} 194 195#endif /* Z_SOLO */ 196 197/* =========================================================================== 198 * Test deflate() with small buffers 199 */ | 85#ifdef NO_GZCOMPRESS 86 fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n"); 87#else 88 int err; 89 int len = (int)strlen(hello)+1; 90 gzFile file; 91 z_off_t pos; 92 --- 65 unchanged lines hidden (view full) --- 158#endif 159} 160 161#endif /* Z_SOLO */ 162 163/* =========================================================================== 164 * Test deflate() with small buffers 165 */ |
200void test_deflate(compr, comprLen) 201 Byte *compr; 202 uLong comprLen; 203{ | 166void test_deflate(Byte *compr, uLong comprLen) { |
204 z_stream c_stream; /* compression stream */ 205 int err; 206 uLong len = (uLong)strlen(hello)+1; 207 208 c_stream.zalloc = zalloc; 209 c_stream.zfree = zfree; 210 c_stream.opaque = (voidpf)0; 211 --- 18 unchanged lines hidden (view full) --- 230 231 err = deflateEnd(&c_stream); 232 CHECK_ERR(err, "deflateEnd"); 233} 234 235/* =========================================================================== 236 * Test inflate() with small buffers 237 */ | 167 z_stream c_stream; /* compression stream */ 168 int err; 169 uLong len = (uLong)strlen(hello)+1; 170 171 c_stream.zalloc = zalloc; 172 c_stream.zfree = zfree; 173 c_stream.opaque = (voidpf)0; 174 --- 18 unchanged lines hidden (view full) --- 193 194 err = deflateEnd(&c_stream); 195 CHECK_ERR(err, "deflateEnd"); 196} 197 198/* =========================================================================== 199 * Test inflate() with small buffers 200 */ |
238void test_inflate(compr, comprLen, uncompr, uncomprLen) 239 Byte *compr, *uncompr; 240 uLong comprLen, uncomprLen; 241{ | 201void test_inflate(Byte *compr, uLong comprLen, Byte *uncompr, 202 uLong uncomprLen) { |
242 int err; 243 z_stream d_stream; /* decompression stream */ 244 245 strcpy((char*)uncompr, "garbage"); 246 247 d_stream.zalloc = zalloc; 248 d_stream.zfree = zfree; 249 d_stream.opaque = (voidpf)0; --- 21 unchanged lines hidden (view full) --- 271 } else { 272 printf("inflate(): %s\n", (char *)uncompr); 273 } 274} 275 276/* =========================================================================== 277 * Test deflate() with large buffers and dynamic change of compression level 278 */ | 203 int err; 204 z_stream d_stream; /* decompression stream */ 205 206 strcpy((char*)uncompr, "garbage"); 207 208 d_stream.zalloc = zalloc; 209 d_stream.zfree = zfree; 210 d_stream.opaque = (voidpf)0; --- 21 unchanged lines hidden (view full) --- 232 } else { 233 printf("inflate(): %s\n", (char *)uncompr); 234 } 235} 236 237/* =========================================================================== 238 * Test deflate() with large buffers and dynamic change of compression level 239 */ |
279void test_large_deflate(compr, comprLen, uncompr, uncomprLen) 280 Byte *compr, *uncompr; 281 uLong comprLen, uncomprLen; 282{ | 240void test_large_deflate(Byte *compr, uLong comprLen, Byte *uncompr, 241 uLong uncomprLen) { |
283 z_stream c_stream; /* compression stream */ 284 int err; 285 286 c_stream.zalloc = zalloc; 287 c_stream.zfree = zfree; 288 c_stream.opaque = (voidpf)0; 289 290 err = deflateInit(&c_stream, Z_BEST_SPEED); --- 12 unchanged lines hidden (view full) --- 303 if (c_stream.avail_in != 0) { 304 fprintf(stderr, "deflate not greedy\n"); 305 exit(1); 306 } 307 308 /* Feed in already compressed data and switch to no compression: */ 309 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); 310 c_stream.next_in = compr; | 242 z_stream c_stream; /* compression stream */ 243 int err; 244 245 c_stream.zalloc = zalloc; 246 c_stream.zfree = zfree; 247 c_stream.opaque = (voidpf)0; 248 249 err = deflateInit(&c_stream, Z_BEST_SPEED); --- 12 unchanged lines hidden (view full) --- 262 if (c_stream.avail_in != 0) { 263 fprintf(stderr, "deflate not greedy\n"); 264 exit(1); 265 } 266 267 /* Feed in already compressed data and switch to no compression: */ 268 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); 269 c_stream.next_in = compr; |
311 c_stream.avail_in = (uInt)comprLen/2; | 270 c_stream.avail_in = (uInt)uncomprLen/2; |
312 err = deflate(&c_stream, Z_NO_FLUSH); 313 CHECK_ERR(err, "deflate"); 314 315 /* Switch back to compressing mode: */ 316 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); 317 c_stream.next_in = uncompr; 318 c_stream.avail_in = (uInt)uncomprLen; 319 err = deflate(&c_stream, Z_NO_FLUSH); --- 6 unchanged lines hidden (view full) --- 326 } 327 err = deflateEnd(&c_stream); 328 CHECK_ERR(err, "deflateEnd"); 329} 330 331/* =========================================================================== 332 * Test inflate() with large buffers 333 */ | 271 err = deflate(&c_stream, Z_NO_FLUSH); 272 CHECK_ERR(err, "deflate"); 273 274 /* Switch back to compressing mode: */ 275 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); 276 c_stream.next_in = uncompr; 277 c_stream.avail_in = (uInt)uncomprLen; 278 err = deflate(&c_stream, Z_NO_FLUSH); --- 6 unchanged lines hidden (view full) --- 285 } 286 err = deflateEnd(&c_stream); 287 CHECK_ERR(err, "deflateEnd"); 288} 289 290/* =========================================================================== 291 * Test inflate() with large buffers 292 */ |
334void test_large_inflate(compr, comprLen, uncompr, uncomprLen) 335 Byte *compr, *uncompr; 336 uLong comprLen, uncomprLen; 337{ | 293void test_large_inflate(Byte *compr, uLong comprLen, Byte *uncompr, 294 uLong uncomprLen) { |
338 int err; 339 z_stream d_stream; /* decompression stream */ 340 341 strcpy((char*)uncompr, "garbage"); 342 343 d_stream.zalloc = zalloc; 344 d_stream.zfree = zfree; 345 d_stream.opaque = (voidpf)0; --- 10 unchanged lines hidden (view full) --- 356 err = inflate(&d_stream, Z_NO_FLUSH); 357 if (err == Z_STREAM_END) break; 358 CHECK_ERR(err, "large inflate"); 359 } 360 361 err = inflateEnd(&d_stream); 362 CHECK_ERR(err, "inflateEnd"); 363 | 295 int err; 296 z_stream d_stream; /* decompression stream */ 297 298 strcpy((char*)uncompr, "garbage"); 299 300 d_stream.zalloc = zalloc; 301 d_stream.zfree = zfree; 302 d_stream.opaque = (voidpf)0; --- 10 unchanged lines hidden (view full) --- 313 err = inflate(&d_stream, Z_NO_FLUSH); 314 if (err == Z_STREAM_END) break; 315 CHECK_ERR(err, "large inflate"); 316 } 317 318 err = inflateEnd(&d_stream); 319 CHECK_ERR(err, "inflateEnd"); 320 |
364 if (d_stream.total_out != 2*uncomprLen + comprLen/2) { | 321 if (d_stream.total_out != 2*uncomprLen + uncomprLen/2) { |
365 fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); 366 exit(1); 367 } else { 368 printf("large_inflate(): OK\n"); 369 } 370} 371 372/* =========================================================================== 373 * Test deflate() with full flush 374 */ | 322 fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); 323 exit(1); 324 } else { 325 printf("large_inflate(): OK\n"); 326 } 327} 328 329/* =========================================================================== 330 * Test deflate() with full flush 331 */ |
375void test_flush(compr, comprLen) 376 Byte *compr; 377 uLong *comprLen; 378{ | 332void test_flush(Byte *compr, uLong *comprLen) { |
379 z_stream c_stream; /* compression stream */ 380 int err; 381 uInt len = (uInt)strlen(hello)+1; 382 383 c_stream.zalloc = zalloc; 384 c_stream.zfree = zfree; 385 c_stream.opaque = (voidpf)0; 386 --- 18 unchanged lines hidden (view full) --- 405 CHECK_ERR(err, "deflateEnd"); 406 407 *comprLen = c_stream.total_out; 408} 409 410/* =========================================================================== 411 * Test inflateSync() 412 */ | 333 z_stream c_stream; /* compression stream */ 334 int err; 335 uInt len = (uInt)strlen(hello)+1; 336 337 c_stream.zalloc = zalloc; 338 c_stream.zfree = zfree; 339 c_stream.opaque = (voidpf)0; 340 --- 18 unchanged lines hidden (view full) --- 359 CHECK_ERR(err, "deflateEnd"); 360 361 *comprLen = c_stream.total_out; 362} 363 364/* =========================================================================== 365 * Test inflateSync() 366 */ |
413void test_sync(compr, comprLen, uncompr, uncomprLen) 414 Byte *compr, *uncompr; 415 uLong comprLen, uncomprLen; 416{ | 367void test_sync(Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen) { |
417 int err; 418 z_stream d_stream; /* decompression stream */ 419 420 strcpy((char*)uncompr, "garbage"); 421 422 d_stream.zalloc = zalloc; 423 d_stream.zfree = zfree; 424 d_stream.opaque = (voidpf)0; --- 23 unchanged lines hidden (view full) --- 448 CHECK_ERR(err, "inflateEnd"); 449 450 printf("after inflateSync(): hel%s\n", (char *)uncompr); 451} 452 453/* =========================================================================== 454 * Test deflate() with preset dictionary 455 */ | 368 int err; 369 z_stream d_stream; /* decompression stream */ 370 371 strcpy((char*)uncompr, "garbage"); 372 373 d_stream.zalloc = zalloc; 374 d_stream.zfree = zfree; 375 d_stream.opaque = (voidpf)0; --- 23 unchanged lines hidden (view full) --- 399 CHECK_ERR(err, "inflateEnd"); 400 401 printf("after inflateSync(): hel%s\n", (char *)uncompr); 402} 403 404/* =========================================================================== 405 * Test deflate() with preset dictionary 406 */ |
456void test_dict_deflate(compr, comprLen) 457 Byte *compr; 458 uLong comprLen; 459{ | 407void test_dict_deflate(Byte *compr, uLong comprLen) { |
460 z_stream c_stream; /* compression stream */ 461 int err; 462 463 c_stream.zalloc = zalloc; 464 c_stream.zfree = zfree; 465 c_stream.opaque = (voidpf)0; 466 467 err = deflateInit(&c_stream, Z_BEST_COMPRESSION); --- 17 unchanged lines hidden (view full) --- 485 } 486 err = deflateEnd(&c_stream); 487 CHECK_ERR(err, "deflateEnd"); 488} 489 490/* =========================================================================== 491 * Test inflate() with a preset dictionary 492 */ | 408 z_stream c_stream; /* compression stream */ 409 int err; 410 411 c_stream.zalloc = zalloc; 412 c_stream.zfree = zfree; 413 c_stream.opaque = (voidpf)0; 414 415 err = deflateInit(&c_stream, Z_BEST_COMPRESSION); --- 17 unchanged lines hidden (view full) --- 433 } 434 err = deflateEnd(&c_stream); 435 CHECK_ERR(err, "deflateEnd"); 436} 437 438/* =========================================================================== 439 * Test inflate() with a preset dictionary 440 */ |
493void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) 494 Byte *compr, *uncompr; 495 uLong comprLen, uncomprLen; 496{ | 441void test_dict_inflate(Byte *compr, uLong comprLen, Byte *uncompr, 442 uLong uncomprLen) { |
497 int err; 498 z_stream d_stream; /* decompression stream */ 499 500 strcpy((char*)uncompr, "garbage"); 501 502 d_stream.zalloc = zalloc; 503 d_stream.zfree = zfree; 504 d_stream.opaque = (voidpf)0; --- 31 unchanged lines hidden (view full) --- 536 printf("inflate with dictionary: %s\n", (char *)uncompr); 537 } 538} 539 540/* =========================================================================== 541 * Usage: example [output.gz [input.gz]] 542 */ 543 | 443 int err; 444 z_stream d_stream; /* decompression stream */ 445 446 strcpy((char*)uncompr, "garbage"); 447 448 d_stream.zalloc = zalloc; 449 d_stream.zfree = zfree; 450 d_stream.opaque = (voidpf)0; --- 31 unchanged lines hidden (view full) --- 482 printf("inflate with dictionary: %s\n", (char *)uncompr); 483 } 484} 485 486/* =========================================================================== 487 * Usage: example [output.gz [input.gz]] 488 */ 489 |
544int main(argc, argv) 545 int argc; 546 char *argv[]; 547{ | 490int main(int argc, char *argv[]) { |
548 Byte *compr, *uncompr; | 491 Byte *compr, *uncompr; |
549 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */ 550 uLong uncomprLen = comprLen; | 492 uLong uncomprLen = 20000; 493 uLong comprLen = 3 * uncomprLen; |
551 static const char* myVersion = ZLIB_VERSION; 552 553 if (zlibVersion()[0] != myVersion[0]) { 554 fprintf(stderr, "incompatible zlib version\n"); 555 exit(1); 556 557 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { 558 fprintf(stderr, "warning: different zlib version linked: %s\n", --- 26 unchanged lines hidden (view full) --- 585 test_deflate(compr, comprLen); 586 test_inflate(compr, comprLen, uncompr, uncomprLen); 587 588 test_large_deflate(compr, comprLen, uncompr, uncomprLen); 589 test_large_inflate(compr, comprLen, uncompr, uncomprLen); 590 591 test_flush(compr, &comprLen); 592 test_sync(compr, comprLen, uncompr, uncomprLen); | 494 static const char* myVersion = ZLIB_VERSION; 495 496 if (zlibVersion()[0] != myVersion[0]) { 497 fprintf(stderr, "incompatible zlib version\n"); 498 exit(1); 499 500 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { 501 fprintf(stderr, "warning: different zlib version linked: %s\n", --- 26 unchanged lines hidden (view full) --- 528 test_deflate(compr, comprLen); 529 test_inflate(compr, comprLen, uncompr, uncomprLen); 530 531 test_large_deflate(compr, comprLen, uncompr, uncomprLen); 532 test_large_inflate(compr, comprLen, uncompr, uncomprLen); 533 534 test_flush(compr, &comprLen); 535 test_sync(compr, comprLen, uncompr, uncomprLen); |
593 comprLen = uncomprLen; | 536 comprLen = 3 * uncomprLen; |
594 595 test_dict_deflate(compr, comprLen); 596 test_dict_inflate(compr, comprLen, uncompr, uncomprLen); 597 598 free(compr); 599 free(uncompr); 600 601 return 0; 602} | 537 538 test_dict_deflate(compr, comprLen); 539 test_dict_inflate(compr, comprLen, uncompr, uncomprLen); 540 541 free(compr); 542 free(uncompr); 543 544 return 0; 545} |