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