sl@0: sl@0: // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Name : ulibz_test.cpp sl@0: // sl@0: // sl@0: sl@0: sl@0: #include "ulibz.h" sl@0: #include sl@0: sl@0: sl@0: // ============================ MEMBER FUNCTIONS =============================== sl@0: sl@0: sl@0: /* sl@0: ------------------------------------------------------------------------------- sl@0: Function Name : CTestlibz::libzcomp_decomp() sl@0: API Tested : Compress() and Decompress() sl@0: sl@0: ------------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: sl@0: TInt CTestlibz::libzcomp_decomp() sl@0: { sl@0: __UHEAP_MARK; sl@0: int err = 0; sl@0: Byte compr[20]; sl@0: Byte uncompr[20]; sl@0: uLong comprLen; sl@0: uLong uncomprLen; sl@0: sl@0: const char hello[] = "hello, hello!"; sl@0: sl@0: comprLen = sizeof(compr); sl@0: uncomprLen = sizeof(uncompr); sl@0: sl@0: uLong len = (uLong)strlen(hello)+1; sl@0: sl@0: INFO_PRINTF1(_L("compress()\n")); sl@0: err = compress(compr, &comprLen, (const Bytef*)hello, len); sl@0: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF1(_L("compress failed\n")); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: strcpy((char*)uncompr, "garbage"); sl@0: sl@0: INFO_PRINTF1(_L("uncompress()\n")); sl@0: err = uncompress(uncompr, &uncomprLen, compr, comprLen); sl@0: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF1(_L("uncompress failed\n")); sl@0: return KErrGeneral; sl@0: } sl@0: if (strcmp((char*)uncompr, hello)) sl@0: { sl@0: INFO_PRINTF1(_L("Bad uncompress\n")); sl@0: return KErrGeneral; sl@0: } sl@0: __UHEAP_MARKEND; sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: /* sl@0: ------------------------------------------------------------------------------- sl@0: Function Name : CTestlibz::libzdefl_Infl() sl@0: API Tested : deflate(), deflateInit(), deflateEnd(). sl@0: sl@0: ------------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: sl@0: TInt CTestlibz::libzdefl_Infl() sl@0: { sl@0: __UHEAP_MARK; sl@0: int ret; sl@0: Byte* compr; sl@0: Byte* uncompr; sl@0: sl@0: uLong comprLen = 30; sl@0: uLong uncomprLen = 30; sl@0: sl@0: compr = (Byte*)calloc((uInt)comprLen, 1); sl@0: if(compr == NULL) sl@0: { sl@0: INFO_PRINTF1(_L("Could not allocate memory for compr.")); sl@0: return KErrNoMemory; sl@0: } sl@0: sl@0: uncompr = (Byte*)calloc((uInt)uncomprLen, 1); sl@0: if(uncompr == NULL) sl@0: { sl@0: INFO_PRINTF1(_L("Could not allocate memory for uncompr.")); sl@0: free(compr); sl@0: return KErrNoMemory; sl@0: } sl@0: sl@0: ret = libzdeflate(compr, comprLen); sl@0: if(ret == KErrGeneral) sl@0: { sl@0: free(compr); sl@0: free(uncompr); sl@0: return KErrGeneral; sl@0: } sl@0: ret = libzinflate(compr, comprLen, uncompr, uncomprLen); sl@0: free(compr); sl@0: free(uncompr); sl@0: __UHEAP_MARKEND; sl@0: return ret; sl@0: sl@0: } sl@0: sl@0: /* sl@0: ------------------------------------------------------------------------------- sl@0: Function Name : CTestlibz::libzdeflate() sl@0: API Tested : deflate(), deflateInit(), deflateEnd(). sl@0: sl@0: ------------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: TInt CTestlibz::libzdeflate(Byte * compr, uLong comprLen) sl@0: { sl@0: z_stream c_stream; /* compression stream */ sl@0: sl@0: int err; sl@0: sl@0: const char hello[] = "hello, hello!"; sl@0: sl@0: uLong len = (uLong)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: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("deflateInit failed with err %d"), err); sl@0: return KErrGeneral; 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 != len && c_stream.total_out < comprLen) sl@0: { 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: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("deflate failed with err %d"), err); sl@0: return KErrGeneral; sl@0: } sl@0: } sl@0: sl@0: /* Finish the stream, still forcing small buffers: */ sl@0: for (;;) sl@0: { sl@0: c_stream.avail_out = 1; sl@0: err = deflate(&c_stream, Z_FINISH); sl@0: if (err == Z_STREAM_END) sl@0: break; sl@0: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("deflate failed with err %d"), err); sl@0: return KErrGeneral; sl@0: } sl@0: } sl@0: INFO_PRINTF1(_L("deflateEnd()\n")); sl@0: err = deflateEnd(&c_stream); sl@0: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("deflateEnd failed with err %d"), err); sl@0: return KErrGeneral; sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: /* sl@0: ------------------------------------------------------------------------------- sl@0: Function Name : CTestlibz::libzinflate() sl@0: API Tested : inflate(), inflateInit(),inflateEnd(). sl@0: sl@0: ------------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: TInt CTestlibz::libzinflate(Byte * compr,uLong comprLen, Byte * uncompr, uLong uncomprLen) sl@0: { sl@0: z_stream d_stream; /* decompression stream */ sl@0: int err; sl@0: const char hello[] = "hello, hello!"; 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: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("inflateInit failed with err %d"), err); sl@0: return KErrGeneral; sl@0: } 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) sl@0: break; sl@0: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("inflate failed with err %d"), err); sl@0: return KErrGeneral; sl@0: } sl@0: } sl@0: sl@0: err = inflateEnd(&d_stream); sl@0: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("inflateEnd failed with err %d"), err); sl@0: return KErrGeneral; sl@0: } sl@0: if (strcmp((char*)uncompr, hello)) sl@0: { sl@0: INFO_PRINTF1(_L("Bad Inflate\n")); sl@0: return KErrGeneral; sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: /* sl@0: ------------------------------------------------------------------------------- sl@0: Function Name : CTestlibz::libzgzio() sl@0: API Tested : gzopen(), gzputc(), gzprintf(), gzseek(), gzclose(), sl@0: gzread(), gzgetc(), gzungetc(), gzgets(), gzputs(), sl@0: gzerror(), gztell(). sl@0: sl@0: ------------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: TInt CTestlibz::libzgzio() sl@0: { sl@0: __UHEAP_MARK; sl@0: #ifdef NO_GZCOMPRESS sl@0: INFO_PRINT1(_L("NO_GZCOMPRESS -- gz* functions cannot compress")); sl@0: return KErrNone; sl@0: #else sl@0: const char Buffer[] = "Symbian Libz!"; sl@0: Byte uncompr[15]; sl@0: char fname[] = "C:\\Libz\\Test1\\bye.gz"; sl@0: int err = 0; sl@0: int len; sl@0: gzFile file; sl@0: z_off_t pos; sl@0: sl@0: err = iRfile.Create(iRfs, KGZFILE, EFileShareAny); sl@0: if( err != KErrNone && err != KErrAlreadyExists ) sl@0: { sl@0: INFO_PRINTF1(_L("File create successfully\n")); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: len = (int)strlen(Buffer)+1; sl@0: sl@0: //-------------gzopen()------------------ sl@0: INFO_PRINTF1(_L("gzopen()\n")); sl@0: file = gzopen(fname, "wb"); sl@0: if (file == NULL) { sl@0: INFO_PRINTF1(_L("gzopen error")); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: //-------------gzputc() ------------ sl@0: INFO_PRINTF1(_L("gputc()\n")); sl@0: gzputc(file, 'S'); sl@0: if (gzputs(file, "ymbian") != 6) { sl@0: INFO_PRINTF2(_L("gzputs err: %s\n"), gzerror(file, &err)); sl@0: return KErrGeneral; sl@0: } sl@0: //-------------gzprintf() ------------ sl@0: INFO_PRINTF1(_L("gzprintf()\n")); sl@0: if (gzprintf(file, " %s!", "Libz") != 6) { sl@0: INFO_PRINTF2(_L("gzprintf err: %s\n"), gzerror(file, &err)); sl@0: return KErrGeneral; sl@0: } sl@0: //-------------gzseek() ------------ sl@0: INFO_PRINTF1(_L("gzseek()\n")); sl@0: if(gzseek(file, 1L, SEEK_CUR) != 14){ /* add one zero byte */ sl@0: INFO_PRINTF2(_L("gzseek err: %s\n"), gzerror(file, &err)); sl@0: return KErrGeneral; sl@0: } sl@0: //-------------gzclose() ------------ sl@0: INFO_PRINTF1(_L("gzclose()\n")); sl@0: if(gzclose(file) == Z_ERRNO){ sl@0: INFO_PRINTF2(_L("gzclose err: %s\n"), gzerror(file, &err)); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: //-------------gzopen()------------------ sl@0: INFO_PRINTF1(_L("gzopen()\n")); sl@0: file = gzopen(fname, "rb"); sl@0: if (file == NULL) { sl@0: INFO_PRINTF1(_L("gzopen error\n")); sl@0: return KErrGeneral; sl@0: } sl@0: strcpy((char*)uncompr, "garbage"); sl@0: sl@0: //-------------gzread()------------------ sl@0: INFO_PRINTF1(_L("gzread()\n")); sl@0: if (gzread(file, uncompr, sizeof(uncompr)) != len) { sl@0: INFO_PRINTF2(_L("gzread err: %s\n"), gzerror(file, &err)); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: if (strcmp((char*)uncompr, Buffer)) sl@0: { sl@0: INFO_PRINTF2(_L("bad gzread: %s\n"), (char*)uncompr); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: //-------------gzseek() & gztell()----------------- sl@0: INFO_PRINTF1(_L("gzseek & gztell()\n")); sl@0: pos = gzseek(file, -7L, SEEK_CUR); sl@0: if (gztell(file) != pos || pos != 7) sl@0: { sl@0: INFO_PRINTF3(_L("gzseek error, pos=%ld, gztell=%ld\n"), (long)pos, (long)gztell(file)); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: //-------------gzgetc()------------------ sl@0: INFO_PRINTF1(_L("gzgetc()\n")); sl@0: if (gzgetc(file) != ' ') sl@0: { sl@0: INFO_PRINTF1(_L("gzgetc error")); sl@0: return KErrGeneral; sl@0: sl@0: } sl@0: sl@0: //-------------gzungetc()------------------ sl@0: INFO_PRINTF1(_L("gzungetc\n")); sl@0: if (gzungetc(' ', file) != ' ') sl@0: { sl@0: INFO_PRINTF1(_L("gzungetc error\n")); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: //-------------gzgets()------------------ sl@0: INFO_PRINTF1(_L("gzgets()\n")); sl@0: gzgets(file, (char*)uncompr, sizeof(uncompr)); sl@0: if (strlen((char*)uncompr) != 6) sl@0: { sl@0: /* " Libz!" */ sl@0: INFO_PRINTF2(_L("gzgets err after gzseek: %s\n"), gzerror(file, &err)); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: if (strcmp((char*)uncompr, Buffer + 7)) sl@0: { sl@0: INFO_PRINTF1(_L("bad gzgets after gzseek\n")); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: //-------------gzclose() ------------ sl@0: if(gzclose(file) == Z_ERRNO) sl@0: { sl@0: INFO_PRINTF2(_L("gzclose err: %s\n"), gzerror(file, &err)); sl@0: return KErrGeneral; sl@0: } sl@0: #endif sl@0: __UHEAP_MARKEND; sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: sl@0: /* sl@0: ------------------------------------------------------------------------------- sl@0: Function Name : CTestlibz::libzversion() sl@0: API Tested : zlibversion() sl@0: sl@0: ------------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: TInt CTestlibz::libzversion() sl@0: { sl@0: __UHEAP_MARK; sl@0: char buf[]="1.2.3"; sl@0: INFO_PRINTF1(_L("zlibVersion()\n")); sl@0: if( (strcmp( buf, zlibVersion() ) != 0) ){ sl@0: INFO_PRINTF1(_L("zlibversion failed\n")); sl@0: return KErrGeneral; sl@0: } sl@0: __UHEAP_MARKEND; sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: