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