Update contrib.
2 * Copyright (c) 2002-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 * This file contains the implementation for class encoding bit strings in ASN1 DER.
26 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewLC(const TDesC8& aBitStr)
28 CASN1EncBitString* self = new (ELeave) CASN1EncBitString();
29 CleanupStack::PushL(self);
30 self->ConstructL(aBitStr);
34 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewL(const TDesC8& aBitStr)
36 CASN1EncBitString* self = NewLC(aBitStr);
37 CleanupStack::Pop(self);
41 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewLC(const TDesC8& aBitStr,
44 CASN1EncBitString* self = new (ELeave) CASN1EncBitString();
45 CleanupStack::PushL(self);
46 self->ConstructL(aBitStr, aLengthBits);
50 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewL(const TDesC8& aBitStr,
53 CASN1EncBitString* self = NewLC(aBitStr, aLengthBits);
54 CleanupStack::Pop(self);
58 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewL(const CASN1EncBase& aEncObj)
60 CASN1EncBitString* self = NewLC(aEncObj);
61 CleanupStack::Pop(self);
65 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewLC(const CASN1EncBase& aEncObj)
67 CASN1EncBitString* self = new (ELeave) CASN1EncBitString();
68 CleanupStack::PushL(self);
69 self->ConstructL(aEncObj);
73 EXPORT_C CASN1EncBitString::~CASN1EncBitString()
78 CASN1EncBitString::CASN1EncBitString()
79 : CASN1EncPrimitive(EASN1BitString),
84 void CASN1EncBitString::ConstructL(const TDesC8& aBitStr)
86 iContents = aBitStr.AllocL();
87 CASN1EncPrimitive::ConstructL();
90 void CASN1EncBitString::ConstructL(const TDesC8& aBitStr, TUint aLengthBits)
92 iContents = aBitStr.AllocL();
93 CASN1EncPrimitive::ConstructL();
94 TUint totalBits = aBitStr.Length() * 8;
95 __ASSERT_ALWAYS(aLengthBits <= totalBits, User::Leave(KErrArgument));
96 iPadding = (TUint8)(totalBits - aLengthBits);
98 User::Leave(KErrArgument);
102 * @internalTechnology
103 * Constructs bit string from ASN.1 encoding object.
104 * @param aEncObj ASN.1 encoding object to wrap in bit string.
105 * @note First produces raw DER encoding from the object, then creates
106 * a bit string using other construct function.
108 void CASN1EncBitString::ConstructL(const CASN1EncBase& aEncObj)
110 // produce raw DER encoding from the created ASN.1 encoding
111 TUint len = aEncObj.LengthDER();
112 HBufC8* intDer = HBufC8::NewMaxLC(len);
113 TPtr8 ptrDer = intDer->Des();
115 aEncObj.WriteDERL(ptrDer, pos);
116 // wrap the produced DER encoding into a bit string
119 CleanupStack::PopAndDestroy(intDer);
123 * @internalTechnology
124 * Calculates length of DER-encoded bit string contents. For non empty
125 * bit strings this differs from octet string in the extra leading byte
126 * containing the number of unused bits in the last octet.
128 void CASN1EncBitString::CalculateContentsLengthDER()
130 iContentsLengthDER = iContents->Length();
131 if (iContentsLengthDER > 0)
133 iContentsLengthDER++;
138 * @internalTechnology
139 * Writes DER-encoded bit string contents to aBuf. Prepends
140 * the actual bit string octets with extra octet containing
141 * number of unused bits in the last octet. Before writing,
142 * converts contents of the bit string to big-endian form.
143 * @param aBuf Buffer to write to; must be long enough;
144 * see #CalculateContentsLengthDER method.
146 void CASN1EncBitString::WriteContentsDERL(TDes8& aBuf) const
148 if (iContents->Length() > 0)
151 TUint len = iContents->Length();
152 // We do not need to swap bits, as it is already done.
153 aBuf.Replace(1, len, *iContents);
157 // no padding octet for the empty bit string