blob: 3793a0e691bfc0c849879eebbc92bb2d87ce4922 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#include <UTCTime.h>
7#include <GeneralizedTime.h>
8#include <time.h>
9#include <errno.h>
vlmfa67ddc2004-06-03 03:38:44 +000010
vlm9f636642005-07-03 05:29:36 +000011#ifndef __ASN_INTERNAL_TEST_MODE__
vlmfa67ddc2004-06-03 03:38:44 +000012
13/*
14 * UTCTime basic type description.
15 */
vlmef6355b2004-09-29 13:26:15 +000016static ber_tlv_tag_t asn_DEF_UTCTime_tags[] = {
vlm6678cb12004-09-26 13:10:40 +000017 (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 */
vlmfa67ddc2004-06-03 03:38:44 +000020};
vlmef6355b2004-09-29 13:26:15 +000021asn_TYPE_descriptor_t asn_DEF_UTCTime = {
vlmfa67ddc2004-06-03 03:38:44 +000022 "UTCTime",
vlm9de248e2004-10-20 15:50:55 +000023 "UTCTime",
vlm39ba4c42004-09-22 16:06:28 +000024 OCTET_STRING_free,
25 UTCTime_print,
vlmfa67ddc2004-06-03 03:38:44 +000026 UTCTime_constraint,
27 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
28 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
vlm9de248e2004-10-20 15:50:55 +000029 OCTET_STRING_decode_xer_utf8,
vlm39ba4c42004-09-22 16:06:28 +000030 UTCTime_encode_xer,
vlm18dd82c2006-08-18 01:34:18 +000031 0, 0,
vlmfa67ddc2004-06-03 03:38:44 +000032 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +000033 asn_DEF_UTCTime_tags,
34 sizeof(asn_DEF_UTCTime_tags)
35 / sizeof(asn_DEF_UTCTime_tags[0]) - 2,
36 asn_DEF_UTCTime_tags,
37 sizeof(asn_DEF_UTCTime_tags)
38 / sizeof(asn_DEF_UTCTime_tags[0]),
vlm337167e2005-11-26 11:25:14 +000039 0, /* No PER visible constraints */
vlme413c122004-08-20 13:23:42 +000040 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000041 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000042};
43
vlm9f636642005-07-03 05:29:36 +000044#endif /* __ASN_INTERNAL_TEST_MODE__ */
vlmfa67ddc2004-06-03 03:38:44 +000045
46/*
47 * Check that the time looks like the time.
48 */
49int
vlmef6355b2004-09-29 13:26:15 +000050UTCTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmaf68ef52006-07-13 11:19:01 +000051 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
vlmda674682004-08-11 09:07:36 +000052 const UTCTime_t *st = (const UTCTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000053 time_t tloc;
54
55 errno = EPERM; /* Just an unlikely error code */
vlmd0c608e2004-08-07 03:51:43 +000056 tloc = asn_UT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +000057 if(tloc == -1 && errno != EPERM) {
vlmaf68ef52006-07-13 11:19:01 +000058 _ASN_CTFAIL(app_key, td,
vlm758530a2004-08-22 13:47:59 +000059 "%s: Invalid time format: %s (%s:%d)",
60 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000061 return -1;
62 }
63
64 return 0;
65}
66
vlm9f636642005-07-03 05:29:36 +000067#ifndef __ASN_INTERNAL_TEST_MODE__
68
vlm39ba4c42004-09-22 16:06:28 +000069asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +000070UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +000071 int ilevel, enum xer_encoder_flags_e flags,
72 asn_app_consume_bytes_f *cb, void *app_key) {
vlm39ba4c42004-09-22 16:06:28 +000073
74 if(flags & XER_F_CANONICAL) {
vlm9f636642005-07-03 05:29:36 +000075 asn_enc_rval_t rv;
76 UTCTime_t *ut;
vlm39ba4c42004-09-22 16:06:28 +000077 struct tm tm;
vlm39ba4c42004-09-22 16:06:28 +000078
79 errno = EPERM;
80 if(asn_UT2time((UTCTime_t *)sptr, &tm, 1) == -1
81 && errno != EPERM)
82 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +000083
vlm9f636642005-07-03 05:29:36 +000084 /* Fractions are not allowed in UTCTime */
85 ut = asn_time2GT(0, 0, 1);
86 if(!ut) _ASN_ENCODE_FAILED;
87
88 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
89 cb, app_key);
90 OCTET_STRING_free(&asn_DEF_UTCTime, ut, 0);
91 return rv;
92 } else {
93 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
94 cb, app_key);
95 }
vlm39ba4c42004-09-22 16:06:28 +000096}
97
vlm9f636642005-07-03 05:29:36 +000098#endif /* __ASN_INTERNAL_TEST_MODE__ */
99
vlmfa67ddc2004-06-03 03:38:44 +0000100int
vlmef6355b2004-09-29 13:26:15 +0000101UTCTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000102 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000103 const UTCTime_t *st = (const UTCTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000104
vlmb42843a2004-06-05 08:17:50 +0000105 (void)td; /* Unused argument */
106 (void)ilevel; /* Unused argument */
107
vlmfa67ddc2004-06-03 03:38:44 +0000108 if(st && st->buf) {
109 char buf[32];
110 struct tm tm;
111 int ret;
112
113 errno = EPERM;
vlmd0c608e2004-08-07 03:51:43 +0000114 if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
vlm6678cb12004-09-26 13:10:40 +0000115 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000116
117 ret = snprintf(buf, sizeof(buf),
vlm9f636642005-07-03 05:29:36 +0000118 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000119 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
120 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000121 assert(ret > 0 && ret < (int)sizeof(buf));
vlm6678cb12004-09-26 13:10:40 +0000122 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000123 } else {
vlm6678cb12004-09-26 13:10:40 +0000124 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000125 }
126}
127
128time_t
vlmd0c608e2004-08-07 03:51:43 +0000129asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
vlm9f636642005-07-03 05:29:36 +0000130 char buf[24]; /* "AAMMJJhhmmss+hhmm" + cushion */
vlmfa67ddc2004-06-03 03:38:44 +0000131 GeneralizedTime_t gt;
132
vlmb42843a2004-06-05 08:17:50 +0000133 if(!st || !st->buf
vlm9f636642005-07-03 05:29:36 +0000134 || st->size < 11 || st->size >= ((int)sizeof(buf) - 2)) {
vlmfa67ddc2004-06-03 03:38:44 +0000135 errno = EINVAL;
136 return -1;
137 }
138
vlm1ff928d2004-08-11 08:10:13 +0000139 gt.buf = (unsigned char *)buf;
vlmfa67ddc2004-06-03 03:38:44 +0000140 gt.size = st->size + 2;
141 memcpy(gt.buf + 2, st->buf, st->size);
142 if(st->buf[0] > 0x35) {
143 /* 19xx */
144 gt.buf[0] = 0x31;
145 gt.buf[1] = 0x39;
146 } else {
147 /* 20xx */
148 gt.buf[0] = 0x32;
149 gt.buf[1] = 0x30;
150 }
151
vlmd0c608e2004-08-07 03:51:43 +0000152 return asn_GT2time(&gt, _tm, as_gmt);
vlmfa67ddc2004-06-03 03:38:44 +0000153}
vlmd0c608e2004-08-07 03:51:43 +0000154
155UTCTime_t *
156asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) {
157 GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut;
158
159 gt = asn_time2GT(gt, tm, force_gmt);
160 if(gt == 0) return 0;
161
162 assert(gt->size >= 2);
163 gt->size -= 2;
164 memmove(gt->buf, gt->buf + 2, gt->size + 1);
165
166 return (UTCTime_t *)gt;
167}
168