1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  * Copyright %G% Sun Microsystems, Inc.
4*0Sstevel@tonic-gate  * All Rights Reserved
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  *
7*0Sstevel@tonic-gate  * Comments:
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  */
10*0Sstevel@tonic-gate 
11*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
12*0Sstevel@tonic-gate 
13*0Sstevel@tonic-gate #include <stdlib.h>
14*0Sstevel@tonic-gate #include <string.h>
15*0Sstevel@tonic-gate #include <ctype.h>
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate static char	hexdig[] = "0123456789abcdef";
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate char* hexa_print(char *aString, int aLen)
20*0Sstevel@tonic-gate {
21*0Sstevel@tonic-gate 	char *res;
22*0Sstevel@tonic-gate 	int i =0;
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate 	if ((res = (char *)calloc (aLen*2 + 1, 1 )) == NULL){
25*0Sstevel@tonic-gate 		return (NULL);
26*0Sstevel@tonic-gate 	}
27*0Sstevel@tonic-gate 	for (;;){
28*0Sstevel@tonic-gate 		if (aLen < 1)
29*0Sstevel@tonic-gate 			break;
30*0Sstevel@tonic-gate 		res[i] = hexdig[ ( *aString & 0xf0 ) >> 4 ];
31*0Sstevel@tonic-gate 		res[i + 1] = hexdig[ *aString & 0x0f ];
32*0Sstevel@tonic-gate 		i+= 2;
33*0Sstevel@tonic-gate 		aLen--;
34*0Sstevel@tonic-gate 		aString++;
35*0Sstevel@tonic-gate 	}
36*0Sstevel@tonic-gate 	return (res);
37*0Sstevel@tonic-gate }
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate static int
41*0Sstevel@tonic-gate unhex( char c )
42*0Sstevel@tonic-gate {
43*0Sstevel@tonic-gate         return( c >= '0' && c <= '9' ? c - '0'
44*0Sstevel@tonic-gate             : c >= 'A' && c <= 'F' ? c - 'A' + 10
45*0Sstevel@tonic-gate             : c - 'a' + 10 );
46*0Sstevel@tonic-gate }
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate char * hexa2str(char *anHexaStr, int *aResLen) {
49*0Sstevel@tonic-gate 	int theLen = 0;
50*0Sstevel@tonic-gate 	char *theRes = malloc(strlen(anHexaStr) /2 + 1);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	while (isxdigit(*anHexaStr)){
53*0Sstevel@tonic-gate 		theRes[theLen] = unhex(*anHexaStr) << 4;
54*0Sstevel@tonic-gate 		if (++anHexaStr != '\0'){
55*0Sstevel@tonic-gate 			theRes[theLen] += unhex(*anHexaStr);
56*0Sstevel@tonic-gate 			anHexaStr++;
57*0Sstevel@tonic-gate 		}
58*0Sstevel@tonic-gate 		theLen++;
59*0Sstevel@tonic-gate 	}
60*0Sstevel@tonic-gate 	theRes[theLen] = '\0';
61*0Sstevel@tonic-gate 	* aResLen = theLen;
62*0Sstevel@tonic-gate 	return (theRes);
63*0Sstevel@tonic-gate }
64