#if !defined(_BOOT) && !defined(_KERNEL)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#endif
#include <sys/types.h>
#include <sys/null.h>
#include <sys/errno.h>
#if defined(_BOOT) || defined(_KERNEL)
#define isdigit(c) ((c) >= '0' && c <= '9')
#define isxdigit(c) (isdigit(c) || (((c) >= 'a') && ((c) <= 'f')) || \
(((c) >= 'A') && ((c) <= 'F')))
#endif
int
octet_to_hexascii(const void *nump, uint_t nlen, char *bufp, uint_t *blen)
{
int i;
char *bp;
const uchar_t *np;
static char ascii_conv[] = "0123456789ABCDEF";
if (nump == NULL || bufp == NULL || blen == NULL)
return (EINVAL);
if ((nlen * 2) >= *blen) {
*blen = 0;
return (E2BIG);
}
for (i = 0, bp = bufp, np = (const uchar_t *)nump; i < nlen; i++) {
*bp++ = ascii_conv[(np[i] >> 4) & 0x0f];
*bp++ = ascii_conv[np[i] & 0x0f];
}
*bp = '\0';
*blen = i * 2;
return (0);
}
int
hexascii_to_octet(const char *asp, uint_t alen, void *bufp, uint_t *blen)
{
int i, j, k;
const char *tp;
uchar_t *u_tp;
if (asp == NULL || bufp == NULL || blen == NULL)
return (EINVAL);
if (alen > (*blen * 2))
return (E2BIG);
k = ((alen % 2) == 0) ? alen / 2 : (alen / 2) + 1;
for (tp = asp, u_tp = (uchar_t *)bufp, i = 0; i < k; i++, u_tp++) {
for (*u_tp = 0, j = 0; j < 2; j++, tp++) {
if (isdigit(*tp))
*u_tp |= *tp - '0';
else if (isxdigit(*tp))
*u_tp |= (*tp & ~0x20) + 10 - 'A';
else
return (EINVAL);
if ((j % 2) == 0)
*u_tp <<= 4;
}
}
*blen = k;
return (0);
}