|
1 /* |
|
2 * Copyright (c) 1998-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 * This file contains the implementation of the General Time ASN1 class. |
|
16 * Fully support three forms of ASN.1 generalizedTime |
|
17 * Fraction of second accepts 1,2 or 3 digits input. This is to maximize the tolerance of client input |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 #include <asn1dec.h> |
|
23 |
|
24 EXPORT_C TASN1DecGeneralizedTime::TASN1DecGeneralizedTime() |
|
25 { |
|
26 } |
|
27 |
|
28 TTime TASN1DecGeneralizedTime::GetTimeL(const TDesC8& aSource) |
|
29 |
|
30 |
|
31 |
|
32 { |
|
33 // __OFFSET__ Extract corrected time to include offset too. |
|
34 |
|
35 // Did this checking ought to be done on creation rather than when we attempt to get the result? |
|
36 |
|
37 // I guess we ought to check that the contents we've got are long enough to contain a time! |
|
38 if (aSource.Length()<13) |
|
39 { |
|
40 User::Leave(KErrArgument); |
|
41 } |
|
42 |
|
43 // Check all digits the main bit of time are valid, YYYY:MM:DD HH:MM |
|
44 TInt i; |
|
45 for (i=0;i<12;i++) |
|
46 { |
|
47 TUint8 j; |
|
48 j=(TUint8)(aSource[i]-'0'); |
|
49 if (j>=10) |
|
50 { |
|
51 User::Leave(KErrArgument); |
|
52 } |
|
53 } |
|
54 //check seconds are valid if present |
|
55 i = 12; |
|
56 if(aSource.Length() > 13) |
|
57 { |
|
58 if ((aSource[i] < '0') || (aSource[i] > '9') |
|
59 || (aSource[i+1] < '0') || (aSource[i+1] > '9')) |
|
60 User::Leave(KErrArgument); |
|
61 } |
|
62 |
|
63 i = 14; |
|
64 //if the time string is longer |
|
65 if(aSource.Length() > 14) |
|
66 { |
|
67 TInt countDot = 0; |
|
68 TInt countOperator = 0; |
|
69 TInt countZ = 0; |
|
70 |
|
71 //check if the extra bytes are valid |
|
72 while(i < aSource.Length()) |
|
73 { |
|
74 switch(aSource[i]) |
|
75 { |
|
76 case '.': //'.' can't appear more than once and at least one digit should follow |
|
77 countDot++; |
|
78 if(countDot > 1) |
|
79 User::Leave(KErrArgument); |
|
80 |
|
81 if(aSource.Length()-1 == i) //error, '.' is last char in time string |
|
82 User::Leave(KErrArgument); |
|
83 else |
|
84 { |
|
85 if(aSource[i+1] - '0' < 0 || aSource[i+1] - '0' > 9) |
|
86 User::Leave(KErrArgument); |
|
87 } |
|
88 break; |
|
89 |
|
90 case '+': |
|
91 case '-': // '+' and '-' can't appear more than once and at least four digits should follow |
|
92 // After that, we reach either EOF or 'Z', no more char could follow |
|
93 countOperator++; |
|
94 if(countOperator > 1) |
|
95 User::Leave(KErrArgument); |
|
96 |
|
97 if(aSource.Length()-1 < i + 4) //error, not enough digits following operator |
|
98 User::Leave(KErrArgument); |
|
99 else |
|
100 { |
|
101 for(TInt j = i+1; j <= i+4; j++) |
|
102 if(aSource[j] - '0' < 0 || aSource[j] -'0' > 9) |
|
103 User::Leave(KErrArgument); |
|
104 |
|
105 |
|
106 if(aSource.Length() > i + 6)//error, unexpected rubbish |
|
107 User::Leave(KErrArgument); |
|
108 if(aSource.Length() == i + 6 && aSource[i+5] != 'Z') |
|
109 User::Leave(KErrArgument); |
|
110 } |
|
111 |
|
112 |
|
113 break; |
|
114 case 'Z': //'Z' can't appear more than once and must be the last char in time string |
|
115 countZ++; |
|
116 if(countZ > 1) |
|
117 User::Leave(KErrArgument); |
|
118 |
|
119 if(aSource.Length()-1 > i) //error, something following 'Z' |
|
120 User::Leave(KErrArgument); |
|
121 |
|
122 |
|
123 break; |
|
124 default: //so is this a digit? |
|
125 if(aSource[i] -'0' < 0 || aSource[i] -'0' > 9) |
|
126 User::Leave(KErrArgument); |
|
127 } |
|
128 i++; |
|
129 } |
|
130 |
|
131 |
|
132 } |
|
133 // Uh-oh, looks like we're going to have to pull each bit manually and pop it in a TDateTime thing |
|
134 TInt Year,Month,Day,Hour,Minute,Second=0; // Only set seconds to zero 'cos they are optional, everything else is going to be set |
|
135 TInt MicroSecond = 0; |
|
136 TInt offsetIntHours = 0, offsetIntMinutes = 0; |
|
137 TTimeIntervalHours offsetHours = 0; |
|
138 TTimeIntervalMinutes offsetMinutes = 0; |
|
139 Year=(aSource[0]-'0')*1000+(aSource[1]-'0')*100+(aSource[2]-'0')*10+(aSource[3]-'0'); |
|
140 Month=(aSource[4]-'0')*10+aSource[5]-'0'; |
|
141 Month--; // Because the enum for months starts at 0 not 1 |
|
142 Day=(aSource[6]-'0')*10+aSource[7]-'0'; |
|
143 Day--; // Because the enum for days starts at 0 not 1 |
|
144 Hour=(aSource[8]-'0')*10+aSource[9]-'0'; |
|
145 Minute=(aSource[10]-'0')*10+aSource[11]-'0'; |
|
146 TInt Pos=12; |
|
147 |
|
148 |
|
149 if (aSource.Length()>13) |
|
150 { |
|
151 // seconds |
|
152 Second = (aSource[Pos++]-'0')*10; |
|
153 Second += aSource[Pos++]-'0'; |
|
154 |
|
155 } |
|
156 |
|
157 // check range for various components |
|
158 if (Month<EJanuary || Month>EDecember) |
|
159 { |
|
160 User::Leave(KErrArgument); |
|
161 } |
|
162 |
|
163 TMonth month = (TMonth)Month; |
|
164 |
|
165 if ( (Day<0 || Day>=Time::DaysInMonth(Year,month)) || |
|
166 (Hour<0 || Hour>=24) || |
|
167 (Minute<0 || Minute>=60) || |
|
168 (Second<0 || Second>=60) ) |
|
169 { |
|
170 User::Leave(KErrArgument); |
|
171 } |
|
172 |
|
173 //optional 3 bytes fraction of seconds |
|
174 //Converts the fraction of seconds to microsecond |
|
175 if(aSource.Length() > Pos && aSource[Pos] == '.') |
|
176 { |
|
177 Pos++; |
|
178 |
|
179 TInt multiplier = 100000; |
|
180 |
|
181 // parse fraction of second |
|
182 while(Pos <= 17 && Pos < aSource.Length()) |
|
183 { |
|
184 if(!((aSource[Pos] >= '0') && (aSource[Pos] <= '9'))) |
|
185 break; //fraction ended |
|
186 |
|
187 MicroSecond += (aSource[Pos++]-'0') * multiplier; |
|
188 multiplier /= 10; |
|
189 |
|
190 } |
|
191 |
|
192 } |
|
193 |
|
194 if (aSource.Length()>Pos) |
|
195 { |
|
196 TBool aheadUTC = EFalse; |
|
197 switch (aSource[Pos]) |
|
198 { |
|
199 case 'Z': |
|
200 // Zulu - nothing more to do |
|
201 break; |
|
202 case '+': |
|
203 //'+' indicates local time is ahead of UTC, need to substract the offset later |
|
204 aheadUTC = ETrue; |
|
205 // The case fall-through is intentional, both cases have the same functionality, with a different |
|
206 // aheadUTC value |
|
207 case '-': |
|
208 {//brackets are required to prevent armv5 warning and gccxml errors |
|
209 |
|
210 |
|
211 //'-' indicates local time is behind of UTC, need to add the offset later |
|
212 //if we are in here with '-', aheadUTC has already been set to the correct value |
|
213 |
|
214 Pos++; |
|
215 |
|
216 |
|
217 //parse |
|
218 TInt multiplier = 10; |
|
219 for(TInt i = 0; i <= 3; i++) |
|
220 { |
|
221 if(i < 2) |
|
222 offsetIntHours += (aSource[Pos + i]-'0') * multiplier; |
|
223 else |
|
224 offsetIntMinutes += (aSource[Pos + i]-'0') * (TInt)multiplier; |
|
225 |
|
226 multiplier = (multiplier == 10)? 1 : 10; |
|
227 |
|
228 |
|
229 |
|
230 } |
|
231 |
|
232 //expected range, |
|
233 //offset hour range: 0-23 |
|
234 //offset minute range: 0-59 |
|
235 if(offsetIntHours < 0 || offsetIntHours > 23 || |
|
236 offsetIntMinutes < 0 || offsetIntMinutes > 59) |
|
237 User::Leave(KErrArgument); |
|
238 |
|
239 offsetHours = aheadUTC ? -offsetIntHours : offsetIntHours ; |
|
240 offsetMinutes = aheadUTC ? -offsetIntMinutes : offsetIntMinutes; |
|
241 |
|
242 break; |
|
243 } |
|
244 default: |
|
245 // Error! |
|
246 User::Leave(KErrArgument); |
|
247 break; |
|
248 } |
|
249 } |
|
250 |
|
251 |
|
252 |
|
253 TDateTime D(Year, month,Day,Hour,Minute,Second,MicroSecond); |
|
254 |
|
255 TTime T(D); |
|
256 T = T + offsetHours; |
|
257 T = T + offsetMinutes; |
|
258 return T; |
|
259 } |
|
260 |
|
261 EXPORT_C TTime TASN1DecGeneralizedTime::DecodeDERL(const TDesC8& aSource,TInt& aPos) |
|
262 |
|
263 { |
|
264 TPtrC8 Source=aSource.Mid(aPos); |
|
265 TASN1DecGeneric gen(Source); |
|
266 gen.InitL(); |
|
267 TTime t = GetTimeL(gen.GetContentDER()); |
|
268 aPos+=gen.LengthDER(); |
|
269 return t; |
|
270 } |
|
271 |
|
272 EXPORT_C TTime TASN1DecGeneralizedTime::DecodeDERL(const TASN1DecGeneric& aGen) |
|
273 |
|
274 { |
|
275 return GetTimeL(aGen.GetContentDER()); |
|
276 } |