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