| 1 | /* example.c -- usage example of the zlib compression library
|
|---|
| 2 | * Copyright (C) 1995-2002 Jean-loup Gailly.
|
|---|
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /* @(#) $Id: example.c,v 1.1.26.1 2002/03/11 22:18:18 tromey Exp $ */
|
|---|
| 7 |
|
|---|
| 8 | #include <stdio.h>
|
|---|
| 9 | #include "zlib.h"
|
|---|
| 10 |
|
|---|
| 11 | #ifdef STDC
|
|---|
| 12 | # include <string.h>
|
|---|
| 13 | # include <stdlib.h>
|
|---|
| 14 | #else
|
|---|
| 15 | extern void exit OF((int));
|
|---|
| 16 | #endif
|
|---|
| 17 |
|
|---|
| 18 | #if defined(VMS) || defined(RISCOS)
|
|---|
| 19 | # define TESTFILE "foo-gz"
|
|---|
| 20 | #else
|
|---|
| 21 | # define TESTFILE "foo.gz"
|
|---|
| 22 | #endif
|
|---|
| 23 |
|
|---|
| 24 | #define CHECK_ERR(err, msg) { \
|
|---|
| 25 | if (err != Z_OK) { \
|
|---|
| 26 | fprintf(stderr, "%s error: %d\n", msg, err); \
|
|---|
| 27 | exit(1); \
|
|---|
| 28 | } \
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | const char hello[] = "hello, hello!";
|
|---|
| 32 | /* "hello world" would be more standard, but the repeated "hello"
|
|---|
| 33 | * stresses the compression code better, sorry...
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | const char dictionary[] = "hello";
|
|---|
| 37 | uLong dictId; /* Adler32 value of the dictionary */
|
|---|
| 38 |
|
|---|
| 39 | void test_compress OF((Byte *compr, uLong comprLen,
|
|---|
| 40 | Byte *uncompr, uLong uncomprLen));
|
|---|
| 41 | void test_gzio OF((const char *out, const char *in,
|
|---|
| 42 | Byte *uncompr, int uncomprLen));
|
|---|
| 43 | void test_deflate OF((Byte *compr, uLong comprLen));
|
|---|
| 44 | void test_inflate OF((Byte *compr, uLong comprLen,
|
|---|
| 45 | Byte *uncompr, uLong uncomprLen));
|
|---|
| 46 | void test_large_deflate OF((Byte *compr, uLong comprLen,
|
|---|
| 47 | Byte *uncompr, uLong uncomprLen));
|
|---|
| 48 | void test_large_inflate OF((Byte *compr, uLong comprLen,
|
|---|
| 49 | Byte *uncompr, uLong uncomprLen));
|
|---|
| 50 | void test_flush OF((Byte *compr, uLong *comprLen));
|
|---|
| 51 | void test_sync OF((Byte *compr, uLong comprLen,
|
|---|
| 52 | Byte *uncompr, uLong uncomprLen));
|
|---|
| 53 | void test_dict_deflate OF((Byte *compr, uLong comprLen));
|
|---|
| 54 | void test_dict_inflate OF((Byte *compr, uLong comprLen,
|
|---|
| 55 | Byte *uncompr, uLong uncomprLen));
|
|---|
| 56 | int main OF((int argc, char *argv[]));
|
|---|
| 57 |
|
|---|
| 58 | /* ===========================================================================
|
|---|
| 59 | * Test compress() and uncompress()
|
|---|
| 60 | */
|
|---|
| 61 | void test_compress(compr, comprLen, uncompr, uncomprLen)
|
|---|
| 62 | Byte *compr, *uncompr;
|
|---|
| 63 | uLong comprLen, uncomprLen;
|
|---|
| 64 | {
|
|---|
| 65 | int err;
|
|---|
| 66 | uLong len = strlen(hello)+1;
|
|---|
| 67 |
|
|---|
| 68 | err = compress(compr, &comprLen, (const Bytef*)hello, len);
|
|---|
| 69 | CHECK_ERR(err, "compress");
|
|---|
| 70 |
|
|---|
| 71 | strcpy((char*)uncompr, "garbage");
|
|---|
| 72 |
|
|---|
| 73 | err = uncompress(uncompr, &uncomprLen, compr, comprLen);
|
|---|
| 74 | CHECK_ERR(err, "uncompress");
|
|---|
| 75 |
|
|---|
| 76 | if (strcmp((char*)uncompr, hello)) {
|
|---|
| 77 | fprintf(stderr, "bad uncompress\n");
|
|---|
| 78 | exit(1);
|
|---|
| 79 | } else {
|
|---|
| 80 | printf("uncompress(): %s\n", (char *)uncompr);
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /* ===========================================================================
|
|---|
| 85 | * Test read/write of .gz files
|
|---|
| 86 | */
|
|---|
| 87 | void test_gzio(out, in, uncompr, uncomprLen)
|
|---|
| 88 | const char *out; /* compressed output file */
|
|---|
| 89 | const char *in; /* compressed input file */
|
|---|
| 90 | Byte *uncompr;
|
|---|
| 91 | int uncomprLen;
|
|---|
| 92 | {
|
|---|
| 93 | int err;
|
|---|
| 94 | int len = strlen(hello)+1;
|
|---|
| 95 | gzFile file;
|
|---|
| 96 | z_off_t pos;
|
|---|
| 97 |
|
|---|
| 98 | file = gzopen(out, "wb");
|
|---|
| 99 | if (file == NULL) {
|
|---|
| 100 | fprintf(stderr, "gzopen error\n");
|
|---|
| 101 | exit(1);
|
|---|
| 102 | }
|
|---|
| 103 | gzputc(file, 'h');
|
|---|
| 104 | if (gzputs(file, "ello") != 4) {
|
|---|
| 105 | fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
|
|---|
| 106 | exit(1);
|
|---|
| 107 | }
|
|---|
| 108 | if (gzprintf(file, ", %s!", "hello") != 8) {
|
|---|
| 109 | fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
|
|---|
| 110 | exit(1);
|
|---|
| 111 | }
|
|---|
| 112 | gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
|
|---|
| 113 | gzclose(file);
|
|---|
| 114 |
|
|---|
| 115 | file = gzopen(in, "rb");
|
|---|
| 116 | if (file == NULL) {
|
|---|
|
|---|