xref: /netbsd-src/lib/libc/inet/nsap_addr.c (revision 59a755a40f74ace0246c95b46337b1ee2e5c6b68)
1*59a755a4Schristos /*	$NetBSD: nsap_addr.c,v 1.6 2009/04/12 17:07:17 christos Exp $	*/
239e7bb71Schristos 
339e7bb71Schristos /*
439e7bb71Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
539e7bb71Schristos  * Copyright (c) 1996-1999 by Internet Software Consortium.
639e7bb71Schristos  *
739e7bb71Schristos  * Permission to use, copy, modify, and distribute this software for any
839e7bb71Schristos  * purpose with or without fee is hereby granted, provided that the above
939e7bb71Schristos  * copyright notice and this permission notice appear in all copies.
1039e7bb71Schristos  *
1139e7bb71Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
1239e7bb71Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1339e7bb71Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
1439e7bb71Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1539e7bb71Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1639e7bb71Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
1739e7bb71Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1839e7bb71Schristos  */
1939e7bb71Schristos 
20df0952c6Schristos #include <sys/cdefs.h>
2139e7bb71Schristos #if defined(LIBC_SCCS) && !defined(lint)
22df0952c6Schristos #if 0
233873655bSchristos static const char rcsid[] = "Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp";
24df0952c6Schristos #else
25*59a755a4Schristos __RCSID("$NetBSD: nsap_addr.c,v 1.6 2009/04/12 17:07:17 christos Exp $");
26df0952c6Schristos #endif
2739e7bb71Schristos #endif /* LIBC_SCCS and not lint */
2839e7bb71Schristos 
2939e7bb71Schristos #include "port_before.h"
3039e7bb71Schristos 
31df0952c6Schristos #include "namespace.h"
3239e7bb71Schristos #include <sys/types.h>
3339e7bb71Schristos #include <sys/param.h>
3439e7bb71Schristos #include <sys/socket.h>
3539e7bb71Schristos 
3639e7bb71Schristos #include <netinet/in.h>
3739e7bb71Schristos #include <arpa/inet.h>
3839e7bb71Schristos #include <arpa/nameser.h>
3939e7bb71Schristos 
40df0952c6Schristos #include <assert.h>
4139e7bb71Schristos #include <ctype.h>
4239e7bb71Schristos #include <resolv.h>
43d73eb73dSchristos #include <resolv_mt.h>
4439e7bb71Schristos 
4539e7bb71Schristos #include "port_after.h"
4639e7bb71Schristos 
47df0952c6Schristos #ifdef __weak_alias
__weak_alias(inet_nsap_addr,_inet_nsap_addr)48df0952c6Schristos __weak_alias(inet_nsap_addr,_inet_nsap_addr)
49df0952c6Schristos __weak_alias(inet_nsap_ntoa,_inet_nsap_ntoa)
50df0952c6Schristos #endif
51df0952c6Schristos 
5239e7bb71Schristos static char
5339e7bb71Schristos xtob(int c) {
5439e7bb71Schristos 	return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
5539e7bb71Schristos }
5639e7bb71Schristos 
5739e7bb71Schristos u_int
inet_nsap_addr(const char * ascii,u_char * binary,int maxlen)5839e7bb71Schristos inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
5939e7bb71Schristos 	u_char c, nib;
6039e7bb71Schristos 	u_int len = 0;
6139e7bb71Schristos 
62df0952c6Schristos 	_DIAGASSERT(ascii != NULL);
63df0952c6Schristos 	_DIAGASSERT(binary != NULL);
64df0952c6Schristos 
6539e7bb71Schristos 	if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
6639e7bb71Schristos 		return (0);
6739e7bb71Schristos 	ascii += 2;
6839e7bb71Schristos 
6939e7bb71Schristos 	while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
7039e7bb71Schristos 		if (c == '.' || c == '+' || c == '/')
7139e7bb71Schristos 			continue;
7239e7bb71Schristos 		if (!isascii(c))
7339e7bb71Schristos 			return (0);
7439e7bb71Schristos 		if (islower(c))
7539e7bb71Schristos 			c = toupper(c);
7639e7bb71Schristos 		if (isxdigit(c)) {
7739e7bb71Schristos 			nib = xtob(c);
7839e7bb71Schristos 			c = *ascii++;
7939e7bb71Schristos 			if (c != '\0') {
8039e7bb71Schristos 				c = toupper(c);
8139e7bb71Schristos 				if (isxdigit(c)) {
8239e7bb71Schristos 					*binary++ = (nib << 4) | xtob(c);
8339e7bb71Schristos 					len++;
8439e7bb71Schristos 				} else
8539e7bb71Schristos 					return (0);
8639e7bb71Schristos 			}
8739e7bb71Schristos 			else
8839e7bb71Schristos 				return (0);
8939e7bb71Schristos 		}
9039e7bb71Schristos 		else
9139e7bb71Schristos 			return (0);
9239e7bb71Schristos 	}
9339e7bb71Schristos 	return (len);
9439e7bb71Schristos }
9539e7bb71Schristos 
9639e7bb71Schristos char *
inet_nsap_ntoa(int binlen,const u_char * binary,char * ascii)9739e7bb71Schristos inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
9839e7bb71Schristos 	int nib;
9939e7bb71Schristos 	int i;
100d73eb73dSchristos 	char *tmpbuf = inet_nsap_ntoa_tmpbuf;
10139e7bb71Schristos 	char *start;
10239e7bb71Schristos 
103df0952c6Schristos 	_DIAGASSERT(binary != NULL);
104df0952c6Schristos 
10539e7bb71Schristos 	if (ascii)
10639e7bb71Schristos 		start = ascii;
10739e7bb71Schristos 	else {
10839e7bb71Schristos 		ascii = tmpbuf;
10939e7bb71Schristos 		start = tmpbuf;
11039e7bb71Schristos 	}
11139e7bb71Schristos 
11239e7bb71Schristos 	*ascii++ = '0';
11339e7bb71Schristos 	*ascii++ = 'x';
11439e7bb71Schristos 
11539e7bb71Schristos 	if (binlen > 255)
11639e7bb71Schristos 		binlen = 255;
11739e7bb71Schristos 
11839e7bb71Schristos 	for (i = 0; i < binlen; i++) {
119df0952c6Schristos 		nib = (u_int32_t)*binary >> 4;
12039e7bb71Schristos 		*ascii++ = nib + (nib < 10 ? '0' : '7');
12139e7bb71Schristos 		nib = *binary++ & 0x0f;
12239e7bb71Schristos 		*ascii++ = nib + (nib < 10 ? '0' : '7');
12339e7bb71Schristos 		if (((i % 2) == 0 && (i + 1) < binlen))
12439e7bb71Schristos 			*ascii++ = '.';
12539e7bb71Schristos 	}
12639e7bb71Schristos 	*ascii = '\0';
12739e7bb71Schristos 	return (start);
12839e7bb71Schristos }
129d73eb73dSchristos 
130d73eb73dSchristos /*! \file */
131