Back to index

tetex-bin  3.0
example.c
Go to the documentation of this file.
00001 /* example.c -- usage example of the zlib compression library
00002  * Copyright (C) 1995-2003 Jean-loup Gailly.
00003  * For conditions of distribution and use, see copyright notice in zlib.h
00004  */
00005 
00006 /* @(#) $Id$ */
00007 
00008 #include <stdio.h>
00009 #include "zlib.h"
00010 
00011 #ifdef STDC
00012 #  include <string.h>
00013 #  include <stdlib.h>
00014 #else
00015    extern void exit  OF((int));
00016 #endif
00017 
00018 #if defined(VMS) || defined(RISCOS)
00019 #  define TESTFILE "foo-gz"
00020 #else
00021 #  define TESTFILE "foo.gz"
00022 #endif
00023 
00024 #define CHECK_ERR(err, msg) { \
00025     if (err != Z_OK) { \
00026         fprintf(stderr, "%s error: %d\n", msg, err); \
00027         exit(1); \
00028     } \
00029 }
00030 
00031 const char hello[] = "hello, hello!";
00032 /* "hello world" would be more standard, but the repeated "hello"
00033  * stresses the compression code better, sorry...
00034  */
00035 
00036 const char dictionary[] = "hello";
00037 uLong dictId; /* Adler32 value of the dictionary */
00038 
00039 void test_compress      OF((Byte *compr, uLong comprLen,
00040                             Byte *uncompr, uLong uncomprLen));
00041 void test_gzio          OF((const char *fname,
00042                             Byte *uncompr, uLong uncomprLen));
00043 void test_deflate       OF((Byte *compr, uLong comprLen));
00044 void test_inflate       OF((Byte *compr, uLong comprLen,
00045                             Byte *uncompr, uLong uncomprLen));
00046 void test_large_deflate OF((Byte *compr, uLong comprLen,
00047                             Byte *uncompr, uLong uncomprLen));
00048 void test_large_inflate OF((Byte *compr, uLong comprLen,
00049                             Byte *uncompr, uLong uncomprLen));
00050 void test_flush         OF((Byte *compr, uLong *comprLen));
00051 void test_sync          OF((Byte *compr, uLong comprLen,
00052                             Byte *uncompr, uLong uncomprLen));
00053 void test_dict_deflate  OF((Byte *compr, uLong comprLen));
00054 void test_dict_inflate  OF((Byte *compr, uLong comprLen,
00055                             Byte *uncompr, uLong uncomprLen));
00056 int  main               OF((int argc, char *argv[]));
00057 
00058 /* ===========================================================================
00059  * Test compress() and uncompress()
00060  */
00061 void test_compress(compr, comprLen, uncompr, uncomprLen)
00062     Byte *compr, *uncompr;
00063     uLong comprLen, uncomprLen;
00064 {
00065     int err;
00066     uLong len = (uLong)strlen(hello)+1;
00067 
00068     err = compress(compr, &comprLen, (const Bytef*)hello, len);
00069     CHECK_ERR(err, "compress");
00070 
00071     strcpy((char*)uncompr, "garbage");
00072 
00073     err = uncompress(uncompr, &uncomprLen, compr, comprLen);
00074     CHECK_ERR(err, "uncompress");
00075 
00076     if (strcmp((char*)uncompr, hello)) {
00077         fprintf(stderr, "bad uncompress\n");
00078         exit(1);
00079     } else {
00080         printf("uncompress(): %s\n", (char *)uncompr);
00081     }
00082 }
00083 
00084 /* ===========================================================================
00085  * Test read/write of .gz files
00086  */
00087 void test_gzio(fname, uncompr, uncomprLen)
00088     const char *fname; /* compressed file name */
00089     Byte *uncompr;
00090     uLong uncomprLen;
00091 {
00092 #ifdef NO_GZCOMPRESS
00093     fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
00094 #else
00095     int err;
00096     int len = (int)strlen(hello)+1;
00097     gzFile file;
00098     z_off_t pos;
00099 
00100     file = gzopen(fname, "wb");
00101     if (file == NULL) {
00102         fprintf(stderr, "gzopen error\n");
00103         exit(1);
00104     }
00105     gzputc(file, 'h');
00106     if (gzputs(file, "ello") != 4) {
00107         fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
00108         exit(1);
00109     }
00110     if (gzprintf(file, ", %s!", "hello") != 8) {
00111         fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
00112         exit(1);
00113     }
00114     gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
00115     gzclose(file);
00116 
00117     file = gzopen(fname, "rb");
00118     if (file == NULL) {
00119         fprintf(stderr, "gzopen error\n");
00120         exit(1);
00121     }
00122     strcpy((char*)uncompr, "garbage");
00123 
00124     if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
00125         fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
00126         exit(1);
00127     }
00128     if (strcmp((char*)uncompr, hello)) {
00129         fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
00130         exit(1);
00131     } else {
00132         printf("gzread(): %s\n", (char*)uncompr);
00133     }
00134 
00135     pos = gzseek(file, -8L, SEEK_CUR);
00136     if (pos != 6 || gztell(file) != pos) {
00137         fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
00138                 (long)pos, (long)gztell(file));
00139         exit(1);
00140     }
00141 
00142     if (gzgetc(file) != ' ') {
00143         fprintf(stderr, "gzgetc error\n");
00144         exit(1);
00145     }
00146 
00147     if (gzungetc(' ', file) != ' ') {
00148         fprintf(stderr, "gzungetc error\n");
00149         exit(1);
00150     }
00151 
00152     gzgets(file, (char*)uncompr, (int)uncomprLen);
00153     if (strlen((char*)uncompr) != 7) { /* " hello!" */
00154         fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
00155         exit(1);
00156     }
00157     if (strcmp((char*)uncompr, hello + 6)) {
00158         fprintf(stderr, "bad gzgets after gzseek\n");
00159         exit(1);
00160     } else {
00161         printf("gzgets() after gzseek: %s\n", (char*)uncompr);
00162     }
00163 
00164     gzclose(file);
00165 #endif
00166 }
00167 
00168 /* ===========================================================================
00169  * Test deflate() with small buffers
00170  */
00171 void test_deflate(compr, comprLen)
00172     Byte *compr;
00173     uLong comprLen;
00174 {
00175     z_stream c_stream; /* compression stream */
00176     int err;
00177     uLong len = (uLong)strlen(hello)+1;
00178 
00179     c_stream.zalloc = (alloc_func)0;
00180     c_stream.zfree = (free_func)0;
00181     c_stream.opaque = (voidpf)0;
00182 
00183     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
00184     CHECK_ERR(err, "deflateInit");
00185 
00186     c_stream.next_in  = (Bytef*)hello;
00187     c_stream.next_out = compr;
00188 
00189     while (c_stream.total_in != len && c_stream.total_out < comprLen) {
00190         c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
00191         err = deflate(&c_stream, Z_NO_FLUSH);
00192         CHECK_ERR(err, "deflate");
00193     }
00194     /* Finish the stream, still forcing small buffers: */
00195     for (;;) {
00196         c_stream.avail_out = 1;
00197         err = deflate(&c_stream, Z_FINISH);
00198         if (err == Z_STREAM_END) break;
00199         CHECK_ERR(err, "deflate");
00200     }
00201 
00202     err = deflateEnd(&c_stream);
00203     CHECK_ERR(err, "deflateEnd");
00204 }
00205 
00206 /* ===========================================================================
00207  * Test inflate() with small buffers
00208  */
00209 void test_inflate(compr, comprLen, uncompr, uncomprLen)
00210     Byte *compr, *uncompr;
00211     uLong comprLen, uncomprLen;
00212 {
00213     int err;
00214     z_stream d_stream; /* decompression stream */
00215 
00216     strcpy((char*)uncompr, "garbage");
00217 
00218     d_stream.zalloc = (alloc_func)0;
00219     d_stream.zfree = (free_func)0;
00220     d_stream.opaque = (voidpf)0;
00221 
00222     d_stream.next_in  = compr;
00223     d_stream.avail_in = 0;
00224     d_stream.next_out = uncompr;
00225 
00226     err = inflateInit(&d_stream);
00227     CHECK_ERR(err, "inflateInit");
00228 
00229     while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
00230         d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
00231         err = inflate(&d_stream, Z_NO_FLUSH);
00232         if (err == Z_STREAM_END) break;
00233         CHECK_ERR(err, "inflate");
00234     }
00235 
00236     err = inflateEnd(&d_stream);
00237     CHECK_ERR(err, "inflateEnd");
00238 
00239     if (strcmp((char*)uncompr, hello)) {
00240         fprintf(stderr, "bad inflate\n");
00241         exit(1);
00242     } else {
00243         printf("inflate(): %s\n", (char *)uncompr);
00244     }
00245 }
00246 
00247 /* ===========================================================================
00248  * Test deflate() with large buffers and dynamic change of compression level
00249  */
00250 void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
00251     Byte *compr, *uncompr;
00252     uLong comprLen, uncomprLen;
00253 {
00254     z_stream c_stream; /* compression stream */
00255     int err;
00256 
00257     c_stream.zalloc = (alloc_func)0;
00258     c_stream.zfree = (free_func)0;
00259     c_stream.opaque = (voidpf)0;
00260 
00261     err = deflateInit(&c_stream, Z_BEST_SPEED);
00262     CHECK_ERR(err, "deflateInit");
00263 
00264     c_stream.next_out = compr;
00265     c_stream.avail_out = (uInt)comprLen;
00266 
00267     /* At this point, uncompr is still mostly zeroes, so it should compress
00268      * very well:
00269      */
00270     c_stream.next_in = uncompr;
00271     c_stream.avail_in = (uInt)uncomprLen;
00272     err = deflate(&c_stream, Z_NO_FLUSH);
00273     CHECK_ERR(err, "deflate");
00274     if (c_stream.avail_in != 0) {
00275         fprintf(stderr, "deflate not greedy\n");
00276         exit(1);
00277     }
00278 
00279     /* Feed in already compressed data and switch to no compression: */
00280     deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
00281     c_stream.next_in = compr;
00282     c_stream.avail_in = (uInt)comprLen/2;
00283     err = deflate(&c_stream, Z_NO_FLUSH);
00284     CHECK_ERR(err, "deflate");
00285 
00286     /* Switch back to compressing mode: */
00287     deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
00288     c_stream.next_in = uncompr;
00289     c_stream.avail_in = (uInt)uncomprLen;
00290     err = deflate(&c_stream, Z_NO_FLUSH);
00291     CHECK_ERR(err, "deflate");
00292 
00293     err = deflate(&c_stream, Z_FINISH);
00294     if (err != Z_STREAM_END) {
00295         fprintf(stderr, "deflate should report Z_STREAM_END\n");
00296         exit(1);
00297     }
00298     err = deflateEnd(&c_stream);
00299     CHECK_ERR(err, "deflateEnd");
00300 }
00301 
00302 /* ===========================================================================
00303  * Test inflate() with large buffers
00304  */
00305 void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
00306     Byte *compr, *uncompr;
00307     uLong comprLen, uncomprLen;
00308 {
00309     int err;
00310     z_stream d_stream; /* decompression stream */
00311 
00312     strcpy((char*)uncompr, "garbage");
00313 
00314     d_stream.zalloc = (alloc_func)0;
00315     d_stream.zfree = (free_func)0;
00316     d_stream.opaque = (voidpf)0;
00317 
00318     d_stream.next_in  = compr;
00319     d_stream.avail_in = (uInt)comprLen;
00320 
00321     err = inflateInit(&d_stream);
00322     CHECK_ERR(err, "inflateInit");
00323 
00324     for (;;) {
00325         d_stream.next_out = uncompr;            /* discard the output */
00326         d_stream.avail_out = (uInt)uncomprLen;
00327         err = inflate(&d_stream, Z_NO_FLUSH);
00328         if (err == Z_STREAM_END) break;
00329         CHECK_ERR(err, "large inflate");
00330     }
00331 
00332     err = inflateEnd(&d_stream);
00333     CHECK_ERR(err, "inflateEnd");
00334 
00335     if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
00336         fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
00337         exit(1);
00338     } else {
00339         printf("large_inflate(): OK\n");
00340     }
00341 }
00342 
00343 /* ===========================================================================
00344  * Test deflate() with full flush
00345  */
00346 void test_flush(compr, comprLen)
00347     Byte *compr;
00348     uLong *comprLen;
00349 {
00350     z_stream c_stream; /* compression stream */
00351     int err;
00352     uInt len = (uInt)strlen(hello)+1;
00353 
00354     c_stream.zalloc = (alloc_func)0;
00355     c_stream.zfree = (free_func)0;
00356     c_stream.opaque = (voidpf)0;
00357 
00358     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
00359     CHECK_ERR(err, "deflateInit");
00360 
00361     c_stream.next_in  = (Bytef*)hello;
00362     c_stream.next_out = compr;
00363     c_stream.avail_in = 3;
00364     c_stream.avail_out = (uInt)*comprLen;
00365     err = deflate(&c_stream, Z_FULL_FLUSH);
00366     CHECK_ERR(err, "deflate");
00367 
00368     compr[3]++; /* force an error in first compressed block */
00369     c_stream.avail_in = len - 3;
00370 
00371     err = deflate(&c_stream, Z_FINISH);
00372     if (err != Z_STREAM_END) {
00373         CHECK_ERR(err, "deflate");
00374     }
00375     err = deflateEnd(&c_stream);
00376     CHECK_ERR(err, "deflateEnd");
00377 
00378     *comprLen = c_stream.total_out;
00379 }
00380 
00381 /* ===========================================================================
00382  * Test inflateSync()
00383  */
00384 void test_sync(compr, comprLen, uncompr, uncomprLen)
00385     Byte *compr, *uncompr;
00386     uLong comprLen, uncomprLen;
00387 {
00388     int err;
00389     z_stream d_stream; /* decompression stream */
00390 
00391     strcpy((char*)uncompr, "garbage");
00392 
00393     d_stream.zalloc = (alloc_func)0;
00394     d_stream.zfree = (free_func)0;
00395     d_stream.opaque = (voidpf)0;
00396 
00397     d_stream.next_in  = compr;
00398     d_stream.avail_in = 2; /* just read the zlib header */
00399 
00400     err = inflateInit(&d_stream);
00401     CHECK_ERR(err, "inflateInit");
00402 
00403     d_stream.next_out = uncompr;
00404     d_stream.avail_out = (uInt)uncomprLen;
00405 
00406     inflate(&d_stream, Z_NO_FLUSH);
00407     CHECK_ERR(err, "inflate");
00408 
00409     d_stream.avail_in = (uInt)comprLen-2;   /* read all compressed data */
00410     err = inflateSync(&d_stream);           /* but skip the damaged part */
00411     CHECK_ERR(err, "inflateSync");
00412 
00413     err = inflate(&d_stream, Z_FINISH);
00414     if (err != Z_DATA_ERROR) {
00415         fprintf(stderr, "inflate should report DATA_ERROR\n");
00416         /* Because of incorrect adler32 */
00417         exit(1);
00418     }
00419     err = inflateEnd(&d_stream);
00420     CHECK_ERR(err, "inflateEnd");
00421 
00422     printf("after inflateSync(): hel%s\n", (char *)uncompr);
00423 }
00424 
00425 /* ===========================================================================
00426  * Test deflate() with preset dictionary
00427  */
00428 void test_dict_deflate(compr, comprLen)
00429     Byte *compr;
00430     uLong comprLen;
00431 {
00432     z_stream c_stream; /* compression stream */
00433     int err;
00434 
00435     c_stream.zalloc = (alloc_func)0;
00436     c_stream.zfree = (free_func)0;
00437     c_stream.opaque = (voidpf)0;
00438 
00439     err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
00440     CHECK_ERR(err, "deflateInit");
00441 
00442     err = deflateSetDictionary(&c_stream,
00443                                (const Bytef*)dictionary, sizeof(dictionary));
00444     CHECK_ERR(err, "deflateSetDictionary");
00445 
00446     dictId = c_stream.adler;
00447     c_stream.next_out = compr;
00448     c_stream.avail_out = (uInt)comprLen;
00449 
00450     c_stream.next_in = (Bytef*)hello;
00451     c_stream.avail_in = (uInt)strlen(hello)+1;
00452 
00453     err = deflate(&c_stream, Z_FINISH);
00454     if (err != Z_STREAM_END) {
00455         fprintf(stderr, "deflate should report Z_STREAM_END\n");
00456         exit(1);
00457     }
00458     err = deflateEnd(&c_stream);
00459     CHECK_ERR(err, "deflateEnd");
00460 }
00461 
00462 /* ===========================================================================
00463  * Test inflate() with a preset dictionary
00464  */
00465 void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
00466     Byte *compr, *uncompr;
00467     uLong comprLen, uncomprLen;
00468 {
00469     int err;
00470     z_stream d_stream; /* decompression stream */
00471 
00472     strcpy((char*)uncompr, "garbage");
00473 
00474     d_stream.zalloc = (alloc_func)0;
00475     d_stream.zfree = (free_func)0;
00476     d_stream.opaque = (voidpf)0;
00477 
00478     d_stream.next_in  = compr;
00479     d_stream.avail_in = (uInt)comprLen;
00480 
00481     err = inflateInit(&d_stream);
00482     CHECK_ERR(err, "inflateInit");
00483 
00484     d_stream.next_out = uncompr;
00485     d_stream.avail_out = (uInt)uncomprLen;
00486 
00487     for (;;) {
00488         err = inflate(&d_stream, Z_NO_FLUSH);
00489         if (err == Z_STREAM_END) break;
00490         if (err == Z_NEED_DICT) {
00491             if (d_stream.adler != dictId) {
00492                 fprintf(stderr, "unexpected dictionary");
00493                 exit(1);
00494             }
00495             err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
00496                                        sizeof(dictionary));
00497         }
00498         CHECK_ERR(err, "inflate with dict");
00499     }
00500 
00501     err = inflateEnd(&d_stream);
00502     CHECK_ERR(err, "inflateEnd");
00503 
00504     if (strcmp((char*)uncompr, hello)) {
00505         fprintf(stderr, "bad inflate with dict\n");
00506         exit(1);
00507     } else {
00508         printf("inflate with dictionary: %s\n", (char *)uncompr);
00509     }
00510 }
00511 
00512 /* ===========================================================================
00513  * Usage:  example [output.gz  [input.gz]]
00514  */
00515 
00516 int main(argc, argv)
00517     int argc;
00518     char *argv[];
00519 {
00520     Byte *compr, *uncompr;
00521     uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
00522     uLong uncomprLen = comprLen;
00523     static const char* myVersion = ZLIB_VERSION;
00524 
00525     if (zlibVersion()[0] != myVersion[0]) {
00526         fprintf(stderr, "incompatible zlib version\n");
00527         exit(1);
00528 
00529     } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
00530         fprintf(stderr, "warning: different zlib version\n");
00531     }
00532 
00533     printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
00534             ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
00535 
00536     compr    = (Byte*)calloc((uInt)comprLen, 1);
00537     uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
00538     /* compr and uncompr are cleared to avoid reading uninitialized
00539      * data and to ensure that uncompr compresses well.
00540      */
00541     if (compr == Z_NULL || uncompr == Z_NULL) {
00542         printf("out of memory\n");
00543         exit(1);
00544     }
00545     test_compress(compr, comprLen, uncompr, uncomprLen);
00546 
00547     test_gzio((argc > 1 ? argv[1] : TESTFILE),
00548               uncompr, uncomprLen);
00549 
00550     test_deflate(compr, comprLen);
00551     test_inflate(compr, comprLen, uncompr, uncomprLen);
00552 
00553     test_large_deflate(compr, comprLen, uncompr, uncomprLen);
00554     test_large_inflate(compr, comprLen, uncompr, uncomprLen);
00555 
00556     test_flush(compr, &comprLen);
00557     test_sync(compr, comprLen, uncompr, uncomprLen);
00558     comprLen = uncomprLen;
00559 
00560     test_dict_deflate(compr, comprLen);
00561     test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
00562 
00563     free(compr);
00564     free(uncompr);
00565 
00566     return 0;
00567 }