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