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