xref: /original-bsd/lib/libc/net/iso_addr.c (revision b3f911e0)
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 are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)iso_addr.c	5.2 (Berkeley) 09/25/89";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <sys/types.h>
23 #include <netiso/iso.h>
24 /* States*/
25 #define VIRGIN	0
26 #define GOTONE	1
27 #define GOTTWO	2
28 /* Inputs */
29 #define	DIGIT	(4*0)
30 #define	END	(4*1)
31 #define DELIM	(4*2)
32 
33 struct iso_addr *
34 iso_addr(addr)
35 register char *addr;
36 {
37 	static struct iso_addr out_addr;
38 	register char *cp = out_addr.isoa_genaddr;
39 	char *cplim = cp + sizeof(out_addr.isoa_genaddr);
40 	register int byte = 0, state = VIRGIN, new;
41 
42 	bzero((char *)&out_addr, sizeof(out_addr));
43 	do {
44 		if ((*addr >= '0') && (*addr <= '9')) {
45 			new = *addr - '0';
46 		} else if ((*addr >= 'a') && (*addr <= 'f')) {
47 			new = *addr - 'a' + 10;
48 		} else if ((*addr >= 'A') && (*addr <= 'F')) {
49 			new = *addr - 'A' + 10;
50 		} else if (*addr == 0)
51 			state |= END;
52 		else
53 			state |= DELIM;
54 		addr++;
55 		switch (state /* | INPUT */) {
56 		case GOTTWO | DIGIT:
57 			*cp++ = byte; /*FALLTHROUGH*/
58 		case VIRGIN | DIGIT:
59 			state = GOTONE; byte = new; continue;
60 		case GOTONE | DIGIT:
61 			state = GOTTWO; byte = new + (byte << 4); continue;
62 		default: /* | DELIM */
63 			state = VIRGIN; *cp++ = byte; byte = 0; continue;
64 		case GOTONE | END:
65 		case GOTTWO | END:
66 			*cp++ = byte; /* FALLTHROUGH */
67 		case VIRGIN | END:
68 			break;
69 		}
70 		break;
71 	} while (cp < cplim);
72 	out_addr.isoa_len = cp - out_addr.isoa_genaddr;
73 	return (&out_addr);
74 }
75 static char hexlist[] = "0123456789abcdef";
76 
77 char *
78 iso_ntoa(isoa)
79 struct iso_addr *isoa;
80 {
81 	static char obuf[64];
82 	register char *out = obuf;
83 	register int i;
84 	register u_char *in = (u_char *)isoa->isoa_genaddr;
85 	u_char *inlim = in + isoa->isoa_len;
86 
87 	out[1] = 0;
88 	while (in < inlim) {
89 		i = *in++;
90 		*out++ = '.';
91 		if (i > 0xf) {
92 			out[1] = hexlist[i & 0xf];
93 			i >>= 4;
94 			out[0] = hexlist[i];
95 			out += 2;
96 		} else
97 			*out++ = hexlist[i];
98 	}
99 	*out = 0;
100 	return(obuf + 1);
101 }
102