cryptoservices/certificateandkeymgmt/asn1/base128enc.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Implementation for TASN1EncBase128 class.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "base128enc.h"
       
    21 
       
    22 
       
    23 TASN1EncBase128DER::TASN1EncBase128DER(TUint aInt) : iInt(aInt), iLengthDER(0)
       
    24 	{
       
    25 	CalculateLengthDER();
       
    26 	}
       
    27 
       
    28 
       
    29 TUint TASN1EncBase128DER::LengthDER() const
       
    30 	{
       
    31 	return iLengthDER;
       
    32 	}
       
    33 
       
    34 
       
    35 void TASN1EncBase128DER::CalculateLengthDER()
       
    36 	{
       
    37 	iLengthDER = 1;
       
    38 	TUint working = iInt;
       
    39 	while (working >>= 7)
       
    40 		{
       
    41 		++iLengthDER;
       
    42 		}
       
    43 	}
       
    44 
       
    45 
       
    46 void TASN1EncBase128DER::WriteDERL(TDes8& aBuf, TUint& aPos) const
       
    47 	{
       
    48 	__ASSERT_DEBUG(aBuf.Length() - aPos >= STATIC_CAST(TUint8, iLengthDER), 
       
    49 		User::Leave(KErrBadDescriptor));
       
    50 
       
    51 	TInt last = aPos + iLengthDER - 1;
       
    52 	TUint working = iInt;
       
    53 	for (TUint cursor = last; cursor >= aPos; --cursor)
       
    54 		{
       
    55 		// Cast takes least significant 8 bits only (actually, we only need 7)
       
    56 		aBuf[cursor] = STATIC_CAST(TUint8, working);
       
    57 		aBuf[cursor] |= 0x80; // Top bit always set to 1...
       
    58 		working >>= 7;
       
    59 		}
       
    60 	// ...except top bit on last byte is 0
       
    61 	aBuf[last] &= 0x7F;
       
    62 
       
    63 	aPos += iLengthDER;
       
    64 	}