Home | History | Annotate | Line # | Download | only in test
example.c revision 1.1.1.3.8.1
      1          1.1  christos /* example.c -- usage example of the zlib compression library
      2          1.1  christos  * Copyright (C) 1995-2006, 2011, 2016 Jean-loup Gailly
      3          1.1  christos  * For conditions of distribution and use, see copyright notice in zlib.h
      4          1.1  christos  */
      5          1.1  christos 
      6      1.1.1.3  christos /* @(#) Id */
      7          1.1  christos 
      8          1.1  christos #include "zlib.h"
      9          1.1  christos #include <stdio.h>
     10          1.1  christos 
     11          1.1  christos #ifdef STDC
     12          1.1  christos #  include <string.h>
     13          1.1  christos #  include <stdlib.h>
     14          1.1  christos #endif
     15          1.1  christos 
     16          1.1  christos #if defined(VMS) || defined(RISCOS)
     17          1.1  christos #  define TESTFILE "foo-gz"
     18          1.1  christos #else
     19          1.1  christos #  define TESTFILE "foo.gz"
     20          1.1  christos #endif
     21          1.1  christos 
     22          1.1  christos #define CHECK_ERR(err, msg) { \
     23          1.1  christos     if (err != Z_OK) { \
     24          1.1  christos         fprintf(stderr, "%s error: %d\n", msg, err); \
     25          1.1  christos         exit(1); \
     26          1.1  christos     } \
     27          1.1  christos }
     28          1.1  christos 
     29          1.1  christos static z_const char hello[] = "hello, hello!";
     30          1.1  christos /* "hello world" would be more standard, but the repeated "hello"
     31          1.1  christos  * stresses the compression code better, sorry...
     32          1.1  christos  */
     33          1.1  christos 
     34          1.1  christos static const char dictionary[] = "hello";
     35          1.1  christos static uLong dictId;    /* Adler32 value of the dictionary */
     36          1.1  christos 
     37          1.1  christos #ifdef Z_SOLO
     38          1.1  christos 
     39  1.1.1.3.8.1  perseant static void *myalloc(void *q, unsigned n, unsigned m) {
     40          1.1  christos     (void)q;
     41          1.1  christos     return calloc(n, m);
     42          1.1  christos }
     43          1.1  christos 
     44  1.1.1.3.8.1  perseant static void myfree(void *q, void *p) {
     45          1.1  christos     (void)q;
     46          1.1  christos     free(p);
     47          1.1  christos }
     48          1.1  christos 
     49          1.1  christos static alloc_func zalloc = myalloc;
     50          1.1  christos static free_func zfree = myfree;
     51          1.1  christos 
     52          1.1  christos #else /* !Z_SOLO */
     53          1.1  christos 
     54          1.1  christos static alloc_func zalloc = (alloc_func)0;
     55          1.1  christos static free_func zfree = (free_func)0;
     56          1.1  christos 
     57          1.1  christos /* ===========================================================================
     58          1.1  christos  * Test compress() and uncompress()
     59          1.1  christos  */
     60  1.1.1.3.8.1  perseant static void test_compress(Byte *compr, uLong comprLen, Byte *uncompr,
     61  1.1.1.3.8.1  perseant                    uLong uncomprLen) {
     62          1.1  christos     int err;
     63          1.1  christos     uLong len = (uLong)strlen(hello)+1;
     64          1.1  christos 
     65          1.1  christos     err = compress(compr, &comprLen, (const Bytef*)hello, len);
     66          1.1  christos     CHECK_ERR(err, "compress");
     67          1.1  christos 
     68          1.1  christos     strcpy((char*)uncompr, "garbage");
     69          1.1  christos 
     70          1.1  christos     err = uncompress(uncompr, &uncomprLen, compr, comprLen);
     71          1.1  christos     CHECK_ERR(err, "uncompress");
     72          1.1  christos 
     73          1.1  christos     if (strcmp((char*)uncompr, hello)) {
     74          1.1  christos         fprintf(stderr, "bad uncompress\n");
     75          1.1  christos         exit(1);
     76          1.1  christos     } else {
     77          1.1  christos         printf("uncompress(): %s\n", (char *)uncompr);
     78          1.1  christos     }
     79          1.1  christos }
     80          1.1  christos 
     81          1.1  christos /* ===========================================================================
     82          1.1  christos  * Test read/write of .gz files
     83          1.1  christos  */
     84  1.1.1.3.8.1  perseant static void test_gzio(const char *fname, Byte *uncompr, uLong uncomprLen) {
     85          1.1  christos #ifdef NO_GZCOMPRESS
     86          1.1  christos     fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
     87          1.1  christos #else
     88          1.1  christos     int err;
     89          1.1  christos     int len = (int)strlen(hello)+1;
     90          1.1  christos     gzFile file;
     91          1.1  christos     z_off_t pos;
     92          1.1  christos 
     93          1.1  christos     file = gzopen(fname, "wb");
     94          1.1  christos     if (file == NULL) {
     95          1.1  christos         fprintf(stderr, "gzopen error\n");
     96          1.1  christos         exit(1);
     97          1.1  christos     }
     98          1.1  christos     gzputc(file, 'h');
     99          1.1  christos     if (gzputs(file, "ello") != 4) {
    100          1.1  christos         fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
    101          1.1  christos         exit(1);
    102          1.1  christos     }
    103          1.1  christos     if (gzprintf(file, ", %s!", "hello") != 8) {
    104          1.1  christos         fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
    105          1.1  christos         exit(1);
    106          1.1  christos     }
    107          1.1  christos     gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    108          1.1  christos     gzclose(file);
    109          1.1  christos 
    110          1.1  christos     file = gzopen(fname, "rb");
    111          1.1  christos     if (file == NULL) {
    112          1.1  christos         fprintf(stderr, "gzopen error\n");
    113          1.1  christos         exit(1);
    114          1.1  christos     }
    115          1.1  christos     strcpy((char*)uncompr, "garbage");
    116          1.1  christos 
    117          1.1  christos     if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
    118          1.1  christos         fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
    119          1.1  christos         exit(1);
    120          1.1  christos     }
    121          1.1  christos     if (strcmp((char*)uncompr, hello)) {
    122          1.1  christos         fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
    123          1.1  christos         exit(1);
    124          1.1  christos     } else {
    125          1.1  christos         printf("gzread(): %s\n", (char*)uncompr);
    126          1.1  christos     }
    127          1.1  christos 
    128          1.1  christos     pos = gzseek(file, -8L, SEEK_CUR);
    129          1.1  christos     if (pos != 6 || gztell(file) != pos) {
    130          1.1  christos         fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
    131          1.1  christos                 (long)pos, (long)gztell(file));
    132          1.1  christos         exit(1);
    133          1.1  christos     }
    134          1.1  christos 
    135          1.1  christos     if (gzgetc(file) != ' ') {
    136          1.1  christos         fprintf(stderr, "gzgetc error\n");
    137          1.1  christos         exit(1);
    138          1.1  christos     }
    139          1.1  christos 
    140          1.1  christos     if (gzungetc(' ', file) != ' ') {
    141          1.1  christos         fprintf(stderr, "gzungetc error\n");
    142          1.1  christos         exit(1);
    143          1.1  christos     }
    144          1.1  christos 
    145          1.1  christos     gzgets(file, (char*)uncompr, (int)uncomprLen);
    146          1.1  christos     if (strlen((char*)uncompr) != 7) { /* " hello!" */
    147          1.1  christos         fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
    148          1.1  christos         exit(1);
    149          1.1  christos     }
    150          1.1  christos     if (strcmp((char*)uncompr, hello + 6)) {
    151          1.1  christos         fprintf(stderr, "bad gzgets after gzseek\n");
    152          1.1  christos         exit(1);
    153          1.1  christos     } else {
    154          1.1  christos         printf("gzgets() after gzseek: %s\n", (char*)uncompr);
    155          1.1  christos     }
    156          1.1  christos 
    157          1.1  christos     gzclose(file);
    158          1.1  christos #endif
    159          1.1  christos }
    160          1.1  christos 
    161          1.1  christos #endif /* Z_SOLO */
    162          1.1  christos 
    163          1.1  christos /* ===========================================================================
    164          1.1  christos  * Test deflate() with small buffers
    165          1.1  christos  */
    166  1.1.1.3.8.1  perseant static void test_deflate(Byte *compr, uLong comprLen) {
    167          1.1  christos     z_stream c_stream; /* compression stream */
    168          1.1  christos     int err;
    169          1.1  christos     uLong len = (uLong)strlen(hello)+1;
    170          1.1  christos 
    171          1.1  christos     c_stream.zalloc = zalloc;
    172          1.1  christos     c_stream.zfree = zfree;
    173          1.1  christos     c_stream.opaque = (voidpf)0;
    174          1.1  christos 
    175          1.1  christos     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
    176          1.1  christos     CHECK_ERR(err, "deflateInit");
    177          1.1  christos 
    178          1.1  christos     c_stream.next_in  = (z_const unsigned char *)hello;
    179          1.1  christos     c_stream.next_out = compr;
    180          1.1  christos 
    181          1.1  christos     while (c_stream.total_in != len && c_stream.total_out < comprLen) {
    182          1.1  christos         c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
    183          1.1  christos         err = deflate(&c_stream, Z_NO_FLUSH);
    184          1.1  christos         CHECK_ERR(err, "deflate");
    185          1.1  christos     }
    186          1.1  christos     /* Finish the stream, still forcing small buffers: */
    187          1.1  christos     for (;;) {
    188          1.1  christos         c_stream.avail_out = 1;
    189          1.1  christos         err = deflate(&c_stream, Z_FINISH);
    190          1.1  christos         if (err == Z_STREAM_END) break;
    191          1.1  christos         CHECK_ERR(err, "deflate");
    192          1.1  christos     }
    193          1.1  christos 
    194          1.1  christos     err = deflateEnd(&c_stream);
    195          1.1  christos     CHECK_ERR(err, "deflateEnd");
    196          1.1  christos }
    197          1.1  christos 
    198          1.1  christos /* ===========================================================================
    199          1.1  christos  * Test inflate() with small buffers
    200          1.1  christos  */
    201  1.1.1.3.8.1  perseant static void test_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
    202  1.1.1.3.8.1  perseant                   uLong uncomprLen) {
    203          1.1  christos     int err;
    204          1.1  christos     z_stream d_stream; /* decompression stream */
    205          1.1  christos 
    206          1.1  christos     strcpy((char*)uncompr, "garbage");
    207          1.1  christos 
    208          1.1  christos     d_stream.zalloc = zalloc;
    209          1.1  christos     d_stream.zfree = zfree;
    210          1.1  christos     d_stream.opaque = (voidpf)0;
    211          1.1  christos 
    212          1.1  christos     d_stream.next_in  = compr;
    213          1.1  christos     d_stream.avail_in = 0;
    214          1.1  christos     d_stream.next_out = uncompr;
    215          1.1  christos 
    216          1.1  christos     err = inflateInit(&d_stream);
    217          1.1  christos     CHECK_ERR(err, "inflateInit");
    218          1.1  christos 
    219          1.1  christos     while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
    220          1.1  christos         d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
    221          1.1  christos         err = inflate(&d_stream, Z_NO_FLUSH);
    222          1.1  christos         if (err == Z_STREAM_END) break;
    223          1.1  christos         CHECK_ERR(err, "inflate");
    224          1.1  christos     }
    225          1.1  christos 
    226          1.1  christos     err = inflateEnd(&d_stream);
    227          1.1  christos     CHECK_ERR(err, "inflateEnd");
    228          1.1  christos 
    229          1.1  christos     if (strcmp((char*)uncompr, hello)) {
    230          1.1  christos         fprintf(stderr, "bad inflate\n");
    231          1.1  christos         exit(1);
    232          1.1  christos     } else {
    233          1.1  christos         printf("inflate(): %s\n", (char *)uncompr);
    234          1.1  christos     }
    235          1.1  christos }
    236          1.1  christos 
    237          1.1  christos /* ===========================================================================
    238          1.1  christos  * Test deflate() with large buffers and dynamic change of compression level
    239          1.1  christos  */
    240  1.1.1.3.8.1  perseant static void test_large_deflate(Byte *compr, uLong comprLen, Byte *uncompr,
    241  1.1.1.3.8.1  perseant                         uLong uncomprLen) {
    242          1.1  christos     z_stream c_stream; /* compression stream */
    243          1.1  christos     int err;
    244          1.1  christos 
    245          1.1  christos     c_stream.zalloc = zalloc;
    246          1.1  christos     c_stream.zfree = zfree;
    247          1.1  christos     c_stream.opaque = (voidpf)0;
    248          1.1  christos 
    249          1.1  christos     err = deflateInit(&c_stream, Z_BEST_SPEED);
    250          1.1  christos     CHECK_ERR(err, "deflateInit");
    251          1.1  christos 
    252          1.1  christos     c_stream.next_out = compr;
    253          1.1  christos     c_stream.avail_out = (uInt)comprLen;
    254          1.1  christos 
    255          1.1  christos     /* At this point, uncompr is still mostly zeroes, so it should compress
    256          1.1  christos      * very well:
    257          1.1  christos      */
    258          1.1  christos     c_stream.next_in = uncompr;
    259          1.1  christos     c_stream.avail_in = (uInt)uncomprLen;
    260          1.1  christos     err = deflate(&c_stream, Z_NO_FLUSH);
    261          1.1  christos     CHECK_ERR(err, "deflate");
    262          1.1  christos     if (c_stream.avail_in != 0) {
    263          1.1  christos         fprintf(stderr, "deflate not greedy\n");
    264          1.1  christos         exit(1);
    265          1.1  christos     }
    266          1.1  christos 
    267          1.1  christos     /* Feed in already compressed data and switch to no compression: */
    268          1.1  christos     deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
    269          1.1  christos     c_stream.next_in = compr;
    270  1.1.1.3.8.1  perseant     c_stream.avail_in = (uInt)uncomprLen/2;
    271          1.1  christos     err = deflate(&c_stream, Z_NO_FLUSH);
    272          1.1  christos     CHECK_ERR(err, "deflate");
    273          1.1  christos 
    274          1.1  christos     /* Switch back to compressing mode: */
    275          1.1  christos     deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
    276          1.1  christos     c_stream.next_in = uncompr;
    277          1.1  christos     c_stream.avail_in = (uInt)uncomprLen;
    278          1.1  christos     err = deflate(&c_stream, Z_NO_FLUSH);
    279          1.1  christos     CHECK_ERR(err, "deflate");
    280          1.1  christos 
    281          1.1  christos     err = deflate(&c_stream, Z_FINISH);
    282          1.1  christos     if (err != Z_STREAM_END) {
    283          1.1  christos         fprintf(stderr, "deflate should report Z_STREAM_END\n");
    284          1.1  christos         exit(1);
    285          1.1  christos     }
    286          1.1  christos     err = deflateEnd(&c_stream);
    287          1.1  christos     CHECK_ERR(err, "deflateEnd");
    288          1.1  christos }
    289          1.1  christos 
    290          1.1  christos /* ===========================================================================
    291          1.1  christos  * Test inflate() with large buffers
    292          1.1  christos  */
    293  1.1.1.3.8.1  perseant static void test_large_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
    294  1.1.1.3.8.1  perseant                         uLong uncomprLen) {
    295          1.1  christos     int err;
    296          1.1  christos     z_stream d_stream; /* decompression stream */
    297          1.1  christos 
    298          1.1  christos     strcpy((char*)uncompr, "garbage");
    299          1.1  christos 
    300          1.1  christos     d_stream.zalloc = zalloc;
    301          1.1  christos     d_stream.zfree = zfree;
    302          1.1  christos     d_stream.opaque = (voidpf)0;
    303          1.1  christos 
    304          1.1  christos     d_stream.next_in  = compr;
    305          1.1  christos     d_stream.avail_in = (uInt)comprLen;
    306          1.1  christos 
    307          1.1  christos     err = inflateInit(&d_stream);
    308          1.1  christos     CHECK_ERR(err, "inflateInit");
    309          1.1  christos 
    310          1.1  christos     for (;;) {
    311          1.1  christos         d_stream.next_out = uncompr;            /* discard the output */
    312          1.1  christos         d_stream.avail_out = (uInt)uncomprLen;
    313          1.1  christos         err = inflate(&d_stream, Z_NO_FLUSH);
    314          1.1  christos         if (err == Z_STREAM_END) break;
    315          1.1  christos         CHECK_ERR(err, "large inflate");
    316          1.1  christos     }
    317          1.1  christos 
    318          1.1  christos     err = inflateEnd(&d_stream);
    319          1.1  christos     CHECK_ERR(err, "inflateEnd");
    320          1.1  christos 
    321  1.1.1.3.8.1  perseant     if (d_stream.total_out != 2*uncomprLen + uncomprLen/2) {
    322          1.1  christos         fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
    323          1.1  christos         exit(1);
    324          1.1  christos     } else {
    325          1.1  christos         printf("large_inflate(): OK\n");
    326          1.1  christos     }
    327          1.1  christos }
    328          1.1  christos 
    329          1.1  christos /* ===========================================================================
    330          1.1  christos  * Test deflate() with full flush
    331          1.1  christos  */
    332  1.1.1.3.8.1  perseant static void test_flush(Byte *compr, uLong *comprLen) {
    333          1.1  christos     z_stream c_stream; /* compression stream */
    334          1.1  christos     int err;
    335          1.1  christos     uInt len = (uInt)strlen(hello)+1;
    336          1.1  christos 
    337          1.1  christos     c_stream.zalloc = zalloc;
    338          1.1  christos     c_stream.zfree = zfree;
    339          1.1  christos     c_stream.opaque = (voidpf)0;
    340          1.1  christos 
    341          1.1  christos     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
    342          1.1  christos     CHECK_ERR(err, "deflateInit");
    343          1.1  christos 
    344          1.1  christos     c_stream.next_in  = (z_const unsigned char *)hello;
    345          1.1  christos     c_stream.next_out = compr;
    346          1.1  christos     c_stream.avail_in = 3;
    347          1.1  christos     c_stream.avail_out = (uInt)*comprLen;
    348          1.1  christos     err = deflate(&c_stream, Z_FULL_FLUSH);
    349          1.1  christos     CHECK_ERR(err, "deflate");
    350          1.1  christos 
    351          1.1  christos     compr[3]++; /* force an error in first compressed block */
    352          1.1  christos     c_stream.avail_in = len - 3;
    353          1.1  christos 
    354          1.1  christos     err = deflate(&c_stream, Z_FINISH);
    355          1.1  christos     if (err != Z_STREAM_END) {
    356          1.1  christos         CHECK_ERR(err, "deflate");
    357          1.1  christos     }
    358          1.1  christos     err = deflateEnd(&c_stream);
    359          1.1  christos     CHECK_ERR(err, "deflateEnd");
    360          1.1  christos 
    361          1.1  christos     *comprLen = c_stream.total_out;
    362          1.1  christos }
    363          1.1  christos 
    364          1.1  christos /* ===========================================================================
    365          1.1  christos  * Test inflateSync()
    366          1.1  christos  */
    367  1.1.1.3.8.1  perseant static void test_sync(Byte *compr, uLong comprLen, Byte *uncompr,
    368  1.1.1.3.8.1  perseant                       uLong uncomprLen) {
    369          1.1  christos     int err;
    370          1.1  christos     z_stream d_stream; /* decompression stream */
    371          1.1  christos 
    372          1.1  christos     strcpy((char*)uncompr, "garbage");
    373          1.1  christos 
    374          1.1  christos     d_stream.zalloc = zalloc;
    375          1.1  christos     d_stream.zfree = zfree;
    376          1.1  christos     d_stream.opaque = (voidpf)0;
    377          1.1  christos 
    378          1.1  christos     d_stream.next_in  = compr;
    379          1.1  christos     d_stream.avail_in = 2; /* just read the zlib header */
    380          1.1  christos 
    381          1.1  christos     err = inflateInit(&d_stream);
    382          1.1  christos     CHECK_ERR(err, "inflateInit");
    383          1.1  christos 
    384          1.1  christos     d_stream.next_out = uncompr;
    385          1.1  christos     d_stream.avail_out = (uInt)uncomprLen;
    386          1.1  christos 
    387          1.1  christos     err = inflate(&d_stream, Z_NO_FLUSH);
    388          1.1  christos     CHECK_ERR(err, "inflate");
    389          1.1  christos 
    390          1.1  christos     d_stream.avail_in = (uInt)comprLen-2;   /* read all compressed data */
    391          1.1  christos     err = inflateSync(&d_stream);           /* but skip the damaged part */
    392          1.1  christos     CHECK_ERR(err, "inflateSync");
    393          1.1  christos 
    394          1.1  christos     err = inflate(&d_stream, Z_FINISH);
    395      1.1.1.2  christos     if (err != Z_STREAM_END) {
    396      1.1.1.2  christos         fprintf(stderr, "inflate should report Z_STREAM_END\n");
    397          1.1  christos         exit(1);
    398          1.1  christos     }
    399          1.1  christos     err = inflateEnd(&d_stream);
    400          1.1  christos     CHECK_ERR(err, "inflateEnd");
    401          1.1  christos 
    402          1.1  christos     printf("after inflateSync(): hel%s\n", (char *)uncompr);
    403          1.1  christos }
    404          1.1  christos 
    405          1.1  christos /* ===========================================================================
    406          1.1  christos  * Test deflate() with preset dictionary
    407          1.1  christos  */
    408  1.1.1.3.8.1  perseant static void test_dict_deflate(Byte *compr, uLong comprLen) {
    409          1.1  christos     z_stream c_stream; /* compression stream */
    410          1.1  christos     int err;
    411          1.1  christos 
    412          1.1  christos     c_stream.zalloc = zalloc;
    413          1.1  christos     c_stream.zfree = zfree;
    414          1.1  christos     c_stream.opaque = (voidpf)0;
    415          1.1  christos 
    416          1.1  christos     err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
    417          1.1  christos     CHECK_ERR(err, "deflateInit");
    418          1.1  christos 
    419          1.1  christos     err = deflateSetDictionary(&c_stream,
    420          1.1  christos                 (const Bytef*)dictionary, (int)sizeof(dictionary));
    421          1.1  christos     CHECK_ERR(err, "deflateSetDictionary");
    422          1.1  christos 
    423          1.1  christos     dictId = c_stream.adler;
    424          1.1  christos     c_stream.next_out = compr;
    425          1.1  christos     c_stream.avail_out = (uInt)comprLen;
    426          1.1  christos 
    427          1.1  christos     c_stream.next_in = (z_const unsigned char *)hello;
    428          1.1  christos     c_stream.avail_in = (uInt)strlen(hello)+1;
    429          1.1  christos 
    430          1.1  christos     err = deflate(&c_stream, Z_FINISH);
    431          1.1  christos     if (err != Z_STREAM_END) {
    432          1.1  christos         fprintf(stderr, "deflate should report Z_STREAM_END\n");
    433          1.1  christos         exit(1);
    434          1.1  christos     }
    435          1.1  christos     err = deflateEnd(&c_stream);
    436          1.1  christos     CHECK_ERR(err, "deflateEnd");
    437          1.1  christos }
    438          1.1  christos 
    439          1.1  christos /* ===========================================================================
    440          1.1  christos  * Test inflate() with a preset dictionary
    441          1.1  christos  */
    442  1.1.1.3.8.1  perseant static void test_dict_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
    443  1.1.1.3.8.1  perseant                        uLong uncomprLen) {
    444          1.1  christos     int err;
    445          1.1  christos     z_stream d_stream; /* decompression stream */
    446          1.1  christos 
    447          1.1  christos     strcpy((char*)uncompr, "garbage");
    448          1.1  christos 
    449          1.1  christos     d_stream.zalloc = zalloc;
    450          1.1  christos     d_stream.zfree = zfree;
    451          1.1  christos     d_stream.opaque = (voidpf)0;
    452          1.1  christos 
    453          1.1  christos     d_stream.next_in  = compr;
    454          1.1  christos     d_stream.avail_in = (uInt)comprLen;
    455          1.1  christos 
    456          1.1  christos     err = inflateInit(&d_stream);
    457          1.1  christos     CHECK_ERR(err, "inflateInit");
    458          1.1  christos 
    459          1.1  christos     d_stream.next_out = uncompr;
    460          1.1  christos     d_stream.avail_out = (uInt)uncomprLen;
    461          1.1  christos 
    462          1.1  christos     for (;;) {
    463          1.1  christos         err = inflate(&d_stream, Z_NO_FLUSH);
    464          1.1  christos         if (err == Z_STREAM_END) break;
    465          1.1  christos         if (err == Z_NEED_DICT) {
    466          1.1  christos             if (d_stream.adler != dictId) {
    467          1.1  christos                 fprintf(stderr, "unexpected dictionary");
    468          1.1  christos                 exit(1);
    469          1.1  christos             }
    470          1.1  christos             err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
    471          1.1  christos                                        (int)sizeof(dictionary));
    472          1.1  christos         }
    473          1.1  christos         CHECK_ERR(err, "inflate with dict");
    474          1.1  christos     }
    475          1.1  christos 
    476          1.1  christos     err = inflateEnd(&d_stream);
    477          1.1  christos     CHECK_ERR(err, "inflateEnd");
    478          1.1  christos 
    479          1.1  christos     if (strcmp((char*)uncompr, hello)) {
    480          1.1  christos         fprintf(stderr, "bad inflate with dict\n");
    481          1.1  christos         exit(1);
    482          1.1  christos     } else {
    483          1.1  christos         printf("inflate with dictionary: %s\n", (char *)uncompr);
    484          1.1  christos     }
    485          1.1  christos }
    486          1.1  christos 
    487          1.1  christos /* ===========================================================================
    488          1.1  christos  * Usage:  example [output.gz  [input.gz]]
    489          1.1  christos  */
    490          1.1  christos 
    491  1.1.1.3.8.1  perseant int main(int argc, char *argv[]) {
    492          1.1  christos     Byte *compr, *uncompr;
    493  1.1.1.3.8.1  perseant     uLong uncomprLen = 20000;
    494  1.1.1.3.8.1  perseant     uLong comprLen = 3 * uncomprLen;
    495          1.1  christos     static const char* myVersion = ZLIB_VERSION;
    496          1.1  christos 
    497          1.1  christos     if (zlibVersion()[0] != myVersion[0]) {
    498          1.1  christos         fprintf(stderr, "incompatible zlib version\n");
    499          1.1  christos         exit(1);
    500          1.1  christos 
    501          1.1  christos     } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
    502      1.1.1.2  christos         fprintf(stderr, "warning: different zlib version linked: %s\n",
    503      1.1.1.2  christos                 zlibVersion());
    504          1.1  christos     }
    505          1.1  christos 
    506          1.1  christos     printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
    507          1.1  christos             ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
    508          1.1  christos 
    509          1.1  christos     compr    = (Byte*)calloc((uInt)comprLen, 1);
    510          1.1  christos     uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
    511          1.1  christos     /* compr and uncompr are cleared to avoid reading uninitialized
    512          1.1  christos      * data and to ensure that uncompr compresses well.
    513          1.1  christos      */
    514          1.1  christos     if (compr == Z_NULL || uncompr == Z_NULL) {
    515          1.1  christos         printf("out of memory\n");
    516          1.1  christos         exit(1);
    517          1.1  christos     }
    518          1.1  christos 
    519          1.1  christos #ifdef Z_SOLO
    520          1.1  christos     (void)argc;
    521          1.1  christos     (void)argv;
    522          1.1  christos #else
    523          1.1  christos     test_compress(compr, comprLen, uncompr, uncomprLen);
    524          1.1  christos 
    525          1.1  christos     test_gzio((argc > 1 ? argv[1] : TESTFILE),
    526          1.1  christos               uncompr, uncomprLen);
    527          1.1  christos #endif
    528          1.1  christos 
    529          1.1  christos     test_deflate(compr, comprLen);
    530          1.1  christos     test_inflate(compr, comprLen, uncompr, uncomprLen);
    531          1.1  christos 
    532          1.1  christos     test_large_deflate(compr, comprLen, uncompr, uncomprLen);
    533          1.1  christos     test_large_inflate(compr, comprLen, uncompr, uncomprLen);
    534          1.1  christos 
    535          1.1  christos     test_flush(compr, &comprLen);
    536          1.1  christos     test_sync(compr, comprLen, uncompr, uncomprLen);
    537  1.1.1.3.8.1  perseant     comprLen = 3 * uncomprLen;
    538          1.1  christos 
    539          1.1  christos     test_dict_deflate(compr, comprLen);
    540          1.1  christos     test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
    541          1.1  christos 
    542          1.1  christos     free(compr);
    543          1.1  christos     free(uncompr);
    544          1.1  christos 
    545          1.1  christos     return 0;
    546          1.1  christos }
    547