blob: f9de9dd35aeede916cf3327a4d32f5ba05589534 [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>
10#include <assert.h>
11
12#ifndef __NO_ASN_TABLE__
13
14/*
15 * UTCTime basic type description.
16 */
vlmef6355b2004-09-29 13:26:15 +000017static ber_tlv_tag_t asn_DEF_UTCTime_tags[] = {
vlm6678cb12004-09-26 13:10:40 +000018 (ASN_TAG_CLASS_UNIVERSAL | (23 << 2)), /* [UNIVERSAL 23] IMPLICIT ...*/
19 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
20 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
vlmfa67ddc2004-06-03 03:38:44 +000021};
vlmef6355b2004-09-29 13:26:15 +000022asn_TYPE_descriptor_t asn_DEF_UTCTime = {
vlmfa67ddc2004-06-03 03:38:44 +000023 "UTCTime",
vlm9de248e2004-10-20 15:50:55 +000024 "UTCTime",
vlm39ba4c42004-09-22 16:06:28 +000025 OCTET_STRING_free,
26 UTCTime_print,
vlmfa67ddc2004-06-03 03:38:44 +000027 UTCTime_constraint,
28 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
29 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
vlm9de248e2004-10-20 15:50:55 +000030 OCTET_STRING_decode_xer_utf8,
vlm39ba4c42004-09-22 16:06:28 +000031 UTCTime_encode_xer,
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]),
vlme413c122004-08-20 13:23:42 +000039 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000040 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000041};
42
43#endif /* __NO_ASN_TABLE__ */
44
45/*
46 * Check that the time looks like the time.
47 */
48int
vlmef6355b2004-09-29 13:26:15 +000049UTCTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +000050 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +000051 const UTCTime_t *st = (const UTCTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000052 time_t tloc;
53
54 errno = EPERM; /* Just an unlikely error code */
vlmd0c608e2004-08-07 03:51:43 +000055 tloc = asn_UT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +000056 if(tloc == -1 && errno != EPERM) {
vlme3f0f282004-08-11 09:44:13 +000057 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000058 "%s: Invalid time format: %s (%s:%d)",
59 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000060 return -1;
61 }
62
63 return 0;
64}
65
vlm39ba4c42004-09-22 16:06:28 +000066asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +000067UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +000068 int ilevel, enum xer_encoder_flags_e flags,
69 asn_app_consume_bytes_f *cb, void *app_key) {
70 OCTET_STRING_t st;
71
72 if(flags & XER_F_CANONICAL) {
73 char buf[32];
74 struct tm tm;
75 ssize_t ret;
76
77 errno = EPERM;
78 if(asn_UT2time((UTCTime_t *)sptr, &tm, 1) == -1
79 && errno != EPERM)
80 _ASN_ENCODE_FAILED;
81
82 ret = snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ",
83 tm.tm_year % 100, tm.tm_mon + 1, tm.tm_mday,
84 tm.tm_hour, tm.tm_min, tm.tm_sec);
85 assert(ret > 0 && ret < (int)sizeof(buf));
86
87 st.buf = (uint8_t *)buf;
88 st.size = ret;
89 sptr = &st;
90 }
91
vlm9de248e2004-10-20 15:50:55 +000092 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
vlm39ba4c42004-09-22 16:06:28 +000093 cb, app_key);
94}
95
vlmfa67ddc2004-06-03 03:38:44 +000096int
vlmef6355b2004-09-29 13:26:15 +000097UTCTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +000098 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +000099 const UTCTime_t *st = (const UTCTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000100
vlmb42843a2004-06-05 08:17:50 +0000101 (void)td; /* Unused argument */
102 (void)ilevel; /* Unused argument */
103
vlmfa67ddc2004-06-03 03:38:44 +0000104 if(st && st->buf) {
105 char buf[32];
106 struct tm tm;
107 int ret;
108
109 errno = EPERM;
vlmd0c608e2004-08-07 03:51:43 +0000110 if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
vlm6678cb12004-09-26 13:10:40 +0000111 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000112
113 ret = snprintf(buf, sizeof(buf),
vlmd0c608e2004-08-07 03:51:43 +0000114 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000115 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
116 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000117 assert(ret > 0 && ret < (int)sizeof(buf));
vlm6678cb12004-09-26 13:10:40 +0000118 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000119 } else {
vlm6678cb12004-09-26 13:10:40 +0000120 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000121 }
122}
123
124time_t
vlmd0c608e2004-08-07 03:51:43 +0000125asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
vlmfa67ddc2004-06-03 03:38:44 +0000126 char buf[17+2]; /* "AAMMJJhhmmss+hhmm" = 17, + 2 = 19 */
127 GeneralizedTime_t gt;
128
vlmb42843a2004-06-05 08:17:50 +0000129 if(!st || !st->buf
130 || st->size < 11 || st->size > ((int)sizeof(buf) - 2)) {
vlmfa67ddc2004-06-03 03:38:44 +0000131 errno = EINVAL;
132 return -1;
133 }
134
vlm1ff928d2004-08-11 08:10:13 +0000135 gt.buf = (unsigned char *)buf;
vlmfa67ddc2004-06-03 03:38:44 +0000136 gt.size = st->size + 2;
137 memcpy(gt.buf + 2, st->buf, st->size);
138 if(st->buf[0] > 0x35) {
139 /* 19xx */
140 gt.buf[0] = 0x31;
141 gt.buf[1] = 0x39;
142 } else {
143 /* 20xx */
144 gt.buf[0] = 0x32;
145 gt.buf[1] = 0x30;
146 }
147
vlmd0c608e2004-08-07 03:51:43 +0000148 return asn_GT2time(&gt, _tm, as_gmt);
vlmfa67ddc2004-06-03 03:38:44 +0000149}
vlmd0c608e2004-08-07 03:51:43 +0000150
151UTCTime_t *
152asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) {
153 GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut;
154
155 gt = asn_time2GT(gt, tm, force_gmt);
156 if(gt == 0) return 0;
157
158 assert(gt->size >= 2);
159 gt->size -= 2;
160 memmove(gt->buf, gt->buf + 2, gt->size + 1);
161
162 return (UTCTime_t *)gt;
163}
164