|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Name : TTlv.cpp |
|
15 // Part of : Series 60 / utility |
|
16 // Based on 3GPP TS 11.14 V8.8.0 |
|
17 // Version : 1.0 |
|
18 // EXTERNAL RESOURCES |
|
19 // |
|
20 |
|
21 |
|
22 |
|
23 // Include Files |
|
24 #include "TTlv.h" // Header of this class |
|
25 #include "tflogger.h" // For logging |
|
26 |
|
27 // External Data Structures |
|
28 // none |
|
29 |
|
30 // External Function Prototypes |
|
31 // none |
|
32 |
|
33 // LOCAL CONSTANTS AND MACROS |
|
34 // none |
|
35 |
|
36 // MODULE DATA STRUCTURES |
|
37 // none |
|
38 |
|
39 // Local Data Structures |
|
40 // none |
|
41 |
|
42 // Local Function Prototypes |
|
43 // none |
|
44 |
|
45 // LOCAL FUNCTIONS |
|
46 // none |
|
47 |
|
48 // MEMBER FUNCTIONS |
|
49 |
|
50 |
|
51 //============================================================================= |
|
52 |
|
53 |
|
54 |
|
55 // ----------------------------------------------------------------------------- |
|
56 // TTlvBase::TTlvBase |
|
57 // constructor |
|
58 // ----------------------------------------------------------------------------- |
|
59 // |
|
60 EXPORT_C TTlvBase::TTlvBase |
|
61 ( |
|
62 TDes8& aData |
|
63 ) : iData(aData) |
|
64 { |
|
65 // Do nothing. |
|
66 } |
|
67 |
|
68 // ----------------------------------------------------------------------------- |
|
69 // TTlvBase::Begin |
|
70 // Start BER-TLV construction |
|
71 // ----------------------------------------------------------------------------- |
|
72 // |
|
73 EXPORT_C void TTlvBase::Begin |
|
74 ( |
|
75 TUint8 aTag |
|
76 ) |
|
77 { |
|
78 TFLOGSTRING("UTILITY: TTlvBase::Begin"); |
|
79 iData.Zero(); |
|
80 iData.Append( aTag ); |
|
81 iData.Append( 0 ); |
|
82 } |
|
83 |
|
84 // ----------------------------------------------------------------------------- |
|
85 // TTlvBase::AddTag |
|
86 // Start new simple TLV |
|
87 // ----------------------------------------------------------------------------- |
|
88 // |
|
89 EXPORT_C void TTlvBase::AddTag |
|
90 ( |
|
91 TUint8 aTag |
|
92 ) |
|
93 { |
|
94 TFLOGSTRING("UTILITY: TTlvBase::AddTag"); |
|
95 iData.Append( aTag ); // tag |
|
96 iData.Append( 0 ); // length |
|
97 iLenIndex = iData.Length() - 1; // index to tag length |
|
98 } |
|
99 |
|
100 // ----------------------------------------------------------------------------- |
|
101 // TTlvBase::AddByte |
|
102 // Add byte data to simple TLV |
|
103 // ----------------------------------------------------------------------------- |
|
104 // |
|
105 EXPORT_C void TTlvBase::AddByte |
|
106 ( |
|
107 TUint8 aValue |
|
108 ) |
|
109 { |
|
110 TFLOGSTRING("UTILITY: TTlvBase::AddByte"); |
|
111 iData.Append( aValue ); |
|
112 iData[iLenIndex]++; |
|
113 } |
|
114 |
|
115 // ----------------------------------------------------------------------------- |
|
116 // TTlvBase::AddData |
|
117 // Add desctiptor data to simple TLV |
|
118 // ----------------------------------------------------------------------------- |
|
119 // |
|
120 EXPORT_C void TTlvBase::AddData |
|
121 ( |
|
122 const TDesC8& aValue |
|
123 ) |
|
124 { |
|
125 TFLOGSTRING("UTILITY: TTlvBase::AddData"); |
|
126 iData.Append( aValue ); |
|
127 iData[ iLenIndex ] = static_cast<TUint8>( iData[ iLenIndex ] + |
|
128 aValue.Length() ); |
|
129 } |
|
130 |
|
131 // ----------------------------------------------------------------------------- |
|
132 // TTlvBase::End |
|
133 // End BER-TLV construction. Adds extra length byte to all simple TLVs that |
|
134 // are longer than KMaxOneByteLength (0x7f) and returns descriptor to the new |
|
135 // valid BER-TLV. |
|
136 // ----------------------------------------------------------------------------- |
|
137 // |
|
138 EXPORT_C const TDesC8& TTlvBase::End |
|
139 ( |
|
140 // None |
|
141 ) |
|
142 { |
|
143 TFLOGSTRING("UTILITY: TTlvBase::End"); |
|
144 _LIT8( KLenTag, "\x81" ); |
|
145 |
|
146 TInt i( 3 ); // Index to tag length |
|
147 |
|
148 // Scan through all lenghts and insert 'length tag' |
|
149 while ( i < iData.Length() ) |
|
150 { |
|
151 if ( iData[i] > KMaxOneByteLength ) |
|
152 { |
|
153 iData.Insert( i, KLenTag ); |
|
154 i++; |
|
155 } |
|
156 i += iData[i] + 2; // jump to next tag length |
|
157 } |
|
158 |
|
159 // Calculate total BER-TLV length |
|
160 iData[1] = static_cast<TUint8>( iData.Length() - 2 ); |
|
161 // Insert 'length tag' |
|
162 if ( iData[1] > KMaxOneByteLength ) |
|
163 { |
|
164 iData.Insert( 1, KLenTag ); |
|
165 } |
|
166 |
|
167 // Done, return BER-TLV |
|
168 return iData; |
|
169 } |
|
170 |
|
171 // ----------------------------------------------------------------------------- |
|
172 // TTlvBase::GetDataWithoutTopLevelTag |
|
173 // End TLV construction. Adds extra length byte to all simple TLVs that are |
|
174 // longer than KMaxOneByteLength (0x7f) and returns descriptor to the valid |
|
175 // TLVs. To be used without Begin(), nor End() in situations where TLV simple |
|
176 // tags are used without top level tag. |
|
177 // ----------------------------------------------------------------------------- |
|
178 // |
|
179 EXPORT_C const TDesC8& TTlvBase::GetDataWithoutTopLevelTag |
|
180 ( |
|
181 // None |
|
182 ) |
|
183 { |
|
184 TFLOGSTRING("UTILITY: TTlvBase::GetDataWithoutTopLevelTag"); |
|
185 _LIT8( KLenTag, "\x81" ); |
|
186 |
|
187 TInt i( 1 ); // Index to tag length |
|
188 |
|
189 // Scan through all lenghts and insert 'length tag' |
|
190 while ( i < iData.Length() ) |
|
191 { |
|
192 if ( iData[i] > KMaxOneByteLength ) |
|
193 { |
|
194 iData.Insert( i, KLenTag ); |
|
195 i++; |
|
196 } |
|
197 i += iData[i] + 2; // Jump to next tag length |
|
198 } |
|
199 |
|
200 // Done, return BER-TLV |
|
201 return iData; |
|
202 |
|
203 } |
|
204 |
|
205 //End Of File |