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