Back to index

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