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