]> git.saurik.com Git - apple/libc.git/blob - net.subproj/nsap_addr.c
2c5ebaa005e026709b557be8dc479161c8b87307
[apple/libc.git] / net.subproj / nsap_addr.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 #if defined(LIBC_SCCS) && !defined(lint)
23 static char rcsid[] = "$Id: nsap_addr.c,v 1.3 2000/06/09 04:01:52 wsanchez Exp $";
24 #endif /* LIBC_SCCS and not lint */
25
26 #include <sys/param.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
30 #include <ctype.h>
31 #include <resolv.h>
32
33 #if !defined(isxdigit) /* XXX - could be a function */
34 static int
35 isxdigit(c)
36 register int c;
37 {
38 return ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'));
39 }
40 #endif
41
42 static char
43 xtob(c)
44 register int c;
45 {
46 return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
47 }
48
49 u_int
50 inet_nsap_addr(ascii, binary, maxlen)
51 const char *ascii;
52 u_char *binary;
53 int maxlen;
54 {
55 register u_char c, nib;
56 u_int len = 0;
57
58 while ((c = *ascii++) != '\0' && len < maxlen) {
59 if (c == '.' || c == '+' || c == '/')
60 continue;
61 if (!isascii(c))
62 return (0);
63 if (islower(c))
64 c = toupper(c);
65 if (isxdigit(c)) {
66 nib = xtob(c);
67 if (c = *ascii++) {
68 c = toupper(c);
69 if (isxdigit(c)) {
70 *binary++ = (nib << 4) | xtob(c);
71 len++;
72 } else
73 return (0);
74 }
75 else
76 return (0);
77 }
78 else
79 return (0);
80 }
81 return (len);
82 }
83
84 char *
85 inet_nsap_ntoa(binlen, binary, ascii)
86 int binlen;
87 register const u_char *binary;
88 register char *ascii;
89 {
90 register int nib;
91 int i;
92 static char tmpbuf[255*3];
93 char *start;
94
95 if (ascii)
96 start = ascii;
97 else {
98 ascii = tmpbuf;
99 start = tmpbuf;
100 }
101
102 if (binlen > 255)
103 binlen = 255;
104
105 for (i = 0; i < binlen; i++) {
106 nib = *binary >> 4;
107 *ascii++ = nib + (nib < 10 ? '0' : '7');
108 nib = *binary++ & 0x0f;
109 *ascii++ = nib + (nib < 10 ? '0' : '7');
110 if (((i % 2) == 0 && (i + 1) < binlen))
111 *ascii++ = '.';
112 }
113 *ascii = '\0';
114 return (start);
115 }