powerpc: Add optimized version of [l]lrintf
[glibc.git] / resolv / inet_pton.c
blob68f0fa54020e9351b8f1ab6d2572a4843d7ac259
1 /*
2 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
18 #include <sys/param.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <arpa/nameser.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <errno.h>
28 static int inet_pton4 (const char *src, unsigned char *dst);
29 static int inet_pton6 (const char *src, unsigned char *dst);
31 /* Convert from presentation format (which usually means ASCII printable)
32 * to network format (which is usually some kind of binary format).
34 * return:
35 * 1 if the address was valid for the specified address family
36 * 0 if the address wasn't valid (`dst' is untouched in this case)
37 * -1 if some other error occurred (`dst' is untouched in this case, too)
38 * author:
39 * Paul Vixie, 1996.
41 int
42 __inet_pton (int af, const char *src, void *dst)
44 switch (af)
46 case AF_INET:
47 return inet_pton4 (src, dst);
48 case AF_INET6:
49 return inet_pton6 (src, dst);
50 default:
51 __set_errno (EAFNOSUPPORT);
52 return -1;
55 libc_hidden_def (__inet_pton)
56 weak_alias (__inet_pton, inet_pton)
57 libc_hidden_weak (inet_pton)
59 /* Like inet_atonbut without all the hexadecimal, octal and shorthand.
61 * return:
62 * 1 if `src' is a valid dotted quad, else 0.
63 * notice:
64 * does not touch `dst' unless it's returning 1.
65 * author:
66 * Paul Vixie, 1996.
68 static int
69 inet_pton4 (const char *src, unsigned char *dst)
71 int saw_digit, octets, ch;
72 unsigned char tmp[NS_INADDRSZ], *tp;
74 saw_digit = 0;
75 octets = 0;
76 *(tp = tmp) = 0;
77 while ((ch = *src++) != '\0')
79 if (ch >= '0' && ch <= '9')
81 unsigned int new = *tp * 10 + (ch - '0');
83 if (saw_digit && *tp == 0)
84 return 0;
85 if (new > 255)
86 return 0;
87 *tp = new;
88 if (! saw_digit)
90 if (++octets > 4)
91 return 0;
92 saw_digit = 1;
95 else if (ch == '.' && saw_digit)
97 if (octets == 4)
98 return 0;
99 *++tp = 0;
100 saw_digit = 0;
102 else
103 return 0;
105 if (octets < 4)
106 return 0;
107 memcpy (dst, tmp, NS_INADDRSZ);
108 return 1;
111 /* Cconvert presentation level address to network order binary form.
113 * return:
114 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
115 * notice:
116 * (1) does not touch `dst' unless it's returning 1.
117 * (2) :: in a full address is silently ignored.
118 * credit:
119 * inspired by Mark Andrews.
120 * author:
121 * Paul Vixie, 1996.
123 static int
124 inet_pton6 (const char *src, unsigned char *dst)
126 static const char xdigits[] = "0123456789abcdef";
127 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
128 const char *curtok;
129 int ch, saw_xdigit;
130 unsigned int val;
132 tp = memset (tmp, '\0', NS_IN6ADDRSZ);
133 endp = tp + NS_IN6ADDRSZ;
134 colonp = NULL;
135 /* Leading :: requires some special handling. */
136 if (*src == ':')
137 if (*++src != ':')
138 return 0;
139 curtok = src;
140 saw_xdigit = 0;
141 val = 0;
142 while ((ch = tolower (*src++)) != '\0')
144 const char *pch;
146 pch = strchr (xdigits, ch);
147 if (pch != NULL)
149 val <<= 4;
150 val |= (pch - xdigits);
151 if (val > 0xffff)
152 return 0;
153 saw_xdigit = 1;
154 continue;
156 if (ch == ':')
158 curtok = src;
159 if (!saw_xdigit)
161 if (colonp)
162 return 0;
163 colonp = tp;
164 continue;
166 else if (*src == '\0')
168 return 0;
170 if (tp + NS_INT16SZ > endp)
171 return 0;
172 *tp++ = (unsigned char) (val >> 8) & 0xff;
173 *tp++ = (unsigned char) val & 0xff;
174 saw_xdigit = 0;
175 val = 0;
176 continue;
178 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
179 inet_pton4 (curtok, tp) > 0)
181 tp += NS_INADDRSZ;
182 saw_xdigit = 0;
183 break; /* '\0' was seen by inet_pton4. */
185 return 0;
187 if (saw_xdigit)
189 if (tp + NS_INT16SZ > endp)
190 return 0;
191 *tp++ = (unsigned char) (val >> 8) & 0xff;
192 *tp++ = (unsigned char) val & 0xff;
194 if (colonp != NULL)
197 * Since some memmove's erroneously fail to handle
198 * overlapping regions, we'll do the shift by hand.
200 const int n = tp - colonp;
201 int i;
203 if (tp == endp)
204 return 0;
205 for (i = 1; i <= n; i++)
207 endp[- i] = colonp[n - i];
208 colonp[n - i] = 0;
210 tp = endp;
212 if (tp != endp)
213 return 0;
214 memcpy (dst, tmp, NS_IN6ADDRSZ);
215 return 1;