]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/ns_ttl.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / BsdSocketLib / ns_ttl.c
1 /*
2 * Copyright (c) 1996 by Internet Software Consortium.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15 * SOFTWARE.
16 */
17
18 /*
19 * Portions copyright (c) 1999, 2000
20 * Intel Corporation.
21 * All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 *
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 *
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 *
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 *
37 * This product includes software developed by Intel Corporation and
38 * its contributors.
39 *
40 * 4. Neither the name of Intel Corporation or its contributors may be
41 * used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
48 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
55 *
56 */
57
58 #ifndef lint
59 static char rcsid[] = "$Id: ns_ttl.c,v 1.1.1.1 2003/11/19 01:51:34 kyu3 Exp $";
60 #endif
61
62 /* Import. */
63
64 #include <arpa/nameser.h>
65
66 #include <ctype.h>
67 #include <errno.h>
68 #include <stdio.h>
69 #include <string.h>
70
71 #define SPRINTF(x) ((size_t)sprintf x)
72
73 /* Forward. */
74
75 static int fmt1(int t, char s, char **buf, size_t *buflen);
76
77 /* Macros. */
78
79 #define T(x) if ((x) < 0) return (-1); else (void)NULL
80
81 /* Public. */
82
83 int
84 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
85 char *odst = dst;
86 int secs, mins, hours, days, weeks, x;
87 char *p;
88
89 secs = (int)(src % 60); src /= 60;
90 mins = (int)(src % 60); src /= 60;
91 hours = (int)(src % 24); src /= 24;
92 days = (int)(src % 7); src /= 7;
93 weeks = (int)src; src = 0;
94
95 x = 0;
96 if (weeks) {
97 T(fmt1(weeks, 'W', &dst, &dstlen));
98 x++;
99 }
100 if (days) {
101 T(fmt1(days, 'D', &dst, &dstlen));
102 x++;
103 }
104 if (hours) {
105 T(fmt1(hours, 'H', &dst, &dstlen));
106 x++;
107 }
108 if (mins) {
109 T(fmt1(mins, 'M', &dst, &dstlen));
110 x++;
111 }
112 if (secs || !(weeks || days || hours || mins)) {
113 T(fmt1(secs, 'S', &dst, &dstlen));
114 x++;
115 }
116
117 if (x > 1) {
118 int ch;
119
120 for (p = odst; (ch = *p) != '\0'; p++)
121 if (isascii(ch) && isupper(ch))
122 *p = (char)( tolower(ch));
123 }
124
125 return ((int)(dst - odst));
126 }
127
128 int
129 ns_parse_ttl(const char *src, u_long *dst) {
130 u_long ttl, tmp;
131 int ch, digits, dirty;
132
133 ttl = 0;
134 tmp = 0;
135 digits = 0;
136 dirty = 0;
137 while ((ch = *src++) != '\0') {
138 if (!isascii(ch) || !isprint(ch))
139 goto einval;
140 if (isdigit(ch)) {
141 tmp *= 10;
142 tmp += (ch - '0');
143 digits++;
144 continue;
145 }
146 if (digits == 0)
147 goto einval;
148 if (islower(ch))
149 ch = toupper(ch);
150 switch (ch) {
151 case 'W': tmp *= 7;
152 case 'D': tmp *= 24;
153 case 'H': tmp *= 60;
154 case 'M': tmp *= 60;
155 case 'S': break;
156 default: goto einval;
157 }
158 ttl += tmp;
159 tmp = 0;
160 digits = 0;
161 dirty = 1;
162 }
163 if (digits > 0) {
164 if (dirty)
165 goto einval;
166 else
167 ttl += tmp;
168 }
169 *dst = ttl;
170 return (0);
171
172 einval:
173 errno = EINVAL;
174 return (-1);
175 }
176
177 /* Private. */
178
179 static int
180 fmt1(int t, char s, char **buf, size_t *buflen) {
181 char tmp[50];
182 size_t len;
183
184 len = SPRINTF((tmp, "%d%c", t, s));
185 if (len + 1 > *buflen)
186 return (-1);
187 strcpy(*buf, tmp);
188 *buf += len;
189 *buflen -= len;
190 return (0);
191 }