os/kernelhwsrv/userlibandfileserver/fileserver/sfile/sf_pgcompr.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1995-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 the License "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 // f32\sfile\sf_pgcompr.cpp
    15 // 
    16 //
    17 
    18 #include <f32file.h>
    19 #include "sf_std.h"
    20 #include "sf_ldr.h"
    21 #include <f32image.h>
    22 #include "sf_image.h"
    23 #include <e32uid.h>
    24 #include <e32rom.h>
    25 #include "sf_cache.h"
    26 
    27 #include "sf_pgcompr.h"
    28 
    29 extern TInt BytePairDecompress(TUint8* /*dst*/, TInt /*dstSize*/, TUint8* /*src*/, TInt /*srcSize*/, TUint8*& /*srcNext*/);
    30 
    31 
    32 // CBytePairReader - reading from in-memory buffer
    33 
    34 
    35 CBytePairReader* CBytePairReader::NewLC(TUint8* aBuffer, TUint32 aLength)
    36 	{
    37 	CBytePairReader* reader = new (ELeave) CBytePairReader(aBuffer, aLength);
    38 	CleanupStack::PushL(reader);
    39 	return reader;
    40 	}
    41 
    42 
    43 CBytePairReader::CBytePairReader(TUint8* aBuffer, TUint32 aLength)
    44 	: iNextPage(aBuffer), iBytesLeft(aLength)
    45 	{
    46 	}
    47 
    48 
    49 void CBytePairReader::SeekForwardL(TUint aBytes)
    50 	{
    51 	if (iBytesLeft < aBytes)
    52 		LEAVE_FAILURE(KErrCorrupt);
    53 	iNextPage += aBytes;
    54 	iBytesLeft -= aBytes;
    55 	}
    56 
    57 
    58 void CBytePairReader::ReadInTableL()
    59 	{
    60 	if (KIndexTableHeaderSize > iBytesLeft)
    61 		LEAVE_FAILURE(KErrCorrupt);
    62 	Mem::Copy(&iHeader, iNextPage, KIndexTableHeaderSize);
    63 	iNextPage += KIndexTableHeaderSize;
    64 	iBytesLeft -= KIndexTableHeaderSize;
    65 	
    66 	__IF_DEBUG(Printf("numberOfPages:%d", iHeader.iNumberOfPages));
    67 	
    68 	TUint size = iHeader.iNumberOfPages * sizeof(TUint16);
    69 	if (size > iBytesLeft)
    70 		LEAVE_FAILURE(KErrCorrupt);
    71 	// coverity[buffer_alloc]
    72 	iIndexTable = new (ELeave) TUint16[size/sizeof(TUint16)];
    73 	Mem::Copy(iIndexTable, iNextPage, size);
    74 	iNextPage += size;
    75 	iBytesLeft -= size;
    76 	} 
    77 
    78 
    79 void CBytePairReader::ReleaseTable()
    80 	{
    81 	delete[] iIndexTable;
    82 	iIndexTable = NULL;
    83 	}
    84 
    85 
    86 TUint CBytePairReader::GetPageL(TUint aPageNum, TUint8* aTarget, TInt aLength, TMemoryMoveFunction aMemMoveFn)
    87 	{
    88 	if (iIndexTable[aPageNum] > iBytesLeft)
    89 		LEAVE_FAILURE(KErrCorrupt);
    90 	iBytesLeft -= iIndexTable[aPageNum];
    91 	aLength = Min(aLength, KBytePairPageSize);
    92 
    93 	TInt size;
    94 	TUint8* nextPage;
    95 
    96 	if (aMemMoveFn)
    97 		size = BytePairDecompress(iPageBuf, aLength, iNextPage, iIndexTable[aPageNum], nextPage);
    98 	else
    99 		size = BytePairDecompress(aTarget, aLength, iNextPage, iIndexTable[aPageNum], nextPage);
   100 		
   101 	User::LeaveIfError(size);
   102 	if (size != aLength)
   103 		LEAVE_FAILURE(KErrCorrupt);
   104 	if (iNextPage + iIndexTable[aPageNum] != nextPage)
   105 		LEAVE_FAILURE(KErrCorrupt);
   106 
   107 	// If a memmove() was provided, use that to copy the data to its final target
   108 	if (aMemMoveFn)
   109 		aMemMoveFn(aTarget, iPageBuf, size);
   110 
   111 	iNextPage = nextPage;
   112 	return size;
   113 	}
   114 
   115 
   116 TUint CBytePairReader::DecompressPagesL(TUint8* aTarget, TInt aLength, TMemoryMoveFunction aMemMoveFn)
   117 	{
   118 	TUint decompressedSize = 0;
   119 
   120 	ReadInTableL();
   121 	
   122 	for (TUint curPage = 0; curPage < iHeader.iNumberOfPages; ++curPage)
   123 		{
   124 		TUint size = GetPageL(curPage, aTarget, aLength, aMemMoveFn);
   125 		
   126 		decompressedSize += size;
   127 		aTarget += size;
   128 		aLength -= size;
   129 		
   130 		__IF_DEBUG(Printf("decomp page size:%d\n", size ));
   131 		} 
   132 	
   133 	__IF_DEBUG(Printf("decompressedSize:%d", decompressedSize));
   134 
   135 	ReleaseTable();
   136 
   137 	return decompressedSize;
   138 	}
   139 
   140 
   141 void CBytePairReader::GetPageOffsetsL(TInt32 aInitialOffset, TInt& aPageCount, TInt32*& aPageOffsets)
   142 	{
   143 	ReadInTableL();
   144 	aPageCount = iHeader.iNumberOfPages;
   145 	aPageOffsets = new (ELeave) TInt32[aPageCount+1];
   146 
   147 	TInt bytes = aInitialOffset + KIndexTableHeaderSize + aPageCount * sizeof(TUint16);
   148 	for (TInt i = 0; i < aPageCount; ++i)
   149 		{
   150 		aPageOffsets[i] = bytes;
   151 		bytes += iIndexTable[i];
   152 		}
   153 	aPageOffsets[aPageCount] = bytes;
   154 
   155 	ReleaseTable();
   156 	}
   157 
   158 
   159 // CBytePairFileReader - reading from file
   160 
   161 
   162 CBytePairFileReader* CBytePairFileReader::NewLC(RFile& aFile)
   163 	{
   164 	CBytePairFileReader* reader = new (ELeave) CBytePairFileReader(aFile);
   165 	CleanupStack::PushL(reader);
   166 	return reader;
   167 	}
   168 
   169 
   170 CBytePairFileReader::CBytePairFileReader(RFile& aFile)
   171 	: CBytePairReader(NULL, 0), iFile(aFile)
   172 	{
   173 	}	
   174 
   175 
   176 CBytePairFileReader::~CBytePairFileReader()
   177 	{
   178 	ReleaseTable();
   179 	}
   180 
   181 
   182 void CBytePairFileReader::SeekForwardL(TUint aBytes)
   183 	{
   184 	TInt bytes = aBytes;
   185 	User::LeaveIfError(iFile.Seek(ESeekCurrent, bytes));
   186 	}
   187 	
   188 
   189 void CBytePairFileReader::ReadInTableL()
   190 	{
   191 	TPtr8 header((TUint8*)&iHeader, KIndexTableHeaderSize);
   192 	User::LeaveIfError(iFile.Read(header, KIndexTableHeaderSize));
   193 	if (header.Length() != (TInt)KIndexTableHeaderSize)
   194 		LEAVE_FAILURE(KErrCorrupt);
   195 	
   196 	__IF_DEBUG(Printf("numberOfPages:%d", iHeader.iNumberOfPages));
   197 	
   198 	TInt size = iHeader.iNumberOfPages * sizeof(TUint16);
   199 	iIndexTable = new (ELeave) TUint16[size/sizeof(TUint16)];
   200 	TPtr8 indexTable((TUint8*)iIndexTable, size);
   201 	User::LeaveIfError(iFile.Read(indexTable, size));
   202 	if (indexTable.Length() != size)
   203 		LEAVE_FAILURE(KErrCorrupt);
   204 	} 
   205 
   206 
   207 TUint CBytePairFileReader::DecompressPagesL(TUint8* aTarget, TInt aLength, TMemoryMoveFunction aMemMoveFn)
   208 	{
   209 	TUint decompressedSize = 0;
   210 
   211 	ReadInTableL();
   212 	
   213 	TUint curPage = 0;
   214 	while (curPage < iHeader.iNumberOfPages)
   215 		{
   216 		TUint bytes = 0;
   217 		TUint pages = 0;
   218 		while (curPage + pages < iHeader.iNumberOfPages &&
   219 			   bytes + iIndexTable[curPage+pages] < sizeof(iBuffer))
   220 			{
   221 			bytes += iIndexTable[curPage+pages];
   222 			++pages;
   223 			}
   224 		if(!bytes)
   225 			LEAVE_FAILURE(KErrCorrupt);
   226 		TPtr8 data(iBuffer, bytes);
   227 		User::LeaveIfError(iFile.Read(data, bytes));
   228 		if (data.Length() != (TInt)bytes)
   229 			LEAVE_FAILURE(KErrCorrupt);
   230 		iNextPage = iBuffer;
   231 		iBytesLeft = bytes;
   232 
   233 		for (; pages; ++curPage, --pages)
   234 			{
   235 			TUint size = GetPageL(curPage, aTarget, aLength, aMemMoveFn);
   236 		
   237 			decompressedSize += size;
   238 			aTarget += size;
   239 			aLength -= size;
   240 		
   241 			__IF_DEBUG(Printf("decomp page size:%d\n", size ));
   242 			}
   243 		} 
   244 	
   245 	__IF_DEBUG(Printf("decompressedSize:%d", decompressedSize));
   246 
   247 	ReleaseTable();
   248 
   249 	return decompressedSize;
   250 	}