os/ossrv/compressionlibs/ziplib/test/rtest/gzip/gzip.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "eustd.h"
    17 #include <ezcompressor.h>
    18 #include <ezdecompressor.h>
    19 #include <ezlib.h>
    20 #include <ezgzip.h>
    21 
    22 #include <f32file.h>
    23 
    24 void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname);
    25 
    26 /**
    27 @SYMTestCaseID          SYSLIB-EZLIB-CT-0829
    28 @SYMTestCaseDesc	    Gzip functionality test
    29 @SYMTestPriority 	    High
    30 @SYMTestActions  	    Decompress and compress a zip file read from the command line. 
    31 @SYMTestExpectedResults Test must not fail
    32 @SYMREQ                 REQ0000
    33 */
    34 
    35 LOCAL_C void doExampleL()
    36 	{
    37 	RFs rfs;
    38 	rfs.Connect();
    39 	TBool compress = ETrue;
    40 	TInt bufferSize = 0x8000;
    41 	TBool readHeader = EFalse;
    42 	TBool readTrailer = EFalse;
    43 	TBool noWork = EFalse;
    44 
    45     TInt cmdLineLen = User::CommandLineLength();
    46 
    47 	if (cmdLineLen <= 0)
    48 		{
    49 		_LIT(KUsage,"Usage:gzip [-dht] [-u bufferSize] filename\n");
    50 		console->Printf(KUsage);
    51 		User::Leave(1);
    52 		}
    53     //(cmdLineLen > 0) case
    54 	HBufC *argv = HBufC::NewLC(cmdLineLen);
    55 	TPtr cmd(argv->Des());
    56     User::CommandLine(cmd);
    57 
    58 	TLex arguments(*argv);
    59 	
    60 	TPtrC options(arguments.NextToken());
    61 	TBool expectBufferSize = EFalse;
    62 	_LIT(KBadBufferSize,"Bad buffersize specified\n");
    63 	_LIT(KUnknownOption,"Unknown Options %S\n");
    64 
    65 	
    66 	while (options[0]=='-' || expectBufferSize)
    67 		{
    68 		TInt i = 1;
    69 
    70 		if (expectBufferSize)
    71 			{
    72 			expectBufferSize = EFalse;
    73 			if (options.Length() == 0)
    74 				{
    75 				console->Printf(KBadBufferSize);
    76 				User::Leave(1);
    77 				}
    78 			else
    79 				{
    80 				TLex bufLex(options);
    81 				if (bufLex.Val(bufferSize) != KErrNone)
    82 					{
    83 					console->Printf(KBadBufferSize);
    84 					User::Leave(1);
    85 					}
    86 				}
    87 			}
    88 		else
    89 			{	
    90 			
    91 			while (i < options.Length())
    92 				{
    93 				if (options[i] == 'd')
    94 					compress = EFalse;
    95 				else if (options[i] == 'b')
    96 					{
    97 					if (i + 1 < options.Length())
    98 						{
    99 						TLex bufLex(options.Right(options.Length() - (i + 1)));
   100 						if (bufLex.Val(bufferSize) != KErrNone)
   101 							{
   102 							console->Printf(KBadBufferSize);
   103 							User::Leave(1);
   104 							}
   105 						}	
   106 					else
   107 						expectBufferSize = ETrue;
   108 					}
   109 				else if (options[i] == 'h')
   110 					readHeader = noWork = ETrue;
   111 				else if (options[i] == 't')
   112 					readTrailer = noWork = ETrue;
   113 				else 
   114 					{
   115 					console->Printf(KUnknownOption,&options);
   116 					i = options.Length();
   117 					}
   118 				i++;
   119 				}
   120 			
   121 			if (i == 1)
   122 				{
   123 				_LIT(KNoOption,"No option specified\n");
   124 				console->Printf(KNoOption);
   125 				User::Leave(1);
   126 				}
   127 			}
   128 		options.Set(arguments.NextToken());
   129 		}
   130 
   131 	console->Printf(_L("Buffer Size %d\n"),bufferSize);
   132 
   133 	if (readHeader)
   134 		{
   135 		ReadAndPrintHeaderL(rfs,options);
   136 		}
   137 
   138 	if (readTrailer)
   139 		{
   140 		TEZGZipTrailer trailer;
   141 		EZGZipFile::LocateAndReadTrailerL(rfs, options, trailer);
   142 		_LIT(KTrailer,"Crc = %d Size = %d\n");
   143 		console->Printf(KTrailer,trailer.iCrc32,trailer.iSize);
   144 		}
   145 
   146 	if (!noWork)
   147 		{
   148 		if (!compress)
   149 			{
   150 			
   151 			TPtrC inputFile(options);
   152 			
   153 			HBufC *uncompressedFile = HBufC::NewLC(inputFile.Length()+1);
   154 			_LIT(KUfl,"%S1");
   155 			uncompressedFile->Des().Format(KUfl,&inputFile);
   156 			
   157 			RFile output;
   158 			TInt err;
   159 			
   160 			_LIT(KInfo,"Decompressing file %S\n");
   161 			console->Printf(KInfo,&inputFile);
   162 			
   163 			err = output.Create(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive);
   164 			if (err == KErrAlreadyExists)
   165 				User::LeaveIfError(output.Open(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive));
   166 			else 
   167 				User::LeaveIfError(err);
   168 			CleanupClosePushL(output);
   169 			
   170 			CEZGZipToFile *def = CEZGZipToFile::NewLC(rfs,inputFile,output,bufferSize);
   171 			while (def->InflateL()){/*do nothing*/}
   172 			
   173 			_LIT(KHoorah,"Hoorah");
   174 			console->Printf(KHoorah);
   175 			
   176 			CleanupStack::PopAndDestroy(3);
   177 			}
   178 		else
   179 			{
   180 			TPtrC inputFile(options);
   181 			
   182 			HBufC *compressedFile = HBufC::NewLC(inputFile.Length()+3);
   183 			_LIT(KUfl,"%S.gz");
   184 			compressedFile->Des().Format(KUfl,&inputFile);
   185 			
   186 			RFile input;
   187 			
   188 			_LIT(KInfo,"Compressing file %S to %S\n");
   189 			console->Printf(KInfo,&inputFile,compressedFile);
   190 			
   191 			User::LeaveIfError(input.Open(rfs,inputFile,EFileStream | EFileRead | EFileShareAny));
   192 			CleanupClosePushL(input);
   193 
   194 			CEZFileToGZip *com = CEZFileToGZip::NewLC(rfs,*compressedFile,input,bufferSize);
   195 			while (com->DeflateL()){/*do nothing*/}
   196 			
   197 			_LIT(KHoorah,"Hoorah");
   198 			console->Printf(KHoorah);
   199 					
   200 			CleanupStack::PopAndDestroy(3);
   201 			}
   202 		}
   203 	CleanupStack::PopAndDestroy(1);
   204 	rfs.Close();
   205 	}
   206 
   207 
   208 void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname)
   209 	{
   210 	TEZGZipHeader header;
   211 	
   212 	if (!EZGZipFile::IsGzipFileL(rfs,fname))
   213 		{
   214 		_LIT(KNotGzipFile,"%S is not a gzip file\n");
   215 		console->Printf(KNotGzipFile,&fname);
   216 		User::Leave(1);
   217 		}
   218 	RFile gzipFile;
   219 	User::LeaveIfError(gzipFile.Open(rfs,fname,EFileStream | EFileRead | EFileShareAny));
   220 	EZGZipFile::ReadHeaderL(gzipFile,header);
   221 	
   222 	_LIT(KFileIds,"ID1 = %d ID2 = %d\n");
   223 	console->Printf(KFileIds,header.iId1,header.iId2);
   224 	_LIT(KCompressionMethod,"Compression Method = %d\n");
   225 	console->Printf(KCompressionMethod,header.iCompressionMethod);
   226 	_LIT(KFlags,"Flags = %d\n");
   227 	console->Printf(KFlags,header.iFlags);
   228 	_LIT(KTime,"Time Stamp = %d\n");
   229 	console->Printf(KTime,header.iTime);
   230 	_LIT(KExtraFlags,"Extra Flags %d\n");
   231 	console->Printf(KExtraFlags,header.iExtraFlags);
   232 	_LIT(KOS,"OS %d\n");
   233 	console->Printf(KOS,header.iOs);
   234 	if (header.iFlags & 4)
   235 		{
   236 		_LIT(KExtraLen,"Extra Length %d\n");
   237 		console->Printf(KExtraLen,header.iXlen);
   238 		HBufC *buf = HBufC::NewMaxLC(header.iExtra->Length());
   239 		buf->Des().Copy(*header.iExtra);
   240 		console->Printf(*buf);
   241 		CleanupStack::PopAndDestroy();
   242 		}
   243 	
   244 	if (header.iFlags & 8)
   245 		{
   246 		_LIT(KName,"Name: %S\n");
   247 		HBufC *buf = HBufC::NewMaxLC(header.iFname->Length());
   248 		buf->Des().Copy(*header.iFname);
   249 		console->Printf(KName,buf);
   250 		CleanupStack::PopAndDestroy();
   251 		}
   252 	
   253 	if (header.iFlags & 16)
   254 		{
   255 		_LIT(KComment,"Comment: %S\n");
   256 		HBufC *buf = HBufC::NewMaxLC(header.iComment->Length());
   257 		buf->Des().Copy(*header.iComment);
   258 		console->Printf(KComment,buf);
   259 		CleanupStack::PopAndDestroy();
   260 		}
   261 	
   262 	if (header.iFlags & 2)
   263 		{
   264 		_LIT(KCrc,"Crc16 = %d\n");
   265 		console->Printf(KCrc,header.iCrc);
   266 		}
   267 	}