]> git.saurik.com Git - apple/libc.git/blob - net/nsap_addr.c
fd3cc99e8ba796ee1620dc01ec2f17dd21d5d839
[apple/libc.git] / net / 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.4 2002/05/17 21:00:31 bbraun 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 #include <stdlib.h>
33
34 #if !defined(isxdigit) /* XXX - could be a function */
35 static int
36 isxdigit(c)
37 register int c;
38 {
39 return ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'));
40 }
41 #endif
42
43 static char
44 xtob(c)
45 register int c;
46 {
47 return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
48 }
49
50 u_int
51 inet_nsap_addr(ascii, binary, maxlen)
52 const char *ascii;
53 u_char *binary;
54 int maxlen;
55 {
56 register u_char c, nib;
57 u_int len = 0;
58
59 while ((c = *ascii++) != '\0' && len < maxlen) {
60 if (c == '.' || c == '+' || c == '/')
61 continue;
62 if (!isascii(c))
63 return (0);
64 if (islower(c))
65 c = toupper(c);
66 if (isxdigit(c)) {
67 nib = xtob(c);
68 if (c = *ascii++) {
69 c = toupper(c);
70 if (isxdigit(c)) {
71 *binary++ = (nib << 4) | xtob(c);
72 len++;
73 } else
74 return (0);
75 }
76 else
77 return (0);
78 }
79 else
80 return (0);
81 }
82 return (len);
83 }
84
85 char *
86 inet_nsap_ntoa(binlen, binary, ascii)
87 int binlen;
88 register const u_char *binary;
89 register char *ascii;
90 {
91 register int nib;
92 int i;
93 static char *tmpbuf = NULL;
94 char *start;
95
96 if( tmpbuf == NULL ) {
97 tmpbuf = malloc(255*3);
98 if( tmpbuf == NULL )
99 return NULL;
100 }
101
102 if (ascii)
103 start = ascii;
104 else {
105 ascii = tmpbuf;
106 start = tmpbuf;
107 }
108
109 if (binlen > 255)
110 binlen = 255;
111
112 for (i = 0; i < binlen; i++) {
113 nib = *binary >> 4;
114 *ascii++ = nib + (nib < 10 ? '0' : '7');
115 nib = *binary++ & 0x0f;
116 *ascii++ = nib + (nib < 10 ? '0' : '7');
117 if (((i % 2) == 0 && (i + 1) < binlen))
118 *ascii++ = '.';
119 }
120 *ascii = '\0';
121 return (start);
122 }