1139804Simp/* example.c -- usage example of the zlib compression library 248391Speter * Copyright (C) 1995-2006, 2011 Jean-loup Gailly. 348391Speter * For conditions of distribution and use, see copyright notice in zlib.h 448391Speter */ 548391Speter 648391Speter/* @(#) $Id$ */ 748391Speter 848391Speter#include "zlib.h" 948391Speter#include <stdio.h> 1048391Speter 1148391Speter#ifdef STDC 1248391Speter# include <string.h> 1348391Speter# include <stdlib.h> 1448391Speter#endif 1548391Speter 1648391Speter#if defined(VMS) || defined(RISCOS) 1748391Speter# define TESTFILE "foo-gz" 1848391Speter#else 1948391Speter# define TESTFILE "foo.gz" 2048391Speter#endif 2148391Speter 2248391Speter#define CHECK_ERR(err, msg) { \ 2348391Speter if (err != Z_OK) { \ 2448391Speter fprintf(stderr, "%s error: %d\n", msg, err); \ 2548391Speter exit(1); \ 2648391Speter } \ 27116182Sobrien} 28116182Sobrien 29116182Sobrienz_const char hello[] = "hello, hello!"; 3048391Speter/* "hello world" would be more standard, but the repeated "hello" 3148391Speter * stresses the compression code better, sorry... 3248391Speter */ 3370317Sjake 3474927Sjhbconst char dictionary[] = "hello"; 3574927SjhbuLong dictId; /* Adler32 value of the dictionary */ 3655722Simp 3755539Sluoqivoid test_deflate OF((Byte *compr, uLong comprLen)); 3874927Sjhbvoid test_inflate OF((Byte *compr, uLong comprLen, 3948391Speter Byte *uncompr, uLong uncomprLen)); 4048391Spetervoid test_large_deflate OF((Byte *compr, uLong comprLen, 41166188Sjeff Byte *uncompr, uLong uncomprLen)); 42173004Sjulianvoid test_large_inflate OF((Byte *compr, uLong comprLen, 43173004Sjulian Byte *uncompr, uLong uncomprLen)); 4448391Spetervoid test_flush OF((Byte *compr, uLong *comprLen)); 4548391Spetervoid test_sync OF((Byte *compr, uLong comprLen, 4648391Speter Byte *uncompr, uLong uncomprLen)); 4748391Spetervoid test_dict_deflate OF((Byte *compr, uLong comprLen)); 4848391Spetervoid test_dict_inflate OF((Byte *compr, uLong comprLen, 4948391Speter Byte *uncompr, uLong uncomprLen)); 5048391Speterint main OF((int argc, char *argv[])); 5148391Speter 5248391Speter 5348391Speter#ifdef Z_SOLO 5448391Speter 5548391Spetervoid *myalloc OF((void *, unsigned, unsigned)); 5648391Spetervoid myfree OF((void *, void *)); 5748391Speter 5848391Spetervoid *myalloc(q, n, m) 5948391Speter void *q; 6048391Speter unsigned n, m; 61172836Sjulian{ 62104354Sscottl q = Z_NULL; 6348391Speter return calloc(n, m); 6448391Speter} 6548391Speter 6648391Spetervoid myfree(void *q, void *p) 6748391Speter{ 6865557Sjasone q = Z_NULL; 6948391Speter free(p); 7065557Sjasone} 7165557Sjasone 7265557Sjasonestatic alloc_func zalloc = myalloc; 7365557Sjasonestatic free_func zfree = myfree; 7465557Sjasone 7565557Sjasone#else /* !Z_SOLO */ 7648391Speter 7748391Speterstatic alloc_func zalloc = (alloc_func)0; 78172836Sjulianstatic free_func zfree = (free_func)0; 79104354Sscottl 8048391Spetervoid test_compress OF((Byte *compr, uLong comprLen, 8148391Speter Byte *uncompr, uLong uncomprLen)); 8248391Spetervoid test_gzio OF((const char *fname, 83103216Sjulian Byte *uncompr, uLong uncomprLen)); 8448391Speter 8548391Speter/* =========================================================================== 86114434Sdes * Test compress() and uncompress() 87172836Sjulian */ 8865557Sjasonevoid test_compress(compr, comprLen, uncompr, uncomprLen) 8990375Speter Byte *compr, *uncompr; 90104354Sscottl uLong comprLen, uncomprLen; 9148391Speter{ 9248391Speter int err; 9348391Speter uLong len = (uLong)strlen(hello)+1; 9448391Speter 9548391Speter err = compress(compr, &comprLen, (const Bytef*)hello, len); 9648391Speter CHECK_ERR(err, "compress"); 9748391Speter 9848391Speter strcpy((char*)uncompr, "garbage"); 9971559Sjhb 100173004Sjulian err = uncompress(uncompr, &uncomprLen, compr, comprLen); 10171559Sjhb CHECK_ERR(err, "uncompress"); 102173004Sjulian 103114983Sjhb if (strcmp((char*)uncompr, hello)) { 104114983Sjhb fprintf(stderr, "bad uncompress\n"); 105114983Sjhb exit(1); 10671559Sjhb } else { 10748391Speter printf("uncompress(): %s\n", (char *)uncompr); 10848391Speter } 10948391Speter} 11048391Speter 11148391Speter/* =========================================================================== 112173004Sjulian * Test read/write of .gz files 113173004Sjulian */ 114173004Sjulianvoid test_gzio(fname, uncompr, uncomprLen) 115173004Sjulian const char *fname; /* compressed file name */ 11648391Speter Byte *uncompr; 11748391Speter uLong uncomprLen; 118103216Sjulian{ 119103216Sjulian#ifdef NO_GZCOMPRESS 12048391Speter fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n"); 12169657Sjhb#else 12269657Sjhb int err; 123170307Sjeff int len = (int)strlen(hello)+1; 124166188Sjeff gzFile file; 125170307Sjeff z_off_t pos; 12669657Sjhb 12769657Sjhb file = gzopen(fname, "wb"); 12848391Speter if (file == NULL) { 12948391Speter fprintf(stderr, "gzopen error\n"); 13048391Speter exit(1); 13148391Speter } 132172836Sjulian gzputc(file, 'h'); 13348391Speter if (gzputs(file, "ello") != 4) { 13486293Speter fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); 13586293Speter exit(1); 13670317Sjake } 13786293Speter if (gzprintf(file, ", %s!", "hello") != 8) { 13886293Speter fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); 139155400Sjhb exit(1); 140155400Sjhb } 141155400Sjhb gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ 142155400Sjhb gzclose(file); 143155400Sjhb 14474927Sjhb file = gzopen(fname, "rb"); 14586292Sdillon if (file == NULL) { 14686292Sdillon fprintf(stderr, "gzopen error\n"); 14786292Sdillon exit(1); 14874927Sjhb } 149155400Sjhb strcpy((char*)uncompr, "garbage"); 150155400Sjhb 151155400Sjhb if (gzread(file, uncompr, (unsigned)uncomprLen) != len) { 152155400Sjhb fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); 153155400Sjhb exit(1); 154155400Sjhb } 155155400Sjhb if (strcmp((char*)uncompr, hello)) { 15686293Speter fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); 15748391Speter exit(1); 15848391Speter } else { 15955539Sluoqi printf("gzread(): %s\n", (char*)uncompr); 16055539Sluoqi } 16155539Sluoqi 16255539Sluoqi pos = gzseek(file, -8L, SEEK_CUR); 16355539Sluoqi if (pos != 6 || gztell(file) != pos) { 164172836Sjulian fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", 16555539Sluoqi (long)pos, (long)gztell(file)); 16655539Sluoqi exit(1); 16755539Sluoqi } 168104306Sjmallett 16955539Sluoqi if (gzgetc(file) != ' ') { 17073911Sjhb fprintf(stderr, "gzgetc error\n"); 17173911Sjhb exit(1); 17273911Sjhb } 17355539Sluoqi 17473911Sjhb if (gzungetc(' ', file) != ' ') { 175104306Sjmallett fprintf(stderr, "gzungetc error\n"); 17688160Speter exit(1); 177173004Sjulian } 17855539Sluoqi 17955539Sluoqi gzgets(file, (char*)uncompr, (int)uncomprLen); 18055539Sluoqi if (strlen((char*)uncompr) != 7) { /* " hello!" */ 181172836Sjulian fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); 18255539Sluoqi exit(1); 18355539Sluoqi } 18455539Sluoqi if (strcmp((char*)uncompr, hello + 6)) { 18555539Sluoqi fprintf(stderr, "bad gzgets after gzseek\n"); 18655539Sluoqi exit(1); 18773911Sjhb } else { 18873911Sjhb printf("gzgets() after gzseek: %s\n", (char*)uncompr); 18973911Sjhb } 19055539Sluoqi 19173911Sjhb gzclose(file); 192104306Sjmallett#endif 19373911Sjhb} 194104306Sjmallett 19555539Sluoqi#endif /* Z_SOLO */ 19655539Sluoqi 19755539Sluoqi/* =========================================================================== 19855539Sluoqi * Test deflate() with small buffers 199172836Sjulian */ 20055539Sluoqivoid test_deflate(compr, comprLen) 20173911Sjhb Byte *compr; 202104306Sjmallett uLong comprLen; 203104306Sjmallett{ 204173004Sjulian z_stream c_stream; /* compression stream */ 20555539Sluoqi int err; 20673911Sjhb uLong len = (uLong)strlen(hello)+1; 20755539Sluoqi 208173004Sjulian c_stream.zalloc = zalloc; 209173004Sjulian c_stream.zfree = zfree; 210173004Sjulian c_stream.opaque = (voidpf)0; 211173004Sjulian 212173004Sjulian err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); 213173004Sjulian CHECK_ERR(err, "deflateInit"); 214173004Sjulian 215173004Sjulian c_stream.next_in = (z_const unsigned char *)hello; 216173004Sjulian c_stream.next_out = compr; 217173004Sjulian 218173004Sjulian while (c_stream.total_in != len && c_stream.total_out < comprLen) { 219173004Sjulian c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ 220173004Sjulian err = deflate(&c_stream, Z_NO_FLUSH); 221173004Sjulian CHECK_ERR(err, "deflate"); 222173004Sjulian } 223173004Sjulian /* Finish the stream, still forcing small buffers: */ 224173004Sjulian for (;;) { 225173004Sjulian c_stream.avail_out = 1; 226173004Sjulian err = deflate(&c_stream, Z_FINISH); 227173004Sjulian if (err == Z_STREAM_END) break; 228173004Sjulian CHECK_ERR(err, "deflate"); 229173004Sjulian } 230173004Sjulian 231173004Sjulian err = deflateEnd(&c_stream); 232173004Sjulian CHECK_ERR(err, "deflateEnd"); 233173004Sjulian} 234173004Sjulian 235173004Sjulian/* =========================================================================== 236173004Sjulian * Test inflate() with small buffers 237173004Sjulian */ 238173004Sjulianvoid test_inflate(compr, comprLen, uncompr, uncomprLen) 239173004Sjulian Byte *compr, *uncompr; 240173004Sjulian uLong comprLen, uncomprLen; 241173004Sjulian{ 242173004Sjulian int err; 243173004Sjulian z_stream d_stream; /* decompression stream */ 244173004Sjulian 245173004Sjulian strcpy((char*)uncompr, "garbage"); 246173004Sjulian 247173004Sjulian d_stream.zalloc = zalloc; 248173004Sjulian d_stream.zfree = zfree; 249173004Sjulian d_stream.opaque = (voidpf)0; 250173004Sjulian 251173004Sjulian d_stream.next_in = compr; 252173052Sjulian d_stream.avail_in = 0; 253173004Sjulian d_stream.next_out = uncompr; 254173004Sjulian 255173004Sjulian err = inflateInit(&d_stream); 256173004Sjulian CHECK_ERR(err, "inflateInit"); 257173052Sjulian 258173004Sjulian while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { 259173004Sjulian d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ 260173052Sjulian err = inflate(&d_stream, Z_NO_FLUSH); 261173004Sjulian if (err == Z_STREAM_END) break; 262173004Sjulian CHECK_ERR(err, "inflate"); 263173004Sjulian } 264173004Sjulian 265173004Sjulian err = inflateEnd(&d_stream); 266173004Sjulian CHECK_ERR(err, "inflateEnd"); 267173004Sjulian 268173004Sjulian if (strcmp((char*)uncompr, hello)) { 269173004Sjulian fprintf(stderr, "bad inflate\n"); 270173004Sjulian exit(1); 271173004Sjulian } else { 272173004Sjulian printf("inflate(): %s\n", (char *)uncompr); 273173004Sjulian } 274173004Sjulian} 275173004Sjulian 276173004Sjulian/* =========================================================================== 277173004Sjulian * Test deflate() with large buffers and dynamic change of compression level 278173004Sjulian */ 279173004Sjulianvoid test_large_deflate(compr, comprLen, uncompr, uncomprLen) 280173004Sjulian Byte *compr, *uncompr; 281173004Sjulian uLong comprLen, uncomprLen; 282173004Sjulian{ 283173004Sjulian z_stream c_stream; /* compression stream */ 284173004Sjulian int err; 285173004Sjulian 286173004Sjulian c_stream.zalloc = zalloc; 287173004Sjulian c_stream.zfree = zfree; 288173004Sjulian c_stream.opaque = (voidpf)0; 289173004Sjulian 290173004Sjulian err = deflateInit(&c_stream, Z_BEST_SPEED); 291173004Sjulian CHECK_ERR(err, "deflateInit"); 292173004Sjulian 293173004Sjulian c_stream.next_out = compr; 294173004Sjulian c_stream.avail_out = (uInt)comprLen; 295173004Sjulian 296173004Sjulian /* At this point, uncompr is still mostly zeroes, so it should compress 297173004Sjulian * very well: 298173004Sjulian */ 299173004Sjulian c_stream.next_in = uncompr; 300173004Sjulian c_stream.avail_in = (uInt)uncomprLen; 301173004Sjulian err = deflate(&c_stream, Z_NO_FLUSH); 302173004Sjulian CHECK_ERR(err, "deflate"); 303173004Sjulian if (c_stream.avail_in != 0) { 304173004Sjulian fprintf(stderr, "deflate not greedy\n"); 305173004Sjulian exit(1); 306173004Sjulian } 307173004Sjulian 308173004Sjulian /* Feed in already compressed data and switch to no compression: */ 309173004Sjulian deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); 310173004Sjulian c_stream.next_in = compr; 311173004Sjulian c_stream.avail_in = (uInt)comprLen/2; 312173004Sjulian err = deflate(&c_stream, Z_NO_FLUSH); 313173004Sjulian CHECK_ERR(err, "deflate"); 314173004Sjulian 315173004Sjulian /* Switch back to compressing mode: */ 316173004Sjulian deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); 317173004Sjulian c_stream.next_in = uncompr; 318173031Sjulian c_stream.avail_in = (uInt)uncomprLen; 319173004Sjulian err = deflate(&c_stream, Z_NO_FLUSH); 320173052Sjulian CHECK_ERR(err, "deflate"); 321173052Sjulian 322173046Sjulian err = deflate(&c_stream, Z_FINISH); 323173046Sjulian if (err != Z_STREAM_END) { 324173046Sjulian fprintf(stderr, "deflate should report Z_STREAM_END\n"); 325173046Sjulian exit(1); 326173046Sjulian } 327173052Sjulian err = deflateEnd(&c_stream); 328173004Sjulian CHECK_ERR(err, "deflateEnd"); 329173004Sjulian} 330173004Sjulian 331173004Sjulian/* =========================================================================== 332173004Sjulian * Test inflate() with large buffers 333173004Sjulian */ 334173004Sjulianvoid test_large_inflate(compr, comprLen, uncompr, uncomprLen) 335173004Sjulian Byte *compr, *uncompr; 336173004Sjulian uLong comprLen, uncomprLen; 337173004Sjulian{ 338173004Sjulian int err; 339173004Sjulian z_stream d_stream; /* decompression stream */ 340173004Sjulian 341173004Sjulian strcpy((char*)uncompr, "garbage"); 342173004Sjulian 343173004Sjulian d_stream.zalloc = zalloc; 344173004Sjulian d_stream.zfree = zfree; 345173004Sjulian d_stream.opaque = (voidpf)0; 346173004Sjulian 347173004Sjulian d_stream.next_in = compr; 348173004Sjulian d_stream.avail_in = (uInt)comprLen; 349173004Sjulian 350173004Sjulian err = inflateInit(&d_stream); 351173004Sjulian CHECK_ERR(err, "inflateInit"); 352173004Sjulian 353173004Sjulian for (;;) { 354173004Sjulian d_stream.next_out = uncompr; /* discard the output */ 355173004Sjulian d_stream.avail_out = (uInt)uncomprLen; 356173004Sjulian err = inflate(&d_stream, Z_NO_FLUSH); 357173004Sjulian if (err == Z_STREAM_END) break; 358173004Sjulian CHECK_ERR(err, "large inflate"); 359173004Sjulian } 360173004Sjulian 361173004Sjulian err = inflateEnd(&d_stream); 362173004Sjulian CHECK_ERR(err, "inflateEnd"); 363173004Sjulian 364173004Sjulian if (d_stream.total_out != 2*uncomprLen + comprLen/2) { 365173004Sjulian fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); 366173004Sjulian exit(1); 367173004Sjulian } else { 368173004Sjulian printf("large_inflate(): OK\n"); 369173004Sjulian } 370173004Sjulian} 371173004Sjulian 372173004Sjulian/* =========================================================================== 373173004Sjulian * Test deflate() with full flush 374173004Sjulian */ 375173004Sjulianvoid test_flush(compr, comprLen) 376173004Sjulian Byte *compr; 377173004Sjulian uLong *comprLen; 378173004Sjulian{ 379173004Sjulian z_stream c_stream; /* compression stream */ 380173004Sjulian int err; 381173004Sjulian uInt len = (uInt)strlen(hello)+1; 382173004Sjulian 383173004Sjulian c_stream.zalloc = zalloc; 384173004Sjulian c_stream.zfree = zfree; 385173004Sjulian c_stream.opaque = (voidpf)0; 386173004Sjulian 387173004Sjulian err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); 388173004Sjulian CHECK_ERR(err, "deflateInit"); 389173004Sjulian 390173004Sjulian c_stream.next_in = (z_const unsigned char *)hello; 391173004Sjulian c_stream.next_out = compr; 392173004Sjulian c_stream.avail_in = 3; 393173004Sjulian c_stream.avail_out = (uInt)*comprLen; 394173004Sjulian err = deflate(&c_stream, Z_FULL_FLUSH); 395173004Sjulian CHECK_ERR(err, "deflate"); 396173004Sjulian 397173004Sjulian compr[3]++; /* force an error in first compressed block */ 398173004Sjulian c_stream.avail_in = len - 3; 399173004Sjulian 400173004Sjulian err = deflate(&c_stream, Z_FINISH); 401173004Sjulian if (err != Z_STREAM_END) { 402173004Sjulian CHECK_ERR(err, "deflate"); 403173004Sjulian } 404173004Sjulian err = deflateEnd(&c_stream); 405173004Sjulian CHECK_ERR(err, "deflateEnd"); 406173004Sjulian 407173004Sjulian *comprLen = c_stream.total_out; 408173004Sjulian} 409173004Sjulian 410173004Sjulian/* =========================================================================== 411173004Sjulian * Test inflateSync() 412173004Sjulian */ 413173004Sjulianvoid test_sync(compr, comprLen, uncompr, uncomprLen) 414 Byte *compr, *uncompr; 415 uLong comprLen, uncomprLen; 416{ 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; 425 426 d_stream.next_in = compr; 427 d_stream.avail_in = 2; /* just read the zlib header */ 428 429 err = inflateInit(&d_stream); 430 CHECK_ERR(err, "inflateInit"); 431 432 d_stream.next_out = uncompr; 433 d_stream.avail_out = (uInt)uncomprLen; 434 435 inflate(&d_stream, Z_NO_FLUSH); 436 CHECK_ERR(err, "inflate"); 437 438 d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */ 439 err = inflateSync(&d_stream); /* but skip the damaged part */ 440 CHECK_ERR(err, "inflateSync"); 441 442 err = inflate(&d_stream, Z_FINISH); 443 if (err != Z_DATA_ERROR) { 444 fprintf(stderr, "inflate should report DATA_ERROR\n"); 445 /* Because of incorrect adler32 */ 446 exit(1); 447 } 448 err = inflateEnd(&d_stream); 449 CHECK_ERR(err, "inflateEnd"); 450 451 printf("after inflateSync(): hel%s\n", (char *)uncompr); 452} 453 454/* =========================================================================== 455 * Test deflate() with preset dictionary 456 */ 457void test_dict_deflate(compr, comprLen) 458 Byte *compr; 459 uLong comprLen; 460{ 461 z_stream c_stream; /* compression stream */ 462 int err; 463 464 c_stream.zalloc = zalloc; 465 c_stream.zfree = zfree; 466 c_stream.opaque = (voidpf)0; 467 468 err = deflateInit(&c_stream, Z_BEST_COMPRESSION); 469 CHECK_ERR(err, "deflateInit"); 470 471 err = deflateSetDictionary(&c_stream, 472 (const Bytef*)dictionary, (int)sizeof(dictionary)); 473 CHECK_ERR(err, "deflateSetDictionary"); 474 475 dictId = c_stream.adler; 476 c_stream.next_out = compr; 477 c_stream.avail_out = (uInt)comprLen; 478 479 c_stream.next_in = (z_const unsigned char *)hello; 480 c_stream.avail_in = (uInt)strlen(hello)+1; 481 482 err = deflate(&c_stream, Z_FINISH); 483 if (err != Z_STREAM_END) { 484 fprintf(stderr, "deflate should report Z_STREAM_END\n"); 485 exit(1); 486 } 487 err = deflateEnd(&c_stream); 488 CHECK_ERR(err, "deflateEnd"); 489} 490 491/* =========================================================================== 492 * Test inflate() with a preset dictionary 493 */ 494void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) 495 Byte *compr, *uncompr; 496 uLong comprLen, uncomprLen; 497{ 498 int err; 499 z_stream d_stream; /* decompression stream */ 500 501 strcpy((char*)uncompr, "garbage"); 502 503 d_stream.zalloc = zalloc; 504 d_stream.zfree = zfree; 505 d_stream.opaque = (voidpf)0; 506 507 d_stream.next_in = compr; 508 d_stream.avail_in = (uInt)comprLen; 509 510 err = inflateInit(&d_stream); 511 CHECK_ERR(err, "inflateInit"); 512 513 d_stream.next_out = uncompr; 514 d_stream.avail_out = (uInt)uncomprLen; 515 516 for (;;) { 517 err = inflate(&d_stream, Z_NO_FLUSH); 518 if (err == Z_STREAM_END) break; 519 if (err == Z_NEED_DICT) { 520 if (d_stream.adler != dictId) { 521 fprintf(stderr, "unexpected dictionary"); 522 exit(1); 523 } 524 err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, 525 (int)sizeof(dictionary)); 526 } 527 CHECK_ERR(err, "inflate with dict"); 528 } 529 530 err = inflateEnd(&d_stream); 531 CHECK_ERR(err, "inflateEnd"); 532 533 if (strcmp((char*)uncompr, hello)) { 534 fprintf(stderr, "bad inflate with dict\n"); 535 exit(1); 536 } else { 537 printf("inflate with dictionary: %s\n", (char *)uncompr); 538 } 539} 540 541/* =========================================================================== 542 * Usage: example [output.gz [input.gz]] 543 */ 544 545int main(argc, argv) 546 int argc; 547 char *argv[]; 548{ 549 Byte *compr, *uncompr; 550 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */ 551 uLong uncomprLen = comprLen; 552 static const char* myVersion = ZLIB_VERSION; 553 554 if (zlibVersion()[0] != myVersion[0]) { 555 fprintf(stderr, "incompatible zlib version\n"); 556 exit(1); 557 558 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { 559 fprintf(stderr, "warning: different zlib version\n"); 560 } 561 562 printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n", 563 ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags()); 564 565 compr = (Byte*)calloc((uInt)comprLen, 1); 566 uncompr = (Byte*)calloc((uInt)uncomprLen, 1); 567 /* compr and uncompr are cleared to avoid reading uninitialized 568 * data and to ensure that uncompr compresses well. 569 */ 570 if (compr == Z_NULL || uncompr == Z_NULL) { 571 printf("out of memory\n"); 572 exit(1); 573 } 574 575#ifdef Z_SOLO 576 argc = strlen(argv[0]); 577#else 578 test_compress(compr, comprLen, uncompr, uncomprLen); 579 580 test_gzio((argc > 1 ? argv[1] : TESTFILE), 581 uncompr, uncomprLen); 582#endif 583 584 test_deflate(compr, comprLen); 585 test_inflate(compr, comprLen, uncompr, uncomprLen); 586 587 test_large_deflate(compr, comprLen, uncompr, uncomprLen); 588 test_large_inflate(compr, comprLen, uncompr, uncomprLen); 589 590 test_flush(compr, &comprLen); 591 test_sync(compr, comprLen, uncompr, uncomprLen); 592 comprLen = uncomprLen; 593 594 test_dict_deflate(compr, comprLen); 595 test_dict_inflate(compr, comprLen, uncompr, uncomprLen); 596 597 free(compr); 598 free(uncompr); 599 600 return 0; 601} 602