|
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 class encoding TInt in ASN1 DER |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include <asn1enc.h> |
|
21 |
|
22 EXPORT_C CASN1EncInt* CASN1EncInt::NewLC(const TInt aInt) |
|
23 { |
|
24 CASN1EncInt* self = new (ELeave) CASN1EncInt(aInt); |
|
25 CleanupStack::PushL(self); |
|
26 self->ConstructL(); |
|
27 return self; |
|
28 } |
|
29 |
|
30 EXPORT_C CASN1EncInt* CASN1EncInt::NewL(const TInt aInt) |
|
31 { |
|
32 CASN1EncInt* self = NewLC(aInt); |
|
33 CleanupStack::Pop(self); |
|
34 return self; |
|
35 } |
|
36 |
|
37 CASN1EncInt::CASN1EncInt(const TInt aInt) |
|
38 : CASN1EncPrimitive(EASN1Integer), iInt(aInt) |
|
39 { |
|
40 } |
|
41 |
|
42 void CASN1EncInt::CalculateContentsLengthDER() |
|
43 { |
|
44 iContentsLengthDER = 1; |
|
45 |
|
46 // Flip bits in -ve numbers |
|
47 TUint working = iInt < 0 ? ~iInt : iInt; |
|
48 TUint last = working; |
|
49 while (working >>= 8) |
|
50 { |
|
51 last = working; |
|
52 ++iContentsLengthDER; |
|
53 } |
|
54 |
|
55 // If most sig non-zero byte had a 1 top bit, we'll need the next byte too |
|
56 // (applies to both +ve and -ve cases) |
|
57 if (last & 0x80) |
|
58 { |
|
59 ++iContentsLengthDER; |
|
60 } |
|
61 } |
|
62 |
|
63 |
|
64 void CASN1EncInt::WriteContentsDERL(TDes8& aBuf) const |
|
65 { |
|
66 // Unsigned, so operator>>= works sensibly |
|
67 TUint working = iInt; |
|
68 TUint position = iContentsLengthDER; |
|
69 while (position) |
|
70 { |
|
71 // Cast takes least sig 8 bits |
|
72 aBuf[--position] = STATIC_CAST(TUint8, working); |
|
73 working >>= 8; |
|
74 } |
|
75 } |