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