1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/oldezlib/zip/ZipFileMemberInputStream.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,297 @@
1.4 +// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// $Revision: 1.1 $
1.18 +//
1.19 +//
1.20 +
1.21 +
1.22 +#include "oldzipfile.h"
1.23 +#include "oldzipfilememberinputstream.h"
1.24 +
1.25 +using namespace TOLDEZIP;
1.26 +
1.27 +RZipFileMemberReaderStream::RZipFileMemberReaderStream(
1.28 + CZipFile& aZipFile,
1.29 + TUint32 aDataOffset,
1.30 + TUint32 aCompressedSize,
1.31 + TUint32 aUncompressedSize,
1.32 + TUint32 aCompressionMethod):
1.33 + iZipFile(aZipFile),
1.34 + iCompressionMethod(aCompressionMethod),
1.35 + iCompressedSize(aCompressedSize),
1.36 + iUncompressedSize(aUncompressedSize),
1.37 + iFileOffset(aDataOffset)
1.38 + {
1.39 + }
1.40 +
1.41 +
1.42 +RZipFileMemberReaderStream* RZipFileMemberReaderStream::NewL(
1.43 + CZipFile& aZipFile,
1.44 + TUint32 aDataOffset,
1.45 + TUint32 aCompressedSize,
1.46 + TUint32 aUncompressedSize,
1.47 + TUint32 aCompressionMethod)
1.48 + {
1.49 + RZipFileMemberReaderStream* me = new(ELeave) RZipFileMemberReaderStream(aZipFile, aDataOffset, aCompressedSize,aUncompressedSize, aCompressionMethod);
1.50 + CleanupStack::PushL(me);
1.51 + me->ConstructL();
1.52 + CleanupStack::Pop(me);
1.53 + return me;
1.54 + }
1.55 +
1.56 +/**
1.57 +Creates input stream to be used for reading the contents of the compressed file.
1.58 +*/
1.59 +void RZipFileMemberReaderStream::ConstructL()
1.60 + {
1.61 + TInt err = inflateInit2(&iStream, -MAX_WBITS);
1.62 + if (err == Z_MEM_ERROR)
1.63 + {
1.64 + User::Leave(KErrNoMemory);
1.65 + }
1.66 + }
1.67 +
1.68 +/**
1.69 +Destructor. All dynamically allocated data structures for this stream are freed.
1.70 +*/
1.71 +EXPORT_C RZipFileMemberReaderStream::~RZipFileMemberReaderStream()
1.72 +{
1.73 + inflateEnd(&iStream);
1.74 +}
1.75 +
1.76 +/**
1.77 +Reads data from the stream buffer into the specified descriptor.
1.78 +On return, contains the data read from the stream buffer
1.79 +
1.80 +@param aDes The target descriptor for the data read from the stream buffer
1.81 +@param aLength The maximum number of bytes to be read
1.82 +@return KErrNone If all bytes read successfully.
1.83 +@return KErrCorrupt If reading fails.
1.84 +@return KErrEof If end of file is reached.
1.85 +@return ... Any one of the system-wide error codes for other errors.
1.86 +*/
1.87 +EXPORT_C TInt RZipFileMemberReaderStream::Read(TDes16& aDes, TInt aLength)
1.88 +{
1.89 + TUint32 numBytesRead = 0;
1.90 + TInt err = Read(CONST_CAST(TByte*,(const TByte*)aDes.Ptr()), 2*aLength, &numBytesRead);
1.91 + if (err != KErrNone)
1.92 + {
1.93 + aDes.SetLength( (err==KErrEof) ? numBytesRead>>2 : 0 );
1.94 + return err;
1.95 + }
1.96 +
1.97 + aDes.SetLength(numBytesRead>>2);
1.98 + return KErrNone;
1.99 +}
1.100 +
1.101 +TInt RZipFileMemberReaderStream::Read(void)
1.102 + {
1.103 + TByte b;
1.104 + TUint32 numRead = 0;
1.105 +
1.106 + if (Read(&b, 1, &numRead) == 1)
1.107 + {
1.108 + return b;
1.109 + }
1.110 + else
1.111 + {
1.112 + return -1;
1.113 + }
1.114 + }
1.115 +
1.116 +TInt RZipFileMemberReaderStream::Read(TDes8& aDes, TInt aLength)
1.117 + {
1.118 + TUint32 numBytesRead = 0;
1.119 + TInt err = Read(CONST_CAST(TByte*,aDes.Ptr()), aLength, &numBytesRead);
1.120 + if (err != KErrNone)
1.121 + {
1.122 + aDes.SetLength( (err==KErrEof) ? numBytesRead : 0 );
1.123 + return err;
1.124 + }
1.125 +
1.126 + aDes.SetLength(numBytesRead);
1.127 + return KErrNone;
1.128 + }
1.129 +
1.130 +void RZipFileMemberReaderStream::ReadL(TDes16& aDes, TInt aLength)
1.131 + {
1.132 + User::LeaveIfError(Read(aDes, aLength));
1.133 + }
1.134 +
1.135 +void RZipFileMemberReaderStream::Release()
1.136 +{}
1.137 +
1.138 +void RZipFileMemberReaderStream::Close()
1.139 +{}
1.140 +
1.141 +
1.142 +TInt RZipFileMemberReaderStream::Read(TByte* aBytes, TUint32 aLength, TUint32* aRetLength)
1.143 + {
1.144 + if (iCompressionMethod == CZipArchive::EDeflated)
1.145 + {
1.146 + return GetBytes(aBytes, aLength, aRetLength);
1.147 + }
1.148 + else
1.149 + {
1.150 + return GetStoredBytes(aBytes, aLength, aRetLength);
1.151 + }
1.152 + }
1.153 +
1.154 +TInt RZipFileMemberReaderStream::GetBytes(TByte* aBytes, TUint32 aLength, TUint32* aRetLength)
1.155 + {
1.156 + TUint32 bytesLeftToRead;
1.157 + TInt status;
1.158 +
1.159 + iBytesLength = 0;
1.160 +
1.161 + // Be careful not to confuse compressed bytes and uncompressed bytes.
1.162 + // The length passed in is in uncompressed bytes, compressed bytes
1.163 + // are mainly used in the GetCompressedBytes() function called.
1.164 + // iBytesLength refers to the number of bytes already read.
1.165 + // If the request is to read past the end of the file
1.166 + // we should return KErrNone on the first instance, i.e. return all bytes
1.167 + // read successfully. On the second instance return KErrEof, i.e. we have
1.168 + // already read all the bytes when another request comes in, so return KErrEof.
1.169 + // This follows the rules for reading an uncompressed file within this component
1.170 + // and this is also the way that RFile::ReadL() does it.
1.171 +
1.172 + if (aLength > iUncompressedSize)
1.173 + {
1.174 + aLength = iUncompressedSize; // no point trying to read more than we have
1.175 + }
1.176 +
1.177 + bytesLeftToRead = aLength;
1.178 +
1.179 + while (bytesLeftToRead > 0)
1.180 + {
1.181 + if (iStream.avail_in == 0)
1.182 + {
1.183 + if (GetCompressedBytes() != KErrNone)
1.184 + {
1.185 + return KErrCorrupt;
1.186 + }
1.187 + }
1.188 +
1.189 + // The decompression will be done in the user provided memory.
1.190 + iStream.next_out = &aBytes[iBytesLength];
1.191 + iStream.avail_out = aLength - iBytesLength;
1.192 + status = inflate(&iStream, Z_SYNC_FLUSH);
1.193 +
1.194 + switch (status)
1.195 + {
1.196 + case Z_OK:
1.197 + iBytesLength = aLength - iStream.avail_out;
1.198 + break;
1.199 +
1.200 + case Z_STREAM_END: //EOF
1.201 + if (iBytesLength == aLength - iStream.avail_out)
1.202 + {
1.203 + *aRetLength = iBytesLength;
1.204 + return KErrEof;
1.205 + }
1.206 + else
1.207 + {
1.208 + iBytesLength = aLength - iStream.avail_out;
1.209 + break;
1.210 + }
1.211 + case Z_MEM_ERROR:
1.212 + return KErrNoMemory;
1.213 +
1.214 + default:
1.215 + return KErrCorrupt;
1.216 +
1.217 + }
1.218 + bytesLeftToRead = aLength - iBytesLength;
1.219 + }
1.220 +
1.221 + *aRetLength = iBytesLength;
1.222 + return KErrNone;
1.223 + }
1.224 +
1.225 +TInt RZipFileMemberReaderStream::GetCompressedBytes(void)
1.226 + {
1.227 + if (iOffset < iCompressedSize)
1.228 + {
1.229 + TUint32 nBytesLeft;
1.230 + TUint32 nBytesToRead;
1.231 +
1.232 + nBytesLeft = iCompressedSize - iOffset;
1.233 + nBytesToRead = (nBytesLeft > sizeof(iCompressedBytes)) ? sizeof(iCompressedBytes) : nBytesLeft;
1.234 + if (iZipFile.Seek(iFileOffset) != KErrNone)
1.235 + {
1.236 + return KErrCorrupt;
1.237 + }
1.238 + else
1.239 + if (iZipFile.Read(iCompressedBytes, nBytesToRead) != KErrNone)
1.240 + {
1.241 + return KErrCorrupt;
1.242 + }
1.243 + iFileOffset += nBytesToRead;
1.244 + iOffset += nBytesToRead;
1.245 + iStream.next_in = iCompressedBytes;
1.246 + iStream.avail_in = nBytesToRead;
1.247 + return KErrNone;
1.248 + }
1.249 + else
1.250 + if (iDone == EFalse)
1.251 + {
1.252 + iCompressedBytes[0] = 0;
1.253 + iStream.avail_in = 1;
1.254 + iStream.next_in = iCompressedBytes;
1.255 + iDone = ETrue;
1.256 + return KErrNone;
1.257 + }
1.258 + else
1.259 + {
1.260 + return KErrCorrupt;
1.261 + }
1.262 + }
1.263 +
1.264 +TInt RZipFileMemberReaderStream::GetStoredBytes(TByte* aBytes, TUint32 aLength, TUint32* aRetLength)
1.265 + {
1.266 + TInt status;
1.267 +
1.268 + if (aLength > iUncompressedSize)
1.269 + {
1.270 + aLength = iUncompressedSize; // no point trying to read more than we have
1.271 + }
1.272 + if (aLength == 0) // empty file like a directory
1.273 + {
1.274 + return KErrNone;
1.275 + }
1.276 + if (iOffset == iCompressedSize) // end of zip item.
1.277 + {
1.278 + return KErrEof;
1.279 + }
1.280 + if ((iOffset + aLength) > iCompressedSize)
1.281 + {
1.282 + aLength = iCompressedSize - iOffset; // adjust read to what is left
1.283 + if ( aLength <= 0 )
1.284 + {
1.285 + return KErrCorrupt;
1.286 + }
1.287 + }
1.288 + if (iZipFile.Seek(iFileOffset) != KErrNone)
1.289 + {
1.290 + return KErrCorrupt;
1.291 + }
1.292 + status = iZipFile.Read(aBytes, aLength);
1.293 + if (status == KErrNone)
1.294 + {
1.295 + iFileOffset += aLength;
1.296 + iOffset += aLength;
1.297 + *aRetLength = aLength;
1.298 + }
1.299 + return status;
1.300 + }