sl@0: /* Portions Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: */ sl@0: sl@0: /* example.c -- usage example of the zlib compression library sl@0: * Copyright (C) 1995-1998 Jean-loup Gailly. sl@0: * For conditions of distribution and use, see copyright notice in zlib.h sl@0: */ sl@0: sl@0: /* @(#) $Id$ */ sl@0: sl@0: #include sl@0: #include sl@0: sl@0: sl@0: #ifdef STDC sl@0: # include sl@0: # include sl@0: #else sl@0: extern void exit OF((int)); sl@0: #endif sl@0: sl@0: #if defined(VMS) || defined(RISCOS) sl@0: # define TESTFILE "foo-gz" sl@0: #else sl@0: # define TESTFILE "foo.gz" sl@0: #endif sl@0: sl@0: #define CHECK_ERR(err, msg) { \ sl@0: if (err != Z_OK) { \ sl@0: fprintf(stderr, "%s error: %d\n", msg, err); \ sl@0: exit(1); \ sl@0: } \ sl@0: } sl@0: sl@0: const char hello[] = "hello, hello!"; sl@0: /* "hello world" would be more standard, but the repeated "hello" sl@0: * stresses the compression code better, sorry... sl@0: */ sl@0: sl@0: const char dictionary[] = "hello"; sl@0: uLong dictId; /* Adler32 value of the dictionary */ sl@0: sl@0: void test_compress OF((Byte *compr, uLong comprLen, sl@0: Byte *uncompr, uLong uncomprLen)); sl@0: void test_deflate OF((Byte *compr, uLong comprLen)); sl@0: void test_inflate OF((Byte *compr, uLong comprLen, sl@0: Byte *uncompr, uLong uncomprLen)); sl@0: void test_large_deflate OF((Byte *compr, uLong comprLen, sl@0: Byte *uncompr, uLong uncomprLen)); sl@0: void test_large_inflate OF((Byte *compr, uLong comprLen, sl@0: Byte *uncompr, uLong uncomprLen)); sl@0: void test_flush OF((Byte *compr, uLong *comprLen)); sl@0: void test_sync OF((Byte *compr, uLong comprLen, sl@0: Byte *uncompr, uLong uncomprLen)); sl@0: void test_dict_deflate OF((Byte *compr, uLong comprLen)); sl@0: void test_dict_inflate OF((Byte *compr, uLong comprLen, sl@0: Byte *uncompr, uLong uncomprLen)); sl@0: int main OF((/*int argc, char *argv[]*/)); sl@0: sl@0: /** sl@0: Test compress() and uncompress() sl@0: sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0820 sl@0: @SYMTestCaseDesc Compression and decompression of a buffer test. sl@0: @SYMTestPriority High sl@0: @SYMTestActions Compress and uncompress string "hello" and check for integrity of the operation done. sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: void test_compress( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: int err; sl@0: uLong len = strlen(hello)+1; sl@0: sl@0: err = compress(compr, &comprLen, (const Bytef*)hello, len); sl@0: CHECK_ERR(err, "compress"); sl@0: sl@0: strcpy((char*)uncompr, "garbage"); sl@0: sl@0: err = uncompress(uncompr, &uncomprLen, compr, comprLen); sl@0: CHECK_ERR(err, "uncompress"); sl@0: sl@0: if (strcmp((char*)uncompr, hello)) { sl@0: fprintf(stderr, "bad uncompress\n"); sl@0: exit(1); sl@0: } else { sl@0: printf("uncompress(): %s\n", (char *)uncompr); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0821 sl@0: @SYMTestCaseDesc Read and write of .gz files test sl@0: @SYMTestPriority High sl@0: @SYMTestActions Tests deflate() with small buffers sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: void test_deflate( sl@0: Byte *compr, sl@0: uLong comprLen) sl@0: { sl@0: z_stream c_stream; /* compression stream */ sl@0: int err; sl@0: int len = strlen(hello)+1; sl@0: sl@0: c_stream.zalloc = (alloc_func)0; sl@0: c_stream.zfree = (free_func)0; sl@0: c_stream.opaque = (voidpf)0; sl@0: sl@0: err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); sl@0: CHECK_ERR(err, "deflateInit"); sl@0: sl@0: c_stream.next_in = (Bytef*)hello; sl@0: c_stream.next_out = compr; sl@0: sl@0: while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) { sl@0: c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ sl@0: err = deflate(&c_stream, Z_NO_FLUSH); sl@0: CHECK_ERR(err, "deflate"); sl@0: } sl@0: /* Finish the stream, still forcing small buffers: */ sl@0: for (;;) { sl@0: c_stream.avail_out = 1; sl@0: err = deflate(&c_stream, Z_FINISH); sl@0: if (err == Z_STREAM_END) break; sl@0: CHECK_ERR(err, "deflate"); sl@0: } sl@0: sl@0: err = deflateEnd(&c_stream); sl@0: CHECK_ERR(err, "deflateEnd"); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0822 sl@0: @SYMTestCaseDesc Read and write of .gz files test sl@0: @SYMTestPriority High sl@0: @SYMTestActions Tests inflate() with small buffers sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: void test_inflate( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: int err; sl@0: z_stream d_stream; /* decompression stream */ sl@0: sl@0: strcpy((char*)uncompr, "garbage"); sl@0: sl@0: d_stream.zalloc = (alloc_func)0; sl@0: d_stream.zfree = (free_func)0; sl@0: d_stream.opaque = (voidpf)0; sl@0: sl@0: d_stream.next_in = compr; sl@0: d_stream.avail_in = 0; sl@0: d_stream.next_out = uncompr; sl@0: sl@0: err = inflateInit(&d_stream); sl@0: CHECK_ERR(err, "inflateInit"); sl@0: sl@0: while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { sl@0: d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ sl@0: err = inflate(&d_stream, Z_NO_FLUSH); sl@0: if (err == Z_STREAM_END) break; sl@0: CHECK_ERR(err, "inflate"); sl@0: } sl@0: sl@0: err = inflateEnd(&d_stream); sl@0: CHECK_ERR(err, "inflateEnd"); sl@0: sl@0: if (strcmp((char*)uncompr, hello)) { sl@0: fprintf(stderr, "bad inflate\n"); sl@0: exit(1); sl@0: } else { sl@0: printf("inflate(): %s\n", (char *)uncompr); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0823 sl@0: @SYMTestCaseDesc Deflate with large buffers test sl@0: @SYMTestPriority High sl@0: @SYMTestActions Test deflate() with large buffers and dynamic change of compression level sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: void test_large_deflate( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: z_stream c_stream; /* compression stream */ sl@0: int err; sl@0: sl@0: c_stream.zalloc = (alloc_func)0; sl@0: c_stream.zfree = (free_func)0; sl@0: c_stream.opaque = (voidpf)0; sl@0: sl@0: err = deflateInit(&c_stream, Z_BEST_SPEED); sl@0: CHECK_ERR(err, "deflateInit"); sl@0: sl@0: c_stream.next_out = compr; sl@0: c_stream.avail_out = (uInt)comprLen; sl@0: sl@0: /* At this point, uncompr is still mostly zeroes, so it should compress sl@0: * very well: sl@0: */ sl@0: c_stream.next_in = uncompr; sl@0: c_stream.avail_in = (uInt)uncomprLen; sl@0: err = deflate(&c_stream, Z_NO_FLUSH); sl@0: CHECK_ERR(err, "deflate"); sl@0: if (c_stream.avail_in != 0) { sl@0: fprintf(stderr, "deflate not greedy\n"); sl@0: exit(1); sl@0: } sl@0: sl@0: /* Feed in already compressed data and switch to no compression: */ sl@0: deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); sl@0: c_stream.next_in = compr; sl@0: c_stream.avail_in = (uInt)comprLen/2; sl@0: err = deflate(&c_stream, Z_NO_FLUSH); sl@0: CHECK_ERR(err, "deflate"); sl@0: sl@0: /* Switch back to compressing mode: */ sl@0: deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); sl@0: c_stream.next_in = uncompr; sl@0: c_stream.avail_in = (uInt)uncomprLen; sl@0: err = deflate(&c_stream, Z_NO_FLUSH); sl@0: CHECK_ERR(err, "deflate"); sl@0: sl@0: err = deflate(&c_stream, Z_FINISH); sl@0: if (err != Z_STREAM_END) { sl@0: fprintf(stderr, "deflate should report Z_STREAM_END\n"); sl@0: exit(1); sl@0: } sl@0: err = deflateEnd(&c_stream); sl@0: CHECK_ERR(err, "deflateEnd"); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0824 sl@0: @SYMTestCaseDesc Inflate with large buffers test sl@0: @SYMTestPriority High sl@0: @SYMTestActions Tests inflate() with large buffers sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: void test_large_inflate( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: int err; sl@0: z_stream d_stream; /* decompression stream */ sl@0: sl@0: strcpy((char*)uncompr, "garbage"); sl@0: sl@0: d_stream.zalloc = (alloc_func)0; sl@0: d_stream.zfree = (free_func)0; sl@0: d_stream.opaque = (voidpf)0; sl@0: sl@0: d_stream.next_in = compr; sl@0: d_stream.avail_in = (uInt)comprLen; sl@0: sl@0: err = inflateInit(&d_stream); sl@0: CHECK_ERR(err, "inflateInit"); sl@0: sl@0: for (;;) { sl@0: d_stream.next_out = uncompr; /* discard the output */ sl@0: d_stream.avail_out = (uInt)uncomprLen; sl@0: err = inflate(&d_stream, Z_NO_FLUSH); sl@0: if (err == Z_STREAM_END) break; sl@0: CHECK_ERR(err, "large inflate"); sl@0: } sl@0: sl@0: err = inflateEnd(&d_stream); sl@0: CHECK_ERR(err, "inflateEnd"); sl@0: sl@0: if (d_stream.total_out != 2*uncomprLen + comprLen/2) { sl@0: fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); sl@0: exit(1); sl@0: } else { sl@0: printf("large_inflate(): OK\n"); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0825 sl@0: @SYMTestCaseDesc Test deflate() with full flush sl@0: @SYMTestPriority High sl@0: @SYMTestActions Test by flushing the output as random access is desired sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: void test_flush( sl@0: Byte *compr, sl@0: uLong *comprLen) sl@0: { sl@0: z_stream c_stream; /* compression stream */ sl@0: int err; sl@0: int len = strlen(hello)+1; sl@0: sl@0: c_stream.zalloc = (alloc_func)0; sl@0: c_stream.zfree = (free_func)0; sl@0: c_stream.opaque = (voidpf)0; sl@0: sl@0: err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); sl@0: CHECK_ERR(err, "deflateInit"); sl@0: sl@0: c_stream.next_in = (Bytef*)hello; sl@0: c_stream.next_out = compr; sl@0: c_stream.avail_in = 3; sl@0: c_stream.avail_out = (uInt)*comprLen; sl@0: err = deflate(&c_stream, Z_FULL_FLUSH); sl@0: CHECK_ERR(err, "deflate"); sl@0: sl@0: compr[3]++; /* force an error in first compressed block */ sl@0: c_stream.avail_in = len - 3; sl@0: sl@0: err = deflate(&c_stream, Z_FINISH); sl@0: if (err != Z_STREAM_END) { sl@0: CHECK_ERR(err, "deflate"); sl@0: } sl@0: err = deflateEnd(&c_stream); sl@0: CHECK_ERR(err, "deflateEnd"); sl@0: sl@0: *comprLen = c_stream.total_out; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0826 sl@0: @SYMTestCaseDesc Test the inflateSync() function sl@0: @SYMTestPriority High sl@0: @SYMTestActions Tests for reading of compressed data with damaged parts and check for errors. sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: void test_sync( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: int err; sl@0: z_stream d_stream; /* decompression stream */ sl@0: sl@0: strcpy((char*)uncompr, "garbage"); sl@0: sl@0: d_stream.zalloc = (alloc_func)0; sl@0: d_stream.zfree = (free_func)0; sl@0: d_stream.opaque = (voidpf)0; sl@0: sl@0: d_stream.next_in = compr; sl@0: d_stream.avail_in = 2; /* just read the zlib header */ sl@0: sl@0: err = inflateInit(&d_stream); sl@0: CHECK_ERR(err, "inflateInit"); sl@0: sl@0: d_stream.next_out = uncompr; sl@0: d_stream.avail_out = (uInt)uncomprLen; sl@0: sl@0: inflate(&d_stream, Z_NO_FLUSH); sl@0: CHECK_ERR(err, "inflate"); sl@0: sl@0: d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */ sl@0: err = inflateSync(&d_stream); /* but skip the damaged part */ sl@0: CHECK_ERR(err, "inflateSync"); sl@0: sl@0: err = inflate(&d_stream, Z_FINISH); sl@0: if (err != Z_DATA_ERROR) { sl@0: fprintf(stderr, "inflate should report DATA_ERROR\n"); sl@0: /* Because of incorrect adler32 */ sl@0: exit(1); sl@0: } sl@0: err = inflateEnd(&d_stream); sl@0: CHECK_ERR(err, "inflateEnd"); sl@0: sl@0: printf("after inflateSync(): hel%s\n", (char *)uncompr); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0827 sl@0: @SYMTestCaseDesc Deflation functionality test sl@0: @SYMTestPriority High sl@0: @SYMTestActions Test deflate() function with preset dictionary sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: void test_dict_deflate( sl@0: Byte *compr, sl@0: uLong comprLen) sl@0: { sl@0: z_stream c_stream; /* compression stream */ sl@0: int err; sl@0: sl@0: c_stream.zalloc = (alloc_func)0; sl@0: c_stream.zfree = (free_func)0; sl@0: c_stream.opaque = (voidpf)0; sl@0: sl@0: err = deflateInit(&c_stream, Z_BEST_COMPRESSION); sl@0: CHECK_ERR(err, "deflateInit"); sl@0: sl@0: err = deflateSetDictionary(&c_stream, sl@0: (const Bytef*)dictionary, sizeof(dictionary)); sl@0: CHECK_ERR(err, "deflateSetDictionary"); sl@0: sl@0: dictId = c_stream.adler; sl@0: c_stream.next_out = compr; sl@0: c_stream.avail_out = (uInt)comprLen; sl@0: sl@0: c_stream.next_in = (Bytef*)hello; sl@0: c_stream.avail_in = (uInt)strlen(hello)+1; sl@0: sl@0: err = deflate(&c_stream, Z_FINISH); sl@0: if (err != Z_STREAM_END) { sl@0: fprintf(stderr, "deflate should report Z_STREAM_END\n"); sl@0: exit(1); sl@0: } sl@0: err = deflateEnd(&c_stream); sl@0: CHECK_ERR(err, "deflateEnd"); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0828 sl@0: @SYMTestCaseDesc Inflation functionality test sl@0: @SYMTestPriority High sl@0: @SYMTestActions Test inflate() with a preset dictionary sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: void test_dict_inflate( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: int err; sl@0: z_stream d_stream; /* decompression stream */ sl@0: sl@0: strcpy((char*)uncompr, "garbage"); sl@0: sl@0: d_stream.zalloc = (alloc_func)0; sl@0: d_stream.zfree = (free_func)0; sl@0: d_stream.opaque = (voidpf)0; sl@0: sl@0: d_stream.next_in = compr; sl@0: d_stream.avail_in = (uInt)comprLen; sl@0: sl@0: err = inflateInit(&d_stream); sl@0: CHECK_ERR(err, "inflateInit"); sl@0: sl@0: d_stream.next_out = uncompr; sl@0: d_stream.avail_out = (uInt)uncomprLen; sl@0: sl@0: for (;;) { sl@0: err = inflate(&d_stream, Z_NO_FLUSH); sl@0: if (err == Z_STREAM_END) break; sl@0: if (err == Z_NEED_DICT) { sl@0: if (d_stream.adler != dictId) { sl@0: fprintf(stderr, "unexpected dictionary"); sl@0: exit(1); sl@0: } sl@0: err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, sl@0: sizeof(dictionary)); sl@0: } sl@0: CHECK_ERR(err, "inflate with dict"); sl@0: } sl@0: sl@0: err = inflateEnd(&d_stream); sl@0: CHECK_ERR(err, "inflateEnd"); sl@0: sl@0: if (strcmp((char*)uncompr, hello)) { sl@0: fprintf(stderr, "bad inflate with dict\n"); sl@0: exit(1); sl@0: } else { sl@0: printf("inflate with dictionary: %s\n", (char *)uncompr); sl@0: } sl@0: } sl@0: sl@0: /* =========================================================================== sl@0: * Usage: example [output.gz [input.gz]] sl@0: */ sl@0: sl@0: int main(/* sl@0: int argc, sl@0: char *argv[]*/) sl@0: { sl@0: Byte *compr, *uncompr; sl@0: uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */ sl@0: uLong uncomprLen = comprLen; sl@0: static const char* myVersion = ZLIB_VERSION; sl@0: sl@0: if (zlibVersion()[0] != myVersion[0]) { sl@0: fprintf(stderr, "incompatible zlib version\n"); sl@0: exit(1); sl@0: sl@0: } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { sl@0: fprintf(stderr, "warning: different zlib version\n"); sl@0: } sl@0: sl@0: compr = (Byte*)calloc((uInt)comprLen, 1); sl@0: uncompr = (Byte*)calloc((uInt)uncomprLen, 1); sl@0: /* compr and uncompr are cleared to avoid reading uninitialized sl@0: * data and to ensure that uncompr compresses well. sl@0: */ sl@0: if (compr == Z_NULL || uncompr == Z_NULL) { sl@0: printf("out of memory\n"); sl@0: exit(1); sl@0: } sl@0: test_compress(compr, comprLen, uncompr, uncomprLen); sl@0: sl@0: test_deflate(compr, comprLen); sl@0: test_inflate(compr, comprLen, uncompr, uncomprLen); sl@0: sl@0: test_large_deflate(compr, comprLen, uncompr, uncomprLen); sl@0: test_large_inflate(compr, comprLen, uncompr, uncomprLen); sl@0: sl@0: test_flush(compr, &comprLen); sl@0: test_sync(compr, comprLen, uncompr, uncomprLen); sl@0: comprLen = uncomprLen; sl@0: sl@0: test_dict_deflate(compr, comprLen); sl@0: test_dict_inflate(compr, comprLen, uncompr, uncomprLen); sl@0: sl@0: exit(0); sl@0: return 0; /* to avoid warning */ sl@0: }