blob: 61cbf44522aba488264ebc07d1d2fafd34a09f2a [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 */
Vasil Velichkovadf65942016-08-01 16:52:02 +03007#ifndef _BSD_SOURCE
Lev Walkin7623d8c2016-03-14 03:55:21 -07008#define _BSD_SOURCE /* for timegm(3) */
Vasil Velichkovadf65942016-08-01 16:52:02 +03009#endif
Lev Walkina9cc46e2004-09-22 16:06:28 +000010#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000011#include <GeneralizedTime.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000012
Lev Walkin223000a2006-09-13 00:21:58 +000013#ifdef __CYGWIN__
14#include "/usr/include/time.h"
15#else
16#include <time.h>
17#endif /* __CYGWIN__ */
18
Lev Walkin7623d8c2016-03-14 03:55:21 -070019#include <stdio.h>
20#include <errno.h>
21
Lev Walkin93659562010-11-20 09:47:13 -080022#if defined(_WIN32)
Lev Walkina57953d2005-06-15 19:01:45 +000023#pragma message( "PLEASE STOP AND READ!")
24#pragma message( " localtime_r is implemented via localtime(), which may be not thread-safe.")
25#pragma message( " gmtime_r is implemented via gmtime(), which may be not thread-safe.")
26#pragma message( " ")
27#pragma message( " You must fix the code by inserting appropriate locking")
28#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
Lev Walkinc8c2cb52006-07-13 13:20:19 +000029#pragma message( "PLEASE STOP AND READ!")
Lev Walkin02a31552004-08-12 03:52:53 +000030
Lev Walkincec43b52004-09-02 05:20:39 +000031static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000032 struct tm *tm;
33 if((tm = localtime(tloc)))
34 return memcpy(result, tm, sizeof(struct tm));
35 return 0;
36}
37
Lev Walkincec43b52004-09-02 05:20:39 +000038static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000039 struct tm *tm;
40 if((tm = gmtime(tloc)))
41 return memcpy(result, tm, sizeof(struct tm));
42 return 0;
43}
44
Lev Walkin93e9fe32004-09-17 06:46:10 +000045#define tzset() _tzset()
Lev Walkina57953d2005-06-15 19:01:45 +000046#define putenv(c) _putenv(c)
Lev Walkin93e9fe32004-09-17 06:46:10 +000047#define _EMULATE_TIMEGM
48
Lev Walkin93659562010-11-20 09:47:13 -080049#endif /* _WIN32 */
Lev Walkin93e9fe32004-09-17 06:46:10 +000050
Lev Walkinc8c2cb52006-07-13 13:20:19 +000051#if defined(sun) || defined(_sun_) || defined(__solaris__)
Lev Walkina460ba32004-10-20 15:40:04 +000052#define _EMULATE_TIMEGM
53#endif
54
Lev Walkin93e9fe32004-09-17 06:46:10 +000055/*
56 * Where to look for offset from GMT, Phase I.
57 * Several platforms are known.
58 */
59#if defined(__FreeBSD__) \
60 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
61 || (defined __GLIBC__ && __GLIBC__ >= 2)
62#undef HAVE_TM_GMTOFF
63#define HAVE_TM_GMTOFF
64#endif /* BSDs and newer glibc */
65
66/*
67 * Where to look for offset from GMT, Phase II.
68 */
69#ifdef HAVE_TM_GMTOFF
70#define GMTOFF(tm) ((tm).tm_gmtoff)
71#else /* HAVE_TM_GMTOFF */
72#define GMTOFF(tm) (-timezone)
73#endif /* HAVE_TM_GMTOFF */
74
Frank Morgner917a81d2013-05-21 09:52:19 +020075#if defined(_WIN32)
76#pragma message( "PLEASE STOP AND READ!")
77#pragma message( " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe.")
78#pragma message( " ")
79#pragma message( " You must fix the code by inserting appropriate locking")
80#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
81#pragma message( "PLEASE STOP AND READ!")
82#else
Lev Walkinc8c2cb52006-07-13 13:20:19 +000083#if (defined(_EMULATE_TIMEGM) || !defined(HAVE_TM_GMTOFF))
84#warning "PLEASE STOP AND READ!"
85#warning " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe."
86#warning " "
87#warning " You must fix the code by inserting appropriate locking"
88#warning " if you want to use asn_GT2time() or asn_UT2time()."
89#warning "PLEASE STOP AND READ!"
90#endif /* _EMULATE_TIMEGM */
Frank Morgner917a81d2013-05-21 09:52:19 +020091#endif
Lev Walkinc8c2cb52006-07-13 13:20:19 +000092
Lev Walkin93e9fe32004-09-17 06:46:10 +000093/*
94 * Override our GMTOFF decision for other known platforms.
95 */
96#ifdef __CYGWIN__
97#undef GMTOFF
98static long GMTOFF(struct tm a){
99 struct tm *lt;
100 time_t local_time, gmt_time;
101 long zone;
102
103 tzset();
104 gmt_time = time (NULL);
105
106 lt = gmtime(&gmt_time);
107
108 local_time = mktime(lt);
109 return (gmt_time - local_time);
110}
111#define _EMULATE_TIMEGM
112
113#endif /* __CYGWIN__ */
114
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000115#define ATZVARS do { \
116 char tzoldbuf[64]; \
117 char *tzold
118#define ATZSAVETZ do { \
119 tzold = getenv("TZ"); \
120 if(tzold) { \
121 size_t tzlen = strlen(tzold); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000122 if(tzlen < sizeof(tzoldbuf)) { \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000123 tzold = memcpy(tzoldbuf, tzold, tzlen + 1); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000124 } else { \
Lev Walkinb9671d12007-10-02 11:23:24 +0000125 char *dupptr = tzold; \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000126 tzold = MALLOC(tzlen + 1); \
127 if(tzold) memcpy(tzold, dupptr, tzlen + 1); \
128 } \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000129 setenv("TZ", "UTC", 1); \
130 } \
131 tzset(); \
132} while(0)
133#define ATZOLDTZ do { \
134 if (tzold) { \
135 setenv("TZ", tzold, 1); \
136 *tzoldbuf = 0; \
137 if(tzold != tzoldbuf) \
Lev Walkin419f6752006-09-13 04:02:00 +0000138 FREEMEM(tzold); \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000139 } else { \
140 unsetenv("TZ"); \
141 } \
142 tzset(); \
143} while(0); } while(0);
144
Lev Walkin04fbe622014-09-17 02:27:01 -0700145#ifndef HAVE_TIMEGM
Lev Walkin93e9fe32004-09-17 06:46:10 +0000146#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +0000147static time_t timegm(struct tm *tm) {
148 time_t tloc;
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000149 ATZVARS;
150 ATZSAVETZ;
Lev Walkindb424002004-08-12 03:58:25 +0000151 tloc = mktime(tm);
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000152 ATZOLDTZ;
Lev Walkindb424002004-08-12 03:58:25 +0000153 return tloc;
154}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000155#endif /* _EMULATE_TIMEGM */
Lev Walkin04fbe622014-09-17 02:27:01 -0700156#endif
Lev Walkindb424002004-08-12 03:58:25 +0000157
Lev Walkin64399722004-08-11 07:17:22 +0000158
Lev Walkin7c1dc052016-03-14 03:08:15 -0700159#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkinf15320b2004-06-03 03:38:44 +0000160
161/*
162 * GeneralizedTime basic type description.
163 */
Wim Lewis18c2ec92014-07-29 11:30:10 -0700164static const ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000165 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
166 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
167 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000168};
Lev Walkin725883b2006-10-09 12:07:58 +0000169static asn_per_constraints_t asn_DEF_GeneralizedTime_constraints = {
170 { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
171 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
172 0, 0
173};
Lev Walkin5e033762004-09-29 13:26:15 +0000174asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 "GeneralizedTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +0000176 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000177 OCTET_STRING_free,
178 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 GeneralizedTime_constraint, /* Check validity of time */
180 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000181 GeneralizedTime_encode_der,
182 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000183 GeneralizedTime_encode_xer,
Lev Walkin80334ed2017-07-06 07:28:21 -0700184#ifdef ASN_DISABLE_PER_SUPPORT
185 0,
186 0,
187#else
Lev Walkin725883b2006-10-09 12:07:58 +0000188 OCTET_STRING_decode_uper,
189 OCTET_STRING_encode_uper,
Lev Walkin80334ed2017-07-06 07:28:21 -0700190#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000192 asn_DEF_GeneralizedTime_tags,
193 sizeof(asn_DEF_GeneralizedTime_tags)
194 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
195 asn_DEF_GeneralizedTime_tags,
196 sizeof(asn_DEF_GeneralizedTime_tags)
197 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin725883b2006-10-09 12:07:58 +0000198 &asn_DEF_GeneralizedTime_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +0000199 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000200 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000201};
202
Lev Walkin7c1dc052016-03-14 03:08:15 -0700203#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000204
205/*
206 * Check that the time looks like the time.
207 */
208int
Lev Walkin5e033762004-09-29 13:26:15 +0000209GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000210 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000211 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 time_t tloc;
213
214 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000215 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 if(tloc == -1 && errno != EPERM) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700217 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000218 "%s: Invalid time format: %s (%s:%d)",
219 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 return -1;
221 }
222
223 return 0;
224}
225
Lev Walkina9cc46e2004-09-22 16:06:28 +0000226asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000227GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000228 int tag_mode, ber_tlv_tag_t tag,
229 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000230 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000231 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000232 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000233 struct tm tm;
234 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000235
Lev Walkin535612a2005-07-03 05:32:40 +0000236 /*
237 * Encode as a canonical DER.
238 */
239 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000240 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000241 if(tloc == -1 && errno != EPERM)
242 /* Failed to recognize time. Fail completely. */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700243 ASN__ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000244
Lev Walkin3a522782005-07-04 12:21:51 +0000245 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700246 if(!st) ASN__ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000247
248 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
249
Lev Walkin535612a2005-07-03 05:32:40 +0000250 FREEMEM(st->buf);
251 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000252
253 return erval;
254}
255
Lev Walkin7c1dc052016-03-14 03:08:15 -0700256#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkin535612a2005-07-03 05:32:40 +0000257
Lev Walkina9cc46e2004-09-22 16:06:28 +0000258asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000259GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000260 int ilevel, enum xer_encoder_flags_e flags,
261 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000262
263 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000264 GeneralizedTime_t *gt;
265 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000266 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000267 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000268
269 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000270 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000271 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000272 && errno != EPERM)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700273 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000274
Lev Walkin3a522782005-07-04 12:21:51 +0000275 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700276 if(!gt) ASN__ENCODE_FAILED;
Lev Walkin535612a2005-07-03 05:32:40 +0000277
278 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
279 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000280 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000281 return rv;
282 } else {
283 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
284 cb, app_key);
285 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000286}
287
Lev Walkin7c1dc052016-03-14 03:08:15 -0700288#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkin535612a2005-07-03 05:32:40 +0000289
Lev Walkinf15320b2004-06-03 03:38:44 +0000290int
Lev Walkin5e033762004-09-29 13:26:15 +0000291GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000292 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000293 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000294
Lev Walkind9bd7752004-06-05 08:17:50 +0000295 (void)td; /* Unused argument */
296 (void)ilevel; /* Unused argument */
297
Lev Walkinf15320b2004-06-03 03:38:44 +0000298 if(st && st->buf) {
299 char buf[32];
300 struct tm tm;
301 int ret;
302
303 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000304 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000305 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000306
307 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000308 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000309 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
310 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000311 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000312 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000313 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000314 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000315 }
316}
317
Lev Walkinf15320b2004-06-03 03:38:44 +0000318time_t
Lev Walkin99006362004-08-07 03:52:26 +0000319asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000320 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
321}
322
323time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000324asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
325 time_t tloc;
326 int fv, fd = 0;
327
328 if(frac_value)
329 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
330 else
331 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
332 if(fd == 0 || frac_digits <= 0) {
333 *frac_value = 0;
334 } else {
335 while(fd > frac_digits)
336 fv /= 10, fd--;
337 while(fd < frac_digits) {
Lev Walkin27126182012-09-03 01:09:54 -0700338 if(fv < INT_MAX / 10) {
339 fv *= 10;
340 fd++;
341 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000342 /* Too long precision request */
343 fv = 0;
344 break;
345 }
Lev Walkin3a522782005-07-04 12:21:51 +0000346 }
347
348 *frac_value = fv;
349 }
350
351 return tloc;
352}
353
354time_t
355asn_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 +0000356 struct tm tm_s;
357 uint8_t *buf;
358 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000359 int gmtoff_h = 0;
360 int gmtoff_m = 0;
361 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000362 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000363 int fvalue = 0;
364 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000365 time_t tloc;
366
367 if(!st || !st->buf) {
368 errno = EINVAL;
369 return -1;
370 } else {
371 buf = st->buf;
372 end = buf + st->size;
373 }
374
375 if(st->size < 10) {
376 errno = EINVAL;
377 return -1;
378 }
379
380 /*
381 * Decode first 10 bytes: "AAAAMMJJhh"
382 */
383 memset(&tm_s, 0, sizeof(tm_s));
384#undef B2F
385#undef B2T
386#define B2F(var) do { \
387 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000388 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000389 errno = EINVAL; \
390 return -1; \
391 } else { \
392 var = var * 10 + (ch - 0x30); \
393 buf++; \
394 } \
395 } while(0)
396#define B2T(var) B2F(tm_s.var)
397
398 B2T(tm_year); /* 1: A */
399 B2T(tm_year); /* 2: A */
400 B2T(tm_year); /* 3: A */
401 B2T(tm_year); /* 4: A */
402 B2T(tm_mon); /* 5: M */
403 B2T(tm_mon); /* 6: M */
404 B2T(tm_mday); /* 7: J */
405 B2T(tm_mday); /* 8: J */
406 B2T(tm_hour); /* 9: h */
407 B2T(tm_hour); /* 0: h */
408
409 if(buf == end) goto local_finish;
410
411 /*
412 * Parse [mm[ss[(.|,)ffff]]]
413 * ^^
414 */
415 switch(*buf) {
416 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
417 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
418 tm_s.tm_min = (*buf++) - 0x30;
419 if(buf == end) { errno = EINVAL; return -1; }
420 B2T(tm_min);
421 break;
422 case 0x2B: case 0x2D: /* +, - */
423 goto offset;
424 case 0x5A: /* Z */
425 goto utc_finish;
426 default:
427 errno = EINVAL;
428 return -1;
429 }
430
431 if(buf == end) goto local_finish;
432
433 /*
434 * Parse [mm[ss[(.|,)ffff]]]
435 * ^^
436 */
437 switch(*buf) {
438 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
439 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
440 tm_s.tm_sec = (*buf++) - 0x30;
441 if(buf == end) { errno = EINVAL; return -1; }
442 B2T(tm_sec);
443 break;
444 case 0x2B: case 0x2D: /* +, - */
445 goto offset;
446 case 0x5A: /* Z */
447 goto utc_finish;
448 default:
449 errno = EINVAL;
450 return -1;
451 }
452
453 if(buf == end) goto local_finish;
454
455 /*
456 * Parse [mm[ss[(.|,)ffff]]]
457 * ^ ^
458 */
459 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000460 case 0x2C: case 0x2E: /* (.|,) */
461 /*
462 * Process fractions of seconds.
463 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000464 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000465 int v = *buf;
Lev Walkin9a338f82012-01-22 16:38:37 -0800466 /* GCC 4.x is being too smart without volatile */
Lev Walkin535612a2005-07-03 05:32:40 +0000467 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000468 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
469 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin27126182012-09-03 01:09:54 -0700470 if(fvalue < INT_MAX/10) {
471 fvalue = fvalue * 10 + (v - 0x30);
Lev Walkin3a522782005-07-04 12:21:51 +0000472 fdigits++;
Lev Walkin27126182012-09-03 01:09:54 -0700473 } else {
474 /* Not enough precision, ignore */
Lev Walkin535612a2005-07-03 05:32:40 +0000475 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 continue;
477 default:
478 break;
479 }
480 break;
481 }
482 }
483
484 if(buf == end) goto local_finish;
485
486 switch(*buf) {
487 case 0x2B: case 0x2D: /* +, - */
488 goto offset;
489 case 0x5A: /* Z */
490 goto utc_finish;
491 default:
492 errno = EINVAL;
493 return -1;
494 }
495
496
497offset:
498
499 if(end - buf < 3) {
500 errno = EINVAL;
501 return -1;
502 }
503 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000504 B2F(gmtoff_h);
505 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000507 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000508 else
Lev Walkin99006362004-08-07 03:52:26 +0000509 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000510
511 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000512 B2F(gmtoff_m);
513 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000514 } else if(end != buf) {
515 errno = EINVAL;
516 return -1;
517 }
518
Lev Walkin99006362004-08-07 03:52:26 +0000519 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000520
521 /* Fall through */
522utc_finish:
523
524 offset_specified = 1;
525
526 /* Fall through */
527local_finish:
528
529 /*
530 * Validation.
531 */
532 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
533 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
534 || (tm_s.tm_hour > 23)
535 || (tm_s.tm_sec > 60)
536 ) {
537 errno = EINVAL;
538 return -1;
539 }
540
541 /* Canonicalize */
542 tm_s.tm_mon -= 1; /* 0 - 11 */
543 tm_s.tm_year -= 1900;
544 tm_s.tm_isdst = -1;
545
Lev Walkin99006362004-08-07 03:52:26 +0000546 tm_s.tm_sec -= gmtoff;
547
548 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
549
Lev Walkin02a31552004-08-12 03:52:53 +0000550 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000551 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000552 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000553 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000554 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000555 * we can only guess that it is a local zone.
556 * Interpret it in this fashion.
557 */
558 tloc = mktime(&tm_s);
559 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000560 if(tloc == -1) {
561 errno = EINVAL;
562 return -1;
563 }
564
Lev Walkin99006362004-08-07 03:52:26 +0000565 if(ret_tm) {
566 if(as_gmt) {
567 if(offset_specified) {
568 *ret_tm = tm_s;
569 } else {
570 if(gmtime_r(&tloc, ret_tm) == 0) {
571 errno = EINVAL;
572 return -1;
573 }
574 }
575 } else {
576 if(localtime_r(&tloc, ret_tm) == 0) {
577 errno = EINVAL;
578 return -1;
579 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000580 }
581 }
582
Lev Walkin535612a2005-07-03 05:32:40 +0000583 /* Fractions of seconds */
584 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000585 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000586
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 return tloc;
588}
589
Lev Walkin99006362004-08-07 03:52:26 +0000590GeneralizedTime_t *
591asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000592 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
593}
594
595GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000596asn_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 +0000597 struct tm tm_s;
598 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000599 const unsigned int buf_size =
600 4 + 2 + 2 /* yyyymmdd */
601 + 2 + 2 + 2 /* hhmmss */
602 + 1 + 6 /* .ffffff */
603 + 1 + 4 /* +hhmm */
604 + 1 /* '\0' */
605 ;
Lev Walkin99006362004-08-07 03:52:26 +0000606 char *buf;
607 char *p;
608 int size;
609
610 /* Check arguments */
611 if(!tm) {
612 errno = EINVAL;
613 return 0;
614 }
615
616 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000617 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000618 if(!buf) return 0;
619
620 gmtoff = GMTOFF(*tm);
621
622 if(force_gmt && gmtoff) {
623 tm_s = *tm;
624 tm_s.tm_sec -= gmtoff;
625 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000626 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000627#ifdef HAVE_TM_GMTOFF
628 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000629#else /* !HAVE_TM_GMTOFF */
630 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000631#endif
Lev Walkin99006362004-08-07 03:52:26 +0000632 }
633
634 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
635 tm->tm_year + 1900,
636 tm->tm_mon + 1,
637 tm->tm_mday,
638 tm->tm_hour,
639 tm->tm_min,
640 tm->tm_sec
641 );
Lev Walkin535612a2005-07-03 05:32:40 +0000642 if(size != 14) {
643 /* Could be assert(size == 14); */
644 FREEMEM(buf);
645 errno = EINVAL;
646 return 0;
647 }
Lev Walkin99006362004-08-07 03:52:26 +0000648
649 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000650
651 /*
652 * Deal with fractions.
653 */
Lev Walkin3a522782005-07-04 12:21:51 +0000654 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000655 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000656 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000657 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000658 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000659
660 /* Place bounds on precision */
661 while(frac_digits-- > 6)
662 frac_value /= 10;
663
664 /* emulate fbase = pow(10, frac_digits) */
665 for(fbase = 1; frac_digits--;)
666 fbase *= 10;
667
Lev Walkin535612a2005-07-03 05:32:40 +0000668 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000669 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000670 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000671 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000672 frac_value %= fbase;
673 fbase /= 10;
674 } while(fbase > 0 && frac_value > 0 && z < end);
675 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000676 for(--z; *z == 0x30; --z); /* Strip zeroes */
677 p = z + (*z != '.');
678 size = p - buf;
679 }
Lev Walkin535612a2005-07-03 05:32:40 +0000680 }
681
Lev Walkin99006362004-08-07 03:52:26 +0000682 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000683 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000684 *p++ = 0;
685 size++;
686 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000687 int ret;
688 gmtoff %= 86400;
689 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
Lev Walkina4347cc2010-03-10 23:05:56 +0000690 gmtoff / 3600, labs(gmtoff % 3600) / 60);
Lev Walkin535612a2005-07-03 05:32:40 +0000691 if(ret != 5) {
692 FREEMEM(buf);
693 errno = EINVAL;
694 return 0;
695 }
Lev Walkin99006362004-08-07 03:52:26 +0000696 size += ret;
697 }
698
699 if(opt_gt) {
700 if(opt_gt->buf)
701 FREEMEM(opt_gt->buf);
702 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000703 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin419f6752006-09-13 04:02:00 +0000704 if(!opt_gt) { FREEMEM(buf); return 0; }
Lev Walkin99006362004-08-07 03:52:26 +0000705 }
706
Lev Walkin4d9528c2004-08-11 08:10:13 +0000707 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000708 opt_gt->size = size;
709
710 return opt_gt;
711}
712
713