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