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