xref: /original-bsd/lib/libc/net/iso_addr.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)iso_addr.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <netiso/iso.h>
14 #include <string.h>
15 
16 /* States*/
17 #define VIRGIN	0
18 #define GOTONE	1
19 #define GOTTWO	2
20 /* Inputs */
21 #define	DIGIT	(4*0)
22 #define	END	(4*1)
23 #define DELIM	(4*2)
24 
25 struct iso_addr *
26 iso_addr(addr)
27 	register const char *addr;
28 {
29 	static struct iso_addr out_addr;
30 	register char *cp = out_addr.isoa_genaddr;
31 	char *cplim = cp + sizeof(out_addr.isoa_genaddr);
32 	register int byte = 0, state = VIRGIN, new;
33 
34 	bzero((char *)&out_addr, sizeof(out_addr));
35 	do {
36 		if ((*addr >= '0') && (*addr <= '9')) {
37 			new = *addr - '0';
38 		} else if ((*addr >= 'a') && (*addr <= 'f')) {
39 			new = *addr - 'a' + 10;
40 		} else if ((*addr >= 'A') && (*addr <= 'F')) {
41 			new = *addr - 'A' + 10;
42 		} else if (*addr == 0)
43 			state |= END;
44 		else
45 			state |= DELIM;
46 		addr++;
47 		switch (state /* | INPUT */) {
48 		case GOTTWO | DIGIT:
49 			*cp++ = byte; /*FALLTHROUGH*/
50 		case VIRGIN | DIGIT:
51 			state = GOTONE; byte = new; continue;
52 		case GOTONE | DIGIT:
53 			state = GOTTWO; byte = new + (byte << 4); continue;
54 		default: /* | DELIM */
55 			state = VIRGIN; *cp++ = byte; byte = 0; continue;
56 		case GOTONE | END:
57 		case GOTTWO | END:
58 			*cp++ = byte; /* FALLTHROUGH */
59 		case VIRGIN | END:
60 			break;
61 		}
62 		break;
63 	} while (cp < cplim);
64 	out_addr.isoa_len = cp - out_addr.isoa_genaddr;
65 	return (&out_addr);
66 }
67 static char hexlist[] = "0123456789abcdef";
68 
69 char *
70 iso_ntoa(isoa)
71 	const struct iso_addr *isoa;
72 {
73 	static char obuf[64];
74 	register char *out = obuf;
75 	register int i;
76 	register u_char *in = (u_char *)isoa->isoa_genaddr;
77 	u_char *inlim = in + isoa->isoa_len;
78 
79 	out[1] = 0;
80 	while (in < inlim) {
81 		i = *in++;
82 		*out++ = '.';
83 		if (i > 0xf) {
84 			out[1] = hexlist[i & 0xf];
85 			i >>= 4;
86 			out[0] = hexlist[i];
87 			out += 2;
88 		} else
89 			*out++ = hexlist[i];
90 	}
91 	*out = 0;
92 	return(obuf + 1);
93 }
94