Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / net / nsap_addr.c
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 #if defined(LIBC_SCCS) && !defined(lint)
19 static char rcsid[] = "$FreeBSD: src/lib/libc/net/nsap_addr.c,v 1.7 1999/08/28 00:00:15 peter Exp $";
20 #endif /* LIBC_SCCS and not lint */
21
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <arpa/nameser.h>
28 #include <ctype.h>
29 #include <resolv.h>
30
31 static char
32 xtob(c)
33         register int c;
34 {
35         return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
36 }
37
38 u_int
39 inet_nsap_addr(ascii, binary, maxlen)
40         const char *ascii;
41         u_char *binary;
42         int maxlen;
43 {
44         u_char c, nib;
45         u_int len = 0;
46
47         while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
48                 if (c == '.' || c == '+' || c == '/')
49                         continue;
50                 if (!isascii(c))
51                         return (0);
52                 if (islower(c))
53                         c = toupper(c);
54                 if (isxdigit(c)) {
55                         nib = xtob(c);
56                         c = *ascii++;
57                         if (c != '\0') {
58                                 c = toupper(c);
59                                 if (isxdigit(c)) {
60                                         *binary++ = (nib << 4) | xtob(c);
61                                         len++;
62                                 } else
63                                         return (0);
64                         }
65                         else
66                                 return (0);
67                 }
68                 else
69                         return (0);
70         }
71         return (len);
72 }
73
74 char *
75 inet_nsap_ntoa(binlen, binary, ascii)
76         int binlen;
77         register const u_char *binary;
78         register char *ascii;
79 {
80         register int nib;
81         int i;
82         static char tmpbuf[255*3];
83         char *start;
84
85         if (ascii)
86                 start = ascii;
87         else {
88                 ascii = tmpbuf;
89                 start = tmpbuf;
90         }
91
92         if (binlen > 255)
93                 binlen = 255;
94
95         for (i = 0; i < binlen; i++) {
96                 nib = *binary >> 4;
97                 *ascii++ = nib + (nib < 10 ? '0' : '7');
98                 nib = *binary++ & 0x0f;
99                 *ascii++ = nib + (nib < 10 ? '0' : '7');
100                 if (((i % 2) == 0 && (i + 1) < binlen))
101                         *ascii++ = '.';
102         }
103         *ascii = '\0';
104         return (start);
105 }
106
107 /*
108  * Weak aliases for applications that use certain private entry points,
109  * and fail to include <arpa/inet.h>.
110  */
111 #undef inet_nsap_addr
112 __weak_reference(__inet_nsap_addr, inet_nsap_addr);
113 #undef inet_nsap_ntoa
114 __weak_reference(__inet_nsap_ntoa, inet_nsap_ntoa);