1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "port_before.h"
19 
20 #include <arpa/nameser.h>
21 
22 #include <ctype.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "port_after.h"
28 
29 #ifdef SPRINTF_CHAR
30 # define SPRINTF(x) strlen(sprintf/**/x)
31 #else
32 # define SPRINTF(x) ((size_t)sprintf x)
33 #endif
34 
35 /* Forward. */
36 
37 static int	fmt1(int t, char s, char **buf, size_t *buflen);
38 
39 /* Macros. */
40 
41 #define T(x) if ((x) < 0) return (-1); else (void)NULL
42 
43 /* Public. */
44 
45 int
ns_format_ttl(u_long src,char * dst,size_t dstlen)46 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
47 	char *odst = dst;
48 	int secs, mins, hours, days, weeks, x;
49 	char *p;
50 
51 	secs = src % 60;   src /= 60;
52 	mins = src % 60;   src /= 60;
53 	hours = src % 24;  src /= 24;
54 	days = src % 7;    src /= 7;
55 	weeks = src;       src = 0;
56 
57 	x = 0;
58 	if (weeks) {
59 		T(fmt1(weeks, 'W', &dst, &dstlen));
60 		x++;
61 	}
62 	if (days) {
63 		T(fmt1(days, 'D', &dst, &dstlen));
64 		x++;
65 	}
66 	if (hours) {
67 		T(fmt1(hours, 'H', &dst, &dstlen));
68 		x++;
69 	}
70 	if (mins) {
71 		T(fmt1(mins, 'M', &dst, &dstlen));
72 		x++;
73 	}
74 	if (secs || !(weeks || days || hours || mins)) {
75 		T(fmt1(secs, 'S', &dst, &dstlen));
76 		x++;
77 	}
78 
79 	if (x > 1) {
80 		int ch;
81 
82 		for (p = odst; (ch = *p) != '\0'; p++)
83 			if (isascii(ch) && isupper(ch))
84 				*p = tolower(ch);
85 	}
86 
87 	return (dst - odst);
88 }
89 
90 int
ns_parse_ttl(const char * src,u_long * dst)91 ns_parse_ttl(const char *src, u_long *dst) {
92 	u_long ttl, tmp;
93 	int ch, digits, dirty;
94 
95 	ttl = 0;
96 	tmp = 0;
97 	digits = 0;
98 	dirty = 0;
99 	while ((ch = *src++) != '\0') {
100 		if (!isascii(ch) || !isprint(ch))
101 			goto einval;
102 		if (isdigit(ch)) {
103 			tmp *= 10;
104 			tmp += (ch - '0');
105 			digits++;
106 			continue;
107 		}
108 		if (digits == 0)
109 			goto einval;
110 		if (islower(ch))
111 			ch = toupper(ch);
112 		switch (ch) {
113 		case 'W':  tmp *= 7;
114 			/* FALLTHROUGH */
115 		case 'D':  tmp *= 24;
116 			/* FALLTHROUGH */
117 		case 'H':  tmp *= 60;
118 			/* FALLTHROUGH */
119 		case 'M':  tmp *= 60;
120 			/* FALLTHROUGH */
121 		case 'S':  break;
122 		default:   goto einval;
123 		}
124 		ttl += tmp;
125 		tmp = 0;
126 		digits = 0;
127 		dirty = 1;
128 	}
129 	if (digits > 0) {
130 		if (dirty)
131 			goto einval;
132 		else
133 			ttl += tmp;
134 	} else if (!dirty)
135 		goto einval;
136 	*dst = ttl;
137 	return (0);
138 
139  einval:
140 	errno = EINVAL;
141 	return (-1);
142 }
143 
144 /* Private. */
145 
146 static int
fmt1(int t,char s,char ** buf,size_t * buflen)147 fmt1(int t, char s, char **buf, size_t *buflen) {
148 	char tmp[50];
149 	size_t len;
150 
151 	len = SPRINTF((tmp, "%d%c", t, s));
152 	if (len + 1 > *buflen)
153 		return (-1);
154 	strcpy(*buf, tmp);
155 	*buf += len;
156 	*buflen -= len;
157 	return (0);
158 }
159 
160 /*! \file */
161