secureswitools/swisistools/source/rscparser/barscimpl.h
branchRCL_3
changeset 66 8b7f4e561641
parent 65 7333d7932ef7
child 70 e8965914fac7
equal deleted inserted replaced
65:7333d7932ef7 66:8b7f4e561641
     1 // Copyright (c) 2009 - 2010 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 /** 
       
    17 * @file barscimpl.h
       
    18 *
       
    19 * @internalComponent
       
    20 * @released
       
    21 */
       
    22 
       
    23 #ifndef __BARSCIMPL_H__
       
    24 #define __BARSCIMPL_H__
       
    25 
       
    26 #include <string>
       
    27 #include "commontypes.h"
       
    28 
       
    29 //Forward declarations
       
    30 struct SDictionaryCompressionData;
       
    31 class RDictionaryCompressionBitStream;
       
    32 /** 
       
    33 Accesses a resource file and reads the resource data into a buffer.
       
    34 It is the implementation class for CResourceFile class.
       
    35 */
       
    36 class RResourceFileImpl
       
    37 	{
       
    38 
       
    39 public:
       
    40 	RResourceFileImpl();
       
    41 	~RResourceFileImpl();
       
    42 	/** 
       
    43 	Opens the resource file reader.
       
    44 	The resource file reader must be opened before reading resources.
       
    45 	@param aName name of the RSC file.
       
    46 	@param aFileOffset RSC file offset 
       
    47 	@param aFileSize RSC file size
       
    48 	*/
       
    49 	void OpenL(const std::string& aName, TUint32 aFileOffset=0, TInt aFileSize=0);
       
    50 	/** 
       
    51 	Reads a resource after allocating heap memory for it.
       
    52 	Ownership of the allocated heap is passes to the caller who must 
       
    53 	free it.	
       
    54 	@param aResourceId The numeric id of the resource to be read.
       
    55 	@return Pointer to the heap containing the resource.
       
    56 	*/
       
    57 	Ptr8* AllocReadL(const TInt& aResourceId);
       
    58 
       
    59 	/** Initialises the offset value from the first resource.
       
    60 
       
    61 	The function tests to catch cases where the first resource is not an RSS_SIGNATURE.
       
    62 	It assumes that the first resource in the file consists of
       
    63 	two 32-bit integers. The first integer contains the version number and
       
    64 	the second is a self-referencing link whose value is the offset for
       
    65 	the resources in the file, plus 1.This function must be called before
       
    66 	calling Offset(), AllocReadL(), AllocReadLC() or ReadL().
       
    67 	*/
       
    68 	void ConfirmSignatureL();
       
    69 	
       
    70 	/** 
       
    71 	Tests whether the resource file owns the specified resource id.
       
    72 	@param aResourceId The resource id to test.
       
    73 	@return True, if the resource file owns the id, false otherwise.
       
    74 	*/
       
    75 	TBool OwnsResourceId(const TInt& aResourceId) const;
       
    76 
       
    77 	/** 
       
    78 	Function to read specified bytes from the RSC file
       
    79 	@param aPos Offset from where to begin reading
       
    80 	@param aData Buffer to store read values
       
    81 	@param aLength Length of the bytes to be read.
       
    82 	@return Length of the bytes to be read.
       
    83 	*/
       
    84 	TInt ReadL(TInt aPos,TUint8* aData,const TInt& aLength);
       
    85 
       
    86 	TUidType UidType() const;
       
    87 
       
    88 
       
    89 private:
       
    90 	struct SSigRecord
       
    91 		{
       
    92 		TInt signature;
       
    93 		TInt offset;
       
    94 		};
       
    95   enum
       
    96 		{
       
    97 		EFlagIsRomFile																=0x80000000,
       
    98 		EFlagPotentiallyContainsCompressedUnicode									=0x40000000,
       
    99 		EFlagDictionaryCompressed													=0x20000000,
       
   100 		EFlagThirdUidIsOffset														=0x10000000,
       
   101 		EFlagGenerate_RSS_SIGNATURE_ForFirstUserResource							=0x08000000,
       
   102 		EFlagFirstResourceIsGeneratedBitArrayOfResourcesContainingCompressedUnicode	=0x04000000,
       
   103 		EFlagCalypsoFileFormat														=0x02000000,
       
   104 		EFlagIsBufferRscFile														=0x01000000,
       
   105 		EAllFlags																	=0xff000000
       
   106 		};
       
   107 	enum
       
   108 		{
       
   109 		EOffsetBits	=0xfffff000,
       
   110 		EIdBits		=0x00000fff
       
   111 		};
       
   112 
       
   113 	class TExtra;
       
   114 
       
   115 
       
   116 private:	
       
   117 	/** 
       
   118 	Function to read specified bytes from the RSC file
       
   119 	@param aFlags Integer conatainig no of resource and status flags.
       
   120 	@param aPos Offset from where to begin reading
       
   121 	@param aData Buffer to store read values
       
   122 	@param aLength Length of the bytes to be read.
       
   123 	@return Length of the bytes to be read.
       
   124 	*/
       
   125 	TInt ReadL(const TUint32& aFlags, TInt aPos, TUint8* aData, const TInt& aLength);
       
   126 
       
   127 	/** @internalComponent
       
   128 	@return The first resource record.
       
   129 	@panic Some BAFL panic codes, if the file is corrupted.
       
   130 	@leave KErrCorrupt The file is corrupted.
       
   131 	Some other error codes are possible too.
       
   132 	The method could panic or leave depending on the state of
       
   133 	iAssertObj member of RResourceFileImpl::TExtra class. */
       
   134 	SSigRecord FirstRecordL() const;
       
   135 
       
   136 	
       
   137 	/** 
       
   138 	Function to retrieve the header and 
       
   139 	resource index information of the RSC file.
       
   140 	*/
       
   141 	void ReadHeaderAndResourceIndexL();
       
   142 	
       
   143 	/** 
       
   144 	The method will decomress the unicode data (aCompressedUnicode argument) and append
       
   145 	the decompressed data to the end of aBuffer (aBuffer argument).
       
   146 
       
   147 	@param aBuffer Destination buffer.
       
   148 	@param aCompressedUnicode Pointer to compressed unicode data.
       
   149 	@param aLengthOfCompressedUnicode Length of compressed unicode data.
       
   150 	*/
       
   151 	void AppendDecompressedUnicodeL(
       
   152 								Ptr8* aBuffer,
       
   153 								const TUint8*  aCompressedUnicode,
       
   154 								const TInt& aLengthOfCompressedUnicode) const;
       
   155 	/** 
       
   156 	The method will decompress the unicode data (aInputResourceData argument), allocate enough
       
   157 	memory from the heap for the decompressed data, copy the data there and return a pointer
       
   158 	to the decompressed data.
       
   159 	The method doesn't own the allocated heap memory for the decompressed data. It's a caller
       
   160 	responsibility to deallocate the allocated memory.
       
   161 	@param aInputResourceData Compressed data.
       
   162 	@return Pointer to decompressed data 
       
   163 	*/
       
   164 	Ptr8* DecompressUnicodeL(const Ptr8* aInputResourceData) const;
       
   165 
       
   166 
       
   167 	/**
       
   168 	The method will decompress the dictionary compressed data, allocate enough
       
   169 	memory from the heap for the decompressed data, copy the data there and return a pointer
       
   170 	to the decompressed data.
       
   171 	The method doesn't own the allocated heap memory for the decompressed data. It's a caller
       
   172 	responsibility to deallocate the allocated memory.
       
   173 	@pre OpenL() is called.
       
   174 	@leave KErrCorrupt The file is corrupted.
       
   175 	@leave KErrNoMemory There is not enough memory for the decompressed data.
       
   176 	Some other error codes are possible too.
       
   177 	*/
       
   178 	Ptr8* DictionaryDecompressedResourceDataL(
       
   179 								TInt aResourceIndex,
       
   180 								TUint aFlags,
       
   181 								const SDictionaryCompressionData& aDictionaryCompressionData,
       
   182 								const Ptr16* aIndex) const;
       
   183 
       
   184 
       
   185 	/** 
       
   186 	The method will decomress the dictionary compressed data (aDictionaryCompressionData argument) and append
       
   187 	the decompressed data to the end of std::vector<RDictionaryCompressionBitStream> 
       
   188 	(aStackOfDictionaryCompressionBitStreams argument).
       
   189 	*/
       
   190 	void AppendDictionaryCompressionBitStreamL(
       
   191 								std::vector<RDictionaryCompressionBitStream>& aStackOfDictionaryCompressionBitStreams,
       
   192 								TUint aFlags,
       
   193 								const SDictionaryCompressionData& aDictionaryCompressionData,
       
   194 								TInt aStartOfBitData,
       
   195 								TInt aStartOfIndex,
       
   196 								TInt aIndexEntry) const;
       
   197 	
       
   198 	/**
       
   199 	Get the two bytes(in Little Endian format) from the specified buffer
       
   200 	@param aBuffer Buffer address from which 2-bytes are to be achieved.
       
   201 	@param aIndexOfFirstByte Offset of the first byte to be retrieved from the
       
   202            buffer.
       
   203 	@return 2-bytes read from the buffer.
       
   204 	*/
       
   205 	
       
   206 	TInt LittleEndianTwoByteInteger(const TUint8* aBuffer,const TInt& aIndexOfFirstByte,TInt aLength) const;
       
   207 
       
   208 
       
   209 private:
       
   210 	std::ifstream* iResourceContents;	
       
   211 	TInt iSizeOfLargestResourceWhenCompletelyUncompressed;
       
   212 	//basically an array of (unsigned) 16-bit file-positions - 
       
   213 	//this is only used for non-dictionary-compressed resource files
       
   214 	Ptr16* iIndex; 
       
   215 
       
   216 	//the position of this member in the class is exposed because RResourceFile::Offset() is 
       
   217 	//an inline function accessing "iOffset". RResourceFileImpl is an implementation class for
       
   218 	//RResourceFile class. The "iOffset" offset from the beginning of the class must be exactly
       
   219 	//12 bytes.
       
   220 	TInt iOffset; 
       
   221 	TExtra* iExtra;
       
   222 	TUint32 iFlagsAndNumberOfResources;
       
   223 	};
       
   224 
       
   225 
       
   226 struct SDictionaryCompressionData
       
   227 	{
       
   228  	inline SDictionaryCompressionData() :
       
   229 		iStartOfDictionaryData(0),
       
   230 		iStartOfDictionaryIndex(0),
       
   231 		iNumberOfDictionaryEntries(0),
       
   232 		iStartOfResourceData(0),
       
   233 		iStartOfResourceIndex(0),
       
   234 		iNumberOfBitsUsedForDictionaryTokens(0), 
       
   235 		iCachedDictionaryIndex(0),
       
   236 		iCachedResourceBuffer(0)// = NULL;
       
   237 		{
       
   238 		}
       
   239 	TInt iStartOfDictionaryData;
       
   240 	TInt iStartOfDictionaryIndex;
       
   241 	TInt iNumberOfDictionaryEntries;
       
   242 	TInt iStartOfResourceData;
       
   243 	TInt iStartOfResourceIndex;
       
   244 	TInt iNumberOfBitsUsedForDictionaryTokens;
       
   245 	TUint16* iCachedDictionaryIndex;
       
   246 	TUint8* iCachedResourceBuffer;
       
   247 	
       
   248 	};
       
   249 
       
   250 
       
   251 /** 
       
   252 Stores the RSC filesize, offset and the bit-array 
       
   253 representing resources containing compressed unicode.
       
   254 */
       
   255 class RResourceFileImpl::TExtra
       
   256 	{
       
   257 
       
   258 public:
       
   259 	TExtra();
       
   260 	~TExtra();
       
   261 	/**
       
   262 	Finds whether the specified resource contain compressed unicode
       
   263 	@param aRscIdx Resource Index
       
   264 	@return Status of whether the resource is actually present or not.
       
   265 	*/
       
   266 	TInt32 ContainsCompressedUnicode(TInt& aRscIdx, TBool aFirstRscIsGen) const;
       
   267 
       
   268 private:
       
   269 	// Prevent default copy constructor
       
   270 	TExtra(const TExtra&);
       
   271 	// Prevent default "=" operator
       
   272 	TExtra& operator=(const TExtra&);
       
   273 
       
   274 public:
       
   275 	// RSC file UID
       
   276 	TUidType iUidType;
       
   277 	// An array of bits, one for each resource in the resource file
       
   278 	Ptr8* iBitArrayOfResourcesContainingCompressedUnicode;
       
   279 	SDictionaryCompressionData iDictionaryCompressionData;
       
   280 	// Offset of RSC chunk
       
   281 	TInt iFileOffset;	
       
   282 	// RSC file size 
       
   283 	TUint32 iFileSize;		
       
   284 	};
       
   285 
       
   286 
       
   287 #endif//__BARSCIMPL_H__