xref: /freebsd/lib/libc/inet/nsap_addr.c (revision c6989859ae9388eeb46a24fe88f9b8d07101c710)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (c) 1996-1999 by Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid[] = "$Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp $";
22 #endif /* LIBC_SCCS and not lint */
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
25 
26 #include "port_before.h"
27 
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
34 
35 #include <ctype.h>
36 #include <resolv.h>
37 #include <resolv_mt.h>
38 
39 #include "port_after.h"
40 
41 static char
42 xtob(int c) {
43 	return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
44 }
45 
46 u_int
47 inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
48 	u_char c, nib;
49 	u_int len = 0;
50 
51 	if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
52 		return (0);
53 	ascii += 2;
54 
55 	while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
56 		if (c == '.' || c == '+' || c == '/')
57 			continue;
58 		if (!isascii(c))
59 			return (0);
60 		if (islower(c))
61 			c = toupper(c);
62 		if (isxdigit(c)) {
63 			nib = xtob(c);
64 			c = *ascii++;
65 			if (c != '\0') {
66 				c = toupper(c);
67 				if (isxdigit(c)) {
68 					*binary++ = (nib << 4) | xtob(c);
69 					len++;
70 				} else
71 					return (0);
72 			}
73 			else
74 				return (0);
75 		}
76 		else
77 			return (0);
78 	}
79 	return (len);
80 }
81 
82 char *
83 inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
84 	int nib;
85 	int i;
86 	char *tmpbuf = inet_nsap_ntoa_tmpbuf;
87 	char *start;
88 
89 	if (ascii)
90 		start = ascii;
91 	else {
92 		ascii = tmpbuf;
93 		start = tmpbuf;
94 	}
95 
96 	*ascii++ = '0';
97 	*ascii++ = 'x';
98 
99 	if (binlen > 255)
100 		binlen = 255;
101 
102 	for (i = 0; i < binlen; i++) {
103 		nib = *binary >> 4;
104 		*ascii++ = nib + (nib < 10 ? '0' : '7');
105 		nib = *binary++ & 0x0f;
106 		*ascii++ = nib + (nib < 10 ? '0' : '7');
107 		if (((i % 2) == 0 && (i + 1) < binlen))
108 			*ascii++ = '.';
109 	}
110 	*ascii = '\0';
111 	return (start);
112 }
113 
114 /*
115  * Weak aliases for applications that use certain private entry points,
116  * and fail to include <arpa/inet.h>.
117  */
118 #undef inet_nsap_addr
119 __weak_reference(__inet_nsap_addr, inet_nsap_addr);
120 #undef inet_nsap_ntoa
121 __weak_reference(__inet_nsap_ntoa, inet_nsap_ntoa);
122 
123 /*! \file */
124