First public contribution.
2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Methods for encoding object identifiers
20 #include "base128enc.h"
25 const TInt KArrayGranularity = 5;
27 const TUint KSecondTermBigLimit = 175;
28 const TUint KFirstTermMultiplier = 40;
29 const TUint KFirstTermMax = 2;
32 EXPORT_C CASN1EncObjectIdentifier* CASN1EncObjectIdentifier::NewLC(const TDesC& aStr)
34 CASN1EncObjectIdentifier* self = new (ELeave) CASN1EncObjectIdentifier();
35 CleanupStack::PushL(self);
36 self->ConstructL(aStr);
40 EXPORT_C CASN1EncObjectIdentifier* CASN1EncObjectIdentifier::NewL(const TDesC& aStr)
42 CASN1EncObjectIdentifier* self = NewLC(aStr);
43 CleanupStack::Pop(self);
47 EXPORT_C CASN1EncObjectIdentifier::~CASN1EncObjectIdentifier()
53 CASN1EncObjectIdentifier::CASN1EncObjectIdentifier()
54 : CASN1EncPrimitive(EASN1ObjectIdentifier), iData(KArrayGranularity)
59 // Takes ints in a string, delimited by '.' characters in between (not at ends)
60 void CASN1EncObjectIdentifier::ConstructL(const TDesC& aStr)
67 __ASSERT_ALWAYS(!lex.Eos(), User::Leave(KErrBadDescriptor));
69 User::LeaveIfError(lex.Val(first));
70 __ASSERT_ALWAYS(first <= KFirstTermMax, User::Leave(KErrBadDescriptor));
71 // Static cast takes 8 least sig bits
72 iFirstOctet = STATIC_CAST(TUint8, KFirstTermMultiplier * first);
75 __ASSERT_ALWAYS(!lex.Eos() && lex.Get() == '.', User::Leave(KErrBadDescriptor));
78 __ASSERT_ALWAYS(!lex.Eos(), User::Leave(KErrBadDescriptor));
80 User::LeaveIfError(lex.Val(second));
81 __ASSERT_ALWAYS((first < KFirstTermMax && second < KFirstTermMultiplier)
82 || (first == KFirstTermMax && second <= KSecondTermBigLimit),
83 User::Leave(KErrBadDescriptor));
84 // Static cast takes 8 least sig bits
85 iFirstOctet = STATIC_CAST(TUint8, iFirstOctet + second);
90 // Delimiter, and check we're not at end after that
91 __ASSERT_ALWAYS(lex.Get() == '.' && !lex.Eos(), User::Leave(KErrBadDescriptor));
94 User::LeaveIfError(lex.Val(value));
97 // Store the data away for later
98 TASN1EncBase128DER encoder(value);
99 User::LeaveIfError(iData.Append(encoder));
102 // Remainder of ConstructL is here - safe to call CalculateContentsLengthDER now.
103 CASN1EncPrimitive::ConstructL();
107 void CASN1EncObjectIdentifier::CalculateContentsLengthDER()
109 iContentsLengthDER = 1;
110 for (TInt i = iData.Count() - 1; i >= 0; --i)
112 iContentsLengthDER += iData[i].LengthDER();
117 void CASN1EncObjectIdentifier::WriteContentsDERL(TDes8& aBuf) const
119 aBuf[0] = iFirstOctet;
121 TInt count = iData.Count();
122 for (TInt i = 0; i < count; ++i)
124 iData[i].WriteDERL(aBuf, cursor);