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 TASN1EncBase128 class. sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "base128enc.h" sl@0: sl@0: sl@0: TASN1EncBase128DER::TASN1EncBase128DER(TUint aInt) : iInt(aInt), iLengthDER(0) sl@0: { sl@0: CalculateLengthDER(); sl@0: } sl@0: sl@0: sl@0: TUint TASN1EncBase128DER::LengthDER() const sl@0: { sl@0: return iLengthDER; sl@0: } sl@0: sl@0: sl@0: void TASN1EncBase128DER::CalculateLengthDER() sl@0: { sl@0: iLengthDER = 1; sl@0: TUint working = iInt; sl@0: while (working >>= 7) sl@0: { sl@0: ++iLengthDER; sl@0: } sl@0: } sl@0: sl@0: sl@0: void TASN1EncBase128DER::WriteDERL(TDes8& aBuf, TUint& aPos) const sl@0: { sl@0: __ASSERT_DEBUG(aBuf.Length() - aPos >= STATIC_CAST(TUint8, iLengthDER), sl@0: User::Leave(KErrBadDescriptor)); sl@0: sl@0: TInt last = aPos + iLengthDER - 1; sl@0: TUint working = iInt; sl@0: for (TUint cursor = last; cursor >= aPos; --cursor) sl@0: { sl@0: // Cast takes least significant 8 bits only (actually, we only need 7) sl@0: aBuf[cursor] = STATIC_CAST(TUint8, working); sl@0: aBuf[cursor] |= 0x80; // Top bit always set to 1... sl@0: working >>= 7; sl@0: } sl@0: // ...except top bit on last byte is 0 sl@0: aBuf[last] &= 0x7F; sl@0: sl@0: aPos += iLengthDER; sl@0: }