xref: /netbsd-src/lib/libc/net/iso_addr.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 /*static char *sccsid = "from: @(#)iso_addr.c	5.4 (Berkeley) 2/24/91";*/
36 static char *rcsid = "$Id: iso_addr.c,v 1.3 1993/08/26 00:46:08 jtc Exp $";
37 #endif /* LIBC_SCCS and not lint */
38 
39 #include <sys/types.h>
40 #include <netiso/iso.h>
41 #include <string.h>
42 
43 /* States*/
44 #define VIRGIN	0
45 #define GOTONE	1
46 #define GOTTWO	2
47 /* Inputs */
48 #define	DIGIT	(4*0)
49 #define	END	(4*1)
50 #define DELIM	(4*2)
51 
52 struct iso_addr *
53 iso_addr(addr)
54 	register const char *addr;
55 {
56 	static struct iso_addr out_addr;
57 	register char *cp = out_addr.isoa_genaddr;
58 	char *cplim = cp + sizeof(out_addr.isoa_genaddr);
59 	register int byte = 0, state = VIRGIN, new;
60 
61 	bzero((char *)&out_addr, sizeof(out_addr));
62 	do {
63 		if ((*addr >= '0') && (*addr <= '9')) {
64 			new = *addr - '0';
65 		} else if ((*addr >= 'a') && (*addr <= 'f')) {
66 			new = *addr - 'a' + 10;
67 		} else if ((*addr >= 'A') && (*addr <= 'F')) {
68 			new = *addr - 'A' + 10;
69 		} else if (*addr == 0)
70 			state |= END;
71 		else
72 			state |= DELIM;
73 		addr++;
74 		switch (state /* | INPUT */) {
75 		case GOTTWO | DIGIT:
76 			*cp++ = byte; /*FALLTHROUGH*/
77 		case VIRGIN | DIGIT:
78 			state = GOTONE; byte = new; continue;
79 		case GOTONE | DIGIT:
80 			state = GOTTWO; byte = new + (byte << 4); continue;
81 		default: /* | DELIM */
82 			state = VIRGIN; *cp++ = byte; byte = 0; continue;
83 		case GOTONE | END:
84 		case GOTTWO | END:
85 			*cp++ = byte; /* FALLTHROUGH */
86 		case VIRGIN | END:
87 			break;
88 		}
89 		break;
90 	} while (cp < cplim);
91 	out_addr.isoa_len = cp - out_addr.isoa_genaddr;
92 	return (&out_addr);
93 }
94 static char hexlist[] = "0123456789abcdef";
95 
96 char *
97 iso_ntoa(isoa)
98 	const struct iso_addr *isoa;
99 {
100 	static char obuf[64];
101 	register char *out = obuf;
102 	register int i;
103 	register u_char *in = (u_char *)isoa->isoa_genaddr;
104 	u_char *inlim = in + isoa->isoa_len;
105 
106 	out[1] = 0;
107 	while (in < inlim) {
108 		i = *in++;
109 		*out++ = '.';
110 		if (i > 0xf) {
111 			out[1] = hexlist[i & 0xf];
112 			i >>= 4;
113 			out[0] = hexlist[i];
114 			out += 2;
115 		} else
116 			*out++ = hexlist[i];
117 	}
118 	*out = 0;
119 	return(obuf + 1);
120 }
121