xref: /illumos-gate/usr/src/lib/libnsl/dial/stoa.c (revision 61961e0f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*61961e0fSrobinson 
23*61961e0fSrobinson /*
24*61961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*61961e0fSrobinson  * Use is subject to license terms.
26*61961e0fSrobinson  */
27*61961e0fSrobinson 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
31*61961e0fSrobinson #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include	"uucp.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef TLI
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <memory.h>
407c478bd9Sstevel@tonic-gate #include <malloc.h>
417c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
427c478bd9Sstevel@tonic-gate #include <ctype.h>
43*61961e0fSrobinson #define	OCT	0
44*61961e0fSrobinson #define	HEX	1
45*61961e0fSrobinson /*
46*61961e0fSrobinson  * #include <nsaddr.h>
47*61961e0fSrobinson  */
487c478bd9Sstevel@tonic-gate #define	toupper(c)	(islower(c) ? _toupper(c) : (c))
497c478bd9Sstevel@tonic-gate #define	todigit(c)	((int)((c) - '0'))	/* char to digit */
507c478bd9Sstevel@tonic-gate #define	toxdigit(c)	((isdigit(c))?todigit(c):(toupper(c)-(int)'A'+10))
517c478bd9Sstevel@tonic-gate #define	isodigit(c)	(isdigit(c) && ((c) != '9') && ((c) != '8'))
52*61961e0fSrobinson #define	itoac(i)	(((i) > 9) ? ((char)((i)-10) + 'A'):((char)(i) + '0'))
537c478bd9Sstevel@tonic-gate #define	MASK(n)		((1 << (n)) - 1)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	SBUFSIZE	128
567c478bd9Sstevel@tonic-gate 
57*61961e0fSrobinson /*
58*61961e0fSrobinson  * #define	TRUE	1
59*61961e0fSrobinson  * #define	FALSE	0
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*	local static functions	*/
63*61961e0fSrobinson static int dobase(char *, char *, int);
64*61961e0fSrobinson static void memcp(char *, char *, int);
65*61961e0fSrobinson static char *xfer(char *, char *, unsigned, unsigned);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
68*61961e0fSrobinson  *	stoa - convert string to address
69*61961e0fSrobinson  *
70*61961e0fSrobinson  *	If a string begins in \o or \O, the following address is octal
71*61961e0fSrobinson  *	"  "   "       "    " \x or \X, the following address is hex
72*61961e0fSrobinson  *
73*61961e0fSrobinson  *	If ok, return pointer to netbuf structure.
74*61961e0fSrobinson  *	A  NULL is returned on any error(s).
75*61961e0fSrobinson  */
767c478bd9Sstevel@tonic-gate 
77*61961e0fSrobinson /* Return netbuf ptr if success */
78*61961e0fSrobinson static struct netbuf *
79*61961e0fSrobinson stoa(char *str, struct netbuf *addr)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	int	myadr;		/* was netbuf struct allocated here ? */
827c478bd9Sstevel@tonic-gate 	static	char *sbuf;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	myadr = FALSE;
857c478bd9Sstevel@tonic-gate 
86*61961e0fSrobinson 	if (!str)
877c478bd9Sstevel@tonic-gate 		return (NULL);
887c478bd9Sstevel@tonic-gate 	while (*str && isspace(*str))	/* leading whites are OK */
897c478bd9Sstevel@tonic-gate 		++str;
907c478bd9Sstevel@tonic-gate 
91*61961e0fSrobinson 	if (!str || !*str)	/* Nothing to convert */
927c478bd9Sstevel@tonic-gate 		return (NULL);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (!addr) {
95*61961e0fSrobinson 		if ((addr = malloc(sizeof (struct netbuf))) == NULL)
967c478bd9Sstevel@tonic-gate 			return (NULL);
97*61961e0fSrobinson 
987c478bd9Sstevel@tonic-gate 		myadr = TRUE;
997c478bd9Sstevel@tonic-gate 		addr->buf = NULL;
1007c478bd9Sstevel@tonic-gate 		addr->maxlen = 0;
1017c478bd9Sstevel@tonic-gate 		addr->len = 0;
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (sbuf == NULL) {
105*61961e0fSrobinson 		sbuf = malloc(SBUFSIZE);
1067c478bd9Sstevel@tonic-gate 		if (sbuf == NULL)
1077c478bd9Sstevel@tonic-gate 			return (NULL);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* Now process the address */
1117c478bd9Sstevel@tonic-gate 	if (*str == '\\') {
1127c478bd9Sstevel@tonic-gate 		++str;
1137c478bd9Sstevel@tonic-gate 		switch (*str) {
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		case 'X':	/* hex */
1167c478bd9Sstevel@tonic-gate 		case 'x':
1177c478bd9Sstevel@tonic-gate 			addr->len = dobase(++str, sbuf, HEX);
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		case 'o':	/* octal */
1217c478bd9Sstevel@tonic-gate 		case 'O':
1227c478bd9Sstevel@tonic-gate 			addr->len = dobase(++str, sbuf, OCT);
1237c478bd9Sstevel@tonic-gate 			break;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		default:	/* error */
1267c478bd9Sstevel@tonic-gate 			addr->len = 0;
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (addr->len == 0) {	/* Error in conversion */
1327c478bd9Sstevel@tonic-gate 		if (myadr)
1337c478bd9Sstevel@tonic-gate 			free(addr);
1347c478bd9Sstevel@tonic-gate 		return (NULL);
1357c478bd9Sstevel@tonic-gate 	}
136*61961e0fSrobinson 	if ((addr->buf = xfer(addr->buf, sbuf, addr->len, addr->maxlen)) ==
137*61961e0fSrobinson 									NULL)
1387c478bd9Sstevel@tonic-gate 		return (NULL);
139*61961e0fSrobinson 	return (addr);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
143*61961e0fSrobinson  *	dobase :	converts a hex or octal ASCII string
144*61961e0fSrobinson  *		to a binary address. Only HEX or OCT may be used
145*61961e0fSrobinson  *		for type.
146*61961e0fSrobinson  *	return length of binary string (in bytes), 0 if error.
147*61961e0fSrobinson  *	The binary result is placed at buf.
148*61961e0fSrobinson  */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static int
151*61961e0fSrobinson dobase(char *s, char *buf, int type)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	int	bp = SBUFSIZE - 1;
1547c478bd9Sstevel@tonic-gate 	int	shift = 0;
1557c478bd9Sstevel@tonic-gate 	char	*end;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	for (end = s; *end && ((type == OCT) ? isodigit(*end) :
158*61961e0fSrobinson 							isxdigit(*end)); ++end)
159*61961e0fSrobinson 		;
1607c478bd9Sstevel@tonic-gate 
161*61961e0fSrobinson 	/*
162*61961e0fSrobinson 	 * any non-white, non-digits cause address to be rejected,
163*61961e0fSrobinson 	 * other fields are ignored
164*61961e0fSrobinson 	 */
1657c478bd9Sstevel@tonic-gate 	if ((*s == 0) || (end == s) || (!isspace(*end) && *end)) {
166*61961e0fSrobinson 		(void) fprintf(stderr,
167*61961e0fSrobinson 				"dobase: Illegal trailer on address string\n");
1687c478bd9Sstevel@tonic-gate 		buf[0] = '\0';
1697c478bd9Sstevel@tonic-gate 		return (0);
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	--end;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	buf[bp] = '\0';
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	while (bp > 0 && end >= s) {
1767c478bd9Sstevel@tonic-gate 		buf[bp] |= toxdigit(*end) << shift;
1777c478bd9Sstevel@tonic-gate 		if (type == OCT) {
1787c478bd9Sstevel@tonic-gate 			if (shift > 5) {
1797c478bd9Sstevel@tonic-gate 				buf[--bp] = (todigit(*end) >> (8 - shift))
1807c478bd9Sstevel@tonic-gate 					& MASK(shift-5);
1817c478bd9Sstevel@tonic-gate 			}
1827c478bd9Sstevel@tonic-gate 			if ((shift = (shift + 3) % 8) == 0)
1837c478bd9Sstevel@tonic-gate 				buf[--bp] = 0;
184*61961e0fSrobinson 		} else	/* hex */
1857c478bd9Sstevel@tonic-gate 			if ((shift = (shift) ? 0 : 4) == 0)
186*61961e0fSrobinson 				buf[--bp] = 0;
1877c478bd9Sstevel@tonic-gate 		--end;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	if (bp == 0) {
190*61961e0fSrobinson 		(void) fprintf(stderr, "stoa: dobase: number to long\n");
1917c478bd9Sstevel@tonic-gate 		return (0);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* need to catch end case to avoid extra 0's in front	*/
1957c478bd9Sstevel@tonic-gate 	if (!shift)
1967c478bd9Sstevel@tonic-gate 		bp++;
1977c478bd9Sstevel@tonic-gate 	memcp(buf, &buf[bp], (SBUFSIZE - bp));
1987c478bd9Sstevel@tonic-gate 	return (SBUFSIZE - bp);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static void
202*61961e0fSrobinson memcp(char *d, char *s, int n)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	while (n--)
2057c478bd9Sstevel@tonic-gate 		*d++ = *s++;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
208*61961e0fSrobinson /*
209*61961e0fSrobinson  * transfer block to a given destination or allocate one of the
210*61961e0fSrobinson  *	right size
211*61961e0fSrobinson  *	if max = 0 : ignore max
212*61961e0fSrobinson  */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static char *
215*61961e0fSrobinson xfer(char *dest, char *src, unsigned len, unsigned max)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	if (max && dest && max < len) {		/* No room */
218*61961e0fSrobinson 		(void) fprintf(stderr, "xfer: destination not long enough\n");
2197c478bd9Sstevel@tonic-gate 		return (NULL);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	if (!dest)
2227c478bd9Sstevel@tonic-gate 		if ((dest = malloc(len)) == NULL) {
223*61961e0fSrobinson 			(void) fprintf(stderr, "xfer: malloc failed\n");
2247c478bd9Sstevel@tonic-gate 			return (NULL);
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 
227*61961e0fSrobinson 	(void) memcpy(dest, src, (size_t)len);
2287c478bd9Sstevel@tonic-gate 	return (dest);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate #endif /* TLI */
231