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