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