blob: 3db79109632cd664fce0718e9758e8f515ad7150 [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 Walkin93e9fe32004-09-17 06:46:10 +0000140#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +0000141static time_t timegm(struct tm *tm) {
142 time_t tloc;
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000143 ATZVARS;
144 ATZSAVETZ;
Lev Walkindb424002004-08-12 03:58:25 +0000145 tloc = mktime(tm);
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000146 ATZOLDTZ;
Lev Walkindb424002004-08-12 03:58:25 +0000147 return tloc;
148}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000149#endif /* _EMULATE_TIMEGM */
Lev Walkindb424002004-08-12 03:58:25 +0000150
Lev Walkin64399722004-08-11 07:17:22 +0000151
Lev Walkin535612a2005-07-03 05:32:40 +0000152#ifndef __ASN_INTERNAL_TEST_MODE__
Lev Walkinf15320b2004-06-03 03:38:44 +0000153
154/*
155 * GeneralizedTime basic type description.
156 */
Lev Walkin5e033762004-09-29 13:26:15 +0000157static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000158 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
159 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
160 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000161};
Lev Walkin725883b2006-10-09 12:07:58 +0000162static asn_per_constraints_t asn_DEF_GeneralizedTime_constraints = {
163 { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
164 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
165 0, 0
166};
Lev Walkin5e033762004-09-29 13:26:15 +0000167asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +0000168 "GeneralizedTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +0000169 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000170 OCTET_STRING_free,
171 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 GeneralizedTime_constraint, /* Check validity of time */
173 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000174 GeneralizedTime_encode_der,
175 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000176 GeneralizedTime_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +0000177 OCTET_STRING_decode_uper,
178 OCTET_STRING_encode_uper,
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000180 asn_DEF_GeneralizedTime_tags,
181 sizeof(asn_DEF_GeneralizedTime_tags)
182 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
183 asn_DEF_GeneralizedTime_tags,
184 sizeof(asn_DEF_GeneralizedTime_tags)
185 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin725883b2006-10-09 12:07:58 +0000186 &asn_DEF_GeneralizedTime_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +0000187 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000188 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000189};
190
Lev Walkin535612a2005-07-03 05:32:40 +0000191#endif /* __ASN_INTERNAL_TEST_MODE__ */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192
193/*
194 * Check that the time looks like the time.
195 */
196int
Lev Walkin5e033762004-09-29 13:26:15 +0000197GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000198 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000199 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 time_t tloc;
201
202 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000203 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000204 if(tloc == -1 && errno != EPERM) {
Lev Walkined9019a2006-10-16 12:18:41 +0000205 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000206 "%s: Invalid time format: %s (%s:%d)",
207 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 return -1;
209 }
210
211 return 0;
212}
213
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000215GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000216 int tag_mode, ber_tlv_tag_t tag,
217 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000218 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000219 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000220 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000221 struct tm tm;
222 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000223
Lev Walkin535612a2005-07-03 05:32:40 +0000224 /*
225 * Encode as a canonical DER.
226 */
227 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000228 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000229 if(tloc == -1 && errno != EPERM)
230 /* Failed to recognize time. Fail completely. */
231 _ASN_ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000232
Lev Walkin3a522782005-07-04 12:21:51 +0000233 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin535612a2005-07-03 05:32:40 +0000234 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000235
236 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
237
Lev Walkin535612a2005-07-03 05:32:40 +0000238 FREEMEM(st->buf);
239 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000240
241 return erval;
242}
243
Lev Walkin535612a2005-07-03 05:32:40 +0000244#ifndef __ASN_INTERNAL_TEST_MODE__
245
Lev Walkina9cc46e2004-09-22 16:06:28 +0000246asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000247GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000248 int ilevel, enum xer_encoder_flags_e flags,
249 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000250
251 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000252 GeneralizedTime_t *gt;
253 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000254 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000255 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000256
257 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000258 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000259 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000260 && errno != EPERM)
261 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000262
Lev Walkin3a522782005-07-04 12:21:51 +0000263 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin535612a2005-07-03 05:32:40 +0000264 if(!gt) _ASN_ENCODE_FAILED;
265
266 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
267 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000268 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000269 return rv;
270 } else {
271 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
272 cb, app_key);
273 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000274}
275
Lev Walkin535612a2005-07-03 05:32:40 +0000276#endif /* __ASN_INTERNAL_TEST_MODE__ */
277
Lev Walkinf15320b2004-06-03 03:38:44 +0000278int
Lev Walkin5e033762004-09-29 13:26:15 +0000279GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000280 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000281 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000282
Lev Walkind9bd7752004-06-05 08:17:50 +0000283 (void)td; /* Unused argument */
284 (void)ilevel; /* Unused argument */
285
Lev Walkinf15320b2004-06-03 03:38:44 +0000286 if(st && st->buf) {
287 char buf[32];
288 struct tm tm;
289 int ret;
290
291 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000292 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000293 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000294
295 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000296 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000297 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
298 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000299 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000300 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000302 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 }
304}
305
Lev Walkinf15320b2004-06-03 03:38:44 +0000306time_t
Lev Walkin99006362004-08-07 03:52:26 +0000307asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000308 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
309}
310
311time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000312asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
313 time_t tloc;
314 int fv, fd = 0;
315
316 if(frac_value)
317 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
318 else
319 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
320 if(fd == 0 || frac_digits <= 0) {
321 *frac_value = 0;
322 } else {
323 while(fd > frac_digits)
324 fv /= 10, fd--;
325 while(fd < frac_digits) {
Lev Walkin27126182012-09-03 01:09:54 -0700326 if(fv < INT_MAX / 10) {
327 fv *= 10;
328 fd++;
329 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000330 /* Too long precision request */
331 fv = 0;
332 break;
333 }
Lev Walkin3a522782005-07-04 12:21:51 +0000334 }
335
336 *frac_value = fv;
337 }
338
339 return tloc;
340}
341
342time_t
343asn_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 +0000344 struct tm tm_s;
345 uint8_t *buf;
346 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000347 int gmtoff_h = 0;
348 int gmtoff_m = 0;
349 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000350 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000351 int fvalue = 0;
352 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000353 time_t tloc;
354
355 if(!st || !st->buf) {
356 errno = EINVAL;
357 return -1;
358 } else {
359 buf = st->buf;
360 end = buf + st->size;
361 }
362
363 if(st->size < 10) {
364 errno = EINVAL;
365 return -1;
366 }
367
368 /*
369 * Decode first 10 bytes: "AAAAMMJJhh"
370 */
371 memset(&tm_s, 0, sizeof(tm_s));
372#undef B2F
373#undef B2T
374#define B2F(var) do { \
375 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000376 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000377 errno = EINVAL; \
378 return -1; \
379 } else { \
380 var = var * 10 + (ch - 0x30); \
381 buf++; \
382 } \
383 } while(0)
384#define B2T(var) B2F(tm_s.var)
385
386 B2T(tm_year); /* 1: A */
387 B2T(tm_year); /* 2: A */
388 B2T(tm_year); /* 3: A */
389 B2T(tm_year); /* 4: A */
390 B2T(tm_mon); /* 5: M */
391 B2T(tm_mon); /* 6: M */
392 B2T(tm_mday); /* 7: J */
393 B2T(tm_mday); /* 8: J */
394 B2T(tm_hour); /* 9: h */
395 B2T(tm_hour); /* 0: h */
396
397 if(buf == end) goto local_finish;
398
399 /*
400 * Parse [mm[ss[(.|,)ffff]]]
401 * ^^
402 */
403 switch(*buf) {
404 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
405 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
406 tm_s.tm_min = (*buf++) - 0x30;
407 if(buf == end) { errno = EINVAL; return -1; }
408 B2T(tm_min);
409 break;
410 case 0x2B: case 0x2D: /* +, - */
411 goto offset;
412 case 0x5A: /* Z */
413 goto utc_finish;
414 default:
415 errno = EINVAL;
416 return -1;
417 }
418
419 if(buf == end) goto local_finish;
420
421 /*
422 * Parse [mm[ss[(.|,)ffff]]]
423 * ^^
424 */
425 switch(*buf) {
426 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
427 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
428 tm_s.tm_sec = (*buf++) - 0x30;
429 if(buf == end) { errno = EINVAL; return -1; }
430 B2T(tm_sec);
431 break;
432 case 0x2B: case 0x2D: /* +, - */
433 goto offset;
434 case 0x5A: /* Z */
435 goto utc_finish;
436 default:
437 errno = EINVAL;
438 return -1;
439 }
440
441 if(buf == end) goto local_finish;
442
443 /*
444 * Parse [mm[ss[(.|,)ffff]]]
445 * ^ ^
446 */
447 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000448 case 0x2C: case 0x2E: /* (.|,) */
449 /*
450 * Process fractions of seconds.
451 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000452 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000453 int v = *buf;
Lev Walkin9a338f82012-01-22 16:38:37 -0800454 /* GCC 4.x is being too smart without volatile */
Lev Walkin535612a2005-07-03 05:32:40 +0000455 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000456 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
457 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin27126182012-09-03 01:09:54 -0700458 if(fvalue < INT_MAX/10) {
459 fvalue = fvalue * 10 + (v - 0x30);
Lev Walkin3a522782005-07-04 12:21:51 +0000460 fdigits++;
Lev Walkin27126182012-09-03 01:09:54 -0700461 } else {
462 /* Not enough precision, ignore */
Lev Walkin535612a2005-07-03 05:32:40 +0000463 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000464 continue;
465 default:
466 break;
467 }
468 break;
469 }
470 }
471
472 if(buf == end) goto local_finish;
473
474 switch(*buf) {
475 case 0x2B: case 0x2D: /* +, - */
476 goto offset;
477 case 0x5A: /* Z */
478 goto utc_finish;
479 default:
480 errno = EINVAL;
481 return -1;
482 }
483
484
485offset:
486
487 if(end - buf < 3) {
488 errno = EINVAL;
489 return -1;
490 }
491 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000492 B2F(gmtoff_h);
493 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000495 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000496 else
Lev Walkin99006362004-08-07 03:52:26 +0000497 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000498
499 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000500 B2F(gmtoff_m);
501 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000502 } else if(end != buf) {
503 errno = EINVAL;
504 return -1;
505 }
506
Lev Walkin99006362004-08-07 03:52:26 +0000507 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000508
509 /* Fall through */
510utc_finish:
511
512 offset_specified = 1;
513
514 /* Fall through */
515local_finish:
516
517 /*
518 * Validation.
519 */
520 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
521 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
522 || (tm_s.tm_hour > 23)
523 || (tm_s.tm_sec > 60)
524 ) {
525 errno = EINVAL;
526 return -1;
527 }
528
529 /* Canonicalize */
530 tm_s.tm_mon -= 1; /* 0 - 11 */
531 tm_s.tm_year -= 1900;
532 tm_s.tm_isdst = -1;
533
Lev Walkin99006362004-08-07 03:52:26 +0000534 tm_s.tm_sec -= gmtoff;
535
536 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
537
Lev Walkin02a31552004-08-12 03:52:53 +0000538 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000539 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000540 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000541 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000542 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000543 * we can only guess that it is a local zone.
544 * Interpret it in this fashion.
545 */
546 tloc = mktime(&tm_s);
547 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000548 if(tloc == -1) {
549 errno = EINVAL;
550 return -1;
551 }
552
Lev Walkin99006362004-08-07 03:52:26 +0000553 if(ret_tm) {
554 if(as_gmt) {
555 if(offset_specified) {
556 *ret_tm = tm_s;
557 } else {
558 if(gmtime_r(&tloc, ret_tm) == 0) {
559 errno = EINVAL;
560 return -1;
561 }
562 }
563 } else {
564 if(localtime_r(&tloc, ret_tm) == 0) {
565 errno = EINVAL;
566 return -1;
567 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000568 }
569 }
570
Lev Walkin535612a2005-07-03 05:32:40 +0000571 /* Fractions of seconds */
572 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000573 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000574
Lev Walkinf15320b2004-06-03 03:38:44 +0000575 return tloc;
576}
577
Lev Walkin99006362004-08-07 03:52:26 +0000578GeneralizedTime_t *
579asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000580 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
581}
582
583GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000584asn_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 +0000585 struct tm tm_s;
586 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000587 const unsigned int buf_size =
588 4 + 2 + 2 /* yyyymmdd */
589 + 2 + 2 + 2 /* hhmmss */
590 + 1 + 6 /* .ffffff */
591 + 1 + 4 /* +hhmm */
592 + 1 /* '\0' */
593 ;
Lev Walkin99006362004-08-07 03:52:26 +0000594 char *buf;
595 char *p;
596 int size;
597
598 /* Check arguments */
599 if(!tm) {
600 errno = EINVAL;
601 return 0;
602 }
603
604 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000605 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000606 if(!buf) return 0;
607
608 gmtoff = GMTOFF(*tm);
609
610 if(force_gmt && gmtoff) {
611 tm_s = *tm;
612 tm_s.tm_sec -= gmtoff;
613 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000614 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000615#ifdef HAVE_TM_GMTOFF
616 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000617#else /* !HAVE_TM_GMTOFF */
618 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000619#endif
Lev Walkin99006362004-08-07 03:52:26 +0000620 }
621
622 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
623 tm->tm_year + 1900,
624 tm->tm_mon + 1,
625 tm->tm_mday,
626 tm->tm_hour,
627 tm->tm_min,
628 tm->tm_sec
629 );
Lev Walkin535612a2005-07-03 05:32:40 +0000630 if(size != 14) {
631 /* Could be assert(size == 14); */
632 FREEMEM(buf);
633 errno = EINVAL;
634 return 0;
635 }
Lev Walkin99006362004-08-07 03:52:26 +0000636
637 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000638
639 /*
640 * Deal with fractions.
641 */
Lev Walkin3a522782005-07-04 12:21:51 +0000642 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000643 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000644 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000645 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000646 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000647
648 /* Place bounds on precision */
649 while(frac_digits-- > 6)
650 frac_value /= 10;
651
652 /* emulate fbase = pow(10, frac_digits) */
653 for(fbase = 1; frac_digits--;)
654 fbase *= 10;
655
Lev Walkin535612a2005-07-03 05:32:40 +0000656 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000657 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000658 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000659 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000660 frac_value %= fbase;
661 fbase /= 10;
662 } while(fbase > 0 && frac_value > 0 && z < end);
663 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000664 for(--z; *z == 0x30; --z); /* Strip zeroes */
665 p = z + (*z != '.');
666 size = p - buf;
667 }
Lev Walkin535612a2005-07-03 05:32:40 +0000668 }
669
Lev Walkin99006362004-08-07 03:52:26 +0000670 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000671 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000672 *p++ = 0;
673 size++;
674 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000675 int ret;
676 gmtoff %= 86400;
677 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
Lev Walkina4347cc2010-03-10 23:05:56 +0000678 gmtoff / 3600, labs(gmtoff % 3600) / 60);
Lev Walkin535612a2005-07-03 05:32:40 +0000679 if(ret != 5) {
680 FREEMEM(buf);
681 errno = EINVAL;
682 return 0;
683 }
Lev Walkin99006362004-08-07 03:52:26 +0000684 size += ret;
685 }
686
687 if(opt_gt) {
688 if(opt_gt->buf)
689 FREEMEM(opt_gt->buf);
690 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000691 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin419f6752006-09-13 04:02:00 +0000692 if(!opt_gt) { FREEMEM(buf); return 0; }
Lev Walkin99006362004-08-07 03:52:26 +0000693 }
694
Lev Walkin4d9528c2004-08-11 08:10:13 +0000695 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000696 opt_gt->size = size;
697
698 return opt_gt;
699}
700
701