xref: /onnv-gate/usr/src/lib/libldap4/sec/secutil.c (revision 3857:21b9b714e4ab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  *
3*3857Sstevel  * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
4*3857Sstevel  * Use is subject to license terms.
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  *
70Sstevel@tonic-gate  * Comments:
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  */
100Sstevel@tonic-gate 
110Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
120Sstevel@tonic-gate 
130Sstevel@tonic-gate #include <stdlib.h>
140Sstevel@tonic-gate #include <string.h>
150Sstevel@tonic-gate #include <ctype.h>
160Sstevel@tonic-gate 
170Sstevel@tonic-gate static char	hexdig[] = "0123456789abcdef";
180Sstevel@tonic-gate 
hexa_print(char * aString,int aLen)190Sstevel@tonic-gate char* hexa_print(char *aString, int aLen)
200Sstevel@tonic-gate {
210Sstevel@tonic-gate 	char *res;
220Sstevel@tonic-gate 	int i =0;
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 	if ((res = (char *)calloc (aLen*2 + 1, 1 )) == NULL){
250Sstevel@tonic-gate 		return (NULL);
260Sstevel@tonic-gate 	}
270Sstevel@tonic-gate 	for (;;){
280Sstevel@tonic-gate 		if (aLen < 1)
290Sstevel@tonic-gate 			break;
300Sstevel@tonic-gate 		res[i] = hexdig[ ( *aString & 0xf0 ) >> 4 ];
310Sstevel@tonic-gate 		res[i + 1] = hexdig[ *aString & 0x0f ];
320Sstevel@tonic-gate 		i+= 2;
330Sstevel@tonic-gate 		aLen--;
340Sstevel@tonic-gate 		aString++;
350Sstevel@tonic-gate 	}
360Sstevel@tonic-gate 	return (res);
370Sstevel@tonic-gate }
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static int
unhex(char c)410Sstevel@tonic-gate unhex( char c )
420Sstevel@tonic-gate {
430Sstevel@tonic-gate         return( c >= '0' && c <= '9' ? c - '0'
440Sstevel@tonic-gate             : c >= 'A' && c <= 'F' ? c - 'A' + 10
450Sstevel@tonic-gate             : c - 'a' + 10 );
460Sstevel@tonic-gate }
470Sstevel@tonic-gate 
hexa2str(char * anHexaStr,int * aResLen)480Sstevel@tonic-gate char * hexa2str(char *anHexaStr, int *aResLen) {
490Sstevel@tonic-gate 	int theLen = 0;
500Sstevel@tonic-gate 	char *theRes = malloc(strlen(anHexaStr) /2 + 1);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	while (isxdigit(*anHexaStr)){
530Sstevel@tonic-gate 		theRes[theLen] = unhex(*anHexaStr) << 4;
540Sstevel@tonic-gate 		if (++anHexaStr != '\0'){
550Sstevel@tonic-gate 			theRes[theLen] += unhex(*anHexaStr);
560Sstevel@tonic-gate 			anHexaStr++;
570Sstevel@tonic-gate 		}
580Sstevel@tonic-gate 		theLen++;
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 	theRes[theLen] = '\0';
610Sstevel@tonic-gate 	* aResLen = theLen;
620Sstevel@tonic-gate 	return (theRes);
630Sstevel@tonic-gate }
64