compressionlibs/ziplib/src/ezlib/gzip.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 <ezgzip.h>
       
    17 #include "libzcore.h"
       
    18 
       
    19 const TUint8 EZGZipFile::ID1 = 31;
       
    20 const TUint8 EZGZipFile::ID2 = 139;
       
    21 
       
    22 /**
       
    23 Constructor
       
    24 */
       
    25 EXPORT_C TEZGZipHeader::TEZGZipHeader() : iId1(EZGZipFile::ID1), iId2(EZGZipFile::ID2), iCompressionMethod(8), iFlags(0), iTime(0), 
       
    26 	iExtraFlags(0), iOs(255), iXlen(0), iExtra(NULL), iFname(NULL), iComment(NULL), iCrc(0)
       
    27 	{
       
    28 	
       
    29 	}
       
    30 
       
    31 /**
       
    32 Destructor
       
    33 */
       
    34 EXPORT_C TEZGZipHeader::~TEZGZipHeader()
       
    35 	{
       
    36 	delete iExtra;
       
    37 	delete iFname;
       
    38 	delete iComment;
       
    39 	}
       
    40 
       
    41 /**
       
    42 Constructor
       
    43 */
       
    44 EXPORT_C TEZGZipTrailer::TEZGZipTrailer() : iCrc32(0), iSize(0)
       
    45 	{
       
    46 
       
    47 	}
       
    48 
       
    49 /**
       
    50 Constructor
       
    51 
       
    52 @param aCrc the CRC to use for archive checking
       
    53 @param aSize the size of the trailer
       
    54 */
       
    55 EXPORT_C TEZGZipTrailer::TEZGZipTrailer(TInt32 aCrc, TInt32 aSize) : iCrc32(aCrc), iSize(aSize)
       
    56 	{
       
    57 
       
    58 	}
       
    59 
       
    60 //--------------------------------------------------------------------------------------------------------
       
    61 
       
    62 /**
       
    63 Read the zip header from the specified zip file into the TEZGZipHeader object
       
    64 
       
    65 @param aFile the zip file to read from
       
    66 @param aHeader the target header object
       
    67 @leave KEZlibErrBadGZipHeader invalid zip header
       
    68 @leave ... Any of the system wide error codes
       
    69 */
       
    70 EXPORT_C void EZGZipFile::ReadHeaderL(RFile &aFile, TEZGZipHeader &aHeader)
       
    71 	{
       
    72 	TInt obligatoryData = sizeof(aHeader.iId1) + sizeof(aHeader.iId2) + sizeof(aHeader.iCompressionMethod) +
       
    73 		sizeof(aHeader.iFlags) + sizeof(aHeader.iTime) + sizeof(aHeader.iExtraFlags) + sizeof(aHeader.iOs);
       
    74 
       
    75 	TPtr8 des(&aHeader.iId1,0,obligatoryData);
       
    76 	TInt err = aFile.Read(des);
       
    77 	if (err != KErrNone || (des.Size() != obligatoryData))
       
    78 		User::Leave(KEZlibErrBadGZipHeader);
       
    79 
       
    80 	if (aHeader.iId1 != ID1 || aHeader.iId2 != ID2)
       
    81 		User::Leave(KEZlibErrBadGZipHeader);
       
    82 	
       
    83 	if (aHeader.iFlags & (1 << EFExtra)) // then the extra bit is set
       
    84 		{
       
    85 		des.Set(REINTERPRET_CAST(TUint8 *,&aHeader.iXlen),0,sizeof(aHeader.iXlen));
       
    86 		err = aFile.Read(des);
       
    87 		if (err != KErrNone || des.Size() != sizeof(aHeader.iXlen) || aHeader.iXlen < 0)
       
    88 			User::Leave(KEZlibErrBadGZipHeader);
       
    89 		
       
    90 		aHeader.iExtra = HBufC8::NewMaxL(aHeader.iXlen);
       
    91 		TPtr8 des = aHeader.iExtra->Des();
       
    92 		err = aFile.Read(des);
       
    93 		if (err != KErrNone || des.Size() != aHeader.iXlen)
       
    94 			User::Leave(KEZlibErrBadGZipHeader);
       
    95 		}
       
    96 	
       
    97 	if (aHeader.iFlags & (1 << EFName)) // then read in filename
       
    98 		ReadStringIntoDescriptorL(aFile,&aHeader.iFname);
       
    99 
       
   100 	if (aHeader.iFlags & (1 << EFComment)) // then read in comment
       
   101 		ReadStringIntoDescriptorL(aFile,&aHeader.iComment);
       
   102 			
       
   103 	if (aHeader.iFlags & (1 << EFHcrc))
       
   104 		{
       
   105 		des.Set(REINTERPRET_CAST(TUint8*,&aHeader.iCrc),0,sizeof(aHeader.iCrc));
       
   106 		err = aFile.Read(des);
       
   107 		if (err != KErrNone || des.Size() != sizeof(aHeader.iCrc))
       
   108 			User::Leave(KEZlibErrBadGZipHeader);
       
   109 		}
       
   110 	}
       
   111 
       
   112 /**
       
   113 Write the zip header to the specified file
       
   114 
       
   115 @param aFile the file to write to
       
   116 @param aHeader the header object to write to the file
       
   117 @leave ... Any of the system wide error codes
       
   118 */
       
   119 EXPORT_C void EZGZipFile::WriteHeaderL(RFile &aFile, TEZGZipHeader &aHeader)
       
   120 	{
       
   121 	TInt obligatoryData = sizeof(aHeader.iId1) + sizeof(aHeader.iId2) + sizeof(aHeader.iCompressionMethod) +
       
   122 		sizeof(aHeader.iFlags) + sizeof(aHeader.iTime) + sizeof(aHeader.iExtraFlags) + sizeof(aHeader.iOs);
       
   123 
       
   124 	TPtrC8 des(&aHeader.iId1,obligatoryData);
       
   125 	User::LeaveIfError(aFile.Write(des));
       
   126 	TBuf8<1> null(1);
       
   127 	null[0] = '\0';
       
   128 
       
   129 	if (aHeader.iFlags & (1 << EFExtra)) // then the extra bit is set
       
   130 		{
       
   131 		des.Set(REINTERPRET_CAST(TUint8 *,&aHeader.iXlen),sizeof(aHeader.iXlen));
       
   132 		User::LeaveIfError(aFile.Write(des));
       
   133 		
       
   134 		User::LeaveIfError(aFile.Write(*aHeader.iExtra));
       
   135 		}
       
   136 						
       
   137 	if (aHeader.iFlags & (1 << EFName)) // then read in filename
       
   138 		{
       
   139 		User::LeaveIfError(aFile.Write(*aHeader.iFname));
       
   140 		User::LeaveIfError(aFile.Write(null));
       
   141 		}
       
   142 
       
   143 	if (aHeader.iFlags & (1 << EFComment)) // then read in comment
       
   144 		{
       
   145 		User::LeaveIfError(aFile.Write(*aHeader.iComment));
       
   146 		User::LeaveIfError(aFile.Write(null));
       
   147 		}
       
   148 	
       
   149 	if (aHeader.iFlags & (1 << EFHcrc))
       
   150 		{
       
   151 		des.Set(REINTERPRET_CAST(TUint8*,&aHeader.iCrc),sizeof(aHeader.iCrc));
       
   152 		User::LeaveIfError(aFile.Write(des));
       
   153 		}
       
   154 	}
       
   155 
       
   156 void EZGZipFile::ReadStringIntoDescriptorL(RFile &aFile, HBufC8 **aDes)
       
   157 	{
       
   158 	TInt i, err;
       
   159 	CArrayFixFlat<TUint8> *bytes = new (ELeave) CArrayFixFlat<TUint8>(16);
       
   160 	CleanupStack::PushL(bytes);
       
   161 	TBuf8<1> ch;
       
   162 	while ((err = aFile.Read(ch)) == KErrNone)
       
   163 		{
       
   164 		if (ch.Length() != 1)
       
   165 			User::Leave(KEZlibErrBadGZipHeader);
       
   166 		else if (ch[0] != '\0')
       
   167 			bytes->AppendL(*ch.Ptr());
       
   168 		else
       
   169 			break;
       
   170 		}
       
   171 	
       
   172 	if (err != KErrNone)
       
   173 		User::Leave(KEZlibErrBadGZipHeader);
       
   174 	
       
   175 	*aDes = HBufC8::NewMaxL(bytes->Count());
       
   176 	
       
   177 	for (i = 0; i < bytes->Count(); i++)
       
   178 		(*aDes)->Des()[i] = (*bytes)[i];
       
   179 	
       
   180 	CleanupStack::PopAndDestroy(); // delete bytes
       
   181 	}
       
   182 
       
   183 /**
       
   184 Read the zip trailer from the specified zip file into the TEZGZipTrailer object
       
   185 
       
   186 @param aFile the zip file to read from
       
   187 @param aTrailer the target trailer object
       
   188 @leave KEZlibErrBadGZipTrailer invalid zip trailer
       
   189 @leave ... Any of the system wide error codes
       
   190 */
       
   191 EXPORT_C void EZGZipFile::ReadTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer)
       
   192 	{
       
   193 	TPtr8 des(REINTERPRET_CAST(TUint8*,&aTrailer.iCrc32),0,sizeof(TEZGZipTrailer));
       
   194 
       
   195 	TInt err = aFile.Read(des);
       
   196 	if (err != KErrNone || des.Size() != sizeof(TEZGZipTrailer))
       
   197 		User::Leave(KEZlibErrBadGZipTrailer);
       
   198 	}
       
   199 
       
   200 /**
       
   201 Write the zip trailer to the specified file
       
   202 
       
   203 @param aFile the file to write to
       
   204 @param aTrailer the trailer object to write to the file
       
   205 @leave ... Any of the system wide error codes
       
   206 */
       
   207 EXPORT_C void EZGZipFile::WriteTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer)
       
   208 	{
       
   209 	TPtrC8 des(REINTERPRET_CAST(TUint8*,&aTrailer.iCrc32),sizeof(TEZGZipTrailer));
       
   210 	User::LeaveIfError(aFile.Write(des));
       
   211 	}
       
   212 
       
   213 /**
       
   214 Find the zip trailer within the specified file, and read it into the TEZGZipTrailer object
       
   215 
       
   216 @param aRfs file server session
       
   217 @param aFname the file to read from
       
   218 @param aTrailer the target trailer object
       
   219 @leave KEZlibErrBadGZipHeader Invalid zip header
       
   220 @leave ... Any of the system wide error codes
       
   221 */
       
   222 EXPORT_C void EZGZipFile::LocateAndReadTrailerL(RFs &aRfs, const TDesC &aFname, TEZGZipTrailer &aTrailer)
       
   223 	{
       
   224 	TInt fileSize;
       
   225 
       
   226 	RFile file;
       
   227 
       
   228 	User::LeaveIfError(file.Open(aRfs,aFname,EFileStream | EFileRead | EFileShareAny));
       
   229 	CleanupClosePushL(file);
       
   230 
       
   231 	TUint8 magic[2];
       
   232 	TPtr8 des(magic,0,sizeof(TUint8) * 2);
       
   233 	User::LeaveIfError(file.Read(des));
       
   234 	if (magic[0] != ID1 || magic[1] != ID2)
       
   235 		User::Leave(KEZlibErrBadGZipHeader);
       
   236 
       
   237 	User::LeaveIfError(file.Size(fileSize));
       
   238 	TInt sizePos = fileSize - (sizeof (TInt32) * 2);
       
   239 	User::LeaveIfError(file.Seek(ESeekStart,sizePos));
       
   240 
       
   241 	des.Set(REINTERPRET_CAST(TUint8 *,&aTrailer),0,sizeof(TInt32)*2);
       
   242 	
       
   243 	User::LeaveIfError(file.Read(des));
       
   244 
       
   245 	CleanupStack::PopAndDestroy();
       
   246 	}
       
   247 
       
   248 /**
       
   249 @deprecated Interface is deprecated because it is unsafe as it may leave. It is available for backward compatibility reasons only.
       
   250 @see EZGZipFile::IsGzipFileL
       
   251 */
       
   252 EXPORT_C TBool EZGZipFile::IsGzipFile(RFs &aRfs, const TDesC &aFname)
       
   253 	{
       
   254 	TBool retBool = true;
       
   255 	TRAPD(errCode, retBool = IsGzipFileL(aRfs, aFname));
       
   256 	if(errCode != KErrNone)
       
   257 		{
       
   258 		retBool = false;
       
   259 		}
       
   260 	return retBool;
       
   261 	}
       
   262 
       
   263 /**
       
   264 Determine if the given file is a valid zip file
       
   265 
       
   266 @param aRfs file server session
       
   267 @param aFname name of the file to check
       
   268 @leave ... Any of the system wide error codes
       
   269 @return ETrue if the file is valid zip file, EFalse otherwise
       
   270 */
       
   271 EXPORT_C TBool EZGZipFile::IsGzipFileL(RFs &aRfs, const TDesC &aFname)
       
   272 	{
       
   273 	TUint8 ids[2];
       
   274 	RFile file;
       
   275 
       
   276 	User::LeaveIfError(file.Open(aRfs,aFname,EFileStream | EFileRead | EFileShareAny));
       
   277 	CleanupClosePushL(file);
       
   278 
       
   279 	TPtr8 des(ids,0,sizeof(TUint8) * 2);
       
   280 	
       
   281 	User::LeaveIfError(file.Read(des));
       
   282 	CleanupStack::PopAndDestroy();
       
   283 	return (ids[0] == ID1 && ids[1] == ID2);
       
   284 	}
       
   285 
       
   286 //--------------------------------------------------------------------------------------------------------
       
   287 
       
   288 
       
   289 CEZFileToGzipBM::CEZFileToGzipBM(RFile &aInput, RFile &aOutput) : CEZFileBufferManager(aInput,aOutput)
       
   290 	{
       
   291 	iCrc = crc32_r(iCrc,NULL,0);
       
   292 	}
       
   293 
       
   294 
       
   295 CEZFileToGzipBM* CEZFileToGzipBM::NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize)
       
   296 	{
       
   297 	CEZFileToGzipBM *bm = new (ELeave) CEZFileToGzipBM(aInput,aOutput);
       
   298 	CleanupStack::PushL(bm);
       
   299 	bm->ConstructL(aBufferSize);
       
   300 	return bm;
       
   301 	}
       
   302 
       
   303 CEZFileToGzipBM* CEZFileToGzipBM::NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize)
       
   304 	{
       
   305 	CEZFileToGzipBM *bm = new (ELeave) CEZFileToGzipBM(aInput,aOutput);
       
   306 	CleanupStack::PushL(bm);
       
   307 	bm->ConstructL(aBufferSize);
       
   308 	CleanupStack::Pop();
       
   309 	return bm;
       
   310 	}
       
   311 
       
   312 void CEZFileToGzipBM::NeedInputL(CEZZStream &aZStream)
       
   313 	{
       
   314 	CEZFileBufferManager::NeedInputL(aZStream);
       
   315 	iCrc = crc32_r(iCrc,iInputDescriptor.Ptr(),iInputDescriptor.Size());
       
   316 	}
       
   317 
       
   318 void CEZFileToGzipBM::InitializeL(CEZZStream &aZStream)
       
   319 	{
       
   320 	CEZFileBufferManager::InitializeL(aZStream);
       
   321 	iCrc = crc32_r(iCrc,iInputDescriptor.Ptr(),iInputDescriptor.Size());
       
   322 	}
       
   323 
       
   324 //--------------------------------------------------------------------------------------------------------
       
   325 
       
   326 
       
   327 CEZGzipToFileBM::CEZGzipToFileBM(RFile &aInput, RFile &aOutput) : CEZFileBufferManager(aInput,aOutput)
       
   328 	{
       
   329 	iCrc = crc32_r(iCrc,NULL,0);
       
   330 	}
       
   331 
       
   332 CEZGzipToFileBM* CEZGzipToFileBM::NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize)
       
   333 	{
       
   334 	CEZGzipToFileBM *bm = new (ELeave) CEZGzipToFileBM(aInput,aOutput);
       
   335 	CleanupStack::PushL(bm);
       
   336 	bm->ConstructL(aBufferSize);
       
   337 	return bm;
       
   338 	}
       
   339 
       
   340 CEZGzipToFileBM* CEZGzipToFileBM::NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize)
       
   341 	{
       
   342 	CEZGzipToFileBM *bm = new (ELeave) CEZGzipToFileBM(aInput,aOutput);
       
   343 	CleanupStack::PushL(bm);
       
   344 	bm->ConstructL(aBufferSize);
       
   345 	CleanupStack::Pop();
       
   346 	return bm;
       
   347 	}
       
   348 
       
   349 void CEZGzipToFileBM::NeedOutputL(CEZZStream &aZStream)
       
   350 	{
       
   351 	TPtrC8 od = aZStream.OutputDescriptor();
       
   352 	iCrc = crc32_r(iCrc,od.Ptr(),od.Size());
       
   353 	CEZFileBufferManager::NeedOutputL(aZStream);
       
   354 	}
       
   355 
       
   356 void CEZGzipToFileBM::FinalizeL(CEZZStream &aZStream)
       
   357 	{
       
   358 	TPtrC8 od = aZStream.OutputDescriptor();
       
   359 	iCrc = crc32_r(iCrc,od.Ptr(),od.Size());
       
   360 	CEZFileBufferManager::FinalizeL(aZStream);
       
   361 	}
       
   362 
       
   363 
       
   364 //--------------------------------------------------------------------------------------------------------
       
   365 CEZGZipToFile::CEZGZipToFile() : iDecompressor(NULL), iBufferManager(NULL)
       
   366 	{
       
   367 
       
   368 	}
       
   369 
       
   370 CEZGZipToFile::~CEZGZipToFile()
       
   371 	{
       
   372 	delete iDecompressor;
       
   373 	delete iBufferManager;
       
   374 	iGZipFile.Close();
       
   375 	}
       
   376 
       
   377 /**
       
   378 Creates a new CEZGZipToFile object and leaves it on the CleanupStack
       
   379 
       
   380 @param aRfs open file server session
       
   381 @param aGzFileName name of the file to be de-compressed
       
   382 @param aOutput the target file to hold the un-compressed data
       
   383 @param aBufferSize required size of buffers
       
   384 @return a pointer to the new CEZGZipToFile object, left on the CleanupStack
       
   385 */
       
   386 EXPORT_C CEZGZipToFile* CEZGZipToFile::NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize)
       
   387 	{
       
   388 	CEZGZipToFile* dec = new (ELeave) CEZGZipToFile;
       
   389 	CleanupStack::PushL(dec);
       
   390 	dec->ConstructL(aRfs,aGzFileName,aOutput,aBufferSize);
       
   391 	return dec;
       
   392 	}
       
   393 
       
   394 /**
       
   395 Creates a new CEZGZipToFile object
       
   396 
       
   397 @param aRfs open file server session
       
   398 @param aGzFileName name of the file to be de-compressed
       
   399 @param aOutput the target file to hold the un-compressed data
       
   400 @param aBufferSize required size of buffers
       
   401 @return a pointer to the new CEZGZipToFile object
       
   402 */
       
   403 EXPORT_C CEZGZipToFile* CEZGZipToFile::NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize)
       
   404 	{
       
   405 	CEZGZipToFile* dec = new (ELeave) CEZGZipToFile;
       
   406 	CleanupStack::PushL(dec);
       
   407 	dec->ConstructL(aRfs,aGzFileName,aOutput,aBufferSize);
       
   408 	CleanupStack::Pop();
       
   409 	return dec;
       
   410 	}
       
   411 
       
   412 /**
       
   413 Quits the current de-compression operation and restarts with the specified arguments
       
   414 
       
   415 @param aRfs open file server session
       
   416 @param aGzFileName name of the file to be de-compressed
       
   417 @param aOutput the target file to hold the un-compressed data
       
   418 @param aBufferSize required size of buffers
       
   419 @leave ... Any of the system wide error codes
       
   420 */
       
   421 EXPORT_C void CEZGZipToFile::ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize)
       
   422 	{
       
   423 	delete iBufferManager;
       
   424 	iBufferManager = NULL;
       
   425 	InitialiseBufManL(aRfs,aGzFileName,aOutput,aBufferSize);
       
   426 	iDecompressor->ResetL(*iBufferManager);
       
   427 	}
       
   428 
       
   429 /**
       
   430 De-compresses the current zip file in stages.  The function needs to called again until the de-compression 
       
   431 is finalised, in which case it will return EFalse - for example...
       
   432 
       
   433 @code
       
   434 while ( decompressor->InflateL() )
       
   435 	{
       
   436 	// No action required
       
   437 	}
       
   438 @endcode
       
   439 
       
   440 @leave KEZlibErrBadGZipCrc Invalid CRC check
       
   441 @leave ... Any of the system wide error codes
       
   442 @return ETrue if the de-compression is not complete, and function must be called again
       
   443 @return EFalse if the de-compression is finalised
       
   444 */
       
   445 EXPORT_C TBool CEZGZipToFile::InflateL()
       
   446 	{
       
   447 	TBool keepGoing = iDecompressor->InflateL();
       
   448 
       
   449 	if (!keepGoing)
       
   450 		{
       
   451 		if (iBufferManager->Crc() != iTrailer.iCrc32)
       
   452 			{
       
   453 			User::Leave(KEZlibErrBadGZipCrc);
       
   454 				
       
   455 			}
       
   456 		iGZipFile.Close();
       
   457 		}
       
   458 
       
   459 	return keepGoing;
       
   460 	}
       
   461 
       
   462 void CEZGZipToFile::InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize)
       
   463 	{
       
   464 	EZGZipFile::LocateAndReadTrailerL(aRfs,aGzFileName,iTrailer);
       
   465 	User::LeaveIfError(iGZipFile.Open(aRfs,aGzFileName,EFileStream | EFileRead | EFileShareAny));
       
   466 	EZGZipFile::ReadHeaderL(iGZipFile,iHeader);
       
   467 	iBufferManager = CEZGzipToFileBM::NewL(iGZipFile,aOutput,aBufferSize);
       
   468 	}
       
   469 
       
   470 void CEZGZipToFile::ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize)
       
   471 	{
       
   472 	InitialiseBufManL(aRfs,aGzFileName,aOutput,aBufferSize);
       
   473 
       
   474 	// this is a special zlib modification to stop it choking when it can't find the normal zlib stream header.
       
   475 
       
   476 	iDecompressor = CEZDecompressor::NewL(*iBufferManager,-CEZDecompressor::EMaxWBits);
       
   477 	}
       
   478 
       
   479 
       
   480 //--------------------------------------------------------------------------------------------------------
       
   481 
       
   482 
       
   483 CEZFileToGZip::CEZFileToGZip() : iCompressor(NULL), iBufferManager(NULL)
       
   484 	{
       
   485 
       
   486 	}
       
   487 
       
   488 CEZFileToGZip::~CEZFileToGZip()
       
   489 	{
       
   490 	delete iCompressor;
       
   491 	delete iBufferManager;
       
   492 	iGZipFile.Close();
       
   493 	}
       
   494 
       
   495 /**
       
   496 Creates a new CEZFileToGZip object and leaves it on the CleanupStack
       
   497 
       
   498 @param aRfs open file server session
       
   499 @param aGzFileName the name of the target zip file
       
   500 @param aInput the file to compress
       
   501 @param aBufferSize required size of buffers
       
   502 @return a pointer to the new CEZFileToGZip object, left on the CleanupStack
       
   503 */
       
   504 EXPORT_C CEZFileToGZip* CEZFileToGZip::NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize)
       
   505 	{
       
   506 	CEZFileToGZip* com = new (ELeave) CEZFileToGZip;
       
   507 	CleanupStack::PushL(com);
       
   508 	com->ConstructL(aRfs,aGzFileName,aInput,aBufferSize);
       
   509 	return com;
       
   510 	}
       
   511 
       
   512 /**
       
   513 Creates a new CEZFileToGZip object and leaves it on the CleanupStack
       
   514 
       
   515 @param aRfs open file server session
       
   516 @param aGzFileName the name of the target zip file
       
   517 @param aInput the file to compress
       
   518 @param aBufferSize required size of buffers
       
   519 @return a pointer to the new CEZFileToGZip object, left on the CleanupStack
       
   520 */
       
   521 EXPORT_C CEZFileToGZip* CEZFileToGZip::NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize)
       
   522 	{
       
   523 	CEZFileToGZip* com = new (ELeave) CEZFileToGZip;
       
   524 	CleanupStack::PushL(com);
       
   525 	com->ConstructL(aRfs,aGzFileName,aInput,aBufferSize);
       
   526 	CleanupStack::Pop();
       
   527 	return com;
       
   528 	}
       
   529 
       
   530 /**
       
   531 Quits the current compression operation and restarts with the specified arguments
       
   532 
       
   533 @param aRfs open file server session
       
   534 @param aGzFileName the name of the target zip file
       
   535 @param aInput the file to compress
       
   536 @param aBufferSize required size of buffers
       
   537 @leave ... Any of the system wide error codes
       
   538 */
       
   539 EXPORT_C void CEZFileToGZip::ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize)
       
   540 	{
       
   541 	delete iBufferManager;
       
   542 	iBufferManager = NULL;
       
   543 	InitialiseBufManL(aRfs,aGzFileName,aInput,aBufferSize);
       
   544 	iCompressor->ResetL(*iBufferManager);
       
   545 	}
       
   546 
       
   547 /**
       
   548 Compresses the current file in stages.  The function needs to called again until the compression 
       
   549 is finalised, in which case it will return EFalse - for example...
       
   550 
       
   551 @code
       
   552 while ( compressor->DeflateL() )
       
   553 	{
       
   554 	// No action required
       
   555 	}
       
   556 @endcode
       
   557 
       
   558 @leave ... Any of the system wide error codes
       
   559 @return ETrue if the compression is not complete, and function must be called again
       
   560 @return EFalse if the compression is finalised
       
   561 */
       
   562 EXPORT_C TBool CEZFileToGZip::DeflateL()
       
   563 	{
       
   564 	TBool keepgoing = iCompressor->DeflateL();
       
   565 	if (!keepgoing)
       
   566 		{
       
   567 		TEZGZipTrailer trailer(iBufferManager->Crc(), iUncompressedDataSize);
       
   568 		EZGZipFile::WriteTrailerL(iGZipFile, trailer);
       
   569 		iGZipFile.Close();
       
   570 		}
       
   571 
       
   572 	return keepgoing;
       
   573 	}
       
   574 
       
   575 void CEZFileToGZip::InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize)
       
   576 	{
       
   577 	User::LeaveIfError(aInput.Size(iUncompressedDataSize));
       
   578 	TInt err;
       
   579 
       
   580 	err = iGZipFile.Create(aRfs, aGzFileName,EFileStream | EFileWrite | EFileShareExclusive);
       
   581 	if (err == KErrAlreadyExists)
       
   582 		User::LeaveIfError(iGZipFile.Open(aRfs,aGzFileName,EFileStream | EFileWrite | EFileShareExclusive));
       
   583 	else 
       
   584 		User::LeaveIfError(err);
       
   585 	
       
   586 	EZGZipFile::WriteHeaderL(iGZipFile,iHeader);
       
   587 	iBufferManager = CEZFileToGzipBM::NewL(aInput,iGZipFile,aBufferSize);
       
   588 	}
       
   589 
       
   590 void CEZFileToGZip::ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize)
       
   591 	{
       
   592 	InitialiseBufManL(aRfs,aGzFileName,aInput,aBufferSize);
       
   593 
       
   594 	// this is a special zlib modification to stop zlib writing out its normal zlib stream header.
       
   595 
       
   596 	iCompressor = CEZCompressor::NewL(*iBufferManager,CEZCompressor::EDefaultCompression,-CEZCompressor::EMaxWBits);
       
   597 	}