summaryrefslogtreecommitdiff
path: root/example.c
diff options
context:
space:
mode:
Diffstat (limited to 'example.c')
-rw-r--r--example.c58
1 files changed, 38 insertions, 20 deletions
diff --git a/example.c b/example.c
index 5e4dc6d..7349dec 100644
--- a/example.c
+++ b/example.c
@@ -1,9 +1,9 @@
1/* example.c -- usage example of the zlib compression library 1/* example.c -- usage example of the zlib compression library
2 * Copyright (C) 1995-1996 Jean-loup Gailly. 2 * Copyright (C) 1995-1998 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: example.c,v 1.16 1996/05/23 17:11:28 me Exp $ */ 6/* @(#) $Id$ */
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include "zlib.h" 9#include "zlib.h"
@@ -86,16 +86,17 @@ void test_gzio(out, in, uncompr, uncomprLen)
86 int err; 86 int err;
87 int len = strlen(hello)+1; 87 int len = strlen(hello)+1;
88 gzFile file; 88 gzFile file;
89 z_off_t pos;
89 90
90 file = gzopen(out, "wb"); 91 file = gzopen(out, "wb");
91 if (file == NULL) { 92 if (file == NULL) {
92 fprintf(stderr, "gzopen error\n"); 93 fprintf(stderr, "gzopen error\n");
93 exit(1); 94 exit(1);
94 } 95 }
95 96 if (gzprintf(file, "%s, %s!", "hello", "hello") != len-1) {
96 if (gzwrite(file, (const voidp)hello, (unsigned)len) != len) { 97 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
97 fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err));
98 } 98 }
99 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
99 gzclose(file); 100 gzclose(file);
100 101
101 file = gzopen(in, "rb"); 102 file = gzopen(in, "rb");
@@ -108,13 +109,28 @@ void test_gzio(out, in, uncompr, uncomprLen)
108 if (uncomprLen != len) { 109 if (uncomprLen != len) {
109 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); 110 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
110 } 111 }
111 gzclose(file);
112
113 if (strcmp((char*)uncompr, hello)) { 112 if (strcmp((char*)uncompr, hello)) {
114 fprintf(stderr, "bad gzread\n"); 113 fprintf(stderr, "bad gzread\n");
115 } else { 114 } else {
116 printf("gzread(): %s\n", uncompr); 115 printf("gzread(): %s\n", uncompr);
117 } 116 }
117
118 pos = gzseek(file, -7L, SEEK_CUR);
119 if (pos != 7 || gztell(file) != pos) {
120 fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
121 pos, gztell(file));
122 }
123 uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
124 if (uncomprLen != 7) {
125 fprintf(stderr, "gzread err after gzseek: %s\n", gzerror(file, &err));
126 }
127 if (strcmp((char*)uncompr, hello+7)) {
128 fprintf(stderr, "bad gzread after gzseek\n");
129 } else {
130 printf("gzread() after gzseek: %s\n", uncompr);
131 }
132
133 gzclose(file);
118} 134}
119 135
120/* =========================================================================== 136/* ===========================================================================
@@ -171,12 +187,13 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen)
171 d_stream.zfree = (free_func)0; 187 d_stream.zfree = (free_func)0;
172 d_stream.opaque = (voidpf)0; 188 d_stream.opaque = (voidpf)0;
173 189
174 err = inflateInit(&d_stream);
175 CHECK_ERR(err, "inflateInit");
176
177 d_stream.next_in = compr; 190 d_stream.next_in = compr;
191 d_stream.avail_in = 0;
178 d_stream.next_out = uncompr; 192 d_stream.next_out = uncompr;
179 193
194 err = inflateInit(&d_stream);
195 CHECK_ERR(err, "inflateInit");
196
180 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { 197 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
181 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ 198 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
182 err = inflate(&d_stream, Z_NO_FLUSH); 199 err = inflate(&d_stream, Z_NO_FLUSH);
@@ -263,12 +280,12 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
263 d_stream.zfree = (free_func)0; 280 d_stream.zfree = (free_func)0;
264 d_stream.opaque = (voidpf)0; 281 d_stream.opaque = (voidpf)0;
265 282
266 err = inflateInit(&d_stream);
267 CHECK_ERR(err, "inflateInit");
268
269 d_stream.next_in = compr; 283 d_stream.next_in = compr;
270 d_stream.avail_in = (uInt)comprLen; 284 d_stream.avail_in = (uInt)comprLen;
271 285
286 err = inflateInit(&d_stream);
287 CHECK_ERR(err, "inflateInit");
288
272 for (;;) { 289 for (;;) {
273 d_stream.next_out = uncompr; /* discard the output */ 290 d_stream.next_out = uncompr; /* discard the output */
274 d_stream.avail_out = (uInt)uncomprLen; 291 d_stream.avail_out = (uInt)uncomprLen;
@@ -339,12 +356,13 @@ void test_sync(compr, comprLen, uncompr, uncomprLen)
339 d_stream.zfree = (free_func)0; 356 d_stream.zfree = (free_func)0;
340 d_stream.opaque = (voidpf)0; 357 d_stream.opaque = (voidpf)0;
341 358
359 d_stream.next_in = compr;
360 d_stream.avail_in = 2; /* just read the zlib header */
361
342 err = inflateInit(&d_stream); 362 err = inflateInit(&d_stream);
343 CHECK_ERR(err, "inflateInit"); 363 CHECK_ERR(err, "inflateInit");
344 364
345 d_stream.next_in = compr;
346 d_stream.next_out = uncompr; 365 d_stream.next_out = uncompr;
347 d_stream.avail_in = 2; /* just read the zlib header */
348 d_stream.avail_out = (uInt)uncomprLen; 366 d_stream.avail_out = (uInt)uncomprLen;
349 367
350 inflate(&d_stream, Z_NO_FLUSH); 368 inflate(&d_stream, Z_NO_FLUSH);
@@ -417,12 +435,12 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
417 d_stream.zfree = (free_func)0; 435 d_stream.zfree = (free_func)0;
418 d_stream.opaque = (voidpf)0; 436 d_stream.opaque = (voidpf)0;
419 437
420 err = inflateInit(&d_stream);
421 CHECK_ERR(err, "inflateInit");
422
423 d_stream.next_in = compr; 438 d_stream.next_in = compr;
424 d_stream.avail_in = (uInt)comprLen; 439 d_stream.avail_in = (uInt)comprLen;
425 440
441 err = inflateInit(&d_stream);
442 CHECK_ERR(err, "inflateInit");
443
426 d_stream.next_out = uncompr; 444 d_stream.next_out = uncompr;
427 d_stream.avail_out = (uInt)uncomprLen; 445 d_stream.avail_out = (uInt)uncomprLen;
428 446
@@ -461,8 +479,9 @@ int main(argc, argv)
461 Byte *compr, *uncompr; 479 Byte *compr, *uncompr;
462 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */ 480 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
463 uLong uncomprLen = comprLen; 481 uLong uncomprLen = comprLen;
482 static const char* myVersion = ZLIB_VERSION;
464 483
465 if (zlibVersion()[0] != ZLIB_VERSION[0]) { 484 if (zlibVersion()[0] != myVersion[0]) {
466 fprintf(stderr, "incompatible zlib version\n"); 485 fprintf(stderr, "incompatible zlib version\n");
467 exit(1); 486 exit(1);
468 487
@@ -479,7 +498,6 @@ int main(argc, argv)
479 printf("out of memory\n"); 498 printf("out of memory\n");
480 exit(1); 499 exit(1);
481 } 500 }
482
483 test_compress(compr, comprLen, uncompr, uncomprLen); 501 test_compress(compr, comprLen, uncompr, uncomprLen);
484 502
485 test_gzio((argc > 1 ? argv[1] : "foo.gz"), 503 test_gzio((argc > 1 ? argv[1] : "foo.gz"),