diff options
Diffstat (limited to '')
| -rw-r--r-- | example.c | 42 |
1 files changed, 26 insertions, 16 deletions
| @@ -3,11 +3,17 @@ | |||
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | /* $Id: example.c,v 1.5 1995/04/14 20:35:56 jloup Exp $ */ | 6 | /* $Id: example.c,v 1.6 1995/04/29 16:53:46 jloup Exp $ */ |
| 7 | 7 | ||
| 8 | #include <stdio.h> | 8 | #include <stdio.h> |
| 9 | #include "zlib.h" | 9 | #include "zlib.h" |
| 10 | 10 | ||
| 11 | #ifdef STDC | ||
| 12 | # include <string.h> | ||
| 13 | #endif | ||
| 14 | |||
| 15 | extern void exit __P((int)); | ||
| 16 | |||
| 11 | #define BUFLEN 4096 | 17 | #define BUFLEN 4096 |
| 12 | 18 | ||
| 13 | #define local static | 19 | #define local static |
| @@ -25,6 +31,12 @@ | |||
| 25 | 31 | ||
| 26 | char *hello = "hello world"; | 32 | char *hello = "hello world"; |
| 27 | 33 | ||
| 34 | void test_compress __P((void)); | ||
| 35 | void test_gzio __P((char *out, char *in)); | ||
| 36 | void test_deflate __P((Byte compr[])); | ||
| 37 | void test_inflate __P((Byte compr[])); | ||
| 38 | void main __P((int argc, char *argv[])); | ||
| 39 | |||
| 28 | /* =========================================================================== | 40 | /* =========================================================================== |
| 29 | * Test compress() and uncompress() | 41 | * Test compress() and uncompress() |
| 30 | */ | 42 | */ |
| @@ -37,15 +49,15 @@ void test_compress() | |||
| 37 | int err; | 49 | int err; |
| 38 | uLong len = strlen(hello)+1; | 50 | uLong len = strlen(hello)+1; |
| 39 | 51 | ||
| 40 | err = compress(compr, &comprLen, hello, len); | 52 | err = compress(compr, &comprLen, (Byte*)hello, len); |
| 41 | CHECK_ERR(err, "compress"); | 53 | CHECK_ERR(err, "compress"); |
| 42 | 54 | ||
| 43 | strcpy(uncompr, "garbage"); | 55 | strcpy((char*)uncompr, "garbage"); |
| 44 | 56 | ||
| 45 | err = uncompress(uncompr, &uncomprLen, compr, comprLen); | 57 | err = uncompress(uncompr, &uncomprLen, compr, comprLen); |
| 46 | CHECK_ERR(err, "uncompress"); | 58 | CHECK_ERR(err, "uncompress"); |
| 47 | 59 | ||
| 48 | if (strcmp(uncompr, hello)) { | 60 | if (strcmp((char*)uncompr, hello)) { |
| 49 | fprintf(stderr, "bad uncompress\n"); | 61 | fprintf(stderr, "bad uncompress\n"); |
| 50 | } else { | 62 | } else { |
| 51 | printf("uncompress(): %s\n", uncompr); | 63 | printf("uncompress(): %s\n", uncompr); |
| @@ -80,7 +92,7 @@ void test_gzio(out, in) | |||
| 80 | if (file == NULL) { | 92 | if (file == NULL) { |
| 81 | fprintf(stderr, "gzopen error\n"); | 93 | fprintf(stderr, "gzopen error\n"); |
| 82 | } | 94 | } |
| 83 | strcpy(uncompr, "garbage"); | 95 | strcpy((char*)uncompr, "garbage"); |
| 84 | 96 | ||
| 85 | uncomprLen = gzread(file, uncompr, uncomprLen); | 97 | uncomprLen = gzread(file, uncompr, uncomprLen); |
| 86 | if (uncomprLen != len) { | 98 | if (uncomprLen != len) { |
| @@ -88,7 +100,7 @@ void test_gzio(out, in) | |||
| 88 | } | 100 | } |
| 89 | gzclose(file); | 101 | gzclose(file); |
| 90 | 102 | ||
| 91 | if (strcmp(uncompr, hello)) { | 103 | if (strcmp((char*)uncompr, hello)) { |
| 92 | fprintf(stderr, "bad gzread\n"); | 104 | fprintf(stderr, "bad gzread\n"); |
| 93 | } else { | 105 | } else { |
| 94 | printf("gzread(): %s\n", uncompr); | 106 | printf("gzread(): %s\n", uncompr); |
| @@ -96,9 +108,9 @@ void test_gzio(out, in) | |||
| 96 | } | 108 | } |
| 97 | 109 | ||
| 98 | /* =========================================================================== | 110 | /* =========================================================================== |
| 99 | * Test deflate() with small buffers, return the compressed length. | 111 | * Test deflate() with small buffers |
| 100 | */ | 112 | */ |
| 101 | uLong test_deflate(compr) | 113 | void test_deflate(compr) |
| 102 | Byte compr[]; | 114 | Byte compr[]; |
| 103 | { | 115 | { |
| 104 | z_stream c_stream; /* compression stream */ | 116 | z_stream c_stream; /* compression stream */ |
| @@ -120,16 +132,15 @@ uLong test_deflate(compr) | |||
| 120 | CHECK_ERR(err, "deflate"); | 132 | CHECK_ERR(err, "deflate"); |
| 121 | } | 133 | } |
| 122 | /* Finish the stream, still forcing small buffers: */ | 134 | /* Finish the stream, still forcing small buffers: */ |
| 123 | do { | 135 | for (;;) { |
| 124 | c_stream.avail_out = 1; | 136 | c_stream.avail_out = 1; |
| 125 | err = deflate(&c_stream, Z_FINISH); | 137 | err = deflate(&c_stream, Z_FINISH); |
| 138 | if (err == Z_STREAM_END) break; | ||
| 126 | CHECK_ERR(err, "deflate"); | 139 | CHECK_ERR(err, "deflate"); |
| 127 | } while (c_stream.avail_out == 0); | 140 | } |
| 128 | 141 | ||
| 129 | err = deflateEnd(&c_stream); | 142 | err = deflateEnd(&c_stream); |
| 130 | CHECK_ERR(err, "deflateEnd"); | 143 | CHECK_ERR(err, "deflateEnd"); |
| 131 | |||
| 132 | return c_stream.total_out; | ||
| 133 | } | 144 | } |
| 134 | 145 | ||
| 135 | /* =========================================================================== | 146 | /* =========================================================================== |
| @@ -142,7 +153,7 @@ void test_inflate(compr) | |||
| 142 | int err; | 153 | int err; |
| 143 | z_stream d_stream; /* decompression stream */ | 154 | z_stream d_stream; /* decompression stream */ |
| 144 | 155 | ||
| 145 | strcpy(uncompr, "garbage"); | 156 | strcpy((char*)uncompr, "garbage"); |
| 146 | 157 | ||
| 147 | d_stream.zalloc = (alloc_func)0; | 158 | d_stream.zalloc = (alloc_func)0; |
| 148 | d_stream.zfree = (free_func)0; | 159 | d_stream.zfree = (free_func)0; |
| @@ -163,7 +174,7 @@ void test_inflate(compr) | |||
| 163 | err = inflateEnd(&d_stream); | 174 | err = inflateEnd(&d_stream); |
| 164 | CHECK_ERR(err, "inflateEnd"); | 175 | CHECK_ERR(err, "inflateEnd"); |
| 165 | 176 | ||
| 166 | if (strcmp(uncompr, hello)) { | 177 | if (strcmp((char*)uncompr, hello)) { |
| 167 | fprintf(stderr, "bad inflate\n"); | 178 | fprintf(stderr, "bad inflate\n"); |
| 168 | } else { | 179 | } else { |
| 169 | printf("inflate(): %s\n", uncompr); | 180 | printf("inflate(): %s\n", uncompr); |
| @@ -179,7 +190,6 @@ void main(argc, argv) | |||
| 179 | char *argv[]; | 190 | char *argv[]; |
| 180 | { | 191 | { |
| 181 | local Byte compr[BUFLEN]; | 192 | local Byte compr[BUFLEN]; |
| 182 | uLong comprLen; | ||
| 183 | 193 | ||
| 184 | if (zlib_version[0] != ZLIB_VERSION[0]) { | 194 | if (zlib_version[0] != ZLIB_VERSION[0]) { |
| 185 | fprintf(stderr, "incompatible zlib version\n"); | 195 | fprintf(stderr, "incompatible zlib version\n"); |
| @@ -193,7 +203,7 @@ void main(argc, argv) | |||
| 193 | test_gzio((argc > 1 ? argv[1] : "foo.gz"), | 203 | test_gzio((argc > 1 ? argv[1] : "foo.gz"), |
| 194 | (argc > 2 ? argv[2] : "foo.gz")); | 204 | (argc > 2 ? argv[2] : "foo.gz")); |
| 195 | 205 | ||
| 196 | comprLen = test_deflate(compr); | 206 | test_deflate(compr); |
| 197 | 207 | ||
| 198 | test_inflate(compr); | 208 | test_inflate(compr); |
| 199 | 209 | ||
