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