blob: 29dcc6c793820cca97c1ba16a8fcc5e779ae688d [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>
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <errno.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000010
Lev Walkin223000a2006-09-13 00:21:58 +000011#ifdef __CYGWIN__
12#include "/usr/include/time.h"
13#else
14#include <time.h>
15#endif /* __CYGWIN__ */
16
Lev Walkin93659562010-11-20 09:47:13 -080017#if defined(_WIN32)
Lev Walkina57953d2005-06-15 19:01:45 +000018#pragma message( "PLEASE STOP AND READ!")
19#pragma message( " localtime_r is implemented via localtime(), which may be not thread-safe.")
20#pragma message( " gmtime_r is implemented via gmtime(), which may be not thread-safe.")
21#pragma message( " ")
22#pragma message( " You must fix the code by inserting appropriate locking")
23#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
Lev Walkinc8c2cb52006-07-13 13:20:19 +000024#pragma message( "PLEASE STOP AND READ!")
Lev Walkin02a31552004-08-12 03:52:53 +000025
Lev Walkincec43b52004-09-02 05:20:39 +000026static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000027 struct tm *tm;
28 if((tm = localtime(tloc)))
29 return memcpy(result, tm, sizeof(struct tm));
30 return 0;
31}
32
Lev Walkincec43b52004-09-02 05:20:39 +000033static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000034 struct tm *tm;
35 if((tm = gmtime(tloc)))
36 return memcpy(result, tm, sizeof(struct tm));
37 return 0;
38}
39
Lev Walkin93e9fe32004-09-17 06:46:10 +000040#define tzset() _tzset()
Lev Walkina57953d2005-06-15 19:01:45 +000041#define putenv(c) _putenv(c)
Lev Walkin93e9fe32004-09-17 06:46:10 +000042#define _EMULATE_TIMEGM
43
Lev Walkin93659562010-11-20 09:47:13 -080044#endif /* _WIN32 */
Lev Walkin93e9fe32004-09-17 06:46:10 +000045
Lev Walkinc8c2cb52006-07-13 13:20:19 +000046#if defined(sun) || defined(_sun_) || defined(__solaris__)
Lev Walkina460ba32004-10-20 15:40:04 +000047#define _EMULATE_TIMEGM
48#endif
49
Lev Walkin93e9fe32004-09-17 06:46:10 +000050/*
51 * Where to look for offset from GMT, Phase I.
52 * Several platforms are known.
53 */
54#if defined(__FreeBSD__) \
55 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
56 || (defined __GLIBC__ && __GLIBC__ >= 2)
57#undef HAVE_TM_GMTOFF
58#define HAVE_TM_GMTOFF
59#endif /* BSDs and newer glibc */
60
61/*
62 * Where to look for offset from GMT, Phase II.
63 */
64#ifdef HAVE_TM_GMTOFF
65#define GMTOFF(tm) ((tm).tm_gmtoff)
66#else /* HAVE_TM_GMTOFF */
67#define GMTOFF(tm) (-timezone)
68#endif /* HAVE_TM_GMTOFF */
69
Frank Morgner917a81d2013-05-21 09:52:19 +020070#if defined(_WIN32)
71#pragma message( "PLEASE STOP AND READ!")
72#pragma message( " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe.")
73#pragma message( " ")
74#pragma message( " You must fix the code by inserting appropriate locking")
75#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
76#pragma message( "PLEASE STOP AND READ!")
77#else
Lev Walkinc8c2cb52006-07-13 13:20:19 +000078#if (defined(_EMULATE_TIMEGM) || !defined(HAVE_TM_GMTOFF))
79#warning "PLEASE STOP AND READ!"
80#warning " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe."
81#warning " "
82#warning " You must fix the code by inserting appropriate locking"
83#warning " if you want to use asn_GT2time() or asn_UT2time()."
84#warning "PLEASE STOP AND READ!"
85#endif /* _EMULATE_TIMEGM */
Frank Morgner917a81d2013-05-21 09:52:19 +020086#endif
Lev Walkinc8c2cb52006-07-13 13:20:19 +000087
Lev Walkin93e9fe32004-09-17 06:46:10 +000088/*
89 * Override our GMTOFF decision for other known platforms.
90 */
91#ifdef __CYGWIN__
92#undef GMTOFF
93static long GMTOFF(struct tm a){
94 struct tm *lt;
95 time_t local_time, gmt_time;
96 long zone;
97
98 tzset();
99 gmt_time = time (NULL);
100
101 lt = gmtime(&gmt_time);
102
103 local_time = mktime(lt);
104 return (gmt_time - local_time);
105}
106#define _EMULATE_TIMEGM
107
108#endif /* __CYGWIN__ */
109
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000110#define ATZVARS do { \
111 char tzoldbuf[64]; \
112 char *tzold
113#define ATZSAVETZ do { \
114 tzold = getenv("TZ"); \
115 if(tzold) { \
116 size_t tzlen = strlen(tzold); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000117 if(tzlen < sizeof(tzoldbuf)) { \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000118 tzold = memcpy(tzoldbuf, tzold, tzlen + 1); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000119 } else { \
Lev Walkinb9671d12007-10-02 11:23:24 +0000120 char *dupptr = tzold; \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000121 tzold = MALLOC(tzlen + 1); \
122 if(tzold) memcpy(tzold, dupptr, tzlen + 1); \
123 } \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000124 setenv("TZ", "UTC", 1); \
125 } \
126 tzset(); \
127} while(0)
128#define ATZOLDTZ do { \
129 if (tzold) { \
130 setenv("TZ", tzold, 1); \
131 *tzoldbuf = 0; \
132 if(tzold != tzoldbuf) \
Lev Walkin419f6752006-09-13 04:02:00 +0000133 FREEMEM(tzold); \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000134 } else { \
135 unsetenv("TZ"); \
136 } \
137 tzset(); \
138} while(0); } while(0);
139
Lev Walkin04fbe622014-09-17 02:27:01 -0700140#ifndef HAVE_TIMEGM
Lev Walkin93e9fe32004-09-17 06:46:10 +0000141#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +0000142static time_t timegm(struct tm *tm) {
143 time_t tloc;
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000144 ATZVARS;
145 ATZSAVETZ;
Lev Walkindb424002004-08-12 03:58:25 +0000146 tloc = mktime(tm);
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000147 ATZOLDTZ;
Lev Walkindb424002004-08-12 03:58:25 +0000148 return tloc;
149}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000150#endif /* _EMULATE_TIMEGM */
Lev Walkin04fbe622014-09-17 02:27:01 -0700151#endif
Lev Walkindb424002004-08-12 03:58:25 +0000152
Lev Walkin64399722004-08-11 07:17:22 +0000153
Lev Walkin535612a2005-07-03 05:32:40 +0000154#ifndef __ASN_INTERNAL_TEST_MODE__
Lev Walkinf15320b2004-06-03 03:38:44 +0000155
156/*
157 * GeneralizedTime basic type description.
158 */
Wim Lewis18c2ec92014-07-29 11:30:10 -0700159static const ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000160 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
161 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
162 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000163};
Lev Walkin725883b2006-10-09 12:07:58 +0000164static asn_per_constraints_t asn_DEF_GeneralizedTime_constraints = {
165 { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
166 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
167 0, 0
168};
Lev Walkin5e033762004-09-29 13:26:15 +0000169asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 "GeneralizedTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +0000171 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000172 OCTET_STRING_free,
173 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 GeneralizedTime_constraint, /* Check validity of time */
175 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000176 GeneralizedTime_encode_der,
177 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000178 GeneralizedTime_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +0000179 OCTET_STRING_decode_uper,
180 OCTET_STRING_encode_uper,
Harald Welte498c9712015-08-30 16:33:07 +0200181 OCTET_STRING_decode_aper,
182 OCTET_STRING_encode_aper,
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000184 asn_DEF_GeneralizedTime_tags,
185 sizeof(asn_DEF_GeneralizedTime_tags)
186 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
187 asn_DEF_GeneralizedTime_tags,
188 sizeof(asn_DEF_GeneralizedTime_tags)
189 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin725883b2006-10-09 12:07:58 +0000190 &asn_DEF_GeneralizedTime_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +0000191 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000192 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193};
194
Lev Walkin535612a2005-07-03 05:32:40 +0000195#endif /* __ASN_INTERNAL_TEST_MODE__ */
Lev Walkinf15320b2004-06-03 03:38:44 +0000196
197/*
198 * Check that the time looks like the time.
199 */
200int
Lev Walkin5e033762004-09-29 13:26:15 +0000201GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000202 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000203 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000204 time_t tloc;
205
206 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000207 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 if(tloc == -1 && errno != EPERM) {
Lev Walkined9019a2006-10-16 12:18:41 +0000209 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000210 "%s: Invalid time format: %s (%s:%d)",
211 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 return -1;
213 }
214
215 return 0;
216}
217
Lev Walkina9cc46e2004-09-22 16:06:28 +0000218asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000219GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000220 int tag_mode, ber_tlv_tag_t tag,
221 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000222 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000223 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000224 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000225 struct tm tm;
226 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000227
Lev Walkin535612a2005-07-03 05:32:40 +0000228 /*
229 * Encode as a canonical DER.
230 */
231 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000232 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000233 if(tloc == -1 && errno != EPERM)
234 /* Failed to recognize time. Fail completely. */
235 _ASN_ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000236
Lev Walkin3a522782005-07-04 12:21:51 +0000237 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin535612a2005-07-03 05:32:40 +0000238 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000239
240 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
241
Lev Walkin535612a2005-07-03 05:32:40 +0000242 FREEMEM(st->buf);
243 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000244
245 return erval;
246}
247
Lev Walkin535612a2005-07-03 05:32:40 +0000248#ifndef __ASN_INTERNAL_TEST_MODE__
249
Lev Walkina9cc46e2004-09-22 16:06:28 +0000250asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000251GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000252 int ilevel, enum xer_encoder_flags_e flags,
253 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000254
255 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000256 GeneralizedTime_t *gt;
257 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000258 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000259 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000260
261 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000262 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000263 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000264 && errno != EPERM)
265 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000266
Lev Walkin3a522782005-07-04 12:21:51 +0000267 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin535612a2005-07-03 05:32:40 +0000268 if(!gt) _ASN_ENCODE_FAILED;
269
270 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
271 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000272 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000273 return rv;
274 } else {
275 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
276 cb, app_key);
277 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000278}
279
Lev Walkin535612a2005-07-03 05:32:40 +0000280#endif /* __ASN_INTERNAL_TEST_MODE__ */
281
Lev Walkinf15320b2004-06-03 03:38:44 +0000282int
Lev Walkin5e033762004-09-29 13:26:15 +0000283GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000284 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000285 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000286
Lev Walkind9bd7752004-06-05 08:17:50 +0000287 (void)td; /* Unused argument */
288 (void)ilevel; /* Unused argument */
289
Lev Walkinf15320b2004-06-03 03:38:44 +0000290 if(st && st->buf) {
291 char buf[32];
292 struct tm tm;
293 int ret;
294
295 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000296 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000297 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000298
299 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000300 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
302 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000303 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000304 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000305 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000306 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000307 }
308}
309
Lev Walkinf15320b2004-06-03 03:38:44 +0000310time_t
Lev Walkin99006362004-08-07 03:52:26 +0000311asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000312 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
313}
314
315time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000316asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
317 time_t tloc;
318 int fv, fd = 0;
319
320 if(frac_value)
321 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
322 else
323 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
324 if(fd == 0 || frac_digits <= 0) {
325 *frac_value = 0;
326 } else {
327 while(fd > frac_digits)
328 fv /= 10, fd--;
329 while(fd < frac_digits) {
Lev Walkin27126182012-09-03 01:09:54 -0700330 if(fv < INT_MAX / 10) {
331 fv *= 10;
332 fd++;
333 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000334 /* Too long precision request */
335 fv = 0;
336 break;
337 }
Lev Walkin3a522782005-07-04 12:21:51 +0000338 }
339
340 *frac_value = fv;
341 }
342
343 return tloc;
344}
345
346time_t
347asn_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 +0000348 struct tm tm_s;
349 uint8_t *buf;
350 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000351 int gmtoff_h = 0;
352 int gmtoff_m = 0;
353 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000355 int fvalue = 0;
356 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000357 time_t tloc;
358
359 if(!st || !st->buf) {
360 errno = EINVAL;
361 return -1;
362 } else {
363 buf = st->buf;
364 end = buf + st->size;
365 }
366
367 if(st->size < 10) {
368 errno = EINVAL;
369 return -1;
370 }
371
372 /*
373 * Decode first 10 bytes: "AAAAMMJJhh"
374 */
375 memset(&tm_s, 0, sizeof(tm_s));
376#undef B2F
377#undef B2T
378#define B2F(var) do { \
379 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000380 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 errno = EINVAL; \
382 return -1; \
383 } else { \
384 var = var * 10 + (ch - 0x30); \
385 buf++; \
386 } \
387 } while(0)
388#define B2T(var) B2F(tm_s.var)
389
390 B2T(tm_year); /* 1: A */
391 B2T(tm_year); /* 2: A */
392 B2T(tm_year); /* 3: A */
393 B2T(tm_year); /* 4: A */
394 B2T(tm_mon); /* 5: M */
395 B2T(tm_mon); /* 6: M */
396 B2T(tm_mday); /* 7: J */
397 B2T(tm_mday); /* 8: J */
398 B2T(tm_hour); /* 9: h */
399 B2T(tm_hour); /* 0: h */
400
401 if(buf == end) goto local_finish;
402
403 /*
404 * Parse [mm[ss[(.|,)ffff]]]
405 * ^^
406 */
407 switch(*buf) {
408 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
409 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
410 tm_s.tm_min = (*buf++) - 0x30;
411 if(buf == end) { errno = EINVAL; return -1; }
412 B2T(tm_min);
413 break;
414 case 0x2B: case 0x2D: /* +, - */
415 goto offset;
416 case 0x5A: /* Z */
417 goto utc_finish;
418 default:
419 errno = EINVAL;
420 return -1;
421 }
422
423 if(buf == end) goto local_finish;
424
425 /*
426 * Parse [mm[ss[(.|,)ffff]]]
427 * ^^
428 */
429 switch(*buf) {
430 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
431 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
432 tm_s.tm_sec = (*buf++) - 0x30;
433 if(buf == end) { errno = EINVAL; return -1; }
434 B2T(tm_sec);
435 break;
436 case 0x2B: case 0x2D: /* +, - */
437 goto offset;
438 case 0x5A: /* Z */
439 goto utc_finish;
440 default:
441 errno = EINVAL;
442 return -1;
443 }
444
445 if(buf == end) goto local_finish;
446
447 /*
448 * Parse [mm[ss[(.|,)ffff]]]
449 * ^ ^
450 */
451 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000452 case 0x2C: case 0x2E: /* (.|,) */
453 /*
454 * Process fractions of seconds.
455 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000456 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000457 int v = *buf;
Lev Walkin9a338f82012-01-22 16:38:37 -0800458 /* GCC 4.x is being too smart without volatile */
Lev Walkin535612a2005-07-03 05:32:40 +0000459 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
461 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin27126182012-09-03 01:09:54 -0700462 if(fvalue < INT_MAX/10) {
463 fvalue = fvalue * 10 + (v - 0x30);
Lev Walkin3a522782005-07-04 12:21:51 +0000464 fdigits++;
Lev Walkin27126182012-09-03 01:09:54 -0700465 } else {
466 /* Not enough precision, ignore */
Lev Walkin535612a2005-07-03 05:32:40 +0000467 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000468 continue;
469 default:
470 break;
471 }
472 break;
473 }
474 }
475
476 if(buf == end) goto local_finish;
477
478 switch(*buf) {
479 case 0x2B: case 0x2D: /* +, - */
480 goto offset;
481 case 0x5A: /* Z */
482 goto utc_finish;
483 default:
484 errno = EINVAL;
485 return -1;
486 }
487
488
489offset:
490
491 if(end - buf < 3) {
492 errno = EINVAL;
493 return -1;
494 }
495 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000496 B2F(gmtoff_h);
497 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000499 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000500 else
Lev Walkin99006362004-08-07 03:52:26 +0000501 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000502
503 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000504 B2F(gmtoff_m);
505 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 } else if(end != buf) {
507 errno = EINVAL;
508 return -1;
509 }
510
Lev Walkin99006362004-08-07 03:52:26 +0000511 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000512
513 /* Fall through */
514utc_finish:
515
516 offset_specified = 1;
517
518 /* Fall through */
519local_finish:
520
521 /*
522 * Validation.
523 */
524 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
525 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
526 || (tm_s.tm_hour > 23)
527 || (tm_s.tm_sec > 60)
528 ) {
529 errno = EINVAL;
530 return -1;
531 }
532
533 /* Canonicalize */
534 tm_s.tm_mon -= 1; /* 0 - 11 */
535 tm_s.tm_year -= 1900;
536 tm_s.tm_isdst = -1;
537
Lev Walkin99006362004-08-07 03:52:26 +0000538 tm_s.tm_sec -= gmtoff;
539
540 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
541
Lev Walkin02a31552004-08-12 03:52:53 +0000542 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000543 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000544 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000545 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000546 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000547 * we can only guess that it is a local zone.
548 * Interpret it in this fashion.
549 */
550 tloc = mktime(&tm_s);
551 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 if(tloc == -1) {
553 errno = EINVAL;
554 return -1;
555 }
556
Lev Walkin99006362004-08-07 03:52:26 +0000557 if(ret_tm) {
558 if(as_gmt) {
559 if(offset_specified) {
560 *ret_tm = tm_s;
561 } else {
562 if(gmtime_r(&tloc, ret_tm) == 0) {
563 errno = EINVAL;
564 return -1;
565 }
566 }
567 } else {
568 if(localtime_r(&tloc, ret_tm) == 0) {
569 errno = EINVAL;
570 return -1;
571 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 }
573 }
574
Lev Walkin535612a2005-07-03 05:32:40 +0000575 /* Fractions of seconds */
576 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000577 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000578
Lev Walkinf15320b2004-06-03 03:38:44 +0000579 return tloc;
580}
581
Lev Walkin99006362004-08-07 03:52:26 +0000582GeneralizedTime_t *
583asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000584 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
585}
586
587GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000588asn_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 +0000589 struct tm tm_s;
590 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000591 const unsigned int buf_size =
592 4 + 2 + 2 /* yyyymmdd */
593 + 2 + 2 + 2 /* hhmmss */
594 + 1 + 6 /* .ffffff */
595 + 1 + 4 /* +hhmm */
596 + 1 /* '\0' */
597 ;
Lev Walkin99006362004-08-07 03:52:26 +0000598 char *buf;
599 char *p;
600 int size;
601
602 /* Check arguments */
603 if(!tm) {
604 errno = EINVAL;
605 return 0;
606 }
607
608 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000609 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000610 if(!buf) return 0;
611
612 gmtoff = GMTOFF(*tm);
613
614 if(force_gmt && gmtoff) {
615 tm_s = *tm;
616 tm_s.tm_sec -= gmtoff;
617 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000618 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000619#ifdef HAVE_TM_GMTOFF
620 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000621#else /* !HAVE_TM_GMTOFF */
622 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000623#endif
Lev Walkin99006362004-08-07 03:52:26 +0000624 }
625
626 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
627 tm->tm_year + 1900,
628 tm->tm_mon + 1,
629 tm->tm_mday,
630 tm->tm_hour,
631 tm->tm_min,
632 tm->tm_sec
633 );
Lev Walkin535612a2005-07-03 05:32:40 +0000634 if(size != 14) {
635 /* Could be assert(size == 14); */
636 FREEMEM(buf);
637 errno = EINVAL;
638 return 0;
639 }
Lev Walkin99006362004-08-07 03:52:26 +0000640
641 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000642
643 /*
644 * Deal with fractions.
645 */
Lev Walkin3a522782005-07-04 12:21:51 +0000646 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000647 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000648 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000649 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000650 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000651
652 /* Place bounds on precision */
653 while(frac_digits-- > 6)
654 frac_value /= 10;
655
656 /* emulate fbase = pow(10, frac_digits) */
657 for(fbase = 1; frac_digits--;)
658 fbase *= 10;
659
Lev Walkin535612a2005-07-03 05:32:40 +0000660 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000661 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000662 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000663 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000664 frac_value %= fbase;
665 fbase /= 10;
666 } while(fbase > 0 && frac_value > 0 && z < end);
667 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000668 for(--z; *z == 0x30; --z); /* Strip zeroes */
669 p = z + (*z != '.');
670 size = p - buf;
671 }
Lev Walkin535612a2005-07-03 05:32:40 +0000672 }
673
Lev Walkin99006362004-08-07 03:52:26 +0000674 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000675 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000676 *p++ = 0;
677 size++;
678 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000679 int ret;
680 gmtoff %= 86400;
681 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
Lev Walkina4347cc2010-03-10 23:05:56 +0000682 gmtoff / 3600, labs(gmtoff % 3600) / 60);
Lev Walkin535612a2005-07-03 05:32:40 +0000683 if(ret != 5) {
684 FREEMEM(buf);
685 errno = EINVAL;
686 return 0;
687 }
Lev Walkin99006362004-08-07 03:52:26 +0000688 size += ret;
689 }
690
691 if(opt_gt) {
692 if(opt_gt->buf)
693 FREEMEM(opt_gt->buf);
694 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000695 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin419f6752006-09-13 04:02:00 +0000696 if(!opt_gt) { FREEMEM(buf); return 0; }
Lev Walkin99006362004-08-07 03:52:26 +0000697 }
698
Lev Walkin4d9528c2004-08-11 08:10:13 +0000699 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000700 opt_gt->size = size;
701
702 return opt_gt;
703}
704
705