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