flag fields are u_int's
[unix-history] / usr / src / lib / libc / net / iso_addr.c
CommitLineData
5f4a344d
KS
1/*
2 * Copyright (c) 1989 Regents of the University of California.
3 * All rights reserved.
4 *
269a7923 5 * %sccs.include.redist.c%
5f4a344d
KS
6 */
7
8#if defined(LIBC_SCCS) && !defined(lint)
24fac7d8 9static char sccsid[] = "@(#)iso_addr.c 5.4 (Berkeley) %G%";
5f4a344d
KS
10#endif /* LIBC_SCCS and not lint */
11
12#include <sys/types.h>
13#include <netiso/iso.h>
24fac7d8
KB
14#include <string.h>
15
afb6a25b
KS
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)
5f4a344d
KS
24
25struct iso_addr *
26iso_addr(addr)
24fac7d8 27 register const char *addr;
5f4a344d
KS
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);
afb6a25b 32 register int byte = 0, state = VIRGIN, new;
5f4a344d
KS
33
34 bzero((char *)&out_addr, sizeof(out_addr));
afb6a25b 35 do {
5f4a344d 36 if ((*addr >= '0') && (*addr <= '9')) {
afb6a25b 37 new = *addr - '0';
5f4a344d 38 } else if ((*addr >= 'a') && (*addr <= 'f')) {
afb6a25b 39 new = *addr - 'a' + 10;
5f4a344d 40 } else if ((*addr >= 'A') && (*addr <= 'F')) {
afb6a25b
KS
41 new = *addr - 'A' + 10;
42 } else if (*addr == 0)
43 state |= END;
44 else
45 state |= DELIM;
5f4a344d 46 addr++;
afb6a25b
KS
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;
5f4a344d 61 }
afb6a25b
KS
62 break;
63 } while (cp < cplim);
5f4a344d
KS
64 out_addr.isoa_len = cp - out_addr.isoa_genaddr;
65 return (&out_addr);
66}
afb6a25b 67static char hexlist[] = "0123456789abcdef";
5f4a344d
KS
68
69char *
70iso_ntoa(isoa)
24fac7d8 71 const struct iso_addr *isoa;
5f4a344d 72{
5f4a344d
KS
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];
afb6a25b 85 i >>= 4;
5f4a344d
KS
86 out[0] = hexlist[i];
87 out += 2;
88 } else
89 *out++ = hexlist[i];
90 }
91 *out = 0;
92 return(obuf + 1);
93}