Merge pull request #3186 from FernetMenta/aefixes
[vuplus_xbmc] / xbmc / cores / DllLoader / exports / emu_socket / inet_pton.c
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 #ifdef _WIN32
19 #include <windows.h>
20 #else
21 #include <rpc/types.h>
22 #endif
23 #include <string.h>
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #ifdef __STDC__
30 #define __weak_reference(sym,alias) \
31     __asm__(".weak " #alias);   \
32     __asm__(".equ "  #alias ", " #sym)
33 #else
34 #define __weak_reference(sym,alias) \
35     __asm__(".weak alias");     \
36     __asm__(".equ alias, sym")
37 #endif  /* __STDC__ */
38
39 /*%
40  * WARNING: Don't even consider trying to compile this on a system where
41  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
42  */
43
44 static int  inet_pton4(const char *src, u_char *dst);
45 static int  inet_pton6(const char *src, u_char *dst);
46
47 /* int
48  * inet_pton(af, src, dst)
49  *  convert from presentation format (which usually means ASCII printable)
50  *  to network format (which is usually some kind of binary format).
51  * return:
52  *  1 if the address was valid for the specified address family
53  *  0 if the address wasn't valid (`dst' is untouched in this case)
54  *  -1 if some other error occurred (`dst' is untouched in this case, too)
55  * author:
56  *  Paul Vixie, 1996.
57  */
58 int
59 inet_pton(int af, const char * __restrict src, void * __restrict dst)
60 {
61     switch (af) {
62     case AF_INET:
63         return (inet_pton4(src, (u_char*)dst));
64     case AF_INET6:
65         return (inet_pton6(src, (u_char*)dst));
66     default:
67         return (-1);
68     }
69     /* NOTREACHED */
70 }
71
72 /* int
73  * inet_pton4(src, dst)
74  *  like inet_aton() but without all the hexadecimal and shorthand.
75  * return:
76  *  1 if `src' is a valid dotted quad, else 0.
77  * notice:
78  *  does not touch `dst' unless it's returning 1.
79  * author:
80  *  Paul Vixie, 1996.
81  */
82 static int
83 inet_pton4(const char *src, u_char *dst)
84 {
85     static const char digits[] = "0123456789";
86     int saw_digit, octets, ch;
87 #define NS_INADDRSZ 4
88     u_char tmp[NS_INADDRSZ], *tp;
89
90     saw_digit = 0;
91     octets = 0;
92     *(tp = tmp) = 0;
93     while ((ch = *src++) != '\0') {
94         const char *pch;
95
96         if ((pch = strchr(digits, ch)) != NULL) {
97             u_int c_new = *tp * 10 + (pch - digits);
98
99             if (saw_digit && *tp == 0)
100                 return (0);
101             if (c_new > 255)
102                 return (0);
103             *tp = c_new;
104             if (!saw_digit) {
105                 if (++octets > 4)
106                     return (0);
107                 saw_digit = 1;
108             }
109         } else if (ch == '.' && saw_digit) {
110             if (octets == 4)
111                 return (0);
112             *++tp = 0;
113             saw_digit = 0;
114         } else
115             return (0);
116     }
117     if (octets < 4)
118         return (0);
119     memcpy(dst, tmp, NS_INADDRSZ);
120     return (1);
121 }
122
123 /* int
124  * inet_pton6(src, dst)
125  *  convert presentation level address to network order binary form.
126  * return:
127  *  1 if `src' is a valid [RFC1884 2.2] address, else 0.
128  * notice:
129  *  (1) does not touch `dst' unless it's returning 1.
130  *  (2) :: in a full address is silently ignored.
131  * credit:
132  *  inspired by Mark Andrews.
133  * author:
134  *  Paul Vixie, 1996.
135  */
136 static int
137 inet_pton6(const char *src, u_char *dst)
138 {
139     static const char xdigits_l[] = "0123456789abcdef",
140               xdigits_u[] = "0123456789ABCDEF";
141 #define NS_IN6ADDRSZ    16
142 #define NS_INT16SZ  2
143     u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
144     const char *curtok;
145     int ch, seen_xdigits;
146     u_int val;
147
148     memset((tp = tmp), '\0', NS_IN6ADDRSZ);
149     endp = tp + NS_IN6ADDRSZ;
150     colonp = NULL;
151     /* Leading :: requires some special handling. */
152     if (*src == ':')
153         if (*++src != ':')
154             return (0);
155     curtok = src;
156     seen_xdigits = 0;
157     val = 0;
158     while ((ch = *src++) != '\0') {
159       const char *xdigits, *pch;
160
161         if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
162             pch = strchr((xdigits = xdigits_u), ch);
163         if (pch != NULL) {
164             val <<= 4;
165             val |= (pch - xdigits);
166             if (++seen_xdigits > 4)
167                 return (0);
168             continue;
169         }
170         if (ch == ':') {
171             curtok = src;
172             if (!seen_xdigits) {
173                 if (colonp)
174                     return (0);
175                 colonp = tp;
176                 continue;
177             } else if (*src == '\0') {
178                 return (0);
179             }
180             if (tp + NS_INT16SZ > endp)
181                 return (0);
182             *tp++ = (u_char) (val >> 8) & 0xff;
183             *tp++ = (u_char) val & 0xff;
184             seen_xdigits = 0;
185             val = 0;
186             continue;
187         }
188         if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
189             inet_pton4(curtok, tp) > 0) {
190             tp += NS_INADDRSZ;
191             seen_xdigits = 0;
192             break;  /*%< '\\0' was seen by inet_pton4(). */
193         }
194         return (0);
195     }
196     if (seen_xdigits) {
197         if (tp + NS_INT16SZ > endp)
198             return (0);
199         *tp++ = (u_char) (val >> 8) & 0xff;
200         *tp++ = (u_char) val & 0xff;
201     }
202     if (colonp != NULL) {
203         /*
204          * Since some memmove()'s erroneously fail to handle
205          * overlapping regions, we'll do the shift by hand.
206          */
207         const int n = tp - colonp;
208         int i;
209
210         if (tp == endp)
211             return (0);
212         for (i = 1; i <= n; i++) {
213             endp[- i] = colonp[n - i];
214             colonp[n - i] = 0;
215         }
216         tp = endp;
217     }
218     if (tp != endp)
219         return (0);
220     memcpy(dst, tmp, NS_IN6ADDRSZ);
221     return (1);
222 }
223
224 /*
225  * Weak aliases for applications that use certain private entry points,
226  * and fail to include <arpa/inet.h>.
227  */
228 #ifndef _WIN32
229 #undef inet_pton
230 __weak_reference(__inet_pton, inet_pton);
231 #endif
232
233 #ifdef __cplusplus
234 }
235 #endif