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