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: /* ezexample.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: #include sl@0: sl@0: static RTest test(_L("ezexample.exe")); sl@0: sl@0: _LIT(KTestTitle, "Open Source Library Defect Tests."); 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: /* Test macro and function */ sl@0: void Check(TInt aValue, TInt aExpected, TInt aLine) sl@0: { sl@0: if (aValue != aExpected) sl@0: { sl@0: test.Printf(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue); sl@0: test.operator()(EFalse, aLine); sl@0: } sl@0: } sl@0: #define test2(a, b) Check(a, b, __LINE__) 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: void PrintString(TRefByValue aFmt, char* string); sl@0: void RunTest(); sl@0: sl@0: /** sl@0: Test compress() and uncompress() sl@0: sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0820-0001 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: void test_compress( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: test.Next(_L("test_compress")); sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-0820-0001 ")); 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: test2(err, Z_OK); sl@0: sl@0: strcpy((char*)uncompr, "garbage"); sl@0: sl@0: err = uncompress(uncompr, &uncomprLen, compr, comprLen); sl@0: test2(err, Z_OK); sl@0: sl@0: test2(strcmp((char*)uncompr, hello), 0); /* Fails if bad uncompress */ sl@0: sl@0: PrintString(_L("uncompress(): %S\n"), (char*)uncompr); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0821-0001 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: void test_deflate( sl@0: Byte *compr, sl@0: uLong comprLen) sl@0: { sl@0: test.Next(_L("test_deflate")); sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-0821-0001 ")); 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: test2(err, Z_OK); 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: test2(err, Z_OK); 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: test2(err, Z_OK); sl@0: } sl@0: sl@0: err = deflateEnd(&c_stream); sl@0: test2(err, Z_OK); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0822-0001 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: void test_inflate( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: test.Next(_L("test_inflate")); sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-0822-0001 ")); 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: test2(err, Z_OK); 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: test2(err, Z_OK); sl@0: } sl@0: sl@0: err = inflateEnd(&d_stream); sl@0: test2(err, Z_OK); sl@0: sl@0: test2(strcmp((char*)uncompr, hello), 0); /* Fails if bad inflate */ sl@0: sl@0: PrintString(_L("inflate(): %S\n"), (char*)uncompr); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0823-0001 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: void test_large_deflate( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: test.Next(_L("test_large_deflate")); sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-0823-0001 ")); 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: test2(err, Z_OK); 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: test2(err, Z_OK); sl@0: sl@0: test2(c_stream.avail_in, 0); /* Fails if deflate not greedy */ 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: test2(err, Z_OK); 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: test2(err, Z_OK); sl@0: sl@0: err = deflate(&c_stream, Z_FINISH); sl@0: test2(err, Z_STREAM_END); sl@0: sl@0: err = deflateEnd(&c_stream); sl@0: test2(err, Z_OK); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0824-0001 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: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-0824-0001 test_large_inflate ")); 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: test2(err, Z_OK); 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: test2(err, Z_OK); sl@0: } sl@0: sl@0: err = inflateEnd(&d_stream); sl@0: test2(err, Z_OK); sl@0: sl@0: test2(d_stream.total_out, (2*uncomprLen + comprLen/2)); /* Fails if bad large inflate */ sl@0: sl@0: test.Printf(_L("large_inflate(): OK\n")); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0825-0001 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: void test_flush( sl@0: Byte *compr, sl@0: uLong *comprLen) sl@0: { sl@0: test.Next(_L("test_flush")); sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-0825-0001 ")); 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: test2(err, Z_OK); 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: test2(err, Z_OK); 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: test2(err, Z_OK); sl@0: } sl@0: err = deflateEnd(&c_stream); sl@0: test2(err, Z_OK); sl@0: sl@0: *comprLen = c_stream.total_out; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0826-0001 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: void test_sync( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: test.Next(_L("test_sync")); sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-0826-0001 ")); 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: test2(err, Z_OK); 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: test2(err, Z_OK); 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: test2(err, Z_OK); sl@0: sl@0: err = inflate(&d_stream, Z_FINISH); sl@0: test2(err, Z_DATA_ERROR); /* inflate should report DATA_ERROR because of incorrect adler32 */ sl@0: sl@0: err = inflateEnd(&d_stream); sl@0: test2(err, Z_OK); sl@0: sl@0: PrintString(_L("after inflateSync(): hel%S\n"), (char*)uncompr); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0827-0001 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: void test_dict_deflate( sl@0: Byte *compr, sl@0: uLong comprLen) sl@0: { sl@0: test.Next(_L("test_dict_deflate")); sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-0827-0001 ")); 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: test2(err, Z_OK); sl@0: sl@0: err = deflateSetDictionary(&c_stream, sl@0: (const Bytef*)dictionary, sizeof(dictionary)); sl@0: test2(err, Z_OK); 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: test2(err, Z_STREAM_END); sl@0: sl@0: err = deflateEnd(&c_stream); sl@0: test2(err, Z_OK); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0828-0001 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: void test_dict_inflate( sl@0: Byte *compr, uLong comprLen, Byte *uncompr, sl@0: uLong uncomprLen) sl@0: { sl@0: test.Next(_L("test_dict_inflate")); sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-0828-0001 ")); 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: test2(err, Z_OK); 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: test2(d_stream.adler, dictId); /* Fails if unexpected dictionary */ sl@0: sl@0: err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, sl@0: sizeof(dictionary)); sl@0: } sl@0: test2(err, Z_OK); sl@0: } sl@0: sl@0: err = inflateEnd(&d_stream); sl@0: test2(err, Z_OK); sl@0: sl@0: test2(strcmp((char*)uncompr, hello), 0); /* Fails if bad inflate with dictionary */ sl@0: sl@0: PrintString(_L("inflate with dictionary: %S\n"), (char*)uncompr); sl@0: } sl@0: sl@0: /* =========================================================================== sl@0: * Usage: example [output.gz [input.gz]] sl@0: */ sl@0: sl@0: void PrintString(TRefByValue aFmt, char* string) sl@0: { sl@0: TPtrC8 ptrc8; sl@0: TBuf<100> buf; sl@0: ptrc8.Set((TUint8*)string, strlen(string)); sl@0: buf.Copy(ptrc8); sl@0: test.Printf(aFmt, &buf); sl@0: } sl@0: sl@0: void RunTest() 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: test2(zlibVersion()[0], myVersion[0]); /* Fails if incompatible zlib version */ sl@0: sl@0: if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { sl@0: test.Printf(_L("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: test2((compr == Z_NULL || uncompr == Z_NULL), 0); /* Fails if out of memory */ 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: delete compr; sl@0: delete uncompr; sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: test.Printf(_L("\n")); sl@0: test.Title(); sl@0: test.Start(KTestTitle); sl@0: sl@0: CTrapCleanup* cleanup = CTrapCleanup::New(); sl@0: sl@0: TRAPD(err, RunTest()); sl@0: test2(err, KErrNone); sl@0: sl@0: test.End(); sl@0: test.Close(); sl@0: delete cleanup; sl@0: sl@0: __UHEAP_MARKEND; sl@0: return KErrNone; sl@0: }