|
1 /* |
|
2 * Copyright (c) 1998-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 * e32tools\petran\Szip\deflate.cpp |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "deflate.h" |
|
21 #include "h_utl.h" |
|
22 #include "panic.h" |
|
23 |
|
24 class HDeflateHash |
|
25 { |
|
26 public: |
|
27 inline static HDeflateHash* NewLC(TInt aLinks); |
|
28 // |
|
29 inline TInt First(const TUint8* aPtr,TInt aPos); |
|
30 inline TInt Next(TInt aPos,TInt aOffset) const; |
|
31 private: |
|
32 inline HDeflateHash(); |
|
33 inline static TInt Hash(const TUint8* aPtr); |
|
34 private: |
|
35 typedef TUint16 TOffset; |
|
36 private: |
|
37 TInt iHash[256]; |
|
38 TOffset iOffset[1]; // or more |
|
39 }; |
|
40 |
|
41 class MDeflater |
|
42 { |
|
43 public: |
|
44 void DeflateL(const TUint8* aBase,TInt aLength); |
|
45 inline virtual ~MDeflater() { }; |
|
46 private: |
|
47 const TUint8* DoDeflateL(const TUint8* aBase,const TUint8* aEnd,HDeflateHash& aHash); |
|
48 static TInt Match(const TUint8* aPtr,const TUint8* aEnd,TInt aPos,HDeflateHash& aHas); |
|
49 void SegmentL(TInt aLength,TInt aDistance); |
|
50 virtual void LitLenL(TInt aCode) =0; |
|
51 virtual void OffsetL(TInt aCode) =0; |
|
52 virtual void ExtraL(TInt aLen,TUint aBits) =0; |
|
53 }; |
|
54 |
|
55 class TDeflateStats : public MDeflater |
|
56 { |
|
57 public: |
|
58 inline TDeflateStats(TEncoding& aEncoding); |
|
59 inline virtual ~TDeflateStats() { } |
|
60 private: |
|
61 // from MDeflater |
|
62 void LitLenL(TInt aCode); |
|
63 void OffsetL(TInt aCode); |
|
64 void ExtraL(TInt aLen,TUint aBits); |
|
65 private: |
|
66 TEncoding& iEncoding; |
|
67 }; |
|
68 |
|
69 class TDeflater : public MDeflater |
|
70 { |
|
71 public: |
|
72 inline TDeflater(TBitOutput& aOutput,const TEncoding& aEncoding); |
|
73 inline virtual ~TDeflater() { }; |
|
74 private: |
|
75 // from MDeflater |
|
76 void LitLenL(TInt aCode); |
|
77 void OffsetL(TInt aCode); |
|
78 void ExtraL(TInt aLen,TUint aBits); |
|
79 private: |
|
80 TBitOutput& iOutput; |
|
81 const TEncoding& iEncoding; |
|
82 }; |
|
83 |
|
84 |
|
85 // Class HDeflateHash |
|
86 |
|
87 inline HDeflateHash::HDeflateHash() |
|
88 {TInt* p=iHash+256;do *--p=-KDeflateMaxDistance-1; while (p>iHash);} |
|
89 |
|
90 inline HDeflateHash* HDeflateHash::NewLC(TInt aLinks) |
|
91 { |
|
92 return new(HMem::Alloc(0,_FOFF(HDeflateHash,iOffset[Min(aLinks,KDeflateMaxDistance)]))) HDeflateHash; |
|
93 } |
|
94 |
|
95 inline TInt HDeflateHash::Hash(const TUint8* aPtr) |
|
96 { |
|
97 TUint x=aPtr[0]|(aPtr[1]<<8)|(aPtr[2]<<16); |
|
98 return (x*KDeflateHashMultiplier)>>KDeflateHashShift; |
|
99 } |
|
100 |
|
101 inline TInt HDeflateHash::First(const TUint8* aPtr,TInt aPos) |
|
102 { |
|
103 TInt h=Hash(aPtr); |
|
104 TInt offset=Min(aPos-iHash[h],KDeflateMaxDistance<<1); |
|
105 iHash[h]=aPos; |
|
106 iOffset[aPos&(KDeflateMaxDistance-1)]=TOffset(offset); |
|
107 return offset; |
|
108 } |
|
109 |
|
110 inline TInt HDeflateHash::Next(TInt aPos,TInt aOffset) const |
|
111 {return aOffset+iOffset[(aPos-aOffset)&(KDeflateMaxDistance-1)];} |
|
112 |
|
113 |
|
114 // Class TDeflater |
|
115 // |
|
116 // generic deflation algorithm, can do either statistics and the encoder |
|
117 |
|
118 TInt MDeflater::Match(const TUint8* aPtr,const TUint8* aEnd,TInt aPos,HDeflateHash& aHash) |
|
119 { |
|
120 TInt offset=aHash.First(aPtr,aPos); |
|
121 if (offset>KDeflateMaxDistance) |
|
122 return 0; |
|
123 TInt match=0; |
|
124 aEnd=Min(aEnd,aPtr+KDeflateMaxLength); |
|
125 TUint8 c=*aPtr; |
|
126 do |
|
127 { |
|
128 const TUint8* p=aPtr-offset; |
|
129 if (p[match>>16]==c) |
|
130 { // might be a better match |
|
131 const TUint8* m=aPtr; |
|
132 for (;;) |
|
133 { |
|
134 if (*p++!=*m++) |
|
135 break; |
|
136 if (m<aEnd) |
|
137 continue; |
|
138 return ((m-aPtr)<<16)|offset; |
|
139 } |
|
140 TInt l=m-aPtr-1; |
|
141 if (l>match>>16) |
|
142 { |
|
143 match=(l<<16)|offset; |
|
144 c=m[-1]; |
|
145 } |
|
146 } |
|
147 offset=aHash.Next(aPos,offset); |
|
148 } while (offset<=KDeflateMaxDistance); |
|
149 return match; |
|
150 } |
|
151 |
|
152 const TUint8* MDeflater::DoDeflateL(const TUint8* aBase,const TUint8* aEnd,HDeflateHash& aHash) |
|
153 // |
|
154 // Apply the deflation algorithm to the data [aBase,aEnd) |
|
155 // Return a pointer after the last byte that was deflated (which may not be aEnd) |
|
156 // |
|
157 { |
|
158 const TUint8* ptr=aBase; |
|
159 TInt prev=0; // the previous deflation match |
|
160 do |
|
161 { |
|
162 TInt match=Match(ptr,aEnd,ptr-aBase,aHash); |
|
163 // Extra deflation applies two optimisations which double the time taken |
|
164 // 1. If we have a match at p, then test for a better match at p+1 before using it |
|
165 // 2. When we have a match, add the hash links for all the data which will be skipped |
|
166 if (match>>16 < prev>>16) |
|
167 { // use the previous match--it was better |
|
168 TInt len=prev>>16; |
|
169 SegmentL(len,prev-(len<<16)); |
|
170 // fill in missing hash entries for better compression |
|
171 const TUint8* e=ptr+len-2; |
|
172 do |
|
173 { |
|
174 ++ptr; |
|
175 if (ptr + 2 < aEnd) |
|
176 aHash.First(ptr,ptr-aBase); |
|
177 } while (ptr<e); |
|
178 prev=0; |
|
179 } |
|
180 else if (match<=(KDeflateMinLength<<16)) |
|
181 LitLenL(*ptr); // no deflation match here |
|
182 else |
|
183 { // save this match and test the next position |
|
184 if (prev) // we had a match at ptr-1, but this is better |
|
185 LitLenL(ptr[-1]); |
|
186 prev=match; |
|
187 } |
|
188 ++ptr; |
|
189 } while (ptr+KDeflateMinLength-1<aEnd); |
|
190 if (prev) |
|
191 { // emit the stored match |
|
192 TInt len=prev>>16; |
|
193 SegmentL(len,prev-(len<<16)); |
|
194 ptr+=len-1; |
|
195 } |
|
196 return ptr; |
|
197 } |
|
198 |
|
199 void MDeflater::DeflateL(const TUint8* aBase,TInt aLength) |
|
200 // |
|
201 // The generic deflation algorithm |
|
202 // |
|
203 { |
|
204 const TUint8* end=aBase+aLength; |
|
205 if (aLength>KDeflateMinLength) |
|
206 { // deflation kicks in if there is enough data |
|
207 HDeflateHash* hash=HDeflateHash::NewLC(aLength); |
|
208 if(hash==NULL) |
|
209 Panic(EHuffmanOutOfMemory); |
|
210 |
|
211 aBase=DoDeflateL(aBase,end,*hash); |
|
212 delete hash; |
|
213 } |
|
214 while (aBase<end) // emit remaining bytes |
|
215 LitLenL(*aBase++); |
|
216 LitLenL(TEncoding::EEos); // eos marker |
|
217 } |
|
218 |
|
219 void MDeflater::SegmentL(TInt aLength,TInt aDistance) |
|
220 // |
|
221 // Turn a (length,offset) pair into the deflation codes+extra bits before calling |
|
222 // the specific LitLen(), Offset() and Extra() functions. |
|
223 // |
|
224 { |
|
225 aLength-=KDeflateMinLength; |
|
226 TInt extralen=0; |
|
227 TUint len=aLength; |
|
228 while (len>=8) |
|
229 { |
|
230 ++extralen; |
|
231 len>>=1; |
|
232 } |
|
233 LitLenL((extralen<<2)+len+TEncoding::ELiterals); |
|
234 if (extralen) |
|
235 ExtraL(extralen,aLength); |
|
236 // |
|
237 aDistance--; |
|
238 extralen=0; |
|
239 TUint dist=aDistance; |
|
240 while (dist>=8) |
|
241 { |
|
242 ++extralen; |
|
243 dist>>=1; |
|
244 } |
|
245 OffsetL((extralen<<2)+dist); |
|
246 if (extralen) |
|
247 ExtraL(extralen,aDistance); |
|
248 } |
|
249 |
|
250 // Class TDeflateStats |
|
251 // |
|
252 // This class analyses the data stream to generate the frequency tables |
|
253 // for the deflation algorithm |
|
254 |
|
255 inline TDeflateStats::TDeflateStats(TEncoding& aEncoding) |
|
256 :iEncoding(aEncoding) |
|
257 {} |
|
258 |
|
259 void TDeflateStats::LitLenL(TInt aCode) |
|
260 { |
|
261 ++iEncoding.iLitLen[aCode]; |
|
262 } |
|
263 |
|
264 void TDeflateStats::OffsetL(TInt aCode) |
|
265 { |
|
266 ++iEncoding.iDistance[aCode]; |
|
267 } |
|
268 |
|
269 void TDeflateStats::ExtraL(TInt,TUint) |
|
270 {} |
|
271 |
|
272 // Class TDeflater |
|
273 // |
|
274 // Extends MDeflater to provide huffman encoding of the output |
|
275 |
|
276 inline TDeflater::TDeflater(TBitOutput& aOutput,const TEncoding& aEncoding) |
|
277 // |
|
278 // construct for encoding |
|
279 // |
|
280 :iOutput(aOutput),iEncoding(aEncoding) |
|
281 {} |
|
282 |
|
283 void TDeflater::LitLenL(TInt aCode) |
|
284 { |
|
285 iOutput.HuffmanL(iEncoding.iLitLen[aCode]); |
|
286 } |
|
287 |
|
288 void TDeflater::OffsetL(TInt aCode) |
|
289 { |
|
290 iOutput.HuffmanL(iEncoding.iDistance[aCode]); |
|
291 } |
|
292 |
|
293 void TDeflater::ExtraL(TInt aLen,TUint aBits) |
|
294 { |
|
295 iOutput.WriteL(aBits,aLen); |
|
296 } |
|
297 |
|
298 void DoDeflateL(const TUint8* aBuf,TInt aLength,TBitOutput& aOutput,TEncoding& aEncoding) |
|
299 { |
|
300 // analyse the data for symbol frequency |
|
301 TDeflateStats analyser(aEncoding); |
|
302 analyser.DeflateL(aBuf,aLength); |
|
303 |
|
304 // generate the required huffman encodings |
|
305 Huffman::HuffmanL(aEncoding.iLitLen,TEncoding::ELitLens,aEncoding.iLitLen); |
|
306 Huffman::HuffmanL(aEncoding.iDistance,TEncoding::EDistances,aEncoding.iDistance); |
|
307 |
|
308 // Store the encoding table |
|
309 Huffman::ExternalizeL(aOutput,aEncoding.iLitLen,KDeflationCodes); |
|
310 |
|
311 // generate the tables |
|
312 Huffman::Encoding(aEncoding.iLitLen,TEncoding::ELitLens,aEncoding.iLitLen); |
|
313 Huffman::Encoding(aEncoding.iDistance,TEncoding::EDistances,aEncoding.iDistance); |
|
314 |
|
315 // now finally deflate the data with the generated encoding |
|
316 TDeflater deflater(aOutput,aEncoding); |
|
317 deflater.DeflateL(aBuf,aLength); |
|
318 aOutput.PadL(1); |
|
319 } |
|
320 |
|
321 void DeflateL(const TUint8* aBuf, TInt aLength, TBitOutput& aOutput) |
|
322 { |
|
323 TEncoding* encoding=new TEncoding; |
|
324 HMem::FillZ(encoding,sizeof(TEncoding)); |
|
325 DoDeflateL(aBuf,aLength,aOutput,*encoding); |
|
326 } |
|
327 |