magic(3): Fix program name extraction from core dumps.
[dragonfly.git] / lib / libipx / ipx_addr.c
blob8abeaaaaec7851d35cb3b5badcf0e9a96a5f67b4
1 /*
2 * Copyright (c) 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * J.Q. Johnson.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * $FreeBSD: src/lib/libipx/ipx_addr.c,v 1.2.8.1 2001/03/05 06:21:40 kris Exp $
37 * $DragonFly: src/lib/libipx/ipx_addr.c,v 1.4 2005/08/04 17:27:09 drhodus Exp $
39 * @(#)ipx_addr.c
42 #include <sys/param.h>
43 #include <netipx/ipx.h>
44 #include <stdio.h>
45 #include <string.h>
47 static struct ipx_addr addr, zero_addr;
49 static void Field(), cvtbase();
51 struct ipx_addr
52 ipx_addr(const char *name)
54 char separator;
55 char *hostname, *socketname, *cp;
56 char buf[50];
58 (void)strncpy(buf, name, sizeof(buf) - 1);
59 buf[sizeof(buf) - 1] = '\0';
62 * First, figure out what he intends as a field separator.
63 * Despite the way this routine is written, the preferred
64 * form 2-272.AA001234H.01777, i.e. XDE standard.
65 * Great efforts are made to ensure backwards compatibility.
67 if ( (hostname = strchr(buf, '#')) )
68 separator = '#';
69 else {
70 hostname = strchr(buf, '.');
71 if ((cp = strchr(buf, ':')) &&
72 ((hostname && cp < hostname) || (hostname == 0))) {
73 hostname = cp;
74 separator = ':';
75 } else
76 separator = '.';
78 if (hostname)
79 *hostname++ = 0;
81 addr = zero_addr;
82 Field(buf, addr.x_net.c_net, 4);
83 if (hostname == 0)
84 return (addr); /* No separator means net only */
86 socketname = strchr(hostname, separator);
87 if (socketname) {
88 *socketname++ = 0;
89 Field(socketname, (u_char *)&addr.x_port, 2);
92 Field(hostname, addr.x_host.c_host, 6);
94 return (addr);
97 static void
98 Field(char *buf, u_char *out, int len)
100 char *bp = buf;
101 int i, ibase, base16 = 0, base10 = 0, clen = 0;
102 int hb[6], *hp;
103 char *fmt;
106 * first try 2-273#2-852-151-014#socket
108 if ((*buf != '-') &&
109 (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
110 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
111 cvtbase(1000L, 256, hb, i, out, len);
112 return;
115 * try form 8E1#0.0.AA.0.5E.E6#socket
117 if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
118 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
119 cvtbase(256L, 256, hb, i, out, len);
120 return;
123 * try form 8E1#0:0:AA:0:5E:E6#socket
125 if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
126 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
127 cvtbase(256L, 256, hb, i, out, len);
128 return;
131 * This is REALLY stretching it but there was a
132 * comma notation separating shorts -- definitely non-standard
134 if (1 < (i = sscanf(buf,"%x,%x,%x",
135 &hb[0], &hb[1], &hb[2]))) {
136 hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
137 hb[2] = htons(hb[2]);
138 cvtbase(65536L, 256, hb, i, out, len);
139 return;
142 /* Need to decide if base 10, 16 or 8 */
143 while (*bp) switch (*bp++) {
145 case '0': case '1': case '2': case '3': case '4': case '5':
146 case '6': case '7': case '-':
147 break;
149 case '8': case '9':
150 base10 = 1;
151 break;
153 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
154 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
155 base16 = 1;
156 break;
158 case 'x': case 'X':
159 *--bp = '0';
160 base16 = 1;
161 break;
163 case 'h': case 'H':
164 base16 = 1;
165 /* fall into */
167 default:
168 *--bp = 0; /* Ends Loop */
170 if (base16) {
171 fmt = "%3x";
172 ibase = 4096;
173 } else if (base10 == 0 && *buf == '0') {
174 fmt = "%3o";
175 ibase = 512;
176 } else {
177 fmt = "%3d";
178 ibase = 1000;
181 for (bp = buf; *bp++; ) clen++;
182 if (clen == 0) clen++;
183 if (clen > 18) clen = 18;
184 i = ((clen - 1) / 3) + 1;
185 bp = clen + buf - 3;
186 hp = hb + i - 1;
188 while (hp > hb) {
189 (void)sscanf(bp, fmt, hp);
190 bp[0] = 0;
191 hp--;
192 bp -= 3;
194 (void)sscanf(buf, fmt, hp);
195 cvtbase((long)ibase, 256, hb, i, out, len);
198 static void
199 cvtbase(long oldbase, int newbase, int input[], int inlen,
200 unsigned char result[], int reslen)
202 int d, e;
203 long sum;
205 e = 1;
206 while (e > 0 && reslen > 0) {
207 d = 0; e = 0; sum = 0;
208 /* long division: input=input/newbase */
209 while (d < inlen) {
210 sum = sum*oldbase + (long) input[d];
211 e += (sum > 0);
212 input[d++] = sum / newbase;
213 sum %= newbase;
215 result[--reslen] = sum; /* accumulate remainder */
217 for (d=0; d < reslen; d++)
218 result[d] = 0;