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