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