]> git.saurik.com Git - apple/libc.git/blame - net/nsap_addr-fbsd.c
Libc-498.1.7.tar.gz
[apple/libc.git] / net / nsap_addr-fbsd.c
CommitLineData
224c7076
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 "xlocale_private.h"
22
23#include <sys/types.h>
24#include <sys/param.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
28#include <arpa/nameser.h>
29#include <ctype.h>
30#include <resolv.h>
31#include <stdlib.h>
32
33static char
34xtob(c)
35 int c;
36{
37 return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
38}
39
40u_int
41inet_nsap_addr(ascii, binary, maxlen)
42 const char *ascii;
43 u_char *binary;
44 int maxlen;
45{
46 u_char c, nib;
47 u_int len = 0;
48 locale_t loc = __current_locale();
49
50 while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
51 if (c == '.' || c == '+' || c == '/')
52 continue;
53 if (!isascii(c))
54 return (0);
55 if (islower_l(c, loc))
56 c = toupper_l(c, loc);
57 if (isxdigit_l(c, loc)) {
58 nib = xtob(c);
59 c = *ascii++;
60 if (c != '\0') {
61 c = toupper_l(c, loc);
62 if (isxdigit_l(c, loc)) {
63 *binary++ = (nib << 4) | xtob(c);
64 len++;
65 } else
66 return (0);
67 }
68 else
69 return (0);
70 }
71 else
72 return (0);
73 }
74 return (len);
75}
76
77char *
78inet_nsap_ntoa(binlen, binary, ascii)
79 int binlen;
80 const u_char *binary;
81 char *ascii;
82{
83 int nib;
84 int i;
85 static char *tmpbuf = NULL;
86 char *start;
87
88 if (tmpbuf == NULL) {
89 tmpbuf = malloc(255*3);
90 if (tmpbuf == NULL)
91 return NULL;
92 }
93 if (ascii)
94 start = ascii;
95 else {
96 ascii = tmpbuf;
97 start = tmpbuf;
98 }
99
100 if (binlen > 255)
101 binlen = 255;
102
103 for (i = 0; i < binlen; i++) {
104 nib = *binary >> 4;
105 *ascii++ = nib + (nib < 10 ? '0' : '7');
106 nib = *binary++ & 0x0f;
107 *ascii++ = nib + (nib < 10 ? '0' : '7');
108 if (((i % 2) == 0 && (i + 1) < binlen))
109 *ascii++ = '.';
110 }
111 *ascii = '\0';
112 return (start);
113}
114
115/*
116 * Weak aliases for applications that use certain private entry points,
117 * and fail to include <arpa/inet.h>.
118 */
119#undef inet_nsap_addr
120__weak_reference(__inet_nsap_addr, inet_nsap_addr);
121#undef inet_nsap_ntoa
122__weak_reference(__inet_nsap_ntoa, inet_nsap_ntoa);