1156952Sume/*
2156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3156952Sume * Copyright (c) 1996-1999 by Internet Software Consortium.
4156952Sume *
5156952Sume * Permission to use, copy, modify, and distribute this software for any
6156952Sume * purpose with or without fee is hereby granted, provided that the above
7156952Sume * copyright notice and this permission notice appear in all copies.
8156952Sume *
9156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11156952Sume * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16156952Sume */
17156952Sume
18156952Sume#if defined(LIBC_SCCS) && !defined(lint)
19170244Sumestatic const char rcsid[] = "$Id: nsap_addr.c,v 1.3.18.2 2005/07/28 07:38:08 marka Exp $";
20156952Sume#endif /* LIBC_SCCS and not lint */
21156956Sume#include <sys/cdefs.h>
22156956Sume__FBSDID("$FreeBSD$");
23156952Sume
24156952Sume#include "port_before.h"
25156952Sume
26156952Sume#include <sys/types.h>
27156952Sume#include <sys/param.h>
28156952Sume#include <sys/socket.h>
29156952Sume
30156952Sume#include <netinet/in.h>
31156952Sume#include <arpa/inet.h>
32156952Sume#include <arpa/nameser.h>
33156952Sume
34156952Sume#include <ctype.h>
35156952Sume#include <resolv.h>
36156952Sume#include <resolv_mt.h>
37156952Sume
38156952Sume#include "port_after.h"
39156952Sume
40156952Sumestatic char
41156952Sumextob(int c) {
42156952Sume	return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
43156952Sume}
44156952Sume
45156952Sumeu_int
46156952Sumeinet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
47156952Sume	u_char c, nib;
48156952Sume	u_int len = 0;
49156952Sume
50156952Sume	if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
51156952Sume		return (0);
52156952Sume	ascii += 2;
53156952Sume
54156952Sume	while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
55156952Sume		if (c == '.' || c == '+' || c == '/')
56156952Sume			continue;
57156952Sume		if (!isascii(c))
58156952Sume			return (0);
59156952Sume		if (islower(c))
60156952Sume			c = toupper(c);
61156952Sume		if (isxdigit(c)) {
62156952Sume			nib = xtob(c);
63156952Sume			c = *ascii++;
64156952Sume			if (c != '\0') {
65156952Sume				c = toupper(c);
66156952Sume				if (isxdigit(c)) {
67156952Sume					*binary++ = (nib << 4) | xtob(c);
68156952Sume					len++;
69156952Sume				} else
70156952Sume					return (0);
71156952Sume			}
72156952Sume			else
73156952Sume				return (0);
74156952Sume		}
75156952Sume		else
76156952Sume			return (0);
77156952Sume	}
78156952Sume	return (len);
79156952Sume}
80156952Sume
81156952Sumechar *
82156952Sumeinet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
83156952Sume	int nib;
84156952Sume	int i;
85156952Sume	char *tmpbuf = inet_nsap_ntoa_tmpbuf;
86156952Sume	char *start;
87156952Sume
88156952Sume	if (ascii)
89156952Sume		start = ascii;
90156952Sume	else {
91156952Sume		ascii = tmpbuf;
92156952Sume		start = tmpbuf;
93156952Sume	}
94156952Sume
95156952Sume	*ascii++ = '0';
96156952Sume	*ascii++ = 'x';
97156952Sume
98156952Sume	if (binlen > 255)
99156952Sume		binlen = 255;
100156952Sume
101156952Sume	for (i = 0; i < binlen; i++) {
102156952Sume		nib = *binary >> 4;
103156952Sume		*ascii++ = nib + (nib < 10 ? '0' : '7');
104156952Sume		nib = *binary++ & 0x0f;
105156952Sume		*ascii++ = nib + (nib < 10 ? '0' : '7');
106156952Sume		if (((i % 2) == 0 && (i + 1) < binlen))
107156952Sume			*ascii++ = '.';
108156952Sume	}
109156952Sume	*ascii = '\0';
110156952Sume	return (start);
111156952Sume}
112156956Sume
113156956Sume/*
114156956Sume * Weak aliases for applications that use certain private entry points,
115156956Sume * and fail to include <arpa/inet.h>.
116156956Sume */
117156956Sume#undef inet_nsap_addr
118156956Sume__weak_reference(__inet_nsap_addr, inet_nsap_addr);
119156956Sume#undef inet_nsap_ntoa
120156956Sume__weak_reference(__inet_nsap_ntoa, inet_nsap_ntoa);
121170244Sume
122170244Sume/*! \file */
123