vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
vlm | 6534a8d | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 5 | #define _POSIX_PTHREAD_SEMANTICS /* for Sun */ |
| 6 | #define _REENTRANT /* for Sun */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 7 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | #include <GeneralizedTime.h> |
| 9 | #include <time.h> |
| 10 | #include <errno.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | |
vlm | c12c210 | 2004-09-04 04:43:28 +0000 | [diff] [blame] | 12 | #if defined(WIN32) |
vlm | 73a781a | 2005-06-15 19:01:45 +0000 | [diff] [blame] | 13 | #pragma message( "PLEASE STOP AND READ!") |
| 14 | #pragma message( " localtime_r is implemented via localtime(), which may be not thread-safe.") |
| 15 | #pragma message( " gmtime_r is implemented via gmtime(), which may be not thread-safe.") |
| 16 | #pragma message( " ") |
| 17 | #pragma message( " You must fix the code by inserting appropriate locking") |
| 18 | #pragma message( " if you want to use asn_GT2time() or asn_UT2time().") |
| 19 | #pragma message( "PLEASE STOP AND READ! ") |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 20 | |
vlm | 17909e9 | 2004-09-02 05:20:39 +0000 | [diff] [blame] | 21 | static struct tm *localtime_r(const time_t *tloc, struct tm *result) { |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 22 | struct tm *tm; |
| 23 | if((tm = localtime(tloc))) |
| 24 | return memcpy(result, tm, sizeof(struct tm)); |
| 25 | return 0; |
| 26 | } |
| 27 | |
vlm | 17909e9 | 2004-09-02 05:20:39 +0000 | [diff] [blame] | 28 | static struct tm *gmtime_r(const time_t *tloc, struct tm *result) { |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 29 | struct tm *tm; |
| 30 | if((tm = gmtime(tloc))) |
| 31 | return memcpy(result, tm, sizeof(struct tm)); |
| 32 | return 0; |
| 33 | } |
| 34 | |
vlm | bed6f81 | 2004-09-17 06:46:10 +0000 | [diff] [blame] | 35 | #define tzset() _tzset() |
vlm | 73a781a | 2005-06-15 19:01:45 +0000 | [diff] [blame] | 36 | #define putenv(c) _putenv(c) |
vlm | bed6f81 | 2004-09-17 06:46:10 +0000 | [diff] [blame] | 37 | #define _EMULATE_TIMEGM |
| 38 | |
| 39 | #endif /* WIN32 */ |
| 40 | |
vlm | 6534a8d | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 41 | #if defined(sun) |
| 42 | #define _EMULATE_TIMEGM |
| 43 | #endif |
| 44 | |
vlm | bed6f81 | 2004-09-17 06:46:10 +0000 | [diff] [blame] | 45 | /* |
| 46 | * Where to look for offset from GMT, Phase I. |
| 47 | * Several platforms are known. |
| 48 | */ |
| 49 | #if defined(__FreeBSD__) \ |
| 50 | || (defined(__GNUC__) && defined(__APPLE_CC__)) \ |
| 51 | || (defined __GLIBC__ && __GLIBC__ >= 2) |
| 52 | #undef HAVE_TM_GMTOFF |
| 53 | #define HAVE_TM_GMTOFF |
| 54 | #endif /* BSDs and newer glibc */ |
| 55 | |
| 56 | /* |
| 57 | * Where to look for offset from GMT, Phase II. |
| 58 | */ |
| 59 | #ifdef HAVE_TM_GMTOFF |
| 60 | #define GMTOFF(tm) ((tm).tm_gmtoff) |
| 61 | #else /* HAVE_TM_GMTOFF */ |
| 62 | #define GMTOFF(tm) (-timezone) |
| 63 | #endif /* HAVE_TM_GMTOFF */ |
| 64 | |
| 65 | /* |
| 66 | * Override our GMTOFF decision for other known platforms. |
| 67 | */ |
| 68 | #ifdef __CYGWIN__ |
| 69 | #undef GMTOFF |
| 70 | static long GMTOFF(struct tm a){ |
| 71 | struct tm *lt; |
| 72 | time_t local_time, gmt_time; |
| 73 | long zone; |
| 74 | |
| 75 | tzset(); |
| 76 | gmt_time = time (NULL); |
| 77 | |
| 78 | lt = gmtime(&gmt_time); |
| 79 | |
| 80 | local_time = mktime(lt); |
| 81 | return (gmt_time - local_time); |
| 82 | } |
| 83 | #define _EMULATE_TIMEGM |
| 84 | |
| 85 | #endif /* __CYGWIN__ */ |
| 86 | |
| 87 | #ifdef _EMULATE_TIMEGM |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 88 | static time_t timegm(struct tm *tm) { |
| 89 | time_t tloc; |
| 90 | char *tz; |
vlm | d369228 | 2004-08-12 07:47:03 +0000 | [diff] [blame] | 91 | char *buf; |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 92 | |
| 93 | tz = getenv("TZ"); |
vlm | 6534a8d | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 94 | putenv("TZ=UTC"); |
vlm | bed6f81 | 2004-09-17 06:46:10 +0000 | [diff] [blame] | 95 | tzset(); |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 96 | tloc = mktime(tm); |
vlm | d369228 | 2004-08-12 07:47:03 +0000 | [diff] [blame] | 97 | if (tz) { |
vlm | bed6f81 | 2004-09-17 06:46:10 +0000 | [diff] [blame] | 98 | int bufsize = strlen(tz) + 4; |
| 99 | buf = alloca(bufsize); |
| 100 | snprintf(buf, bufsize, "TZ=%s", tz); |
vlm | d369228 | 2004-08-12 07:47:03 +0000 | [diff] [blame] | 101 | } else { |
| 102 | buf = "TZ="; |
| 103 | } |
vlm | 6534a8d | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 104 | putenv(buf); |
vlm | bed6f81 | 2004-09-17 06:46:10 +0000 | [diff] [blame] | 105 | tzset(); |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 106 | return tloc; |
| 107 | } |
vlm | bed6f81 | 2004-09-17 06:46:10 +0000 | [diff] [blame] | 108 | #endif /* _EMULATE_TIMEGM */ |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 109 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 110 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 111 | #ifndef __ASN_INTERNAL_TEST_MODE__ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * GeneralizedTime basic type description. |
| 115 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 116 | static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 117 | (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/ |
| 118 | (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/ |
| 119 | (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 120 | }; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 121 | asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 122 | "GeneralizedTime", |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 123 | "GeneralizedTime", |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 124 | OCTET_STRING_free, |
| 125 | GeneralizedTime_print, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 126 | GeneralizedTime_constraint, /* Check validity of time */ |
| 127 | OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 128 | GeneralizedTime_encode_der, |
| 129 | OCTET_STRING_decode_xer_utf8, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 130 | GeneralizedTime_encode_xer, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 131 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 132 | asn_DEF_GeneralizedTime_tags, |
| 133 | sizeof(asn_DEF_GeneralizedTime_tags) |
| 134 | / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2, |
| 135 | asn_DEF_GeneralizedTime_tags, |
| 136 | sizeof(asn_DEF_GeneralizedTime_tags) |
| 137 | / sizeof(asn_DEF_GeneralizedTime_tags[0]), |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 138 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 139 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 142 | #endif /* __ASN_INTERNAL_TEST_MODE__ */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Check that the time looks like the time. |
| 146 | */ |
| 147 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 148 | GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 150 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | time_t tloc; |
| 152 | |
| 153 | errno = EPERM; /* Just an unlikely error code */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 154 | tloc = asn_GT2time(st, 0, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | if(tloc == -1 && errno != EPERM) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 156 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 157 | "%s: Invalid time format: %s (%s:%d)", |
| 158 | td->name, strerror(errno), __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 165 | asn_enc_rval_t |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 166 | GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 167 | int tag_mode, ber_tlv_tag_t tag, |
| 168 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 169 | GeneralizedTime_t *st = (GeneralizedTime_t *)sptr; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 170 | asn_enc_rval_t erval; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 171 | long fv, fb; /* seconds fraction value and base */ |
| 172 | struct tm tm; |
| 173 | time_t tloc; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 174 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 175 | /* |
| 176 | * Encode as a canonical DER. |
| 177 | */ |
| 178 | errno = EPERM; |
| 179 | tloc = asn_GT2time_frac(st, &fv, &fb, &tm, 1); /* Recognize time */ |
| 180 | if(tloc == -1 && errno != EPERM) |
| 181 | /* Failed to recognize time. Fail completely. */ |
| 182 | _ASN_ENCODE_FAILED; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 183 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 184 | st = asn_time2GT_frac(0, &tm, fv, fb, 1); /* Save time canonically */ |
| 185 | if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 186 | |
| 187 | erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key); |
| 188 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 189 | FREEMEM(st->buf); |
| 190 | FREEMEM(st); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 191 | |
| 192 | return erval; |
| 193 | } |
| 194 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 195 | #ifndef __ASN_INTERNAL_TEST_MODE__ |
| 196 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 197 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 198 | GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 199 | int ilevel, enum xer_encoder_flags_e flags, |
| 200 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 201 | |
| 202 | if(flags & XER_F_CANONICAL) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 203 | GeneralizedTime_t *gt; |
| 204 | asn_enc_rval_t rv; |
| 205 | long fv, fb; /* fractional parts */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 206 | struct tm tm; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 207 | |
| 208 | errno = EPERM; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 209 | if(asn_GT2time_frac((GeneralizedTime_t *)sptr, |
| 210 | &fv, &fb, &tm, 1) == -1 |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 211 | && errno != EPERM) |
| 212 | _ASN_ENCODE_FAILED; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 213 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 214 | gt = asn_time2GT_frac(0, &tm, fv, fb, 1); |
| 215 | if(!gt) _ASN_ENCODE_FAILED; |
| 216 | |
| 217 | rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 218 | cb, app_key); |
| 219 | asn_DEF_GeneralizedTime.free_struct(&asn_DEF_GeneralizedTime, |
| 220 | gt, 0); |
| 221 | return rv; |
| 222 | } else { |
| 223 | return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 224 | cb, app_key); |
| 225 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 226 | } |
| 227 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 228 | #endif /* __ASN_INTERNAL_TEST_MODE__ */ |
| 229 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 230 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 231 | GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 232 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 233 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 234 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 235 | (void)td; /* Unused argument */ |
| 236 | (void)ilevel; /* Unused argument */ |
| 237 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 238 | if(st && st->buf) { |
| 239 | char buf[32]; |
| 240 | struct tm tm; |
| 241 | int ret; |
| 242 | |
| 243 | errno = EPERM; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 244 | if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM) |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 245 | return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 246 | |
| 247 | ret = snprintf(buf, sizeof(buf), |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 248 | "%04d-%02d-%02d %02d:%02d:%02d (GMT)", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 249 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 250 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 251 | assert(ret > 0 && ret < (int)sizeof(buf)); |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 252 | return (cb(buf, ret, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 253 | } else { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 254 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 258 | time_t |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 259 | asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 260 | return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt); |
| 261 | } |
| 262 | |
| 263 | time_t |
| 264 | asn_GT2time_frac(const GeneralizedTime_t *st, long *frac_value, long *frac_base, struct tm *ret_tm, int as_gmt) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 265 | struct tm tm_s; |
| 266 | uint8_t *buf; |
| 267 | uint8_t *end; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 268 | int gmtoff_h = 0; |
| 269 | int gmtoff_m = 0; |
| 270 | int gmtoff = 0; /* h + m */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 271 | int offset_specified = 0; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 272 | long fvalue = 0; |
| 273 | long fbase = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 274 | time_t tloc; |
| 275 | |
| 276 | if(!st || !st->buf) { |
| 277 | errno = EINVAL; |
| 278 | return -1; |
| 279 | } else { |
| 280 | buf = st->buf; |
| 281 | end = buf + st->size; |
| 282 | } |
| 283 | |
| 284 | if(st->size < 10) { |
| 285 | errno = EINVAL; |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Decode first 10 bytes: "AAAAMMJJhh" |
| 291 | */ |
| 292 | memset(&tm_s, 0, sizeof(tm_s)); |
| 293 | #undef B2F |
| 294 | #undef B2T |
| 295 | #define B2F(var) do { \ |
| 296 | unsigned ch = *buf; \ |
| 297 | if(ch < 0x30 && ch > 0x39) { \ |
| 298 | errno = EINVAL; \ |
| 299 | return -1; \ |
| 300 | } else { \ |
| 301 | var = var * 10 + (ch - 0x30); \ |
| 302 | buf++; \ |
| 303 | } \ |
| 304 | } while(0) |
| 305 | #define B2T(var) B2F(tm_s.var) |
| 306 | |
| 307 | B2T(tm_year); /* 1: A */ |
| 308 | B2T(tm_year); /* 2: A */ |
| 309 | B2T(tm_year); /* 3: A */ |
| 310 | B2T(tm_year); /* 4: A */ |
| 311 | B2T(tm_mon); /* 5: M */ |
| 312 | B2T(tm_mon); /* 6: M */ |
| 313 | B2T(tm_mday); /* 7: J */ |
| 314 | B2T(tm_mday); /* 8: J */ |
| 315 | B2T(tm_hour); /* 9: h */ |
| 316 | B2T(tm_hour); /* 0: h */ |
| 317 | |
| 318 | if(buf == end) goto local_finish; |
| 319 | |
| 320 | /* |
| 321 | * Parse [mm[ss[(.|,)ffff]]] |
| 322 | * ^^ |
| 323 | */ |
| 324 | switch(*buf) { |
| 325 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 326 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 327 | tm_s.tm_min = (*buf++) - 0x30; |
| 328 | if(buf == end) { errno = EINVAL; return -1; } |
| 329 | B2T(tm_min); |
| 330 | break; |
| 331 | case 0x2B: case 0x2D: /* +, - */ |
| 332 | goto offset; |
| 333 | case 0x5A: /* Z */ |
| 334 | goto utc_finish; |
| 335 | default: |
| 336 | errno = EINVAL; |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | if(buf == end) goto local_finish; |
| 341 | |
| 342 | /* |
| 343 | * Parse [mm[ss[(.|,)ffff]]] |
| 344 | * ^^ |
| 345 | */ |
| 346 | switch(*buf) { |
| 347 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 348 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 349 | tm_s.tm_sec = (*buf++) - 0x30; |
| 350 | if(buf == end) { errno = EINVAL; return -1; } |
| 351 | B2T(tm_sec); |
| 352 | break; |
| 353 | case 0x2B: case 0x2D: /* +, - */ |
| 354 | goto offset; |
| 355 | case 0x5A: /* Z */ |
| 356 | goto utc_finish; |
| 357 | default: |
| 358 | errno = EINVAL; |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | if(buf == end) goto local_finish; |
| 363 | |
| 364 | /* |
| 365 | * Parse [mm[ss[(.|,)ffff]]] |
| 366 | * ^ ^ |
| 367 | */ |
| 368 | switch(*buf) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 369 | case 0x2C: case 0x2E: /* (.|,) */ |
| 370 | /* |
| 371 | * Process fractions of seconds. |
| 372 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 373 | for(buf++; buf < end; buf++) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 374 | int v = *buf; |
| 375 | switch(v) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 376 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 377 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 378 | if((fbase * 10 / fbase) != 10) { |
| 379 | /* Not enough precision, ignore */ |
| 380 | } else { |
| 381 | fbase *= 10; |
| 382 | fvalue = fvalue * 10 + (v - 0x30); |
| 383 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 384 | continue; |
| 385 | default: |
| 386 | break; |
| 387 | } |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if(buf == end) goto local_finish; |
| 393 | |
| 394 | switch(*buf) { |
| 395 | case 0x2B: case 0x2D: /* +, - */ |
| 396 | goto offset; |
| 397 | case 0x5A: /* Z */ |
| 398 | goto utc_finish; |
| 399 | default: |
| 400 | errno = EINVAL; |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | offset: |
| 406 | |
| 407 | if(end - buf < 3) { |
| 408 | errno = EINVAL; |
| 409 | return -1; |
| 410 | } |
| 411 | buf++; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 412 | B2F(gmtoff_h); |
| 413 | B2F(gmtoff_h); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 414 | if(buf[-3] == 0x2D) /* Negative */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 415 | gmtoff = -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 416 | else |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 417 | gmtoff = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 418 | |
| 419 | if((end - buf) == 2) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 420 | B2F(gmtoff_m); |
| 421 | B2F(gmtoff_m); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 422 | } else if(end != buf) { |
| 423 | errno = EINVAL; |
| 424 | return -1; |
| 425 | } |
| 426 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 427 | gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 428 | |
| 429 | /* Fall through */ |
| 430 | utc_finish: |
| 431 | |
| 432 | offset_specified = 1; |
| 433 | |
| 434 | /* Fall through */ |
| 435 | local_finish: |
| 436 | |
| 437 | /* |
| 438 | * Validation. |
| 439 | */ |
| 440 | if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1) |
| 441 | || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1) |
| 442 | || (tm_s.tm_hour > 23) |
| 443 | || (tm_s.tm_sec > 60) |
| 444 | ) { |
| 445 | errno = EINVAL; |
| 446 | return -1; |
| 447 | } |
| 448 | |
| 449 | /* Canonicalize */ |
| 450 | tm_s.tm_mon -= 1; /* 0 - 11 */ |
| 451 | tm_s.tm_year -= 1900; |
| 452 | tm_s.tm_isdst = -1; |
| 453 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 454 | tm_s.tm_sec -= gmtoff; |
| 455 | |
| 456 | /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/ |
| 457 | |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 458 | if(offset_specified) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 459 | tloc = timegm(&tm_s); |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 460 | } else { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 461 | /* |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 462 | * Without an offset (or "Z"), |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 463 | * we can only guess that it is a local zone. |
| 464 | * Interpret it in this fashion. |
| 465 | */ |
| 466 | tloc = mktime(&tm_s); |
| 467 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 468 | if(tloc == -1) { |
| 469 | errno = EINVAL; |
| 470 | return -1; |
| 471 | } |
| 472 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 473 | if(ret_tm) { |
| 474 | if(as_gmt) { |
| 475 | if(offset_specified) { |
| 476 | *ret_tm = tm_s; |
| 477 | } else { |
| 478 | if(gmtime_r(&tloc, ret_tm) == 0) { |
| 479 | errno = EINVAL; |
| 480 | return -1; |
| 481 | } |
| 482 | } |
| 483 | } else { |
| 484 | if(localtime_r(&tloc, ret_tm) == 0) { |
| 485 | errno = EINVAL; |
| 486 | return -1; |
| 487 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 491 | /* Fractions of seconds */ |
| 492 | if(frac_value) *frac_value = fvalue; |
| 493 | if(frac_base) *frac_base = fbase; |
| 494 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 495 | return tloc; |
| 496 | } |
| 497 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 498 | GeneralizedTime_t * |
| 499 | asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 500 | return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt); |
| 501 | } |
| 502 | |
| 503 | GeneralizedTime_t * |
| 504 | asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, long frac_value, long frac_base, int force_gmt) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 505 | struct tm tm_s; |
| 506 | long gmtoff; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 507 | const unsigned int buf_size = |
| 508 | 4 + 2 + 2 /* yyyymmdd */ |
| 509 | + 2 + 2 + 2 /* hhmmss */ |
| 510 | + 1 + 6 /* .ffffff */ |
| 511 | + 1 + 4 /* +hhmm */ |
| 512 | + 1 /* '\0' */ |
| 513 | ; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 514 | char *buf; |
| 515 | char *p; |
| 516 | int size; |
| 517 | |
| 518 | /* Check arguments */ |
| 519 | if(!tm) { |
| 520 | errno = EINVAL; |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /* Pre-allocate a buffer of sufficient yet small length */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 525 | buf = (char *)MALLOC(buf_size); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 526 | if(!buf) return 0; |
| 527 | |
| 528 | gmtoff = GMTOFF(*tm); |
| 529 | |
| 530 | if(force_gmt && gmtoff) { |
| 531 | tm_s = *tm; |
| 532 | tm_s.tm_sec -= gmtoff; |
| 533 | timegm(&tm_s); /* Fix the time */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 534 | tm = &tm_s; |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 535 | #ifdef HAVE_TM_GMTOFF |
| 536 | assert(!GMTOFF(tm_s)); /* Will fix itself */ |
| 537 | #else |
| 538 | gmtoff = 0; /* Intervention required */ |
| 539 | #endif |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d", |
| 543 | tm->tm_year + 1900, |
| 544 | tm->tm_mon + 1, |
| 545 | tm->tm_mday, |
| 546 | tm->tm_hour, |
| 547 | tm->tm_min, |
| 548 | tm->tm_sec |
| 549 | ); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 550 | if(size != 14) { |
| 551 | /* Could be assert(size == 14); */ |
| 552 | FREEMEM(buf); |
| 553 | errno = EINVAL; |
| 554 | return 0; |
| 555 | } |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 556 | |
| 557 | p = buf + size; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 558 | |
| 559 | /* |
| 560 | * Deal with fractions. |
| 561 | */ |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame^] | 562 | if(frac_base >= 10 |
| 563 | && frac_value > 0 |
| 564 | /* 1001 ms? should ignore or adjust seconds */ |
| 565 | && (frac_value/frac_base) == 0 |
| 566 | ) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 567 | char *end = p + 1 + 6; /* '.' + maximum 6 digits */ |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame^] | 568 | char *z = p; |
| 569 | *z++ = '.'; |
| 570 | frac_value %= frac_base; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 571 | do { |
| 572 | int digit; |
| 573 | frac_base /= 10; |
| 574 | digit = frac_value / frac_base; |
| 575 | frac_value %= frac_base; |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame^] | 576 | *z++ = digit + 0x30; |
| 577 | } while(frac_base >= 10 && frac_value > 0 && z < end); |
| 578 | for(--z; *z == 0x30; --z); /* Strip zeroes */ |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 579 | p = z + (*z != '.'); |
| 580 | size = p - buf; |
| 581 | } |
| 582 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 583 | if(force_gmt) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 584 | *p++ = 0x5a; /* "Z" */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 585 | *p++ = 0; |
| 586 | size++; |
| 587 | } else { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 588 | int ret; |
| 589 | gmtoff %= 86400; |
| 590 | ret = snprintf(p, buf_size - size, "%+03ld%02ld", |
| 591 | gmtoff / 3600, labs(gmtoff % 3600)); |
| 592 | if(ret != 5) { |
| 593 | FREEMEM(buf); |
| 594 | errno = EINVAL; |
| 595 | return 0; |
| 596 | } |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 597 | size += ret; |
| 598 | } |
| 599 | |
| 600 | if(opt_gt) { |
| 601 | if(opt_gt->buf) |
| 602 | FREEMEM(opt_gt->buf); |
| 603 | } else { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 604 | opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 605 | if(!opt_gt) { free(buf); return 0; } |
| 606 | } |
| 607 | |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 608 | opt_gt->buf = (unsigned char *)buf; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 609 | opt_gt->size = size; |
| 610 | |
| 611 | return opt_gt; |
| 612 | } |
| 613 | |
| 614 | |