|
1 /* |
|
2 * Copyright (c) 2000 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 "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: Implementation of terminalsecurity components |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <e32debug.h> |
|
19 #include <f32file.h> |
|
20 |
|
21 #include "SCPParamObject.h" |
|
22 #include "SCPClient.h" |
|
23 |
|
24 #include "SCPDebug.h" |
|
25 /*#ifdef _DEBUG |
|
26 #define __SCP_DEBUG |
|
27 #endif // _DEBUG |
|
28 |
|
29 // Define this so the precompiler in CW 3.1 won't complain about token pasting, |
|
30 // the warnings are not valid |
|
31 #pragma warn_illtokenpasting off |
|
32 |
|
33 #ifdef __SCP_DEBUG |
|
34 #define Dprint(a) RDebug::Print##a |
|
35 #else |
|
36 #define Dprint(a) |
|
37 #endif // _DEBUG*/ |
|
38 |
|
39 // ================= MEMBER FUNCTIONS ======================= |
|
40 // |
|
41 // ---------------------------------------------------------- |
|
42 // CSCPParamObject::CSCPParamObject() |
|
43 // C++ constructor |
|
44 // |
|
45 // Status: Approved |
|
46 // ---------------------------------------------------------- |
|
47 CSCPParamObject::CSCPParamObject() |
|
48 { |
|
49 // No implementation required |
|
50 } |
|
51 |
|
52 |
|
53 // ---------------------------------------------------------- |
|
54 // CSCPParamObject::NewL() |
|
55 // Static constructor |
|
56 // |
|
57 // Status: Approved |
|
58 // ---------------------------------------------------------- |
|
59 EXPORT_C CSCPParamObject* CSCPParamObject::NewL() |
|
60 { |
|
61 CSCPParamObject* self = CSCPParamObject::NewLC(); |
|
62 CleanupStack::Pop( self ); |
|
63 |
|
64 return self; |
|
65 } |
|
66 |
|
67 |
|
68 // ---------------------------------------------------------- |
|
69 // CSCPParamObject::NewLC() |
|
70 // Static constructor, leaves object pointer to the cleanup stack. |
|
71 // |
|
72 // Status: Approved |
|
73 // ---------------------------------------------------------- |
|
74 EXPORT_C CSCPParamObject* CSCPParamObject::NewLC() |
|
75 { |
|
76 Dprint( (_L("--> CSCPParamObject::NewLC()")) ); |
|
77 |
|
78 CSCPParamObject* self = new (ELeave) CSCPParamObject(); |
|
79 |
|
80 CleanupStack::PushL( self ); |
|
81 self->ConstructL(); |
|
82 |
|
83 Dprint( (_L("<-- CSCPParamObject::NewLC()") )); |
|
84 return self; |
|
85 } |
|
86 |
|
87 // ---------------------------------------------------------- |
|
88 // CSCPParamObject::ConstructL() |
|
89 // Symbian 2nd phase constructor |
|
90 // |
|
91 // Status: Approved |
|
92 // ---------------------------------------------------------- |
|
93 void CSCPParamObject::ConstructL() |
|
94 { |
|
95 iParamIDs.Reset(); |
|
96 iParamValues.Reset(); |
|
97 } |
|
98 |
|
99 |
|
100 // ---------------------------------------------------------- |
|
101 // CSCPParamObject::GetParamLength() |
|
102 // Return the length of the given parameter. |
|
103 // |
|
104 // Status: Approved |
|
105 // ---------------------------------------------------------- |
|
106 EXPORT_C TInt CSCPParamObject::GetParamLength( TInt aParamID ) |
|
107 { |
|
108 TInt ret = iParamIDs.Find( aParamID ); |
|
109 if ( ( ret != KErrNotFound ) && ( ret >= 0 ) ) |
|
110 { |
|
111 ret = ( iParamValues[ret]->Des() ).Length(); |
|
112 } |
|
113 |
|
114 return ret; |
|
115 } |
|
116 |
|
117 |
|
118 // ---------------------------------------------------------- |
|
119 // CSCPParamObject::Get() |
|
120 // Retrieve the given value from the arrays |
|
121 // |
|
122 // Status: Approved |
|
123 // ---------------------------------------------------------- |
|
124 EXPORT_C TInt CSCPParamObject::Get( TInt aParamID, TDes& aValue ) |
|
125 { |
|
126 TInt ret = KErrNone; |
|
127 |
|
128 // Find the object |
|
129 TInt index = iParamIDs.Find( aParamID ); |
|
130 if ( ( index != KErrNotFound ) && ( index >= 0 ) ) |
|
131 { |
|
132 // Retrieve the value |
|
133 if ( aValue.MaxLength() < ( iParamValues[index]->Des() ).Length() ) |
|
134 { |
|
135 ret = KErrOverflow; |
|
136 } |
|
137 else |
|
138 { |
|
139 aValue.Copy( iParamValues[index]->Des() ); |
|
140 } |
|
141 } |
|
142 else |
|
143 { |
|
144 ret = index; |
|
145 } |
|
146 |
|
147 return ret; |
|
148 } |
|
149 |
|
150 // ---------------------------------------------------------- |
|
151 // CSCPParamObject::Get() |
|
152 // Retrieve the given value from the arrays, trying to |
|
153 // convert it to an integer. |
|
154 // |
|
155 // Status: Approved |
|
156 // ---------------------------------------------------------- |
|
157 EXPORT_C TInt CSCPParamObject::Get( TInt aParamID, TInt& aValue ) |
|
158 { |
|
159 TBuf<KSCPMaxIntLength> convBuf; |
|
160 convBuf.Zero(); |
|
161 |
|
162 // An integer should fit into 16chars, if not, it's an error |
|
163 TInt ret = Get( aParamID, convBuf ); |
|
164 |
|
165 if ( ret == KErrNone ) |
|
166 { |
|
167 TLex lex( convBuf ); |
|
168 ret = lex.Val( aValue ); |
|
169 } |
|
170 |
|
171 return ret; |
|
172 } |
|
173 |
|
174 |
|
175 |
|
176 // ---------------------------------------------------------- |
|
177 // CSCPParamObject::Set() |
|
178 // Insert or replace the parameter value of aParamID with |
|
179 // aValue. |
|
180 // |
|
181 // Status: Approved |
|
182 // ---------------------------------------------------------- |
|
183 EXPORT_C TInt CSCPParamObject::Set( TInt aParamID, TDes& aValue ) |
|
184 { |
|
185 TBool isNewParam = ETrue; |
|
186 TInt ret = KErrNone; |
|
187 |
|
188 // Create a buffer for the value |
|
189 HBufC* newValue = NULL; |
|
190 TRAPD( err, newValue = HBufC::NewL( aValue.Length() ) ); |
|
191 |
|
192 if ( err != KErrNone ) |
|
193 { |
|
194 return err; |
|
195 } |
|
196 |
|
197 TPtr newValPtr = newValue->Des(); |
|
198 newValPtr.Copy( aValue ); |
|
199 |
|
200 if ( err != KErrNone ) |
|
201 { |
|
202 Dprint( (_L("CSCPParamObject::Set(): ERROR: \ |
|
203 HBufC Allocation failed") )); |
|
204 ret = err; |
|
205 } |
|
206 |
|
207 // Check if we've got this ID already |
|
208 for ( TInt i = 0; i < iParamIDs.Count(); i++ ) |
|
209 { |
|
210 if ( iParamIDs[i] == aParamID ) |
|
211 { |
|
212 // Remove the old entry |
|
213 HBufC* tmpValue = iParamValues[ i ]; |
|
214 iParamValues.Remove( i ); |
|
215 delete tmpValue; |
|
216 |
|
217 iParamValues.Insert( newValue, i ); |
|
218 isNewParam = EFalse; |
|
219 } |
|
220 } |
|
221 |
|
222 if ( isNewParam ) |
|
223 { |
|
224 // Create a new value |
|
225 iParamIDs.Append( aParamID ); |
|
226 iParamValues.Append( newValue ); |
|
227 } |
|
228 |
|
229 return ret; |
|
230 } |
|
231 |
|
232 // ---------------------------------------------------------- |
|
233 // CSCPParamObject::Set() |
|
234 // Convert the int into a descriptor and propagate to Set. |
|
235 // |
|
236 // Status: Approved |
|
237 // ---------------------------------------------------------- |
|
238 EXPORT_C TInt CSCPParamObject::Set( TInt aParamID, TInt aValue ) |
|
239 { |
|
240 // Convert the value |
|
241 TBuf<KSCPMaxIntLength> convBuf; |
|
242 convBuf.Zero(); |
|
243 convBuf.AppendNum( aValue ); |
|
244 |
|
245 return Set( aParamID, convBuf ); |
|
246 } |
|
247 |
|
248 |
|
249 // ---------------------------------------------------------- |
|
250 // CSCPParamObject::Unset() |
|
251 // Remove the given ID along with its value from the arrays. |
|
252 // |
|
253 // Status: Approved |
|
254 // ---------------------------------------------------------- |
|
255 EXPORT_C TInt CSCPParamObject::Unset( TInt aParamID ) |
|
256 { |
|
257 TInt ret = KErrNotFound; |
|
258 |
|
259 // Check if we've got this ID |
|
260 for ( TInt i = 0; i < iParamIDs.Count(); i++ ) |
|
261 { |
|
262 if ( iParamIDs[i] == aParamID ) |
|
263 { |
|
264 // Remove the entry |
|
265 HBufC* tmpValue = iParamValues[ i ]; |
|
266 iParamValues.Remove( i ); |
|
267 delete tmpValue; |
|
268 |
|
269 iParamIDs.Remove( i ); |
|
270 ret = KErrNone; |
|
271 } |
|
272 } |
|
273 |
|
274 return ret; |
|
275 } |
|
276 |
|
277 |
|
278 // ---------------------------------------------------------- |
|
279 // CSCPParamObject::Reset() |
|
280 // Reset the ID and value arrays. |
|
281 // |
|
282 // Status: Approved |
|
283 // ---------------------------------------------------------- |
|
284 EXPORT_C TInt CSCPParamObject::Reset() |
|
285 { |
|
286 iParamIDs.Reset(); |
|
287 iParamValues.ResetAndDestroy(); |
|
288 |
|
289 return KErrNone; |
|
290 } |
|
291 |
|
292 |
|
293 // ---------------------------------------------------------- |
|
294 // CSCPParamObject::Parse() |
|
295 // Parse a previously compiled buffer, and add the parameters |
|
296 // found to the arrays. |
|
297 // |
|
298 // Status: Approved |
|
299 // ---------------------------------------------------------- |
|
300 EXPORT_C TInt CSCPParamObject::Parse( TDes8& aBuffer ) |
|
301 { |
|
302 TInt ret = KErrNone; |
|
303 |
|
304 TInt intSize = sizeof( TInt ); |
|
305 |
|
306 #ifdef UNICODE |
|
307 TInt charSize = 2; |
|
308 #else |
|
309 TInt charSize = 1; |
|
310 #endif // __UNICODE |
|
311 |
|
312 if ( aBuffer.Length() == 0 ) |
|
313 { |
|
314 // OK, nothing to parse |
|
315 return KErrNone; |
|
316 } |
|
317 |
|
318 if ( aBuffer.Length() < intSize ) |
|
319 { |
|
320 return KErrArgument; |
|
321 } |
|
322 |
|
323 // The number of parameters is the first TInt in the buffer |
|
324 TInt numParams = *(reinterpret_cast<TInt*>( &aBuffer[0] ) ); |
|
325 |
|
326 TInt curOffset = ( 1 + numParams * 2 ) * intSize; |
|
327 |
|
328 if ( aBuffer.Length() < curOffset ) |
|
329 { |
|
330 // The buffer must not be shorter than the header |
|
331 return KErrArgument; |
|
332 } |
|
333 |
|
334 for ( TInt i = 0; i < numParams; i++ ) |
|
335 { |
|
336 // 2 ints per param, offset 1 = ID, offset 2 = length |
|
337 TInt curID = *(reinterpret_cast<TInt*>( &aBuffer[ (i * 2 + 1) * intSize ]) ); |
|
338 TInt curLen = *(reinterpret_cast<TInt*>( &aBuffer[ (i * 2 + 2) * intSize ]) ); |
|
339 |
|
340 #ifdef UNICODE |
|
341 TPtr curDes( reinterpret_cast<TUint16*>(&aBuffer[ curOffset ]), curLen/charSize ); |
|
342 #else |
|
343 TPtr curDes( &aBuffer[ curOffset ], curLen/charSize ); |
|
344 #endif // __UNICODE |
|
345 curDes.SetLength( curDes.MaxLength() ); |
|
346 |
|
347 curOffset += curLen; |
|
348 |
|
349 if ( aBuffer.Length() < curOffset ) |
|
350 { |
|
351 ret = KErrArgument; |
|
352 break; |
|
353 } |
|
354 |
|
355 ret = Set( curID, curDes ); |
|
356 |
|
357 if ( ret != KErrNone ) |
|
358 { |
|
359 break; |
|
360 } |
|
361 } |
|
362 |
|
363 return ret; |
|
364 } |
|
365 |
|
366 |
|
367 |
|
368 // ---------------------------------------------------------- |
|
369 // CSCPParamObject::GetBuffer() |
|
370 // Form a buffer containing the stored parameters |
|
371 // |
|
372 // Status: Approved |
|
373 // ---------------------------------------------------------- |
|
374 EXPORT_C TInt CSCPParamObject::GetBuffer( HBufC8*& aBuffer ) |
|
375 { |
|
376 // Calculate the space required |
|
377 // The format is: |
|
378 // <numparams> + <numparams>*(<paramid><paramlen>) + |
|
379 // <numparams>*<paramvalue> |
|
380 TInt intSize = sizeof( TInt ); |
|
381 |
|
382 #ifdef UNICODE |
|
383 TInt charSize = 2; |
|
384 #else |
|
385 TInt charSize = 1; |
|
386 #endif // __UNICODE |
|
387 |
|
388 TInt headerSpace = ( 1 + iParamIDs.Count() * 2) * intSize; |
|
389 TInt dataSpace = 0; |
|
390 |
|
391 TInt i; |
|
392 for ( i = 0; i < iParamValues.Count(); i++ ) |
|
393 { |
|
394 // Add the size of each value |
|
395 dataSpace += ( iParamValues[i]->Des() ).Length() * charSize; |
|
396 } |
|
397 TInt spaceNeeded = headerSpace + dataSpace; |
|
398 |
|
399 HBufC8* buffer = NULL; |
|
400 |
|
401 TRAPD( ret, buffer = HBufC8::NewL( spaceNeeded ) ); |
|
402 if ( ret != KErrNone ) |
|
403 { |
|
404 Dprint( (_L("--> CSCPParamObject::GetBuffer(): Allocation FAILED") )); |
|
405 } |
|
406 else |
|
407 { |
|
408 TPtr8 bufPtr = buffer->Des(); |
|
409 TUint8* theBuffer = const_cast<TUint8*>( bufPtr.Ptr() ); |
|
410 |
|
411 // Header |
|
412 *(reinterpret_cast<TInt*>( &theBuffer[0] )) = iParamIDs.Count(); |
|
413 |
|
414 for ( i = 0; i < iParamIDs.Count(); i++ ) |
|
415 { |
|
416 // 2 ints per param, offset 1 = ID, offset 2 = length |
|
417 *(reinterpret_cast<TInt*>( &theBuffer[ (i * 2 + 1) * intSize ] )) |
|
418 = iParamIDs[i]; // ID |
|
419 *(reinterpret_cast<TInt*>( &theBuffer[ (i * 2 + 2) * intSize ] )) |
|
420 = ( iParamValues[i]->Des() ).Length() * charSize; // length |
|
421 } |
|
422 |
|
423 TInt curOffset = headerSpace; |
|
424 |
|
425 // Copy the values after the header |
|
426 for ( i = 0; i < iParamIDs.Count(); i++ ) |
|
427 { |
|
428 TAny* target = &theBuffer[ curOffset ]; |
|
429 TPtr sourceDes = iParamValues[i]->Des(); |
|
430 const TAny* source = static_cast<const TAny*>( sourceDes.Ptr() ); |
|
431 |
|
432 memcpy( target, source, sourceDes.Length() * charSize ); |
|
433 |
|
434 curOffset += sourceDes.Length() * charSize; |
|
435 } |
|
436 |
|
437 bufPtr.SetLength( curOffset ); |
|
438 aBuffer = buffer; |
|
439 } |
|
440 |
|
441 return ret; |
|
442 } |
|
443 |
|
444 |
|
445 |
|
446 // ---------------------------------------------------------- |
|
447 // CSCPParamObject::WriteToFile() |
|
448 // Write the output from GetBuffer to a file |
|
449 // |
|
450 // Status: Approved |
|
451 // ---------------------------------------------------------- |
|
452 // |
|
453 EXPORT_C void CSCPParamObject::WriteToFileL( TDesC& aFilename, RFs* aRfs /*= NULL*/ ) |
|
454 { |
|
455 HBufC8* paramBuffer; |
|
456 |
|
457 TInt ret = GetBuffer( paramBuffer ); |
|
458 if ( ret != KErrNone ) |
|
459 { |
|
460 Dprint( (_L("CSCPPluginEventHandler::WritePluginConfigurationL(): FAILED \ |
|
461 to get the configuration buffer: %d"), ret )); |
|
462 User::Leave( ret ); |
|
463 } |
|
464 CleanupStack::PushL( paramBuffer ); |
|
465 |
|
466 RFs* fsSessionPtr; |
|
467 RFs fsSession; |
|
468 |
|
469 if ( aRfs != NULL ) |
|
470 { |
|
471 fsSessionPtr = aRfs; |
|
472 } |
|
473 else |
|
474 { |
|
475 User::LeaveIfError( fsSession.Connect() ); |
|
476 CleanupClosePushL( fsSession ); |
|
477 fsSessionPtr = &fsSession; |
|
478 } |
|
479 |
|
480 TPtr8 paramDes = paramBuffer->Des(); |
|
481 |
|
482 RFile configFile; |
|
483 |
|
484 // Make sure that the target directory exists |
|
485 ret = fsSessionPtr->MkDirAll( aFilename ); |
|
486 if ( ( ret != KErrNone ) && ( ret != KErrAlreadyExists ) ) |
|
487 { |
|
488 Dprint( (_L("CSCPPluginEventHandler::WritePluginConfigurationL(): FAILED \ |
|
489 to create the target directory: %d"), ret )); |
|
490 User::Leave( ret ); |
|
491 } |
|
492 |
|
493 // Create or replace the existing file |
|
494 ret = configFile.Replace( *fsSessionPtr, |
|
495 aFilename, |
|
496 EFileShareAny | EFileWrite |
|
497 ); |
|
498 |
|
499 if ( ret != KErrNone ) |
|
500 { |
|
501 Dprint( (_L("CSCPPluginEventHandler::WritePluginConfigurationL():\ |
|
502 FAILED to replace configuration file: %d"), ret )); |
|
503 User::Leave( ret ); |
|
504 } |
|
505 |
|
506 CleanupClosePushL( configFile ); |
|
507 |
|
508 // Write the data |
|
509 ret = configFile.Write( paramDes ); |
|
510 |
|
511 if ( aRfs != NULL ) |
|
512 { |
|
513 CleanupStack::PopAndDestroy(); // configFile |
|
514 } |
|
515 else |
|
516 { |
|
517 CleanupStack::PopAndDestroy( 2 ); // configFile - fsSession |
|
518 } |
|
519 |
|
520 CleanupStack::PopAndDestroy( paramBuffer ); |
|
521 |
|
522 if ( ret != KErrNone ) |
|
523 { |
|
524 Dprint( (_L("CSCPPluginEventHandler::WritePluginConfigurationL():\ |
|
525 FAILED to write configuration file: %d"), ret )); |
|
526 User::Leave( ret ); |
|
527 } |
|
528 } |
|
529 |
|
530 |
|
531 // ---------------------------------------------------------- |
|
532 // CSCPParamObject::ReadFromFile() |
|
533 // Fetch the configuration from disk |
|
534 // |
|
535 // Status: Approved |
|
536 // ---------------------------------------------------------- |
|
537 // |
|
538 EXPORT_C void CSCPParamObject::ReadFromFileL( TDesC& aFilename, RFs* aRfs /*= NULL*/ ) |
|
539 { |
|
540 TInt ret; |
|
541 |
|
542 RFs* fsSessionPtr; |
|
543 RFs fsSession; |
|
544 |
|
545 if ( aRfs != NULL ) |
|
546 { |
|
547 fsSessionPtr = aRfs; |
|
548 } |
|
549 else |
|
550 { |
|
551 User::LeaveIfError( fsSession.Connect() ); |
|
552 CleanupClosePushL( fsSession ); |
|
553 fsSessionPtr = &fsSession; |
|
554 } |
|
555 |
|
556 RFile configFile; |
|
557 |
|
558 ret = configFile.Open( *fsSessionPtr, |
|
559 aFilename, |
|
560 EFileShareAny | EFileRead |
|
561 ); |
|
562 |
|
563 if ( ret != KErrNone ) |
|
564 { |
|
565 Dprint( (_L("CSCPParamObject::ReadFromFileL(): Failed \ |
|
566 to open config file: %d"), ret )); |
|
567 User::Leave( ret ); |
|
568 } |
|
569 |
|
570 CleanupClosePushL( configFile ); |
|
571 |
|
572 TInt size; |
|
573 ret = configFile.Size( size ); |
|
574 if ( ret != KErrNone ) |
|
575 { |
|
576 Dprint( (_L("CSCPParamObject::ReadFromFile(): Failed \ |
|
577 to get config file size: %d"), ret )); |
|
578 User::Leave( ret ); |
|
579 } |
|
580 |
|
581 HBufC8* paramBuf = HBufC8::NewL( size ); |
|
582 CleanupStack::PushL( paramBuf ); |
|
583 |
|
584 TPtr8 paramDes = paramBuf->Des(); |
|
585 ret = configFile.Read( paramDes ); |
|
586 if ( ret != KErrNone ) |
|
587 { |
|
588 Dprint( (_L("CSCPParamObject::ReadFromFile(): Failed \ |
|
589 read configuration data: %d"), ret )); |
|
590 User::Leave( ret ); |
|
591 } |
|
592 |
|
593 // Parse the parameters from the buffer |
|
594 ret = Parse( paramDes ); |
|
595 |
|
596 CleanupStack::PopAndDestroy( paramBuf ); |
|
597 |
|
598 if ( aRfs != NULL ) |
|
599 { |
|
600 CleanupStack::PopAndDestroy(); // configFile |
|
601 } |
|
602 else |
|
603 { |
|
604 CleanupStack::PopAndDestroy( 2 ); // configFile - fsSession |
|
605 } |
|
606 |
|
607 if ( ret != KErrNone ) |
|
608 { |
|
609 Dprint( (_L("CSCPParamObject::ReadFromFile(): Failed \ |
|
610 to parse buffer for parameters: %d"), ret )); |
|
611 User::Leave( ret ); |
|
612 } |
|
613 } |
|
614 |
|
615 |
|
616 // ---------------------------------------------------------- |
|
617 // CSCPParamObject::~CSCPParamObject() |
|
618 // Destructor |
|
619 // |
|
620 // Status: Approved |
|
621 // ---------------------------------------------------------- |
|
622 EXPORT_C CSCPParamObject::~CSCPParamObject() |
|
623 { |
|
624 Dprint( (_L("--> CSCPParamObject::~CSCPParamObject()") )); |
|
625 |
|
626 Reset(); |
|
627 |
|
628 Dprint( (_L("<-- CSCPParamObject::~CSCPParamObject()") )); |
|
629 } |
|
630 |
|
631 // End of file |
|
632 |