blob: 6236d2af95d94fdd0b9c6d7eb3feeee60e0d0215 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina460ba32004-10-20 15:40:04 +00005#define _POSIX_PTHREAD_SEMANTICS /* for Sun */
6#define _REENTRANT /* for Sun */
Lev Walkina9cc46e2004-09-22 16:06:28 +00007#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008#include <GeneralizedTime.h>
9#include <time.h>
10#include <errno.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000011
Lev Walkin2a1646d2004-09-04 04:43:28 +000012#if defined(WIN32)
Lev Walkina57953d2005-06-15 19:01:45 +000013#pragma message( "PLEASE STOP AND READ!")
14#pragma message( " localtime_r is implemented via localtime(), which may be not thread-safe.")
15#pragma message( " gmtime_r is implemented via gmtime(), which may be not thread-safe.")
16#pragma message( " ")
17#pragma message( " You must fix the code by inserting appropriate locking")
18#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
Lev Walkinc8c2cb52006-07-13 13:20:19 +000019#pragma message( "PLEASE STOP AND READ!")
Lev Walkin02a31552004-08-12 03:52:53 +000020
Lev Walkincec43b52004-09-02 05:20:39 +000021static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000022 struct tm *tm;
23 if((tm = localtime(tloc)))
24 return memcpy(result, tm, sizeof(struct tm));
25 return 0;
26}
27
Lev Walkincec43b52004-09-02 05:20:39 +000028static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000029 struct tm *tm;
30 if((tm = gmtime(tloc)))
31 return memcpy(result, tm, sizeof(struct tm));
32 return 0;
33}
34
Lev Walkin93e9fe32004-09-17 06:46:10 +000035#define tzset() _tzset()
Lev Walkina57953d2005-06-15 19:01:45 +000036#define putenv(c) _putenv(c)
Lev Walkin93e9fe32004-09-17 06:46:10 +000037#define _EMULATE_TIMEGM
38
39#endif /* WIN32 */
40
Lev Walkinc8c2cb52006-07-13 13:20:19 +000041#if defined(sun) || defined(_sun_) || defined(__solaris__)
Lev Walkina460ba32004-10-20 15:40:04 +000042#define _EMULATE_TIMEGM
43#endif
44
Lev Walkin93e9fe32004-09-17 06:46:10 +000045/*
46 * Where to look for offset from GMT, Phase I.
47 * Several platforms are known.
48 */
49#if defined(__FreeBSD__) \
50 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
51 || (defined __GLIBC__ && __GLIBC__ >= 2)
52#undef HAVE_TM_GMTOFF
53#define HAVE_TM_GMTOFF
54#endif /* BSDs and newer glibc */
55
56/*
57 * Where to look for offset from GMT, Phase II.
58 */
59#ifdef HAVE_TM_GMTOFF
60#define GMTOFF(tm) ((tm).tm_gmtoff)
61#else /* HAVE_TM_GMTOFF */
62#define GMTOFF(tm) (-timezone)
63#endif /* HAVE_TM_GMTOFF */
64
Lev Walkinc8c2cb52006-07-13 13:20:19 +000065#if (defined(_EMULATE_TIMEGM) || !defined(HAVE_TM_GMTOFF))
66#warning "PLEASE STOP AND READ!"
67#warning " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe."
68#warning " "
69#warning " You must fix the code by inserting appropriate locking"
70#warning " if you want to use asn_GT2time() or asn_UT2time()."
71#warning "PLEASE STOP AND READ!"
72#endif /* _EMULATE_TIMEGM */
73
Lev Walkin93e9fe32004-09-17 06:46:10 +000074/*
75 * Override our GMTOFF decision for other known platforms.
76 */
77#ifdef __CYGWIN__
78#undef GMTOFF
79static long GMTOFF(struct tm a){
80 struct tm *lt;
81 time_t local_time, gmt_time;
82 long zone;
83
84 tzset();
85 gmt_time = time (NULL);
86
87 lt = gmtime(&gmt_time);
88
89 local_time = mktime(lt);
90 return (gmt_time - local_time);
91}
92#define _EMULATE_TIMEGM
93
94#endif /* __CYGWIN__ */
95
Lev Walkinc8c2cb52006-07-13 13:20:19 +000096#define ATZVARS do { \
97 char tzoldbuf[64]; \
98 char *tzold
99#define ATZSAVETZ do { \
100 tzold = getenv("TZ"); \
101 if(tzold) { \
102 size_t tzlen = strlen(tzold); \
103 if(tzlen < sizeof(tzoldbuf)) \
104 tzold = memcpy(tzoldbuf, tzold, tzlen + 1); \
105 else \
106 tzold = strdup(tzold); /* Ignore error */ \
107 setenv("TZ", "UTC", 1); \
108 } \
109 tzset(); \
110} while(0)
111#define ATZOLDTZ do { \
112 if (tzold) { \
113 setenv("TZ", tzold, 1); \
114 *tzoldbuf = 0; \
115 if(tzold != tzoldbuf) \
116 free(tzold); \
117 } else { \
118 unsetenv("TZ"); \
119 } \
120 tzset(); \
121} while(0); } while(0);
122
Lev Walkin93e9fe32004-09-17 06:46:10 +0000123#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +0000124static time_t timegm(struct tm *tm) {
125 time_t tloc;
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000126 ATZVARS;
127 ATZSAVETZ;
Lev Walkindb424002004-08-12 03:58:25 +0000128 tloc = mktime(tm);
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000129 ATZOLDTZ;
Lev Walkindb424002004-08-12 03:58:25 +0000130 return tloc;
131}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000132#endif /* _EMULATE_TIMEGM */
Lev Walkindb424002004-08-12 03:58:25 +0000133
Lev Walkin64399722004-08-11 07:17:22 +0000134
Lev Walkin535612a2005-07-03 05:32:40 +0000135#ifndef __ASN_INTERNAL_TEST_MODE__
Lev Walkinf15320b2004-06-03 03:38:44 +0000136
137/*
138 * GeneralizedTime basic type description.
139 */
Lev Walkin5e033762004-09-29 13:26:15 +0000140static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000141 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
142 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
143 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000144};
Lev Walkin5e033762004-09-29 13:26:15 +0000145asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 "GeneralizedTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +0000147 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000148 OCTET_STRING_free,
149 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 GeneralizedTime_constraint, /* Check validity of time */
151 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000152 GeneralizedTime_encode_der,
153 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000154 GeneralizedTime_encode_xer,
Lev Walkin59b176e2005-11-26 11:25:14 +0000155 0,
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000157 asn_DEF_GeneralizedTime_tags,
158 sizeof(asn_DEF_GeneralizedTime_tags)
159 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
160 asn_DEF_GeneralizedTime_tags,
161 sizeof(asn_DEF_GeneralizedTime_tags)
162 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +0000163 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +0000164 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000165 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000166};
167
Lev Walkin535612a2005-07-03 05:32:40 +0000168#endif /* __ASN_INTERNAL_TEST_MODE__ */
Lev Walkinf15320b2004-06-03 03:38:44 +0000169
170/*
171 * Check that the time looks like the time.
172 */
173int
Lev Walkin5e033762004-09-29 13:26:15 +0000174GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000175 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000176 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 time_t tloc;
178
179 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000180 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 if(tloc == -1 && errno != EPERM) {
Lev Walkin1eded352006-07-13 11:19:01 +0000182 _ASN_CTFAIL(app_key, td,
Lev Walkin16835b62004-08-22 13:47:59 +0000183 "%s: Invalid time format: %s (%s:%d)",
184 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 return -1;
186 }
187
188 return 0;
189}
190
Lev Walkina9cc46e2004-09-22 16:06:28 +0000191asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000192GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000193 int tag_mode, ber_tlv_tag_t tag,
194 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000195 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000196 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000197 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000198 struct tm tm;
199 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000200
Lev Walkin535612a2005-07-03 05:32:40 +0000201 /*
202 * Encode as a canonical DER.
203 */
204 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000205 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000206 if(tloc == -1 && errno != EPERM)
207 /* Failed to recognize time. Fail completely. */
208 _ASN_ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000209
Lev Walkin3a522782005-07-04 12:21:51 +0000210 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin535612a2005-07-03 05:32:40 +0000211 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000212
213 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
214
Lev Walkin535612a2005-07-03 05:32:40 +0000215 FREEMEM(st->buf);
216 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000217
218 return erval;
219}
220
Lev Walkin535612a2005-07-03 05:32:40 +0000221#ifndef __ASN_INTERNAL_TEST_MODE__
222
Lev Walkina9cc46e2004-09-22 16:06:28 +0000223asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000224GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000225 int ilevel, enum xer_encoder_flags_e flags,
226 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000227
228 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000229 GeneralizedTime_t *gt;
230 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000231 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000232 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000233
234 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000235 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000236 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000237 && errno != EPERM)
238 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000239
Lev Walkin3a522782005-07-04 12:21:51 +0000240 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin535612a2005-07-03 05:32:40 +0000241 if(!gt) _ASN_ENCODE_FAILED;
242
243 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
244 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000245 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000246 return rv;
247 } else {
248 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
249 cb, app_key);
250 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000251}
252
Lev Walkin535612a2005-07-03 05:32:40 +0000253#endif /* __ASN_INTERNAL_TEST_MODE__ */
254
Lev Walkinf15320b2004-06-03 03:38:44 +0000255int
Lev Walkin5e033762004-09-29 13:26:15 +0000256GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000258 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000259
Lev Walkind9bd7752004-06-05 08:17:50 +0000260 (void)td; /* Unused argument */
261 (void)ilevel; /* Unused argument */
262
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 if(st && st->buf) {
264 char buf[32];
265 struct tm tm;
266 int ret;
267
268 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000269 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000270 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000271
272 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000273 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000274 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
275 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000276 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000277 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000278 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000279 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000280 }
281}
282
Lev Walkinf15320b2004-06-03 03:38:44 +0000283time_t
Lev Walkin99006362004-08-07 03:52:26 +0000284asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000285 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
286}
287
288time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000289asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
290 time_t tloc;
291 int fv, fd = 0;
292
293 if(frac_value)
294 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
295 else
296 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
297 if(fd == 0 || frac_digits <= 0) {
298 *frac_value = 0;
299 } else {
300 while(fd > frac_digits)
301 fv /= 10, fd--;
302 while(fd < frac_digits) {
303 int new_fv = fv * 10;
304 if(new_fv / 10 != fv) {
305 /* Too long precision request */
306 fv = 0;
307 break;
308 }
309 fv = new_fv, fd++;
310 }
311
312 *frac_value = fv;
313 }
314
315 return tloc;
316}
317
318time_t
319asn_GT2time_frac(const GeneralizedTime_t *st, int *frac_value, int *frac_digits, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000320 struct tm tm_s;
321 uint8_t *buf;
322 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000323 int gmtoff_h = 0;
324 int gmtoff_m = 0;
325 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000326 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000327 int fvalue = 0;
328 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000329 time_t tloc;
330
331 if(!st || !st->buf) {
332 errno = EINVAL;
333 return -1;
334 } else {
335 buf = st->buf;
336 end = buf + st->size;
337 }
338
339 if(st->size < 10) {
340 errno = EINVAL;
341 return -1;
342 }
343
344 /*
345 * Decode first 10 bytes: "AAAAMMJJhh"
346 */
347 memset(&tm_s, 0, sizeof(tm_s));
348#undef B2F
349#undef B2T
350#define B2F(var) do { \
351 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000352 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000353 errno = EINVAL; \
354 return -1; \
355 } else { \
356 var = var * 10 + (ch - 0x30); \
357 buf++; \
358 } \
359 } while(0)
360#define B2T(var) B2F(tm_s.var)
361
362 B2T(tm_year); /* 1: A */
363 B2T(tm_year); /* 2: A */
364 B2T(tm_year); /* 3: A */
365 B2T(tm_year); /* 4: A */
366 B2T(tm_mon); /* 5: M */
367 B2T(tm_mon); /* 6: M */
368 B2T(tm_mday); /* 7: J */
369 B2T(tm_mday); /* 8: J */
370 B2T(tm_hour); /* 9: h */
371 B2T(tm_hour); /* 0: h */
372
373 if(buf == end) goto local_finish;
374
375 /*
376 * Parse [mm[ss[(.|,)ffff]]]
377 * ^^
378 */
379 switch(*buf) {
380 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
381 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
382 tm_s.tm_min = (*buf++) - 0x30;
383 if(buf == end) { errno = EINVAL; return -1; }
384 B2T(tm_min);
385 break;
386 case 0x2B: case 0x2D: /* +, - */
387 goto offset;
388 case 0x5A: /* Z */
389 goto utc_finish;
390 default:
391 errno = EINVAL;
392 return -1;
393 }
394
395 if(buf == end) goto local_finish;
396
397 /*
398 * Parse [mm[ss[(.|,)ffff]]]
399 * ^^
400 */
401 switch(*buf) {
402 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
403 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
404 tm_s.tm_sec = (*buf++) - 0x30;
405 if(buf == end) { errno = EINVAL; return -1; }
406 B2T(tm_sec);
407 break;
408 case 0x2B: case 0x2D: /* +, - */
409 goto offset;
410 case 0x5A: /* Z */
411 goto utc_finish;
412 default:
413 errno = EINVAL;
414 return -1;
415 }
416
417 if(buf == end) goto local_finish;
418
419 /*
420 * Parse [mm[ss[(.|,)ffff]]]
421 * ^ ^
422 */
423 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000424 case 0x2C: case 0x2E: /* (.|,) */
425 /*
426 * Process fractions of seconds.
427 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000429 int v = *buf;
Lev Walkin3a522782005-07-04 12:21:51 +0000430 int new_fvalue;
Lev Walkin535612a2005-07-03 05:32:40 +0000431 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
433 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin3a522782005-07-04 12:21:51 +0000434 new_fvalue = fvalue * 10 + (v - 0x30);
435 if(new_fvalue / 10 != fvalue) {
Lev Walkin535612a2005-07-03 05:32:40 +0000436 /* Not enough precision, ignore */
437 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000438 fvalue = new_fvalue;
439 fdigits++;
Lev Walkin535612a2005-07-03 05:32:40 +0000440 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 continue;
442 default:
443 break;
444 }
445 break;
446 }
447 }
448
449 if(buf == end) goto local_finish;
450
451 switch(*buf) {
452 case 0x2B: case 0x2D: /* +, - */
453 goto offset;
454 case 0x5A: /* Z */
455 goto utc_finish;
456 default:
457 errno = EINVAL;
458 return -1;
459 }
460
461
462offset:
463
464 if(end - buf < 3) {
465 errno = EINVAL;
466 return -1;
467 }
468 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000469 B2F(gmtoff_h);
470 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000471 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000472 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 else
Lev Walkin99006362004-08-07 03:52:26 +0000474 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000475
476 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000477 B2F(gmtoff_m);
478 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000479 } else if(end != buf) {
480 errno = EINVAL;
481 return -1;
482 }
483
Lev Walkin99006362004-08-07 03:52:26 +0000484 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000485
486 /* Fall through */
487utc_finish:
488
489 offset_specified = 1;
490
491 /* Fall through */
492local_finish:
493
494 /*
495 * Validation.
496 */
497 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
498 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
499 || (tm_s.tm_hour > 23)
500 || (tm_s.tm_sec > 60)
501 ) {
502 errno = EINVAL;
503 return -1;
504 }
505
506 /* Canonicalize */
507 tm_s.tm_mon -= 1; /* 0 - 11 */
508 tm_s.tm_year -= 1900;
509 tm_s.tm_isdst = -1;
510
Lev Walkin99006362004-08-07 03:52:26 +0000511 tm_s.tm_sec -= gmtoff;
512
513 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
514
Lev Walkin02a31552004-08-12 03:52:53 +0000515 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000516 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000517 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000518 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000519 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000520 * we can only guess that it is a local zone.
521 * Interpret it in this fashion.
522 */
523 tloc = mktime(&tm_s);
524 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000525 if(tloc == -1) {
526 errno = EINVAL;
527 return -1;
528 }
529
Lev Walkin99006362004-08-07 03:52:26 +0000530 if(ret_tm) {
531 if(as_gmt) {
532 if(offset_specified) {
533 *ret_tm = tm_s;
534 } else {
535 if(gmtime_r(&tloc, ret_tm) == 0) {
536 errno = EINVAL;
537 return -1;
538 }
539 }
540 } else {
541 if(localtime_r(&tloc, ret_tm) == 0) {
542 errno = EINVAL;
543 return -1;
544 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000545 }
546 }
547
Lev Walkin535612a2005-07-03 05:32:40 +0000548 /* Fractions of seconds */
549 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000550 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000551
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 return tloc;
553}
554
Lev Walkin99006362004-08-07 03:52:26 +0000555GeneralizedTime_t *
556asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000557 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
558}
559
560GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000561asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, int frac_value, int frac_digits, int force_gmt) {
Lev Walkin99006362004-08-07 03:52:26 +0000562 struct tm tm_s;
563 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000564 const unsigned int buf_size =
565 4 + 2 + 2 /* yyyymmdd */
566 + 2 + 2 + 2 /* hhmmss */
567 + 1 + 6 /* .ffffff */
568 + 1 + 4 /* +hhmm */
569 + 1 /* '\0' */
570 ;
Lev Walkin99006362004-08-07 03:52:26 +0000571 char *buf;
572 char *p;
573 int size;
574
575 /* Check arguments */
576 if(!tm) {
577 errno = EINVAL;
578 return 0;
579 }
580
581 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000582 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000583 if(!buf) return 0;
584
585 gmtoff = GMTOFF(*tm);
586
587 if(force_gmt && gmtoff) {
588 tm_s = *tm;
589 tm_s.tm_sec -= gmtoff;
590 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000591 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000592#ifdef HAVE_TM_GMTOFF
593 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000594#else /* !HAVE_TM_GMTOFF */
595 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000596#endif
Lev Walkin99006362004-08-07 03:52:26 +0000597 }
598
599 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
600 tm->tm_year + 1900,
601 tm->tm_mon + 1,
602 tm->tm_mday,
603 tm->tm_hour,
604 tm->tm_min,
605 tm->tm_sec
606 );
Lev Walkin535612a2005-07-03 05:32:40 +0000607 if(size != 14) {
608 /* Could be assert(size == 14); */
609 FREEMEM(buf);
610 errno = EINVAL;
611 return 0;
612 }
Lev Walkin99006362004-08-07 03:52:26 +0000613
614 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000615
616 /*
617 * Deal with fractions.
618 */
Lev Walkin3a522782005-07-04 12:21:51 +0000619 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000620 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000621 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000622 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000623 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000624
625 /* Place bounds on precision */
626 while(frac_digits-- > 6)
627 frac_value /= 10;
628
629 /* emulate fbase = pow(10, frac_digits) */
630 for(fbase = 1; frac_digits--;)
631 fbase *= 10;
632
Lev Walkin535612a2005-07-03 05:32:40 +0000633 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000634 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000635 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000636 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000637 frac_value %= fbase;
638 fbase /= 10;
639 } while(fbase > 0 && frac_value > 0 && z < end);
640 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000641 for(--z; *z == 0x30; --z); /* Strip zeroes */
642 p = z + (*z != '.');
643 size = p - buf;
644 }
Lev Walkin535612a2005-07-03 05:32:40 +0000645 }
646
Lev Walkin99006362004-08-07 03:52:26 +0000647 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000648 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000649 *p++ = 0;
650 size++;
651 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000652 int ret;
653 gmtoff %= 86400;
654 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
655 gmtoff / 3600, labs(gmtoff % 3600));
656 if(ret != 5) {
657 FREEMEM(buf);
658 errno = EINVAL;
659 return 0;
660 }
Lev Walkin99006362004-08-07 03:52:26 +0000661 size += ret;
662 }
663
664 if(opt_gt) {
665 if(opt_gt->buf)
666 FREEMEM(opt_gt->buf);
667 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000668 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin99006362004-08-07 03:52:26 +0000669 if(!opt_gt) { free(buf); return 0; }
670 }
671
Lev Walkin4d9528c2004-08-11 08:10:13 +0000672 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000673 opt_gt->size = size;
674
675 return opt_gt;
676}
677
678