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