blob: cd36f034f714bc5a90277b839fef1c2b41054644 [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,
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000182 asn_DEF_GeneralizedTime_tags,
183 sizeof(asn_DEF_GeneralizedTime_tags)
184 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
185 asn_DEF_GeneralizedTime_tags,
186 sizeof(asn_DEF_GeneralizedTime_tags)
187 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin725883b2006-10-09 12:07:58 +0000188 &asn_DEF_GeneralizedTime_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +0000189 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000190 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000191};
192
Lev Walkin535612a2005-07-03 05:32:40 +0000193#endif /* __ASN_INTERNAL_TEST_MODE__ */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194
195/*
196 * Check that the time looks like the time.
197 */
198int
Lev Walkin5e033762004-09-29 13:26:15 +0000199GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000200 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000201 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000202 time_t tloc;
203
204 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000205 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000206 if(tloc == -1 && errno != EPERM) {
Lev Walkined9019a2006-10-16 12:18:41 +0000207 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000208 "%s: Invalid time format: %s (%s:%d)",
209 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 return -1;
211 }
212
213 return 0;
214}
215
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000217GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000218 int tag_mode, ber_tlv_tag_t tag,
219 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000220 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000221 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000222 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000223 struct tm tm;
224 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000225
Lev Walkin535612a2005-07-03 05:32:40 +0000226 /*
227 * Encode as a canonical DER.
228 */
229 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000230 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000231 if(tloc == -1 && errno != EPERM)
232 /* Failed to recognize time. Fail completely. */
233 _ASN_ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000234
Lev Walkin3a522782005-07-04 12:21:51 +0000235 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin535612a2005-07-03 05:32:40 +0000236 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000237
238 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
239
Lev Walkin535612a2005-07-03 05:32:40 +0000240 FREEMEM(st->buf);
241 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000242
243 return erval;
244}
245
Lev Walkin535612a2005-07-03 05:32:40 +0000246#ifndef __ASN_INTERNAL_TEST_MODE__
247
Lev Walkina9cc46e2004-09-22 16:06:28 +0000248asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000249GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000250 int ilevel, enum xer_encoder_flags_e flags,
251 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000252
253 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000254 GeneralizedTime_t *gt;
255 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000256 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000257 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000258
259 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000260 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000261 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000262 && errno != EPERM)
263 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000264
Lev Walkin3a522782005-07-04 12:21:51 +0000265 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin535612a2005-07-03 05:32:40 +0000266 if(!gt) _ASN_ENCODE_FAILED;
267
268 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
269 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000270 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000271 return rv;
272 } else {
273 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
274 cb, app_key);
275 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000276}
277
Lev Walkin535612a2005-07-03 05:32:40 +0000278#endif /* __ASN_INTERNAL_TEST_MODE__ */
279
Lev Walkinf15320b2004-06-03 03:38:44 +0000280int
Lev Walkin5e033762004-09-29 13:26:15 +0000281GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000282 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000283 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000284
Lev Walkind9bd7752004-06-05 08:17:50 +0000285 (void)td; /* Unused argument */
286 (void)ilevel; /* Unused argument */
287
Lev Walkinf15320b2004-06-03 03:38:44 +0000288 if(st && st->buf) {
289 char buf[32];
290 struct tm tm;
291 int ret;
292
293 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000294 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000295 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000296
297 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000298 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000299 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
300 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000301 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000302 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000304 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000305 }
306}
307
Lev Walkinf15320b2004-06-03 03:38:44 +0000308time_t
Lev Walkin99006362004-08-07 03:52:26 +0000309asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000310 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
311}
312
313time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000314asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
315 time_t tloc;
316 int fv, fd = 0;
317
318 if(frac_value)
319 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
320 else
321 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
322 if(fd == 0 || frac_digits <= 0) {
323 *frac_value = 0;
324 } else {
325 while(fd > frac_digits)
326 fv /= 10, fd--;
327 while(fd < frac_digits) {
Lev Walkin27126182012-09-03 01:09:54 -0700328 if(fv < INT_MAX / 10) {
329 fv *= 10;
330 fd++;
331 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000332 /* Too long precision request */
333 fv = 0;
334 break;
335 }
Lev Walkin3a522782005-07-04 12:21:51 +0000336 }
337
338 *frac_value = fv;
339 }
340
341 return tloc;
342}
343
344time_t
345asn_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 +0000346 struct tm tm_s;
347 uint8_t *buf;
348 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000349 int gmtoff_h = 0;
350 int gmtoff_m = 0;
351 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000352 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000353 int fvalue = 0;
354 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000355 time_t tloc;
356
357 if(!st || !st->buf) {
358 errno = EINVAL;
359 return -1;
360 } else {
361 buf = st->buf;
362 end = buf + st->size;
363 }
364
365 if(st->size < 10) {
366 errno = EINVAL;
367 return -1;
368 }
369
370 /*
371 * Decode first 10 bytes: "AAAAMMJJhh"
372 */
373 memset(&tm_s, 0, sizeof(tm_s));
374#undef B2F
375#undef B2T
376#define B2F(var) do { \
377 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000378 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000379 errno = EINVAL; \
380 return -1; \
381 } else { \
382 var = var * 10 + (ch - 0x30); \
383 buf++; \
384 } \
385 } while(0)
386#define B2T(var) B2F(tm_s.var)
387
388 B2T(tm_year); /* 1: A */
389 B2T(tm_year); /* 2: A */
390 B2T(tm_year); /* 3: A */
391 B2T(tm_year); /* 4: A */
392 B2T(tm_mon); /* 5: M */
393 B2T(tm_mon); /* 6: M */
394 B2T(tm_mday); /* 7: J */
395 B2T(tm_mday); /* 8: J */
396 B2T(tm_hour); /* 9: h */
397 B2T(tm_hour); /* 0: h */
398
399 if(buf == end) goto local_finish;
400
401 /*
402 * Parse [mm[ss[(.|,)ffff]]]
403 * ^^
404 */
405 switch(*buf) {
406 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
407 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
408 tm_s.tm_min = (*buf++) - 0x30;
409 if(buf == end) { errno = EINVAL; return -1; }
410 B2T(tm_min);
411 break;
412 case 0x2B: case 0x2D: /* +, - */
413 goto offset;
414 case 0x5A: /* Z */
415 goto utc_finish;
416 default:
417 errno = EINVAL;
418 return -1;
419 }
420
421 if(buf == end) goto local_finish;
422
423 /*
424 * Parse [mm[ss[(.|,)ffff]]]
425 * ^^
426 */
427 switch(*buf) {
428 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
429 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
430 tm_s.tm_sec = (*buf++) - 0x30;
431 if(buf == end) { errno = EINVAL; return -1; }
432 B2T(tm_sec);
433 break;
434 case 0x2B: case 0x2D: /* +, - */
435 goto offset;
436 case 0x5A: /* Z */
437 goto utc_finish;
438 default:
439 errno = EINVAL;
440 return -1;
441 }
442
443 if(buf == end) goto local_finish;
444
445 /*
446 * Parse [mm[ss[(.|,)ffff]]]
447 * ^ ^
448 */
449 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000450 case 0x2C: case 0x2E: /* (.|,) */
451 /*
452 * Process fractions of seconds.
453 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000454 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000455 int v = *buf;
Lev Walkin9a338f82012-01-22 16:38:37 -0800456 /* GCC 4.x is being too smart without volatile */
Lev Walkin535612a2005-07-03 05:32:40 +0000457 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000458 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
459 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin27126182012-09-03 01:09:54 -0700460 if(fvalue < INT_MAX/10) {
461 fvalue = fvalue * 10 + (v - 0x30);
Lev Walkin3a522782005-07-04 12:21:51 +0000462 fdigits++;
Lev Walkin27126182012-09-03 01:09:54 -0700463 } else {
464 /* Not enough precision, ignore */
Lev Walkin535612a2005-07-03 05:32:40 +0000465 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000466 continue;
467 default:
468 break;
469 }
470 break;
471 }
472 }
473
474 if(buf == end) goto local_finish;
475
476 switch(*buf) {
477 case 0x2B: case 0x2D: /* +, - */
478 goto offset;
479 case 0x5A: /* Z */
480 goto utc_finish;
481 default:
482 errno = EINVAL;
483 return -1;
484 }
485
486
487offset:
488
489 if(end - buf < 3) {
490 errno = EINVAL;
491 return -1;
492 }
493 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000494 B2F(gmtoff_h);
495 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000496 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000497 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 else
Lev Walkin99006362004-08-07 03:52:26 +0000499 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000500
501 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000502 B2F(gmtoff_m);
503 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000504 } else if(end != buf) {
505 errno = EINVAL;
506 return -1;
507 }
508
Lev Walkin99006362004-08-07 03:52:26 +0000509 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000510
511 /* Fall through */
512utc_finish:
513
514 offset_specified = 1;
515
516 /* Fall through */
517local_finish:
518
519 /*
520 * Validation.
521 */
522 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
523 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
524 || (tm_s.tm_hour > 23)
525 || (tm_s.tm_sec > 60)
526 ) {
527 errno = EINVAL;
528 return -1;
529 }
530
531 /* Canonicalize */
532 tm_s.tm_mon -= 1; /* 0 - 11 */
533 tm_s.tm_year -= 1900;
534 tm_s.tm_isdst = -1;
535
Lev Walkin99006362004-08-07 03:52:26 +0000536 tm_s.tm_sec -= gmtoff;
537
538 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
539
Lev Walkin02a31552004-08-12 03:52:53 +0000540 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000541 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000542 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000543 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000544 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000545 * we can only guess that it is a local zone.
546 * Interpret it in this fashion.
547 */
548 tloc = mktime(&tm_s);
549 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000550 if(tloc == -1) {
551 errno = EINVAL;
552 return -1;
553 }
554
Lev Walkin99006362004-08-07 03:52:26 +0000555 if(ret_tm) {
556 if(as_gmt) {
557 if(offset_specified) {
558 *ret_tm = tm_s;
559 } else {
560 if(gmtime_r(&tloc, ret_tm) == 0) {
561 errno = EINVAL;
562 return -1;
563 }
564 }
565 } else {
566 if(localtime_r(&tloc, ret_tm) == 0) {
567 errno = EINVAL;
568 return -1;
569 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000570 }
571 }
572
Lev Walkin535612a2005-07-03 05:32:40 +0000573 /* Fractions of seconds */
574 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000575 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000576
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 return tloc;
578}
579
Lev Walkin99006362004-08-07 03:52:26 +0000580GeneralizedTime_t *
581asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000582 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
583}
584
585GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000586asn_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 +0000587 struct tm tm_s;
588 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000589 const unsigned int buf_size =
590 4 + 2 + 2 /* yyyymmdd */
591 + 2 + 2 + 2 /* hhmmss */
592 + 1 + 6 /* .ffffff */
593 + 1 + 4 /* +hhmm */
594 + 1 /* '\0' */
595 ;
Lev Walkin99006362004-08-07 03:52:26 +0000596 char *buf;
597 char *p;
598 int size;
599
600 /* Check arguments */
601 if(!tm) {
602 errno = EINVAL;
603 return 0;
604 }
605
606 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000607 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000608 if(!buf) return 0;
609
610 gmtoff = GMTOFF(*tm);
611
612 if(force_gmt && gmtoff) {
613 tm_s = *tm;
614 tm_s.tm_sec -= gmtoff;
615 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000616 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000617#ifdef HAVE_TM_GMTOFF
618 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000619#else /* !HAVE_TM_GMTOFF */
620 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000621#endif
Lev Walkin99006362004-08-07 03:52:26 +0000622 }
623
624 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
625 tm->tm_year + 1900,
626 tm->tm_mon + 1,
627 tm->tm_mday,
628 tm->tm_hour,
629 tm->tm_min,
630 tm->tm_sec
631 );
Lev Walkin535612a2005-07-03 05:32:40 +0000632 if(size != 14) {
633 /* Could be assert(size == 14); */
634 FREEMEM(buf);
635 errno = EINVAL;
636 return 0;
637 }
Lev Walkin99006362004-08-07 03:52:26 +0000638
639 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000640
641 /*
642 * Deal with fractions.
643 */
Lev Walkin3a522782005-07-04 12:21:51 +0000644 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000645 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000646 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000647 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000648 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000649
650 /* Place bounds on precision */
651 while(frac_digits-- > 6)
652 frac_value /= 10;
653
654 /* emulate fbase = pow(10, frac_digits) */
655 for(fbase = 1; frac_digits--;)
656 fbase *= 10;
657
Lev Walkin535612a2005-07-03 05:32:40 +0000658 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000659 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000660 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000661 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000662 frac_value %= fbase;
663 fbase /= 10;
664 } while(fbase > 0 && frac_value > 0 && z < end);
665 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000666 for(--z; *z == 0x30; --z); /* Strip zeroes */
667 p = z + (*z != '.');
668 size = p - buf;
669 }
Lev Walkin535612a2005-07-03 05:32:40 +0000670 }
671
Lev Walkin99006362004-08-07 03:52:26 +0000672 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000673 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000674 *p++ = 0;
675 size++;
676 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000677 int ret;
678 gmtoff %= 86400;
679 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
Lev Walkina4347cc2010-03-10 23:05:56 +0000680 gmtoff / 3600, labs(gmtoff % 3600) / 60);
Lev Walkin535612a2005-07-03 05:32:40 +0000681 if(ret != 5) {
682 FREEMEM(buf);
683 errno = EINVAL;
684 return 0;
685 }
Lev Walkin99006362004-08-07 03:52:26 +0000686 size += ret;
687 }
688
689 if(opt_gt) {
690 if(opt_gt->buf)
691 FREEMEM(opt_gt->buf);
692 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000693 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin419f6752006-09-13 04:02:00 +0000694 if(!opt_gt) { FREEMEM(buf); return 0; }
Lev Walkin99006362004-08-07 03:52:26 +0000695 }
696
Lev Walkin4d9528c2004-08-11 08:10:13 +0000697 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000698 opt_gt->size = size;
699
700 return opt_gt;
701}
702
703