1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/oldezlib/EZLib/compressor.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,315 @@
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 +//
1.18 +
1.19 +#include "OldEZCompressor.h"
1.20 +
1.21 +using namespace TOLDEZLIB;
1.22 +
1.23 +CEZCompressor::CEZCompressor(MEZBufferManager* aInit) :
1.24 + iBufferInit(aInit)
1.25 + {
1.26 +
1.27 + }
1.28 +
1.29 +CEZCompressor::~CEZCompressor()
1.30 + {
1.31 + // Note that deflateEnd may have already been called by zlib if for example and alloc failure
1.32 + // occurred in deflateInit2. However there is no harm in calling deflateEnd twice.
1.33 +
1.34 + deflateEnd(&iStream);
1.35 + }
1.36 +
1.37 +/**
1.38 +Creates a new CEZCompressor object and leaves it on the CleanupStack
1.39 +
1.40 +@param aInit buffer manager to handle both input and output buffers
1.41 +@param aLevel compression levels
1.42 +@param aWindowBits the base two logarithm of the window size (the size of the history buffer). It should
1.43 +be in the range 8..15 for this version of the library. Larger values of this parameter result in better
1.44 +compression at the expense of memory usage.
1.45 +@param aMemLevel specifies how much memory should be allocated for the internal compression state.
1.46 +memLevel=1 uses minimum memory but is slow and reduces compression ratio; memLevel=9 uses maximum memory
1.47 +for optimal speed.
1.48 +@param aStrategy compression strategy - used to tune the compression algorithm. The strategy parameter only affects
1.49 +the compression ratio but not the correctness of the compressed output even if it is not set appropriately
1.50 +@see TStrategy
1.51 +@return the new CEZCompressor object (on the CleanupStack)
1.52 +*/
1.53 +EXPORT_C CEZCompressor *CEZCompressor::NewLC(MEZBufferManager& aInit, TInt aLevel, TInt aWindowBits,
1.54 + TInt aMemLevel, TStrategy aStrategy)
1.55 + {
1.56 + CEZCompressor* deflater = new (ELeave) CEZCompressor(&aInit);
1.57 + CleanupStack::PushL(deflater);
1.58 + deflater->ConstructL(aLevel,aWindowBits,aMemLevel,aStrategy);
1.59 + return deflater;
1.60 + }
1.61 +
1.62 +/**
1.63 +Creates a new CEZCompressor object
1.64 +
1.65 +@param aInit buffer manager to handle both input and output buffers
1.66 +@param aLevel compression levels
1.67 +@param aWindowBits the base two logarithm of the window size (the size of the history buffer). It should
1.68 +be in the range 8..15 for this version of the library. Larger values of this parameter result in better
1.69 +compression at the expense of memory usage.
1.70 +@param aMemLevel specifies how much memory should be allocated for the internal compression state.
1.71 +memLevel=1 uses minimum memory but is slow and reduces compression ratio; memLevel=9 uses maximum memory
1.72 +for optimal speed.
1.73 +@param aStrategy compression strategy - used to tune the compression algorithm. The strategy parameter only affects
1.74 +the compression ratio but not the correctness of the compressed output even if it is not set appropriately
1.75 +@see TStrategy
1.76 +@return the new CEZCompressor object
1.77 +*/
1.78 +EXPORT_C CEZCompressor* CEZCompressor::NewL(MEZBufferManager& aInit, TInt aLevel, TInt aWindowBits,
1.79 + TInt aMemLevel, TStrategy aStrategy)
1.80 + {
1.81 + CEZCompressor* deflater = new (ELeave) CEZCompressor(&aInit);
1.82 + CleanupStack::PushL(deflater);
1.83 + deflater->ConstructL(aLevel,aWindowBits,aMemLevel,aStrategy);
1.84 + CleanupStack::Pop();
1.85 + return deflater;
1.86 + }
1.87 +
1.88 +/**
1.89 +Overload of CEZCompressor constructor takes aDictionary argument
1.90 +
1.91 +@param aInit buffer manager to handle both input and output buffers
1.92 +@param aDictionary used to initialize the compression dictionary from the given byte sequence
1.93 +without producing any compressed output. The compressor and decompressor must use exactly the same dictionary.
1.94 +The dictionary should consist of strings (byte sequences) that are likely to be encountered later in the data to be compressed,
1.95 +with the most commonly used strings preferably put towards the end of the dictionary. Using a dictionary is most useful
1.96 +when the data to be compressed is short and can be predicted with good accuracy; the data can then be compressed better than
1.97 +with the default empty dictionary.
1.98 +@param aLevel compression level
1.99 +@param aWindowBits the base two logarithm of the window size (the size of the history buffer). It should
1.100 +be in the range 8..15 for this version of the library. Larger values of this parameter result in better
1.101 +compression at the expense of memory usage.
1.102 +@param aMemLevel specifies how much memory should be allocated for the internal compression state.
1.103 +memLevel=1 uses minimum memory but is slow and reduces compression ratio; memLevel=9 uses maximum memory
1.104 +for optimal speed.
1.105 +@param aStrategy compression strategy - used to tune the compression algorithm. The strategy parameter only affects
1.106 +the compression ratio but not the correctness of the compressed output even if it is not set appropriately
1.107 +@see TStrategy
1.108 +@return the new CEZCompressor object (on the CleanupStack)
1.109 +*/
1.110 +EXPORT_C CEZCompressor* CEZCompressor::NewLC(MEZBufferManager& aInit, const TDesC8& aDictionary,
1.111 + TInt aLevel, TInt aWindowBits, TInt aMemLevel, TStrategy aStrategy)
1.112 + {
1.113 + CEZCompressor* deflater = new (ELeave) CEZCompressor(&aInit);
1.114 + CleanupStack::PushL(deflater);
1.115 + deflater->ConstructL(aLevel,aDictionary.Ptr(),aDictionary.Size(),aWindowBits,aMemLevel,aStrategy);
1.116 + return deflater;
1.117 + }
1.118 +
1.119 +/**
1.120 +Overload of CEZCompressor constructor takes aDictionary argument
1.121 +
1.122 +@param aInit buffer manager to handle both input and output buffers
1.123 +@param aDictionary used to initialize the compression dictionary from the given byte sequence
1.124 +without producing any compressed output. The compressor and decompressor must use exactly the same dictionary.
1.125 +The dictionary should consist of strings (byte sequences) that are likely to be encountered later in the data to be compressed,
1.126 +with the most commonly used strings preferably put towards the end of the dictionary. Using a dictionary is most useful
1.127 +when the data to be compressed is short and can be predicted with good accuracy; the data can then be compressed better than
1.128 +with the default empty dictionary.
1.129 +@param aLevel compression level
1.130 +@param aWindowBits the base two logarithm of the window size (the size of the history buffer). It should
1.131 +be in the range 8..15 for this version of the library. Larger values of this parameter result in better
1.132 +compression at the expense of memory usage.
1.133 +@param aMemLevel specifies how much memory should be allocated for the internal compression state.
1.134 +memLevel=1 uses minimum memory but is slow and reduces compression ratio; memLevel=9 uses maximum memory
1.135 +for optimal speed.
1.136 +@param aStrategy compression strategy - used to tune the compression algorithm. The strategy parameter only affects
1.137 +the compression ratio but not the correctness of the compressed output even if it is not set appropriately
1.138 +@see TStrategy
1.139 +@return the new CEZCompressor object
1.140 +*/
1.141 +EXPORT_C CEZCompressor* CEZCompressor::NewL(MEZBufferManager& aInit, const TDesC8& aDictionary,
1.142 + TInt aLevel, TInt aWindowBits, TInt aMemLevel, TStrategy aStrategy)
1.143 + {
1.144 + CEZCompressor* deflater = new (ELeave) CEZCompressor(&aInit);
1.145 + CleanupStack::PushL(deflater);
1.146 + deflater->ConstructL(aLevel,aDictionary.Ptr(),aDictionary.Size(),aWindowBits,aMemLevel,aStrategy);
1.147 + CleanupStack::Pop();
1.148 + return deflater;
1.149 + }
1.150 +
1.151 +/**
1.152 +Resets the current compression operation, with the new buffer manager
1.153 +
1.154 +@param aInit new buffer manager to handle the new input and output buffers
1.155 +@leave ... Any of the system wide error codes
1.156 +*/
1.157 +EXPORT_C void CEZCompressor::ResetL(MEZBufferManager& aInit)
1.158 + {
1.159 + iBufferInit = &aInit;
1.160 + iBufferInit->InitializeL(*this);
1.161 + if (deflateReset(&iStream) == Z_STREAM_ERROR)
1.162 + User::Leave(KEZlibErrStream);
1.163 + iDeflationState = ENoFlush;
1.164 + }
1.165 +
1.166 +/**
1.167 +Compress the data to the buffer in stages, return value indicates if the compression has finalised
1.168 +or if further calls are necessary
1.169 +
1.170 +@leave KEZlibErrStream There is a problem with the stream
1.171 +@leave KEZlibErrBuf There is a problem with the buffer
1.172 +@leave KEZlibErrUnexpected Unexpected programming error
1.173 +@leave ... Any of the System wide error codes
1.174 +@return ETrue if the function must be called again, EFalse if compression is finalised
1.175 +*/
1.176 +EXPORT_C TBool CEZCompressor::DeflateL()
1.177 + {
1.178 + TInt err;
1.179 + TBool callAgain = ETrue;
1.180 +
1.181 + switch (iDeflationState)
1.182 + {
1.183 + case ENoFlush:
1.184 + err = deflate(&iStream,Z_NO_FLUSH);
1.185 +
1.186 + switch (err)
1.187 + {
1.188 + case Z_STREAM_ERROR:
1.189 + User::Leave(KEZlibErrStream);
1.190 + break;
1.191 +
1.192 + case Z_OK:
1.193 + if (iStream.avail_in == 0)
1.194 + iBufferInit->NeedInputL(*this);
1.195 +
1.196 + if (iStream.avail_out == 0)
1.197 + iBufferInit->NeedOutputL(*this);
1.198 + break;
1.199 +
1.200 + case Z_BUF_ERROR:
1.201 + // this is probably ok we have just run out of input.
1.202 +
1.203 + iDeflationState = EFinish;
1.204 + break;
1.205 +
1.206 + default:
1.207 +
1.208 + // there's something wrong with this code if we get here !
1.209 +
1.210 + User::Leave(KEZlibErrUnexpected);
1.211 + break;
1.212 + }
1.213 +
1.214 + break;
1.215 +
1.216 + case EFinish:
1.217 + err = deflate(&iStream,Z_FINISH);
1.218 +
1.219 + switch (err)
1.220 + {
1.221 + case Z_STREAM_ERROR:
1.222 + User::Leave(KEZlibErrStream);
1.223 + break;
1.224 +
1.225 + case Z_BUF_ERROR:
1.226 + User::Leave(KEZlibErrBuf);
1.227 + break;
1.228 +
1.229 + case Z_OK:
1.230 + if (iStream.avail_in == 0)
1.231 + iBufferInit->NeedInputL(*this);
1.232 +
1.233 + if (iStream.avail_out == 0)
1.234 + iBufferInit->NeedOutputL(*this);
1.235 + break;
1.236 +
1.237 + case Z_STREAM_END:
1.238 + iDeflationState = EFinalize;
1.239 + break;
1.240 +
1.241 + default:
1.242 + // there's something wrong with this code if we get here !
1.243 +
1.244 + User::Leave(KEZlibErrUnexpected);
1.245 + break;
1.246 + }
1.247 +
1.248 + break;
1.249 +
1.250 + case EFinalize:
1.251 + iBufferInit->FinalizeL(*this);
1.252 + callAgain = EFalse;
1.253 + iDeflationState = ETerminated;
1.254 + break;
1.255 +
1.256 + case ETerminated:
1.257 + User::Leave(KEZlibErrDeflateTerminated);
1.258 + }
1.259 +
1.260 + return callAgain;
1.261 + }
1.262 +
1.263 +void CEZCompressor::ConstructL(TInt aLevel, const TUint8 *aDictionary, TInt aLength,
1.264 + TInt aWindowBits, TInt aMemLevel, TStrategy aStrategy)
1.265 + {
1.266 + ConstructL(aLevel,aWindowBits,aMemLevel,aStrategy);
1.267 + if (deflateSetDictionary(&iStream,STATIC_CAST(const Bytef *,aDictionary),aLength) ==
1.268 + Z_STREAM_ERROR)
1.269 + User::Leave(KEZlibErrStream); // This should never happen.
1.270 + }
1.271 +
1.272 +void CEZCompressor::ConstructL(TInt aLevel, TInt aWindowBits, TInt aMemLevel, TStrategy aStrategy)
1.273 + {
1.274 + // don't need to assert the validity of aWindowBits, aMemLevel & aStrategy as deflateInit2 will
1.275 + // do this for us.
1.276 +
1.277 + iStream.zalloc = Z_NULL;
1.278 + iStream.zfree = Z_NULL;
1.279 + iStream.opaque = Z_NULL;
1.280 +
1.281 + iBufferInit->InitializeL(*this);
1.282 +
1.283 + TInt err = deflateInit2(&iStream,aLevel,Z_DEFLATED,aWindowBits,aMemLevel, STATIC_CAST(int,aStrategy));
1.284 + if (err == Z_STREAM_ERROR)
1.285 + User::Leave(KEZlibErrStream);
1.286 + else if (err == Z_MEM_ERROR)
1.287 + User::LeaveNoMemory();
1.288 +
1.289 + iDeflationState = ENoFlush;
1.290 + }
1.291 +
1.292 +/**
1.293 +Compresses the data in the given buffer
1.294 +
1.295 +@param aDestination the target buffer for the compressed data
1.296 +@param aSource the buffer containing the data to be compressed
1.297 +@param aLevel the level of compression
1.298 +@leave KEZLibErrBuf There is a problem with the buffer
1.299 +@leave KEZLIbErrStream There is a problem with the stream
1.300 +@leave ... Any of the system wide error codes
1.301 +*/
1.302 +EXPORT_C void CEZCompressor::CompressL(TDes8 &aDestination, const TDesC8 &aSource,
1.303 + TInt aLevel)
1.304 + {
1.305 + Bytef* destinationBuffer = STATIC_CAST(Bytef* ,CONST_CAST(TUint8* ,aDestination.Ptr()));
1.306 + const Bytef* sourceBuffer = STATIC_CAST(const Bytef* ,aSource.Ptr());
1.307 + uLongf dl = aDestination.MaxSize();
1.308 + TInt err = compress2(destinationBuffer,&dl,sourceBuffer,aSource.Size(),aLevel);
1.309 +
1.310 + if (err == Z_MEM_ERROR)
1.311 + User::LeaveNoMemory();
1.312 + else if (err == Z_BUF_ERROR)
1.313 + User::Leave(KEZlibErrBuf);
1.314 + else if (err == Z_STREAM_ERROR)
1.315 + User::Leave(KEZlibErrStream);
1.316 +
1.317 + aDestination.SetLength(dl);
1.318 + }