]> git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/nsap_addr.c
Libinfo-330.3.tar.gz
[apple/libinfo.git] / dns.subproj / nsap_addr.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 #if defined(LIBC_SCCS) && !defined(lint)
25 static char rcsid[] = "$Id: nsap_addr.c,v 1.3 2003/02/18 17:29:24 majka Exp $";
26 #endif /* LIBC_SCCS and not lint */
27
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <ctype.h>
32
33 #include "nameser8_compat.h"
34 #include "resolv8_compat.h"
35 #include "portability.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[255*3];
97 char *start;
98
99 if (ascii)
100 start = ascii;
101 else {
102 ascii = tmpbuf;
103 start = tmpbuf;
104 }
105
106 if (binlen > 255)
107 binlen = 255;
108
109 for (i = 0; i < binlen; i++) {
110 nib = *binary >> 4;
111 *ascii++ = nib + (nib < 10 ? '0' : '7');
112 nib = *binary++ & 0x0f;
113 *ascii++ = nib + (nib < 10 ? '0' : '7');
114 if (((i % 2) == 0 && (i + 1) < binlen))
115 *ascii++ = '.';
116 }
117 *ascii = '\0';
118 return (start);
119 }