|
1 /* |
|
2 * Copyright (c) 2002 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 * Decompression buffer manager for GZip data |
|
16 * |
|
17 * |
|
18 */ |
|
19 |
|
20 #include <EZStream.h> |
|
21 #include "GZipBufMgr.h" |
|
22 //----------------------------------------------------------------------------- |
|
23 |
|
24 // NOTE: This code was taken from the DeflateFilter (s60/DeflateFilter/) |
|
25 |
|
26 // ---------------------------------------------------------------------------- |
|
27 // StreamBufMgr::NewL |
|
28 // Two-phase constructor |
|
29 // ---------------------------------------------------------------------------- |
|
30 // |
|
31 GZipBufMgr* GZipBufMgr::NewL(TPtrC8 in) |
|
32 { |
|
33 GZipBufMgr* self = new (ELeave)GZipBufMgr(in); |
|
34 CleanupStack::PushL(self); |
|
35 self->ConstructL(); |
|
36 CleanupStack::Pop(); |
|
37 return self; |
|
38 }//lint !e1746 const reference is not necessary here, because TPtrC8 takes only 4-bytes |
|
39 |
|
40 // ---------------------------------------------------------------------------- |
|
41 // GZipBufMgr::GZipBufMgr |
|
42 // Constructor |
|
43 // ---------------------------------------------------------------------------- |
|
44 // |
|
45 GZipBufMgr::GZipBufMgr(TPtrC8 in) |
|
46 : iInput(in), iOutputDes(0, 0), iOutput(0), |
|
47 iNeedInput(EFalse), iNeedOutput(EFalse), |
|
48 iFinalized(EFalse), iOffset(0), iID1(31), iID2(139) |
|
49 { |
|
50 } //lint !e1746 |
|
51 |
|
52 // ---------------------------------------------------------------------------- |
|
53 // GZipBufMgr::ConstructL |
|
54 // Initialize the output buffer |
|
55 // ---------------------------------------------------------------------------- |
|
56 // |
|
57 void GZipBufMgr::ConstructL() |
|
58 { |
|
59 // Output buffer and descriptor |
|
60 iOutput = HBufC8::NewMaxL(1<<15); |
|
61 iOutputDes.Set(iOutput->Des()); |
|
62 |
|
63 // Setup the GZip header, this sets iOffset to after the header and start |
|
64 // of the data |
|
65 TEZGZipHeader header; |
|
66 ReadGZipHeaderL(header); |
|
67 |
|
68 // Move pass the header and set the buffer to the data |
|
69 TPtrC8 data(REINTERPRET_CAST(const TUint8*, iInput.Ptr() + iOffset), iInput.Length() - iOffset); |
|
70 |
|
71 // Setup the data buffer |
|
72 SetGZipBuffer(data); |
|
73 |
|
74 } |
|
75 |
|
76 // ---------------------------------------------------------------------------- |
|
77 // GZipBufMgr::~GZipBufMgr |
|
78 // Destructor |
|
79 // ---------------------------------------------------------------------------- |
|
80 // |
|
81 GZipBufMgr::~GZipBufMgr() |
|
82 { |
|
83 delete iOutput; |
|
84 iOutput = NULL; |
|
85 } |
|
86 |
|
87 // ---------------------------------------------------------------------------- |
|
88 // GZipBufMgr::InitializeL |
|
89 // Overriden function from MEZBufferManager |
|
90 // ---------------------------------------------------------------------------- |
|
91 // |
|
92 void GZipBufMgr::InitializeL(CEZZStream& aZStream) |
|
93 { |
|
94 // read more |
|
95 aZStream.SetInput(iInput); |
|
96 iOutputDes.SetLength(0); |
|
97 aZStream.SetOutput(iOutputDes); |
|
98 iNeedInput = EFalse; |
|
99 iNeedOutput = EFalse; |
|
100 } |
|
101 |
|
102 // ---------------------------------------------------------------------------- |
|
103 // GZipBufMgr::NeedInputL |
|
104 // Overriden function from MEZBufferManager |
|
105 // ---------------------------------------------------------------------------- |
|
106 // |
|
107 void GZipBufMgr::NeedInputL(CEZZStream& /*aZStream*/) |
|
108 { |
|
109 //aZStream.SetInput(iInput); |
|
110 iNeedInput = ETrue; |
|
111 } |
|
112 |
|
113 // ---------------------------------------------------------------------------- |
|
114 // GZipBufMgr::NeedOutputL |
|
115 // Overriden function from MEZBufferManager |
|
116 // ---------------------------------------------------------------------------- |
|
117 // |
|
118 void GZipBufMgr::NeedOutputL(CEZZStream& /*aZStream*/) |
|
119 { |
|
120 //aZStream.SetOutput(iOutputDes); |
|
121 iNeedOutput = ETrue; |
|
122 } |
|
123 |
|
124 // ---------------------------------------------------------------------------- |
|
125 // GZipBufMgr::NeedOutputL |
|
126 // Overriden function from MEZBufferManager |
|
127 // ---------------------------------------------------------------------------- |
|
128 // |
|
129 void GZipBufMgr::FinalizeL(CEZZStream& /*aZStream*/) |
|
130 { |
|
131 // do nothing now, should check the checksum value here |
|
132 // read the checksum value from the last chunk |
|
133 //const TInt32* checksum = REINTERPRET_CAST(const TInt32*, iInput.Ptr() + (iInput.Length() - sizeof(TInt32)*2)); |
|
134 iFinalized = ETrue; |
|
135 } |
|
136 |
|
137 |
|
138 // ---------------------------------------------------------------------------- |
|
139 // Public methods |
|
140 // ---------------------------------------------------------------------------- |
|
141 |
|
142 // ---------------------------------------------------------------------------- |
|
143 // GZipBufMgr::ReadGZipHeaderL |
|
144 // Read the header of Gzip-ed stream in Gzip format, RFC1952. |
|
145 // Sets the iOffset pointer, which marks the end of the header and start of the |
|
146 // data. This method needs to be called (or iOffset set) before calling |
|
147 // SetGZipBuffer method. |
|
148 // ---------------------------------------------------------------------------- |
|
149 // |
|
150 void GZipBufMgr::ReadGZipHeaderL(TEZGZipHeader& aHeader) |
|
151 { |
|
152 // construct a stream for reading |
|
153 RMemReadStream memBuf; |
|
154 memBuf.Open(iInput.Ptr(), iInput.Length()); |
|
155 |
|
156 TInt obligatoryData = sizeof(aHeader.iId1) + sizeof(aHeader.iId2) + |
|
157 sizeof(aHeader.iCompressionMethod) + |
|
158 sizeof(aHeader.iFlags) + sizeof(aHeader.iTime) + |
|
159 sizeof(aHeader.iExtraFlags) + sizeof(aHeader.iOs); |
|
160 |
|
161 // copy the header data |
|
162 TPtr8 des(&aHeader.iId1, 0, obligatoryData); |
|
163 TRAPD(error, memBuf.ReadL(des)); |
|
164 |
|
165 if (error == KErrEof) |
|
166 { |
|
167 User::Leave(EZGZipFile::EBadGZipHeader); |
|
168 } |
|
169 |
|
170 if (des.Size() < obligatoryData) |
|
171 { |
|
172 // TODO: should consider the situation when not enough data has arrived |
|
173 User::Leave(EZGZipFile::EBadGZipHeader); |
|
174 } |
|
175 |
|
176 if (aHeader.iId1 != iID1 || aHeader.iId2 != iID2) |
|
177 { |
|
178 User::Leave(EZGZipFile::EBadGZipHeader); |
|
179 } |
|
180 |
|
181 if (aHeader.iFlags & (1 << EZGZipFile::EFExtra)) |
|
182 { |
|
183 // The extra bit is set |
|
184 des.Set(REINTERPRET_CAST(TUint8 *, &aHeader.iXlen), 0, sizeof(aHeader.iXlen)); |
|
185 memBuf.ReadL(des); |
|
186 if (des.Size() != sizeof(aHeader.iXlen) || aHeader.iXlen < 0) //lint !e737 sign/unsigned doesn't realy matter here |
|
187 { |
|
188 User::Leave(EZGZipFile::EBadGZipHeader); |
|
189 } |
|
190 |
|
191 aHeader.iExtra = HBufC8::NewMaxL(aHeader.iXlen); |
|
192 TPtr8 des1 = aHeader.iExtra->Des(); |
|
193 memBuf.ReadL(des1); |
|
194 |
|
195 if (des1.Size() != aHeader.iXlen) |
|
196 { |
|
197 User::Leave(EZGZipFile::EBadGZipHeader); |
|
198 } |
|
199 } |
|
200 |
|
201 if (aHeader.iFlags & (1 << EZGZipFile::EFName)) |
|
202 { |
|
203 // Read in filename |
|
204 ReadStringIntoDescriptorL(memBuf, &aHeader.iFname); |
|
205 } |
|
206 |
|
207 if (aHeader.iFlags & (1 << EZGZipFile::EFComment)) |
|
208 { |
|
209 // Read in comment |
|
210 ReadStringIntoDescriptorL(memBuf, &aHeader.iComment); |
|
211 } |
|
212 |
|
213 if (aHeader.iFlags & (1 << EZGZipFile::EFHcrc)) |
|
214 { |
|
215 des.Set(REINTERPRET_CAST(TUint8*, &aHeader.iCrc), 0, sizeof(aHeader.iCrc)); |
|
216 memBuf.ReadL(des); |
|
217 if (des.Size() != sizeof(aHeader.iCrc)) //lint !e737 unsigned/signed doesn't matter |
|
218 { |
|
219 User::Leave(EZGZipFile::EBadGZipHeader); |
|
220 } |
|
221 } |
|
222 |
|
223 // Set iOffset to mark end of header and beginning of data |
|
224 iOffset = memBuf.Source()->TellL(MStreamBuf::ERead).Offset(); |
|
225 |
|
226 memBuf.Close(); |
|
227 } |
|
228 |
|
229 // ---------------------------------------------------------------------------- |
|
230 // GZipBufMgr::ReadStringIntoDescriptorL |
|
231 // Read null-terminated strings into the buffer |
|
232 // ---------------------------------------------------------------------------- |
|
233 // |
|
234 void GZipBufMgr::ReadStringIntoDescriptorL(RMemReadStream& aMem, HBufC8 **aDes) const |
|
235 { |
|
236 TInt i; |
|
237 CArrayFixFlat<TUint8> *bytes = new (ELeave) CArrayFixFlat<TUint8>(16); |
|
238 CleanupStack::PushL(bytes); |
|
239 TUint8 ch; |
|
240 |
|
241 while ((ch = aMem.ReadUint8L()) != '\0') |
|
242 { |
|
243 bytes->AppendL(ch); |
|
244 } |
|
245 |
|
246 *aDes = HBufC8::NewMaxL(bytes->Count()); |
|
247 TPtr8 desPtr = (*aDes)->Des(); //lint because error #111 - separate desPtr used |
|
248 |
|
249 for (i = 0; i < bytes->Count(); i++) |
|
250 { |
|
251 desPtr[i] = (*bytes)[i]; //lint !e1058 force conversion here |
|
252 } |
|
253 |
|
254 CleanupStack::PopAndDestroy(); // delete bytes |
|
255 } |
|
256 |
|
257 // ---------------------------------------------------------------------------- |
|
258 // GZipBufMgr::SetBuffer |
|
259 // Set the input buffer |
|
260 // ---------------------------------------------------------------------------- |
|
261 // |
|
262 void GZipBufMgr::SetGZipBuffer(TPtrC8 data) |
|
263 { |
|
264 iInput.Set(data); |
|
265 }//lint !e1746 |