xref: /original-bsd/lib/libc/net/iso_addr.c (revision 4aa23bb2)
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.1 (Berkeley) 03/27/89";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <sys/types.h>
23 #include <netiso/iso.h>
24 
25 struct iso_addr *
26 iso_addr(addr)
27 register 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;
33 	register nibble_cnt = 0;
34 
35 	bzero((char *)&out_addr, sizeof(out_addr));
36 	while (*addr && (cp < cplim)) {
37 		byte <<= 8;
38 		if ((*addr >= '0') && (*addr <= '9')) {
39 			byte += *addr - '0';
40 		} else if ((*addr >= 'a') && (*addr <= 'f')) {
41 			byte += *addr - 'a' + 10;
42 		} else if ((*addr >= 'A') && (*addr <= 'F')) {
43 			byte += *addr - 'A' + 10;
44 		} else
45 			nibble_cnt++;
46 		addr++;
47 		nibble_cnt++;
48 		if (nibble_cnt > 1) {
49 			*cp++ = byte;
50 			nibble_cnt = byte = 0;
51 		}
52 	}
53 	if (nibble_cnt && (cp < cplim))
54 		*cp++ = byte;
55 	out_addr.isoa_len = cp - out_addr.isoa_genaddr;
56 	return (&out_addr);
57 }
58 
59 char *
60 iso_ntoa(isoa)
61 struct iso_addr *isoa;
62 {
63 	static char hexlist[] = "0123456789abcdef";
64 	static char obuf[64];
65 	register char *out = obuf;
66 	register int i;
67 	register u_char *in = (u_char *)isoa->isoa_genaddr;
68 	u_char *inlim = in + isoa->isoa_len;
69 
70 	out[1] = 0;
71 	while (in < inlim) {
72 		i = *in++;
73 		*out++ = '.';
74 		if (i > 0xf) {
75 			out[1] = hexlist[i & 0xf];
76 			i >>= 8;
77 			out[0] = hexlist[i];
78 			out += 2;
79 		} else
80 			*out++ = hexlist[i];
81 	}
82 	*out = 0;
83 	return(obuf + 1);
84 }
85