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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#define __INCLUDE_CAPABILITY_NAMES__
sl@0
    20
#include "capabilityset.h"
sl@0
    21
sl@0
    22
CapabilitySet::CapabilitySet()
sl@0
    23
{
sl@0
    24
	memset(iCaps,0,sizeof(iCaps));
sl@0
    25
}
sl@0
    26
sl@0
    27
#if 0
sl@0
    28
// Not currently used or tested
sl@0
    29
CapabilitySet::CapabilitySet(const CapabilitySet &aRef)
sl@0
    30
{
sl@0
    31
	memcpy(iCaps, aRef.iCaps, sizeof(iCaps));
sl@0
    32
}
sl@0
    33
#endif
sl@0
    34
sl@0
    35
CapabilitySet &CapabilitySet::operator=(const CapabilitySet &aRhs)
sl@0
    36
{
sl@0
    37
BULLSEYE_OFF
sl@0
    38
	if(this == &aRhs) return *this; // handle self assignment
sl@0
    39
BULLSEYE_RESTORE
sl@0
    40
	memcpy(iCaps, aRhs.iCaps, sizeof(iCaps));
sl@0
    41
	return *this;
sl@0
    42
}
sl@0
    43
sl@0
    44
sl@0
    45
void CapabilitySet::AddCapability(TCapability aCapability)
sl@0
    46
{
sl@0
    47
	BULLSEYE_OFF
sl@0
    48
	if((TUint32)aCapability>=(TUint32)ECapability_HardLimit)
sl@0
    49
		{
sl@0
    50
		dbg << Log::Indent() << "Illegal capabaility value " << aCapability << Log::Endl();
sl@0
    51
		FatalError();
sl@0
    52
		}
sl@0
    53
	BULLSEYE_RESTORE
sl@0
    54
	
sl@0
    55
	TInt index = aCapability>>3;
sl@0
    56
	TUint8 mask = (TUint8)(1<<(aCapability&7));
sl@0
    57
	//mask &= ((TUint8*)&AllSupportedCapabilities)[index];
sl@0
    58
	((TUint8*)iCaps)[index] |= mask;
sl@0
    59
}
sl@0
    60
sl@0
    61
TBool CapabilitySet::HasCapability(TCapability aCapability) const
sl@0
    62
{
sl@0
    63
	BULLSEYE_OFF
sl@0
    64
	if((TUint32)aCapability>=(TUint32)ECapability_HardLimit)
sl@0
    65
		{
sl@0
    66
		FatalError();
sl@0
    67
		}
sl@0
    68
	BULLSEYE_RESTORE
sl@0
    69
sl@0
    70
	return (((TUint8*)iCaps)[aCapability>>3]>>(aCapability&7))&1;
sl@0
    71
}
sl@0
    72
sl@0
    73
sl@0
    74
void EncodeHuman(REncodeWriteStream& aStream,const CapabilitySet &aCapSet)
sl@0
    75
{
sl@0
    76
	aStream.WriteCStr(" { ");
sl@0
    77
	for(TUint32 cap=0; cap < ECapability_Limit; ++cap)
sl@0
    78
		{
sl@0
    79
		if(aCapSet.HasCapability(TCapability(cap)))
sl@0
    80
			{
sl@0
    81
			aStream.WriteCStr(CapabilityNames[cap]);
sl@0
    82
			aStream.WriteSpace();
sl@0
    83
			}
sl@0
    84
		}
sl@0
    85
sl@0
    86
	for(TUint32 cap=ECapability_Limit; cap < ECapability_HardLimit; ++cap)
sl@0
    87
		{
sl@0
    88
		if(aCapSet.HasCapability(TCapability(cap)))
sl@0
    89
			{
sl@0
    90
			EncodeHuman(aStream, TUint32(cap));		
sl@0
    91
			aStream.WriteSpace();
sl@0
    92
			}
sl@0
    93
		}
sl@0
    94
sl@0
    95
	aStream.WriteByte('}');
sl@0
    96
}
sl@0
    97
sl@0
    98
void DecodeHuman(RDecodeReadStream& aStream, CapabilitySet &aCapSet)
sl@0
    99
{
sl@0
   100
	aStream.CheckName("{");
sl@0
   101
	while(aStream.PeakToken() != "}")
sl@0
   102
		{
sl@0
   103
		AutoIndent ai(prog);
sl@0
   104
		// We process PeakToken so if it is not valid we can call
sl@0
   105
		// DecodeHuman TUint32 to read the next token and process it
sl@0
   106
		// as a number.
sl@0
   107
		prog << Log::Indent() << "Parsing capability '" << aStream.PeakToken() << "'" << Log::Endl();
sl@0
   108
		TUint32 cap;
sl@0
   109
		for(cap=0; cap < ECapability_Limit; ++cap)
sl@0
   110
			{
sl@0
   111
			if(aStream.PeakToken() == CapabilityNames[cap])
sl@0
   112
				{
sl@0
   113
				break;
sl@0
   114
				}
sl@0
   115
			}
sl@0
   116
		if(cap < ECapability_Limit)
sl@0
   117
			{
sl@0
   118
			// The token was ok, so read/discard it.
sl@0
   119
			aStream.ReadNextToken();
sl@0
   120
			}
sl@0
   121
		else
sl@0
   122
			{
sl@0
   123
			// Fallback to decoding as a number
sl@0
   124
			prog<< Log::Indent() << "WARNING: Unknown capability '" << aStream.PeakToken() << "' attempting to decode as a bit number..." << Log::Endl();
sl@0
   125
			DecodeHuman(aStream, cap);
sl@0
   126
			prog << Log::Indent() << "Decoded as " << cap << Log::Endl();
sl@0
   127
			}
sl@0
   128
		aCapSet.AddCapability(TCapability(cap));
sl@0
   129
		}
sl@0
   130
	aStream.CheckName("}");
sl@0
   131
}
sl@0
   132
sl@0
   133
sl@0
   134
void CapabilitySet::ExternalizeL(RWriteStream &aStream) const
sl@0
   135
{
sl@0
   136
	TPckg<typeof(iCaps)> capsPckg(iCaps);
sl@0
   137
	aStream << capsPckg;
sl@0
   138
}
sl@0
   139
sl@0
   140
void CapabilitySet::InternalizeL(RReadStream &aStream)
sl@0
   141
{
sl@0
   142
	TPckg<typeof(iCaps)> capsPckg(iCaps);
sl@0
   143
	aStream >> capsPckg;
sl@0
   144
}
sl@0
   145
sl@0
   146
sl@0
   147
sl@0
   148
// End of file