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