blob: a421c25e20b9339f71a743c2158c8d296b91cb92 [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};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800174asn_TYPE_operation_t asn_OP_GeneralizedTime = {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000175 OCTET_STRING_free,
176 GeneralizedTime_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -0700177 OCTET_STRING_compare, /* Does not normalize time zones! */
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 GeneralizedTime_constraint, /* Check validity of time */
179 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000180 GeneralizedTime_encode_der,
181 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000182 GeneralizedTime_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -0700183#ifdef ASN_DISABLE_OER_SUPPORT
184 0,
185 0,
186#else
187 0,
188 0,
189#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +0400190#ifdef ASN_DISABLE_PER_SUPPORT
191 0,
192 0,
193#else
194 OCTET_STRING_decode_uper,
195 OCTET_STRING_encode_uper,
196#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800197 0 /* Use generic outmost tag fetcher */
198};
199asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
200 "GeneralizedTime",
201 "GeneralizedTime",
202 &asn_OP_GeneralizedTime,
203 GeneralizedTime_constraint, /* Check validity of time */
Lev Walkin5e033762004-09-29 13:26:15 +0000204 asn_DEF_GeneralizedTime_tags,
205 sizeof(asn_DEF_GeneralizedTime_tags)
206 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
207 asn_DEF_GeneralizedTime_tags,
208 sizeof(asn_DEF_GeneralizedTime_tags)
209 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -0700210 0, /* No OER visible constraints */
Lev Walkin725883b2006-10-09 12:07:58 +0000211 &asn_DEF_GeneralizedTime_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +0000212 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000213 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000214};
215
Lev Walkin7c1dc052016-03-14 03:08:15 -0700216#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000217
218/*
219 * Check that the time looks like the time.
220 */
221int
Lev Walkin5e033762004-09-29 13:26:15 +0000222GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000223 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000224 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000225 time_t tloc;
226
227 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000228 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000229 if(tloc == -1 && errno != EPERM) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700230 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000231 "%s: Invalid time format: %s (%s:%d)",
232 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 return -1;
234 }
235
236 return 0;
237}
238
Lev Walkina9cc46e2004-09-22 16:06:28 +0000239asn_enc_rval_t
Lev Walkin535612a2005-07-03 05:32:40 +0000240GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin99006362004-08-07 03:52:26 +0000241 int tag_mode, ber_tlv_tag_t tag,
242 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin535612a2005-07-03 05:32:40 +0000243 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000244 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000245 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000246 struct tm tm;
247 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000248
Lev Walkin535612a2005-07-03 05:32:40 +0000249 /*
250 * Encode as a canonical DER.
251 */
252 errno = EPERM;
Lev Walkin3a522782005-07-04 12:21:51 +0000253 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
Lev Walkin535612a2005-07-03 05:32:40 +0000254 if(tloc == -1 && errno != EPERM)
255 /* Failed to recognize time. Fail completely. */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700256 ASN__ENCODE_FAILED;
Lev Walkin99006362004-08-07 03:52:26 +0000257
Lev Walkin3a522782005-07-04 12:21:51 +0000258 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700259 if(!st) ASN__ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000260
261 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
262
Lev Walkin535612a2005-07-03 05:32:40 +0000263 FREEMEM(st->buf);
264 FREEMEM(st);
Lev Walkin99006362004-08-07 03:52:26 +0000265
266 return erval;
267}
268
Lev Walkin7c1dc052016-03-14 03:08:15 -0700269#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkin535612a2005-07-03 05:32:40 +0000270
Lev Walkina9cc46e2004-09-22 16:06:28 +0000271asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000272GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000273 int ilevel, enum xer_encoder_flags_e flags,
274 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000275
276 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000277 GeneralizedTime_t *gt;
278 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000279 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000280 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000281
282 errno = EPERM;
Lev Walkin535612a2005-07-03 05:32:40 +0000283 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000284 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000285 && errno != EPERM)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700286 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000287
Lev Walkin3a522782005-07-04 12:21:51 +0000288 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700289 if(!gt) ASN__ENCODE_FAILED;
Lev Walkin535612a2005-07-03 05:32:40 +0000290
291 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
292 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000293 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000294 return rv;
295 } else {
296 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
297 cb, app_key);
298 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000299}
300
Lev Walkin7c1dc052016-03-14 03:08:15 -0700301#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkin535612a2005-07-03 05:32:40 +0000302
Lev Walkinf15320b2004-06-03 03:38:44 +0000303int
Lev Walkin5e033762004-09-29 13:26:15 +0000304GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000305 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000306 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000307
Lev Walkind9bd7752004-06-05 08:17:50 +0000308 (void)td; /* Unused argument */
309 (void)ilevel; /* Unused argument */
310
Lev Walkinf15320b2004-06-03 03:38:44 +0000311 if(st && st->buf) {
312 char buf[32];
313 struct tm tm;
314 int ret;
315
316 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000317 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000318 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000319
320 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000321 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
323 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000324 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000325 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000326 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000327 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000328 }
329}
330
Lev Walkinf15320b2004-06-03 03:38:44 +0000331time_t
Lev Walkin99006362004-08-07 03:52:26 +0000332asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000333 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
334}
335
336time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000337asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
338 time_t tloc;
339 int fv, fd = 0;
340
341 if(frac_value)
342 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
343 else
344 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
345 if(fd == 0 || frac_digits <= 0) {
346 *frac_value = 0;
347 } else {
348 while(fd > frac_digits)
349 fv /= 10, fd--;
350 while(fd < frac_digits) {
Lev Walkin27126182012-09-03 01:09:54 -0700351 if(fv < INT_MAX / 10) {
352 fv *= 10;
353 fd++;
354 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000355 /* Too long precision request */
356 fv = 0;
357 break;
358 }
Lev Walkin3a522782005-07-04 12:21:51 +0000359 }
360
361 *frac_value = fv;
362 }
363
364 return tloc;
365}
366
367time_t
368asn_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 +0000369 struct tm tm_s;
370 uint8_t *buf;
371 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000372 int gmtoff_h = 0;
373 int gmtoff_m = 0;
374 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000375 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000376 int fvalue = 0;
377 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000378 time_t tloc;
379
380 if(!st || !st->buf) {
381 errno = EINVAL;
382 return -1;
383 } else {
384 buf = st->buf;
385 end = buf + st->size;
386 }
387
388 if(st->size < 10) {
389 errno = EINVAL;
390 return -1;
391 }
392
393 /*
394 * Decode first 10 bytes: "AAAAMMJJhh"
395 */
396 memset(&tm_s, 0, sizeof(tm_s));
397#undef B2F
398#undef B2T
399#define B2F(var) do { \
400 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000401 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 errno = EINVAL; \
403 return -1; \
404 } else { \
405 var = var * 10 + (ch - 0x30); \
406 buf++; \
407 } \
408 } while(0)
409#define B2T(var) B2F(tm_s.var)
410
411 B2T(tm_year); /* 1: A */
412 B2T(tm_year); /* 2: A */
413 B2T(tm_year); /* 3: A */
414 B2T(tm_year); /* 4: A */
415 B2T(tm_mon); /* 5: M */
416 B2T(tm_mon); /* 6: M */
417 B2T(tm_mday); /* 7: J */
418 B2T(tm_mday); /* 8: J */
419 B2T(tm_hour); /* 9: h */
420 B2T(tm_hour); /* 0: h */
421
422 if(buf == end) goto local_finish;
423
424 /*
425 * Parse [mm[ss[(.|,)ffff]]]
426 * ^^
427 */
428 switch(*buf) {
429 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
430 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
431 tm_s.tm_min = (*buf++) - 0x30;
432 if(buf == end) { errno = EINVAL; return -1; }
433 B2T(tm_min);
434 break;
435 case 0x2B: case 0x2D: /* +, - */
436 goto offset;
437 case 0x5A: /* Z */
438 goto utc_finish;
439 default:
440 errno = EINVAL;
441 return -1;
442 }
443
444 if(buf == end) goto local_finish;
445
446 /*
447 * Parse [mm[ss[(.|,)ffff]]]
448 * ^^
449 */
450 switch(*buf) {
451 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
452 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
453 tm_s.tm_sec = (*buf++) - 0x30;
454 if(buf == end) { errno = EINVAL; return -1; }
455 B2T(tm_sec);
456 break;
457 case 0x2B: case 0x2D: /* +, - */
458 goto offset;
459 case 0x5A: /* Z */
460 goto utc_finish;
461 default:
462 errno = EINVAL;
463 return -1;
464 }
465
466 if(buf == end) goto local_finish;
467
468 /*
469 * Parse [mm[ss[(.|,)ffff]]]
470 * ^ ^
471 */
472 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000473 case 0x2C: case 0x2E: /* (.|,) */
474 /*
475 * Process fractions of seconds.
476 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000478 int v = *buf;
Lev Walkin9a338f82012-01-22 16:38:37 -0800479 /* GCC 4.x is being too smart without volatile */
Lev Walkin535612a2005-07-03 05:32:40 +0000480 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000481 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
482 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin27126182012-09-03 01:09:54 -0700483 if(fvalue < INT_MAX/10) {
484 fvalue = fvalue * 10 + (v - 0x30);
Lev Walkin3a522782005-07-04 12:21:51 +0000485 fdigits++;
Lev Walkin27126182012-09-03 01:09:54 -0700486 } else {
487 /* Not enough precision, ignore */
Lev Walkin535612a2005-07-03 05:32:40 +0000488 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000489 continue;
490 default:
491 break;
492 }
493 break;
494 }
495 }
496
497 if(buf == end) goto local_finish;
498
499 switch(*buf) {
500 case 0x2B: case 0x2D: /* +, - */
501 goto offset;
502 case 0x5A: /* Z */
503 goto utc_finish;
504 default:
505 errno = EINVAL;
506 return -1;
507 }
508
509
510offset:
511
512 if(end - buf < 3) {
513 errno = EINVAL;
514 return -1;
515 }
516 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000517 B2F(gmtoff_h);
518 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000519 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000520 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 else
Lev Walkin99006362004-08-07 03:52:26 +0000522 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000523
524 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000525 B2F(gmtoff_m);
526 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000527 } else if(end != buf) {
528 errno = EINVAL;
529 return -1;
530 }
531
Lev Walkin99006362004-08-07 03:52:26 +0000532 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000533
534 /* Fall through */
535utc_finish:
536
537 offset_specified = 1;
538
539 /* Fall through */
540local_finish:
541
542 /*
543 * Validation.
544 */
545 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
546 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
547 || (tm_s.tm_hour > 23)
548 || (tm_s.tm_sec > 60)
549 ) {
550 errno = EINVAL;
551 return -1;
552 }
553
554 /* Canonicalize */
555 tm_s.tm_mon -= 1; /* 0 - 11 */
556 tm_s.tm_year -= 1900;
557 tm_s.tm_isdst = -1;
558
Lev Walkin99006362004-08-07 03:52:26 +0000559 tm_s.tm_sec -= gmtoff;
560
561 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
562
Lev Walkin02a31552004-08-12 03:52:53 +0000563 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000564 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000565 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000566 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000567 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000568 * we can only guess that it is a local zone.
569 * Interpret it in this fashion.
570 */
571 tloc = mktime(&tm_s);
572 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000573 if(tloc == -1) {
574 errno = EINVAL;
575 return -1;
576 }
577
Lev Walkin99006362004-08-07 03:52:26 +0000578 if(ret_tm) {
579 if(as_gmt) {
580 if(offset_specified) {
581 *ret_tm = tm_s;
582 } else {
583 if(gmtime_r(&tloc, ret_tm) == 0) {
584 errno = EINVAL;
585 return -1;
586 }
587 }
588 } else {
589 if(localtime_r(&tloc, ret_tm) == 0) {
590 errno = EINVAL;
591 return -1;
592 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000593 }
594 }
595
Lev Walkin535612a2005-07-03 05:32:40 +0000596 /* Fractions of seconds */
597 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000598 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000599
Lev Walkinf15320b2004-06-03 03:38:44 +0000600 return tloc;
601}
602
Lev Walkin99006362004-08-07 03:52:26 +0000603GeneralizedTime_t *
604asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000605 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
606}
607
608GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000609asn_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 +0000610 struct tm tm_s;
611 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000612 const unsigned int buf_size =
613 4 + 2 + 2 /* yyyymmdd */
614 + 2 + 2 + 2 /* hhmmss */
615 + 1 + 6 /* .ffffff */
616 + 1 + 4 /* +hhmm */
617 + 1 /* '\0' */
618 ;
Lev Walkin99006362004-08-07 03:52:26 +0000619 char *buf;
620 char *p;
621 int size;
622
623 /* Check arguments */
624 if(!tm) {
625 errno = EINVAL;
626 return 0;
627 }
628
629 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000630 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000631 if(!buf) return 0;
632
633 gmtoff = GMTOFF(*tm);
634
635 if(force_gmt && gmtoff) {
636 tm_s = *tm;
637 tm_s.tm_sec -= gmtoff;
638 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000639 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000640#ifdef HAVE_TM_GMTOFF
641 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000642#else /* !HAVE_TM_GMTOFF */
643 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000644#endif
Lev Walkin99006362004-08-07 03:52:26 +0000645 }
646
647 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
648 tm->tm_year + 1900,
649 tm->tm_mon + 1,
650 tm->tm_mday,
651 tm->tm_hour,
652 tm->tm_min,
653 tm->tm_sec
654 );
Lev Walkin535612a2005-07-03 05:32:40 +0000655 if(size != 14) {
656 /* Could be assert(size == 14); */
657 FREEMEM(buf);
658 errno = EINVAL;
659 return 0;
660 }
Lev Walkin99006362004-08-07 03:52:26 +0000661
662 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000663
664 /*
665 * Deal with fractions.
666 */
Lev Walkin3a522782005-07-04 12:21:51 +0000667 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000668 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000669 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000670 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000671 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000672
673 /* Place bounds on precision */
674 while(frac_digits-- > 6)
675 frac_value /= 10;
676
677 /* emulate fbase = pow(10, frac_digits) */
678 for(fbase = 1; frac_digits--;)
679 fbase *= 10;
680
Lev Walkin535612a2005-07-03 05:32:40 +0000681 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000682 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000683 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000684 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000685 frac_value %= fbase;
686 fbase /= 10;
687 } while(fbase > 0 && frac_value > 0 && z < end);
688 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000689 for(--z; *z == 0x30; --z); /* Strip zeroes */
690 p = z + (*z != '.');
691 size = p - buf;
692 }
Lev Walkin535612a2005-07-03 05:32:40 +0000693 }
694
Lev Walkin99006362004-08-07 03:52:26 +0000695 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000696 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000697 *p++ = 0;
698 size++;
699 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000700 int ret;
701 gmtoff %= 86400;
702 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
Lev Walkina4347cc2010-03-10 23:05:56 +0000703 gmtoff / 3600, labs(gmtoff % 3600) / 60);
Lev Walkin535612a2005-07-03 05:32:40 +0000704 if(ret != 5) {
705 FREEMEM(buf);
706 errno = EINVAL;
707 return 0;
708 }
Lev Walkin99006362004-08-07 03:52:26 +0000709 size += ret;
710 }
711
712 if(opt_gt) {
713 if(opt_gt->buf)
714 FREEMEM(opt_gt->buf);
715 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000716 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin419f6752006-09-13 04:02:00 +0000717 if(!opt_gt) { FREEMEM(buf); return 0; }
Lev Walkin99006362004-08-07 03:52:26 +0000718 }
719
Lev Walkin4d9528c2004-08-11 08:10:13 +0000720 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000721 opt_gt->size = size;
722
723 return opt_gt;
724}
725
726