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