|
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 // Name : SigComp.cpp |
|
15 // Part of : SigComp |
|
16 // SigComp API frontend |
|
17 // Version : 1.0 |
|
18 // |
|
19 |
|
20 |
|
21 |
|
22 // INCLUDE FILES |
|
23 #include "Sigcomp.h" |
|
24 #include "sigcompstateitem.h" |
|
25 #include "Udvm.h" |
|
26 #include "StateMgr.h" |
|
27 #include "Compressor.h" |
|
28 #include "MessageReader.h" |
|
29 |
|
30 // ============================ MEMBER FUNCTIONS ============================== |
|
31 |
|
32 CSigComp::CSigComp() |
|
33 { |
|
34 } |
|
35 |
|
36 void CSigComp::ConstructL(TMemorySize aDecompressionMemSize, |
|
37 TCyclesPerBit aCyclesPerBit, |
|
38 TMemorySize aStateMemorySize, |
|
39 TBool aUploadState) |
|
40 { |
|
41 |
|
42 if (aDecompressionMemSize == E0) |
|
43 { |
|
44 User::Leave(KErrArgument); |
|
45 } |
|
46 |
|
47 iMemSize = aDecompressionMemSize; |
|
48 iCyclesPerBit = aCyclesPerBit; |
|
49 iStateMemorySize = aStateMemorySize; |
|
50 iUploadState = aUploadState; |
|
51 |
|
52 iStateMgr = CStateMgr::NewL(); |
|
53 } |
|
54 |
|
55 void CSigComp::ConstructL(TMemorySize aDecompressionMemSize, |
|
56 TCyclesPerBit aCyclesPerBit, |
|
57 TMemorySize aStateMemorySize, |
|
58 const TDesC8& aAlgorithm) |
|
59 { |
|
60 |
|
61 ConstructL(aDecompressionMemSize, |
|
62 aCyclesPerBit, |
|
63 aStateMemorySize, |
|
64 EFalse); |
|
65 |
|
66 iCompressor = CSigCompCompressor::NewL(aAlgorithm, iStateMgr); |
|
67 } |
|
68 |
|
69 void CSigComp::ConstructL(TMemorySize aDecompressionMemSize, |
|
70 TCyclesPerBit aCyclesPerBit, |
|
71 TMemorySize aStateMemorySize, |
|
72 const TDesC8& aAlgorithm, |
|
73 const TSigCompStateItem& aStateItem, |
|
74 TBool aUploadState) |
|
75 { |
|
76 ConstructL(aDecompressionMemSize, |
|
77 aCyclesPerBit, |
|
78 aStateMemorySize, |
|
79 aUploadState); |
|
80 |
|
81 RFs rfs; |
|
82 User::LeaveIfError(rfs.Connect()); |
|
83 CleanupClosePushL(rfs); |
|
84 |
|
85 #if !defined(__WINS__) && !defined(__WINSCW__) |
|
86 TParsePtrC parser(RProcess().FileName()); |
|
87 User::LeaveIfError(rfs.SetSessionPath(parser.Drive())); |
|
88 #endif |
|
89 |
|
90 TInt stateLen = 0; |
|
91 RFile file; |
|
92 User::LeaveIfError(file.Open(rfs, aStateItem.iStateValue, EFileRead)); |
|
93 CleanupClosePushL(file); |
|
94 |
|
95 file.Size(stateLen); |
|
96 HBufC8* valueBuf = HBufC8::NewL(stateLen); |
|
97 TPtr8 valueBufDes = valueBuf->Des(); |
|
98 file.Read(valueBufDes, stateLen); |
|
99 CleanupStack::PopAndDestroy(2); // file, rfs |
|
100 CleanupStack::PushL(valueBuf); |
|
101 |
|
102 TStateItem* stateItem = iStateMgr->CreateStateL(NULL, |
|
103 (TInt16)stateLen, |
|
104 aStateItem.iStateAddress, |
|
105 aStateItem.iStateInstruction, |
|
106 aStateItem.iMinimumAccessLength, |
|
107 valueBuf->Ptr(), 0xffff); |
|
108 |
|
109 if (aStateItem.iStateIdentifier.Size() > 0) |
|
110 { |
|
111 if (aStateItem.iStateIdentifier.Compare(TPtrC8( |
|
112 stateItem->iStateIdentifier, 20))) |
|
113 { |
|
114 User::Leave(KErrNotFound); |
|
115 } |
|
116 } |
|
117 |
|
118 iCompressor = CSigCompCompressor::NewL(aAlgorithm, |
|
119 iStateMgr, |
|
120 stateItem, |
|
121 aStateItem.iStateLength, |
|
122 aUploadState); |
|
123 CleanupStack::PopAndDestroy(valueBuf); |
|
124 } |
|
125 |
|
126 |
|
127 EXPORT_C CSigComp* CSigComp::NewL(TMemorySize aDecompressionMemSize, |
|
128 TCyclesPerBit aCyclesPerBit, |
|
129 TMemorySize aStateMemorySize, |
|
130 const TDesC8& aAlgorithm) |
|
131 { |
|
132 |
|
133 CSigComp* self = NewLC(aDecompressionMemSize, |
|
134 aCyclesPerBit, |
|
135 aStateMemorySize, |
|
136 aAlgorithm); |
|
137 CleanupStack::Pop(); |
|
138 |
|
139 return self; |
|
140 } |
|
141 |
|
142 EXPORT_C CSigComp* CSigComp::NewL(TMemorySize aDecompressionMemSize, |
|
143 TCyclesPerBit aCyclesPerBit, |
|
144 TMemorySize aStateMemorySize, |
|
145 const TDesC8& aAlgorithm, |
|
146 const TSigCompStateItem& aStateItem, |
|
147 TBool aUploadState) |
|
148 { |
|
149 |
|
150 CSigComp* self = NewLC(aDecompressionMemSize, |
|
151 aCyclesPerBit, |
|
152 aStateMemorySize, |
|
153 aAlgorithm, |
|
154 aStateItem, |
|
155 aUploadState); |
|
156 CleanupStack::Pop(); |
|
157 |
|
158 return self; |
|
159 } |
|
160 |
|
161 EXPORT_C CSigComp* CSigComp::NewLC(TMemorySize aDecompressionMemSize, |
|
162 TCyclesPerBit aCyclesPerBit, |
|
163 TMemorySize aStateMemorySize, |
|
164 const TDesC8& aAlgorithm) |
|
165 { |
|
166 |
|
167 CSigComp* self = new (ELeave)CSigComp; |
|
168 |
|
169 CleanupStack::PushL(self); |
|
170 self->ConstructL(aDecompressionMemSize, |
|
171 aCyclesPerBit, |
|
172 aStateMemorySize, |
|
173 aAlgorithm); |
|
174 |
|
175 return self; |
|
176 } |
|
177 |
|
178 EXPORT_C CSigComp* CSigComp::NewLC(TMemorySize aDecompressionMemSize, |
|
179 TCyclesPerBit aCyclesPerBit, |
|
180 TMemorySize aStateMemorySize, |
|
181 const TDesC8& aAlgorithm, |
|
182 const TSigCompStateItem& aStateItem, |
|
183 TBool aUploadState) |
|
184 { |
|
185 |
|
186 CSigComp* self = new (ELeave)CSigComp; |
|
187 |
|
188 CleanupStack::PushL(self); |
|
189 self->ConstructL(aDecompressionMemSize, |
|
190 aCyclesPerBit, |
|
191 aStateMemorySize, |
|
192 aAlgorithm, |
|
193 aStateItem, |
|
194 aUploadState); |
|
195 |
|
196 return self; |
|
197 } |
|
198 |
|
199 |
|
200 // Destructor |
|
201 EXPORT_C CSigComp::~CSigComp() |
|
202 { |
|
203 |
|
204 delete iCompressor; |
|
205 delete iStateMgr; |
|
206 delete iUdvm; |
|
207 } |
|
208 |
|
209 |
|
210 // ---------------------------------------------------------------------------- |
|
211 // CSigComp::DecompressL |
|
212 // Decompress message |
|
213 // ---------------------------------------------------------------------------- |
|
214 // |
|
215 |
|
216 EXPORT_C CBufBase* CSigComp::DecompressL(const TDesC8& aMessage, |
|
217 TUint& aBytesConsumed, |
|
218 TBool aStreamBasedProtocol) |
|
219 { |
|
220 delete iUdvm; |
|
221 iUdvm = NULL; |
|
222 iUdvm = CUdvm::NewL(iStateMgr, |
|
223 iMemSize, |
|
224 iCyclesPerBit); |
|
225 return iUdvm->DecompressL(aMessage, aBytesConsumed, aStreamBasedProtocol); |
|
226 } |
|
227 |
|
228 |
|
229 // ---------------------------------------------------------------------------- |
|
230 // CSigComp::AllowL |
|
231 // Allow state create/free |
|
232 // ---------------------------------------------------------------------------- |
|
233 // |
|
234 |
|
235 EXPORT_C void CSigComp::AllowL(CSigCompCompartment& aSigCompCompartment) |
|
236 { |
|
237 |
|
238 if (iUdvm) |
|
239 { |
|
240 iUdvm->AllowStateOperationsL(&aSigCompCompartment); |
|
241 delete iUdvm; |
|
242 iUdvm = NULL; |
|
243 } |
|
244 } |
|
245 |
|
246 |
|
247 // ---------------------------------------------------------------------------- |
|
248 // CSigComp::Deny |
|
249 // Deny states |
|
250 // ---------------------------------------------------------------------------- |
|
251 // |
|
252 |
|
253 EXPORT_C void CSigComp::Deny() |
|
254 { |
|
255 |
|
256 if (iUdvm) |
|
257 { |
|
258 delete iUdvm; |
|
259 iUdvm = NULL; |
|
260 } |
|
261 } |
|
262 |
|
263 |
|
264 // ---------------------------------------------------------------------------- |
|
265 // CSigComp::IsSigCompMsg |
|
266 // Is SigComp message? |
|
267 // ---------------------------------------------------------------------------- |
|
268 // |
|
269 |
|
270 EXPORT_C TBool CSigComp::IsSigCompMsg(const TDesC8& aMessage) const |
|
271 { |
|
272 |
|
273 TInt pos = 0; |
|
274 |
|
275 // skip delimiters |
|
276 |
|
277 TBool skip = ETrue; |
|
278 |
|
279 while (skip) |
|
280 { |
|
281 if (pos < aMessage.Length()) |
|
282 { |
|
283 if (aMessage[pos] == 0xff) |
|
284 { |
|
285 if ((pos+1) < aMessage.Length()) |
|
286 { |
|
287 if (aMessage[pos+1] == 0xff) |
|
288 { |
|
289 pos += 2; |
|
290 } |
|
291 else |
|
292 { |
|
293 skip = EFalse; // no more delimiters |
|
294 } |
|
295 } |
|
296 else |
|
297 { |
|
298 return EFalse; // message too short |
|
299 } |
|
300 } |
|
301 else |
|
302 { |
|
303 skip = EFalse; // no more delimiters |
|
304 } |
|
305 } |
|
306 else |
|
307 { |
|
308 return EFalse; // message too short |
|
309 } |
|
310 } |
|
311 |
|
312 // |
|
313 |
|
314 TUint byte; |
|
315 |
|
316 byte = aMessage[pos]; |
|
317 |
|
318 // if byte == 0xff then quoted bytes, but byte is still 0xff |
|
319 |
|
320 return ((byte & KSigCompHeaderMask) == KSigCompHeaderMask); |
|
321 } |
|
322 |
|
323 |
|
324 // ---------------------------------------------------------------------------- |
|
325 // CSigComp::IsCompleteL |
|
326 // Is stream based message complete? |
|
327 // ---------------------------------------------------------------------------- |
|
328 // |
|
329 |
|
330 EXPORT_C TBool CSigComp::IsCompleteL(const TDesC8& aMessage) const |
|
331 { |
|
332 |
|
333 if (aMessage.Length() > 1) |
|
334 { |
|
335 CMessageReader* msgReader = new (ELeave)CMessageReader(aMessage, |
|
336 ETrue); |
|
337 CleanupStack::PushL(msgReader); |
|
338 |
|
339 msgReader->SkipDelimiters(); |
|
340 |
|
341 TUint byte; |
|
342 TInt retval; |
|
343 |
|
344 while ((retval = msgReader->ReadByte(byte)) == KErrNone) |
|
345 { |
|
346 } |
|
347 if (retval == KErrEof) |
|
348 { |
|
349 CleanupStack::PopAndDestroy(msgReader); |
|
350 return ETrue; |
|
351 } |
|
352 else |
|
353 { |
|
354 CleanupStack::PopAndDestroy(msgReader); |
|
355 return EFalse; |
|
356 } |
|
357 } |
|
358 |
|
359 return EFalse; |
|
360 } |
|
361 |
|
362 |
|
363 // ---------------------------------------------------------------------------- |
|
364 // CSigComp::DecompressionMemorySize |
|
365 // get DMS |
|
366 // ---------------------------------------------------------------------------- |
|
367 // |
|
368 |
|
369 CSigComp::TMemorySize CSigComp::DecompressionMemorySize() const |
|
370 { |
|
371 return static_cast<CSigComp::TMemorySize>(iMemSize); |
|
372 }; |
|
373 |
|
374 |
|
375 // ---------------------------------------------------------------------------- |
|
376 // CSigComp::CyclesPerBit |
|
377 // Get CPB |
|
378 // ---------------------------------------------------------------------------- |
|
379 // |
|
380 |
|
381 CSigComp::TCyclesPerBit CSigComp::CyclesPerBit() const |
|
382 { |
|
383 return iCyclesPerBit; |
|
384 }; |
|
385 |
|
386 |
|
387 // ---------------------------------------------------------------------------- |
|
388 // CSigComp::StateMemorySize |
|
389 // Get SMS |
|
390 // ---------------------------------------------------------------------------- |
|
391 // |
|
392 |
|
393 CSigComp::TMemorySize CSigComp::StateMemorySize() const |
|
394 { |
|
395 return iStateMemorySize; |
|
396 }; |
|
397 |
|
398 |
|
399 // ---------------------------------------------------------------------------- |
|
400 // CSigComp::StateMgr |
|
401 // Get state menager |
|
402 // ---------------------------------------------------------------------------- |
|
403 // |
|
404 |
|
405 const CStateMgr* CSigComp::StateMgr() const |
|
406 { |
|
407 return iStateMgr; |
|
408 } |
|
409 |
|
410 // ---------------------------------------------------------------------------- |
|
411 // CSigComp::Compressor |
|
412 // Get compressor |
|
413 // ---------------------------------------------------------------------------- |
|
414 // |
|
415 |
|
416 const CSigCompCompressor* CSigComp::Compressor() const |
|
417 { |
|
418 return iCompressor; |
|
419 } |
|
420 |
|
421 // ---------------------------------------------------------------------------- |
|
422 // CSigComp::CyclesConsumed |
|
423 // get consumed cycles number |
|
424 // ---------------------------------------------------------------------------- |
|
425 // |
|
426 |
|
427 #if defined(SIGCOMP_DEBUG) |
|
428 |
|
429 EXPORT_C TUint CSigComp::CyclesConsumed() const |
|
430 { |
|
431 return iUdvm->CyclesConsumed(); |
|
432 } |
|
433 |
|
434 #endif |