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