| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <asn_internal.h> |
| 6 |
#include <UTCTime.h> |
| 7 |
#include <GeneralizedTime.h> |
| 8 |
#include <errno.h> |
| 9 |
|
| 10 |
#ifdef __CYGWIN__ |
| 11 |
#include "/usr/include/time.h" |
| 12 |
#else |
| 13 |
#include <time.h> |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifndef ASN___INTERNAL_TEST_MODE |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
static const ber_tlv_tag_t asn_DEF_UTCTime_tags[] = { |
| 22 |
(ASN_TAG_CLASS_UNIVERSAL | (23 << 2)), |
| 23 |
(ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), |
| 24 |
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) |
| 25 |
}; |
| 26 |
static asn_per_constraints_t asn_DEF_UTCTime_constraints = { |
| 27 |
{ APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, |
| 28 |
{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, |
| 29 |
0, 0 |
| 30 |
}; |
| 31 |
asn_TYPE_descriptor_t asn_DEF_UTCTime = { |
| 32 |
"UTCTime", |
| 33 |
"UTCTime", |
| 34 |
OCTET_STRING_free, |
| 35 |
UTCTime_print, |
| 36 |
UTCTime_constraint, |
| 37 |
OCTET_STRING_decode_ber, |
| 38 |
OCTET_STRING_encode_der, |
| 39 |
OCTET_STRING_decode_xer_utf8, |
| 40 |
UTCTime_encode_xer, |
| 41 |
OCTET_STRING_decode_uper, |
| 42 |
OCTET_STRING_encode_uper, |
| 43 |
0, |
| 44 |
asn_DEF_UTCTime_tags, |
| 45 |
sizeof(asn_DEF_UTCTime_tags) |
| 46 |
/ sizeof(asn_DEF_UTCTime_tags[0]) - 2, |
| 47 |
asn_DEF_UTCTime_tags, |
| 48 |
sizeof(asn_DEF_UTCTime_tags) |
| 49 |
/ sizeof(asn_DEF_UTCTime_tags[0]), |
| 50 |
&asn_DEF_UTCTime_constraints, |
| 51 |
0, 0, |
| 52 |
0 |
| 53 |
}; |
| 54 |
|
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
int |
| 61 |
UTCTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
| 62 |
asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
| 63 |
const UTCTime_t *st = (const UTCTime_t *)sptr; |
| 64 |
time_t tloc; |
| 65 |
|
| 66 |
errno = EPERM; |
| 67 |
tloc = asn_UT2time(st, 0, 0); |
| 68 |
if(tloc == -1 && errno != EPERM) { |
| 69 |
ASN__CTFAIL(app_key, td, sptr, |
| 70 |
"%s: Invalid time format: %s (%s:%d)", |
| 71 |
td->name, strerror(errno), __FILE__, __LINE__); |
| 72 |
return -1; |
| 73 |
} |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
#ifndef ASN___INTERNAL_TEST_MODE |
| 79 |
|
| 80 |
asn_enc_rval_t |
| 81 |
UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
| 82 |
int ilevel, enum xer_encoder_flags_e flags, |
| 83 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 84 |
|
| 85 |
if(flags & XER_F_CANONICAL) { |
| 86 |
asn_enc_rval_t rv; |
| 87 |
UTCTime_t *ut; |
| 88 |
struct tm tm; |
| 89 |
|
| 90 |
errno = EPERM; |
| 91 |
if(asn_UT2time((UTCTime_t *)sptr, &tm, 1) == -1 |
| 92 |
&& errno != EPERM) |
| 93 |
ASN__ENCODE_FAILED; |
| 94 |
|
| 95 |
|
| 96 |
ut = asn_time2GT(0, 0, 1); |
| 97 |
if(!ut) ASN__ENCODE_FAILED; |
| 98 |
|
| 99 |
rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 100 |
cb, app_key); |
| 101 |
OCTET_STRING_free(&asn_DEF_UTCTime, ut, 0); |
| 102 |
return rv; |
| 103 |
} else { |
| 104 |
return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 105 |
cb, app_key); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
#endif |
| 110 |
|
| 111 |
int |
| 112 |
UTCTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 113 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 114 |
const UTCTime_t *st = (const UTCTime_t *)sptr; |
| 115 |
|
| 116 |
(void)td; |
| 117 |
(void)ilevel; |
| 118 |
|
| 119 |
if(st && st->buf) { |
| 120 |
char buf[32]; |
| 121 |
struct tm tm; |
| 122 |
int ret; |
| 123 |
|
| 124 |
errno = EPERM; |
| 125 |
if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM) |
| 126 |
return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0; |
| 127 |
|
| 128 |
ret = snprintf(buf, sizeof(buf), |
| 129 |
"%04d-%02d-%02d %02d:%02d:%02d (GMT)", |
| 130 |
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 131 |
tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 132 |
assert(ret > 0 && ret < (int)sizeof(buf)); |
| 133 |
return (cb(buf, ret, app_key) < 0) ? -1 : 0; |
| 134 |
} else { |
| 135 |
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
time_t |
| 140 |
asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) { |
| 141 |
char buf[24]; |
| 142 |
GeneralizedTime_t gt; |
| 143 |
|
| 144 |
if(!st || !st->buf |
| 145 |
|| st->size < 11 || st->size >= ((int)sizeof(buf) - 2)) { |
| 146 |
errno = EINVAL; |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
gt.buf = (unsigned char *)buf; |
| 151 |
gt.size = st->size + 2; |
| 152 |
memcpy(gt.buf + 2, st->buf, st->size); |
| 153 |
if(st->buf[0] > 0x35) { |
| 154 |
|
| 155 |
gt.buf[0] = 0x31; |
| 156 |
gt.buf[1] = 0x39; |
| 157 |
} else { |
| 158 |
|
| 159 |
gt.buf[0] = 0x32; |
| 160 |
gt.buf[1] = 0x30; |
| 161 |
} |
| 162 |
|
| 163 |
return asn_GT2time(>, _tm, as_gmt); |
| 164 |
} |
| 165 |
|
| 166 |
UTCTime_t * |
| 167 |
asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) { |
| 168 |
GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut; |
| 169 |
|
| 170 |
gt = asn_time2GT(gt, tm, force_gmt); |
| 171 |
if(gt == 0) return 0; |
| 172 |
|
| 173 |
assert(gt->size >= 2); |
| 174 |
gt->size -= 2; |
| 175 |
memmove(gt->buf, gt->buf + 2, gt->size + 1); |
| 176 |
|
| 177 |
return (UTCTime_t *)gt; |
| 178 |
} |
| 179 |
|