|
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 // |
|
15 |
|
16 #include "OldEZCompressor.h" |
|
17 |
|
18 using namespace TOLDEZLIB; |
|
19 |
|
20 CEZCompressor::CEZCompressor(MEZBufferManager* aInit) : |
|
21 iBufferInit(aInit) |
|
22 { |
|
23 |
|
24 } |
|
25 |
|
26 CEZCompressor::~CEZCompressor() |
|
27 { |
|
28 // Note that deflateEnd may have already been called by zlib if for example and alloc failure |
|
29 // occurred in deflateInit2. However there is no harm in calling deflateEnd twice. |
|
30 |
|
31 deflateEnd(&iStream); |
|
32 } |
|
33 |
|
34 /** |
|
35 Creates a new CEZCompressor object and leaves it on the CleanupStack |
|
36 |
|
37 @param aInit buffer manager to handle both input and output buffers |
|
38 @param aLevel compression levels |
|
39 @param aWindowBits the base two logarithm of the window size (the size of the history buffer). It should |
|
40 be in the range 8..15 for this version of the library. Larger values of this parameter result in better |
|
41 compression at the expense of memory usage. |
|
42 @param aMemLevel specifies how much memory should be allocated for the internal compression state. |
|
43 memLevel=1 uses minimum memory but is slow and reduces compression ratio; memLevel=9 uses maximum memory |
|
44 for optimal speed. |
|
45 @param aStrategy compression strategy - used to tune the compression algorithm. The strategy parameter only affects |
|
46 the compression ratio but not the correctness of the compressed output even if it is not set appropriately |
|
47 @see TStrategy |
|
48 @return the new CEZCompressor object (on the CleanupStack) |
|
49 */ |
|
50 EXPORT_C CEZCompressor *CEZCompressor::NewLC(MEZBufferManager& aInit, TInt aLevel, TInt aWindowBits, |
|
51 TInt aMemLevel, TStrategy aStrategy) |
|
52 { |
|
53 CEZCompressor* deflater = new (ELeave) CEZCompressor(&aInit); |
|
54 CleanupStack::PushL(deflater); |
|
55 deflater->ConstructL(aLevel,aWindowBits,aMemLevel,aStrategy); |
|
56 return deflater; |
|
57 } |
|
58 |
|
59 /** |
|
60 Creates a new CEZCompressor object |
|
61 |
|
62 @param aInit buffer manager to handle both input and output buffers |
|
63 @param aLevel compression levels |
|
64 @param aWindowBits the base two logarithm of the window size (the size of the history buffer). It should |
|
65 be in the range 8..15 for this version of the library. Larger values of this parameter result in better |
|
66 compression at the expense of memory usage. |
|
67 @param aMemLevel specifies how much memory should be allocated for the internal compression state. |
|
68 memLevel=1 uses minimum memory but is slow and reduces compression ratio; memLevel=9 uses maximum memory |
|
69 for optimal speed. |
|
70 @param aStrategy compression strategy - used to tune the compression algorithm. The strategy parameter only affects |
|
71 the compression ratio but not the correctness of the compressed output even if it is not set appropriately |
|
72 @see TStrategy |
|
73 @return the new CEZCompressor object |
|
74 */ |
|
75 EXPORT_C CEZCompressor* CEZCompressor::NewL(MEZBufferManager& aInit, TInt aLevel, TInt aWindowBits, |
|
76 TInt aMemLevel, TStrategy aStrategy) |
|
77 { |
|
78 CEZCompressor* deflater = new (ELeave) CEZCompressor(&aInit); |
|
79 CleanupStack::PushL(deflater); |
|
80 deflater->ConstructL(aLevel,aWindowBits,aMemLevel,aStrategy); |
|
81 CleanupStack::Pop(); |
|
82 return deflater; |
|
83 } |
|
84 |
|
85 /** |
|
86 Overload of CEZCompressor constructor takes aDictionary argument |
|
87 |
|
88 @param aInit buffer manager to handle both input and output buffers |
|
89 @param aDictionary used to initialize the compression dictionary from the given byte sequence |
|
90 without producing any compressed output. The compressor and decompressor must use exactly the same dictionary. |
|
91 The dictionary should consist of strings (byte sequences) that are likely to be encountered later in the data to be compressed, |
|
92 with the most commonly used strings preferably put towards the end of the dictionary. Using a dictionary is most useful |
|
93 when the data to be compressed is short and can be predicted with good accuracy; the data can then be compressed better than |
|
94 with the default empty dictionary. |
|
95 @param aLevel compression level |
|
96 @param aWindowBits the base two logarithm of the window size (the size of the history buffer). It should |
|
97 be in the range 8..15 for this version of the library. Larger values of this parameter result in better |
|
98 compression at the expense of memory usage. |
|
99 @param aMemLevel specifies how much memory should be allocated for the internal compression state. |
|
100 memLevel=1 uses minimum memory but is slow and reduces compression ratio; memLevel=9 uses maximum memory |
|
101 for optimal speed. |
|
102 @param aStrategy compression strategy - used to tune the compression algorithm. The strategy parameter only affects |
|
103 the compression ratio but not the correctness of the compressed output even if it is not set appropriately |
|
104 @see TStrategy |
|
105 @return the new CEZCompressor object (on the CleanupStack) |
|
106 */ |
|
107 EXPORT_C CEZCompressor* CEZCompressor::NewLC(MEZBufferManager& aInit, const TDesC8& aDictionary, |
|
108 TInt aLevel, TInt aWindowBits, TInt aMemLevel, TStrategy aStrategy) |
|
109 { |
|
110 CEZCompressor* deflater = new (ELeave) CEZCompressor(&aInit); |
|
111 CleanupStack::PushL(deflater); |
|
112 deflater->ConstructL(aLevel,aDictionary.Ptr(),aDictionary.Size(),aWindowBits,aMemLevel,aStrategy); |
|
113 return deflater; |
|
114 } |
|
115 |
|
116 /** |
|
117 Overload of CEZCompressor constructor takes aDictionary argument |
|
118 |
|
119 @param aInit buffer manager to handle both input and output buffers |
|
120 @param aDictionary used to initialize the compression dictionary from the given byte sequence |
|
121 without producing any compressed output. The compressor and decompressor must use exactly the same dictionary. |
|
122 The dictionary should consist of strings (byte sequences) that are likely to be encountered later in the data to be compressed, |
|
123 with the most commonly used strings preferably put towards the end of the dictionary. Using a dictionary is most useful |
|
124 when the data to be compressed is short and can be predicted with good accuracy; the data can then be compressed better than |
|
125 with the default empty dictionary. |
|
126 @param aLevel compression level |
|
127 @param aWindowBits the base two logarithm of the window size (the size of the history buffer). It should |
|
128 be in the range 8..15 for this version of the library. Larger values of this parameter result in better |
|
129 compression at the expense of memory usage. |
|
130 @param aMemLevel specifies how much memory should be allocated for the internal compression state. |
|
131 memLevel=1 uses minimum memory but is slow and reduces compression ratio; memLevel=9 uses maximum memory |
|
132 for optimal speed. |
|
133 @param aStrategy compression strategy - used to tune the compression algorithm. The strategy parameter only affects |
|
134 the compression ratio but not the correctness of the compressed output even if it is not set appropriately |
|
135 @see TStrategy |
|
136 @return the new CEZCompressor object |
|
137 */ |
|
138 EXPORT_C CEZCompressor* CEZCompressor::NewL(MEZBufferManager& aInit, const TDesC8& aDictionary, |
|
139 TInt aLevel, TInt aWindowBits, TInt aMemLevel, TStrategy aStrategy) |
|
140 { |
|
141 CEZCompressor* deflater = new (ELeave) CEZCompressor(&aInit); |
|
142 CleanupStack::PushL(deflater); |
|
143 deflater->ConstructL(aLevel,aDictionary.Ptr(),aDictionary.Size(),aWindowBits,aMemLevel,aStrategy); |
|
144 CleanupStack::Pop(); |
|
145 return deflater; |
|
146 } |
|
147 |
|
148 /** |
|
149 Resets the current compression operation, with the new buffer manager |
|
150 |
|
151 @param aInit new buffer manager to handle the new input and output buffers |
|
152 @leave ... Any of the system wide error codes |
|
153 */ |
|
154 EXPORT_C void CEZCompressor::ResetL(MEZBufferManager& aInit) |
|
155 { |
|
156 iBufferInit = &aInit; |
|
157 iBufferInit->InitializeL(*this); |
|
158 if (deflateReset(&iStream) == Z_STREAM_ERROR) |
|
159 User::Leave(KEZlibErrStream); |
|
160 iDeflationState = ENoFlush; |
|
161 } |
|
162 |
|
163 /** |
|
164 Compress the data to the buffer in stages, return value indicates if the compression has finalised |
|
165 or if further calls are necessary |
|
166 |
|
167 @leave KEZlibErrStream There is a problem with the stream |
|
168 @leave KEZlibErrBuf There is a problem with the buffer |
|
169 @leave KEZlibErrUnexpected Unexpected programming error |
|
170 @leave ... Any of the System wide error codes |
|
171 @return ETrue if the function must be called again, EFalse if compression is finalised |
|
172 */ |
|
173 EXPORT_C TBool CEZCompressor::DeflateL() |
|
174 { |
|
175 TInt err; |
|
176 TBool callAgain = ETrue; |
|
177 |
|
178 switch (iDeflationState) |
|
179 { |
|
180 case ENoFlush: |
|
181 err = deflate(&iStream,Z_NO_FLUSH); |
|
182 |
|
183 switch (err) |
|
184 { |
|
185 case Z_STREAM_ERROR: |
|
186 User::Leave(KEZlibErrStream); |
|
187 break; |
|
188 |
|
189 case Z_OK: |
|
190 if (iStream.avail_in == 0) |
|
191 iBufferInit->NeedInputL(*this); |
|
192 |
|
193 if (iStream.avail_out == 0) |
|
194 iBufferInit->NeedOutputL(*this); |
|
195 break; |
|
196 |
|
197 case Z_BUF_ERROR: |
|
198 // this is probably ok we have just run out of input. |
|
199 |
|
200 iDeflationState = EFinish; |
|
201 break; |
|
202 |
|
203 default: |
|
204 |
|
205 // there's something wrong with this code if we get here ! |
|
206 |
|
207 User::Leave(KEZlibErrUnexpected); |
|
208 break; |
|
209 } |
|
210 |
|
211 break; |
|
212 |
|
213 case EFinish: |
|
214 err = deflate(&iStream,Z_FINISH); |
|
215 |
|
216 switch (err) |
|
217 { |
|
218 case Z_STREAM_ERROR: |
|
219 User::Leave(KEZlibErrStream); |
|
220 break; |
|
221 |
|
222 case Z_BUF_ERROR: |
|
223 User::Leave(KEZlibErrBuf); |
|
224 break; |
|
225 |
|
226 case Z_OK: |
|
227 if (iStream.avail_in == 0) |
|
228 iBufferInit->NeedInputL(*this); |
|
229 |
|
230 if (iStream.avail_out == 0) |
|
231 iBufferInit->NeedOutputL(*this); |
|
232 break; |
|
233 |
|
234 case Z_STREAM_END: |
|
235 iDeflationState = EFinalize; |
|
236 break; |
|
237 |
|
238 default: |
|
239 // there's something wrong with this code if we get here ! |
|
240 |
|
241 User::Leave(KEZlibErrUnexpected); |
|
242 break; |
|
243 } |
|
244 |
|
245 break; |
|
246 |
|
247 case EFinalize: |
|
248 iBufferInit->FinalizeL(*this); |
|
249 callAgain = EFalse; |
|
250 iDeflationState = ETerminated; |
|
251 break; |
|
252 |
|
253 case ETerminated: |
|
254 User::Leave(KEZlibErrDeflateTerminated); |
|
255 } |
|
256 |
|
257 return callAgain; |
|
258 } |
|
259 |
|
260 void CEZCompressor::ConstructL(TInt aLevel, const TUint8 *aDictionary, TInt aLength, |
|
261 TInt aWindowBits, TInt aMemLevel, TStrategy aStrategy) |
|
262 { |
|
263 ConstructL(aLevel,aWindowBits,aMemLevel,aStrategy); |
|
264 if (deflateSetDictionary(&iStream,STATIC_CAST(const Bytef *,aDictionary),aLength) == |
|
265 Z_STREAM_ERROR) |
|
266 User::Leave(KEZlibErrStream); // This should never happen. |
|
267 } |
|
268 |
|
269 void CEZCompressor::ConstructL(TInt aLevel, TInt aWindowBits, TInt aMemLevel, TStrategy aStrategy) |
|
270 { |
|
271 // don't need to assert the validity of aWindowBits, aMemLevel & aStrategy as deflateInit2 will |
|
272 // do this for us. |
|
273 |
|
274 iStream.zalloc = Z_NULL; |
|
275 iStream.zfree = Z_NULL; |
|
276 iStream.opaque = Z_NULL; |
|
277 |
|
278 iBufferInit->InitializeL(*this); |
|
279 |
|
280 TInt err = deflateInit2(&iStream,aLevel,Z_DEFLATED,aWindowBits,aMemLevel, STATIC_CAST(int,aStrategy)); |
|
281 if (err == Z_STREAM_ERROR) |
|
282 User::Leave(KEZlibErrStream); |
|
283 else if (err == Z_MEM_ERROR) |
|
284 User::LeaveNoMemory(); |
|
285 |
|
286 iDeflationState = ENoFlush; |
|
287 } |
|
288 |
|
289 /** |
|
290 Compresses the data in the given buffer |
|
291 |
|
292 @param aDestination the target buffer for the compressed data |
|
293 @param aSource the buffer containing the data to be compressed |
|
294 @param aLevel the level of compression |
|
295 @leave KEZLibErrBuf There is a problem with the buffer |
|
296 @leave KEZLIbErrStream There is a problem with the stream |
|
297 @leave ... Any of the system wide error codes |
|
298 */ |
|
299 EXPORT_C void CEZCompressor::CompressL(TDes8 &aDestination, const TDesC8 &aSource, |
|
300 TInt aLevel) |
|
301 { |
|
302 Bytef* destinationBuffer = STATIC_CAST(Bytef* ,CONST_CAST(TUint8* ,aDestination.Ptr())); |
|
303 const Bytef* sourceBuffer = STATIC_CAST(const Bytef* ,aSource.Ptr()); |
|
304 uLongf dl = aDestination.MaxSize(); |
|
305 TInt err = compress2(destinationBuffer,&dl,sourceBuffer,aSource.Size(),aLevel); |
|
306 |
|
307 if (err == Z_MEM_ERROR) |
|
308 User::LeaveNoMemory(); |
|
309 else if (err == Z_BUF_ERROR) |
|
310 User::Leave(KEZlibErrBuf); |
|
311 else if (err == Z_STREAM_ERROR) |
|
312 User::Leave(KEZlibErrStream); |
|
313 |
|
314 aDestination.SetLength(dl); |
|
315 } |