]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/SysCall/inet_pton.c
1 /* Copyright (c) 1996 by Internet Software Consortium.
3 * Permission to use, copy, modify, and distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
8 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
9 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
10 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
11 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
12 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 * Portions copyright (c) 1999, 2000
20 * All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
33 * 3. All advertising materials mentioning features or use of this software
34 * must display the following acknowledgement:
36 * This product includes software developed by Intel Corporation and
39 * 4. Neither the name of Intel Corporation or its contributors may be
40 * used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
44 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
47 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
53 * THE POSSIBILITY OF SUCH DAMAGE.
57 #if defined (LIBC_SCCS) && !defined (lint)
58 static char rcsid
[] = "$Id: inet_pton.c,v 1.1.1.1 2003/11/19 01:51:30 kyu3 Exp $";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/param.h>
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
71 * WARNING: Don't even consider trying to compile this on a system where
72 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
88 * inet_pton(af, src, dst)
89 * convert from presentation format (which usually means ASCII printable)
90 * to network format (which is usually some kind of binary format).
92 * 1 if the address was valid for the specified address family
93 * 0 if the address wasn't valid (`dst' is untouched in this case)
94 * -1 if some other error occurred (`dst' is untouched in this case, too)
107 return (inet_pton4 (src
, dst
));
109 return (inet_pton6 (src
, dst
));
111 errno
= EAFNOSUPPORT
;
119 * inet_pton4(src, dst)
120 * like inet_aton() but without all the hexadecimal and shorthand.
122 * 1 if `src' is a valid dotted quad, else 0.
124 * does not touch `dst' unless it's returning 1.
134 static const char digits
[] = "0123456789";
135 int saw_digit
, octets
, ch
;
136 u_char tmp
[NS_INADDRSZ
], *tp
;
141 while ((ch
= *src
++) != '\0') {
144 if ((pch
= strchr (digits
, ch
)) != NULL
) {
145 u_int
new = *tp
* 10 + (u_int
)(pch
- digits
);
159 } else if ((ch
== '.') && saw_digit
) {
175 memcpy (dst
, tmp
, NS_INADDRSZ
);
180 * inet_pton6(src, dst)
181 * convert presentation level address to network order binary form.
183 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
185 * (1) does not touch `dst' unless it's returning 1.
186 * (2) :: in a full address is silently ignored.
188 * inspired by Mark Andrews.
198 static const char xdigits_l
[] = "0123456789abcdef",
199 xdigits_u
[] = "0123456789ABCDEF";
200 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
201 const char *xdigits
, *curtok
;
205 memset ((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
206 endp
= tp
+ NS_IN6ADDRSZ
;
208 /* Leading :: requires some special handling. */
218 while ((ch
= *src
++) != '\0') {
221 if ((pch
= strchr ((xdigits
= xdigits_l
), ch
)) == NULL
) {
222 pch
= strchr ((xdigits
= xdigits_u
), ch
);
227 val
|= (pch
- xdigits
);
247 if (tp
+ NS_INT16SZ
> endp
) {
251 *tp
++ = (u_char
)(val
>> 8) & 0xff;
252 *tp
++ = (u_char
)val
& 0xff;
258 if ((ch
== '.') && ((tp
+ NS_INADDRSZ
) <= endp
) &&
259 (inet_pton4 (curtok
, tp
) > 0))
263 break; /* '\0' was seen by inet_pton4(). */
270 if (tp
+ NS_INT16SZ
> endp
) {
274 *tp
++ = (u_char
)(val
>> 8) & 0xff;
275 *tp
++ = (u_char
)val
& 0xff;
278 if (colonp
!= NULL
) {
280 * Since some memmove()'s erroneously fail to handle
281 * overlapping regions, we'll do the shift by hand.
283 const int n
= (int)(tp
- colonp
);
286 for (i
= 1; i
<= n
; i
++) {
287 endp
[-i
] = colonp
[n
- i
];
298 memcpy (dst
, tmp
, NS_IN6ADDRSZ
);