os/security/securityanddataprivacytools/securitytools/certapp/encdec/capabilityset.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2008-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 *
    16 */
    17 
    18 
    19 #define __INCLUDE_CAPABILITY_NAMES__
    20 #include "capabilityset.h"
    21 
    22 CapabilitySet::CapabilitySet()
    23 {
    24 	memset(iCaps,0,sizeof(iCaps));
    25 }
    26 
    27 #if 0
    28 // Not currently used or tested
    29 CapabilitySet::CapabilitySet(const CapabilitySet &aRef)
    30 {
    31 	memcpy(iCaps, aRef.iCaps, sizeof(iCaps));
    32 }
    33 #endif
    34 
    35 CapabilitySet &CapabilitySet::operator=(const CapabilitySet &aRhs)
    36 {
    37 BULLSEYE_OFF
    38 	if(this == &aRhs) return *this; // handle self assignment
    39 BULLSEYE_RESTORE
    40 	memcpy(iCaps, aRhs.iCaps, sizeof(iCaps));
    41 	return *this;
    42 }
    43 
    44 
    45 void CapabilitySet::AddCapability(TCapability aCapability)
    46 {
    47 	BULLSEYE_OFF
    48 	if((TUint32)aCapability>=(TUint32)ECapability_HardLimit)
    49 		{
    50 		dbg << Log::Indent() << "Illegal capabaility value " << aCapability << Log::Endl();
    51 		FatalError();
    52 		}
    53 	BULLSEYE_RESTORE
    54 	
    55 	TInt index = aCapability>>3;
    56 	TUint8 mask = (TUint8)(1<<(aCapability&7));
    57 	//mask &= ((TUint8*)&AllSupportedCapabilities)[index];
    58 	((TUint8*)iCaps)[index] |= mask;
    59 }
    60 
    61 TBool CapabilitySet::HasCapability(TCapability aCapability) const
    62 {
    63 	BULLSEYE_OFF
    64 	if((TUint32)aCapability>=(TUint32)ECapability_HardLimit)
    65 		{
    66 		FatalError();
    67 		}
    68 	BULLSEYE_RESTORE
    69 
    70 	return (((TUint8*)iCaps)[aCapability>>3]>>(aCapability&7))&1;
    71 }
    72 
    73 
    74 void EncodeHuman(REncodeWriteStream& aStream,const CapabilitySet &aCapSet)
    75 {
    76 	aStream.WriteCStr(" { ");
    77 	for(TUint32 cap=0; cap < ECapability_Limit; ++cap)
    78 		{
    79 		if(aCapSet.HasCapability(TCapability(cap)))
    80 			{
    81 			aStream.WriteCStr(CapabilityNames[cap]);
    82 			aStream.WriteSpace();
    83 			}
    84 		}
    85 
    86 	for(TUint32 cap=ECapability_Limit; cap < ECapability_HardLimit; ++cap)
    87 		{
    88 		if(aCapSet.HasCapability(TCapability(cap)))
    89 			{
    90 			EncodeHuman(aStream, TUint32(cap));		
    91 			aStream.WriteSpace();
    92 			}
    93 		}
    94 
    95 	aStream.WriteByte('}');
    96 }
    97 
    98 void DecodeHuman(RDecodeReadStream& aStream, CapabilitySet &aCapSet)
    99 {
   100 	aStream.CheckName("{");
   101 	while(aStream.PeakToken() != "}")
   102 		{
   103 		AutoIndent ai(prog);
   104 		// We process PeakToken so if it is not valid we can call
   105 		// DecodeHuman TUint32 to read the next token and process it
   106 		// as a number.
   107 		prog << Log::Indent() << "Parsing capability '" << aStream.PeakToken() << "'" << Log::Endl();
   108 		TUint32 cap;
   109 		for(cap=0; cap < ECapability_Limit; ++cap)
   110 			{
   111 			if(aStream.PeakToken() == CapabilityNames[cap])
   112 				{
   113 				break;
   114 				}
   115 			}
   116 		if(cap < ECapability_Limit)
   117 			{
   118 			// The token was ok, so read/discard it.
   119 			aStream.ReadNextToken();
   120 			}
   121 		else
   122 			{
   123 			// Fallback to decoding as a number
   124 			prog<< Log::Indent() << "WARNING: Unknown capability '" << aStream.PeakToken() << "' attempting to decode as a bit number..." << Log::Endl();
   125 			DecodeHuman(aStream, cap);
   126 			prog << Log::Indent() << "Decoded as " << cap << Log::Endl();
   127 			}
   128 		aCapSet.AddCapability(TCapability(cap));
   129 		}
   130 	aStream.CheckName("}");
   131 }
   132 
   133 
   134 void CapabilitySet::ExternalizeL(RWriteStream &aStream) const
   135 {
   136 	TPckg<typeof(iCaps)> capsPckg(iCaps);
   137 	aStream << capsPckg;
   138 }
   139 
   140 void CapabilitySet::InternalizeL(RReadStream &aStream)
   141 {
   142 	TPckg<typeof(iCaps)> capsPckg(iCaps);
   143 	aStream >> capsPckg;
   144 }
   145 
   146 
   147 
   148 // End of file