os/security/securityanddataprivacytools/securitytools/certapp/encdec/capabilityset.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/securityanddataprivacytools/securitytools/certapp/encdec/capabilityset.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,148 @@
     1.4 +/*
     1.5 +* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#define __INCLUDE_CAPABILITY_NAMES__
    1.23 +#include "capabilityset.h"
    1.24 +
    1.25 +CapabilitySet::CapabilitySet()
    1.26 +{
    1.27 +	memset(iCaps,0,sizeof(iCaps));
    1.28 +}
    1.29 +
    1.30 +#if 0
    1.31 +// Not currently used or tested
    1.32 +CapabilitySet::CapabilitySet(const CapabilitySet &aRef)
    1.33 +{
    1.34 +	memcpy(iCaps, aRef.iCaps, sizeof(iCaps));
    1.35 +}
    1.36 +#endif
    1.37 +
    1.38 +CapabilitySet &CapabilitySet::operator=(const CapabilitySet &aRhs)
    1.39 +{
    1.40 +BULLSEYE_OFF
    1.41 +	if(this == &aRhs) return *this; // handle self assignment
    1.42 +BULLSEYE_RESTORE
    1.43 +	memcpy(iCaps, aRhs.iCaps, sizeof(iCaps));
    1.44 +	return *this;
    1.45 +}
    1.46 +
    1.47 +
    1.48 +void CapabilitySet::AddCapability(TCapability aCapability)
    1.49 +{
    1.50 +	BULLSEYE_OFF
    1.51 +	if((TUint32)aCapability>=(TUint32)ECapability_HardLimit)
    1.52 +		{
    1.53 +		dbg << Log::Indent() << "Illegal capabaility value " << aCapability << Log::Endl();
    1.54 +		FatalError();
    1.55 +		}
    1.56 +	BULLSEYE_RESTORE
    1.57 +	
    1.58 +	TInt index = aCapability>>3;
    1.59 +	TUint8 mask = (TUint8)(1<<(aCapability&7));
    1.60 +	//mask &= ((TUint8*)&AllSupportedCapabilities)[index];
    1.61 +	((TUint8*)iCaps)[index] |= mask;
    1.62 +}
    1.63 +
    1.64 +TBool CapabilitySet::HasCapability(TCapability aCapability) const
    1.65 +{
    1.66 +	BULLSEYE_OFF
    1.67 +	if((TUint32)aCapability>=(TUint32)ECapability_HardLimit)
    1.68 +		{
    1.69 +		FatalError();
    1.70 +		}
    1.71 +	BULLSEYE_RESTORE
    1.72 +
    1.73 +	return (((TUint8*)iCaps)[aCapability>>3]>>(aCapability&7))&1;
    1.74 +}
    1.75 +
    1.76 +
    1.77 +void EncodeHuman(REncodeWriteStream& aStream,const CapabilitySet &aCapSet)
    1.78 +{
    1.79 +	aStream.WriteCStr(" { ");
    1.80 +	for(TUint32 cap=0; cap < ECapability_Limit; ++cap)
    1.81 +		{
    1.82 +		if(aCapSet.HasCapability(TCapability(cap)))
    1.83 +			{
    1.84 +			aStream.WriteCStr(CapabilityNames[cap]);
    1.85 +			aStream.WriteSpace();
    1.86 +			}
    1.87 +		}
    1.88 +
    1.89 +	for(TUint32 cap=ECapability_Limit; cap < ECapability_HardLimit; ++cap)
    1.90 +		{
    1.91 +		if(aCapSet.HasCapability(TCapability(cap)))
    1.92 +			{
    1.93 +			EncodeHuman(aStream, TUint32(cap));		
    1.94 +			aStream.WriteSpace();
    1.95 +			}
    1.96 +		}
    1.97 +
    1.98 +	aStream.WriteByte('}');
    1.99 +}
   1.100 +
   1.101 +void DecodeHuman(RDecodeReadStream& aStream, CapabilitySet &aCapSet)
   1.102 +{
   1.103 +	aStream.CheckName("{");
   1.104 +	while(aStream.PeakToken() != "}")
   1.105 +		{
   1.106 +		AutoIndent ai(prog);
   1.107 +		// We process PeakToken so if it is not valid we can call
   1.108 +		// DecodeHuman TUint32 to read the next token and process it
   1.109 +		// as a number.
   1.110 +		prog << Log::Indent() << "Parsing capability '" << aStream.PeakToken() << "'" << Log::Endl();
   1.111 +		TUint32 cap;
   1.112 +		for(cap=0; cap < ECapability_Limit; ++cap)
   1.113 +			{
   1.114 +			if(aStream.PeakToken() == CapabilityNames[cap])
   1.115 +				{
   1.116 +				break;
   1.117 +				}
   1.118 +			}
   1.119 +		if(cap < ECapability_Limit)
   1.120 +			{
   1.121 +			// The token was ok, so read/discard it.
   1.122 +			aStream.ReadNextToken();
   1.123 +			}
   1.124 +		else
   1.125 +			{
   1.126 +			// Fallback to decoding as a number
   1.127 +			prog<< Log::Indent() << "WARNING: Unknown capability '" << aStream.PeakToken() << "' attempting to decode as a bit number..." << Log::Endl();
   1.128 +			DecodeHuman(aStream, cap);
   1.129 +			prog << Log::Indent() << "Decoded as " << cap << Log::Endl();
   1.130 +			}
   1.131 +		aCapSet.AddCapability(TCapability(cap));
   1.132 +		}
   1.133 +	aStream.CheckName("}");
   1.134 +}
   1.135 +
   1.136 +
   1.137 +void CapabilitySet::ExternalizeL(RWriteStream &aStream) const
   1.138 +{
   1.139 +	TPckg<typeof(iCaps)> capsPckg(iCaps);
   1.140 +	aStream << capsPckg;
   1.141 +}
   1.142 +
   1.143 +void CapabilitySet::InternalizeL(RReadStream &aStream)
   1.144 +{
   1.145 +	TPckg<typeof(iCaps)> capsPckg(iCaps);
   1.146 +	aStream >> capsPckg;
   1.147 +}
   1.148 +
   1.149 +
   1.150 +
   1.151 +// End of file