toolsandutils/e32tools/compress/pagedcompress.cpp
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 // Copyright (c) 2005-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 // byte_pair.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <malloc.h>
       
    19 #include <string.h>
       
    20 #include <stdlib.h>
       
    21 #include "h_utl.h"
       
    22 #include "h_ver.h"
       
    23 #include <stdio.h>
       
    24 
       
    25 #include "byte_pair.h"
       
    26 
       
    27 #define PAGE_SIZE 4096
       
    28 
       
    29 typedef struct IndexTableItemTag
       
    30 {
       
    31 	TUint16 iSizeOfCompressedPageData;	// pointer to an array TUint16[NumberOfPages]
       
    32 	TUint8 *iCompressedPageData;		// pointer to an array TUint8*. Each elemet of 
       
    33 										// this array point a compressed Page data	
       
    34 }IndexTableItem;
       
    35 
       
    36 
       
    37 typedef struct IndexTableHeaderTag
       
    38 {
       
    39 	TInt	iSizeOfData;					// Includes the index and compressed pages
       
    40 	TInt	iDecompressedSize;
       
    41 	TUint16	iNumberOfPages;
       
    42 } IndexTableHeader;
       
    43 
       
    44 
       
    45 class CBytePairCompressedImage
       
    46 {
       
    47 	public:
       
    48 		static CBytePairCompressedImage* NewLC(TUint16 aNumberOfPages, TInt aSize);
       
    49 		
       
    50 		~CBytePairCompressedImage();
       
    51 		
       
    52 		void AddPage(TUint16 aPageNum, TUint8 * aPageData, TUint16 aPageSize, CBytePair *aBPE);
       
    53 		int  GetPage(TUint16 aPageNum, TUint8 * aPageData, CBytePair *aBPE);
       
    54 		void WriteOutTable(ostream &os);
       
    55 		int  ReadInTable(istream &is, TUint & aNumberOfPages);
       
    56 	
       
    57 	private:
       
    58 		TInt ConstructL( TUint16 aNumberOfPages, TInt aSize);
       
    59 		CBytePairCompressedImage();
       
    60 		
       
    61 	private:
       
    62 		IndexTableHeader 	iHeader;
       
    63 		IndexTableItem*		iPages;
       
    64 		TUint8* 			iOutBuffer; 
       
    65 		
       
    66 };
       
    67 
       
    68 
       
    69 CBytePairCompressedImage::CBytePairCompressedImage()
       
    70 {
       
    71 	
       
    72 }
       
    73 
       
    74 
       
    75 CBytePairCompressedImage* CBytePairCompressedImage::NewLC(TUint16 aNumberOfPages, TInt aSize)
       
    76 {
       
    77 	CBytePairCompressedImage* self = new CBytePairCompressedImage;
       
    78 	if( NULL == self)
       
    79 	{
       
    80 		return self;
       
    81 	}
       
    82 	
       
    83 	if( KErrNone == self->ConstructL(aNumberOfPages, aSize))
       
    84 	{
       
    85 		return self;
       
    86 	}
       
    87 	return NULL;
       
    88 }
       
    89 
       
    90 
       
    91 TInt CBytePairCompressedImage::ConstructL(TUint16 aNumberOfPages, TInt aSize)
       
    92 {
       
    93 #ifdef __DEBUG_MSG__
       
    94 	Print(EWarning,"Start ofCBytePairCompressedImage::ConstructL(%d, %d)\n", aNumberOfPages, aSize );
       
    95 #endif
       
    96 
       
    97 	iHeader.iNumberOfPages = aNumberOfPages;
       
    98 	iHeader.iDecompressedSize = aSize;
       
    99 	
       
   100 	if( 0 != aNumberOfPages)
       
   101 	{
       
   102 		iPages = (IndexTableItem *) calloc(aNumberOfPages, sizeof(IndexTableItem));
       
   103 		
       
   104 		if( NULL == iPages )
       
   105 		{
       
   106 			return KErrNoMemory;
       
   107 		}
       
   108 	}
       
   109 		
       
   110 	iHeader.iSizeOfData = 	sizeof(iHeader.iSizeOfData) + 
       
   111 							sizeof(iHeader.iDecompressedSize) + 
       
   112 							sizeof(iHeader.iNumberOfPages) + 
       
   113 							aNumberOfPages * sizeof(TUint16);
       
   114 	
       
   115 	iOutBuffer = (TUint8 *) calloc(4 * PAGE_SIZE, sizeof(TUint8) ); 
       
   116 	
       
   117 	if ( NULL == iOutBuffer)
       
   118 	{
       
   119 		return KErrNoMemory;
       
   120 	}
       
   121 	return KErrNone;
       
   122 } // End of ConstructL()
       
   123 
       
   124 CBytePairCompressedImage::~CBytePairCompressedImage()
       
   125 {
       
   126 #ifdef __DEBUG_MSG__
       
   127 	Print(EWarning,"Destructor calling.");
       
   128 #endif
       
   129 
       
   130 	for( int i = 0; i < iHeader.iNumberOfPages; i++)
       
   131 	{
       
   132 		free(iPages[i].iCompressedPageData);
       
   133 		iPages[i].iCompressedPageData = NULL;
       
   134 	}
       
   135 	
       
   136 	free( iPages );
       
   137 	iPages = NULL;
       
   138 	
       
   139 	free( iOutBuffer );
       
   140 	iOutBuffer = NULL;
       
   141 }
       
   142 
       
   143 
       
   144 void CBytePairCompressedImage::AddPage(TUint16 aPageNum, TUint8 * aPageData, TUint16 aPageSize, CBytePair *aBPE)
       
   145 {
       
   146 #ifdef __DEBUG_MSG__
       
   147 	Print(EWarning,"Start of AddPage(aPageNum:%d, ,aPageSize:%d)\n",aPageNum, aPageSize );
       
   148 #endif
       
   149 
       
   150 #ifdef __TEST_ONLY__
       
   151 
       
   152 	iPages[aPageNum].iSizeOfCompressedPageData = 2;
       
   153 
       
   154 	iPages[aPageNum].iCompressedPageData = (TUint8 *) calloc(iPages[aPageNum].iSizeOfCompressedPageData, sizeof(TUint8) );	
       
   155 	
       
   156 	memcpy(iPages[aPageNum].iCompressedPageData, (TUint8 *) &aPageNum, iPages[aPageNum].iSizeOfCompressedPageData);
       
   157 	
       
   158 	
       
   159 #else
       
   160 
       
   161 	TUint16 compressedSize = (TUint16) aBPE->Compress(iOutBuffer,aPageData,aPageSize);
       
   162 	iPages[aPageNum].iSizeOfCompressedPageData = compressedSize;
       
   163 
       
   164 #ifdef __DEBUG_MSG__
       
   165 	Print(EWarning,"Compressed page size:%d\n", iPages[aPageNum].iSizeOfCompressedPageData );
       
   166 #endif
       
   167 	
       
   168 	iPages[aPageNum].iCompressedPageData = (TUint8 *) calloc(iPages[aPageNum].iSizeOfCompressedPageData, sizeof(TUint8) );
       
   169 	
       
   170 	if( NULL == iPages[aPageNum].iCompressedPageData )
       
   171 	{
       
   172 		return;
       
   173 	}
       
   174 	
       
   175 	memcpy(iPages[aPageNum].iCompressedPageData, iOutBuffer, iPages[aPageNum].iSizeOfCompressedPageData );
       
   176 	
       
   177 #endif
       
   178 
       
   179 	iHeader.iSizeOfData += iPages[aPageNum].iSizeOfCompressedPageData;
       
   180 }
       
   181 
       
   182 void CBytePairCompressedImage::WriteOutTable(ostream &os)
       
   183 {
       
   184 	// Write out IndexTableHeader
       
   185 	os.write((const char *) &iHeader.iSizeOfData, sizeof(iHeader.iSizeOfData));
       
   186 	os.write((const char *) &iHeader.iDecompressedSize, sizeof(iHeader.iDecompressedSize));
       
   187 	os.write((const char *) &iHeader.iNumberOfPages, sizeof(iHeader.iNumberOfPages));
       
   188 	
       
   189 	TInt i;
       
   190 	// Write out IndexTableItems (size of each compressed page)
       
   191 	for(i = 0; i < iHeader.iNumberOfPages; i++)
       
   192 	{
       
   193 		os.write((const char *) &(iPages[i].iSizeOfCompressedPageData), sizeof(TUint16));
       
   194 	}
       
   195 	
       
   196 	// Write out compressed pages
       
   197 	for(i = 0; i < iHeader.iNumberOfPages; i++)
       
   198 	{
       
   199 		os.write((const char*)iPages[i].iCompressedPageData, iPages[i].iSizeOfCompressedPageData); 
       
   200 	}
       
   201 	
       
   202 }
       
   203 
       
   204 
       
   205 int CBytePairCompressedImage::ReadInTable(istream &is, TUint & aNumberOfPages)
       
   206 {
       
   207 	// Read page index table header 
       
   208 	is.read((char *)&iHeader, (sizeof(iHeader.iSizeOfData)+sizeof(iHeader.iDecompressedSize)+sizeof(iHeader.iNumberOfPages)));
       
   209 
       
   210 	// Allocatin place to Page index table entries
       
   211 	iPages = (IndexTableItem *) calloc(iHeader.iNumberOfPages, sizeof(IndexTableItem));
       
   212 		
       
   213 	if( NULL == iPages )
       
   214 	{
       
   215 		return KErrNoMemory;
       
   216 	}
       
   217 	TInt i;
       
   218 	// Read whole Page index table 
       
   219 
       
   220 	for(i = 0; i < iHeader.iNumberOfPages; i++)
       
   221 	{
       
   222 		is.read((char *) &(iPages[i].iSizeOfCompressedPageData), sizeof(TUint16));
       
   223 	}
       
   224 
       
   225 	// Read compressed data pages page by page, decompress and store them
       
   226 	for(i = 0; i < iHeader.iNumberOfPages; i++)
       
   227 	{
       
   228 
       
   229 		iPages[i].iCompressedPageData = (TUint8 *) calloc(iPages[i].iSizeOfCompressedPageData, sizeof(TUint8) );
       
   230 	
       
   231 		if( NULL == iPages[i].iCompressedPageData )
       
   232 		{
       
   233 			return KErrNoMemory;
       
   234 		}
       
   235 
       
   236 		is.read((char*)iPages[i].iCompressedPageData, iPages[i].iSizeOfCompressedPageData);
       
   237 	}
       
   238 
       
   239 	aNumberOfPages = iHeader.iNumberOfPages;
       
   240 
       
   241 	return KErrNone;
       
   242 }
       
   243 
       
   244 int  CBytePairCompressedImage::GetPage(TUint16 aPageNum, TUint8 * aPageData, CBytePair *aBPE)
       
   245 {
       
   246 	TUint8* pakEnd;
       
   247         
       
   248     const TInt MaxBlockSize = 0x1000;
       
   249         
       
   250    	TUint16 uncompressedSize = (TUint16) aBPE->Decompress( aPageData, 
       
   251 												MaxBlockSize, 
       
   252 												iPages[aPageNum].iCompressedPageData, 
       
   253 												iPages[aPageNum].iSizeOfCompressedPageData, 
       
   254 												pakEnd );
       
   255 
       
   256 	return uncompressedSize;
       
   257 
       
   258 
       
   259 }
       
   260 
       
   261 
       
   262 void CompressPages(TUint8 * bytes, TInt size, ostream& os, CBytePair *aBPE)
       
   263 {
       
   264 	// Build a list of compressed pages
       
   265 	TUint16 numOfPages = (TUint16) (size / PAGE_SIZE);
       
   266 
       
   267 #ifdef __DEBUG_MSG__
       
   268 	Print(EWarning,"numOfPages:%d, (size %% PAGE_SIZE:%d)\n", numOfPages, size % PAGE_SIZE);
       
   269 #endif
       
   270 
       
   271 	if ( size % PAGE_SIZE > 0)
       
   272 		++numOfPages;
       
   273 	
       
   274 	CBytePairCompressedImage *comprImage = CBytePairCompressedImage::NewLC(numOfPages, size);
       
   275 	if( NULL == comprImage)
       
   276 	{
       
   277 		Print(EError," NULL == comprImage\n");
       
   278 		return;
       
   279 	}
       
   280 	
       
   281 	TUint8* iPageStart;
       
   282 	TUint16 iPageLen;
       
   283 	TUint16 iPage = 0;
       
   284 	
       
   285 	while(iPage * PAGE_SIZE < size )
       
   286 	{
       
   287 		iPageStart = &bytes[iPage * PAGE_SIZE];
       
   288 		iPageLen = (TUint16)( (iPage + 1) * PAGE_SIZE < size ? PAGE_SIZE : size - iPage * PAGE_SIZE);
       
   289 		
       
   290 		comprImage->AddPage(iPage, iPageStart, iPageLen, aBPE);
       
   291 
       
   292 		++iPage;
       
   293 	}
       
   294 	
       
   295 	// Write out index table and compressed pages
       
   296 	comprImage->WriteOutTable(os);
       
   297 	
       
   298 	
       
   299 	delete comprImage;
       
   300 	comprImage = NULL;
       
   301 	
       
   302 }
       
   303 
       
   304 int DecompressPages(TUint8 * bytes, istream& is, CBytePair *aBPE)
       
   305 {
       
   306 	TUint decompressedSize = 0;
       
   307 	CBytePairCompressedImage *comprImage = CBytePairCompressedImage::NewLC(0, 0);
       
   308 	if( NULL == comprImage)
       
   309 	{
       
   310 		Print(EError," NULL == comprImage\n");
       
   311 		return KErrNoMemory;
       
   312 	}
       
   313 
       
   314 	TUint numberOfPages = 0;
       
   315 	comprImage->ReadInTable(is, numberOfPages);
       
   316 
       
   317 
       
   318 	TUint8* iPageStart;
       
   319 	TUint16 iPage = 0;
       
   320 	
       
   321 	while(iPage < numberOfPages )
       
   322 	{
       
   323 		iPageStart = &bytes[iPage * PAGE_SIZE];
       
   324 		
       
   325 		decompressedSize += comprImage->GetPage(iPage, iPageStart, aBPE);
       
   326 
       
   327 		++iPage;
       
   328 	}
       
   329 	
       
   330 	delete comprImage;
       
   331 	return decompressedSize;
       
   332 
       
   333 }