diff options
Diffstat (limited to 'gnu/usr.bin/cvs/zlib/example.c')
-rw-r--r-- | gnu/usr.bin/cvs/zlib/example.c | 121 |
1 files changed, 87 insertions, 34 deletions
diff --git a/gnu/usr.bin/cvs/zlib/example.c b/gnu/usr.bin/cvs/zlib/example.c index 4a025240c28..2f0e1973752 100644 --- a/gnu/usr.bin/cvs/zlib/example.c +++ b/gnu/usr.bin/cvs/zlib/example.c @@ -1,9 +1,9 @@ /* example.c -- usage example of the zlib compression library - * Copyright (C) 1995-1996 Jean-loup Gailly. + * Copyright (C) 1995-1998 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h */ -/* $Id: example.c,v 1.1.1.1 1996/10/18 03:35:04 tholo Exp $ */ +/* @(#) $Id: example.c,v 1.1.1.2 2001/09/28 22:45:40 tholo Exp $ */ #include <stdio.h> #include "zlib.h" @@ -15,6 +15,12 @@ extern void exit OF((int)); #endif +#if defined(VMS) || defined(RISCOS) +# define TESTFILE "foo-gz" +#else +# define TESTFILE "foo.gz" +#endif + #define CHECK_ERR(err, msg) { \ if (err != Z_OK) { \ fprintf(stderr, "%s error: %d\n", msg, err); \ @@ -41,7 +47,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,8 +75,9 @@ 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); + printf("uncompress(): %s\n", (char *)uncompr); } } @@ -78,24 +85,31 @@ void test_compress(compr, comprLen, uncompr, uncomprLen) * Test read/write of .gz files */ void test_gzio(out, in, uncompr, uncomprLen) - const char *out; /* output file */ - const char *in; /* input file */ + const char *out; /* compressed output file */ + const char *in; /* compressed input file */ Byte *uncompr; int 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); } - - if (gzwrite(file, (const voidp)hello, (unsigned)len) != len) { - fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); + 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); } + gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ gzclose(file); file = gzopen(in, "rb"); @@ -107,14 +121,41 @@ 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\n"); + fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); + exit(1); + } else { + printf("gzread(): %s\n", (char *)uncompr); + } + + pos = gzseek(file, -8L, SEEK_CUR); + if (pos != 6 || gztell(file) != pos) { + fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", + (long)pos, (long)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("gzread(): %s\n", uncompr); + printf("gzgets() after gzseek: %s\n", (char *)uncompr); } + + gzclose(file); } /* =========================================================================== @@ -171,12 +212,13 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen) d_stream.zfree = (free_func)0; d_stream.opaque = (voidpf)0; - err = inflateInit(&d_stream); - CHECK_ERR(err, "inflateInit"); - d_stream.next_in = compr; + d_stream.avail_in = 0; d_stream.next_out = uncompr; + err = inflateInit(&d_stream); + CHECK_ERR(err, "inflateInit"); + 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); @@ -189,8 +231,9 @@ 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); + printf("inflate(): %s\n", (char *)uncompr); } } @@ -223,6 +266,7 @@ 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: */ @@ -242,6 +286,7 @@ 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"); @@ -263,12 +308,12 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) d_stream.zfree = (free_func)0; d_stream.opaque = (voidpf)0; - err = inflateInit(&d_stream); - CHECK_ERR(err, "inflateInit"); - d_stream.next_in = compr; d_stream.avail_in = (uInt)comprLen; + err = inflateInit(&d_stream); + CHECK_ERR(err, "inflateInit"); + for (;;) { d_stream.next_out = uncompr; /* discard the output */ d_stream.avail_out = (uInt)uncomprLen; @@ -282,6 +327,7 @@ 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"); } @@ -292,7 +338,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; @@ -308,7 +354,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"); @@ -321,6 +367,8 @@ void test_flush(compr, comprLen) } err = deflateEnd(&c_stream); CHECK_ERR(err, "deflateEnd"); + + *comprLen = c_stream.total_out; } /* =========================================================================== @@ -339,12 +387,13 @@ 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); @@ -358,11 +407,12 @@ 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"); - printf("after inflateSync(): hel%s\n", uncompr); + printf("after inflateSync(): hel%s\n", (char *)uncompr); } /* =========================================================================== @@ -396,6 +446,7 @@ 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"); @@ -417,12 +468,12 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) d_stream.zfree = (free_func)0; d_stream.opaque = (voidpf)0; - err = inflateInit(&d_stream); - CHECK_ERR(err, "inflateInit"); - d_stream.next_in = compr; d_stream.avail_in = (uInt)comprLen; + err = inflateInit(&d_stream); + CHECK_ERR(err, "inflateInit"); + d_stream.next_out = uncompr; d_stream.avail_out = (uInt)uncomprLen; @@ -445,8 +496,9 @@ 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); + printf("inflate with dictionary: %s\n", (char *)uncompr); } } @@ -461,8 +513,9 @@ 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] != ZLIB_VERSION[0]) { + if (zlibVersion()[0] != myVersion[0]) { fprintf(stderr, "incompatible zlib version\n"); exit(1); @@ -479,11 +532,10 @@ 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"), - (argc > 2 ? argv[2] : "foo.gz"), + test_gzio((argc > 1 ? argv[1] : TESTFILE), + (argc > 2 ? argv[2] : TESTFILE), uncompr, (int)uncomprLen); test_deflate(compr, comprLen); @@ -492,8 +544,9 @@ 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); |