1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/rtest/example/example.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,534 @@
1.4 +/* Portions Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 + * All rights reserved.
1.6 + */
1.7 +
1.8 +/* example.c -- usage example of the zlib compression library
1.9 + * Copyright (C) 1995-1998 Jean-loup Gailly.
1.10 + * For conditions of distribution and use, see copyright notice in zlib.h
1.11 + */
1.12 +
1.13 +/* @(#) $Id$ */
1.14 +
1.15 +#include <stdio.h>
1.16 +#include <ezlib.h>
1.17 +
1.18 +
1.19 +#ifdef STDC
1.20 +# include <string.h>
1.21 +# include <stdlib.h>
1.22 +#else
1.23 + extern void exit OF((int));
1.24 +#endif
1.25 +
1.26 +#if defined(VMS) || defined(RISCOS)
1.27 +# define TESTFILE "foo-gz"
1.28 +#else
1.29 +# define TESTFILE "foo.gz"
1.30 +#endif
1.31 +
1.32 +#define CHECK_ERR(err, msg) { \
1.33 + if (err != Z_OK) { \
1.34 + fprintf(stderr, "%s error: %d\n", msg, err); \
1.35 + exit(1); \
1.36 + } \
1.37 +}
1.38 +
1.39 +const char hello[] = "hello, hello!";
1.40 +/* "hello world" would be more standard, but the repeated "hello"
1.41 + * stresses the compression code better, sorry...
1.42 + */
1.43 +
1.44 +const char dictionary[] = "hello";
1.45 +uLong dictId; /* Adler32 value of the dictionary */
1.46 +
1.47 +void test_compress OF((Byte *compr, uLong comprLen,
1.48 + Byte *uncompr, uLong uncomprLen));
1.49 +void test_deflate OF((Byte *compr, uLong comprLen));
1.50 +void test_inflate OF((Byte *compr, uLong comprLen,
1.51 + Byte *uncompr, uLong uncomprLen));
1.52 +void test_large_deflate OF((Byte *compr, uLong comprLen,
1.53 + Byte *uncompr, uLong uncomprLen));
1.54 +void test_large_inflate OF((Byte *compr, uLong comprLen,
1.55 + Byte *uncompr, uLong uncomprLen));
1.56 +void test_flush OF((Byte *compr, uLong *comprLen));
1.57 +void test_sync OF((Byte *compr, uLong comprLen,
1.58 + Byte *uncompr, uLong uncomprLen));
1.59 +void test_dict_deflate OF((Byte *compr, uLong comprLen));
1.60 +void test_dict_inflate OF((Byte *compr, uLong comprLen,
1.61 + Byte *uncompr, uLong uncomprLen));
1.62 +int main OF((/*int argc, char *argv[]*/));
1.63 +
1.64 +/**
1.65 +Test compress() and uncompress()
1.66 +
1.67 +@SYMTestCaseID SYSLIB-EZLIB-CT-0820
1.68 +@SYMTestCaseDesc Compression and decompression of a buffer test.
1.69 +@SYMTestPriority High
1.70 +@SYMTestActions Compress and uncompress string "hello" and check for integrity of the operation done.
1.71 +@SYMTestExpectedResults The test must not fail.
1.72 +@SYMREQ REQ0000
1.73 +*/
1.74 +
1.75 +void test_compress(
1.76 + Byte *compr, uLong comprLen, Byte *uncompr,
1.77 + uLong uncomprLen)
1.78 +{
1.79 + int err;
1.80 + uLong len = strlen(hello)+1;
1.81 +
1.82 + err = compress(compr, &comprLen, (const Bytef*)hello, len);
1.83 + CHECK_ERR(err, "compress");
1.84 +
1.85 + strcpy((char*)uncompr, "garbage");
1.86 +
1.87 + err = uncompress(uncompr, &uncomprLen, compr, comprLen);
1.88 + CHECK_ERR(err, "uncompress");
1.89 +
1.90 + if (strcmp((char*)uncompr, hello)) {
1.91 + fprintf(stderr, "bad uncompress\n");
1.92 + exit(1);
1.93 + } else {
1.94 + printf("uncompress(): %s\n", (char *)uncompr);
1.95 + }
1.96 +}
1.97 +
1.98 +/**
1.99 +@SYMTestCaseID SYSLIB-EZLIB-CT-0821
1.100 +@SYMTestCaseDesc Read and write of .gz files test
1.101 +@SYMTestPriority High
1.102 +@SYMTestActions Tests deflate() with small buffers
1.103 +@SYMTestExpectedResults The test must not fail.
1.104 +@SYMREQ REQ0000
1.105 +*/
1.106 +
1.107 +void test_deflate(
1.108 + Byte *compr,
1.109 + uLong comprLen)
1.110 +{
1.111 + z_stream c_stream; /* compression stream */
1.112 + int err;
1.113 + int len = strlen(hello)+1;
1.114 +
1.115 + c_stream.zalloc = (alloc_func)0;
1.116 + c_stream.zfree = (free_func)0;
1.117 + c_stream.opaque = (voidpf)0;
1.118 +
1.119 + err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
1.120 + CHECK_ERR(err, "deflateInit");
1.121 +
1.122 + c_stream.next_in = (Bytef*)hello;
1.123 + c_stream.next_out = compr;
1.124 +
1.125 + while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
1.126 + c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
1.127 + err = deflate(&c_stream, Z_NO_FLUSH);
1.128 + CHECK_ERR(err, "deflate");
1.129 + }
1.130 + /* Finish the stream, still forcing small buffers: */
1.131 + for (;;) {
1.132 + c_stream.avail_out = 1;
1.133 + err = deflate(&c_stream, Z_FINISH);
1.134 + if (err == Z_STREAM_END) break;
1.135 + CHECK_ERR(err, "deflate");
1.136 + }
1.137 +
1.138 + err = deflateEnd(&c_stream);
1.139 + CHECK_ERR(err, "deflateEnd");
1.140 +}
1.141 +
1.142 +/**
1.143 +@SYMTestCaseID SYSLIB-EZLIB-CT-0822
1.144 +@SYMTestCaseDesc Read and write of .gz files test
1.145 +@SYMTestPriority High
1.146 +@SYMTestActions Tests inflate() with small buffers
1.147 +@SYMTestExpectedResults The test must not fail.
1.148 +@SYMREQ REQ0000
1.149 +*/
1.150 +
1.151 +void test_inflate(
1.152 + Byte *compr, uLong comprLen, Byte *uncompr,
1.153 + uLong uncomprLen)
1.154 +{
1.155 + int err;
1.156 + z_stream d_stream; /* decompression stream */
1.157 +
1.158 + strcpy((char*)uncompr, "garbage");
1.159 +
1.160 + d_stream.zalloc = (alloc_func)0;
1.161 + d_stream.zfree = (free_func)0;
1.162 + d_stream.opaque = (voidpf)0;
1.163 +
1.164 + d_stream.next_in = compr;
1.165 + d_stream.avail_in = 0;
1.166 + d_stream.next_out = uncompr;
1.167 +
1.168 + err = inflateInit(&d_stream);
1.169 + CHECK_ERR(err, "inflateInit");
1.170 +
1.171 + while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
1.172 + d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
1.173 + err = inflate(&d_stream, Z_NO_FLUSH);
1.174 + if (err == Z_STREAM_END) break;
1.175 + CHECK_ERR(err, "inflate");
1.176 + }
1.177 +
1.178 + err = inflateEnd(&d_stream);
1.179 + CHECK_ERR(err, "inflateEnd");
1.180 +
1.181 + if (strcmp((char*)uncompr, hello)) {
1.182 + fprintf(stderr, "bad inflate\n");
1.183 + exit(1);
1.184 + } else {
1.185 + printf("inflate(): %s\n", (char *)uncompr);
1.186 + }
1.187 +}
1.188 +
1.189 +/**
1.190 +@SYMTestCaseID SYSLIB-EZLIB-CT-0823
1.191 +@SYMTestCaseDesc Deflate with large buffers test
1.192 +@SYMTestPriority High
1.193 +@SYMTestActions Test deflate() with large buffers and dynamic change of compression level
1.194 +@SYMTestExpectedResults The test must not fail.
1.195 +@SYMREQ REQ0000
1.196 +*/
1.197 +
1.198 +void test_large_deflate(
1.199 + Byte *compr, uLong comprLen, Byte *uncompr,
1.200 + uLong uncomprLen)
1.201 +{
1.202 + z_stream c_stream; /* compression stream */
1.203 + int err;
1.204 +
1.205 + c_stream.zalloc = (alloc_func)0;
1.206 + c_stream.zfree = (free_func)0;
1.207 + c_stream.opaque = (voidpf)0;
1.208 +
1.209 + err = deflateInit(&c_stream, Z_BEST_SPEED);
1.210 + CHECK_ERR(err, "deflateInit");
1.211 +
1.212 + c_stream.next_out = compr;
1.213 + c_stream.avail_out = (uInt)comprLen;
1.214 +
1.215 + /* At this point, uncompr is still mostly zeroes, so it should compress
1.216 + * very well:
1.217 + */
1.218 + c_stream.next_in = uncompr;
1.219 + c_stream.avail_in = (uInt)uncomprLen;
1.220 + err = deflate(&c_stream, Z_NO_FLUSH);
1.221 + CHECK_ERR(err, "deflate");
1.222 + if (c_stream.avail_in != 0) {
1.223 + fprintf(stderr, "deflate not greedy\n");
1.224 + exit(1);
1.225 + }
1.226 +
1.227 + /* Feed in already compressed data and switch to no compression: */
1.228 + deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
1.229 + c_stream.next_in = compr;
1.230 + c_stream.avail_in = (uInt)comprLen/2;
1.231 + err = deflate(&c_stream, Z_NO_FLUSH);
1.232 + CHECK_ERR(err, "deflate");
1.233 +
1.234 + /* Switch back to compressing mode: */
1.235 + deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
1.236 + c_stream.next_in = uncompr;
1.237 + c_stream.avail_in = (uInt)uncomprLen;
1.238 + err = deflate(&c_stream, Z_NO_FLUSH);
1.239 + CHECK_ERR(err, "deflate");
1.240 +
1.241 + err = deflate(&c_stream, Z_FINISH);
1.242 + if (err != Z_STREAM_END) {
1.243 + fprintf(stderr, "deflate should report Z_STREAM_END\n");
1.244 + exit(1);
1.245 + }
1.246 + err = deflateEnd(&c_stream);
1.247 + CHECK_ERR(err, "deflateEnd");
1.248 +}
1.249 +
1.250 +/**
1.251 +@SYMTestCaseID SYSLIB-EZLIB-CT-0824
1.252 +@SYMTestCaseDesc Inflate with large buffers test
1.253 +@SYMTestPriority High
1.254 +@SYMTestActions Tests inflate() with large buffers
1.255 +@SYMTestExpectedResults The test must not fail.
1.256 +@SYMREQ REQ0000
1.257 +*/
1.258 +
1.259 +void test_large_inflate(
1.260 + Byte *compr, uLong comprLen, Byte *uncompr,
1.261 + uLong uncomprLen)
1.262 +{
1.263 + int err;
1.264 + z_stream d_stream; /* decompression stream */
1.265 +
1.266 + strcpy((char*)uncompr, "garbage");
1.267 +
1.268 + d_stream.zalloc = (alloc_func)0;
1.269 + d_stream.zfree = (free_func)0;
1.270 + d_stream.opaque = (voidpf)0;
1.271 +
1.272 + d_stream.next_in = compr;
1.273 + d_stream.avail_in = (uInt)comprLen;
1.274 +
1.275 + err = inflateInit(&d_stream);
1.276 + CHECK_ERR(err, "inflateInit");
1.277 +
1.278 + for (;;) {
1.279 + d_stream.next_out = uncompr; /* discard the output */
1.280 + d_stream.avail_out = (uInt)uncomprLen;
1.281 + err = inflate(&d_stream, Z_NO_FLUSH);
1.282 + if (err == Z_STREAM_END) break;
1.283 + CHECK_ERR(err, "large inflate");
1.284 + }
1.285 +
1.286 + err = inflateEnd(&d_stream);
1.287 + CHECK_ERR(err, "inflateEnd");
1.288 +
1.289 + if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
1.290 + fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
1.291 + exit(1);
1.292 + } else {
1.293 + printf("large_inflate(): OK\n");
1.294 + }
1.295 +}
1.296 +
1.297 +/**
1.298 +@SYMTestCaseID SYSLIB-EZLIB-CT-0825
1.299 +@SYMTestCaseDesc Test deflate() with full flush
1.300 +@SYMTestPriority High
1.301 +@SYMTestActions Test by flushing the output as random access is desired
1.302 +@SYMTestExpectedResults The test must not fail.
1.303 +@SYMREQ REQ0000
1.304 +*/
1.305 +
1.306 +void test_flush(
1.307 + Byte *compr,
1.308 + uLong *comprLen)
1.309 +{
1.310 + z_stream c_stream; /* compression stream */
1.311 + int err;
1.312 + int len = strlen(hello)+1;
1.313 +
1.314 + c_stream.zalloc = (alloc_func)0;
1.315 + c_stream.zfree = (free_func)0;
1.316 + c_stream.opaque = (voidpf)0;
1.317 +
1.318 + err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
1.319 + CHECK_ERR(err, "deflateInit");
1.320 +
1.321 + c_stream.next_in = (Bytef*)hello;
1.322 + c_stream.next_out = compr;
1.323 + c_stream.avail_in = 3;
1.324 + c_stream.avail_out = (uInt)*comprLen;
1.325 + err = deflate(&c_stream, Z_FULL_FLUSH);
1.326 + CHECK_ERR(err, "deflate");
1.327 +
1.328 + compr[3]++; /* force an error in first compressed block */
1.329 + c_stream.avail_in = len - 3;
1.330 +
1.331 + err = deflate(&c_stream, Z_FINISH);
1.332 + if (err != Z_STREAM_END) {
1.333 + CHECK_ERR(err, "deflate");
1.334 + }
1.335 + err = deflateEnd(&c_stream);
1.336 + CHECK_ERR(err, "deflateEnd");
1.337 +
1.338 + *comprLen = c_stream.total_out;
1.339 +}
1.340 +
1.341 +/**
1.342 +@SYMTestCaseID SYSLIB-EZLIB-CT-0826
1.343 +@SYMTestCaseDesc Test the inflateSync() function
1.344 +@SYMTestPriority High
1.345 +@SYMTestActions Tests for reading of compressed data with damaged parts and check for errors.
1.346 +@SYMTestExpectedResults The test must not fail.
1.347 +@SYMREQ REQ0000
1.348 +*/
1.349 +
1.350 +void test_sync(
1.351 + Byte *compr, uLong comprLen, Byte *uncompr,
1.352 + uLong uncomprLen)
1.353 +{
1.354 + int err;
1.355 + z_stream d_stream; /* decompression stream */
1.356 +
1.357 + strcpy((char*)uncompr, "garbage");
1.358 +
1.359 + d_stream.zalloc = (alloc_func)0;
1.360 + d_stream.zfree = (free_func)0;
1.361 + d_stream.opaque = (voidpf)0;
1.362 +
1.363 + d_stream.next_in = compr;
1.364 + d_stream.avail_in = 2; /* just read the zlib header */
1.365 +
1.366 + err = inflateInit(&d_stream);
1.367 + CHECK_ERR(err, "inflateInit");
1.368 +
1.369 + d_stream.next_out = uncompr;
1.370 + d_stream.avail_out = (uInt)uncomprLen;
1.371 +
1.372 + inflate(&d_stream, Z_NO_FLUSH);
1.373 + CHECK_ERR(err, "inflate");
1.374 +
1.375 + d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
1.376 + err = inflateSync(&d_stream); /* but skip the damaged part */
1.377 + CHECK_ERR(err, "inflateSync");
1.378 +
1.379 + err = inflate(&d_stream, Z_FINISH);
1.380 + if (err != Z_DATA_ERROR) {
1.381 + fprintf(stderr, "inflate should report DATA_ERROR\n");
1.382 + /* Because of incorrect adler32 */
1.383 + exit(1);
1.384 + }
1.385 + err = inflateEnd(&d_stream);
1.386 + CHECK_ERR(err, "inflateEnd");
1.387 +
1.388 + printf("after inflateSync(): hel%s\n", (char *)uncompr);
1.389 +}
1.390 +
1.391 +/**
1.392 +@SYMTestCaseID SYSLIB-EZLIB-CT-0827
1.393 +@SYMTestCaseDesc Deflation functionality test
1.394 +@SYMTestPriority High
1.395 +@SYMTestActions Test deflate() function with preset dictionary
1.396 +@SYMTestExpectedResults The test must not fail.
1.397 +@SYMREQ REQ0000
1.398 +*/
1.399 +
1.400 +void test_dict_deflate(
1.401 + Byte *compr,
1.402 + uLong comprLen)
1.403 +{
1.404 + z_stream c_stream; /* compression stream */
1.405 + int err;
1.406 +
1.407 + c_stream.zalloc = (alloc_func)0;
1.408 + c_stream.zfree = (free_func)0;
1.409 + c_stream.opaque = (voidpf)0;
1.410 +
1.411 + err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
1.412 + CHECK_ERR(err, "deflateInit");
1.413 +
1.414 + err = deflateSetDictionary(&c_stream,
1.415 + (const Bytef*)dictionary, sizeof(dictionary));
1.416 + CHECK_ERR(err, "deflateSetDictionary");
1.417 +
1.418 + dictId = c_stream.adler;
1.419 + c_stream.next_out = compr;
1.420 + c_stream.avail_out = (uInt)comprLen;
1.421 +
1.422 + c_stream.next_in = (Bytef*)hello;
1.423 + c_stream.avail_in = (uInt)strlen(hello)+1;
1.424 +
1.425 + err = deflate(&c_stream, Z_FINISH);
1.426 + if (err != Z_STREAM_END) {
1.427 + fprintf(stderr, "deflate should report Z_STREAM_END\n");
1.428 + exit(1);
1.429 + }
1.430 + err = deflateEnd(&c_stream);
1.431 + CHECK_ERR(err, "deflateEnd");
1.432 +}
1.433 +
1.434 +/**
1.435 +@SYMTestCaseID SYSLIB-EZLIB-CT-0828
1.436 +@SYMTestCaseDesc Inflation functionality test
1.437 +@SYMTestPriority High
1.438 +@SYMTestActions Test inflate() with a preset dictionary
1.439 +@SYMTestExpectedResults The test must not fail.
1.440 +@SYMREQ REQ0000
1.441 +*/
1.442 +
1.443 +void test_dict_inflate(
1.444 + Byte *compr, uLong comprLen, Byte *uncompr,
1.445 + uLong uncomprLen)
1.446 +{
1.447 + int err;
1.448 + z_stream d_stream; /* decompression stream */
1.449 +
1.450 + strcpy((char*)uncompr, "garbage");
1.451 +
1.452 + d_stream.zalloc = (alloc_func)0;
1.453 + d_stream.zfree = (free_func)0;
1.454 + d_stream.opaque = (voidpf)0;
1.455 +
1.456 + d_stream.next_in = compr;
1.457 + d_stream.avail_in = (uInt)comprLen;
1.458 +
1.459 + err = inflateInit(&d_stream);
1.460 + CHECK_ERR(err, "inflateInit");
1.461 +
1.462 + d_stream.next_out = uncompr;
1.463 + d_stream.avail_out = (uInt)uncomprLen;
1.464 +
1.465 + for (;;) {
1.466 + err = inflate(&d_stream, Z_NO_FLUSH);
1.467 + if (err == Z_STREAM_END) break;
1.468 + if (err == Z_NEED_DICT) {
1.469 + if (d_stream.adler != dictId) {
1.470 + fprintf(stderr, "unexpected dictionary");
1.471 + exit(1);
1.472 + }
1.473 + err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
1.474 + sizeof(dictionary));
1.475 + }
1.476 + CHECK_ERR(err, "inflate with dict");
1.477 + }
1.478 +
1.479 + err = inflateEnd(&d_stream);
1.480 + CHECK_ERR(err, "inflateEnd");
1.481 +
1.482 + if (strcmp((char*)uncompr, hello)) {
1.483 + fprintf(stderr, "bad inflate with dict\n");
1.484 + exit(1);
1.485 + } else {
1.486 + printf("inflate with dictionary: %s\n", (char *)uncompr);
1.487 + }
1.488 +}
1.489 +
1.490 +/* ===========================================================================
1.491 + * Usage: example [output.gz [input.gz]]
1.492 + */
1.493 +
1.494 +int main(/*
1.495 + int argc,
1.496 + char *argv[]*/)
1.497 +{
1.498 + Byte *compr, *uncompr;
1.499 + uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
1.500 + uLong uncomprLen = comprLen;
1.501 + static const char* myVersion = ZLIB_VERSION;
1.502 +
1.503 + if (zlibVersion()[0] != myVersion[0]) {
1.504 + fprintf(stderr, "incompatible zlib version\n");
1.505 + exit(1);
1.506 +
1.507 + } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
1.508 + fprintf(stderr, "warning: different zlib version\n");
1.509 + }
1.510 +
1.511 + compr = (Byte*)calloc((uInt)comprLen, 1);
1.512 + uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
1.513 + /* compr and uncompr are cleared to avoid reading uninitialized
1.514 + * data and to ensure that uncompr compresses well.
1.515 + */
1.516 + if (compr == Z_NULL || uncompr == Z_NULL) {
1.517 + printf("out of memory\n");
1.518 + exit(1);
1.519 + }
1.520 + test_compress(compr, comprLen, uncompr, uncomprLen);
1.521 +
1.522 + test_deflate(compr, comprLen);
1.523 + test_inflate(compr, comprLen, uncompr, uncomprLen);
1.524 +
1.525 + test_large_deflate(compr, comprLen, uncompr, uncomprLen);
1.526 + test_large_inflate(compr, comprLen, uncompr, uncomprLen);
1.527 +
1.528 + test_flush(compr, &comprLen);
1.529 + test_sync(compr, comprLen, uncompr, uncomprLen);
1.530 + comprLen = uncomprLen;
1.531 +
1.532 + test_dict_deflate(compr, comprLen);
1.533 + test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
1.534 +
1.535 + exit(0);
1.536 + return 0; /* to avoid warning */
1.537 +}