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