Mercurial > ~darius > hgwebdir.cgi > paradise_client
comparison zlib/example.c @ 3:5a977ccbc7a9 default tip
Empty changelog
author | darius |
---|---|
date | Sat, 06 Dec 1997 05:41:29 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:fba0b6e6cdc7 | 3:5a977ccbc7a9 |
---|---|
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.1.1.1 1997/12/06 05:41:36 darius Exp $ */ | |
7 | |
8 #include <stdio.h> | |
9 #include "zlib.h" | |
10 | |
11 #ifdef STDC | |
12 # include <string.h> | |
13 #endif | |
14 | |
15 #ifndef __GO32__ | |
16 extern void exit __P((int)); | |
17 #endif | |
18 | |
19 #define BUFLEN 4096 | |
20 | |
21 #define local static | |
22 /* For MSDOS and other systems with limitation on stack size. For Unix, | |
23 #define local | |
24 works also. | |
25 */ | |
26 | |
27 #define CHECK_ERR(err, msg) { \ | |
28 if (err != Z_OK) { \ | |
29 fprintf(stderr, "%s error: %d\n", msg, err); \ | |
30 exit(1); \ | |
31 } \ | |
32 } | |
33 | |
34 char *hello = "hello world"; | |
35 | |
36 void test_compress __P((void)); | |
37 void test_gzio __P((char *out, char *in)); | |
38 void test_deflate __P((Byte compr[])); | |
39 void test_inflate __P((Byte compr[])); | |
40 void main __P((int argc, char *argv[])); | |
41 | |
42 /* =========================================================================== | |
43 * Test compress() and uncompress() | |
44 */ | |
45 void test_compress() | |
46 { | |
47 local Byte compr[BUFLEN]; | |
48 uLong comprLen = sizeof(compr); | |
49 local Byte uncompr[BUFLEN]; | |
50 uLong uncomprLen = sizeof(uncompr); | |
51 int err; | |
52 uLong len = strlen(hello)+1; | |
53 | |
54 err = compress(compr, &comprLen, (Byte*)hello, len); | |
55 CHECK_ERR(err, "compress"); | |
56 | |
57 strcpy((char*)uncompr, "garbage"); | |
58 | |
59 err = uncompress(uncompr, &uncomprLen, compr, comprLen); | |
60 CHECK_ERR(err, "uncompress"); | |
61 | |
62 if (strcmp((char*)uncompr, hello)) { | |
63 fprintf(stderr, "bad uncompress\n"); | |
64 } else { | |
65 printf("uncompress(): %s\n", uncompr); | |
66 } | |
67 } | |
68 | |
69 /* =========================================================================== | |
70 * Test read/write of .gz files | |
71 */ | |
72 void test_gzio(out, in) | |
73 char *out; /* output file */ | |
74 char *in; /* input file */ | |
75 { | |
76 local Byte uncompr[BUFLEN]; | |
77 int uncomprLen = sizeof(uncompr); | |
78 int err; | |
79 int len = strlen(hello)+1; | |
80 gzFile file; | |
81 | |
82 file = gzopen(out, "wb"); | |
83 if (file == NULL) { | |
84 fprintf(stderr, "gzopen error\n"); | |
85 exit(1); | |
86 } | |
87 | |
88 if (gzwrite(file, hello, len) != len) { | |
89 fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); | |
90 } | |
91 gzclose(file); | |
92 | |
93 file = gzopen(in, "rb"); | |
94 if (file == NULL) { | |
95 fprintf(stderr, "gzopen error\n"); | |
96 } | |
97 strcpy((char*)uncompr, "garbage"); | |
98 | |
99 uncomprLen = gzread(file, uncompr, uncomprLen); | |
100 if (uncomprLen != len) { | |
101 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); | |
102 } | |
103 gzclose(file); | |
104 | |
105 if (strcmp((char*)uncompr, hello)) { | |
106 fprintf(stderr, "bad gzread\n"); | |
107 } else { | |
108 printf("gzread(): %s\n", uncompr); | |
109 } | |
110 } | |
111 | |
112 /* =========================================================================== | |
113 * Test deflate() with small buffers | |
114 */ | |
115 void test_deflate(compr) | |
116 Byte compr[]; | |
117 { | |
118 z_stream c_stream; /* compression stream */ | |
119 int err; | |
120 int len = strlen(hello)+1; | |
121 | |
122 c_stream.zalloc = (alloc_func)0; | |
123 c_stream.zfree = (free_func)0; | |
124 | |
125 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); | |
126 CHECK_ERR(err, "deflateInit"); | |
127 | |
128 c_stream.next_in = (Byte*)hello; | |
129 c_stream.next_out = compr; | |
130 | |
131 while (c_stream.total_in != (uLong)len) { | |
132 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ | |
133 err = deflate(&c_stream, Z_NO_FLUSH); | |
134 CHECK_ERR(err, "deflate"); | |
135 } | |
136 /* Finish the stream, still forcing small buffers: */ | |
137 for (;;) { | |
138 c_stream.avail_out = 1; | |
139 err = deflate(&c_stream, Z_FINISH); | |
140 if (err == Z_STREAM_END) break; | |
141 CHECK_ERR(err, "deflate"); | |
142 } | |
143 | |
144 err = deflateEnd(&c_stream); | |
145 CHECK_ERR(err, "deflateEnd"); | |
146 } | |
147 | |
148 /* =========================================================================== | |
149 * Test inflate() with small buffers | |
150 */ | |
151 void test_inflate(compr) | |
152 Byte compr[]; | |
153 { | |
154 local Byte uncompr[BUFLEN]; | |
155 int err; | |
156 z_stream d_stream; /* decompression stream */ | |
157 | |
158 strcpy((char*)uncompr, "garbage"); | |
159 | |
160 d_stream.zalloc = (alloc_func)0; | |
161 d_stream.zfree = (free_func)0; | |
162 | |
163 err = inflateInit(&d_stream); | |
164 CHECK_ERR(err, "inflateInit"); | |
165 | |
166 d_stream.next_in = compr; | |
167 d_stream.next_out = uncompr; | |
168 | |
169 for (;;) { | |
170 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ | |
171 err = inflate(&d_stream, Z_NO_FLUSH); | |
172 if (err == Z_STREAM_END) break; | |
173 CHECK_ERR(err, "inflate"); | |
174 } | |
175 | |
176 err = inflateEnd(&d_stream); | |
177 CHECK_ERR(err, "inflateEnd"); | |
178 | |
179 if (strcmp((char*)uncompr, hello)) { | |
180 fprintf(stderr, "bad inflate\n"); | |
181 } else { | |
182 printf("inflate(): %s\n", uncompr); | |
183 } | |
184 } | |
185 | |
186 /* =========================================================================== | |
187 * Test deflate() with full flush | |
188 */ | |
189 void test_flush(compr) | |
190 Byte compr[]; | |
191 { | |
192 z_stream c_stream; /* compression stream */ | |
193 int err; | |
194 int len = strlen(hello)+1; | |
195 | |
196 c_stream.zalloc = (alloc_func)0; | |
197 c_stream.zfree = (free_func)0; | |
198 | |
199 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); | |
200 CHECK_ERR(err, "deflateInit"); | |
201 | |
202 c_stream.next_in = (Byte*)hello; | |
203 c_stream.next_out = compr; | |
204 c_stream.avail_in = 3; | |
205 c_stream.avail_out = BUFLEN; | |
206 err = deflate(&c_stream, Z_FULL_FLUSH); | |
207 CHECK_ERR(err, "deflate"); | |
208 | |
209 compr[3]++; /* force an error in first compressed block */ | |
210 c_stream.avail_in = len - 3; | |
211 | |
212 err = deflate(&c_stream, Z_FINISH); | |
213 if (err != Z_STREAM_END) { | |
214 CHECK_ERR(err, "deflate"); | |
215 } | |
216 err = deflateEnd(&c_stream); | |
217 CHECK_ERR(err, "deflateEnd"); | |
218 } | |
219 | |
220 /* =========================================================================== | |
221 * Test inflateSync() | |
222 */ | |
223 void test_sync(compr) | |
224 Byte compr[]; | |
225 { | |
226 local Byte uncompr[BUFLEN]; | |
227 int err; | |
228 z_stream d_stream; /* decompression stream */ | |
229 | |
230 strcpy((char*)uncompr, "garbage"); | |
231 | |
232 d_stream.zalloc = (alloc_func)0; | |
233 d_stream.zfree = (free_func)0; | |
234 | |
235 err = inflateInit(&d_stream); | |
236 CHECK_ERR(err, "inflateInit"); | |
237 | |
238 d_stream.next_in = compr; | |
239 d_stream.next_out = uncompr; | |
240 d_stream.avail_in = 2; /* just read the zlib header */ | |
241 d_stream.avail_out = sizeof(uncompr); | |
242 | |
243 inflate(&d_stream, Z_NO_FLUSH); | |
244 CHECK_ERR(err, "inflate"); | |
245 | |
246 d_stream.avail_in = BUFLEN-2; /* let inflate read all compressed data */ | |
247 err = inflateSync(&d_stream); /* skip the damaged part */ | |
248 CHECK_ERR(err, "inflateSync"); | |
249 | |
250 err = inflate(&d_stream, Z_FINISH); | |
251 if (err != Z_DATA_ERROR) { | |
252 fprintf(stderr, "inflate should report DATA_ERROR\n"); | |
253 /* Because of incorrect adler32 */ | |
254 } | |
255 err = inflateEnd(&d_stream); | |
256 CHECK_ERR(err, "inflateEnd"); | |
257 | |
258 printf("after inflateSync(): hel%s\n", uncompr); | |
259 } | |
260 | |
261 /* =========================================================================== | |
262 * Usage: example [output.gz [input.gz]] | |
263 */ | |
264 | |
265 void main(argc, argv) | |
266 int argc; | |
267 char *argv[]; | |
268 { | |
269 local Byte compr[BUFLEN]; | |
270 | |
271 if (zlib_version[0] != ZLIB_VERSION[0]) { | |
272 fprintf(stderr, "incompatible zlib version\n"); | |
273 exit(1); | |
274 | |
275 } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) { | |
276 fprintf(stderr, "warning: different zlib version\n"); | |
277 } | |
278 test_compress(); | |
279 | |
280 test_gzio((argc > 1 ? argv[1] : "foo.gz"), | |
281 (argc > 2 ? argv[2] : "foo.gz")); | |
282 | |
283 test_deflate(compr); | |
284 test_inflate(compr); | |
285 | |
286 test_flush(compr); | |
287 test_sync(compr); | |
288 | |
289 exit(0); | |
290 } |