annotate zlib/example.c @ 17:65de6eb6861a

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