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 * Define methods for asn1-encoding base class
20 #include "base128enc.h"
25 const TUint KMaxLowNumberFormTag = 30;
26 const TUint KMaxShortFormLength = 127;
29 EXPORT_C CASN1EncBase::CASN1EncBase(const TTagType aType, const TASN1Class aClass)
30 : iType(aType), iClass(aClass)
32 CalculateTagLengthDER();
36 EXPORT_C CASN1EncBase::~CASN1EncBase()
41 EXPORT_C void CASN1EncBase::ConstructL()
43 ContentsLengthChanged();
47 void CASN1EncBase::ContentsLengthChanged()
49 CalculateContentsLengthDER();
50 CalculateLengthLengthDER();
54 iParent->ContentsLengthChanged();
59 // Set tagging scheme/tag/class
60 EXPORT_C void CASN1EncBase::SetTag(const TTagType aType, const TASN1Class aClass)
65 CalculateTagLengthDER();
69 // Gives total number of octets in the DER encoding of this object
70 EXPORT_C TUint CASN1EncBase::LengthDER() const
72 return iTagLengthDER + iLengthLengthDER + iContentsLengthDER;
76 // Writes entire DER encoding of this object into the given buffer
77 EXPORT_C void CASN1EncBase::WriteDERL(TDes8& aBuf, TUint& aPos) const
79 __ASSERT_ALWAYS(aBuf.Length() - STATIC_CAST(TInt, aPos) >= STATIC_CAST(TInt, LengthDER()),
80 Panic(KErrDescriptorTooShort));
82 WriteHelperL(aBuf, aPos, iTagLengthDER, &CASN1EncBase::WriteTagDERL);
83 WriteHelperL(aBuf, aPos, iLengthLengthDER, &CASN1EncBase::WriteLengthDER);
84 WriteHelperL(aBuf, aPos, iContentsLengthDER, &CASN1EncBase::WriteContentsDERL);
88 // Internal helper method, used for efficiency
89 void CASN1EncBase::WriteHelperL(TDes8& aBuf, TUint& aPos, const TUint aLength,
90 WriteFunc aWriteFunc) const
94 // Get the middle bit of buffer we should be using
95 TPtr8 ptr(&aBuf[aPos], aLength, aLength);
97 // Call the function to actually write the data
98 (this->*aWriteFunc)(ptr);
105 // Give number of octets in tag section
106 void CASN1EncBase::CalculateTagLengthDER()
108 if (iType <= KMaxLowNumberFormTag)
110 // Low tag number form
115 // High tag number form
116 TASN1EncBase128DER encoder(iType);
117 iTagLengthDER = encoder.LengthDER() + 1;
122 // Give number of octets in length section
123 // Only called after contents length is worked out
124 void CASN1EncBase::CalculateLengthLengthDER()
126 iLengthLengthDER = 1;
127 if (iContentsLengthDER > KMaxShortFormLength)
129 // Long form - need number of octets in base256 representation
130 // of length, plus 1.
132 TUint working = iContentsLengthDER;
137 while (working >>= 8);
142 // Writes the tag section into the given buffer
143 void CASN1EncBase::WriteTagDERL(TDes8& aBuf) const
145 // Write class - bits 7-8
146 aBuf[0] = STATIC_CAST(TUint8, iClass << 6);
148 // Write constructed/primitive flag in bit 6
155 if (iType <= KMaxLowNumberFormTag)
161 // First byte signals more to come
164 // The rest we delegate
165 TASN1EncBase128DER encoder(iType);
167 encoder.WriteDERL(aBuf, offset);
172 // Writes the length section into the given buffer
173 void CASN1EncBase::WriteLengthDER(TDes8& aBuf) const
175 if (iContentsLengthDER > KMaxShortFormLength)
177 // First byte is the number of other bytes, with top bit also set
178 TUint octets = iLengthLengthDER - 1;
180 // Cast takes 8 least sig bits
181 aBuf[0] = STATIC_CAST(TUint8, octets);
184 // Other bytes are base 256 representation of length, most significant first
185 TUint working = iContentsLengthDER;
188 // Cast to TUint8 takes 8 least sig bits
189 aBuf[octets] = STATIC_CAST(TUint8, working);
196 // Short form, nice and easy - take least sig 8 bits with a cast
197 aBuf[0] = STATIC_CAST(TUint8, iContentsLengthDER);
202 // Sets the parent (can only do this once)
203 void CASN1EncBase::SetParent(CASN1EncBase* aParent)
205 __ASSERT_ALWAYS(iParent == 0, Panic(KErrAlreadyHasParent));