summaryrefslogtreecommitdiff
path: root/example.c
diff options
context:
space:
mode:
Diffstat (limited to 'example.c')
-rw-r--r--example.c81
1 files changed, 79 insertions, 2 deletions
diff --git a/example.c b/example.c
index 5b482b8..c1bac1e 100644
--- a/example.c
+++ b/example.c
@@ -3,7 +3,7 @@
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.6 1995/04/29 16:53:46 jloup Exp $ */ 6/* $Id: example.c,v 1.7 1995/05/01 16:57:22 jloup Exp $ */
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include "zlib.h" 9#include "zlib.h"
@@ -182,6 +182,81 @@ void test_inflate(compr)
182} 182}
183 183
184/* =========================================================================== 184/* ===========================================================================
185 * Test deflate() with full flush
186 */
187void test_flush(compr)
188 Byte compr[];
189{
190 z_stream c_stream; /* compression stream */
191 int err;
192 int len = strlen(hello)+1;
193
194 c_stream.zalloc = (alloc_func)0;
195 c_stream.zfree = (free_func)0;
196
197 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
198 CHECK_ERR(err, "deflateInit");
199
200 c_stream.next_in = (Byte*)hello;
201 c_stream.next_out = compr;
202 c_stream.avail_in = 3;
203 c_stream.avail_out = BUFLEN;
204 err = deflate(&c_stream, Z_FULL_FLUSH);
205 CHECK_ERR(err, "deflate");
206
207 compr[3]++; /* force an error in first compressed block */
208 c_stream.avail_in = len - 3;
209
210 err = deflate(&c_stream, Z_FINISH);
211 if (err != Z_STREAM_END) {
212 CHECK_ERR(err, "deflate");
213 }
214 err = deflateEnd(&c_stream);
215 CHECK_ERR(err, "deflateEnd");
216}
217
218/* ===========================================================================
219 * Test inflateSync()
220 */
221void test_sync(compr)
222 Byte compr[];
223{
224 local Byte uncompr[BUFLEN];
225 int err;
226 z_stream d_stream; /* decompression stream */
227
228 strcpy((char*)uncompr, "garbage");
229
230 d_stream.zalloc = (alloc_func)0;
231 d_stream.zfree = (free_func)0;
232
233 err = inflateInit(&d_stream);
234 CHECK_ERR(err, "inflateInit");
235
236 d_stream.next_in = compr;
237 d_stream.next_out = uncompr;
238 d_stream.avail_in = 2; /* just read the zlib header */
239 d_stream.avail_out = sizeof(uncompr);
240
241 inflate(&d_stream, Z_NO_FLUSH);
242 CHECK_ERR(err, "inflate");
243
244 d_stream.avail_in = BUFLEN-2; /* let inflate read all compressed data */
245 err = inflateSync(&d_stream); /* skip the damaged part */
246 CHECK_ERR(err, "inflateSync");
247
248 err = inflate(&d_stream, Z_FINISH);
249 if (err != Z_DATA_ERROR) {
250 fprintf(stderr, "inflate should report DATA_ERROR\n");
251 /* Because of incorrect adler32 */
252 }
253 err = inflateEnd(&d_stream);
254 CHECK_ERR(err, "inflateEnd");
255
256 printf("after inflateSync(): %s\n", uncompr);
257}
258
259/* ===========================================================================
185 * Usage: example [output.gz [input.gz]] 260 * Usage: example [output.gz [input.gz]]
186 */ 261 */
187 262
@@ -204,8 +279,10 @@ void main(argc, argv)
204 (argc > 2 ? argv[2] : "foo.gz")); 279 (argc > 2 ? argv[2] : "foo.gz"));
205 280
206 test_deflate(compr); 281 test_deflate(compr);
207
208 test_inflate(compr); 282 test_inflate(compr);
209 283
284 test_flush(compr);
285 test_sync(compr);
286
210 exit(0); 287 exit(0);
211} 288}