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 | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 131 | 0, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 133 | asn_DEF_GeneralizedTime_tags, |
| 134 | sizeof(asn_DEF_GeneralizedTime_tags) |
| 135 | / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2, |
| 136 | asn_DEF_GeneralizedTime_tags, |
| 137 | sizeof(asn_DEF_GeneralizedTime_tags) |
| 138 | / sizeof(asn_DEF_GeneralizedTime_tags[0]), |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 139 | 0, /* No PER visible constraints */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 140 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 141 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 144 | #endif /* __ASN_INTERNAL_TEST_MODE__ */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * Check that the time looks like the time. |
| 148 | */ |
| 149 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 150 | GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 152 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 153 | time_t tloc; |
| 154 | |
| 155 | errno = EPERM; /* Just an unlikely error code */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 156 | tloc = asn_GT2time(st, 0, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 157 | if(tloc == -1 && errno != EPERM) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 158 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 159 | "%s: Invalid time format: %s (%s:%d)", |
| 160 | td->name, strerror(errno), __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 167 | asn_enc_rval_t |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 168 | GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 169 | int tag_mode, ber_tlv_tag_t tag, |
| 170 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 171 | GeneralizedTime_t *st = (GeneralizedTime_t *)sptr; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 172 | asn_enc_rval_t erval; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 173 | int fv, fd; /* seconds fraction value and number of digits */ |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 174 | struct tm tm; |
| 175 | time_t tloc; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 176 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 177 | /* |
| 178 | * Encode as a canonical DER. |
| 179 | */ |
| 180 | errno = EPERM; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 181 | tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */ |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 182 | if(tloc == -1 && errno != EPERM) |
| 183 | /* Failed to recognize time. Fail completely. */ |
| 184 | _ASN_ENCODE_FAILED; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 185 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 186 | st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */ |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 187 | if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 188 | |
| 189 | erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key); |
| 190 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 191 | FREEMEM(st->buf); |
| 192 | FREEMEM(st); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 193 | |
| 194 | return erval; |
| 195 | } |
| 196 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 197 | #ifndef __ASN_INTERNAL_TEST_MODE__ |
| 198 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 199 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 200 | GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 201 | int ilevel, enum xer_encoder_flags_e flags, |
| 202 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 203 | |
| 204 | if(flags & XER_F_CANONICAL) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 205 | GeneralizedTime_t *gt; |
| 206 | asn_enc_rval_t rv; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 207 | int fv, fd; /* fractional parts */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 208 | struct tm tm; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 209 | |
| 210 | errno = EPERM; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 211 | if(asn_GT2time_frac((GeneralizedTime_t *)sptr, |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 212 | &fv, &fd, &tm, 1) == -1 |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 213 | && errno != EPERM) |
| 214 | _ASN_ENCODE_FAILED; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 215 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 216 | gt = asn_time2GT_frac(0, &tm, fv, fd, 1); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 217 | if(!gt) _ASN_ENCODE_FAILED; |
| 218 | |
| 219 | rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 220 | cb, app_key); |
| 221 | asn_DEF_GeneralizedTime.free_struct(&asn_DEF_GeneralizedTime, |
| 222 | gt, 0); |
| 223 | return rv; |
| 224 | } else { |
| 225 | return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 226 | cb, app_key); |
| 227 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 228 | } |
| 229 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 230 | #endif /* __ASN_INTERNAL_TEST_MODE__ */ |
| 231 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 232 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 233 | GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 234 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 235 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 236 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 237 | (void)td; /* Unused argument */ |
| 238 | (void)ilevel; /* Unused argument */ |
| 239 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 240 | if(st && st->buf) { |
| 241 | char buf[32]; |
| 242 | struct tm tm; |
| 243 | int ret; |
| 244 | |
| 245 | errno = EPERM; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 246 | if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM) |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 247 | return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 248 | |
| 249 | ret = snprintf(buf, sizeof(buf), |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 250 | "%04d-%02d-%02d %02d:%02d:%02d (GMT)", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 251 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 252 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 253 | assert(ret > 0 && ret < (int)sizeof(buf)); |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 254 | return (cb(buf, ret, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 255 | } else { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 256 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 260 | time_t |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 261 | asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 262 | return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt); |
| 263 | } |
| 264 | |
| 265 | time_t |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 266 | asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) { |
| 267 | time_t tloc; |
| 268 | int fv, fd = 0; |
| 269 | |
| 270 | if(frac_value) |
| 271 | tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt); |
| 272 | else |
| 273 | return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt); |
| 274 | if(fd == 0 || frac_digits <= 0) { |
| 275 | *frac_value = 0; |
| 276 | } else { |
| 277 | while(fd > frac_digits) |
| 278 | fv /= 10, fd--; |
| 279 | while(fd < frac_digits) { |
| 280 | int new_fv = fv * 10; |
| 281 | if(new_fv / 10 != fv) { |
| 282 | /* Too long precision request */ |
| 283 | fv = 0; |
| 284 | break; |
| 285 | } |
| 286 | fv = new_fv, fd++; |
| 287 | } |
| 288 | |
| 289 | *frac_value = fv; |
| 290 | } |
| 291 | |
| 292 | return tloc; |
| 293 | } |
| 294 | |
| 295 | time_t |
| 296 | asn_GT2time_frac(const GeneralizedTime_t *st, int *frac_value, int *frac_digits, struct tm *ret_tm, int as_gmt) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 297 | struct tm tm_s; |
| 298 | uint8_t *buf; |
| 299 | uint8_t *end; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 300 | int gmtoff_h = 0; |
| 301 | int gmtoff_m = 0; |
| 302 | int gmtoff = 0; /* h + m */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 303 | int offset_specified = 0; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 304 | int fvalue = 0; |
| 305 | int fdigits = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 306 | time_t tloc; |
| 307 | |
| 308 | if(!st || !st->buf) { |
| 309 | errno = EINVAL; |
| 310 | return -1; |
| 311 | } else { |
| 312 | buf = st->buf; |
| 313 | end = buf + st->size; |
| 314 | } |
| 315 | |
| 316 | if(st->size < 10) { |
| 317 | errno = EINVAL; |
| 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Decode first 10 bytes: "AAAAMMJJhh" |
| 323 | */ |
| 324 | memset(&tm_s, 0, sizeof(tm_s)); |
| 325 | #undef B2F |
| 326 | #undef B2T |
| 327 | #define B2F(var) do { \ |
| 328 | unsigned ch = *buf; \ |
| 329 | if(ch < 0x30 && ch > 0x39) { \ |
| 330 | errno = EINVAL; \ |
| 331 | return -1; \ |
| 332 | } else { \ |
| 333 | var = var * 10 + (ch - 0x30); \ |
| 334 | buf++; \ |
| 335 | } \ |
| 336 | } while(0) |
| 337 | #define B2T(var) B2F(tm_s.var) |
| 338 | |
| 339 | B2T(tm_year); /* 1: A */ |
| 340 | B2T(tm_year); /* 2: A */ |
| 341 | B2T(tm_year); /* 3: A */ |
| 342 | B2T(tm_year); /* 4: A */ |
| 343 | B2T(tm_mon); /* 5: M */ |
| 344 | B2T(tm_mon); /* 6: M */ |
| 345 | B2T(tm_mday); /* 7: J */ |
| 346 | B2T(tm_mday); /* 8: J */ |
| 347 | B2T(tm_hour); /* 9: h */ |
| 348 | B2T(tm_hour); /* 0: h */ |
| 349 | |
| 350 | if(buf == end) goto local_finish; |
| 351 | |
| 352 | /* |
| 353 | * Parse [mm[ss[(.|,)ffff]]] |
| 354 | * ^^ |
| 355 | */ |
| 356 | switch(*buf) { |
| 357 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 358 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 359 | tm_s.tm_min = (*buf++) - 0x30; |
| 360 | if(buf == end) { errno = EINVAL; return -1; } |
| 361 | B2T(tm_min); |
| 362 | break; |
| 363 | case 0x2B: case 0x2D: /* +, - */ |
| 364 | goto offset; |
| 365 | case 0x5A: /* Z */ |
| 366 | goto utc_finish; |
| 367 | default: |
| 368 | errno = EINVAL; |
| 369 | return -1; |
| 370 | } |
| 371 | |
| 372 | if(buf == end) goto local_finish; |
| 373 | |
| 374 | /* |
| 375 | * Parse [mm[ss[(.|,)ffff]]] |
| 376 | * ^^ |
| 377 | */ |
| 378 | switch(*buf) { |
| 379 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 380 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 381 | tm_s.tm_sec = (*buf++) - 0x30; |
| 382 | if(buf == end) { errno = EINVAL; return -1; } |
| 383 | B2T(tm_sec); |
| 384 | break; |
| 385 | case 0x2B: case 0x2D: /* +, - */ |
| 386 | goto offset; |
| 387 | case 0x5A: /* Z */ |
| 388 | goto utc_finish; |
| 389 | default: |
| 390 | errno = EINVAL; |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | if(buf == end) goto local_finish; |
| 395 | |
| 396 | /* |
| 397 | * Parse [mm[ss[(.|,)ffff]]] |
| 398 | * ^ ^ |
| 399 | */ |
| 400 | switch(*buf) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 401 | case 0x2C: case 0x2E: /* (.|,) */ |
| 402 | /* |
| 403 | * Process fractions of seconds. |
| 404 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 405 | for(buf++; buf < end; buf++) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 406 | int v = *buf; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 407 | int new_fvalue; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 408 | switch(v) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 409 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 410 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 411 | new_fvalue = fvalue * 10 + (v - 0x30); |
| 412 | if(new_fvalue / 10 != fvalue) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 413 | /* Not enough precision, ignore */ |
| 414 | } else { |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 415 | fvalue = new_fvalue; |
| 416 | fdigits++; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 417 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 418 | continue; |
| 419 | default: |
| 420 | break; |
| 421 | } |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if(buf == end) goto local_finish; |
| 427 | |
| 428 | switch(*buf) { |
| 429 | case 0x2B: case 0x2D: /* +, - */ |
| 430 | goto offset; |
| 431 | case 0x5A: /* Z */ |
| 432 | goto utc_finish; |
| 433 | default: |
| 434 | errno = EINVAL; |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | offset: |
| 440 | |
| 441 | if(end - buf < 3) { |
| 442 | errno = EINVAL; |
| 443 | return -1; |
| 444 | } |
| 445 | buf++; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 446 | B2F(gmtoff_h); |
| 447 | B2F(gmtoff_h); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 448 | if(buf[-3] == 0x2D) /* Negative */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 449 | gmtoff = -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 450 | else |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 451 | gmtoff = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 452 | |
| 453 | if((end - buf) == 2) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 454 | B2F(gmtoff_m); |
| 455 | B2F(gmtoff_m); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 456 | } else if(end != buf) { |
| 457 | errno = EINVAL; |
| 458 | return -1; |
| 459 | } |
| 460 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 461 | gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 462 | |
| 463 | /* Fall through */ |
| 464 | utc_finish: |
| 465 | |
| 466 | offset_specified = 1; |
| 467 | |
| 468 | /* Fall through */ |
| 469 | local_finish: |
| 470 | |
| 471 | /* |
| 472 | * Validation. |
| 473 | */ |
| 474 | if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1) |
| 475 | || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1) |
| 476 | || (tm_s.tm_hour > 23) |
| 477 | || (tm_s.tm_sec > 60) |
| 478 | ) { |
| 479 | errno = EINVAL; |
| 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | /* Canonicalize */ |
| 484 | tm_s.tm_mon -= 1; /* 0 - 11 */ |
| 485 | tm_s.tm_year -= 1900; |
| 486 | tm_s.tm_isdst = -1; |
| 487 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 488 | tm_s.tm_sec -= gmtoff; |
| 489 | |
| 490 | /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/ |
| 491 | |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 492 | if(offset_specified) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 493 | tloc = timegm(&tm_s); |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 494 | } else { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 495 | /* |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 496 | * Without an offset (or "Z"), |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 497 | * we can only guess that it is a local zone. |
| 498 | * Interpret it in this fashion. |
| 499 | */ |
| 500 | tloc = mktime(&tm_s); |
| 501 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 502 | if(tloc == -1) { |
| 503 | errno = EINVAL; |
| 504 | return -1; |
| 505 | } |
| 506 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 507 | if(ret_tm) { |
| 508 | if(as_gmt) { |
| 509 | if(offset_specified) { |
| 510 | *ret_tm = tm_s; |
| 511 | } else { |
| 512 | if(gmtime_r(&tloc, ret_tm) == 0) { |
| 513 | errno = EINVAL; |
| 514 | return -1; |
| 515 | } |
| 516 | } |
| 517 | } else { |
| 518 | if(localtime_r(&tloc, ret_tm) == 0) { |
| 519 | errno = EINVAL; |
| 520 | return -1; |
| 521 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 525 | /* Fractions of seconds */ |
| 526 | if(frac_value) *frac_value = fvalue; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 527 | if(frac_digits) *frac_digits = fdigits; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 528 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 529 | return tloc; |
| 530 | } |
| 531 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 532 | GeneralizedTime_t * |
| 533 | asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 534 | return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt); |
| 535 | } |
| 536 | |
| 537 | GeneralizedTime_t * |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 538 | asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, int frac_value, int frac_digits, int force_gmt) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 539 | struct tm tm_s; |
| 540 | long gmtoff; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 541 | const unsigned int buf_size = |
| 542 | 4 + 2 + 2 /* yyyymmdd */ |
| 543 | + 2 + 2 + 2 /* hhmmss */ |
| 544 | + 1 + 6 /* .ffffff */ |
| 545 | + 1 + 4 /* +hhmm */ |
| 546 | + 1 /* '\0' */ |
| 547 | ; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 548 | char *buf; |
| 549 | char *p; |
| 550 | int size; |
| 551 | |
| 552 | /* Check arguments */ |
| 553 | if(!tm) { |
| 554 | errno = EINVAL; |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | /* Pre-allocate a buffer of sufficient yet small length */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 559 | buf = (char *)MALLOC(buf_size); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 560 | if(!buf) return 0; |
| 561 | |
| 562 | gmtoff = GMTOFF(*tm); |
| 563 | |
| 564 | if(force_gmt && gmtoff) { |
| 565 | tm_s = *tm; |
| 566 | tm_s.tm_sec -= gmtoff; |
| 567 | timegm(&tm_s); /* Fix the time */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 568 | tm = &tm_s; |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 569 | #ifdef HAVE_TM_GMTOFF |
| 570 | assert(!GMTOFF(tm_s)); /* Will fix itself */ |
| 571 | #else |
| 572 | gmtoff = 0; /* Intervention required */ |
| 573 | #endif |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d", |
| 577 | tm->tm_year + 1900, |
| 578 | tm->tm_mon + 1, |
| 579 | tm->tm_mday, |
| 580 | tm->tm_hour, |
| 581 | tm->tm_min, |
| 582 | tm->tm_sec |
| 583 | ); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 584 | if(size != 14) { |
| 585 | /* Could be assert(size == 14); */ |
| 586 | FREEMEM(buf); |
| 587 | errno = EINVAL; |
| 588 | return 0; |
| 589 | } |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 590 | |
| 591 | p = buf + size; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * Deal with fractions. |
| 595 | */ |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 596 | if(frac_value > 0 && frac_digits > 0) { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 597 | char *end = p + 1 + 6; /* '.' + maximum 6 digits */ |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 598 | char *z = p; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 599 | long fbase; |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 600 | *z++ = '.'; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 601 | |
| 602 | /* Place bounds on precision */ |
| 603 | while(frac_digits-- > 6) |
| 604 | frac_value /= 10; |
| 605 | |
| 606 | /* emulate fbase = pow(10, frac_digits) */ |
| 607 | for(fbase = 1; frac_digits--;) |
| 608 | fbase *= 10; |
| 609 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 610 | do { |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 611 | int digit = frac_value / fbase; |
vlm | baf858b | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 612 | if(digit > 9) { z = 0; break; } |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 613 | *z++ = digit + 0x30; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 614 | frac_value %= fbase; |
| 615 | fbase /= 10; |
| 616 | } while(fbase > 0 && frac_value > 0 && z < end); |
| 617 | if(z) { |
vlm | baf858b | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 618 | for(--z; *z == 0x30; --z); /* Strip zeroes */ |
| 619 | p = z + (*z != '.'); |
| 620 | size = p - buf; |
| 621 | } |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 622 | } |
| 623 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 624 | if(force_gmt) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 625 | *p++ = 0x5a; /* "Z" */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 626 | *p++ = 0; |
| 627 | size++; |
| 628 | } else { |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 629 | int ret; |
| 630 | gmtoff %= 86400; |
| 631 | ret = snprintf(p, buf_size - size, "%+03ld%02ld", |
| 632 | gmtoff / 3600, labs(gmtoff % 3600)); |
| 633 | if(ret != 5) { |
| 634 | FREEMEM(buf); |
| 635 | errno = EINVAL; |
| 636 | return 0; |
| 637 | } |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 638 | size += ret; |
| 639 | } |
| 640 | |
| 641 | if(opt_gt) { |
| 642 | if(opt_gt->buf) |
| 643 | FREEMEM(opt_gt->buf); |
| 644 | } else { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 645 | opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 646 | if(!opt_gt) { free(buf); return 0; } |
| 647 | } |
| 648 | |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 649 | opt_gt->buf = (unsigned char *)buf; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 650 | opt_gt->size = size; |
| 651 | |
| 652 | return opt_gt; |
| 653 | } |
| 654 | |
| 655 | |