comparison zlib/example.c @ 10:1040ca591f2e

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