blob: c4a547a0c6014d8f768bbc2c2c15f3d44f035775 [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
Lev Walkinc8c2cb52006-07-13 13:20:19 +000070#if (defined(_EMULATE_TIMEGM) || !defined(HAVE_TM_GMTOFF))
71#warning "PLEASE STOP AND READ!"
72#warning " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe."
73#warning " "
74#warning " You must fix the code by inserting appropriate locking"
75#warning " if you want to use asn_GT2time() or asn_UT2time()."
76#warning "PLEASE STOP AND READ!"
77#endif /* _EMULATE_TIMEGM */
78
Lev Walkin93e9fe32004-09-17 06:46:10 +000079/*
80 * Override our GMTOFF decision for other known platforms.
81 */
82#ifdef __CYGWIN__
83#undef GMTOFF
84static long GMTOFF(struct tm a){
85 struct tm *lt;
86 time_t local_time, gmt_time;
87 long zone;
88
89 tzset();
90 gmt_time = time (NULL);
91
92 lt = gmtime(&gmt_time);
93
94 local_time = mktime(lt);
95 return (gmt_time - local_time);
96}
97#define _EMULATE_TIMEGM
98
99#endif /* __CYGWIN__ */
100
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000101#define ATZVARS do { \
102 char tzoldbuf[64]; \
103 char *tzold
104#define ATZSAVETZ do { \
105 tzold = getenv("TZ"); \
106 if(tzold) { \
107 size_t tzlen = strlen(tzold); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000108 if(tzlen < sizeof(tzoldbuf)) { \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000109 tzold = memcpy(tzoldbuf, tzold, tzlen + 1); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000110 } else { \
Lev Walkinb9671d12007-10-02 11:23:24 +0000111 char *dupptr = tzold; \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000112 tzold = MALLOC(tzlen + 1); \
113 if(tzold) memcpy(tzold, dupptr, tzlen + 1); \
114 } \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000115 setenv("TZ", "UTC", 1); \
116 } \
117 tzset(); \
118} while(0)
119#define ATZOLDTZ do { \
120 if (tzold) { \
121 setenv("TZ", tzold, 1); \
122 *tzoldbuf = 0; \
123 if(tzold != tzoldbuf) \
Lev Walkin419f6752006-09-13 04:02:00 +0000124 FREEMEM(tzold); \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000125 } else { \
126 unsetenv("TZ"); \
127 } \
128 tzset(); \
129} while(0); } while(0);
130
Lev Walkin93e9fe32004-09-17 06:46:10 +0000131#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +0000132static time_t timegm(struct tm *tm) {
133 time_t tloc;
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000134 ATZVARS;
135 ATZSAVETZ;
Lev Walkindb424002004-08-12 03:58:25 +0000136 tloc = mktime(tm);
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000137 ATZOLDTZ;
Lev Walkindb424002004-08-12 03:58:25 +0000138 return tloc;
139}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000140#endif /* _EMULATE_TIMEGM */
Lev Walkindb424002004-08-12 03:58:25 +0000141
Lev Walkin64399722004-08-11 07:17:22 +0000142
Lev Walkin535612a2005-07-03 05:32:40 +0000143#ifndef __ASN_INTERNAL_TEST_MODE__
Lev Walkinf15320b2004-06-03 03:38:44 +0000144
145/*
146 * GeneralizedTime basic type description.
147 */
Lev Walkin5e033762004-09-29 13:26:15 +0000148static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000149 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
150 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
151 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000152};
Lev Walkin725883b2006-10-09 12:07:58 +0000153static asn_per_constraints_t asn_DEF_GeneralizedTime_constraints = {
154 { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
155 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
156 0, 0
157};
Lev Walkin5e033762004-09-29 13:26:15 +0000158asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +0000159 "GeneralizedTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +0000160 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000161 OCTET_STRING_free,
162 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000163 GeneralizedTime_constraint, /* Check validity of time */
164 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000165 GeneralizedTime_encode_der,
166 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000167 GeneralizedTime_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +0000168 OCTET_STRING_decode_uper,
169 OCTET_STRING_encode_uper,
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000171 asn_DEF_GeneralizedTime_tags,
172 sizeof(asn_DEF_GeneralizedTime_tags)
173 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
174 asn_DEF_GeneralizedTime_tags,
175 sizeof(asn_DEF_GeneralizedTime_tags)
176 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin725883b2006-10-09 12:07:58 +0000177 &asn_DEF_GeneralizedTime_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +0000178 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000179 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000180};
181
Lev Walkin535612a2005-07-03 05:32:40 +0000182#endif /* __ASN_INTERNAL_TEST_MODE__ */
Lev Walkinf15320b2004-06-03 03:38:44 +0000183
184/*
185 * Check that the time looks like the time.
186 */
187int
Lev Walkin5e033762004-09-29 13:26:15 +0000188GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000189 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000190 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 time_t tloc;
192
193 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000194 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 if(tloc == -1 && errno != EPERM) {
Lev Walkined9019a2006-10-16 12:18:41 +0000196 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000197 "%s: Invalid time format: %s (%s:%d)",
198 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 return -1;
200 }
201
202 return 0;
203}
204
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000206GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000207 int tag_mode, ber_tlv_tag_t tag,
208 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000209 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000210 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000211 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000212 struct tm tm;
213 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000214
Lev Walkin535612a2005-07-03 05:32:40 +0000215 /*
216 * Encode as a canonical DER.
217 */
218 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000219 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000220 if(tloc == -1 && errno != EPERM)
221 /* Failed to recognize time. Fail completely. */
222 _ASN_ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000223
Lev Walkin3a522782005-07-04 12:21:51 +0000224 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin535612a2005-07-03 05:32:40 +0000225 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000226
227 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
228
Lev Walkin535612a2005-07-03 05:32:40 +0000229 FREEMEM(st->buf);
230 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000231
232 return erval;
233}
234
Lev Walkin535612a2005-07-03 05:32:40 +0000235#ifndef __ASN_INTERNAL_TEST_MODE__
236
Lev Walkina9cc46e2004-09-22 16:06:28 +0000237asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000238GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000239 int ilevel, enum xer_encoder_flags_e flags,
240 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000241
242 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000243 GeneralizedTime_t *gt;
244 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000245 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000246 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000247
248 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000249 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000250 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000251 && errno != EPERM)
252 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000253
Lev Walkin3a522782005-07-04 12:21:51 +0000254 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin535612a2005-07-03 05:32:40 +0000255 if(!gt) _ASN_ENCODE_FAILED;
256
257 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
258 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000259 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000260 return rv;
261 } else {
262 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
263 cb, app_key);
264 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000265}
266
Lev Walkin535612a2005-07-03 05:32:40 +0000267#endif /* __ASN_INTERNAL_TEST_MODE__ */
268
Lev Walkinf15320b2004-06-03 03:38:44 +0000269int
Lev Walkin5e033762004-09-29 13:26:15 +0000270GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000272 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000273
Lev Walkind9bd7752004-06-05 08:17:50 +0000274 (void)td; /* Unused argument */
275 (void)ilevel; /* Unused argument */
276
Lev Walkinf15320b2004-06-03 03:38:44 +0000277 if(st && st->buf) {
278 char buf[32];
279 struct tm tm;
280 int ret;
281
282 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000283 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000284 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000285
286 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000287 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000288 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
289 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000290 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000291 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000292 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000293 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000294 }
295}
296
Lev Walkinf15320b2004-06-03 03:38:44 +0000297time_t
Lev Walkin99006362004-08-07 03:52:26 +0000298asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000299 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
300}
301
302time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000303asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
304 time_t tloc;
305 int fv, fd = 0;
306
307 if(frac_value)
308 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
309 else
310 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
311 if(fd == 0 || frac_digits <= 0) {
312 *frac_value = 0;
313 } else {
314 while(fd > frac_digits)
315 fv /= 10, fd--;
316 while(fd < frac_digits) {
Lev Walkin27126182012-09-03 01:09:54 -0700317 if(fv < INT_MAX / 10) {
318 fv *= 10;
319 fd++;
320 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000321 /* Too long precision request */
322 fv = 0;
323 break;
324 }
Lev Walkin3a522782005-07-04 12:21:51 +0000325 }
326
327 *frac_value = fv;
328 }
329
330 return tloc;
331}
332
333time_t
334asn_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 +0000335 struct tm tm_s;
336 uint8_t *buf;
337 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000338 int gmtoff_h = 0;
339 int gmtoff_m = 0;
340 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000341 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000342 int fvalue = 0;
343 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000344 time_t tloc;
345
346 if(!st || !st->buf) {
347 errno = EINVAL;
348 return -1;
349 } else {
350 buf = st->buf;
351 end = buf + st->size;
352 }
353
354 if(st->size < 10) {
355 errno = EINVAL;
356 return -1;
357 }
358
359 /*
360 * Decode first 10 bytes: "AAAAMMJJhh"
361 */
362 memset(&tm_s, 0, sizeof(tm_s));
363#undef B2F
364#undef B2T
365#define B2F(var) do { \
366 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000367 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000368 errno = EINVAL; \
369 return -1; \
370 } else { \
371 var = var * 10 + (ch - 0x30); \
372 buf++; \
373 } \
374 } while(0)
375#define B2T(var) B2F(tm_s.var)
376
377 B2T(tm_year); /* 1: A */
378 B2T(tm_year); /* 2: A */
379 B2T(tm_year); /* 3: A */
380 B2T(tm_year); /* 4: A */
381 B2T(tm_mon); /* 5: M */
382 B2T(tm_mon); /* 6: M */
383 B2T(tm_mday); /* 7: J */
384 B2T(tm_mday); /* 8: J */
385 B2T(tm_hour); /* 9: h */
386 B2T(tm_hour); /* 0: h */
387
388 if(buf == end) goto local_finish;
389
390 /*
391 * Parse [mm[ss[(.|,)ffff]]]
392 * ^^
393 */
394 switch(*buf) {
395 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
396 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
397 tm_s.tm_min = (*buf++) - 0x30;
398 if(buf == end) { errno = EINVAL; return -1; }
399 B2T(tm_min);
400 break;
401 case 0x2B: case 0x2D: /* +, - */
402 goto offset;
403 case 0x5A: /* Z */
404 goto utc_finish;
405 default:
406 errno = EINVAL;
407 return -1;
408 }
409
410 if(buf == end) goto local_finish;
411
412 /*
413 * Parse [mm[ss[(.|,)ffff]]]
414 * ^^
415 */
416 switch(*buf) {
417 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
418 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
419 tm_s.tm_sec = (*buf++) - 0x30;
420 if(buf == end) { errno = EINVAL; return -1; }
421 B2T(tm_sec);
422 break;
423 case 0x2B: case 0x2D: /* +, - */
424 goto offset;
425 case 0x5A: /* Z */
426 goto utc_finish;
427 default:
428 errno = EINVAL;
429 return -1;
430 }
431
432 if(buf == end) goto local_finish;
433
434 /*
435 * Parse [mm[ss[(.|,)ffff]]]
436 * ^ ^
437 */
438 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000439 case 0x2C: case 0x2E: /* (.|,) */
440 /*
441 * Process fractions of seconds.
442 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000443 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000444 int v = *buf;
Lev Walkin9a338f82012-01-22 16:38:37 -0800445 int volatile new_fvalue;
446 /* GCC 4.x is being too smart without volatile */
Lev Walkin535612a2005-07-03 05:32:40 +0000447 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000448 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
449 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin27126182012-09-03 01:09:54 -0700450 if(fvalue < INT_MAX/10) {
451 fvalue = fvalue * 10 + (v - 0x30);
Lev Walkin3a522782005-07-04 12:21:51 +0000452 fdigits++;
Lev Walkin27126182012-09-03 01:09:54 -0700453 } else {
454 /* Not enough precision, ignore */
Lev Walkin535612a2005-07-03 05:32:40 +0000455 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000456 continue;
457 default:
458 break;
459 }
460 break;
461 }
462 }
463
464 if(buf == end) goto local_finish;
465
466 switch(*buf) {
467 case 0x2B: case 0x2D: /* +, - */
468 goto offset;
469 case 0x5A: /* Z */
470 goto utc_finish;
471 default:
472 errno = EINVAL;
473 return -1;
474 }
475
476
477offset:
478
479 if(end - buf < 3) {
480 errno = EINVAL;
481 return -1;
482 }
483 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000484 B2F(gmtoff_h);
485 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000486 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000487 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000488 else
Lev Walkin99006362004-08-07 03:52:26 +0000489 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000490
491 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000492 B2F(gmtoff_m);
493 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 } else if(end != buf) {
495 errno = EINVAL;
496 return -1;
497 }
498
Lev Walkin99006362004-08-07 03:52:26 +0000499 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000500
501 /* Fall through */
502utc_finish:
503
504 offset_specified = 1;
505
506 /* Fall through */
507local_finish:
508
509 /*
510 * Validation.
511 */
512 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
513 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
514 || (tm_s.tm_hour > 23)
515 || (tm_s.tm_sec > 60)
516 ) {
517 errno = EINVAL;
518 return -1;
519 }
520
521 /* Canonicalize */
522 tm_s.tm_mon -= 1; /* 0 - 11 */
523 tm_s.tm_year -= 1900;
524 tm_s.tm_isdst = -1;
525
Lev Walkin99006362004-08-07 03:52:26 +0000526 tm_s.tm_sec -= gmtoff;
527
528 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
529
Lev Walkin02a31552004-08-12 03:52:53 +0000530 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000531 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000532 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000533 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000534 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000535 * we can only guess that it is a local zone.
536 * Interpret it in this fashion.
537 */
538 tloc = mktime(&tm_s);
539 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000540 if(tloc == -1) {
541 errno = EINVAL;
542 return -1;
543 }
544
Lev Walkin99006362004-08-07 03:52:26 +0000545 if(ret_tm) {
546 if(as_gmt) {
547 if(offset_specified) {
548 *ret_tm = tm_s;
549 } else {
550 if(gmtime_r(&tloc, ret_tm) == 0) {
551 errno = EINVAL;
552 return -1;
553 }
554 }
555 } else {
556 if(localtime_r(&tloc, ret_tm) == 0) {
557 errno = EINVAL;
558 return -1;
559 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000560 }
561 }
562
Lev Walkin535612a2005-07-03 05:32:40 +0000563 /* Fractions of seconds */
564 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000565 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000566
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 return tloc;
568}
569
Lev Walkin99006362004-08-07 03:52:26 +0000570GeneralizedTime_t *
571asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000572 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
573}
574
575GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000576asn_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 +0000577 struct tm tm_s;
578 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000579 const unsigned int buf_size =
580 4 + 2 + 2 /* yyyymmdd */
581 + 2 + 2 + 2 /* hhmmss */
582 + 1 + 6 /* .ffffff */
583 + 1 + 4 /* +hhmm */
584 + 1 /* '\0' */
585 ;
Lev Walkin99006362004-08-07 03:52:26 +0000586 char *buf;
587 char *p;
588 int size;
589
590 /* Check arguments */
591 if(!tm) {
592 errno = EINVAL;
593 return 0;
594 }
595
596 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000597 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000598 if(!buf) return 0;
599
600 gmtoff = GMTOFF(*tm);
601
602 if(force_gmt && gmtoff) {
603 tm_s = *tm;
604 tm_s.tm_sec -= gmtoff;
605 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000606 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000607#ifdef HAVE_TM_GMTOFF
608 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000609#else /* !HAVE_TM_GMTOFF */
610 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000611#endif
Lev Walkin99006362004-08-07 03:52:26 +0000612 }
613
614 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
615 tm->tm_year + 1900,
616 tm->tm_mon + 1,
617 tm->tm_mday,
618 tm->tm_hour,
619 tm->tm_min,
620 tm->tm_sec
621 );
Lev Walkin535612a2005-07-03 05:32:40 +0000622 if(size != 14) {
623 /* Could be assert(size == 14); */
624 FREEMEM(buf);
625 errno = EINVAL;
626 return 0;
627 }
Lev Walkin99006362004-08-07 03:52:26 +0000628
629 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000630
631 /*
632 * Deal with fractions.
633 */
Lev Walkin3a522782005-07-04 12:21:51 +0000634 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000635 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000636 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000637 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000638 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000639
640 /* Place bounds on precision */
641 while(frac_digits-- > 6)
642 frac_value /= 10;
643
644 /* emulate fbase = pow(10, frac_digits) */
645 for(fbase = 1; frac_digits--;)
646 fbase *= 10;
647
Lev Walkin535612a2005-07-03 05:32:40 +0000648 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000649 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000650 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000651 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000652 frac_value %= fbase;
653 fbase /= 10;
654 } while(fbase > 0 && frac_value > 0 && z < end);
655 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000656 for(--z; *z == 0x30; --z); /* Strip zeroes */
657 p = z + (*z != '.');
658 size = p - buf;
659 }
Lev Walkin535612a2005-07-03 05:32:40 +0000660 }
661
Lev Walkin99006362004-08-07 03:52:26 +0000662 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000663 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000664 *p++ = 0;
665 size++;
666 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000667 int ret;
668 gmtoff %= 86400;
669 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
Lev Walkina4347cc2010-03-10 23:05:56 +0000670 gmtoff / 3600, labs(gmtoff % 3600) / 60);
Lev Walkin535612a2005-07-03 05:32:40 +0000671 if(ret != 5) {
672 FREEMEM(buf);
673 errno = EINVAL;
674 return 0;
675 }
Lev Walkin99006362004-08-07 03:52:26 +0000676 size += ret;
677 }
678
679 if(opt_gt) {
680 if(opt_gt->buf)
681 FREEMEM(opt_gt->buf);
682 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000683 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin419f6752006-09-13 04:02:00 +0000684 if(!opt_gt) { FREEMEM(buf); return 0; }
Lev Walkin99006362004-08-07 03:52:26 +0000685 }
686
Lev Walkin4d9528c2004-08-11 08:10:13 +0000687 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000688 opt_gt->size = size;
689
690 return opt_gt;
691}
692
693