os/security/cryptoservices/certificateandkeymgmt/asn1/intenc.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/asn1/intenc.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,75 @@
     1.4 +/*
     1.5 +* Copyright (c) 2001-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 +* Implementation for class encoding TInt in ASN1 DER
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +#include <asn1enc.h>
    1.24 +
    1.25 +EXPORT_C CASN1EncInt* CASN1EncInt::NewLC(const TInt aInt)
    1.26 +	{
    1.27 +	CASN1EncInt* self = new (ELeave) CASN1EncInt(aInt);
    1.28 +	CleanupStack::PushL(self);
    1.29 +	self->ConstructL();
    1.30 +	return self;
    1.31 +	}
    1.32 +
    1.33 +EXPORT_C CASN1EncInt* CASN1EncInt::NewL(const TInt aInt)
    1.34 +	{
    1.35 +	CASN1EncInt* self = NewLC(aInt);
    1.36 +	CleanupStack::Pop(self);
    1.37 +	return self;
    1.38 +	}
    1.39 +
    1.40 +CASN1EncInt::CASN1EncInt(const TInt aInt)
    1.41 +: CASN1EncPrimitive(EASN1Integer), iInt(aInt)
    1.42 +	{
    1.43 +	}
    1.44 +
    1.45 +void CASN1EncInt::CalculateContentsLengthDER()
    1.46 +	{
    1.47 +	iContentsLengthDER = 1;
    1.48 +
    1.49 +	// Flip bits in -ve numbers
    1.50 +	TUint working = iInt < 0 ? ~iInt : iInt;
    1.51 +	TUint last = working;
    1.52 +	while (working >>= 8)
    1.53 +		{
    1.54 +		last = working;
    1.55 +		++iContentsLengthDER;
    1.56 +		}
    1.57 +	
    1.58 +	// If most sig non-zero byte had a 1 top bit, we'll need the next byte too
    1.59 +	// (applies to both +ve and -ve cases)
    1.60 +	if (last & 0x80)
    1.61 +		{
    1.62 +		++iContentsLengthDER;
    1.63 +		}
    1.64 +	}
    1.65 +
    1.66 +
    1.67 +void CASN1EncInt::WriteContentsDERL(TDes8& aBuf) const
    1.68 +	{
    1.69 +	// Unsigned, so operator>>= works sensibly
    1.70 +	TUint working = iInt;
    1.71 +	TUint position = iContentsLengthDER;
    1.72 +	while (position)
    1.73 +		{
    1.74 +		// Cast takes least sig 8 bits
    1.75 +		aBuf[--position] = STATIC_CAST(TUint8, working);
    1.76 +		working >>= 8;
    1.77 +		}
    1.78 +	}