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
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.
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]
23 * Copyright 1996-2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 #if !defined(_BOOT) && !defined(_KERNEL)
33 #endif /* _BOOT && _KERNEL */
34 #include <sys/types.h>
36 #include <sys/errno.h>
38 #if defined(_BOOT) || defined(_KERNEL)
39 #define isdigit(c) ((c) >= '0' && c <= '9')
40 #define isxdigit(c) (isdigit(c) || (((c) >= 'a') && ((c) <= 'f')) || \
41 (((c) >= 'A') && ((c) <= 'F')))
42 #endif /* _BOOT || _KERNEL */
45 * Converts an octet string into an ASCII string. The string returned is
46 * NULL-terminated, and the length recorded in blen does *not* include the
47 * null terminator (in other words, octet_to_hexascii() returns the length a'la
50 * Returns 0 for Success, errno otherwise.
53 octet_to_hexascii(const void *nump
, uint_t nlen
, char *bufp
, uint_t
*blen
)
58 static char ascii_conv
[] = "0123456789ABCDEF";
60 if (nump
== NULL
|| bufp
== NULL
|| blen
== NULL
)
63 if ((nlen
* 2) >= *blen
) {
67 for (i
= 0, bp
= bufp
, np
= (const uchar_t
*)nump
; i
< nlen
; i
++) {
68 *bp
++ = ascii_conv
[(np
[i
] >> 4) & 0x0f];
69 *bp
++ = ascii_conv
[np
[i
] & 0x0f];
77 * Converts an ASCII string into an octet string.
79 * Returns 0 for success, errno otherwise.
82 hexascii_to_octet(const char *asp
, uint_t alen
, void *bufp
, uint_t
*blen
)
88 if (asp
== NULL
|| bufp
== NULL
|| blen
== NULL
)
91 if (alen
> (*blen
* 2))
94 k
= ((alen
% 2) == 0) ? alen
/ 2 : (alen
/ 2) + 1;
95 for (tp
= asp
, u_tp
= (uchar_t
*)bufp
, i
= 0; i
< k
; i
++, u_tp
++) {
96 /* one nibble at a time */
97 for (*u_tp
= 0, j
= 0; j
< 2; j
++, tp
++) {
100 else if (isxdigit(*tp
))
101 *u_tp
|= (*tp
& ~0x20) + 10 - 'A';