os/ossrv/compressionlibs/ziplib/test/oldezlib/zip/ZipFileMemberInputStream.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2003-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 // $Revision: 1.1 $
    15 // 
    16 //
    17 
    18 
    19 #include "oldzipfile.h"
    20 #include "oldzipfilememberinputstream.h"
    21 
    22 using namespace TOLDEZIP;
    23 
    24 RZipFileMemberReaderStream::RZipFileMemberReaderStream(
    25     CZipFile& aZipFile,
    26     TUint32   aDataOffset,
    27     TUint32   aCompressedSize,
    28     TUint32   aUncompressedSize,
    29     TUint32   aCompressionMethod):
    30     iZipFile(aZipFile),
    31 	iCompressionMethod(aCompressionMethod),
    32     iCompressedSize(aCompressedSize),
    33     iUncompressedSize(aUncompressedSize),
    34 	iFileOffset(aDataOffset)
    35     {    
    36     }
    37 
    38 
    39 RZipFileMemberReaderStream* RZipFileMemberReaderStream::NewL(
    40 	CZipFile& aZipFile,
    41     TUint32   aDataOffset,
    42     TUint32   aCompressedSize,
    43     TUint32   aUncompressedSize,
    44     TUint32   aCompressionMethod)
    45     {    
    46 	RZipFileMemberReaderStream* me = new(ELeave) RZipFileMemberReaderStream(aZipFile, aDataOffset, aCompressedSize,aUncompressedSize, aCompressionMethod);
    47 	CleanupStack::PushL(me);
    48 	me->ConstructL();
    49 	CleanupStack::Pop(me);
    50 	return me;		
    51     }
    52 
    53 /**
    54 Creates input stream to be used for reading the contents of the compressed file.
    55 */
    56 void RZipFileMemberReaderStream::ConstructL()
    57 	{
    58 	TInt err = inflateInit2(&iStream, -MAX_WBITS);
    59 	if (err == Z_MEM_ERROR)
    60 		{
    61 		User::Leave(KErrNoMemory);
    62 		}
    63 	}
    64 
    65 /**
    66 Destructor. All dynamically allocated data structures for this stream are freed.
    67 */
    68 EXPORT_C RZipFileMemberReaderStream::~RZipFileMemberReaderStream()
    69 {
    70 	inflateEnd(&iStream);
    71 }
    72 
    73 /**
    74 Reads data from the stream buffer into the specified descriptor.
    75 On return, contains the data read from the stream buffer
    76 
    77 @param aDes The target descriptor for the data read from the stream buffer
    78 @param aLength The maximum number of bytes to be read
    79 @return KErrNone If all bytes read successfully.
    80 @return	KErrCorrupt If reading fails.
    81 @return KErrEof If end of file is reached.
    82 @return ... Any one of the system-wide error codes for other errors.
    83 */
    84 EXPORT_C TInt RZipFileMemberReaderStream::Read(TDes16& aDes, TInt aLength)
    85 {
    86 	TUint32 numBytesRead = 0;
    87 	TInt err = Read(CONST_CAST(TByte*,(const TByte*)aDes.Ptr()), 2*aLength, &numBytesRead);
    88 	if (err != KErrNone)
    89 	{
    90 		aDes.SetLength( (err==KErrEof) ? numBytesRead>>2 : 0 );
    91 		return err;
    92 	}
    93 	
    94 	aDes.SetLength(numBytesRead>>2);
    95 	return KErrNone;
    96 }
    97 
    98 TInt RZipFileMemberReaderStream::Read(void)
    99 	{
   100 	TByte b;
   101 	TUint32 numRead = 0;
   102 	
   103 	if (Read(&b, 1, &numRead) == 1)
   104 		{
   105 		return b;
   106 		}
   107 	else
   108 		{
   109 		return -1;
   110 		}
   111 	}
   112 	
   113 TInt RZipFileMemberReaderStream::Read(TDes8& aDes, TInt aLength)
   114 	{
   115 	TUint32 numBytesRead = 0;
   116 	TInt err = Read(CONST_CAST(TByte*,aDes.Ptr()), aLength, &numBytesRead);
   117 	if (err != KErrNone)
   118 	{
   119 		aDes.SetLength( (err==KErrEof) ? numBytesRead : 0 );
   120 		return err;
   121 	}
   122 
   123 	aDes.SetLength(numBytesRead);
   124 	return KErrNone;
   125 	}
   126 
   127 void RZipFileMemberReaderStream::ReadL(TDes16& aDes, TInt aLength)
   128 	{
   129 	User::LeaveIfError(Read(aDes, aLength));
   130 	}
   131 
   132 void RZipFileMemberReaderStream::Release()
   133 {}
   134 
   135 void RZipFileMemberReaderStream::Close()
   136 {}
   137 
   138 
   139 TInt RZipFileMemberReaderStream::Read(TByte* aBytes, TUint32 aLength, TUint32* aRetLength)
   140 	{
   141 	if (iCompressionMethod == CZipArchive::EDeflated)
   142 		{
   143 		return GetBytes(aBytes, aLength, aRetLength);
   144 		}
   145 	else
   146 		{
   147 		return GetStoredBytes(aBytes, aLength, aRetLength);
   148 		}
   149 	}
   150 	
   151 TInt RZipFileMemberReaderStream::GetBytes(TByte* aBytes, TUint32 aLength, TUint32* aRetLength)
   152 	{
   153 	TUint32 bytesLeftToRead;
   154 	TInt	status;
   155 
   156 	iBytesLength = 0;
   157 
   158 	// Be careful not to confuse compressed bytes and uncompressed bytes.
   159 	// The length passed in is in uncompressed bytes, compressed bytes
   160 	// are mainly used in the GetCompressedBytes() function called.
   161 	// iBytesLength refers to the number of bytes already read.
   162 	// If the request is to read past the end of the file
   163 	// we should return KErrNone on the first instance, i.e. return all bytes
   164 	// read successfully. On the second instance return KErrEof, i.e. we have 
   165 	// already read all the bytes when another request comes in, so return KErrEof.
   166 	// This follows the rules for reading an uncompressed file within this component
   167 	// and this is also the way that RFile::ReadL() does it.
   168 
   169 	if (aLength > iUncompressedSize)
   170 		{
   171 		aLength = iUncompressedSize; // no point trying to read more than we have
   172 		}
   173 
   174 	bytesLeftToRead = aLength;
   175 
   176 	while (bytesLeftToRead > 0) 
   177 		{
   178 		if (iStream.avail_in == 0)
   179 			{
   180 			if (GetCompressedBytes() != KErrNone)
   181 				{
   182 				return KErrCorrupt;
   183 				}
   184 			}
   185 
   186 		// The decompression will be done in the user provided memory.
   187 		iStream.next_out = &aBytes[iBytesLength];
   188 		iStream.avail_out = aLength - iBytesLength;
   189 		status = inflate(&iStream, Z_SYNC_FLUSH);
   190 
   191 		switch (status)
   192 			{
   193 			case Z_OK:
   194 				iBytesLength = aLength - iStream.avail_out;
   195 				break;
   196 				
   197 			case Z_STREAM_END:	//EOF
   198 				if (iBytesLength == aLength - iStream.avail_out)
   199 					{
   200 					*aRetLength = iBytesLength;
   201 					return KErrEof;
   202 					}
   203 				else
   204 					{
   205 					iBytesLength = aLength - iStream.avail_out;
   206 					break;
   207 					}
   208 			case Z_MEM_ERROR:
   209 				return KErrNoMemory;
   210 				
   211 			default:
   212 				return KErrCorrupt;
   213 
   214 			}
   215 		bytesLeftToRead = aLength - iBytesLength;
   216 		}
   217 
   218 	*aRetLength = iBytesLength;
   219 	return KErrNone;
   220 	}
   221 
   222 TInt RZipFileMemberReaderStream::GetCompressedBytes(void)
   223 	{
   224 	if (iOffset < iCompressedSize)
   225 		{
   226 		TUint32	nBytesLeft;
   227 		TUint32 nBytesToRead;
   228 		
   229 		nBytesLeft = iCompressedSize - iOffset;
   230 		nBytesToRead = (nBytesLeft > sizeof(iCompressedBytes)) ? sizeof(iCompressedBytes) : nBytesLeft;
   231 		if (iZipFile.Seek(iFileOffset) != KErrNone)
   232 			{
   233 			return KErrCorrupt; 
   234 			}
   235 		else
   236 		if (iZipFile.Read(iCompressedBytes, nBytesToRead) != KErrNone)
   237 			{
   238 			return KErrCorrupt; 
   239 			}
   240 		iFileOffset += nBytesToRead;
   241 		iOffset += nBytesToRead;
   242 		iStream.next_in = iCompressedBytes;
   243 		iStream.avail_in = nBytesToRead;
   244 		return KErrNone;
   245 		}
   246 	else
   247 	if (iDone == EFalse)
   248 		{
   249 		iCompressedBytes[0] = 0;
   250 		iStream.avail_in = 1;
   251 		iStream.next_in = iCompressedBytes;
   252 		iDone = ETrue;
   253 		return KErrNone;
   254 		}
   255 	else
   256 		{
   257 		return KErrCorrupt; 
   258 		}
   259 	}
   260 	
   261 TInt RZipFileMemberReaderStream::GetStoredBytes(TByte* aBytes, TUint32 aLength, TUint32* aRetLength)
   262 	{
   263 	TInt status;
   264 	
   265 	if (aLength > iUncompressedSize)
   266 		{
   267 		aLength = iUncompressedSize; // no point trying to read more than we have
   268 		}
   269 	if (aLength == 0) // empty file like a directory
   270 		{
   271 		return KErrNone;
   272 		}
   273 	if (iOffset == iCompressedSize) // end of zip item.
   274 		{
   275 		return KErrEof; 
   276 		}
   277 	if ((iOffset + aLength) > iCompressedSize)
   278 		{
   279 		aLength = iCompressedSize - iOffset; // adjust read to what is left
   280 		if ( aLength <= 0 )
   281 			{
   282 			return KErrCorrupt; 
   283 			}
   284 		}
   285 	if (iZipFile.Seek(iFileOffset) != KErrNone)
   286 		{
   287 		return KErrCorrupt; 
   288 		}
   289 	status = iZipFile.Read(aBytes, aLength);
   290 	if (status == KErrNone)
   291 		{
   292 		iFileOffset += aLength;
   293 		iOffset += aLength;
   294 		*aRetLength = aLength;
   295 		}
   296 	return status;
   297 	}