blob: 9103455dd22d79633158fc66b38dfd19ed2d7fc1 [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().")
19#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 Walkina460ba32004-10-20 15:40:04 +000041#if defined(sun)
42#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
65/*
66 * Override our GMTOFF decision for other known platforms.
67 */
68#ifdef __CYGWIN__
69#undef GMTOFF
70static long GMTOFF(struct tm a){
71 struct tm *lt;
72 time_t local_time, gmt_time;
73 long zone;
74
75 tzset();
76 gmt_time = time (NULL);
77
78 lt = gmtime(&gmt_time);
79
80 local_time = mktime(lt);
81 return (gmt_time - local_time);
82}
83#define _EMULATE_TIMEGM
84
85#endif /* __CYGWIN__ */
86
87#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +000088static time_t timegm(struct tm *tm) {
89 time_t tloc;
90 char *tz;
Lev Walkine730f2b2004-08-12 07:47:03 +000091 char *buf;
Lev Walkindb424002004-08-12 03:58:25 +000092
93 tz = getenv("TZ");
Lev Walkina460ba32004-10-20 15:40:04 +000094 putenv("TZ=UTC");
Lev Walkin93e9fe32004-09-17 06:46:10 +000095 tzset();
Lev Walkindb424002004-08-12 03:58:25 +000096 tloc = mktime(tm);
Lev Walkine730f2b2004-08-12 07:47:03 +000097 if (tz) {
Lev Walkin93e9fe32004-09-17 06:46:10 +000098 int bufsize = strlen(tz) + 4;
99 buf = alloca(bufsize);
100 snprintf(buf, bufsize, "TZ=%s", tz);
Lev Walkine730f2b2004-08-12 07:47:03 +0000101 } else {
102 buf = "TZ=";
103 }
Lev Walkina460ba32004-10-20 15:40:04 +0000104 putenv(buf);
Lev Walkin93e9fe32004-09-17 06:46:10 +0000105 tzset();
Lev Walkindb424002004-08-12 03:58:25 +0000106 return tloc;
107}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000108#endif /* _EMULATE_TIMEGM */
Lev Walkindb424002004-08-12 03:58:25 +0000109
Lev Walkin64399722004-08-11 07:17:22 +0000110
Lev Walkin535612a2005-07-03 05:32:40 +0000111#ifndef __ASN_INTERNAL_TEST_MODE__
Lev Walkinf15320b2004-06-03 03:38:44 +0000112
113/*
114 * GeneralizedTime basic type description.
115 */
Lev Walkin5e033762004-09-29 13:26:15 +0000116static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000117 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
118 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
119 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000120};
Lev Walkin5e033762004-09-29 13:26:15 +0000121asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 "GeneralizedTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +0000123 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000124 OCTET_STRING_free,
125 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 GeneralizedTime_constraint, /* Check validity of time */
127 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000128 GeneralizedTime_encode_der,
129 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000130 GeneralizedTime_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +0000131 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000132 asn_DEF_GeneralizedTime_tags,
133 sizeof(asn_DEF_GeneralizedTime_tags)
134 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
135 asn_DEF_GeneralizedTime_tags,
136 sizeof(asn_DEF_GeneralizedTime_tags)
137 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +0000138 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000139 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000140};
141
Lev Walkin535612a2005-07-03 05:32:40 +0000142#endif /* __ASN_INTERNAL_TEST_MODE__ */
Lev Walkinf15320b2004-06-03 03:38:44 +0000143
144/*
145 * Check that the time looks like the time.
146 */
147int
Lev Walkin5e033762004-09-29 13:26:15 +0000148GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000150 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 time_t tloc;
152
153 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000154 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000156 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000157 "%s: Invalid time format: %s (%s:%d)",
158 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000159 return -1;
160 }
161
162 return 0;
163}
164
Lev Walkina9cc46e2004-09-22 16:06:28 +0000165asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000166GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000167 int tag_mode, ber_tlv_tag_t tag,
168 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000169 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000170 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000171 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000172 struct tm tm;
173 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000174
Lev Walkin535612a2005-07-03 05:32:40 +0000175 /*
176 * Encode as a canonical DER.
177 */
178 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000179 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000180 if(tloc == -1 && errno != EPERM)
181 /* Failed to recognize time. Fail completely. */
182 _ASN_ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000183
Lev Walkin3a522782005-07-04 12:21:51 +0000184 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin535612a2005-07-03 05:32:40 +0000185 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000186
187 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
188
Lev Walkin535612a2005-07-03 05:32:40 +0000189 FREEMEM(st->buf);
190 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000191
192 return erval;
193}
194
Lev Walkin535612a2005-07-03 05:32:40 +0000195#ifndef __ASN_INTERNAL_TEST_MODE__
196
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000198GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000199 int ilevel, enum xer_encoder_flags_e flags,
200 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000201
202 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000203 GeneralizedTime_t *gt;
204 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000205 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000207
208 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000209 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000210 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000211 && errno != EPERM)
212 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213
Lev Walkin3a522782005-07-04 12:21:51 +0000214 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin535612a2005-07-03 05:32:40 +0000215 if(!gt) _ASN_ENCODE_FAILED;
216
217 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
218 cb, app_key);
219 asn_DEF_GeneralizedTime.free_struct(&asn_DEF_GeneralizedTime,
220 gt, 0);
221 return rv;
222 } else {
223 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
224 cb, app_key);
225 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000226}
227
Lev Walkin535612a2005-07-03 05:32:40 +0000228#endif /* __ASN_INTERNAL_TEST_MODE__ */
229
Lev Walkinf15320b2004-06-03 03:38:44 +0000230int
Lev Walkin5e033762004-09-29 13:26:15 +0000231GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000232 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000233 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000234
Lev Walkind9bd7752004-06-05 08:17:50 +0000235 (void)td; /* Unused argument */
236 (void)ilevel; /* Unused argument */
237
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 if(st && st->buf) {
239 char buf[32];
240 struct tm tm;
241 int ret;
242
243 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000244 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000245 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000246
247 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000248 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000249 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
250 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000251 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000252 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000253 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000254 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000255 }
256}
257
Lev Walkinf15320b2004-06-03 03:38:44 +0000258time_t
Lev Walkin99006362004-08-07 03:52:26 +0000259asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000260 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
261}
262
263time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000264asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
265 time_t tloc;
266 int fv, fd = 0;
267
268 if(frac_value)
269 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
270 else
271 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
272 if(fd == 0 || frac_digits <= 0) {
273 *frac_value = 0;
274 } else {
275 while(fd > frac_digits)
276 fv /= 10, fd--;
277 while(fd < frac_digits) {
278 int new_fv = fv * 10;
279 if(new_fv / 10 != fv) {
280 /* Too long precision request */
281 fv = 0;
282 break;
283 }
284 fv = new_fv, fd++;
285 }
286
287 *frac_value = fv;
288 }
289
290 return tloc;
291}
292
293time_t
294asn_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 +0000295 struct tm tm_s;
296 uint8_t *buf;
297 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000298 int gmtoff_h = 0;
299 int gmtoff_m = 0;
300 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000302 int fvalue = 0;
303 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 time_t tloc;
305
306 if(!st || !st->buf) {
307 errno = EINVAL;
308 return -1;
309 } else {
310 buf = st->buf;
311 end = buf + st->size;
312 }
313
314 if(st->size < 10) {
315 errno = EINVAL;
316 return -1;
317 }
318
319 /*
320 * Decode first 10 bytes: "AAAAMMJJhh"
321 */
322 memset(&tm_s, 0, sizeof(tm_s));
323#undef B2F
324#undef B2T
325#define B2F(var) do { \
326 unsigned ch = *buf; \
327 if(ch < 0x30 && ch > 0x39) { \
328 errno = EINVAL; \
329 return -1; \
330 } else { \
331 var = var * 10 + (ch - 0x30); \
332 buf++; \
333 } \
334 } while(0)
335#define B2T(var) B2F(tm_s.var)
336
337 B2T(tm_year); /* 1: A */
338 B2T(tm_year); /* 2: A */
339 B2T(tm_year); /* 3: A */
340 B2T(tm_year); /* 4: A */
341 B2T(tm_mon); /* 5: M */
342 B2T(tm_mon); /* 6: M */
343 B2T(tm_mday); /* 7: J */
344 B2T(tm_mday); /* 8: J */
345 B2T(tm_hour); /* 9: h */
346 B2T(tm_hour); /* 0: h */
347
348 if(buf == end) goto local_finish;
349
350 /*
351 * Parse [mm[ss[(.|,)ffff]]]
352 * ^^
353 */
354 switch(*buf) {
355 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
356 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
357 tm_s.tm_min = (*buf++) - 0x30;
358 if(buf == end) { errno = EINVAL; return -1; }
359 B2T(tm_min);
360 break;
361 case 0x2B: case 0x2D: /* +, - */
362 goto offset;
363 case 0x5A: /* Z */
364 goto utc_finish;
365 default:
366 errno = EINVAL;
367 return -1;
368 }
369
370 if(buf == end) goto local_finish;
371
372 /*
373 * Parse [mm[ss[(.|,)ffff]]]
374 * ^^
375 */
376 switch(*buf) {
377 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
378 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
379 tm_s.tm_sec = (*buf++) - 0x30;
380 if(buf == end) { errno = EINVAL; return -1; }
381 B2T(tm_sec);
382 break;
383 case 0x2B: case 0x2D: /* +, - */
384 goto offset;
385 case 0x5A: /* Z */
386 goto utc_finish;
387 default:
388 errno = EINVAL;
389 return -1;
390 }
391
392 if(buf == end) goto local_finish;
393
394 /*
395 * Parse [mm[ss[(.|,)ffff]]]
396 * ^ ^
397 */
398 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000399 case 0x2C: case 0x2E: /* (.|,) */
400 /*
401 * Process fractions of seconds.
402 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000403 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000404 int v = *buf;
Lev Walkin3a522782005-07-04 12:21:51 +0000405 int new_fvalue;
Lev Walkin535612a2005-07-03 05:32:40 +0000406 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000407 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
408 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin3a522782005-07-04 12:21:51 +0000409 new_fvalue = fvalue * 10 + (v - 0x30);
410 if(new_fvalue / 10 != fvalue) {
Lev Walkin535612a2005-07-03 05:32:40 +0000411 /* Not enough precision, ignore */
412 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000413 fvalue = new_fvalue;
414 fdigits++;
Lev Walkin535612a2005-07-03 05:32:40 +0000415 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000416 continue;
417 default:
418 break;
419 }
420 break;
421 }
422 }
423
424 if(buf == end) goto local_finish;
425
426 switch(*buf) {
427 case 0x2B: case 0x2D: /* +, - */
428 goto offset;
429 case 0x5A: /* Z */
430 goto utc_finish;
431 default:
432 errno = EINVAL;
433 return -1;
434 }
435
436
437offset:
438
439 if(end - buf < 3) {
440 errno = EINVAL;
441 return -1;
442 }
443 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000444 B2F(gmtoff_h);
445 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000446 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000447 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000448 else
Lev Walkin99006362004-08-07 03:52:26 +0000449 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000450
451 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000452 B2F(gmtoff_m);
453 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000454 } else if(end != buf) {
455 errno = EINVAL;
456 return -1;
457 }
458
Lev Walkin99006362004-08-07 03:52:26 +0000459 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000460
461 /* Fall through */
462utc_finish:
463
464 offset_specified = 1;
465
466 /* Fall through */
467local_finish:
468
469 /*
470 * Validation.
471 */
472 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
473 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
474 || (tm_s.tm_hour > 23)
475 || (tm_s.tm_sec > 60)
476 ) {
477 errno = EINVAL;
478 return -1;
479 }
480
481 /* Canonicalize */
482 tm_s.tm_mon -= 1; /* 0 - 11 */
483 tm_s.tm_year -= 1900;
484 tm_s.tm_isdst = -1;
485
Lev Walkin99006362004-08-07 03:52:26 +0000486 tm_s.tm_sec -= gmtoff;
487
488 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
489
Lev Walkin02a31552004-08-12 03:52:53 +0000490 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000491 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000492 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000493 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000494 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000495 * we can only guess that it is a local zone.
496 * Interpret it in this fashion.
497 */
498 tloc = mktime(&tm_s);
499 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000500 if(tloc == -1) {
501 errno = EINVAL;
502 return -1;
503 }
504
Lev Walkin99006362004-08-07 03:52:26 +0000505 if(ret_tm) {
506 if(as_gmt) {
507 if(offset_specified) {
508 *ret_tm = tm_s;
509 } else {
510 if(gmtime_r(&tloc, ret_tm) == 0) {
511 errno = EINVAL;
512 return -1;
513 }
514 }
515 } else {
516 if(localtime_r(&tloc, ret_tm) == 0) {
517 errno = EINVAL;
518 return -1;
519 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 }
521 }
522
Lev Walkin535612a2005-07-03 05:32:40 +0000523 /* Fractions of seconds */
524 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000525 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000526
Lev Walkinf15320b2004-06-03 03:38:44 +0000527 return tloc;
528}
529
Lev Walkin99006362004-08-07 03:52:26 +0000530GeneralizedTime_t *
531asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000532 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
533}
534
535GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000536asn_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 +0000537 struct tm tm_s;
538 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000539 const unsigned int buf_size =
540 4 + 2 + 2 /* yyyymmdd */
541 + 2 + 2 + 2 /* hhmmss */
542 + 1 + 6 /* .ffffff */
543 + 1 + 4 /* +hhmm */
544 + 1 /* '\0' */
545 ;
Lev Walkin99006362004-08-07 03:52:26 +0000546 char *buf;
547 char *p;
548 int size;
549
550 /* Check arguments */
551 if(!tm) {
552 errno = EINVAL;
553 return 0;
554 }
555
556 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000557 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000558 if(!buf) return 0;
559
560 gmtoff = GMTOFF(*tm);
561
562 if(force_gmt && gmtoff) {
563 tm_s = *tm;
564 tm_s.tm_sec -= gmtoff;
565 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000566 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000567#ifdef HAVE_TM_GMTOFF
568 assert(!GMTOFF(tm_s)); /* Will fix itself */
569#else
570 gmtoff = 0; /* Intervention required */
571#endif
Lev Walkin99006362004-08-07 03:52:26 +0000572 }
573
574 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
575 tm->tm_year + 1900,
576 tm->tm_mon + 1,
577 tm->tm_mday,
578 tm->tm_hour,
579 tm->tm_min,
580 tm->tm_sec
581 );
Lev Walkin535612a2005-07-03 05:32:40 +0000582 if(size != 14) {
583 /* Could be assert(size == 14); */
584 FREEMEM(buf);
585 errno = EINVAL;
586 return 0;
587 }
Lev Walkin99006362004-08-07 03:52:26 +0000588
589 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000590
591 /*
592 * Deal with fractions.
593 */
Lev Walkin3a522782005-07-04 12:21:51 +0000594 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000595 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000596 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000597 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000598 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000599
600 /* Place bounds on precision */
601 while(frac_digits-- > 6)
602 frac_value /= 10;
603
604 /* emulate fbase = pow(10, frac_digits) */
605 for(fbase = 1; frac_digits--;)
606 fbase *= 10;
607
Lev Walkin535612a2005-07-03 05:32:40 +0000608 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000609 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000610 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000611 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000612 frac_value %= fbase;
613 fbase /= 10;
614 } while(fbase > 0 && frac_value > 0 && z < end);
615 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000616 for(--z; *z == 0x30; --z); /* Strip zeroes */
617 p = z + (*z != '.');
618 size = p - buf;
619 }
Lev Walkin535612a2005-07-03 05:32:40 +0000620 }
621
Lev Walkin99006362004-08-07 03:52:26 +0000622 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000623 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000624 *p++ = 0;
625 size++;
626 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000627 int ret;
628 gmtoff %= 86400;
629 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
630 gmtoff / 3600, labs(gmtoff % 3600));
631 if(ret != 5) {
632 FREEMEM(buf);
633 errno = EINVAL;
634 return 0;
635 }
Lev Walkin99006362004-08-07 03:52:26 +0000636 size += ret;
637 }
638
639 if(opt_gt) {
640 if(opt_gt->buf)
641 FREEMEM(opt_gt->buf);
642 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000643 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin99006362004-08-07 03:52:26 +0000644 if(!opt_gt) { free(buf); return 0; }
645 }
646
Lev Walkin4d9528c2004-08-11 08:10:13 +0000647 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000648 opt_gt->size = size;
649
650 return opt_gt;
651}
652
653