blob: 90be7cdfbca35349a274ac20c5d23d50534b8009 [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 Walkincd2f48e2017-08-10 02:14:59 -0700179 OCTET_STRING_compare, /* Does not normalize time zones! */
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 GeneralizedTime_constraint, /* Check validity of time */
181 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000182 GeneralizedTime_encode_der,
183 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000184 GeneralizedTime_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -0700185#ifdef ASN_DISABLE_OER_SUPPORT
186 0,
187 0,
188#else
189 0,
190 0,
191#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +0400192#ifdef ASN_DISABLE_PER_SUPPORT
193 0,
194 0,
195#else
196 OCTET_STRING_decode_uper,
197 OCTET_STRING_encode_uper,
198#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000200 asn_DEF_GeneralizedTime_tags,
201 sizeof(asn_DEF_GeneralizedTime_tags)
202 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
203 asn_DEF_GeneralizedTime_tags,
204 sizeof(asn_DEF_GeneralizedTime_tags)
205 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -0700206 0, /* No OER visible constraints */
Lev Walkin725883b2006-10-09 12:07:58 +0000207 &asn_DEF_GeneralizedTime_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +0000208 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000209 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000210};
211
Lev Walkin7c1dc052016-03-14 03:08:15 -0700212#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000213
214/*
215 * Check that the time looks like the time.
216 */
217int
Lev Walkin5e033762004-09-29 13:26:15 +0000218GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000219 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000220 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000221 time_t tloc;
222
223 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000224 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000225 if(tloc == -1 && errno != EPERM) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700226 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000227 "%s: Invalid time format: %s (%s:%d)",
228 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000229 return -1;
230 }
231
232 return 0;
233}
234
Lev Walkina9cc46e2004-09-22 16:06:28 +0000235asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000236GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000237 int tag_mode, ber_tlv_tag_t tag,
238 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000239 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000240 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000241 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000242 struct tm tm;
243 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000244
Lev Walkin535612a2005-07-03 05:32:40 +0000245 /*
246 * Encode as a canonical DER.
247 */
248 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000249 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000250 if(tloc == -1 && errno != EPERM)
251 /* Failed to recognize time. Fail completely. */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700252 ASN__ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000253
Lev Walkin3a522782005-07-04 12:21:51 +0000254 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700255 if(!st) ASN__ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000256
257 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
258
Lev Walkin535612a2005-07-03 05:32:40 +0000259 FREEMEM(st->buf);
260 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000261
262 return erval;
263}
264
Lev Walkin7c1dc052016-03-14 03:08:15 -0700265#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkin535612a2005-07-03 05:32:40 +0000266
Lev Walkina9cc46e2004-09-22 16:06:28 +0000267asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000268GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000269 int ilevel, enum xer_encoder_flags_e flags,
270 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000271
272 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000273 GeneralizedTime_t *gt;
274 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000275 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000276 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000277
278 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000279 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000280 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000281 && errno != EPERM)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700282 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000283
Lev Walkin3a522782005-07-04 12:21:51 +0000284 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700285 if(!gt) ASN__ENCODE_FAILED;
Lev Walkin535612a2005-07-03 05:32:40 +0000286
287 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
288 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000289 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000290 return rv;
291 } else {
292 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
293 cb, app_key);
294 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000295}
296
Lev Walkin7c1dc052016-03-14 03:08:15 -0700297#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkin535612a2005-07-03 05:32:40 +0000298
Lev Walkinf15320b2004-06-03 03:38:44 +0000299int
Lev Walkin5e033762004-09-29 13:26:15 +0000300GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000302 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000303
Lev Walkind9bd7752004-06-05 08:17:50 +0000304 (void)td; /* Unused argument */
305 (void)ilevel; /* Unused argument */
306
Lev Walkinf15320b2004-06-03 03:38:44 +0000307 if(st && st->buf) {
308 char buf[32];
309 struct tm tm;
310 int ret;
311
312 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000313 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000314 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000315
316 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000317 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000318 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
319 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000320 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000321 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000323 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000324 }
325}
326
Lev Walkinf15320b2004-06-03 03:38:44 +0000327time_t
Lev Walkin99006362004-08-07 03:52:26 +0000328asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000329 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
330}
331
332time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000333asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
334 time_t tloc;
335 int fv, fd = 0;
336
337 if(frac_value)
338 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
339 else
340 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
341 if(fd == 0 || frac_digits <= 0) {
342 *frac_value = 0;
343 } else {
344 while(fd > frac_digits)
345 fv /= 10, fd--;
346 while(fd < frac_digits) {
Lev Walkin27126182012-09-03 01:09:54 -0700347 if(fv < INT_MAX / 10) {
348 fv *= 10;
349 fd++;
350 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000351 /* Too long precision request */
352 fv = 0;
353 break;
354 }
Lev Walkin3a522782005-07-04 12:21:51 +0000355 }
356
357 *frac_value = fv;
358 }
359
360 return tloc;
361}
362
363time_t
364asn_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 +0000365 struct tm tm_s;
366 uint8_t *buf;
367 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000368 int gmtoff_h = 0;
369 int gmtoff_m = 0;
370 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000371 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000372 int fvalue = 0;
373 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000374 time_t tloc;
375
376 if(!st || !st->buf) {
377 errno = EINVAL;
378 return -1;
379 } else {
380 buf = st->buf;
381 end = buf + st->size;
382 }
383
384 if(st->size < 10) {
385 errno = EINVAL;
386 return -1;
387 }
388
389 /*
390 * Decode first 10 bytes: "AAAAMMJJhh"
391 */
392 memset(&tm_s, 0, sizeof(tm_s));
393#undef B2F
394#undef B2T
395#define B2F(var) do { \
396 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000397 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000398 errno = EINVAL; \
399 return -1; \
400 } else { \
401 var = var * 10 + (ch - 0x30); \
402 buf++; \
403 } \
404 } while(0)
405#define B2T(var) B2F(tm_s.var)
406
407 B2T(tm_year); /* 1: A */
408 B2T(tm_year); /* 2: A */
409 B2T(tm_year); /* 3: A */
410 B2T(tm_year); /* 4: A */
411 B2T(tm_mon); /* 5: M */
412 B2T(tm_mon); /* 6: M */
413 B2T(tm_mday); /* 7: J */
414 B2T(tm_mday); /* 8: J */
415 B2T(tm_hour); /* 9: h */
416 B2T(tm_hour); /* 0: h */
417
418 if(buf == end) goto local_finish;
419
420 /*
421 * Parse [mm[ss[(.|,)ffff]]]
422 * ^^
423 */
424 switch(*buf) {
425 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
426 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
427 tm_s.tm_min = (*buf++) - 0x30;
428 if(buf == end) { errno = EINVAL; return -1; }
429 B2T(tm_min);
430 break;
431 case 0x2B: case 0x2D: /* +, - */
432 goto offset;
433 case 0x5A: /* Z */
434 goto utc_finish;
435 default:
436 errno = EINVAL;
437 return -1;
438 }
439
440 if(buf == end) goto local_finish;
441
442 /*
443 * Parse [mm[ss[(.|,)ffff]]]
444 * ^^
445 */
446 switch(*buf) {
447 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
448 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
449 tm_s.tm_sec = (*buf++) - 0x30;
450 if(buf == end) { errno = EINVAL; return -1; }
451 B2T(tm_sec);
452 break;
453 case 0x2B: case 0x2D: /* +, - */
454 goto offset;
455 case 0x5A: /* Z */
456 goto utc_finish;
457 default:
458 errno = EINVAL;
459 return -1;
460 }
461
462 if(buf == end) goto local_finish;
463
464 /*
465 * Parse [mm[ss[(.|,)ffff]]]
466 * ^ ^
467 */
468 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000469 case 0x2C: case 0x2E: /* (.|,) */
470 /*
471 * Process fractions of seconds.
472 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000474 int v = *buf;
Lev Walkin9a338f82012-01-22 16:38:37 -0800475 /* GCC 4.x is being too smart without volatile */
Lev Walkin535612a2005-07-03 05:32:40 +0000476 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
478 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin27126182012-09-03 01:09:54 -0700479 if(fvalue < INT_MAX/10) {
480 fvalue = fvalue * 10 + (v - 0x30);
Lev Walkin3a522782005-07-04 12:21:51 +0000481 fdigits++;
Lev Walkin27126182012-09-03 01:09:54 -0700482 } else {
483 /* Not enough precision, ignore */
Lev Walkin535612a2005-07-03 05:32:40 +0000484 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000485 continue;
486 default:
487 break;
488 }
489 break;
490 }
491 }
492
493 if(buf == end) goto local_finish;
494
495 switch(*buf) {
496 case 0x2B: case 0x2D: /* +, - */
497 goto offset;
498 case 0x5A: /* Z */
499 goto utc_finish;
500 default:
501 errno = EINVAL;
502 return -1;
503 }
504
505
506offset:
507
508 if(end - buf < 3) {
509 errno = EINVAL;
510 return -1;
511 }
512 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000513 B2F(gmtoff_h);
514 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000516 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000517 else
Lev Walkin99006362004-08-07 03:52:26 +0000518 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000519
520 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000521 B2F(gmtoff_m);
522 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 } else if(end != buf) {
524 errno = EINVAL;
525 return -1;
526 }
527
Lev Walkin99006362004-08-07 03:52:26 +0000528 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000529
530 /* Fall through */
531utc_finish:
532
533 offset_specified = 1;
534
535 /* Fall through */
536local_finish:
537
538 /*
539 * Validation.
540 */
541 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
542 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
543 || (tm_s.tm_hour > 23)
544 || (tm_s.tm_sec > 60)
545 ) {
546 errno = EINVAL;
547 return -1;
548 }
549
550 /* Canonicalize */
551 tm_s.tm_mon -= 1; /* 0 - 11 */
552 tm_s.tm_year -= 1900;
553 tm_s.tm_isdst = -1;
554
Lev Walkin99006362004-08-07 03:52:26 +0000555 tm_s.tm_sec -= gmtoff;
556
557 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
558
Lev Walkin02a31552004-08-12 03:52:53 +0000559 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000560 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000561 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000562 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000563 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000564 * we can only guess that it is a local zone.
565 * Interpret it in this fashion.
566 */
567 tloc = mktime(&tm_s);
568 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000569 if(tloc == -1) {
570 errno = EINVAL;
571 return -1;
572 }
573
Lev Walkin99006362004-08-07 03:52:26 +0000574 if(ret_tm) {
575 if(as_gmt) {
576 if(offset_specified) {
577 *ret_tm = tm_s;
578 } else {
579 if(gmtime_r(&tloc, ret_tm) == 0) {
580 errno = EINVAL;
581 return -1;
582 }
583 }
584 } else {
585 if(localtime_r(&tloc, ret_tm) == 0) {
586 errno = EINVAL;
587 return -1;
588 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000589 }
590 }
591
Lev Walkin535612a2005-07-03 05:32:40 +0000592 /* Fractions of seconds */
593 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000594 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000595
Lev Walkinf15320b2004-06-03 03:38:44 +0000596 return tloc;
597}
598
Lev Walkin99006362004-08-07 03:52:26 +0000599GeneralizedTime_t *
600asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000601 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
602}
603
604GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000605asn_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 +0000606 struct tm tm_s;
607 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000608 const unsigned int buf_size =
609 4 + 2 + 2 /* yyyymmdd */
610 + 2 + 2 + 2 /* hhmmss */
611 + 1 + 6 /* .ffffff */
612 + 1 + 4 /* +hhmm */
613 + 1 /* '\0' */
614 ;
Lev Walkin99006362004-08-07 03:52:26 +0000615 char *buf;
616 char *p;
617 int size;
618
619 /* Check arguments */
620 if(!tm) {
621 errno = EINVAL;
622 return 0;
623 }
624
625 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000626 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000627 if(!buf) return 0;
628
629 gmtoff = GMTOFF(*tm);
630
631 if(force_gmt && gmtoff) {
632 tm_s = *tm;
633 tm_s.tm_sec -= gmtoff;
634 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000635 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000636#ifdef HAVE_TM_GMTOFF
637 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000638#else /* !HAVE_TM_GMTOFF */
639 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000640#endif
Lev Walkin99006362004-08-07 03:52:26 +0000641 }
642
643 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
644 tm->tm_year + 1900,
645 tm->tm_mon + 1,
646 tm->tm_mday,
647 tm->tm_hour,
648 tm->tm_min,
649 tm->tm_sec
650 );
Lev Walkin535612a2005-07-03 05:32:40 +0000651 if(size != 14) {
652 /* Could be assert(size == 14); */
653 FREEMEM(buf);
654 errno = EINVAL;
655 return 0;
656 }
Lev Walkin99006362004-08-07 03:52:26 +0000657
658 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000659
660 /*
661 * Deal with fractions.
662 */
Lev Walkin3a522782005-07-04 12:21:51 +0000663 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000664 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000665 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000666 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000667 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000668
669 /* Place bounds on precision */
670 while(frac_digits-- > 6)
671 frac_value /= 10;
672
673 /* emulate fbase = pow(10, frac_digits) */
674 for(fbase = 1; frac_digits--;)
675 fbase *= 10;
676
Lev Walkin535612a2005-07-03 05:32:40 +0000677 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000678 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000679 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000680 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000681 frac_value %= fbase;
682 fbase /= 10;
683 } while(fbase > 0 && frac_value > 0 && z < end);
684 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000685 for(--z; *z == 0x30; --z); /* Strip zeroes */
686 p = z + (*z != '.');
687 size = p - buf;
688 }
Lev Walkin535612a2005-07-03 05:32:40 +0000689 }
690
Lev Walkin99006362004-08-07 03:52:26 +0000691 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000692 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000693 *p++ = 0;
694 size++;
695 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000696 int ret;
697 gmtoff %= 86400;
698 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
Lev Walkina4347cc2010-03-10 23:05:56 +0000699 gmtoff / 3600, labs(gmtoff % 3600) / 60);
Lev Walkin535612a2005-07-03 05:32:40 +0000700 if(ret != 5) {
701 FREEMEM(buf);
702 errno = EINVAL;
703 return 0;
704 }
Lev Walkin99006362004-08-07 03:52:26 +0000705 size += ret;
706 }
707
708 if(opt_gt) {
709 if(opt_gt->buf)
710 FREEMEM(opt_gt->buf);
711 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000712 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin419f6752006-09-13 04:02:00 +0000713 if(!opt_gt) { FREEMEM(buf); return 0; }
Lev Walkin99006362004-08-07 03:52:26 +0000714 }
715
Lev Walkin4d9528c2004-08-11 08:10:13 +0000716 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000717 opt_gt->size = size;
718
719 return opt_gt;
720}
721
722