Update contrib.
1 /* Portions Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
5 /* example.c -- usage example of the zlib compression library
6 * Copyright (C) 1995-1998 Jean-loup Gailly.
7 * For conditions of distribution and use, see copyright notice in zlib.h
20 extern void exit OF((int));
23 #if defined(VMS) || defined(RISCOS)
24 # define TESTFILE "foo-gz"
26 # define TESTFILE "foo.gz"
29 #define CHECK_ERR(err, msg) { \
31 fprintf(stderr, "%s error: %d\n", msg, err); \
36 const char hello[] = "hello, hello!";
37 /* "hello world" would be more standard, but the repeated "hello"
38 * stresses the compression code better, sorry...
41 const char dictionary[] = "hello";
42 uLong dictId; /* Adler32 value of the dictionary */
44 void test_compress OF((Byte *compr, uLong comprLen,
45 Byte *uncompr, uLong uncomprLen));
46 void test_deflate OF((Byte *compr, uLong comprLen));
47 void test_inflate OF((Byte *compr, uLong comprLen,
48 Byte *uncompr, uLong uncomprLen));
49 void test_large_deflate OF((Byte *compr, uLong comprLen,
50 Byte *uncompr, uLong uncomprLen));
51 void test_large_inflate OF((Byte *compr, uLong comprLen,
52 Byte *uncompr, uLong uncomprLen));
53 void test_flush OF((Byte *compr, uLong *comprLen));
54 void test_sync OF((Byte *compr, uLong comprLen,
55 Byte *uncompr, uLong uncomprLen));
56 void test_dict_deflate OF((Byte *compr, uLong comprLen));
57 void test_dict_inflate OF((Byte *compr, uLong comprLen,
58 Byte *uncompr, uLong uncomprLen));
59 int main OF((/*int argc, char *argv[]*/));
62 Test compress() and uncompress()
64 @SYMTestCaseID SYSLIB-EZLIB-CT-0820
65 @SYMTestCaseDesc Compression and decompression of a buffer test.
67 @SYMTestActions Compress and uncompress string "hello" and check for integrity of the operation done.
68 @SYMTestExpectedResults The test must not fail.
73 Byte *compr, uLong comprLen, Byte *uncompr,
77 uLong len = strlen(hello)+1;
79 err = compress(compr, &comprLen, (const Bytef*)hello, len);
80 CHECK_ERR(err, "compress");
82 strcpy((char*)uncompr, "garbage");
84 err = uncompress(uncompr, &uncomprLen, compr, comprLen);
85 CHECK_ERR(err, "uncompress");
87 if (strcmp((char*)uncompr, hello)) {
88 fprintf(stderr, "bad uncompress\n");
91 printf("uncompress(): %s\n", (char *)uncompr);
96 @SYMTestCaseID SYSLIB-EZLIB-CT-0821
97 @SYMTestCaseDesc Read and write of .gz files test
99 @SYMTestActions Tests deflate() with small buffers
100 @SYMTestExpectedResults The test must not fail.
108 z_stream c_stream; /* compression stream */
110 int len = strlen(hello)+1;
112 c_stream.zalloc = (alloc_func)0;
113 c_stream.zfree = (free_func)0;
114 c_stream.opaque = (voidpf)0;
116 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
117 CHECK_ERR(err, "deflateInit");
119 c_stream.next_in = (Bytef*)hello;
120 c_stream.next_out = compr;
122 while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
123 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
124 err = deflate(&c_stream, Z_NO_FLUSH);
125 CHECK_ERR(err, "deflate");
127 /* Finish the stream, still forcing small buffers: */
129 c_stream.avail_out = 1;
130 err = deflate(&c_stream, Z_FINISH);
131 if (err == Z_STREAM_END) break;
132 CHECK_ERR(err, "deflate");
135 err = deflateEnd(&c_stream);
136 CHECK_ERR(err, "deflateEnd");
140 @SYMTestCaseID SYSLIB-EZLIB-CT-0822
141 @SYMTestCaseDesc Read and write of .gz files test
142 @SYMTestPriority High
143 @SYMTestActions Tests inflate() with small buffers
144 @SYMTestExpectedResults The test must not fail.
149 Byte *compr, uLong comprLen, Byte *uncompr,
153 z_stream d_stream; /* decompression stream */
155 strcpy((char*)uncompr, "garbage");
157 d_stream.zalloc = (alloc_func)0;
158 d_stream.zfree = (free_func)0;
159 d_stream.opaque = (voidpf)0;
161 d_stream.next_in = compr;
162 d_stream.avail_in = 0;
163 d_stream.next_out = uncompr;
165 err = inflateInit(&d_stream);
166 CHECK_ERR(err, "inflateInit");
168 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
169 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
170 err = inflate(&d_stream, Z_NO_FLUSH);
171 if (err == Z_STREAM_END) break;
172 CHECK_ERR(err, "inflate");
175 err = inflateEnd(&d_stream);
176 CHECK_ERR(err, "inflateEnd");
178 if (strcmp((char*)uncompr, hello)) {
179 fprintf(stderr, "bad inflate\n");
182 printf("inflate(): %s\n", (char *)uncompr);
187 @SYMTestCaseID SYSLIB-EZLIB-CT-0823
188 @SYMTestCaseDesc Deflate with large buffers test
189 @SYMTestPriority High
190 @SYMTestActions Test deflate() with large buffers and dynamic change of compression level
191 @SYMTestExpectedResults The test must not fail.
195 void test_large_deflate(
196 Byte *compr, uLong comprLen, Byte *uncompr,
199 z_stream c_stream; /* compression stream */
202 c_stream.zalloc = (alloc_func)0;
203 c_stream.zfree = (free_func)0;
204 c_stream.opaque = (voidpf)0;
206 err = deflateInit(&c_stream, Z_BEST_SPEED);
207 CHECK_ERR(err, "deflateInit");
209 c_stream.next_out = compr;
210 c_stream.avail_out = (uInt)comprLen;
212 /* At this point, uncompr is still mostly zeroes, so it should compress
215 c_stream.next_in = uncompr;
216 c_stream.avail_in = (uInt)uncomprLen;
217 err = deflate(&c_stream, Z_NO_FLUSH);
218 CHECK_ERR(err, "deflate");
219 if (c_stream.avail_in != 0) {
220 fprintf(stderr, "deflate not greedy\n");
224 /* Feed in already compressed data and switch to no compression: */
225 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
226 c_stream.next_in = compr;
227 c_stream.avail_in = (uInt)comprLen/2;
228 err = deflate(&c_stream, Z_NO_FLUSH);
229 CHECK_ERR(err, "deflate");
231 /* Switch back to compressing mode: */
232 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
233 c_stream.next_in = uncompr;
234 c_stream.avail_in = (uInt)uncomprLen;
235 err = deflate(&c_stream, Z_NO_FLUSH);
236 CHECK_ERR(err, "deflate");
238 err = deflate(&c_stream, Z_FINISH);
239 if (err != Z_STREAM_END) {
240 fprintf(stderr, "deflate should report Z_STREAM_END\n");
243 err = deflateEnd(&c_stream);
244 CHECK_ERR(err, "deflateEnd");
248 @SYMTestCaseID SYSLIB-EZLIB-CT-0824
249 @SYMTestCaseDesc Inflate with large buffers test
250 @SYMTestPriority High
251 @SYMTestActions Tests inflate() with large buffers
252 @SYMTestExpectedResults The test must not fail.
256 void test_large_inflate(
257 Byte *compr, uLong comprLen, Byte *uncompr,
261 z_stream d_stream; /* decompression stream */
263 strcpy((char*)uncompr, "garbage");
265 d_stream.zalloc = (alloc_func)0;
266 d_stream.zfree = (free_func)0;
267 d_stream.opaque = (voidpf)0;
269 d_stream.next_in = compr;
270 d_stream.avail_in = (uInt)comprLen;
272 err = inflateInit(&d_stream);
273 CHECK_ERR(err, "inflateInit");
276 d_stream.next_out = uncompr; /* discard the output */
277 d_stream.avail_out = (uInt)uncomprLen;
278 err = inflate(&d_stream, Z_NO_FLUSH);
279 if (err == Z_STREAM_END) break;
280 CHECK_ERR(err, "large inflate");
283 err = inflateEnd(&d_stream);
284 CHECK_ERR(err, "inflateEnd");
286 if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
287 fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
290 printf("large_inflate(): OK\n");
295 @SYMTestCaseID SYSLIB-EZLIB-CT-0825
296 @SYMTestCaseDesc Test deflate() with full flush
297 @SYMTestPriority High
298 @SYMTestActions Test by flushing the output as random access is desired
299 @SYMTestExpectedResults The test must not fail.
307 z_stream c_stream; /* compression stream */
309 int len = strlen(hello)+1;
311 c_stream.zalloc = (alloc_func)0;
312 c_stream.zfree = (free_func)0;
313 c_stream.opaque = (voidpf)0;
315 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
316 CHECK_ERR(err, "deflateInit");
318 c_stream.next_in = (Bytef*)hello;
319 c_stream.next_out = compr;
320 c_stream.avail_in = 3;
321 c_stream.avail_out = (uInt)*comprLen;
322 err = deflate(&c_stream, Z_FULL_FLUSH);
323 CHECK_ERR(err, "deflate");
325 compr[3]++; /* force an error in first compressed block */
326 c_stream.avail_in = len - 3;
328 err = deflate(&c_stream, Z_FINISH);
329 if (err != Z_STREAM_END) {
330 CHECK_ERR(err, "deflate");
332 err = deflateEnd(&c_stream);
333 CHECK_ERR(err, "deflateEnd");
335 *comprLen = c_stream.total_out;
339 @SYMTestCaseID SYSLIB-EZLIB-CT-0826
340 @SYMTestCaseDesc Test the inflateSync() function
341 @SYMTestPriority High
342 @SYMTestActions Tests for reading of compressed data with damaged parts and check for errors.
343 @SYMTestExpectedResults The test must not fail.
348 Byte *compr, uLong comprLen, Byte *uncompr,
352 z_stream d_stream; /* decompression stream */
354 strcpy((char*)uncompr, "garbage");
356 d_stream.zalloc = (alloc_func)0;
357 d_stream.zfree = (free_func)0;
358 d_stream.opaque = (voidpf)0;
360 d_stream.next_in = compr;
361 d_stream.avail_in = 2; /* just read the zlib header */
363 err = inflateInit(&d_stream);
364 CHECK_ERR(err, "inflateInit");
366 d_stream.next_out = uncompr;
367 d_stream.avail_out = (uInt)uncomprLen;
369 inflate(&d_stream, Z_NO_FLUSH);
370 CHECK_ERR(err, "inflate");
372 d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
373 err = inflateSync(&d_stream); /* but skip the damaged part */
374 CHECK_ERR(err, "inflateSync");
376 err = inflate(&d_stream, Z_FINISH);
377 if (err != Z_DATA_ERROR) {
378 fprintf(stderr, "inflate should report DATA_ERROR\n");
379 /* Because of incorrect adler32 */
382 err = inflateEnd(&d_stream);
383 CHECK_ERR(err, "inflateEnd");
385 printf("after inflateSync(): hel%s\n", (char *)uncompr);
389 @SYMTestCaseID SYSLIB-EZLIB-CT-0827
390 @SYMTestCaseDesc Deflation functionality test
391 @SYMTestPriority High
392 @SYMTestActions Test deflate() function with preset dictionary
393 @SYMTestExpectedResults The test must not fail.
397 void test_dict_deflate(
401 z_stream c_stream; /* compression stream */
404 c_stream.zalloc = (alloc_func)0;
405 c_stream.zfree = (free_func)0;
406 c_stream.opaque = (voidpf)0;
408 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
409 CHECK_ERR(err, "deflateInit");
411 err = deflateSetDictionary(&c_stream,
412 (const Bytef*)dictionary, sizeof(dictionary));
413 CHECK_ERR(err, "deflateSetDictionary");
415 dictId = c_stream.adler;
416 c_stream.next_out = compr;
417 c_stream.avail_out = (uInt)comprLen;
419 c_stream.next_in = (Bytef*)hello;
420 c_stream.avail_in = (uInt)strlen(hello)+1;
422 err = deflate(&c_stream, Z_FINISH);
423 if (err != Z_STREAM_END) {
424 fprintf(stderr, "deflate should report Z_STREAM_END\n");
427 err = deflateEnd(&c_stream);
428 CHECK_ERR(err, "deflateEnd");
432 @SYMTestCaseID SYSLIB-EZLIB-CT-0828
433 @SYMTestCaseDesc Inflation functionality test
434 @SYMTestPriority High
435 @SYMTestActions Test inflate() with a preset dictionary
436 @SYMTestExpectedResults The test must not fail.
440 void test_dict_inflate(
441 Byte *compr, uLong comprLen, Byte *uncompr,
445 z_stream d_stream; /* decompression stream */
447 strcpy((char*)uncompr, "garbage");
449 d_stream.zalloc = (alloc_func)0;
450 d_stream.zfree = (free_func)0;
451 d_stream.opaque = (voidpf)0;
453 d_stream.next_in = compr;
454 d_stream.avail_in = (uInt)comprLen;
456 err = inflateInit(&d_stream);
457 CHECK_ERR(err, "inflateInit");
459 d_stream.next_out = uncompr;
460 d_stream.avail_out = (uInt)uncomprLen;
463 err = inflate(&d_stream, Z_NO_FLUSH);
464 if (err == Z_STREAM_END) break;
465 if (err == Z_NEED_DICT) {
466 if (d_stream.adler != dictId) {
467 fprintf(stderr, "unexpected dictionary");
470 err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
473 CHECK_ERR(err, "inflate with dict");
476 err = inflateEnd(&d_stream);
477 CHECK_ERR(err, "inflateEnd");
479 if (strcmp((char*)uncompr, hello)) {
480 fprintf(stderr, "bad inflate with dict\n");
483 printf("inflate with dictionary: %s\n", (char *)uncompr);
487 /* ===========================================================================
488 * Usage: example [output.gz [input.gz]]
495 Byte *compr, *uncompr;
496 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
497 uLong uncomprLen = comprLen;
498 static const char* myVersion = ZLIB_VERSION;
500 if (zlibVersion()[0] != myVersion[0]) {
501 fprintf(stderr, "incompatible zlib version\n");
504 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
505 fprintf(stderr, "warning: different zlib version\n");
508 compr = (Byte*)calloc((uInt)comprLen, 1);
509 uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
510 /* compr and uncompr are cleared to avoid reading uninitialized
511 * data and to ensure that uncompr compresses well.
513 if (compr == Z_NULL || uncompr == Z_NULL) {
514 printf("out of memory\n");
517 test_compress(compr, comprLen, uncompr, uncomprLen);
519 test_deflate(compr, comprLen);
520 test_inflate(compr, comprLen, uncompr, uncomprLen);
522 test_large_deflate(compr, comprLen, uncompr, uncomprLen);
523 test_large_inflate(compr, comprLen, uncompr, uncomprLen);
525 test_flush(compr, &comprLen);
526 test_sync(compr, comprLen, uncompr, uncomprLen);
527 comprLen = uncomprLen;
529 test_dict_deflate(compr, comprLen);
530 test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
533 return 0; /* to avoid warning */