installationservices/swi/source/sisfile/compresseddataprovider.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Definition of the Swi::CCompressedDataProvider
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <ezdecompressor.h>
       
    21 #include <ezstream.h>
       
    22 
       
    23 #include "compresseddataprovider.h"
       
    24 
       
    25 using namespace Swi;
       
    26 
       
    27 /*static*/ CCompressedDataProvider* CCompressedDataProvider::NewLC(MSisDataProvider& aDataProvider,
       
    28 															       TInt aBufferSize)
       
    29 	{
       
    30 	CCompressedDataProvider* self = new(ELeave) CCompressedDataProvider(aDataProvider);
       
    31 	CleanupStack::PushL(self);
       
    32 	self->ConstructL(aBufferSize);
       
    33 	return self;
       
    34 	}
       
    35 
       
    36 /*static*/ CCompressedDataProvider* CCompressedDataProvider::NewL(MSisDataProvider& aDataProvider,
       
    37 																  TInt aBufferSize)
       
    38 	{
       
    39 	CCompressedDataProvider* self = NewLC(aDataProvider, aBufferSize);
       
    40 	CleanupStack::Pop(self);
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 CCompressedDataProvider::CCompressedDataProvider(MSisDataProvider& aDataProvider) : iDataProvider(aDataProvider), 
       
    45 																					iInputDescriptor(NULL,0), 
       
    46 																					iOutputDescriptor(NULL,0),
       
    47 																					iMore(ETrue)
       
    48 	{	
       
    49 	}
       
    50 
       
    51 void CCompressedDataProvider::ConstructL(TInt aBufferSize)
       
    52 	{
       
    53 	TInt bufferSize;
       
    54 	if (aBufferSize > KDeflateBufferMaxSize)
       
    55 		{
       
    56 		bufferSize = KDeflateBufferMaxSize;
       
    57 		}
       
    58 	else
       
    59 		{
       
    60 		bufferSize = aBufferSize;
       
    61 		}
       
    62 	
       
    63 	iInputBuffer = HBufC8::NewL(bufferSize);
       
    64     iOutputBuffer = HBufC8::NewL(bufferSize);
       
    65 	iInputDescriptor.Set(iInputBuffer->Des());
       
    66 	iOutputDescriptor.Set(iOutputBuffer->Des());
       
    67 	iDecompressor = CEZDecompressor::NewL(*this);
       
    68 	iBuffer = CBufFlat::NewL(bufferSize);
       
    69 	}
       
    70 
       
    71 CCompressedDataProvider::~CCompressedDataProvider()
       
    72 	{
       
    73 	delete iDecompressor;
       
    74     delete iInputBuffer;
       
    75     delete iOutputBuffer;
       
    76     delete iBuffer;	
       
    77 	}
       
    78     
       
    79 TInt CCompressedDataProvider::Read(TDes8& aDes)
       
    80 	{
       
    81 	return Read(aDes, aDes.MaxSize());
       
    82 	}
       
    83 
       
    84 TInt CCompressedDataProvider::Read(TDes8& aDes, TInt aLength)
       
    85 	{ 	
       
    86 	if (aLength < 0)                  // Cannot read a negative amount of data!
       
    87 		{
       
    88 		return KErrArgument;
       
    89 		}
       
    90 
       
    91 	if (aLength > aDes.MaxSize())     // The descriptor is not big enough
       
    92 		{
       
    93 		return KErrOverflow;
       
    94 		}
       
    95 
       
    96 	TInt needed = aLength;
       
    97 	
       
    98 	if (aLength > iBuffer->Size())    // will still need to decompress more
       
    99 		{
       
   100 		iBuffer->Read(0, aDes, iBuffer->Size()); // read any buffered data
       
   101 		needed -= iBuffer->Size();
       
   102 		iBuffer->Reset(); // all data has been read, free the memory
       
   103 		}
       
   104 	else
       
   105 		{  	                          // enough data in the buffer no need to decompress more
       
   106 		iBuffer->Read(0, aDes, aLength);	// read the whole length from buffered data
       
   107 		iBuffer->Delete(0, aLength); // delete the data that has been read from the buffer
       
   108 		return KErrNone;		
       
   109 		}
       
   110 	
       
   111 	// Not enough data buffered yet... 
       
   112 	// We need to uncompress more data! (if available)
       
   113 
       
   114 	if (!iMore)  // no more data to uncompress
       
   115 		{
       
   116 		return KErrNone;
       
   117 		}
       
   118 
       
   119 	// If we are here we used up all the buffered data!!
       
   120 
       
   121 	while (iMore)
       
   122 		{
       
   123 		TRAPD(error, iMore = iDecompressor->InflateL());
       
   124 		if(KErrNone != error)
       
   125 			{
       
   126 			return error;
       
   127 			}
       
   128 		
       
   129 		TInt currentLength = aDes.Length();	
       
   130 		
       
   131 		if (needed > iBuffer->Size())    // will still need to decompress more
       
   132 			{
       
   133 			// Append the new data to the descriptor
       
   134 			aDes.SetLength(currentLength + iBuffer->Size());
       
   135 			TPtr8 rest = aDes.MidTPtr(currentLength, iBuffer->Size()); 
       
   136 			iBuffer->Read(0, rest, iBuffer->Size()); // read the buffered data
       
   137 			needed -= iBuffer->Size();
       
   138 			iBuffer->Reset(); // all data has been read, free the memory
       
   139 			}
       
   140 		else
       
   141 			{  	                          // enough data in the buffer no need to decompress more
       
   142 			// Append the new data to the descriptor
       
   143 			aDes.SetLength(currentLength + needed);
       
   144 			TPtr8 rest = aDes.MidTPtr(currentLength, needed); 
       
   145 			iBuffer->Read(0, rest, needed); // read the needed amount from buffered data
       
   146 			iBuffer->Delete(0, needed); // delete the data that has been read from the buffer
       
   147 			return KErrNone;		
       
   148 			}
       
   149 
       
   150 		if ((!iMore) || (needed==0))  // no more data to uncompress or needed
       
   151 			{
       
   152 			return KErrNone;
       
   153 			}
       
   154 		}
       
   155 	return KErrGeneral;
       
   156 	}
       
   157 
       
   158 TInt CCompressedDataProvider::Seek(TSeek /*aMode*/, TInt64& /*aPos*/)
       
   159 	{
       
   160 	// we cannot seek on a compressed stream at the moment!
       
   161 	return KErrNotSupported;
       
   162 	}
       
   163 
       
   164 
       
   165 void CCompressedDataProvider::InitializeL(CEZZStream& aZStream)
       
   166 	{
       
   167 	User::LeaveIfError(iDataProvider.Read(iInputDescriptor));
       
   168 
       
   169 	aZStream.SetInput(iInputDescriptor);
       
   170 	aZStream.SetOutput(iOutputDescriptor);
       
   171 	}
       
   172 
       
   173 void CCompressedDataProvider::NeedInputL(CEZZStream& aZStream)
       
   174 	{
       
   175 	User::LeaveIfError(iDataProvider.Read(iInputDescriptor));
       
   176 	aZStream.SetInput(iInputDescriptor);
       
   177 	}
       
   178 
       
   179 void CCompressedDataProvider::NeedOutputL(CEZZStream& aZStream)
       
   180 	{
       
   181 	TPtrC8 od = aZStream.OutputDescriptor();
       
   182 	iBuffer->InsertL(iBuffer->Size(), od);    // Should really check we are not blowing everything up!!!
       
   183 	iOutputDescriptor.SetLength(0); // reset since data now stored in buffer
       
   184 	aZStream.SetOutput(iOutputDescriptor);
       
   185 	}
       
   186 
       
   187 void CCompressedDataProvider::FinalizeL(CEZZStream& aZStream)
       
   188 	{
       
   189 	TPtrC8 od = aZStream.OutputDescriptor();
       
   190 	iBuffer->InsertL(iBuffer->Size(), od);
       
   191 	}