acpi.4: Add some missing references.
[dragonfly.git] / contrib / bind-9.3 / lib / dns / ttl.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  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 WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: ttl.c,v 1.21.12.5 2004/03/08 09:04:32 marka Exp $ */
19
20 #include <config.h>
21
22 #include <ctype.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include <isc/buffer.h>
28 #include <isc/parseint.h>
29 #include <isc/print.h>
30 #include <isc/region.h>
31 #include <isc/string.h>
32 #include <isc/util.h>
33
34 #include <dns/result.h>
35 #include <dns/ttl.h>
36
37 #define RETERR(x) do { \
38         isc_result_t _r = (x); \
39         if (_r != ISC_R_SUCCESS) \
40                 return (_r); \
41         } while (0)
42
43
44 static isc_result_t bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl);
45
46 /*
47  * Helper for dns_ttl_totext().
48  */
49 static isc_result_t
50 ttlfmt(unsigned int t, const char *s, isc_boolean_t verbose,
51        isc_boolean_t space, isc_buffer_t *target)
52 {
53         char tmp[60];
54         size_t len;
55         isc_region_t region;
56
57         if (verbose)
58                 len = snprintf(tmp, sizeof(tmp), "%s%u %s%s",
59                                space ? " " : "",
60                                t, s,
61                                t == 1 ? "" : "s");
62         else
63                 len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
64
65         INSIST(len + 1 <= sizeof(tmp));
66         isc_buffer_availableregion(target, &region);
67         if (len > region.length)
68                 return (ISC_R_NOSPACE);
69         memcpy(region.base, tmp, len);
70         isc_buffer_add(target, len);
71
72         return (ISC_R_SUCCESS);
73 }
74
75 /*
76  * Derived from bind8 ns_format_ttl().
77  */
78 isc_result_t
79 dns_ttl_totext(isc_uint32_t src, isc_boolean_t verbose, isc_buffer_t *target) {
80         unsigned secs, mins, hours, days, weeks, x;
81
82         secs = src % 60;   src /= 60;
83         mins = src % 60;   src /= 60;
84         hours = src % 24;  src /= 24;
85         days = src % 7;    src /= 7;
86         weeks = src;       src = 0;
87
88         x = 0;
89         if (weeks != 0) {
90                 RETERR(ttlfmt(weeks, "week", verbose, ISC_TF(x > 0), target));
91                 x++;
92         }
93         if (days != 0) {
94                 RETERR(ttlfmt(days, "day", verbose, ISC_TF(x > 0), target));
95                 x++;
96         }
97         if (hours != 0) {
98                 RETERR(ttlfmt(hours, "hour", verbose, ISC_TF(x > 0), target));
99                 x++;
100         }
101         if (mins != 0) {
102                 RETERR(ttlfmt(mins, "minute", verbose, ISC_TF(x > 0), target));
103                 x++;
104         }
105         if (secs != 0 ||
106             (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
107                 RETERR(ttlfmt(secs, "second", verbose, ISC_TF(x > 0), target));
108                 x++;
109         }
110         INSIST (x > 0);
111         /*
112          * If only a single unit letter is printed, print it
113          * in upper case. (Why?  Because BIND 8 does that.
114          * Presumably it has a reason.)
115          */
116         if (x == 1 && !verbose) {
117                 isc_region_t region;
118                 /*
119                  * The unit letter is the last character in the
120                  * used region of the buffer.
121                  *
122                  * toupper() does not need its argument to be masked of cast
123                  * here because region.base is type unsigned char *.
124                  */
125                 isc_buffer_usedregion(target, &region);
126                 region.base[region.length - 1] =
127                         toupper(region.base[region.length - 1]);
128         }
129         return (ISC_R_SUCCESS);
130 }
131
132 isc_result_t
133 dns_counter_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
134         return (bind_ttl(source, ttl));
135 }
136
137 isc_result_t
138 dns_ttl_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
139         isc_result_t result;
140
141         result = bind_ttl(source, ttl);
142         if (result != ISC_R_SUCCESS)
143                 result = DNS_R_BADTTL;
144         return (result);
145 }
146
147 static isc_result_t
148 bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl) {
149         isc_uint32_t tmp = 0;
150         isc_uint32_t n;
151         char *s;
152         char buf[64];
153         char nbuf[64]; /* Number buffer */
154
155         /*
156          * Copy the buffer as it may not be NULL terminated.
157          * No legal counter / ttl is longer that 63 characters.
158          */
159         if (source->length > sizeof(buf) - 1)
160                 return (DNS_R_SYNTAX);
161         strncpy(buf, source->base, source->length);
162         buf[source->length] = '\0';
163         s = buf;
164
165         do {
166                 isc_result_t result;
167
168                 char *np = nbuf;
169                 while (*s != '\0' && isdigit((unsigned char)*s))
170                         *np++ = *s++;
171                 *np++ = '\0';
172                 INSIST(np - nbuf <= (int)sizeof(nbuf));
173                 result = isc_parse_uint32(&n, nbuf, 10);
174                 if (result != ISC_R_SUCCESS)
175                         return (DNS_R_SYNTAX);
176                 switch (*s) {
177                 case 'w':
178                 case 'W':
179                         tmp += n * 7 * 24 * 3600;
180                         s++;
181                         break;
182                 case 'd':
183                 case 'D':
184                         tmp += n * 24 * 3600;
185                         s++;
186                         break;
187                 case 'h':
188                 case 'H':
189                         tmp += n * 3600;
190                         s++;
191                         break;
192                 case 'm':
193                 case 'M':
194                         tmp += n * 60;
195                         s++;
196                         break;
197                 case 's':
198                 case 'S':
199                         tmp += n;
200                         s++;
201                         break;
202                 case '\0':
203                         /* Plain number? */
204                         if (tmp != 0)
205                                 return (DNS_R_SYNTAX);
206                         tmp = n;
207                         break;
208                 default:
209                         return (DNS_R_SYNTAX);
210                 }
211         } while (*s != '\0');
212         *ttl = tmp;
213         return (ISC_R_SUCCESS);
214 }