1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include "mt.h"
#include "uucp.h"
#ifdef TLI
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
#include <sys/tiuser.h>
#include <ctype.h>
#define OCT 0
#define HEX 1
/*
* #include <nsaddr.h>
*/
#define toupper(c) (islower(c) ? _toupper(c) : (c))
#define todigit(c) ((int)((c) - '0')) /* char to digit */
#define toxdigit(c) ((isdigit(c))?todigit(c):(toupper(c)-(int)'A'+10))
#define isodigit(c) (isdigit(c) && ((c) != '9') && ((c) != '8'))
#define itoac(i) (((i) > 9) ? ((char)((i)-10) + 'A'):((char)(i) + '0'))
#define MASK(n) ((1 << (n)) - 1)
#define SBUFSIZE 128
/*
* #define TRUE 1
* #define FALSE 0
*/
/* local static functions */
static int dobase(char *, char *, int);
static void memcp(char *, char *, int);
static char *xfer(char *, char *, unsigned, unsigned);
/*
* stoa - convert string to address
*
* If a string begins in \o or \O, the following address is octal
* " " " " " \x or \X, the following address is hex
*
* If ok, return pointer to netbuf structure.
* A NULL is returned on any error(s).
*/
/* Return netbuf ptr if success */
static struct netbuf *
stoa(char *str, struct netbuf *addr)
{
int myadr; /* was netbuf struct allocated here ? */
static char *sbuf;
myadr = FALSE;
if (!str)
return (NULL);
while (*str && isspace(*str)) /* leading whites are OK */
++str;
if (!str || !*str) /* Nothing to convert */
return (NULL);
if (!addr) {
if ((addr = malloc(sizeof (struct netbuf))) == NULL)
return (NULL);
myadr = TRUE;
addr->buf = NULL;
addr->maxlen = 0;
addr->len = 0;
}
if (sbuf == NULL) {
sbuf = malloc(SBUFSIZE);
if (sbuf == NULL)
return (NULL);
}
/* Now process the address */
if (*str == '\\') {
++str;
switch (*str) {
case 'X': /* hex */
case 'x':
addr->len = dobase(++str, sbuf, HEX);
break;
case 'o': /* octal */
case 'O':
addr->len = dobase(++str, sbuf, OCT);
break;
default: /* error */
addr->len = 0;
break;
}
}
if (addr->len == 0) { /* Error in conversion */
if (myadr)
free(addr);
return (NULL);
}
if ((addr->buf = xfer(addr->buf, sbuf, addr->len, addr->maxlen)) ==
NULL)
return (NULL);
return (addr);
}
/*
* dobase : converts a hex or octal ASCII string
* to a binary address. Only HEX or OCT may be used
* for type.
* return length of binary string (in bytes), 0 if error.
* The binary result is placed at buf.
*/
static int
dobase(char *s, char *buf, int type)
{
int bp = SBUFSIZE - 1;
int shift = 0;
char *end;
for (end = s; *end && ((type == OCT) ? isodigit(*end) :
isxdigit(*end)); ++end)
;
/*
* any non-white, non-digits cause address to be rejected,
* other fields are ignored
*/
if ((*s == 0) || (end == s) || (!isspace(*end) && *end)) {
(void) fprintf(stderr,
"dobase: Illegal trailer on address string\n");
buf[0] = '\0';
return (0);
}
--end;
buf[bp] = '\0';
while (bp > 0 && end >= s) {
buf[bp] |= toxdigit(*end) << shift;
if (type == OCT) {
if (shift > 5) {
buf[--bp] = (todigit(*end) >> (8 - shift))
& MASK(shift-5);
}
if ((shift = (shift + 3) % 8) == 0)
buf[--bp] = 0;
} else /* hex */
if ((shift = (shift) ? 0 : 4) == 0)
buf[--bp] = 0;
--end;
}
if (bp == 0) {
(void) fprintf(stderr, "stoa: dobase: number to long\n");
return (0);
}
/* need to catch end case to avoid extra 0's in front */
if (!shift)
bp++;
memcp(buf, &buf[bp], (SBUFSIZE - bp));
return (SBUFSIZE - bp);
}
static void
memcp(char *d, char *s, int n)
{
while (n--)
*d++ = *s++;
}
/*
* transfer block to a given destination or allocate one of the
* right size
* if max = 0 : ignore max
*/
static char *
xfer(char *dest, char *src, unsigned len, unsigned max)
{
if (max && dest && max < len) { /* No room */
(void) fprintf(stderr, "xfer: destination not long enough\n");
return (NULL);
}
if (!dest)
if ((dest = malloc(len)) == NULL) {
(void) fprintf(stderr, "xfer: malloc failed\n");
return (NULL);
}
(void) memcpy(dest, src, (size_t)len);
return (dest);
}
#endif /* TLI */
|