A massively spiffy yet delicately unobtrusive compression library. (grpc依赖)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

547 lines
15 KiB

13 years ago
/* example.c -- usage example of the zlib compression library
8 years ago
* Copyright (C) 1995-2006, 2011, 2016 Jean-loup Gailly
13 years ago
* For conditions of distribution and use, see copyright notice in zlib.h
13 years ago
*/
13 years ago
/* @(#) $Id$ */
13 years ago
#include "zlib.h"
13 years ago
#include <stdio.h>
13 years ago
13 years ago
#ifdef STDC
# include <string.h>
13 years ago
# include <stdlib.h>
13 years ago
#endif
13 years ago
#if defined(VMS) || defined(RISCOS)
# define TESTFILE "foo-gz"
#else
# define TESTFILE "foo.gz"
#endif
13 years ago
#define CHECK_ERR(err, msg) { \
if (err != Z_OK) { \
fprintf(stderr, "%s error: %d\n", msg, err); \
13 years ago
exit(1); \
13 years ago
} \
}
static z_const char hello[] = "hello, hello!";
13 years ago
/* "hello world" would be more standard, but the repeated "hello"
* stresses the compression code better, sorry...
*/
13 years ago
static const char dictionary[] = "hello";
static uLong dictId; /* Adler32 value of the dictionary */
13 years ago
#ifdef Z_SOLO
static void *myalloc(void *q, unsigned n, unsigned m) {
(void)q;
return calloc(n, m);
}
static void myfree(void *q, void *p) {
(void)q;
free(p);
}
static alloc_func zalloc = myalloc;
static free_func zfree = myfree;
#else /* !Z_SOLO */
static alloc_func zalloc = (alloc_func)0;
static free_func zfree = (free_func)0;
13 years ago
/* ===========================================================================
* Test compress() and uncompress()
*/
static void test_compress(Byte *compr, uLong comprLen, Byte *uncompr,
uLong uncomprLen) {
13 years ago
int err;
13 years ago
uLong len = (uLong)strlen(hello)+1;
13 years ago
13 years ago
err = compress(compr, &comprLen, (const Bytef*)hello, len);
13 years ago
CHECK_ERR(err, "compress");
13 years ago
strcpy((char*)uncompr, "garbage");
13 years ago
err = uncompress(uncompr, &uncomprLen, compr, comprLen);
CHECK_ERR(err, "uncompress");
13 years ago
if (strcmp((char*)uncompr, hello)) {
13 years ago
fprintf(stderr, "bad uncompress\n");
13 years ago
exit(1);
13 years ago
} else {
13 years ago
printf("uncompress(): %s\n", (char *)uncompr);
13 years ago
}
}
/* ===========================================================================
* Test read/write of .gz files
*/
static void test_gzio(const char *fname, Byte *uncompr, uLong uncomprLen) {
13 years ago
#ifdef NO_GZCOMPRESS
fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
#else
13 years ago
int err;
13 years ago
int len = (int)strlen(hello)+1;
13 years ago
gzFile file;
13 years ago
z_off_t pos;
13 years ago
13 years ago
file = gzopen(fname, "wb");
13 years ago
if (file == NULL) {
13 years ago
fprintf(stderr, "gzopen error\n");
exit(1);
13 years ago
}
13 years ago
gzputc(file, 'h');
13 years ago
if (gzputs(file, "ello") != 4) {
fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
13 years ago
exit(1);
13 years ago
}
if (gzprintf(file, ", %s!", "hello") != 8) {
13 years ago
fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
13 years ago
exit(1);
13 years ago
}
13 years ago
gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
13 years ago
gzclose(file);
13 years ago
file = gzopen(fname, "rb");
13 years ago
if (file == NULL) {
13 years ago
fprintf(stderr, "gzopen error\n");
13 years ago
exit(1);
13 years ago
}
13 years ago
strcpy((char*)uncompr, "garbage");
13 years ago
13 years ago
if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
13 years ago
fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
13 years ago
exit(1);
13 years ago
}
13 years ago
if (strcmp((char*)uncompr, hello)) {
13 years ago
fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
13 years ago
exit(1);
13 years ago
} else {
13 years ago
printf("gzread(): %s\n", (char*)uncompr);
13 years ago
}
13 years ago
13 years ago
pos = gzseek(file, -8L, SEEK_CUR);
if (pos != 6 || gztell(file) != pos) {
13 years ago
fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
(long)pos, (long)gztell(file));
exit(1);
13 years ago
}
13 years ago
if (gzgetc(file) != ' ') {
13 years ago
fprintf(stderr, "gzgetc error\n");
exit(1);
13 years ago
}
13 years ago
if (gzungetc(' ', file) != ' ') {
fprintf(stderr, "gzungetc error\n");
exit(1);
}
13 years ago
gzgets(file, (char*)uncompr, (int)uncomprLen);
if (strlen((char*)uncompr) != 7) { /* " hello!" */
13 years ago
fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
13 years ago
exit(1);
13 years ago
}
13 years ago
if (strcmp((char*)uncompr, hello + 6)) {
13 years ago
fprintf(stderr, "bad gzgets after gzseek\n");
13 years ago
exit(1);
13 years ago
} else {
13 years ago
printf("gzgets() after gzseek: %s\n", (char*)uncompr);
13 years ago
}
gzclose(file);
13 years ago
#endif
13 years ago
}
#endif /* Z_SOLO */
13 years ago
/* ===========================================================================
13 years ago
* Test deflate() with small buffers
13 years ago
*/
static void test_deflate(Byte *compr, uLong comprLen) {
13 years ago
z_stream c_stream; /* compression stream */
int err;
13 years ago
uLong len = (uLong)strlen(hello)+1;
13 years ago
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
13 years ago
c_stream.opaque = (voidpf)0;
13 years ago
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
CHECK_ERR(err, "deflateInit");
c_stream.next_in = (z_const unsigned char *)hello;
13 years ago
c_stream.next_out = compr;
13 years ago
while (c_stream.total_in != len && c_stream.total_out < comprLen) {
13 years ago
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
13 years ago
}
/* Finish the stream, still forcing small buffers: */
13 years ago
for (;;) {
13 years ago
c_stream.avail_out = 1;
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "deflate");
13 years ago
}
13 years ago
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test inflate() with small buffers
*/
static void test_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
uLong uncomprLen) {
13 years ago
int err;
z_stream d_stream; /* decompression stream */
13 years ago
strcpy((char*)uncompr, "garbage");
13 years ago
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
13 years ago
d_stream.opaque = (voidpf)0;
13 years ago
d_stream.next_in = compr;
13 years ago
d_stream.avail_in = 0;
13 years ago
d_stream.next_out = uncompr;
13 years ago
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
13 years ago
while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
13 years ago
d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "inflate");
13 years ago
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
13 years ago
if (strcmp((char*)uncompr, hello)) {
13 years ago
fprintf(stderr, "bad inflate\n");
13 years ago
exit(1);
13 years ago
} else {
13 years ago
printf("inflate(): %s\n", (char *)uncompr);
13 years ago
}
}
13 years ago
/* ===========================================================================
* Test deflate() with large buffers and dynamic change of compression level
*/
static void test_large_deflate(Byte *compr, uLong comprLen, Byte *uncompr,
uLong uncomprLen) {
13 years ago
z_stream c_stream; /* compression stream */
int err;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
13 years ago
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_BEST_SPEED);
CHECK_ERR(err, "deflateInit");
c_stream.next_out = compr;
13 years ago
c_stream.avail_out = (uInt)comprLen;
13 years ago
/* At this point, uncompr is still mostly zeroes, so it should compress
* very well:
*/
c_stream.next_in = uncompr;
c_stream.avail_in = (uInt)uncomprLen;
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
if (c_stream.avail_in != 0) {
fprintf(stderr, "deflate not greedy\n");
13 years ago
exit(1);
13 years ago
}
/* Feed in already compressed data and switch to no compression: */
deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
c_stream.next_in = compr;
c_stream.avail_in = (uInt)uncomprLen/2;
13 years ago
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
/* Switch back to compressing mode: */
deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
c_stream.next_in = uncompr;
c_stream.avail_in = (uInt)uncomprLen;
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
fprintf(stderr, "deflate should report Z_STREAM_END\n");
13 years ago
exit(1);
13 years ago
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test inflate() with large buffers
*/
static void test_large_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
uLong uncomprLen) {
13 years ago
int err;
z_stream d_stream; /* decompression stream */
strcpy((char*)uncompr, "garbage");
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
13 years ago
d_stream.opaque = (voidpf)0;
d_stream.next_in = compr;
d_stream.avail_in = (uInt)comprLen;
13 years ago
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
13 years ago
for (;;) {
d_stream.next_out = uncompr; /* discard the output */
13 years ago
d_stream.avail_out = (uInt)uncomprLen;
13 years ago
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "large inflate");
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
if (d_stream.total_out != 2*uncomprLen + uncomprLen/2) {
13 years ago
fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
13 years ago
exit(1);
13 years ago
} else {
printf("large_inflate(): OK\n");
}
}
13 years ago
/* ===========================================================================
* Test deflate() with full flush
*/
static void test_flush(Byte *compr, uLong *comprLen) {
13 years ago
z_stream c_stream; /* compression stream */
int err;
13 years ago
uInt len = (uInt)strlen(hello)+1;
13 years ago
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
13 years ago
c_stream.opaque = (voidpf)0;
13 years ago
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
CHECK_ERR(err, "deflateInit");
c_stream.next_in = (z_const unsigned char *)hello;
13 years ago
c_stream.next_out = compr;
c_stream.avail_in = 3;
13 years ago
c_stream.avail_out = (uInt)*comprLen;
13 years ago
err = deflate(&c_stream, Z_FULL_FLUSH);
CHECK_ERR(err, "deflate");
compr[3]++; /* force an error in first compressed block */
c_stream.avail_in = len - 3;
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
13 years ago
CHECK_ERR(err, "deflate");
13 years ago
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
13 years ago
*comprLen = c_stream.total_out;
13 years ago
}
/* ===========================================================================
* Test inflateSync()
*/
static void test_sync(Byte *compr, uLong comprLen, Byte *uncompr,
uLong uncomprLen) {
13 years ago
int err;
z_stream d_stream; /* decompression stream */
strcpy((char*)uncompr, "garbage");
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
13 years ago
d_stream.opaque = (voidpf)0;
13 years ago
13 years ago
d_stream.next_in = compr;
d_stream.avail_in = 2; /* just read the zlib header */
13 years ago
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
d_stream.next_out = uncompr;
13 years ago
d_stream.avail_out = (uInt)uncomprLen;
13 years ago
err = inflate(&d_stream, Z_NO_FLUSH);
13 years ago
CHECK_ERR(err, "inflate");
13 years ago
d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
13 years ago
err = inflateSync(&d_stream); /* but skip the damaged part */
13 years ago
CHECK_ERR(err, "inflateSync");
err = inflate(&d_stream, Z_FINISH);
if (err != Z_STREAM_END) {
fprintf(stderr, "inflate should report Z_STREAM_END\n");
13 years ago
exit(1);
13 years ago
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
13 years ago
printf("after inflateSync(): hel%s\n", (char *)uncompr);
13 years ago
}
13 years ago
/* ===========================================================================
* Test deflate() with preset dictionary
*/
static void test_dict_deflate(Byte *compr, uLong comprLen) {
13 years ago
z_stream c_stream; /* compression stream */
int err;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
13 years ago
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
CHECK_ERR(err, "deflateInit");
err = deflateSetDictionary(&c_stream,
13 years ago
(const Bytef*)dictionary, (int)sizeof(dictionary));
13 years ago
CHECK_ERR(err, "deflateSetDictionary");
dictId = c_stream.adler;
c_stream.next_out = compr;
c_stream.avail_out = (uInt)comprLen;
c_stream.next_in = (z_const unsigned char *)hello;
13 years ago
c_stream.avail_in = (uInt)strlen(hello)+1;
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
fprintf(stderr, "deflate should report Z_STREAM_END\n");
13 years ago
exit(1);
13 years ago
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test inflate() with a preset dictionary
*/
static void test_dict_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
uLong uncomprLen) {
13 years ago
int err;
z_stream d_stream; /* decompression stream */
strcpy((char*)uncompr, "garbage");
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
13 years ago
d_stream.opaque = (voidpf)0;
d_stream.next_in = compr;
d_stream.avail_in = (uInt)comprLen;
13 years ago
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
13 years ago
d_stream.next_out = uncompr;
d_stream.avail_out = (uInt)uncomprLen;
for (;;) {
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
13 years ago
if (err == Z_NEED_DICT) {
if (d_stream.adler != dictId) {
fprintf(stderr, "unexpected dictionary");
exit(1);
}
err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
13 years ago
(int)sizeof(dictionary));
13 years ago
}
13 years ago
CHECK_ERR(err, "inflate with dict");
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
if (strcmp((char*)uncompr, hello)) {
fprintf(stderr, "bad inflate with dict\n");
13 years ago
exit(1);
13 years ago
} else {
13 years ago
printf("inflate with dictionary: %s\n", (char *)uncompr);
13 years ago
}
}
13 years ago
/* ===========================================================================
* Usage: example [output.gz [input.gz]]
*/
int main(int argc, char *argv[]) {
13 years ago
Byte *compr, *uncompr;
uLong uncomprLen = 20000;
uLong comprLen = 3 * uncomprLen;
13 years ago
static const char* myVersion = ZLIB_VERSION;
13 years ago
13 years ago
if (zlibVersion()[0] != myVersion[0]) {
13 years ago
fprintf(stderr, "incompatible zlib version\n");
exit(1);
13 years ago
13 years ago
} else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
fprintf(stderr, "warning: different zlib version linked: %s\n",
zlibVersion());
13 years ago
}
13 years ago
13 years ago
printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
13 years ago
ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
13 years ago
compr = (Byte*)calloc((uInt)comprLen, 1);
uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
/* compr and uncompr are cleared to avoid reading uninitialized
* data and to ensure that uncompr compresses well.
*/
13 years ago
if (compr == Z_NULL || uncompr == Z_NULL) {
printf("out of memory\n");
13 years ago
exit(1);
13 years ago
}
#ifdef Z_SOLO
(void)argc;
(void)argv;
#else
13 years ago
test_compress(compr, comprLen, uncompr, uncomprLen);
13 years ago
13 years ago
test_gzio((argc > 1 ? argv[1] : TESTFILE),
13 years ago
uncompr, uncomprLen);
#endif
13 years ago
test_deflate(compr, comprLen);
test_inflate(compr, comprLen, uncompr, uncomprLen);
test_large_deflate(compr, comprLen, uncompr, uncomprLen);
test_large_inflate(compr, comprLen, uncompr, uncomprLen);
13 years ago
13 years ago
test_flush(compr, &comprLen);
13 years ago
test_sync(compr, comprLen, uncompr, uncomprLen);
comprLen = 3 * uncomprLen;
13 years ago
13 years ago
test_dict_deflate(compr, comprLen);
test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
13 years ago
free(compr);
free(uncompr);
13 years ago
return 0;
13 years ago
}