sl@0: // Copyright (c) 2003-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: //
sl@0: 
sl@0: #ifndef __GZIP_H__
sl@0: #define __GZIP_H__
sl@0: 
sl@0: #include <ezdecompressor.h>
sl@0: #include <ezcompressor.h>
sl@0: #include <ezfilebuffer.h>
sl@0: 
sl@0: /**
sl@0: The TEZGZipHeader class encapsulates a zip file header, which is written to the start of the zip
sl@0: file to store various settings of the archive
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: class TEZGZipHeader
sl@0: 	{
sl@0: 
sl@0: public:
sl@0: 	IMPORT_C TEZGZipHeader();
sl@0: 	IMPORT_C ~TEZGZipHeader();
sl@0: 	
sl@0: public:
sl@0: 	TUint8 iId1;
sl@0: 	TUint8 iId2;
sl@0: 	TUint8 iCompressionMethod;
sl@0: 	TUint8 iFlags;
sl@0: 	TInt32 iTime;
sl@0: 	TUint8 iExtraFlags;
sl@0: 	TUint8 iOs;
sl@0: 	TInt16 iXlen;
sl@0: 	HBufC8* iExtra;
sl@0: 	HBufC8* iFname;
sl@0: 	HBufC8* iComment;
sl@0: 	TInt16 iCrc;
sl@0: 	};
sl@0: 
sl@0: /**
sl@0: The TEZGZipTrailer class encapsulates a zip file trailer, which uses a CRC (cyclic redundancy check) to 
sl@0: confirm the validity of the unpacked archive
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: class TEZGZipTrailer
sl@0: 	{
sl@0: public:
sl@0: 	IMPORT_C TEZGZipTrailer();
sl@0: 	IMPORT_C TEZGZipTrailer(TInt32 aCrc, TInt32 aSize);
sl@0: public:
sl@0: 	TInt32 iCrc32;
sl@0: 	TInt32 iSize;
sl@0: 	};
sl@0: 
sl@0: /**
sl@0: The EZGZipFile class handles writing / reading of headers and trailers to / from zip files
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: class EZGZipFile
sl@0: 	{
sl@0: public:
sl@0: 	
sl@0: 	/** Zip file error codes */	
sl@0: 	enum { 	ENotGZipFile = KEZlibErrNotGZipFile, 					
sl@0: 			EInvalidCompressionMethod = KEZlibErrInvalidCompression,
sl@0: 			EBadGZipHeader= KEZlibErrBadGZipHeader, 				
sl@0: 			EBadGZipTrailer = KEZlibErrBadGZipTrailer, 				
sl@0: 			EBadGZipCrc = KEZlibErrBadGZipCrc };					
sl@0: 	/** Flags to determine which part of the header / trailer is being written */			
sl@0: 	enum { EFText = 0, EFHcrc = 1, EFExtra = 2, EFName = 3, EFComment = 4};
sl@0: 
sl@0: public:
sl@0: 	IMPORT_C static void ReadHeaderL(RFile &aFile, TEZGZipHeader &aHeader);
sl@0: 	IMPORT_C static void WriteHeaderL(RFile &aFile, TEZGZipHeader &aHeader);
sl@0: 	IMPORT_C static void ReadTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer);
sl@0: 	IMPORT_C static void WriteTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer);
sl@0: 	IMPORT_C static void LocateAndReadTrailerL(RFs &aRfs, const TDesC &aFname, TEZGZipTrailer &aTrailer);
sl@0: 	IMPORT_C static TBool IsGzipFile(RFs &aRfs, const TDesC &aFname);
sl@0: 	IMPORT_C static TBool IsGzipFileL(RFs &aRfs, const TDesC &aFname);
sl@0: 
sl@0: private:
sl@0: 	static void ReadStringIntoDescriptorL(RFile &aFile, HBufC8 **aDes);
sl@0: 
sl@0: public:
sl@0: 	/** First of the header ID pair */
sl@0: 	static const TUint8 ID1;
sl@0: 	/** Second of the header ID pair */	
sl@0: 	static const TUint8 ID2;
sl@0: 	};
sl@0: 
sl@0: /**
sl@0: The CEZFileToGzipBM manages the input and output buffers for compression
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: NONSHARABLE_CLASS(CEZFileToGzipBM) : public CEZFileBufferManager
sl@0: 	{
sl@0: public:
sl@0: 
sl@0: 	static CEZFileToGzipBM* NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize);
sl@0: 	static CEZFileToGzipBM* NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize);
sl@0: 
sl@0: 	/**
sl@0: 	Initialise the stream with input and output buffers and starts reading
sl@0: 
sl@0: 	@param aZStream the stream to initialise
sl@0: 	*/
sl@0: 	virtual void InitializeL(CEZZStream &aZStream);
sl@0: 	
sl@0: 	/**
sl@0: 	Set the stream's input buffer and starts reading
sl@0: 	
sl@0: 	@param aZStream the steam whose input buffer to set
sl@0: 	*/	
sl@0: 	virtual void NeedInputL(CEZZStream &aZStream);
sl@0: 	
sl@0: 	/**
sl@0: 	Return the CRC - used for checking validity of the archive
sl@0: 
sl@0: 	@return the CRC value
sl@0: 	*/	
sl@0: 	TInt32 Crc() const { return iCrc; } ;
sl@0: 
sl@0: private:
sl@0: 	CEZFileToGzipBM(RFile &aInput, RFile &aOutput);
sl@0: private:
sl@0: 	TInt32 iCrc;
sl@0: 	};
sl@0: 
sl@0: /**
sl@0: The CEZGzipToFileBM class manages the input and output buffers for de-compression
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: NONSHARABLE_CLASS(CEZGzipToFileBM) : public CEZFileBufferManager
sl@0: 	{
sl@0: public:
sl@0: 
sl@0: 	static CEZGzipToFileBM* NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize);
sl@0: 	static CEZGzipToFileBM* NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize);
sl@0: 
sl@0: 	/**
sl@0: 	Finish writing to the stream
sl@0: 
sl@0: 	@param aZStream the stream to complete writing to
sl@0: 	*/	
sl@0: 	virtual void FinalizeL(CEZZStream &aZStream);
sl@0: 	
sl@0: 	/**
sl@0: 	Set the stream's output buffer and start writing
sl@0: 
sl@0: 	@param aZStream the steam whose output buffer to set
sl@0: 	*/	
sl@0: 	virtual void NeedOutputL(CEZZStream &aZStream);
sl@0: 	
sl@0: 	/**
sl@0: 	Return the CRC - used for checking validity of the archive
sl@0: 
sl@0: 	@return the CRC value
sl@0: 	*/	
sl@0: 	TInt32 Crc() const { return iCrc; } ;
sl@0: 
sl@0: private:
sl@0: 	CEZGzipToFileBM(RFile &aInput, RFile &aOutput);
sl@0: private:
sl@0: 	TInt32 iCrc;
sl@0: 	};
sl@0: 
sl@0: 
sl@0: /**
sl@0: A CEZGZipToFile object allows de-compression of a compressed file.
sl@0: The name of the compressed file is passed into the constructor along with a target file 
sl@0: to contain the uncompressed data.  The file is uncompressed by calling the InflateL() function.
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: class CEZGZipToFile : public CBase
sl@0: 	{
sl@0: public:
sl@0: 	~CEZGZipToFile();
sl@0: 
sl@0: 	IMPORT_C static CEZGZipToFile* NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize = 0x8000);
sl@0: 	IMPORT_C static CEZGZipToFile* NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize = 0x8000);
sl@0: 
sl@0: 	IMPORT_C void ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize = 0x8000);
sl@0: 
sl@0: 	IMPORT_C TBool InflateL();
sl@0: 
sl@0: private:
sl@0: 	CEZGZipToFile();
sl@0: 	void ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize);
sl@0: 	void InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize);
sl@0: private:
sl@0: 	CEZDecompressor *iDecompressor;
sl@0: 	CEZGzipToFileBM *iBufferManager;
sl@0: 	TEZGZipTrailer iTrailer;
sl@0: 	RFile iGZipFile;
sl@0: 	TEZGZipHeader iHeader;
sl@0: 	};
sl@0: 
sl@0: /**
sl@0: A CEZFileToGZip object allows compression of an uncompressed file to a zip file.
sl@0: The uncompressed source file is passed into the constructor along with the name of the target
sl@0: zip file.  The file is compressed by calling the DeflateL() function.
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: class CEZFileToGZip : public CBase
sl@0: 	{
sl@0: public:
sl@0: 	~CEZFileToGZip();
sl@0: 
sl@0: 	IMPORT_C static CEZFileToGZip* NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize = 0x8000);
sl@0: 	IMPORT_C static CEZFileToGZip* NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize = 0x8000);
sl@0: 
sl@0: 	IMPORT_C void ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize = 0x8000);
sl@0: 
sl@0: 	IMPORT_C TBool DeflateL();
sl@0: 
sl@0: private:
sl@0: 	CEZFileToGZip();
sl@0: 	void ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize);
sl@0: 	void InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize);
sl@0: private:
sl@0: 	CEZCompressor *iCompressor;
sl@0: 	CEZFileToGzipBM *iBufferManager;
sl@0: 	TInt iUncompressedDataSize;
sl@0: 	RFile iGZipFile;
sl@0: 	TEZGZipHeader iHeader;
sl@0: 	};
sl@0: 
sl@0: 
sl@0: #endif