summaryrefslogtreecommitdiffstats
path: root/lib/libz/example.c
diff options
context:
space:
mode:
authortjr <tjr@FreeBSD.org>2004-06-30 23:54:46 +0000
committertjr <tjr@FreeBSD.org>2004-06-30 23:54:46 +0000
commit23687471664c322d3854e587a32af938a5b1c185 (patch)
treede1d43b9c6f17b0eac60ab29d51d8339ed949c0b /lib/libz/example.c
parent6ea5314b57928a6385cf31fb23a37d5b50104805 (diff)
downloadFreeBSD-src-23687471664c322d3854e587a32af938a5b1c185.zip
FreeBSD-src-23687471664c322d3854e587a32af938a5b1c185.tar.gz
Resolve conflicts.
Diffstat (limited to 'lib/libz/example.c')
-rw-r--r--lib/libz/example.c137
1 files changed, 74 insertions, 63 deletions
diff --git a/lib/libz/example.c b/lib/libz/example.c
index 362a4a5..c528b62 100644
--- a/lib/libz/example.c
+++ b/lib/libz/example.c
@@ -1,6 +1,6 @@
/* example.c -- usage example of the zlib compression library
- * Copyright (C) 1995-2002 Jean-loup Gailly.
- * For conditions of distribution and use, see copyright notice in zlib.h
+ * Copyright (C) 1995-2003 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
*/
#include <sys/cdefs.h>
@@ -38,22 +38,22 @@ const char dictionary[] = "hello";
uLong dictId; /* Adler32 value of the dictionary */
void test_compress OF((Byte *compr, uLong comprLen,
- Byte *uncompr, uLong uncomprLen));
-void test_gzio OF((const char *out, const char *in,
- Byte *uncompr, int uncomprLen));
+ Byte *uncompr, uLong uncomprLen));
+void test_gzio OF((const char *fname,
+ Byte *uncompr, uLong uncomprLen));
void test_deflate OF((Byte *compr, uLong comprLen));
void test_inflate OF((Byte *compr, uLong comprLen,
- Byte *uncompr, uLong uncomprLen));
+ Byte *uncompr, uLong uncomprLen));
void test_large_deflate OF((Byte *compr, uLong comprLen,
- Byte *uncompr, uLong uncomprLen));
+ Byte *uncompr, uLong uncomprLen));
void test_large_inflate OF((Byte *compr, uLong comprLen,
- Byte *uncompr, uLong uncomprLen));
+ Byte *uncompr, uLong uncomprLen));
void test_flush OF((Byte *compr, uLong *comprLen));
void test_sync OF((Byte *compr, uLong comprLen,
- Byte *uncompr, uLong uncomprLen));
+ Byte *uncompr, uLong uncomprLen));
void test_dict_deflate OF((Byte *compr, uLong comprLen));
void test_dict_inflate OF((Byte *compr, uLong comprLen,
- Byte *uncompr, uLong uncomprLen));
+ Byte *uncompr, uLong uncomprLen));
int main OF((int argc, char *argv[]));
/* ===========================================================================
@@ -64,7 +64,7 @@ void test_compress(compr, comprLen, uncompr, uncomprLen)
uLong comprLen, uncomprLen;
{
int err;
- uLong len = strlen(hello)+1;
+ uLong len = (uLong)strlen(hello)+1;
err = compress(compr, &comprLen, (const Bytef*)hello, len);
CHECK_ERR(err, "compress");
@@ -76,7 +76,7 @@ void test_compress(compr, comprLen, uncompr, uncomprLen)
if (strcmp((char*)uncompr, hello)) {
fprintf(stderr, "bad uncompress\n");
- exit(1);
+ exit(1);
} else {
printf("uncompress(): %s\n", (char *)uncompr);
}
@@ -85,18 +85,20 @@ void test_compress(compr, comprLen, uncompr, uncomprLen)
/* ===========================================================================
* Test read/write of .gz files
*/
-void test_gzio(out, in, uncompr, uncomprLen)
- const char *out; /* compressed output file */
- const char *in; /* compressed input file */
+void test_gzio(fname, uncompr, uncomprLen)
+ const char *fname; /* compressed file name */
Byte *uncompr;
- int uncomprLen;
+ uLong uncomprLen;
{
+#ifdef NO_GZCOMPRESS
+ fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
+#else
int err;
- int len = strlen(hello)+1;
+ int len = (int)strlen(hello)+1;
gzFile file;
z_off_t pos;
- file = gzopen(out, "wb");
+ file = gzopen(fname, "wb");
if (file == NULL) {
fprintf(stderr, "gzopen error\n");
exit(1);
@@ -104,59 +106,64 @@ void test_gzio(out, in, uncompr, uncomprLen)
gzputc(file, 'h');
if (gzputs(file, "ello") != 4) {
fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
- exit(1);
+ exit(1);
}
if (gzprintf(file, ", %s!", "hello") != 8) {
fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
- exit(1);
+ exit(1);
}
gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
gzclose(file);
- file = gzopen(in, "rb");
+ file = gzopen(fname, "rb");
if (file == NULL) {
fprintf(stderr, "gzopen error\n");
+ exit(1);
}
strcpy((char*)uncompr, "garbage");
- uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
- if (uncomprLen != len) {
+ if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
- exit(1);
+ exit(1);
}
if (strcmp((char*)uncompr, hello)) {
fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
- exit(1);
+ exit(1);
} else {
- printf("gzread(): %s\n", (char *)uncompr);
+ 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);
+ 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);
+ fprintf(stderr, "gzgetc error\n");
+ exit(1);
+ }
+
+ if (gzungetc(' ', file) != ' ') {
+ fprintf(stderr, "gzungetc error\n");
+ exit(1);
}
- gzgets(file, (char*)uncompr, uncomprLen);
- uncomprLen = strlen((char*)uncompr);
- if (uncomprLen != 6) { /* "hello!" */
+ gzgets(file, (char*)uncompr, (int)uncomprLen);
+ if (strlen((char*)uncompr) != 7) { /* " hello!" */
fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
- exit(1);
+ exit(1);
}
- if (strcmp((char*)uncompr, hello+7)) {
+ if (strcmp((char*)uncompr, hello + 6)) {
fprintf(stderr, "bad gzgets after gzseek\n");
- exit(1);
+ exit(1);
} else {
- printf("gzgets() after gzseek: %s\n", (char *)uncompr);
+ printf("gzgets() after gzseek: %s\n", (char*)uncompr);
}
gzclose(file);
+#endif
}
/* ===========================================================================
@@ -168,7 +175,7 @@ void test_deflate(compr, comprLen)
{
z_stream c_stream; /* compression stream */
int err;
- int len = strlen(hello)+1;
+ uLong len = (uLong)strlen(hello)+1;
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
@@ -180,7 +187,7 @@ void test_deflate(compr, comprLen)
c_stream.next_in = (Bytef*)hello;
c_stream.next_out = compr;
- while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
+ while (c_stream.total_in != len && c_stream.total_out < comprLen) {
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
@@ -232,7 +239,7 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen)
if (strcmp((char*)uncompr, hello)) {
fprintf(stderr, "bad inflate\n");
- exit(1);
+ exit(1);
} else {
printf("inflate(): %s\n", (char *)uncompr);
}
@@ -267,7 +274,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);
+ exit(1);
}
/* Feed in already compressed data and switch to no compression: */
@@ -287,7 +294,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);
+ exit(1);
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
@@ -317,7 +324,7 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
for (;;) {
d_stream.next_out = uncompr; /* discard the output */
- d_stream.avail_out = (uInt)uncomprLen;
+ d_stream.avail_out = (uInt)uncomprLen;
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "large inflate");
@@ -328,7 +335,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);
+ exit(1);
} else {
printf("large_inflate(): OK\n");
}
@@ -343,7 +350,7 @@ void test_flush(compr, comprLen)
{
z_stream c_stream; /* compression stream */
int err;
- int len = strlen(hello)+1;
+ uInt len = (uInt)strlen(hello)+1;
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
@@ -408,7 +415,7 @@ 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);
+ exit(1);
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
@@ -434,7 +441,7 @@ void test_dict_deflate(compr, comprLen)
CHECK_ERR(err, "deflateInit");
err = deflateSetDictionary(&c_stream,
- (const Bytef*)dictionary, sizeof(dictionary));
+ (const Bytef*)dictionary, sizeof(dictionary));
CHECK_ERR(err, "deflateSetDictionary");
dictId = c_stream.adler;
@@ -447,7 +454,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);
+ exit(1);
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
@@ -481,14 +488,14 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
for (;;) {
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
- if (err == Z_NEED_DICT) {
- if (d_stream.adler != dictId) {
- fprintf(stderr, "unexpected dictionary");
- exit(1);
- }
- err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
- sizeof(dictionary));
- }
+ if (err == Z_NEED_DICT) {
+ if (d_stream.adler != dictId) {
+ fprintf(stderr, "unexpected dictionary");
+ exit(1);
+ }
+ err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
+ sizeof(dictionary));
+ }
CHECK_ERR(err, "inflate with dict");
}
@@ -497,7 +504,7 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
if (strcmp((char*)uncompr, hello)) {
fprintf(stderr, "bad inflate with dict\n");
- exit(1);
+ exit(1);
} else {
printf("inflate with dictionary: %s\n", (char *)uncompr);
}
@@ -524,6 +531,9 @@ int main(argc, argv)
fprintf(stderr, "warning: different zlib version\n");
}
+ printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
+ ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
+
compr = (Byte*)calloc((uInt)comprLen, 1);
uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
/* compr and uncompr are cleared to avoid reading uninitialized
@@ -531,13 +541,12 @@ int main(argc, argv)
*/
if (compr == Z_NULL || uncompr == Z_NULL) {
printf("out of memory\n");
- exit(1);
+ exit(1);
}
test_compress(compr, comprLen, uncompr, uncomprLen);
test_gzio((argc > 1 ? argv[1] : TESTFILE),
- (argc > 2 ? argv[2] : TESTFILE),
- uncompr, (int)uncomprLen);
+ uncompr, uncomprLen);
test_deflate(compr, comprLen);
test_inflate(compr, comprLen, uncompr, uncomprLen);
@@ -552,6 +561,8 @@ int main(argc, argv)
test_dict_deflate(compr, comprLen);
test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
- exit(0);
- return 0; /* to avoid warning */
+ free(compr);
+ free(uncompr);
+
+ return 0;
}
OpenPOWER on IntegriCloud