diff options
Diffstat (limited to '')
| -rw-r--r-- | example.c | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/example.c b/example.c new file mode 100644 index 0000000..ea1a9eb --- /dev/null +++ b/example.c | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | /* example.c -- usage example of the zlib compression library | ||
| 2 | * Copyright (C) 1995 Jean-loup Gailly. | ||
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
| 4 | */ | ||
| 5 | |||
| 6 | /* $Id: example.c,v 1.4 1995/04/14 13:32:49 jloup Exp $ */ | ||
| 7 | |||
| 8 | #include <stdio.h> | ||
| 9 | #include "zlib.h" | ||
| 10 | |||
| 11 | #define BUFLEN 4096 | ||
| 12 | |||
| 13 | #define local static | ||
| 14 | /* For MSDOS and other systems with limitation on stack size. For Unix, | ||
| 15 | #define local | ||
| 16 | works also. | ||
| 17 | */ | ||
| 18 | |||
| 19 | #define CHECK_ERR(err, msg) { \ | ||
| 20 | if (err != Z_OK) { \ | ||
| 21 | fprintf(stderr, "%s error: %d\n", msg, err); \ | ||
| 22 | exit(1); \ | ||
| 23 | } \ | ||
| 24 | } | ||
| 25 | |||
| 26 | char *hello = "hello world"; | ||
| 27 | |||
| 28 | /* =========================================================================== | ||
| 29 | * Test compress() and uncompress() | ||
| 30 | */ | ||
| 31 | void test_compress() | ||
| 32 | { | ||
| 33 | local Byte compr[BUFLEN]; | ||
| 34 | uLong comprLen = sizeof(compr); | ||
| 35 | local Byte uncompr[BUFLEN]; | ||
| 36 | uLong uncomprLen = sizeof(uncompr); | ||
| 37 | int err; | ||
| 38 | uLong len = strlen(hello)+1; | ||
| 39 | |||
| 40 | err = compress(compr, &comprLen, hello, len); | ||
| 41 | CHECK_ERR(err, "compress"); | ||
| 42 | |||
| 43 | strcpy(uncompr, "garbage"); | ||
| 44 | |||
| 45 | err = uncompress(uncompr, &uncomprLen, compr, comprLen); | ||
| 46 | CHECK_ERR(err, "uncompress"); | ||
| 47 | |||
| 48 | if (strcmp(uncompr, hello)) { | ||
| 49 | fprintf(stderr, "bad uncompress\n"); | ||
| 50 | } else { | ||
| 51 | printf("uncompress(): %s\n", uncompr); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | /* =========================================================================== | ||
| 56 | * Test read/write of .gz files | ||
| 57 | */ | ||
| 58 | void test_gzio(out, in) | ||
| 59 | char *out; /* output file */ | ||
| 60 | char *in; /* input file */ | ||
| 61 | { | ||
| 62 | local Byte uncompr[BUFLEN]; | ||
| 63 | uLong uncomprLen = sizeof(uncompr); | ||
| 64 | int err; | ||
| 65 | int len = strlen(hello)+1; | ||
| 66 | gzFile file; | ||
| 67 | |||
| 68 | file = gzopen(out, "wb"); | ||
| 69 | if (file == NULL) { | ||
| 70 | fprintf(stderr, "gzopen error\n"); | ||
| 71 | exit(1); | ||
| 72 | } | ||
| 73 | |||
| 74 | if (gzwrite(file, hello, len) != len) { | ||
| 75 | fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); | ||
| 76 | } | ||
| 77 | gzclose(file); | ||
| 78 | |||
| 79 | file = gzopen(in, "rb"); | ||
| 80 | if (file == NULL) { | ||
| 81 | fprintf(stderr, "gzopen error\n"); | ||
| 82 | } | ||
| 83 | strcpy(uncompr, "garbage"); | ||
| 84 | |||
| 85 | uncomprLen = gzread(file, uncompr, uncomprLen); | ||
| 86 | if (uncomprLen != len) { | ||
| 87 | fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); | ||
| 88 | } | ||
| 89 | gzclose(file); | ||
| 90 | |||
| 91 | if (strcmp(uncompr, hello)) { | ||
| 92 | fprintf(stderr, "bad gzread\n"); | ||
| 93 | } else { | ||
| 94 | printf("gzread(): %s\n", uncompr); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | /* =========================================================================== | ||
| 99 | * Test deflate() with small buffers, return the compressed length. | ||
| 100 | */ | ||
| 101 | uLong test_deflate(compr) | ||
| 102 | Byte compr[]; | ||
| 103 | { | ||
| 104 | z_stream c_stream; /* compression stream */ | ||
| 105 | int err; | ||
| 106 | int len = strlen(hello)+1; | ||
| 107 | |||
| 108 | c_stream.zalloc = (alloc_func)0; | ||
| 109 | c_stream.zfree = (free_func)0; | ||
| 110 | |||
| 111 | err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); | ||
| 112 | CHECK_ERR(err, "deflateInit"); | ||
| 113 | |||
| 114 | c_stream.next_in = (Byte*)hello; | ||
| 115 | c_stream.next_out = compr; | ||
| 116 | |||
| 117 | while (c_stream.total_in != len) { | ||
| 118 | c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ | ||
| 119 | err = deflate(&c_stream, Z_NO_FLUSH); | ||
| 120 | CHECK_ERR(err, "deflate"); | ||
| 121 | } | ||
| 122 | /* Finish the stream, still forcing small buffers: */ | ||
| 123 | do { | ||
| 124 | c_stream.avail_out = 1; | ||
| 125 | err = deflate(&c_stream, Z_FINISH); | ||
| 126 | CHECK_ERR(err, "deflate"); | ||
| 127 | } while (c_stream.avail_out == 0); | ||
| 128 | |||
| 129 | err = deflateEnd(&c_stream); | ||
| 130 | CHECK_ERR(err, "deflateEnd"); | ||
| 131 | |||
| 132 | return c_stream.total_out; | ||
| 133 | } | ||
| 134 | |||
| 135 | /* =========================================================================== | ||
| 136 | * Test inflate() with small buffers | ||
| 137 | */ | ||
| 138 | void test_inflate(compr) | ||
| 139 | Byte compr[]; | ||
| 140 | { | ||
| 141 | local Byte uncompr[BUFLEN]; | ||
| 142 | int err; | ||
| 143 | z_stream d_stream; /* decompression stream */ | ||
| 144 | |||
| 145 | strcpy(uncompr, "garbage"); | ||
| 146 | |||
| 147 | d_stream.zalloc = (alloc_func)0; | ||
| 148 | d_stream.zfree = (free_func)0; | ||
| 149 | |||
| 150 | err = inflateInit(&d_stream); | ||
| 151 | CHECK_ERR(err, "inflateInit"); | ||
| 152 | |||
| 153 | d_stream.next_in = compr; | ||
| 154 | d_stream.next_out = uncompr; | ||
| 155 | |||
| 156 | for (;;) { | ||
| 157 | d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ | ||
| 158 | err = inflate(&d_stream, Z_NO_FLUSH); | ||
| 159 | if (err == Z_STREAM_END) break; | ||
| 160 | CHECK_ERR(err, "inflate"); | ||
| 161 | } | ||
| 162 | |||
| 163 | err = inflateEnd(&d_stream); | ||
| 164 | CHECK_ERR(err, "inflateEnd"); | ||
| 165 | |||
| 166 | if (strcmp(uncompr, hello)) { | ||
| 167 | fprintf(stderr, "bad inflate\n"); | ||
| 168 | } else { | ||
| 169 | printf("inflate(): %s\n", uncompr); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | /* =========================================================================== | ||
| 174 | * Usage: example [output.gz [input.gz]] | ||
| 175 | */ | ||
| 176 | |||
| 177 | void main(argc, argv) | ||
| 178 | int argc; | ||
| 179 | char *argv[]; | ||
| 180 | { | ||
| 181 | local Byte compr[BUFLEN]; | ||
| 182 | uLong comprLen; | ||
| 183 | |||
| 184 | if (zlib_version[0] != ZLIB_VERSION[0]) { | ||
| 185 | fprintf(stderr, "incompatible zlib version\n"); | ||
| 186 | exit(1); | ||
| 187 | |||
| 188 | } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) { | ||
| 189 | fprintf(stderr, "warning: different zlib version\n"); | ||
| 190 | } | ||
| 191 | test_compress(); | ||
| 192 | |||
| 193 | test_gzio((argc > 1 ? argv[1] : "foo.gz"), | ||
| 194 | (argc > 2 ? argv[2] : "foo.gz")); | ||
| 195 | |||
| 196 | comprLen = test_deflate(compr); | ||
| 197 | |||
| 198 | test_inflate(compr); | ||
| 199 | |||
| 200 | exit(0); | ||
| 201 | } | ||
