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: * Methods for encoding object identifiers sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "base128enc.h" sl@0: #include "panic.h" sl@0: sl@0: #include sl@0: sl@0: const TInt KArrayGranularity = 5; sl@0: sl@0: const TUint KSecondTermBigLimit = 175; sl@0: const TUint KFirstTermMultiplier = 40; sl@0: const TUint KFirstTermMax = 2; sl@0: sl@0: sl@0: EXPORT_C CASN1EncObjectIdentifier* CASN1EncObjectIdentifier::NewLC(const TDesC& aStr) sl@0: { sl@0: CASN1EncObjectIdentifier* self = new (ELeave) CASN1EncObjectIdentifier(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aStr); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CASN1EncObjectIdentifier* CASN1EncObjectIdentifier::NewL(const TDesC& aStr) sl@0: { sl@0: CASN1EncObjectIdentifier* self = NewLC(aStr); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CASN1EncObjectIdentifier::~CASN1EncObjectIdentifier() sl@0: { sl@0: iData.Close(); sl@0: } sl@0: sl@0: sl@0: CASN1EncObjectIdentifier::CASN1EncObjectIdentifier() sl@0: : CASN1EncPrimitive(EASN1ObjectIdentifier), iData(KArrayGranularity) sl@0: { sl@0: } sl@0: sl@0: sl@0: // Takes ints in a string, delimited by '.' characters in between (not at ends) sl@0: void CASN1EncObjectIdentifier::ConstructL(const TDesC& aStr) sl@0: { sl@0: iData.Reset(); sl@0: TInt index = 0; sl@0: TLex lex(aStr); sl@0: sl@0: // First term sl@0: __ASSERT_ALWAYS(!lex.Eos(), User::Leave(KErrBadDescriptor)); sl@0: TUint first; sl@0: User::LeaveIfError(lex.Val(first)); sl@0: __ASSERT_ALWAYS(first <= KFirstTermMax, User::Leave(KErrBadDescriptor)); sl@0: // Static cast takes 8 least sig bits sl@0: iFirstOctet = STATIC_CAST(TUint8, KFirstTermMultiplier * first); sl@0: sl@0: // A '.' to delimit sl@0: __ASSERT_ALWAYS(!lex.Eos() && lex.Get() == '.', User::Leave(KErrBadDescriptor)); sl@0: sl@0: // Second term sl@0: __ASSERT_ALWAYS(!lex.Eos(), User::Leave(KErrBadDescriptor)); sl@0: TUint second; sl@0: User::LeaveIfError(lex.Val(second)); sl@0: __ASSERT_ALWAYS((first < KFirstTermMax && second < KFirstTermMultiplier) sl@0: || (first == KFirstTermMax && second <= KSecondTermBigLimit), sl@0: User::Leave(KErrBadDescriptor)); sl@0: // Static cast takes 8 least sig bits sl@0: iFirstOctet = STATIC_CAST(TUint8, iFirstOctet + second); sl@0: sl@0: // Remaining terms sl@0: while (!lex.Eos()) sl@0: { sl@0: // Delimiter, and check we're not at end after that sl@0: __ASSERT_ALWAYS(lex.Get() == '.' && !lex.Eos(), User::Leave(KErrBadDescriptor)); sl@0: sl@0: TUint value; sl@0: User::LeaveIfError(lex.Val(value)); sl@0: ++index; sl@0: sl@0: // Store the data away for later sl@0: TASN1EncBase128DER encoder(value); sl@0: User::LeaveIfError(iData.Append(encoder)); sl@0: } sl@0: sl@0: // Remainder of ConstructL is here - safe to call CalculateContentsLengthDER now. sl@0: CASN1EncPrimitive::ConstructL(); sl@0: } sl@0: sl@0: sl@0: void CASN1EncObjectIdentifier::CalculateContentsLengthDER() sl@0: { sl@0: iContentsLengthDER = 1; sl@0: for (TInt i = iData.Count() - 1; i >= 0; --i) sl@0: { sl@0: iContentsLengthDER += iData[i].LengthDER(); sl@0: } sl@0: } sl@0: sl@0: sl@0: void CASN1EncObjectIdentifier::WriteContentsDERL(TDes8& aBuf) const sl@0: { sl@0: aBuf[0] = iFirstOctet; sl@0: TUint cursor = 1; sl@0: TInt count = iData.Count(); sl@0: for (TInt i = 0; i < count; ++i) sl@0: { sl@0: iData[i].WriteDERL(aBuf, cursor); sl@0: } sl@0: }