blob: 0abe1db74420584e9d8eb3779ce3429b7aaec6ae [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>
vlmfa67ddc2004-06-03 03:38:44 +00008#include <errno.h>
vlmfa67ddc2004-06-03 03:38:44 +00009
vlma411eee2006-09-13 00:21:58 +000010#ifdef __CYGWIN__
11#include "/usr/include/time.h"
12#else
13#include <time.h>
14#endif /* __CYGWIN__ */
15
vlm9f636642005-07-03 05:29:36 +000016#ifndef __ASN_INTERNAL_TEST_MODE__
vlmfa67ddc2004-06-03 03:38:44 +000017
18/*
19 * UTCTime basic type description.
20 */
vlmef6355b2004-09-29 13:26:15 +000021static ber_tlv_tag_t asn_DEF_UTCTime_tags[] = {
vlm6678cb12004-09-26 13:10:40 +000022 (ASN_TAG_CLASS_UNIVERSAL | (23 << 2)), /* [UNIVERSAL 23] IMPLICIT ...*/
23 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
24 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
vlmfa67ddc2004-06-03 03:38:44 +000025};
vlm86380d32006-10-09 12:07:58 +000026static asn_per_constraints_t asn_DEF_UTCTime_constraints = {
27 { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
28 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
29 0, 0
30};
vlmef6355b2004-09-29 13:26:15 +000031asn_TYPE_descriptor_t asn_DEF_UTCTime = {
vlmfa67ddc2004-06-03 03:38:44 +000032 "UTCTime",
vlm9de248e2004-10-20 15:50:55 +000033 "UTCTime",
vlm39ba4c42004-09-22 16:06:28 +000034 OCTET_STRING_free,
35 UTCTime_print,
vlmfa67ddc2004-06-03 03:38:44 +000036 UTCTime_constraint,
37 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
38 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
vlm9de248e2004-10-20 15:50:55 +000039 OCTET_STRING_decode_xer_utf8,
vlm39ba4c42004-09-22 16:06:28 +000040 UTCTime_encode_xer,
vlm86380d32006-10-09 12:07:58 +000041 OCTET_STRING_decode_uper,
42 OCTET_STRING_encode_uper,
vlmfa67ddc2004-06-03 03:38:44 +000043 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +000044 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]),
vlm86380d32006-10-09 12:07:58 +000050 &asn_DEF_UTCTime_constraints,
vlme413c122004-08-20 13:23:42 +000051 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000052 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000053};
54
vlm9f636642005-07-03 05:29:36 +000055#endif /* __ASN_INTERNAL_TEST_MODE__ */
vlmfa67ddc2004-06-03 03:38:44 +000056
57/*
58 * Check that the time looks like the time.
59 */
60int
vlmef6355b2004-09-29 13:26:15 +000061UTCTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmaf68ef52006-07-13 11:19:01 +000062 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
vlmda674682004-08-11 09:07:36 +000063 const UTCTime_t *st = (const UTCTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000064 time_t tloc;
65
66 errno = EPERM; /* Just an unlikely error code */
vlmd0c608e2004-08-07 03:51:43 +000067 tloc = asn_UT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +000068 if(tloc == -1 && errno != EPERM) {
vlm18269b62006-10-16 12:18:41 +000069 _ASN_CTFAIL(app_key, td, sptr,
vlm758530a2004-08-22 13:47:59 +000070 "%s: Invalid time format: %s (%s:%d)",
71 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000072 return -1;
73 }
74
75 return 0;
76}
77
vlm9f636642005-07-03 05:29:36 +000078#ifndef __ASN_INTERNAL_TEST_MODE__
79
vlm39ba4c42004-09-22 16:06:28 +000080asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +000081UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +000082 int ilevel, enum xer_encoder_flags_e flags,
83 asn_app_consume_bytes_f *cb, void *app_key) {
vlm39ba4c42004-09-22 16:06:28 +000084
85 if(flags & XER_F_CANONICAL) {
vlm9f636642005-07-03 05:29:36 +000086 asn_enc_rval_t rv;
87 UTCTime_t *ut;
vlm39ba4c42004-09-22 16:06:28 +000088 struct tm tm;
vlm39ba4c42004-09-22 16:06:28 +000089
90 errno = EPERM;
91 if(asn_UT2time((UTCTime_t *)sptr, &tm, 1) == -1
92 && errno != EPERM)
93 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +000094
vlm9f636642005-07-03 05:29:36 +000095 /* Fractions are not allowed in UTCTime */
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 }
vlm39ba4c42004-09-22 16:06:28 +0000107}
108
vlm9f636642005-07-03 05:29:36 +0000109#endif /* __ASN_INTERNAL_TEST_MODE__ */
110
vlmfa67ddc2004-06-03 03:38:44 +0000111int
vlmef6355b2004-09-29 13:26:15 +0000112UTCTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000113 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000114 const UTCTime_t *st = (const UTCTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000115
vlmb42843a2004-06-05 08:17:50 +0000116 (void)td; /* Unused argument */
117 (void)ilevel; /* Unused argument */
118
vlmfa67ddc2004-06-03 03:38:44 +0000119 if(st && st->buf) {
120 char buf[32];
121 struct tm tm;
122 int ret;
123
124 errno = EPERM;
vlmd0c608e2004-08-07 03:51:43 +0000125 if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
vlm6678cb12004-09-26 13:10:40 +0000126 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000127
128 ret = snprintf(buf, sizeof(buf),
vlm9f636642005-07-03 05:29:36 +0000129 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000130 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
131 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000132 assert(ret > 0 && ret < (int)sizeof(buf));
vlm6678cb12004-09-26 13:10:40 +0000133 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000134 } else {
vlm6678cb12004-09-26 13:10:40 +0000135 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000136 }
137}
138
139time_t
vlmd0c608e2004-08-07 03:51:43 +0000140asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
vlm9f636642005-07-03 05:29:36 +0000141 char buf[24]; /* "AAMMJJhhmmss+hhmm" + cushion */
vlmfa67ddc2004-06-03 03:38:44 +0000142 GeneralizedTime_t gt;
143
vlmb42843a2004-06-05 08:17:50 +0000144 if(!st || !st->buf
vlm9f636642005-07-03 05:29:36 +0000145 || st->size < 11 || st->size >= ((int)sizeof(buf) - 2)) {
vlmfa67ddc2004-06-03 03:38:44 +0000146 errno = EINVAL;
147 return -1;
148 }
149
vlm1ff928d2004-08-11 08:10:13 +0000150 gt.buf = (unsigned char *)buf;
vlmfa67ddc2004-06-03 03:38:44 +0000151 gt.size = st->size + 2;
152 memcpy(gt.buf + 2, st->buf, st->size);
153 if(st->buf[0] > 0x35) {
154 /* 19xx */
155 gt.buf[0] = 0x31;
156 gt.buf[1] = 0x39;
157 } else {
158 /* 20xx */
159 gt.buf[0] = 0x32;
160 gt.buf[1] = 0x30;
161 }
162
vlmd0c608e2004-08-07 03:51:43 +0000163 return asn_GT2time(&gt, _tm, as_gmt);
vlmfa67ddc2004-06-03 03:38:44 +0000164}
vlmd0c608e2004-08-07 03:51:43 +0000165
166UTCTime_t *
167asn_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