diff options
Diffstat (limited to 'lib/libz/example.c')
-rw-r--r-- | lib/libz/example.c | 97 |
1 files changed, 25 insertions, 72 deletions
diff --git a/lib/libz/example.c b/lib/libz/example.c index 5b3404a..3830842 100644 --- a/lib/libz/example.c +++ b/lib/libz/example.c @@ -1,9 +1,9 @@ /* example.c -- usage example of the zlib compression library - * Copyright (C) 1995-1998 Jean-loup Gailly. + * Copyright (C) 1995-1996 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h */ -/* @(#) $Id$ */ +/* $FreeBSD$ */ #include <stdio.h> #include "zlib.h" @@ -41,7 +41,7 @@ void test_large_deflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_large_inflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); -void test_flush OF((Byte *compr, uLong *comprLen)); +void test_flush OF((Byte *compr, uLong comprLen)); void test_sync OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_dict_deflate OF((Byte *compr, uLong comprLen)); @@ -69,7 +69,6 @@ void test_compress(compr, comprLen, uncompr, uncomprLen) if (strcmp((char*)uncompr, hello)) { fprintf(stderr, "bad uncompress\n"); - exit(1); } else { printf("uncompress(): %s\n", uncompr); } @@ -87,23 +86,16 @@ void test_gzio(out, in, uncompr, uncomprLen) int err; int len = strlen(hello)+1; gzFile file; - z_off_t pos; file = gzopen(out, "wb"); if (file == NULL) { fprintf(stderr, "gzopen error\n"); exit(1); } - gzputc(file, 'h'); - if (gzputs(file, "ello") != 4) { - fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); - exit(1); - } - if (gzprintf(file, ", %s!", "hello") != 8) { - fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); - exit(1); + + if (gzwrite(file, (const voidp)hello, (unsigned)len) != len) { + fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); } - gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ gzclose(file); file = gzopen(in, "rb"); @@ -115,41 +107,14 @@ void test_gzio(out, in, uncompr, uncomprLen) uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen); if (uncomprLen != len) { fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); - exit(1); } + gzclose(file); + if (strcmp((char*)uncompr, hello)) { - fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); - exit(1); + fprintf(stderr, "bad gzread\n"); } else { printf("gzread(): %s\n", uncompr); } - - pos = gzseek(file, -8L, SEEK_CUR); - if (pos != 6 || gztell(file) != pos) { - fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", - pos, gztell(file)); - exit(1); - } - - if (gzgetc(file) != ' ') { - fprintf(stderr, "gzgetc error\n"); - exit(1); - } - - gzgets(file, (char*)uncompr, uncomprLen); - uncomprLen = strlen((char*)uncompr); - if (uncomprLen != 6) { /* "hello!" */ - fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); - exit(1); - } - if (strcmp((char*)uncompr, hello+7)) { - fprintf(stderr, "bad gzgets after gzseek\n"); - exit(1); - } else { - printf("gzgets() after gzseek: %s\n", uncompr); - } - - gzclose(file); } /* =========================================================================== @@ -206,13 +171,12 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen) d_stream.zfree = (free_func)0; d_stream.opaque = (voidpf)0; - d_stream.next_in = compr; - d_stream.avail_in = 0; - d_stream.next_out = uncompr; - err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); + d_stream.next_in = compr; + d_stream.next_out = uncompr; + while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ err = inflate(&d_stream, Z_NO_FLUSH); @@ -225,7 +189,6 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen) if (strcmp((char*)uncompr, hello)) { fprintf(stderr, "bad inflate\n"); - exit(1); } else { printf("inflate(): %s\n", uncompr); } @@ -260,7 +223,6 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen) CHECK_ERR(err, "deflate"); if (c_stream.avail_in != 0) { fprintf(stderr, "deflate not greedy\n"); - exit(1); } /* Feed in already compressed data and switch to no compression: */ @@ -280,7 +242,6 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen) err = deflate(&c_stream, Z_FINISH); if (err != Z_STREAM_END) { fprintf(stderr, "deflate should report Z_STREAM_END\n"); - exit(1); } err = deflateEnd(&c_stream); CHECK_ERR(err, "deflateEnd"); @@ -302,12 +263,12 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) d_stream.zfree = (free_func)0; d_stream.opaque = (voidpf)0; - d_stream.next_in = compr; - d_stream.avail_in = (uInt)comprLen; - err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); + d_stream.next_in = compr; + d_stream.avail_in = (uInt)comprLen; + for (;;) { d_stream.next_out = uncompr; /* discard the output */ d_stream.avail_out = (uInt)uncomprLen; @@ -321,7 +282,6 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) if (d_stream.total_out != 2*uncomprLen + comprLen/2) { fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); - exit(1); } else { printf("large_inflate(): OK\n"); } @@ -332,7 +292,7 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) */ void test_flush(compr, comprLen) Byte *compr; - uLong *comprLen; + uLong comprLen; { z_stream c_stream; /* compression stream */ int err; @@ -348,7 +308,7 @@ void test_flush(compr, comprLen) c_stream.next_in = (Bytef*)hello; c_stream.next_out = compr; c_stream.avail_in = 3; - c_stream.avail_out = (uInt)*comprLen; + c_stream.avail_out = (uInt)comprLen; err = deflate(&c_stream, Z_FULL_FLUSH); CHECK_ERR(err, "deflate"); @@ -361,8 +321,6 @@ void test_flush(compr, comprLen) } err = deflateEnd(&c_stream); CHECK_ERR(err, "deflateEnd"); - - *comprLen = c_stream.total_out; } /* =========================================================================== @@ -381,13 +339,12 @@ void test_sync(compr, comprLen, uncompr, uncomprLen) d_stream.zfree = (free_func)0; d_stream.opaque = (voidpf)0; - d_stream.next_in = compr; - d_stream.avail_in = 2; /* just read the zlib header */ - err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); + d_stream.next_in = compr; d_stream.next_out = uncompr; + d_stream.avail_in = 2; /* just read the zlib header */ d_stream.avail_out = (uInt)uncomprLen; inflate(&d_stream, Z_NO_FLUSH); @@ -401,7 +358,6 @@ void test_sync(compr, comprLen, uncompr, uncomprLen) if (err != Z_DATA_ERROR) { fprintf(stderr, "inflate should report DATA_ERROR\n"); /* Because of incorrect adler32 */ - exit(1); } err = inflateEnd(&d_stream); CHECK_ERR(err, "inflateEnd"); @@ -440,7 +396,6 @@ void test_dict_deflate(compr, comprLen) err = deflate(&c_stream, Z_FINISH); if (err != Z_STREAM_END) { fprintf(stderr, "deflate should report Z_STREAM_END\n"); - exit(1); } err = deflateEnd(&c_stream); CHECK_ERR(err, "deflateEnd"); @@ -462,12 +417,12 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) d_stream.zfree = (free_func)0; d_stream.opaque = (voidpf)0; - d_stream.next_in = compr; - d_stream.avail_in = (uInt)comprLen; - err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); + d_stream.next_in = compr; + d_stream.avail_in = (uInt)comprLen; + d_stream.next_out = uncompr; d_stream.avail_out = (uInt)uncomprLen; @@ -490,7 +445,6 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) if (strcmp((char*)uncompr, hello)) { fprintf(stderr, "bad inflate with dict\n"); - exit(1); } else { printf("inflate with dictionary: %s\n", uncompr); } @@ -507,9 +461,8 @@ int main(argc, argv) Byte *compr, *uncompr; uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */ uLong uncomprLen = comprLen; - static const char* myVersion = ZLIB_VERSION; - if (zlibVersion()[0] != myVersion[0]) { + if (zlibVersion()[0] != ZLIB_VERSION[0]) { fprintf(stderr, "incompatible zlib version\n"); exit(1); @@ -526,6 +479,7 @@ int main(argc, argv) printf("out of memory\n"); exit(1); } + test_compress(compr, comprLen, uncompr, uncomprLen); test_gzio((argc > 1 ? argv[1] : "foo.gz"), @@ -538,9 +492,8 @@ int main(argc, argv) test_large_deflate(compr, comprLen, uncompr, uncomprLen); test_large_inflate(compr, comprLen, uncompr, uncomprLen); - test_flush(compr, &comprLen); + test_flush(compr, comprLen); test_sync(compr, comprLen, uncompr, uncomprLen); - comprLen = uncomprLen; test_dict_deflate(compr, comprLen); test_dict_inflate(compr, comprLen, uncompr, uncomprLen); |