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 | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <UTCTime.h> |
| 7 | #include <GeneralizedTime.h> |
| 8 | #include <time.h> |
| 9 | #include <errno.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | |
Lev Walkin | 7b3713a | 2005-07-03 05:29:36 +0000 | [diff] [blame] | 11 | #ifndef __ASN_INTERNAL_TEST_MODE__ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 12 | |
| 13 | /* |
| 14 | * UTCTime basic type description. |
| 15 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 16 | static ber_tlv_tag_t asn_DEF_UTCTime_tags[] = { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 17 | (ASN_TAG_CLASS_UNIVERSAL | (23 << 2)), /* [UNIVERSAL 23] IMPLICIT ...*/ |
| 18 | (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/ |
| 19 | (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | }; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 21 | asn_TYPE_descriptor_t asn_DEF_UTCTime = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | "UTCTime", |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 23 | "UTCTime", |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 24 | OCTET_STRING_free, |
| 25 | UTCTime_print, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | UTCTime_constraint, |
| 27 | OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
| 28 | OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 29 | OCTET_STRING_decode_xer_utf8, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 30 | UTCTime_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 31 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 32 | asn_DEF_UTCTime_tags, |
| 33 | sizeof(asn_DEF_UTCTime_tags) |
| 34 | / sizeof(asn_DEF_UTCTime_tags[0]) - 2, |
| 35 | asn_DEF_UTCTime_tags, |
| 36 | sizeof(asn_DEF_UTCTime_tags) |
| 37 | / sizeof(asn_DEF_UTCTime_tags[0]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 38 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 39 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Lev Walkin | 7b3713a | 2005-07-03 05:29:36 +0000 | [diff] [blame] | 42 | #endif /* __ASN_INTERNAL_TEST_MODE__ */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Check that the time looks like the time. |
| 46 | */ |
| 47 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 48 | UTCTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 50 | const UTCTime_t *st = (const UTCTime_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | time_t tloc; |
| 52 | |
| 53 | errno = EPERM; /* Just an unlikely error code */ |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 54 | tloc = asn_UT2time(st, 0, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | if(tloc == -1 && errno != EPERM) { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 56 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 57 | "%s: Invalid time format: %s (%s:%d)", |
| 58 | td->name, strerror(errno), __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Lev Walkin | 7b3713a | 2005-07-03 05:29:36 +0000 | [diff] [blame] | 65 | #ifndef __ASN_INTERNAL_TEST_MODE__ |
| 66 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 67 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 68 | UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 69 | int ilevel, enum xer_encoder_flags_e flags, |
| 70 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 71 | |
| 72 | if(flags & XER_F_CANONICAL) { |
Lev Walkin | 7b3713a | 2005-07-03 05:29:36 +0000 | [diff] [blame] | 73 | asn_enc_rval_t rv; |
| 74 | UTCTime_t *ut; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 75 | struct tm tm; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 76 | |
| 77 | errno = EPERM; |
| 78 | if(asn_UT2time((UTCTime_t *)sptr, &tm, 1) == -1 |
| 79 | && errno != EPERM) |
| 80 | _ASN_ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 81 | |
Lev Walkin | 7b3713a | 2005-07-03 05:29:36 +0000 | [diff] [blame] | 82 | /* Fractions are not allowed in UTCTime */ |
| 83 | ut = asn_time2GT(0, 0, 1); |
| 84 | if(!ut) _ASN_ENCODE_FAILED; |
| 85 | |
| 86 | rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 87 | cb, app_key); |
| 88 | OCTET_STRING_free(&asn_DEF_UTCTime, ut, 0); |
| 89 | return rv; |
| 90 | } else { |
| 91 | return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 92 | cb, app_key); |
| 93 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Lev Walkin | 7b3713a | 2005-07-03 05:29:36 +0000 | [diff] [blame] | 96 | #endif /* __ASN_INTERNAL_TEST_MODE__ */ |
| 97 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 98 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 99 | UTCTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 101 | const UTCTime_t *st = (const UTCTime_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 103 | (void)td; /* Unused argument */ |
| 104 | (void)ilevel; /* Unused argument */ |
| 105 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | if(st && st->buf) { |
| 107 | char buf[32]; |
| 108 | struct tm tm; |
| 109 | int ret; |
| 110 | |
| 111 | errno = EPERM; |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 112 | if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM) |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 113 | return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | |
| 115 | ret = snprintf(buf, sizeof(buf), |
Lev Walkin | 7b3713a | 2005-07-03 05:29:36 +0000 | [diff] [blame] | 116 | "%04d-%02d-%02d %02d:%02d:%02d (GMT)", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 118 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 119 | assert(ret > 0 && ret < (int)sizeof(buf)); |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 120 | return (cb(buf, ret, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 121 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 122 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | time_t |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 127 | asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) { |
Lev Walkin | 7b3713a | 2005-07-03 05:29:36 +0000 | [diff] [blame] | 128 | char buf[24]; /* "AAMMJJhhmmss+hhmm" + cushion */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | GeneralizedTime_t gt; |
| 130 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 131 | if(!st || !st->buf |
Lev Walkin | 7b3713a | 2005-07-03 05:29:36 +0000 | [diff] [blame] | 132 | || st->size < 11 || st->size >= ((int)sizeof(buf) - 2)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 133 | errno = EINVAL; |
| 134 | return -1; |
| 135 | } |
| 136 | |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 137 | gt.buf = (unsigned char *)buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 138 | gt.size = st->size + 2; |
| 139 | memcpy(gt.buf + 2, st->buf, st->size); |
| 140 | if(st->buf[0] > 0x35) { |
| 141 | /* 19xx */ |
| 142 | gt.buf[0] = 0x31; |
| 143 | gt.buf[1] = 0x39; |
| 144 | } else { |
| 145 | /* 20xx */ |
| 146 | gt.buf[0] = 0x32; |
| 147 | gt.buf[1] = 0x30; |
| 148 | } |
| 149 | |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 150 | return asn_GT2time(>, _tm, as_gmt); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | } |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 152 | |
| 153 | UTCTime_t * |
| 154 | asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) { |
| 155 | GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut; |
| 156 | |
| 157 | gt = asn_time2GT(gt, tm, force_gmt); |
| 158 | if(gt == 0) return 0; |
| 159 | |
| 160 | assert(gt->size >= 2); |
| 161 | gt->size -= 2; |
| 162 | memmove(gt->buf, gt->buf + 2, gt->size + 1); |
| 163 | |
| 164 | return (UTCTime_t *)gt; |
| 165 | } |
| 166 | |