Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 2 | * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 169 | static asn_per_constraints_t asn_DEF_GeneralizedTime_per_constraints = { |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 170 | { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */ |
| 171 | { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */ |
| 172 | 0, 0 |
| 173 | }; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 174 | asn_TYPE_operation_t asn_OP_GeneralizedTime = { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 175 | OCTET_STRING_free, |
| 176 | GeneralizedTime_print, |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 177 | OCTET_STRING_compare, /* Does not normalize time zones! */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 182 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 183 | 0, |
| 184 | 0, |
| 185 | #else |
| 186 | 0, |
| 187 | 0, |
| 188 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 189 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 190 | 0, |
| 191 | 0, |
| 192 | #else |
| 193 | OCTET_STRING_decode_uper, |
| 194 | OCTET_STRING_encode_uper, |
| 195 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 196 | GeneralizedTime_random_fill, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 197 | 0 /* Use generic outmost tag fetcher */ |
| 198 | }; |
| 199 | asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = { |
| 200 | "GeneralizedTime", |
| 201 | "GeneralizedTime", |
| 202 | &asn_OP_GeneralizedTime, |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 203 | asn_DEF_GeneralizedTime_tags, |
| 204 | sizeof(asn_DEF_GeneralizedTime_tags) |
| 205 | / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2, |
| 206 | asn_DEF_GeneralizedTime_tags, |
| 207 | sizeof(asn_DEF_GeneralizedTime_tags) |
| 208 | / sizeof(asn_DEF_GeneralizedTime_tags[0]), |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 209 | { 0, &asn_DEF_GeneralizedTime_per_constraints, GeneralizedTime_constraint }, |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 210 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 211 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 214 | #endif /* ASN___INTERNAL_TEST_MODE */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * Check that the time looks like the time. |
| 218 | */ |
| 219 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 220 | GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 221 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 222 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 223 | time_t tloc; |
| 224 | |
| 225 | errno = EPERM; /* Just an unlikely error code */ |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 226 | tloc = asn_GT2time(st, 0, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 227 | if(tloc == -1 && errno != EPERM) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 228 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 229 | "%s: Invalid time format: %s (%s:%d)", |
| 230 | td->name, strerror(errno), __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 237 | asn_enc_rval_t |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 238 | GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 239 | int tag_mode, ber_tlv_tag_t tag, |
| 240 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 241 | GeneralizedTime_t *st = (GeneralizedTime_t *)sptr; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 242 | asn_enc_rval_t erval; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 243 | int fv, fd; /* seconds fraction value and number of digits */ |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 244 | struct tm tm; |
| 245 | time_t tloc; |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 246 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 247 | /* |
| 248 | * Encode as a canonical DER. |
| 249 | */ |
| 250 | errno = EPERM; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 251 | tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */ |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 252 | if(tloc == -1 && errno != EPERM) |
| 253 | /* Failed to recognize time. Fail completely. */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 254 | ASN__ENCODE_FAILED; |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 255 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 256 | st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 257 | if(!st) ASN__ENCODE_FAILED; /* Memory allocation failure. */ |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 258 | |
| 259 | erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key); |
| 260 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 261 | FREEMEM(st->buf); |
| 262 | FREEMEM(st); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 263 | |
| 264 | return erval; |
| 265 | } |
| 266 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 267 | #ifndef ASN___INTERNAL_TEST_MODE |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 268 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 269 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 270 | GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 271 | int ilevel, enum xer_encoder_flags_e flags, |
| 272 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 273 | |
| 274 | if(flags & XER_F_CANONICAL) { |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 275 | GeneralizedTime_t *gt; |
| 276 | asn_enc_rval_t rv; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 277 | int fv, fd; /* fractional parts */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 278 | struct tm tm; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 279 | |
| 280 | errno = EPERM; |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 281 | if(asn_GT2time_frac((GeneralizedTime_t *)sptr, |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 282 | &fv, &fd, &tm, 1) == -1 |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 283 | && errno != EPERM) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 284 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 285 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 286 | gt = asn_time2GT_frac(0, &tm, fv, fd, 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 287 | if(!gt) ASN__ENCODE_FAILED; |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 288 | |
| 289 | rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 290 | cb, app_key); |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 291 | ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt); |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 292 | return rv; |
| 293 | } else { |
| 294 | return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 295 | cb, app_key); |
| 296 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 299 | #endif /* ASN___INTERNAL_TEST_MODE */ |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 300 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 301 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 302 | GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 303 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 304 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 305 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 306 | (void)td; /* Unused argument */ |
| 307 | (void)ilevel; /* Unused argument */ |
| 308 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 309 | if(st && st->buf) { |
| 310 | char buf[32]; |
| 311 | struct tm tm; |
| 312 | int ret; |
| 313 | |
| 314 | errno = EPERM; |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 315 | if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM) |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 316 | return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 317 | |
| 318 | ret = snprintf(buf, sizeof(buf), |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 319 | "%04d-%02d-%02d %02d:%02d:%02d (GMT)", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 320 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 321 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 322 | assert(ret > 0 && ret < (int)sizeof(buf)); |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 323 | return (cb(buf, ret, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 324 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 325 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 329 | time_t |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 330 | 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] | 331 | return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt); |
| 332 | } |
| 333 | |
| 334 | time_t |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 335 | asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) { |
| 336 | time_t tloc; |
| 337 | int fv, fd = 0; |
| 338 | |
| 339 | if(frac_value) |
| 340 | tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt); |
| 341 | else |
| 342 | return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt); |
| 343 | if(fd == 0 || frac_digits <= 0) { |
| 344 | *frac_value = 0; |
| 345 | } else { |
| 346 | while(fd > frac_digits) |
| 347 | fv /= 10, fd--; |
| 348 | while(fd < frac_digits) { |
Lev Walkin | 2712618 | 2012-09-03 01:09:54 -0700 | [diff] [blame] | 349 | if(fv < INT_MAX / 10) { |
| 350 | fv *= 10; |
| 351 | fd++; |
| 352 | } else { |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 353 | /* Too long precision request */ |
| 354 | fv = 0; |
| 355 | break; |
| 356 | } |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | *frac_value = fv; |
| 360 | } |
| 361 | |
| 362 | return tloc; |
| 363 | } |
| 364 | |
| 365 | time_t |
| 366 | 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] | 367 | struct tm tm_s; |
| 368 | uint8_t *buf; |
| 369 | uint8_t *end; |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 370 | int gmtoff_h = 0; |
| 371 | int gmtoff_m = 0; |
| 372 | int gmtoff = 0; /* h + m */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 373 | int offset_specified = 0; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 374 | int fvalue = 0; |
| 375 | int fdigits = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 376 | time_t tloc; |
| 377 | |
| 378 | if(!st || !st->buf) { |
| 379 | errno = EINVAL; |
| 380 | return -1; |
| 381 | } else { |
| 382 | buf = st->buf; |
| 383 | end = buf + st->size; |
| 384 | } |
| 385 | |
| 386 | if(st->size < 10) { |
| 387 | errno = EINVAL; |
| 388 | return -1; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Decode first 10 bytes: "AAAAMMJJhh" |
| 393 | */ |
| 394 | memset(&tm_s, 0, sizeof(tm_s)); |
| 395 | #undef B2F |
| 396 | #undef B2T |
| 397 | #define B2F(var) do { \ |
| 398 | unsigned ch = *buf; \ |
Lev Walkin | c81dab1 | 2006-01-10 08:08:14 +0000 | [diff] [blame] | 399 | if(ch < 0x30 || ch > 0x39) { \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 400 | errno = EINVAL; \ |
| 401 | return -1; \ |
| 402 | } else { \ |
| 403 | var = var * 10 + (ch - 0x30); \ |
| 404 | buf++; \ |
| 405 | } \ |
| 406 | } while(0) |
| 407 | #define B2T(var) B2F(tm_s.var) |
| 408 | |
| 409 | B2T(tm_year); /* 1: A */ |
| 410 | B2T(tm_year); /* 2: A */ |
| 411 | B2T(tm_year); /* 3: A */ |
| 412 | B2T(tm_year); /* 4: A */ |
| 413 | B2T(tm_mon); /* 5: M */ |
| 414 | B2T(tm_mon); /* 6: M */ |
| 415 | B2T(tm_mday); /* 7: J */ |
| 416 | B2T(tm_mday); /* 8: J */ |
| 417 | B2T(tm_hour); /* 9: h */ |
| 418 | B2T(tm_hour); /* 0: h */ |
| 419 | |
| 420 | if(buf == end) goto local_finish; |
| 421 | |
| 422 | /* |
| 423 | * Parse [mm[ss[(.|,)ffff]]] |
| 424 | * ^^ |
| 425 | */ |
| 426 | switch(*buf) { |
| 427 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 428 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 429 | tm_s.tm_min = (*buf++) - 0x30; |
| 430 | if(buf == end) { errno = EINVAL; return -1; } |
| 431 | B2T(tm_min); |
| 432 | break; |
| 433 | case 0x2B: case 0x2D: /* +, - */ |
| 434 | goto offset; |
| 435 | case 0x5A: /* Z */ |
| 436 | goto utc_finish; |
| 437 | default: |
| 438 | errno = EINVAL; |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | if(buf == end) goto local_finish; |
| 443 | |
| 444 | /* |
| 445 | * Parse [mm[ss[(.|,)ffff]]] |
| 446 | * ^^ |
| 447 | */ |
| 448 | switch(*buf) { |
| 449 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 450 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 451 | tm_s.tm_sec = (*buf++) - 0x30; |
| 452 | if(buf == end) { errno = EINVAL; return -1; } |
| 453 | B2T(tm_sec); |
| 454 | break; |
| 455 | case 0x2B: case 0x2D: /* +, - */ |
| 456 | goto offset; |
| 457 | case 0x5A: /* Z */ |
| 458 | goto utc_finish; |
| 459 | default: |
| 460 | errno = EINVAL; |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | if(buf == end) goto local_finish; |
| 465 | |
| 466 | /* |
| 467 | * Parse [mm[ss[(.|,)ffff]]] |
| 468 | * ^ ^ |
| 469 | */ |
| 470 | switch(*buf) { |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 471 | case 0x2C: case 0x2E: /* (.|,) */ |
| 472 | /* |
| 473 | * Process fractions of seconds. |
| 474 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 475 | for(buf++; buf < end; buf++) { |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 476 | int v = *buf; |
Lev Walkin | 9a338f8 | 2012-01-22 16:38:37 -0800 | [diff] [blame] | 477 | /* GCC 4.x is being too smart without volatile */ |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 478 | switch(v) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 479 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 480 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
Lev Walkin | 2712618 | 2012-09-03 01:09:54 -0700 | [diff] [blame] | 481 | if(fvalue < INT_MAX/10) { |
| 482 | fvalue = fvalue * 10 + (v - 0x30); |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 483 | fdigits++; |
Lev Walkin | 2712618 | 2012-09-03 01:09:54 -0700 | [diff] [blame] | 484 | } else { |
| 485 | /* Not enough precision, ignore */ |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 486 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 487 | continue; |
| 488 | default: |
| 489 | break; |
| 490 | } |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if(buf == end) goto local_finish; |
| 496 | |
| 497 | switch(*buf) { |
| 498 | case 0x2B: case 0x2D: /* +, - */ |
| 499 | goto offset; |
| 500 | case 0x5A: /* Z */ |
| 501 | goto utc_finish; |
| 502 | default: |
| 503 | errno = EINVAL; |
| 504 | return -1; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | offset: |
| 509 | |
| 510 | if(end - buf < 3) { |
| 511 | errno = EINVAL; |
| 512 | return -1; |
| 513 | } |
| 514 | buf++; |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 515 | B2F(gmtoff_h); |
| 516 | B2F(gmtoff_h); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 517 | if(buf[-3] == 0x2D) /* Negative */ |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 518 | gmtoff = -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 519 | else |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 520 | gmtoff = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 521 | |
| 522 | if((end - buf) == 2) { |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 523 | B2F(gmtoff_m); |
| 524 | B2F(gmtoff_m); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 525 | } else if(end != buf) { |
| 526 | errno = EINVAL; |
| 527 | return -1; |
| 528 | } |
| 529 | |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 530 | gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 531 | |
| 532 | /* Fall through */ |
| 533 | utc_finish: |
| 534 | |
| 535 | offset_specified = 1; |
| 536 | |
| 537 | /* Fall through */ |
| 538 | local_finish: |
| 539 | |
| 540 | /* |
| 541 | * Validation. |
| 542 | */ |
| 543 | if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1) |
| 544 | || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1) |
| 545 | || (tm_s.tm_hour > 23) |
| 546 | || (tm_s.tm_sec > 60) |
| 547 | ) { |
| 548 | errno = EINVAL; |
| 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | /* Canonicalize */ |
| 553 | tm_s.tm_mon -= 1; /* 0 - 11 */ |
| 554 | tm_s.tm_year -= 1900; |
| 555 | tm_s.tm_isdst = -1; |
| 556 | |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 557 | tm_s.tm_sec -= gmtoff; |
| 558 | |
| 559 | /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/ |
| 560 | |
Lev Walkin | 02a3155 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 561 | if(offset_specified) { |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 562 | tloc = timegm(&tm_s); |
Lev Walkin | 02a3155 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 563 | } else { |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 564 | /* |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 565 | * Without an offset (or "Z"), |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 566 | * we can only guess that it is a local zone. |
| 567 | * Interpret it in this fashion. |
| 568 | */ |
| 569 | tloc = mktime(&tm_s); |
| 570 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 571 | if(tloc == -1) { |
| 572 | errno = EINVAL; |
| 573 | return -1; |
| 574 | } |
| 575 | |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 576 | if(ret_tm) { |
| 577 | if(as_gmt) { |
| 578 | if(offset_specified) { |
| 579 | *ret_tm = tm_s; |
| 580 | } else { |
| 581 | if(gmtime_r(&tloc, ret_tm) == 0) { |
| 582 | errno = EINVAL; |
| 583 | return -1; |
| 584 | } |
| 585 | } |
| 586 | } else { |
| 587 | if(localtime_r(&tloc, ret_tm) == 0) { |
| 588 | errno = EINVAL; |
| 589 | return -1; |
| 590 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 594 | /* Fractions of seconds */ |
| 595 | if(frac_value) *frac_value = fvalue; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 596 | if(frac_digits) *frac_digits = fdigits; |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 597 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 598 | return tloc; |
| 599 | } |
| 600 | |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 601 | GeneralizedTime_t * |
| 602 | 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] | 603 | return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt); |
| 604 | } |
| 605 | |
| 606 | GeneralizedTime_t * |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 607 | 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] | 608 | struct tm tm_s; |
| 609 | long gmtoff; |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 610 | const unsigned int buf_size = |
| 611 | 4 + 2 + 2 /* yyyymmdd */ |
| 612 | + 2 + 2 + 2 /* hhmmss */ |
| 613 | + 1 + 6 /* .ffffff */ |
| 614 | + 1 + 4 /* +hhmm */ |
| 615 | + 1 /* '\0' */ |
| 616 | ; |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 617 | char *buf; |
| 618 | char *p; |
| 619 | int size; |
| 620 | |
| 621 | /* Check arguments */ |
| 622 | if(!tm) { |
| 623 | errno = EINVAL; |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* Pre-allocate a buffer of sufficient yet small length */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 628 | buf = (char *)MALLOC(buf_size); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 629 | if(!buf) return 0; |
| 630 | |
| 631 | gmtoff = GMTOFF(*tm); |
| 632 | |
| 633 | if(force_gmt && gmtoff) { |
| 634 | tm_s = *tm; |
| 635 | tm_s.tm_sec -= gmtoff; |
| 636 | timegm(&tm_s); /* Fix the time */ |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 637 | tm = &tm_s; |
Lev Walkin | 659a751 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 638 | #ifdef HAVE_TM_GMTOFF |
| 639 | assert(!GMTOFF(tm_s)); /* Will fix itself */ |
Lev Walkin | c8c2cb5 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 640 | #else /* !HAVE_TM_GMTOFF */ |
| 641 | gmtoff = 0; |
Lev Walkin | 659a751 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 642 | #endif |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d", |
| 646 | tm->tm_year + 1900, |
| 647 | tm->tm_mon + 1, |
| 648 | tm->tm_mday, |
| 649 | tm->tm_hour, |
| 650 | tm->tm_min, |
| 651 | tm->tm_sec |
| 652 | ); |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 653 | if(size != 14) { |
| 654 | /* Could be assert(size == 14); */ |
| 655 | FREEMEM(buf); |
| 656 | errno = EINVAL; |
| 657 | return 0; |
| 658 | } |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 659 | |
| 660 | p = buf + size; |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 661 | |
| 662 | /* |
| 663 | * Deal with fractions. |
| 664 | */ |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 665 | if(frac_value > 0 && frac_digits > 0) { |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 666 | char *end = p + 1 + 6; /* '.' + maximum 6 digits */ |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 667 | char *z = p; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 668 | long fbase; |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 669 | *z++ = '.'; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 670 | |
| 671 | /* Place bounds on precision */ |
| 672 | while(frac_digits-- > 6) |
| 673 | frac_value /= 10; |
| 674 | |
| 675 | /* emulate fbase = pow(10, frac_digits) */ |
| 676 | for(fbase = 1; frac_digits--;) |
| 677 | fbase *= 10; |
| 678 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 679 | do { |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 680 | int digit = frac_value / fbase; |
Lev Walkin | 88e0a77 | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 681 | if(digit > 9) { z = 0; break; } |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 682 | *z++ = digit + 0x30; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 683 | frac_value %= fbase; |
| 684 | fbase /= 10; |
| 685 | } while(fbase > 0 && frac_value > 0 && z < end); |
| 686 | if(z) { |
Lev Walkin | 88e0a77 | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 687 | for(--z; *z == 0x30; --z); /* Strip zeroes */ |
| 688 | p = z + (*z != '.'); |
| 689 | size = p - buf; |
| 690 | } |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 693 | if(force_gmt) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 694 | *p++ = 0x5a; /* "Z" */ |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 695 | *p++ = 0; |
| 696 | size++; |
| 697 | } else { |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 698 | int ret; |
| 699 | gmtoff %= 86400; |
| 700 | ret = snprintf(p, buf_size - size, "%+03ld%02ld", |
Lev Walkin | a4347cc | 2010-03-10 23:05:56 +0000 | [diff] [blame] | 701 | gmtoff / 3600, labs(gmtoff % 3600) / 60); |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 702 | if(ret != 5) { |
| 703 | FREEMEM(buf); |
| 704 | errno = EINVAL; |
| 705 | return 0; |
| 706 | } |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 707 | size += ret; |
| 708 | } |
| 709 | |
| 710 | if(opt_gt) { |
| 711 | if(opt_gt->buf) |
| 712 | FREEMEM(opt_gt->buf); |
| 713 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 714 | opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt); |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 715 | if(!opt_gt) { FREEMEM(buf); return 0; } |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 718 | opt_gt->buf = (unsigned char *)buf; |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 719 | opt_gt->size = size; |
| 720 | |
| 721 | return opt_gt; |
| 722 | } |
| 723 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 724 | asn_random_fill_result_t |
| 725 | GeneralizedTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, |
| 726 | const asn_encoding_constraints_t *constraints, |
| 727 | size_t max_length) { |
| 728 | asn_random_fill_result_t result_ok = {ARFILL_OK, 1}; |
| 729 | asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; |
| 730 | asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; |
| 731 | static const char *values[] = { |
| 732 | "19700101000000", "19700101000000-0000", "19700101000000+0000", |
| 733 | "19700101000000Z", "19700101000000.3Z", "19821106210623.3", |
| 734 | "19821106210629.3Z", "19691106210827.3-0500", "19821106210629.456", |
| 735 | }; |
| 736 | size_t rnd = asn_random_between(0, sizeof(values)/sizeof(values[0])-1); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 737 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 738 | (void)constraints; |
| 739 | |
| 740 | if(max_length < sizeof("yyyymmddhhmmss")) { |
| 741 | return result_skipped; |
| 742 | } |
| 743 | |
| 744 | if(*sptr) { |
| 745 | if(OCTET_STRING_fromBuf(*sptr, values[rnd], -1) != 0) { |
| 746 | if(!sptr) return result_failed; |
| 747 | } |
| 748 | } else { |
| 749 | *sptr = OCTET_STRING_new_fromBuf(td, values[rnd], -1); |
| 750 | if(!sptr) return result_failed; |
| 751 | } |
| 752 | |
| 753 | return result_ok; |
| 754 | } |