sl@0: /* sl@0: * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Implementation for class encoding TInt in ASN1 DER sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: sl@0: EXPORT_C CASN1EncInt* CASN1EncInt::NewLC(const TInt aInt) sl@0: { sl@0: CASN1EncInt* self = new (ELeave) CASN1EncInt(aInt); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CASN1EncInt* CASN1EncInt::NewL(const TInt aInt) sl@0: { sl@0: CASN1EncInt* self = NewLC(aInt); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CASN1EncInt::CASN1EncInt(const TInt aInt) sl@0: : CASN1EncPrimitive(EASN1Integer), iInt(aInt) sl@0: { sl@0: } sl@0: sl@0: void CASN1EncInt::CalculateContentsLengthDER() sl@0: { sl@0: iContentsLengthDER = 1; sl@0: sl@0: // Flip bits in -ve numbers sl@0: TUint working = iInt < 0 ? ~iInt : iInt; sl@0: TUint last = working; sl@0: while (working >>= 8) sl@0: { sl@0: last = working; sl@0: ++iContentsLengthDER; sl@0: } sl@0: sl@0: // If most sig non-zero byte had a 1 top bit, we'll need the next byte too sl@0: // (applies to both +ve and -ve cases) sl@0: if (last & 0x80) sl@0: { sl@0: ++iContentsLengthDER; sl@0: } sl@0: } sl@0: sl@0: sl@0: void CASN1EncInt::WriteContentsDERL(TDes8& aBuf) const sl@0: { sl@0: // Unsigned, so operator>>= works sensibly sl@0: TUint working = iInt; sl@0: TUint position = iContentsLengthDER; sl@0: while (position) sl@0: { sl@0: // Cast takes least sig 8 bits sl@0: aBuf[--position] = STATIC_CAST(TUint8, working); sl@0: working >>= 8; sl@0: } sl@0: }