xref: /titanic_51/usr/src/common/net/dhcp/octet.c (revision 0219346b07c8d846112a335f1543309c21e3d8da)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1996-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /* LINTLIBRARY */
30 
31 #if !defined(_BOOT) && !defined(_KERNEL)
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <stdlib.h>
35 #endif	/* _BOOT && _KERNEL */
36 #include <sys/types.h>
37 #include <sys/errno.h>
38 
39 #if	defined(_BOOT) || defined(_KERNEL)
40 #define	NULL		0
41 #define	isdigit(c)	((c) >= '0' && c <= '9')
42 #define	isxdigit(c)	(isdigit(c) || (((c) >= 'a') && ((c) <= 'f')) || \
43 			(((c) >= 'A') && ((c) <= 'F')))
44 #endif	/* _BOOT || _KERNEL */
45 
46 /*
47  * Converts an octet string into an ASCII string. The string returned is
48  * NULL-terminated, and the length recorded in blen does *not* include the
49  * null terminator (in other words, octet_to_hexascii() returns the length a'la
50  * strlen()).
51  *
52  * Returns 0 for Success, errno otherwise.
53  */
54 int
55 octet_to_hexascii(const void *nump, uint_t nlen, char *bufp, uint_t *blen)
56 {
57 	int		i;
58 	char		*bp;
59 	const uchar_t	*np;
60 	static char	ascii_conv[] = "0123456789ABCDEF";
61 
62 	if (nump == NULL || bufp == NULL || blen == NULL)
63 		return (EINVAL);
64 
65 	if ((nlen * 2) >= *blen) {
66 		*blen = 0;
67 		return (E2BIG);
68 	}
69 	for (i = 0, bp = bufp, np = (const uchar_t *)nump; i < nlen; i++) {
70 		*bp++ = ascii_conv[(np[i] >> 4) & 0x0f];
71 		*bp++ = ascii_conv[np[i] & 0x0f];
72 	}
73 	*bp = '\0';
74 	*blen = i * 2;
75 	return (0);
76 }
77 
78 /*
79  * Converts an ASCII string into an octet string.
80  *
81  * Returns 0 for success, errno otherwise.
82  */
83 int
84 hexascii_to_octet(const char *asp, uint_t alen, void *bufp, uint_t *blen)
85 {
86 	int		i, j, k;
87 	const char	*tp;
88 	uchar_t		*u_tp;
89 
90 	if (asp == NULL || bufp == NULL || blen == NULL)
91 		return (EINVAL);
92 
93 	if (alen > (*blen * 2))
94 		return (E2BIG);
95 
96 	k = ((alen % 2) == 0) ? alen / 2 : (alen / 2) + 1;
97 	for (tp = asp, u_tp = (uchar_t *)bufp, i = 0; i < k; i++, u_tp++) {
98 		/* one nibble at a time */
99 		for (*u_tp = 0, j = 0; j < 2; j++, tp++) {
100 			if (isdigit(*tp))
101 				*u_tp |= *tp - '0';
102 			else if (isxdigit(*tp))
103 				*u_tp |= (*tp & ~0x20) + 10 - 'A';
104 			else
105 				return (EINVAL);
106 			if ((j % 2) == 0)
107 				*u_tp <<= 4;
108 		}
109 	}
110 	*blen = k;
111 	return (0);
112 }
113