1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file */
13 
14 #include <ctype.h>
15 #include <inttypes.h>
16 #include <stdio.h>
17 #include <time.h>
18 
19 #include <isc/print.h>
20 #include <isc/region.h>
21 #include <isc/result.h>
22 #include <isc/serial.h>
23 #include <isc/stdtime.h>
24 #include <isc/string.h> /* Required for HP/UX (and others?) */
25 #include <isc/util.h>
26 
27 #include <dns/time.h>
28 
29 static const int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
30 
31 isc_result_t
dns_time64_totext(int64_t t,isc_buffer_t * target)32 dns_time64_totext(int64_t t, isc_buffer_t *target) {
33 	struct tm tm;
34 	char buf[sizeof("!!!!!!YYYY!!!!!!!!MM!!!!!!!!DD!!!!!!!!HH!!!!!!!!MM!!!!"
35 			"!!!!SS")];
36 	int secs;
37 	unsigned int l;
38 	isc_region_t region;
39 
40 /*
41  * Warning. Do NOT use arguments with side effects with these macros.
42  */
43 #define is_leap(y)	 ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
44 #define year_secs(y)	 ((is_leap(y) ? 366 : 365) * 86400)
45 #define month_secs(m, y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0)) * 86400)
46 
47 	tm.tm_year = 70;
48 	while (t < 0) {
49 		if (tm.tm_year == 0) {
50 			return (ISC_R_RANGE);
51 		}
52 		tm.tm_year--;
53 		secs = year_secs(tm.tm_year + 1900);
54 		t += secs;
55 	}
56 	while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
57 		t -= secs;
58 		tm.tm_year++;
59 		if (tm.tm_year + 1900 > 9999) {
60 			return (ISC_R_RANGE);
61 		}
62 	}
63 	tm.tm_mon = 0;
64 	while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
65 		t -= secs;
66 		tm.tm_mon++;
67 	}
68 	tm.tm_mday = 1;
69 	while (86400 <= t) {
70 		t -= 86400;
71 		tm.tm_mday++;
72 	}
73 	tm.tm_hour = 0;
74 	while (3600 <= t) {
75 		t -= 3600;
76 		tm.tm_hour++;
77 	}
78 	tm.tm_min = 0;
79 	while (60 <= t) {
80 		t -= 60;
81 		tm.tm_min++;
82 	}
83 	tm.tm_sec = (int)t;
84 	/* yyyy  mm  dd  HH  MM  SS */
85 	snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
86 		 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
87 		 tm.tm_min, tm.tm_sec);
88 
89 	isc_buffer_availableregion(target, &region);
90 	l = strlen(buf);
91 
92 	if (l > region.length) {
93 		return (ISC_R_NOSPACE);
94 	}
95 
96 	memmove(region.base, buf, l);
97 	isc_buffer_add(target, l);
98 	return (ISC_R_SUCCESS);
99 }
100 
101 int64_t
dns_time64_from32(uint32_t value)102 dns_time64_from32(uint32_t value) {
103 	isc_stdtime_t now;
104 	int64_t start;
105 	int64_t t;
106 
107 	/*
108 	 * Adjust the time to the closest epoch.  This should be changed
109 	 * to use a 64-bit counterpart to isc_stdtime_get() if one ever
110 	 * is defined, but even the current code is good until the year
111 	 * 2106.
112 	 */
113 	isc_stdtime_get(&now);
114 	start = (int64_t)now;
115 	if (isc_serial_gt(value, now)) {
116 		t = start + (value - now);
117 	} else {
118 		t = start - (now - value);
119 	}
120 
121 	return (t);
122 }
123 
124 isc_result_t
dns_time32_totext(uint32_t value,isc_buffer_t * target)125 dns_time32_totext(uint32_t value, isc_buffer_t *target) {
126 	return (dns_time64_totext(dns_time64_from32(value), target));
127 }
128 
129 isc_result_t
dns_time64_fromtext(const char * source,int64_t * target)130 dns_time64_fromtext(const char *source, int64_t *target) {
131 	int year, month, day, hour, minute, second;
132 	int64_t value;
133 	int secs;
134 	int i;
135 
136 #define RANGE(min, max, value)                      \
137 	do {                                        \
138 		if (value < (min) || value > (max)) \
139 			return ((ISC_R_RANGE));     \
140 	} while (0)
141 
142 	if (strlen(source) != 14U) {
143 		return (DNS_R_SYNTAX);
144 	}
145 	/*
146 	 * Confirm the source only consists digits.  sscanf() allows some
147 	 * minor exceptions.
148 	 */
149 	for (i = 0; i < 14; i++) {
150 		if (!isdigit((unsigned char)source[i])) {
151 			return (DNS_R_SYNTAX);
152 		}
153 	}
154 	if (sscanf(source, "%4d%2d%2d%2d%2d%2d", &year, &month, &day, &hour,
155 		   &minute, &second) != 6)
156 	{
157 		return (DNS_R_SYNTAX);
158 	}
159 
160 	RANGE(0, 9999, year);
161 	RANGE(1, 12, month);
162 	RANGE(1, days[month - 1] + ((month == 2 && is_leap(year)) ? 1 : 0),
163 	      day);
164 #ifdef __COVERITY__
165 	/*
166 	 * Use a simplified range to silence Coverity warning (in
167 	 * arithmetic with day below).
168 	 */
169 	RANGE(1, 31, day);
170 #endif /* __COVERITY__ */
171 
172 	RANGE(0, 23, hour);
173 	RANGE(0, 59, minute);
174 	RANGE(0, 60, second); /* 60 == leap second. */
175 
176 	/*
177 	 * Calculate seconds from epoch.
178 	 * Note: this uses a idealized calendar.
179 	 */
180 	value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
181 	for (i = 0; i < (month - 1); i++) {
182 		value += days[i] * 86400;
183 	}
184 	if (is_leap(year) && month > 2) {
185 		value += 86400;
186 	}
187 	if (year < 1970) {
188 		for (i = 1969; i >= year; i--) {
189 			secs = (is_leap(i) ? 366 : 365) * 86400;
190 			value -= secs;
191 		}
192 	} else {
193 		for (i = 1970; i < year; i++) {
194 			secs = (is_leap(i) ? 366 : 365) * 86400;
195 			value += secs;
196 		}
197 	}
198 
199 	*target = value;
200 	return (ISC_R_SUCCESS);
201 }
202 
203 isc_result_t
dns_time32_fromtext(const char * source,uint32_t * target)204 dns_time32_fromtext(const char *source, uint32_t *target) {
205 	int64_t value64;
206 	isc_result_t result;
207 	result = dns_time64_fromtext(source, &value64);
208 	if (result != ISC_R_SUCCESS) {
209 		return (result);
210 	}
211 	*target = (uint32_t)value64;
212 
213 	return (ISC_R_SUCCESS);
214 }
215