os/ossrv/compressionlibs/ziplib/test/tef/ulibz/src/ulibz_test.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 
     2 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 // All rights reserved.
     4 // This component and the accompanying materials are made available
     5 // under the terms of "Eclipse Public License v1.0"
     6 // which accompanies this distribution, and is available
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 //
     9 // Initial Contributors:
    10 // Nokia Corporation - initial contribution.
    11 //
    12 // Contributors:
    13 //
    14 // Description:
    15 // Name        : ulibz_test.cpp
    16 // 
    17 //
    18 
    19  
    20 #include "ulibz.h"
    21 #include <zlib.h>
    22 
    23 
    24 // ============================ MEMBER FUNCTIONS ===============================
    25 
    26 
    27 /*
    28 -------------------------------------------------------------------------------
    29 Function Name		: CTestlibz::libzcomp_decomp()
    30 API Tested			: Compress() and Decompress()
    31 
    32 -------------------------------------------------------------------------------
    33 */
    34 
    35 	
    36 TInt CTestlibz::libzcomp_decomp()
    37 	{
    38 	__UHEAP_MARK;
    39     int err = 0;
    40 	Byte compr[20];
    41 	Byte uncompr[20];
    42 	uLong comprLen;
    43 	uLong uncomprLen;
    44 
    45 	const char hello[] = "hello, hello!";
    46 
    47 	comprLen = sizeof(compr);
    48 	uncomprLen = sizeof(uncompr);
    49 	
    50     uLong len = (uLong)strlen(hello)+1;
    51 
    52 	INFO_PRINTF1(_L("compress()\n"));
    53     err = compress(compr, &comprLen, (const Bytef*)hello, len);
    54    	if(err != Z_OK)
    55 		{
    56 			INFO_PRINTF1(_L("compress failed\n"));
    57 			return KErrGeneral;
    58 		}
    59 
    60     strcpy((char*)uncompr, "garbage");
    61 
    62 	INFO_PRINTF1(_L("uncompress()\n"));
    63     err = uncompress(uncompr, &uncomprLen, compr, comprLen);
    64     	if(err != Z_OK)
    65 		{
    66 			INFO_PRINTF1(_L("uncompress failed\n"));
    67 			return KErrGeneral;
    68 		}
    69     if (strcmp((char*)uncompr, hello)) 
    70     {
    71         INFO_PRINTF1(_L("Bad uncompress\n"));
    72        	return KErrGeneral;
    73     } 
    74  	__UHEAP_MARKEND;
    75 	return KErrNone;
    76 	}
    77 
    78 	
    79 /*
    80 -------------------------------------------------------------------------------
    81 Function Name		: CTestlibz::libzdefl_Infl()
    82 API Tested			: deflate(), deflateInit(), deflateEnd().
    83 
    84 -------------------------------------------------------------------------------
    85 */
    86 
    87 
    88 TInt CTestlibz::libzdefl_Infl()
    89 	{
    90 	__UHEAP_MARK;
    91 	int ret;
    92 	Byte* compr;
    93 	Byte* uncompr;
    94 	
    95 	uLong comprLen = 30;
    96 	uLong uncomprLen = 30;
    97 	
    98 	compr = (Byte*)calloc((uInt)comprLen, 1);
    99 	if(compr == NULL)
   100 		{
   101 		INFO_PRINTF1(_L("Could not allocate memory for compr."));
   102 		return KErrNoMemory;
   103 		}
   104 	
   105 	uncompr = (Byte*)calloc((uInt)uncomprLen, 1);	
   106 	if(uncompr == NULL)
   107 		{
   108 		INFO_PRINTF1(_L("Could not allocate memory for uncompr."));
   109 		free(compr);
   110 		return KErrNoMemory;
   111 		}
   112 	
   113 	ret = libzdeflate(compr, comprLen);
   114 	if(ret == KErrGeneral)
   115 	{
   116 		free(compr);
   117 		free(uncompr);
   118 		return KErrGeneral;
   119 	}
   120 	ret = libzinflate(compr, comprLen, uncompr, uncomprLen);
   121 	free(compr);
   122 	free(uncompr);
   123 	__UHEAP_MARKEND;
   124 	return ret;
   125 
   126 	}
   127 
   128 /*
   129 -------------------------------------------------------------------------------
   130 Function Name		: CTestlibz::libzdeflate()
   131 API Tested			: deflate(), deflateInit(), deflateEnd().
   132 
   133 -------------------------------------------------------------------------------
   134 */
   135 
   136 TInt CTestlibz::libzdeflate(Byte * compr, uLong comprLen)
   137 	{
   138     z_stream c_stream; 			/* compression stream */
   139 
   140 	int err;
   141 
   142 	const char hello[] = "hello, hello!";
   143 
   144     uLong len = (uLong)strlen(hello)+1;
   145 
   146     c_stream.zalloc = (alloc_func)0;
   147     c_stream.zfree = (free_func)0;
   148     c_stream.opaque = (voidpf)0;
   149 
   150     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
   151    	if(err != Z_OK)
   152 		{
   153 			INFO_PRINTF2(_L("deflateInit failed with err %d"), err);
   154 			return KErrGeneral;
   155 		}
   156     c_stream.next_in  = (Bytef*)hello;
   157     c_stream.next_out = compr;
   158 
   159     while (c_stream.total_in != len && c_stream.total_out < comprLen) 
   160     {
   161         c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
   162         err = deflate(&c_stream, Z_NO_FLUSH);
   163    		if(err != Z_OK)
   164 		{
   165 			INFO_PRINTF2(_L("deflate failed with err %d"), err);
   166 			return KErrGeneral;
   167 		}
   168     }
   169 
   170     /* Finish the stream, still forcing small buffers: */
   171     for (;;) 
   172 	{
   173         c_stream.avail_out = 1;
   174         err = deflate(&c_stream, Z_FINISH);
   175         if (err == Z_STREAM_END) 
   176 			break;
   177      	if(err != Z_OK)
   178 		{
   179 			INFO_PRINTF2(_L("deflate failed with err %d"), err);
   180 			return KErrGeneral;
   181 		}
   182     }
   183 	INFO_PRINTF1(_L("deflateEnd()\n"));
   184     err = deflateEnd(&c_stream);
   185     if(err != Z_OK)
   186 		{
   187 			INFO_PRINTF2(_L("deflateEnd failed with err %d"), err);
   188 			return KErrGeneral;
   189 		}
   190 	return KErrNone;
   191 	}
   192 	
   193 
   194 /*
   195 -------------------------------------------------------------------------------
   196 Function Name		: CTestlibz::libzinflate()
   197 API Tested			: inflate(), inflateInit(),inflateEnd(). 
   198 
   199 -------------------------------------------------------------------------------
   200 */
   201 
   202 TInt CTestlibz::libzinflate(Byte * compr,uLong  comprLen, Byte *  uncompr, uLong uncomprLen)
   203 	{
   204     z_stream d_stream; /* decompression stream */
   205 	int err;
   206 	const char hello[] = "hello, hello!";
   207 
   208     strcpy((char*)uncompr, "garbage");
   209 
   210     d_stream.zalloc = (alloc_func)0;
   211     d_stream.zfree = (free_func)0;
   212     d_stream.opaque = (voidpf)0;
   213 
   214     d_stream.next_in  = compr;
   215     d_stream.avail_in = 0;
   216     d_stream.next_out = uncompr;
   217 
   218     err = inflateInit(&d_stream);
   219    	if(err != Z_OK)
   220 		{
   221 			INFO_PRINTF2(_L("inflateInit failed with err %d"), err);
   222 			return KErrGeneral;
   223 		}
   224 
   225     while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
   226         d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
   227         err = inflate(&d_stream, Z_NO_FLUSH);
   228         if (err == Z_STREAM_END) 
   229 			break;
   230 	   	if(err != Z_OK)
   231 		{
   232 			INFO_PRINTF2(_L("inflate failed with err %d"), err);
   233 			return KErrGeneral;
   234 		}
   235     }
   236 
   237     err = inflateEnd(&d_stream);
   238    	if(err != Z_OK)
   239 	{
   240 		INFO_PRINTF2(_L("inflateEnd failed with err %d"), err);
   241 		return KErrGeneral;
   242 	}
   243     if (strcmp((char*)uncompr, hello)) 
   244 	{
   245 		INFO_PRINTF1(_L("Bad Inflate\n"));
   246 		return KErrGeneral;
   247     } 
   248     return KErrNone;
   249 	}
   250 	
   251 /*
   252 -------------------------------------------------------------------------------
   253 Function Name		: CTestlibz::libzgzio()
   254 API Tested			: gzopen(), gzputc(), gzprintf(), gzseek(), gzclose(), 
   255 					  gzread(), gzgetc(), gzungetc(), gzgets(), gzputs(), 
   256 					  gzerror(), gztell().
   257 
   258 -------------------------------------------------------------------------------
   259 */
   260 
   261 TInt CTestlibz::libzgzio()
   262 	{
   263 	__UHEAP_MARK;
   264 #ifdef NO_GZCOMPRESS
   265     INFO_PRINT1(_L("NO_GZCOMPRESS -- gz* functions cannot compress"));
   266 	return KErrNone;
   267 #else
   268 	const char Buffer[] = "Symbian Libz!";
   269 	Byte uncompr[15];
   270 	char fname[] = "C:\\Libz\\Test1\\bye.gz";
   271 	int err = 0;
   272 	int len;
   273     gzFile file;
   274     z_off_t pos;
   275 
   276 	err = iRfile.Create(iRfs, KGZFILE, EFileShareAny);
   277 	if( err != KErrNone && err != KErrAlreadyExists )
   278 		{
   279 		INFO_PRINTF1(_L("File create successfully\n"));
   280 		return KErrGeneral;
   281 		}
   282 
   283 	len = (int)strlen(Buffer)+1;
   284 
   285 	//-------------gzopen()------------------
   286 	INFO_PRINTF1(_L("gzopen()\n"));
   287 	file = gzopen(fname, "wb");
   288     if (file == NULL) {
   289         INFO_PRINTF1(_L("gzopen error"));
   290         return KErrGeneral;
   291     }
   292 
   293 	//-------------gzputc() ------------
   294 	INFO_PRINTF1(_L("gputc()\n"));
   295     gzputc(file, 'S');
   296     if (gzputs(file, "ymbian") != 6) {
   297         INFO_PRINTF2(_L("gzputs err: %s\n"), gzerror(file, &err));
   298         return KErrGeneral;
   299     }
   300 	//-------------gzprintf() ------------
   301 	INFO_PRINTF1(_L("gzprintf()\n"));
   302     if (gzprintf(file, " %s!", "Libz") != 6) {
   303         INFO_PRINTF2(_L("gzprintf err: %s\n"), gzerror(file, &err));
   304         return KErrGeneral;
   305     }
   306 	//-------------gzseek() ------------
   307 	INFO_PRINTF1(_L("gzseek()\n"));
   308     if(gzseek(file, 1L, SEEK_CUR) != 14){ /* add one zero byte */
   309 		INFO_PRINTF2(_L("gzseek err: %s\n"), gzerror(file, &err));
   310 		return KErrGeneral;
   311 	}
   312 	//-------------gzclose() ------------
   313 	INFO_PRINTF1(_L("gzclose()\n"));
   314     if(gzclose(file) == Z_ERRNO){
   315 		INFO_PRINTF2(_L("gzclose err: %s\n"), gzerror(file, &err));
   316 		return KErrGeneral;
   317 	}
   318 
   319 	//-------------gzopen()------------------
   320 	INFO_PRINTF1(_L("gzopen()\n"));
   321 	file = gzopen(fname, "rb");
   322     if (file == NULL) {
   323         INFO_PRINTF1(_L("gzopen error\n"));
   324         return KErrGeneral;
   325     }
   326 	strcpy((char*)uncompr, "garbage");
   327 
   328 	//-------------gzread()------------------
   329 	INFO_PRINTF1(_L("gzread()\n"));
   330     if (gzread(file, uncompr, sizeof(uncompr)) != len) {
   331         INFO_PRINTF2(_L("gzread err: %s\n"), gzerror(file, &err));
   332         return KErrGeneral;
   333     }
   334 
   335     if (strcmp((char*)uncompr, Buffer)) 
   336     {
   337         INFO_PRINTF2(_L("bad gzread: %s\n"), (char*)uncompr);
   338  		return KErrGeneral;
   339     }
   340 
   341 	//-------------gzseek() & gztell()-----------------
   342 	INFO_PRINTF1(_L("gzseek & gztell()\n"));
   343     pos = gzseek(file, -7L, SEEK_CUR);
   344     if (gztell(file) != pos || pos != 7) 
   345     {
   346        INFO_PRINTF3(_L("gzseek error, pos=%ld, gztell=%ld\n"), (long)pos, (long)gztell(file));
   347        return KErrGeneral;
   348     }
   349 
   350 	//-------------gzgetc()------------------
   351 	INFO_PRINTF1(_L("gzgetc()\n"));
   352     if (gzgetc(file) != ' ') 
   353     {
   354         INFO_PRINTF1(_L("gzgetc error"));
   355         return KErrGeneral;
   356       
   357     }
   358     
   359 	//-------------gzungetc()------------------
   360 	INFO_PRINTF1(_L("gzungetc\n"));
   361     if (gzungetc(' ', file) != ' ') 
   362     {
   363         INFO_PRINTF1(_L("gzungetc error\n"));
   364         return KErrGeneral;
   365     }
   366 
   367 	//-------------gzgets()------------------
   368 	INFO_PRINTF1(_L("gzgets()\n"));
   369     gzgets(file, (char*)uncompr, sizeof(uncompr)); 
   370     if (strlen((char*)uncompr) != 6) 
   371     { 
   372     	/* " Libz!" */
   373         INFO_PRINTF2(_L("gzgets err after gzseek: %s\n"), gzerror(file, &err));
   374         return KErrGeneral;
   375     }
   376     
   377     if (strcmp((char*)uncompr, Buffer + 7)) 
   378     {
   379         INFO_PRINTF1(_L("bad gzgets after gzseek\n"));
   380         return KErrGeneral;
   381     }
   382 
   383 	//-------------gzclose() ------------
   384     if(gzclose(file) == Z_ERRNO)
   385     {
   386 		INFO_PRINTF2(_L("gzclose err: %s\n"), gzerror(file, &err));
   387 		return KErrGeneral;
   388 	}
   389 #endif
   390 	__UHEAP_MARKEND;
   391 	return KErrNone;
   392 	}
   393 	
   394 
   395 
   396 /*
   397 -------------------------------------------------------------------------------
   398 Function Name		: CTestlibz::libzversion()
   399 API Tested			: zlibversion()
   400 
   401 -------------------------------------------------------------------------------
   402 */
   403 
   404 TInt CTestlibz::libzversion()
   405 	{
   406 	__UHEAP_MARK;
   407 	char buf[]="1.2.3";
   408 	INFO_PRINTF1(_L("zlibVersion()\n"));
   409 	if( (strcmp( buf, zlibVersion() ) != 0) ){
   410 		INFO_PRINTF1(_L("zlibversion failed\n"));
   411 		return KErrGeneral;
   412 		}
   413 	__UHEAP_MARKEND;
   414 	return KErrNone;
   415 	}
   416 	
   417 	
   418 	
   419 	
   420