annotate src/zlib-1.2.7/test/example.c @ 83:ae30d91d2ffe

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