blob: 7e144777ae1f0542fa696868fcd6cd1b41d0952a [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 Walkin7623d8c2016-03-14 03:55:21 -07007#define _BSD_SOURCE /* for timegm(3) */
Lev Walkina9cc46e2004-09-22 16:06:28 +00008#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <GeneralizedTime.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 Walkin7623d8c2016-03-14 03:55:21 -070017#include <stdio.h>
18#include <errno.h>
19
Lev Walkin93659562010-11-20 09:47:13 -080020#if defined(_WIN32)
Lev Walkina57953d2005-06-15 19:01:45 +000021#pragma message( "PLEASE STOP AND READ!")
22#pragma message( " localtime_r is implemented via localtime(), which may be not thread-safe.")
23#pragma message( " gmtime_r is implemented via gmtime(), which may be not thread-safe.")
24#pragma message( " ")
25#pragma message( " You must fix the code by inserting appropriate locking")
26#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
Lev Walkinc8c2cb52006-07-13 13:20:19 +000027#pragma message( "PLEASE STOP AND READ!")
Lev Walkin02a31552004-08-12 03:52:53 +000028
Lev Walkincec43b52004-09-02 05:20:39 +000029static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000030 struct tm *tm;
31 if((tm = localtime(tloc)))
32 return memcpy(result, tm, sizeof(struct tm));
33 return 0;
34}
35
Lev Walkincec43b52004-09-02 05:20:39 +000036static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000037 struct tm *tm;
38 if((tm = gmtime(tloc)))
39 return memcpy(result, tm, sizeof(struct tm));
40 return 0;
41}
42
Lev Walkin93e9fe32004-09-17 06:46:10 +000043#define tzset() _tzset()
Lev Walkina57953d2005-06-15 19:01:45 +000044#define putenv(c) _putenv(c)
Lev Walkin93e9fe32004-09-17 06:46:10 +000045#define _EMULATE_TIMEGM
46
Lev Walkin93659562010-11-20 09:47:13 -080047#endif /* _WIN32 */
Lev Walkin93e9fe32004-09-17 06:46:10 +000048
Lev Walkinc8c2cb52006-07-13 13:20:19 +000049#if defined(sun) || defined(_sun_) || defined(__solaris__)
Lev Walkina460ba32004-10-20 15:40:04 +000050#define _EMULATE_TIMEGM
51#endif
52
Lev Walkin93e9fe32004-09-17 06:46:10 +000053/*
54 * Where to look for offset from GMT, Phase I.
55 * Several platforms are known.
56 */
57#if defined(__FreeBSD__) \
58 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
59 || (defined __GLIBC__ && __GLIBC__ >= 2)
60#undef HAVE_TM_GMTOFF
61#define HAVE_TM_GMTOFF
62#endif /* BSDs and newer glibc */
63
64/*
65 * Where to look for offset from GMT, Phase II.
66 */
67#ifdef HAVE_TM_GMTOFF
68#define GMTOFF(tm) ((tm).tm_gmtoff)
69#else /* HAVE_TM_GMTOFF */
70#define GMTOFF(tm) (-timezone)
71#endif /* HAVE_TM_GMTOFF */
72
Frank Morgner917a81d2013-05-21 09:52:19 +020073#if defined(_WIN32)
74#pragma message( "PLEASE STOP AND READ!")
75#pragma message( " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe.")
76#pragma message( " ")
77#pragma message( " You must fix the code by inserting appropriate locking")
78#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
79#pragma message( "PLEASE STOP AND READ!")
80#else
Lev Walkinc8c2cb52006-07-13 13:20:19 +000081#if (defined(_EMULATE_TIMEGM) || !defined(HAVE_TM_GMTOFF))
82#warning "PLEASE STOP AND READ!"
83#warning " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe."
84#warning " "
85#warning " You must fix the code by inserting appropriate locking"
86#warning " if you want to use asn_GT2time() or asn_UT2time()."
87#warning "PLEASE STOP AND READ!"
88#endif /* _EMULATE_TIMEGM */
Frank Morgner917a81d2013-05-21 09:52:19 +020089#endif
Lev Walkinc8c2cb52006-07-13 13:20:19 +000090
Lev Walkin93e9fe32004-09-17 06:46:10 +000091/*
92 * Override our GMTOFF decision for other known platforms.
93 */
94#ifdef __CYGWIN__
95#undef GMTOFF
96static long GMTOFF(struct tm a){
97 struct tm *lt;
98 time_t local_time, gmt_time;
99 long zone;
100
101 tzset();
102 gmt_time = time (NULL);
103
104 lt = gmtime(&gmt_time);
105
106 local_time = mktime(lt);
107 return (gmt_time - local_time);
108}
109#define _EMULATE_TIMEGM
110
111#endif /* __CYGWIN__ */
112
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000113#define ATZVARS do { \
114 char tzoldbuf[64]; \
115 char *tzold
116#define ATZSAVETZ do { \
117 tzold = getenv("TZ"); \
118 if(tzold) { \
119 size_t tzlen = strlen(tzold); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000120 if(tzlen < sizeof(tzoldbuf)) { \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000121 tzold = memcpy(tzoldbuf, tzold, tzlen + 1); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000122 } else { \
Lev Walkinb9671d12007-10-02 11:23:24 +0000123 char *dupptr = tzold; \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000124 tzold = MALLOC(tzlen + 1); \
125 if(tzold) memcpy(tzold, dupptr, tzlen + 1); \
126 } \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000127 setenv("TZ", "UTC", 1); \
128 } \
129 tzset(); \
130} while(0)
131#define ATZOLDTZ do { \
132 if (tzold) { \
133 setenv("TZ", tzold, 1); \
134 *tzoldbuf = 0; \
135 if(tzold != tzoldbuf) \
Lev Walkin419f6752006-09-13 04:02:00 +0000136 FREEMEM(tzold); \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000137 } else { \
138 unsetenv("TZ"); \
139 } \
140 tzset(); \
141} while(0); } while(0);
142
Lev Walkin04fbe622014-09-17 02:27:01 -0700143#ifndef HAVE_TIMEGM
Lev Walkin93e9fe32004-09-17 06:46:10 +0000144#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +0000145static time_t timegm(struct tm *tm) {
146 time_t tloc;
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000147 ATZVARS;
148 ATZSAVETZ;
Lev Walkindb424002004-08-12 03:58:25 +0000149 tloc = mktime(tm);
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000150 ATZOLDTZ;
Lev Walkindb424002004-08-12 03:58:25 +0000151 return tloc;
152}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000153#endif /* _EMULATE_TIMEGM */
Lev Walkin04fbe622014-09-17 02:27:01 -0700154#endif
Lev Walkindb424002004-08-12 03:58:25 +0000155
Lev Walkin64399722004-08-11 07:17:22 +0000156
Lev Walkin7c1dc052016-03-14 03:08:15 -0700157#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkinf15320b2004-06-03 03:38:44 +0000158
159/*
160 * GeneralizedTime basic type description.
161 */
Wim Lewis18c2ec92014-07-29 11:30:10 -0700162static const ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000163 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
164 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
165 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000166};
Lev Walkin725883b2006-10-09 12:07:58 +0000167static asn_per_constraints_t asn_DEF_GeneralizedTime_constraints = {
168 { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
169 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
170 0, 0
171};
Lev Walkin5e033762004-09-29 13:26:15 +0000172asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 "GeneralizedTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +0000174 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000175 OCTET_STRING_free,
176 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 GeneralizedTime_constraint, /* Check validity of time */
178 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000179 GeneralizedTime_encode_der,
180 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000181 GeneralizedTime_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +0000182 OCTET_STRING_decode_uper,
183 OCTET_STRING_encode_uper,
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000185 asn_DEF_GeneralizedTime_tags,
186 sizeof(asn_DEF_GeneralizedTime_tags)
187 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
188 asn_DEF_GeneralizedTime_tags,
189 sizeof(asn_DEF_GeneralizedTime_tags)
190 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin725883b2006-10-09 12:07:58 +0000191 &asn_DEF_GeneralizedTime_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +0000192 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000193 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194};
195
Lev Walkin7c1dc052016-03-14 03:08:15 -0700196#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000197
198/*
199 * Check that the time looks like the time.
200 */
201int
Lev Walkin5e033762004-09-29 13:26:15 +0000202GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000203 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000204 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000205 time_t tloc;
206
207 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000208 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000209 if(tloc == -1 && errno != EPERM) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700210 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000211 "%s: Invalid time format: %s (%s:%d)",
212 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 return -1;
214 }
215
216 return 0;
217}
218
Lev Walkina9cc46e2004-09-22 16:06:28 +0000219asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000220GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000221 int tag_mode, ber_tlv_tag_t tag,
222 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000223 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000224 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000225 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000226 struct tm tm;
227 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000228
Lev Walkin535612a2005-07-03 05:32:40 +0000229 /*
230 * Encode as a canonical DER.
231 */
232 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000233 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000234 if(tloc == -1 && errno != EPERM)
235 /* Failed to recognize time. Fail completely. */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700236 ASN__ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000237
Lev Walkin3a522782005-07-04 12:21:51 +0000238 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700239 if(!st) ASN__ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000240
241 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
242
Lev Walkin535612a2005-07-03 05:32:40 +0000243 FREEMEM(st->buf);
244 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000245
246 return erval;
247}
248
Lev Walkin7c1dc052016-03-14 03:08:15 -0700249#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkin535612a2005-07-03 05:32:40 +0000250
Lev Walkina9cc46e2004-09-22 16:06:28 +0000251asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000252GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000253 int ilevel, enum xer_encoder_flags_e flags,
254 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000255
256 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000257 GeneralizedTime_t *gt;
258 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000259 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000260 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000261
262 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000263 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000264 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000265 && errno != EPERM)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700266 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000267
Lev Walkin3a522782005-07-04 12:21:51 +0000268 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700269 if(!gt) ASN__ENCODE_FAILED;
Lev Walkin535612a2005-07-03 05:32:40 +0000270
271 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
272 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000273 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000274 return rv;
275 } else {
276 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
277 cb, app_key);
278 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000279}
280
Lev Walkin7c1dc052016-03-14 03:08:15 -0700281#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkin535612a2005-07-03 05:32:40 +0000282
Lev Walkinf15320b2004-06-03 03:38:44 +0000283int
Lev Walkin5e033762004-09-29 13:26:15 +0000284GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000285 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000286 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000287
Lev Walkind9bd7752004-06-05 08:17:50 +0000288 (void)td; /* Unused argument */
289 (void)ilevel; /* Unused argument */
290
Lev Walkinf15320b2004-06-03 03:38:44 +0000291 if(st && st->buf) {
292 char buf[32];
293 struct tm tm;
294 int ret;
295
296 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000297 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000298 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000299
300 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000301 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000302 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
303 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000304 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000305 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000306 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000307 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000308 }
309}
310
Lev Walkinf15320b2004-06-03 03:38:44 +0000311time_t
Lev Walkin99006362004-08-07 03:52:26 +0000312asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000313 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
314}
315
316time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000317asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
318 time_t tloc;
319 int fv, fd = 0;
320
321 if(frac_value)
322 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
323 else
324 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
325 if(fd == 0 || frac_digits <= 0) {
326 *frac_value = 0;
327 } else {
328 while(fd > frac_digits)
329 fv /= 10, fd--;
330 while(fd < frac_digits) {
Lev Walkin27126182012-09-03 01:09:54 -0700331 if(fv < INT_MAX / 10) {
332 fv *= 10;
333 fd++;
334 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000335 /* Too long precision request */
336 fv = 0;
337 break;
338 }
Lev Walkin3a522782005-07-04 12:21:51 +0000339 }
340
341 *frac_value = fv;
342 }
343
344 return tloc;
345}
346
347time_t
348asn_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 +0000349 struct tm tm_s;
350 uint8_t *buf;
351 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000352 int gmtoff_h = 0;
353 int gmtoff_m = 0;
354 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000355 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000356 int fvalue = 0;
357 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000358 time_t tloc;
359
360 if(!st || !st->buf) {
361 errno = EINVAL;
362 return -1;
363 } else {
364 buf = st->buf;
365 end = buf + st->size;
366 }
367
368 if(st->size < 10) {
369 errno = EINVAL;
370 return -1;
371 }
372
373 /*
374 * Decode first 10 bytes: "AAAAMMJJhh"
375 */
376 memset(&tm_s, 0, sizeof(tm_s));
377#undef B2F
378#undef B2T
379#define B2F(var) do { \
380 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000381 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000382 errno = EINVAL; \
383 return -1; \
384 } else { \
385 var = var * 10 + (ch - 0x30); \
386 buf++; \
387 } \
388 } while(0)
389#define B2T(var) B2F(tm_s.var)
390
391 B2T(tm_year); /* 1: A */
392 B2T(tm_year); /* 2: A */
393 B2T(tm_year); /* 3: A */
394 B2T(tm_year); /* 4: A */
395 B2T(tm_mon); /* 5: M */
396 B2T(tm_mon); /* 6: M */
397 B2T(tm_mday); /* 7: J */
398 B2T(tm_mday); /* 8: J */
399 B2T(tm_hour); /* 9: h */
400 B2T(tm_hour); /* 0: h */
401
402 if(buf == end) goto local_finish;
403
404 /*
405 * Parse [mm[ss[(.|,)ffff]]]
406 * ^^
407 */
408 switch(*buf) {
409 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
410 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
411 tm_s.tm_min = (*buf++) - 0x30;
412 if(buf == end) { errno = EINVAL; return -1; }
413 B2T(tm_min);
414 break;
415 case 0x2B: case 0x2D: /* +, - */
416 goto offset;
417 case 0x5A: /* Z */
418 goto utc_finish;
419 default:
420 errno = EINVAL;
421 return -1;
422 }
423
424 if(buf == end) goto local_finish;
425
426 /*
427 * Parse [mm[ss[(.|,)ffff]]]
428 * ^^
429 */
430 switch(*buf) {
431 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
432 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
433 tm_s.tm_sec = (*buf++) - 0x30;
434 if(buf == end) { errno = EINVAL; return -1; }
435 B2T(tm_sec);
436 break;
437 case 0x2B: case 0x2D: /* +, - */
438 goto offset;
439 case 0x5A: /* Z */
440 goto utc_finish;
441 default:
442 errno = EINVAL;
443 return -1;
444 }
445
446 if(buf == end) goto local_finish;
447
448 /*
449 * Parse [mm[ss[(.|,)ffff]]]
450 * ^ ^
451 */
452 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000453 case 0x2C: case 0x2E: /* (.|,) */
454 /*
455 * Process fractions of seconds.
456 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000457 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000458 int v = *buf;
Lev Walkin9a338f82012-01-22 16:38:37 -0800459 /* GCC 4.x is being too smart without volatile */
Lev Walkin535612a2005-07-03 05:32:40 +0000460 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000461 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
462 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin27126182012-09-03 01:09:54 -0700463 if(fvalue < INT_MAX/10) {
464 fvalue = fvalue * 10 + (v - 0x30);
Lev Walkin3a522782005-07-04 12:21:51 +0000465 fdigits++;
Lev Walkin27126182012-09-03 01:09:54 -0700466 } else {
467 /* Not enough precision, ignore */
Lev Walkin535612a2005-07-03 05:32:40 +0000468 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000469 continue;
470 default:
471 break;
472 }
473 break;
474 }
475 }
476
477 if(buf == end) goto local_finish;
478
479 switch(*buf) {
480 case 0x2B: case 0x2D: /* +, - */
481 goto offset;
482 case 0x5A: /* Z */
483 goto utc_finish;
484 default:
485 errno = EINVAL;
486 return -1;
487 }
488
489
490offset:
491
492 if(end - buf < 3) {
493 errno = EINVAL;
494 return -1;
495 }
496 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000497 B2F(gmtoff_h);
498 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000499 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000500 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000501 else
Lev Walkin99006362004-08-07 03:52:26 +0000502 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000503
504 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000505 B2F(gmtoff_m);
506 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000507 } else if(end != buf) {
508 errno = EINVAL;
509 return -1;
510 }
511
Lev Walkin99006362004-08-07 03:52:26 +0000512 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000513
514 /* Fall through */
515utc_finish:
516
517 offset_specified = 1;
518
519 /* Fall through */
520local_finish:
521
522 /*
523 * Validation.
524 */
525 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
526 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
527 || (tm_s.tm_hour > 23)
528 || (tm_s.tm_sec > 60)
529 ) {
530 errno = EINVAL;
531 return -1;
532 }
533
534 /* Canonicalize */
535 tm_s.tm_mon -= 1; /* 0 - 11 */
536 tm_s.tm_year -= 1900;
537 tm_s.tm_isdst = -1;
538
Lev Walkin99006362004-08-07 03:52:26 +0000539 tm_s.tm_sec -= gmtoff;
540
541 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
542
Lev Walkin02a31552004-08-12 03:52:53 +0000543 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000544 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000545 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000546 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000547 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000548 * we can only guess that it is a local zone.
549 * Interpret it in this fashion.
550 */
551 tloc = mktime(&tm_s);
552 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 if(tloc == -1) {
554 errno = EINVAL;
555 return -1;
556 }
557
Lev Walkin99006362004-08-07 03:52:26 +0000558 if(ret_tm) {
559 if(as_gmt) {
560 if(offset_specified) {
561 *ret_tm = tm_s;
562 } else {
563 if(gmtime_r(&tloc, ret_tm) == 0) {
564 errno = EINVAL;
565 return -1;
566 }
567 }
568 } else {
569 if(localtime_r(&tloc, ret_tm) == 0) {
570 errno = EINVAL;
571 return -1;
572 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000573 }
574 }
575
Lev Walkin535612a2005-07-03 05:32:40 +0000576 /* Fractions of seconds */
577 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000578 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000579
Lev Walkinf15320b2004-06-03 03:38:44 +0000580 return tloc;
581}
582
Lev Walkin99006362004-08-07 03:52:26 +0000583GeneralizedTime_t *
584asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000585 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
586}
587
588GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000589asn_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 +0000590 struct tm tm_s;
591 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000592 const unsigned int buf_size =
593 4 + 2 + 2 /* yyyymmdd */
594 + 2 + 2 + 2 /* hhmmss */
595 + 1 + 6 /* .ffffff */
596 + 1 + 4 /* +hhmm */
597 + 1 /* '\0' */
598 ;
Lev Walkin99006362004-08-07 03:52:26 +0000599 char *buf;
600 char *p;
601 int size;
602
603 /* Check arguments */
604 if(!tm) {
605 errno = EINVAL;
606 return 0;
607 }
608
609 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000610 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000611 if(!buf) return 0;
612
613 gmtoff = GMTOFF(*tm);
614
615 if(force_gmt && gmtoff) {
616 tm_s = *tm;
617 tm_s.tm_sec -= gmtoff;
618 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000619 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000620#ifdef HAVE_TM_GMTOFF
621 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000622#else /* !HAVE_TM_GMTOFF */
623 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000624#endif
Lev Walkin99006362004-08-07 03:52:26 +0000625 }
626
627 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
628 tm->tm_year + 1900,
629 tm->tm_mon + 1,
630 tm->tm_mday,
631 tm->tm_hour,
632 tm->tm_min,
633 tm->tm_sec
634 );
Lev Walkin535612a2005-07-03 05:32:40 +0000635 if(size != 14) {
636 /* Could be assert(size == 14); */
637 FREEMEM(buf);
638 errno = EINVAL;
639 return 0;
640 }
Lev Walkin99006362004-08-07 03:52:26 +0000641
642 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000643
644 /*
645 * Deal with fractions.
646 */
Lev Walkin3a522782005-07-04 12:21:51 +0000647 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000648 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000649 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000650 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000651 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000652
653 /* Place bounds on precision */
654 while(frac_digits-- > 6)
655 frac_value /= 10;
656
657 /* emulate fbase = pow(10, frac_digits) */
658 for(fbase = 1; frac_digits--;)
659 fbase *= 10;
660
Lev Walkin535612a2005-07-03 05:32:40 +0000661 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000662 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000663 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000664 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000665 frac_value %= fbase;
666 fbase /= 10;
667 } while(fbase > 0 && frac_value > 0 && z < end);
668 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000669 for(--z; *z == 0x30; --z); /* Strip zeroes */
670 p = z + (*z != '.');
671 size = p - buf;
672 }
Lev Walkin535612a2005-07-03 05:32:40 +0000673 }
674
Lev Walkin99006362004-08-07 03:52:26 +0000675 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000676 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000677 *p++ = 0;
678 size++;
679 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000680 int ret;
681 gmtoff %= 86400;
682 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
Lev Walkina4347cc2010-03-10 23:05:56 +0000683 gmtoff / 3600, labs(gmtoff % 3600) / 60);
Lev Walkin535612a2005-07-03 05:32:40 +0000684 if(ret != 5) {
685 FREEMEM(buf);
686 errno = EINVAL;
687 return 0;
688 }
Lev Walkin99006362004-08-07 03:52:26 +0000689 size += ret;
690 }
691
692 if(opt_gt) {
693 if(opt_gt->buf)
694 FREEMEM(opt_gt->buf);
695 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000696 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin419f6752006-09-13 04:02:00 +0000697 if(!opt_gt) { FREEMEM(buf); return 0; }
Lev Walkin99006362004-08-07 03:52:26 +0000698 }
699
Lev Walkin4d9528c2004-08-11 08:10:13 +0000700 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000701 opt_gt->size = size;
702
703 return opt_gt;
704}
705
706