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