library/src/utility/BP-StringUtilities.cc
// This will convert a string such as 0xCAFI EBABE in ascii, to a binary string.  The length
// of the new bpcalloced buffer (returned pointer) will be stored in out_blob_length if provided.
This will convert a string such as 0xCAFI EBABE in ascii, to a binary string.  The length
of the new bpcalloced buffer (returned pointer) will be stored in out_blob_length if provided.
This will convert a string such as 0xCAFI EBABE in ascii to a binary string . The length of the new bpcalloced buffer ( returned pointer ) will be stored in out_blob_length if provided .
char * BP_StringHexToBinaryBlob(char *ascii_hex_string_to_convert, size_t *out_blob_length)
{
	if(!ascii_hex_string_to_convert)
		return NULL;
	if(!BP_StringIsReasonablePrintableString(ascii_hex_string_to_convert, bpstrlen(ascii_hex_string_to_convert), BP_FALSE, BP_FALSE))
		return NULL;
	size_t in_str_len = bpstrlen(ascii_hex_string_to_convert);
	char *in_str_ptr = NULL;
	if(in_str_len >= 2)
	{
		if(memcmp("0x", ascii_hex_string_to_convert, 2) == 0)
		{
			in_str_ptr = &ascii_hex_string_to_convert[2];
			in_str_len -= 2;
		}
		else
		{
			in_str_ptr = ascii_hex_string_to_convert;
		}
	}
	else
	{
		in_str_ptr = ascii_hex_string_to_convert;
	}
	char *converted_lower_hex = BP_StringToLowerCase(in_str_ptr, in_str_len);
	if(!converted_lower_hex)
		return NULL;
	size_t converted_lower_hex_len = bpstrlen(converted_lower_hex);
	if(!BP_StringIsReasonableForHexLowerASCII(converted_lower_hex, converted_lower_hex_len))
	{
		bpfree(converted_lower_hex);
		return NULL;
	}
	size_t remainder_modulo                = converted_lower_hex_len % 2;
	size_t byte_len_not_counting_remainder = converted_lower_hex_len / 2;
	char *final_out_buff = (char *) bpcalloc(byte_len_not_counting_remainder+remainder_modulo+1, 1);
	unsigned long curr_char = 0;
	size_t str_pos_iter = 0;
	size_t out_str_idx = 0;
	for(; str_pos_iter < converted_lower_hex_len; str_pos_iter+=2)
	{
		unsigned char *curr_ptr_idx = (unsigned char *) &converted_lower_hex[str_pos_iter];
		unsigned char tmp_hex_buff[3];
		BP_ZERO_STACK(tmp_hex_buff);
		tmp_hex_buff[0] = (unsigned char) curr_ptr_idx[0];
		tmp_hex_buff[1] = (unsigned char) curr_ptr_idx[1];
		tmp_hex_buff[2] = (unsigned char) 0x00;
		curr_char = strtoul((char *) &tmp_hex_buff, NULL, 16);
		final_out_buff[out_str_idx] = (unsigned char) curr_char;
		out_str_idx++;
	}
	bpfree(converted_lower_hex);
	if(out_blob_length)
		*out_blob_length = out_str_idx;
	return final_out_buff;
}
char * BP_StringHexToBinaryBlob ( char * ascii_hex_string_to_convert , size_t * out_blob_length ) { if ( ! ascii_hex_string_to_convert ) return NULL ; if ( ! BP_StringIsReasonablePrintableString ( ascii_hex_string_to_convert , bpstrlen ( ascii_hex_string_to_convert ) , BP_FALSE , BP_FALSE ) ) return NULL ; size_t in_str_len = bpstrlen ( ascii_hex_string_to_convert ) ; char * in_str_ptr = NULL ; if ( in_str_len >= 2 ) { if ( memcmp ( " " , ascii_hex_string_to_convert , 2 ) == 0 ) { in_str_ptr = & ascii_hex_string_to_convert [ 2 ] ; in_str_len -= 2 ; } else { in_str_ptr = ascii_hex_string_to_convert ; } } else { in_str_ptr = ascii_hex_string_to_convert ; } char * converted_lower_hex = BP_StringToLowerCase ( in_str_ptr , in_str_len ) ; if ( ! converted_lower_hex ) return NULL ; size_t converted_lower_hex_len = bpstrlen ( converted_lower_hex ) ; if ( ! BP_StringIsReasonableForHexLowerASCII ( converted_lower_hex , converted_lower_hex_len ) ) { bpfree ( converted_lower_hex ) ; return NULL ; } size_t remainder_modulo = converted_lower_hex_len % 2 ; size_t byte_len_not_counting_remainder = converted_lower_hex_len / 2 ; char * final_out_buff = ( char * ) bpcalloc ( byte_len_not_counting_remainder + remainder_modulo + 1 , 1 ) ; unsigned long curr_char = 0 ; size_t str_pos_iter = 0 ; size_t out_str_idx = 0 ; for ( ; str_pos_iter < converted_lower_hex_len ; str_pos_iter += 2 ) { unsigned char * curr_ptr_idx = ( unsigned char * ) & converted_lower_hex [ str_pos_iter ] ; unsigned char tmp_hex_buff [ 3 ] ; BP_ZERO_STACK ( tmp_hex_buff ) ; tmp_hex_buff [ 0 ] = ( unsigned char ) curr_ptr_idx [ 0 ] ; tmp_hex_buff [ 1 ] = ( unsigned char ) curr_ptr_idx [ 1 ] ; tmp_hex_buff [ 2 ] = ( unsigned char ) 0x00 ; curr_char = strtoul ( ( char * ) & tmp_hex_buff , NULL , 16 ) ; final_out_buff [ out_str_idx ] = ( unsigned char ) curr_char ; out_str_idx ++ ; } bpfree ( converted_lower_hex ) ; if ( out_blob_length ) * out_blob_length = out_str_idx ; return final_out_buff ; }
This will convert a string such as 0xCAFI EBABE in ascii, to a binary string.
This will convert a string such as 0xCAFI EBABE in ascii to a binary string .
// run basic checks // calculate string length // string pointer to use // skip 0x prefix if it's set // attempt conversion to lowercase hexidecimal // calculate the new length // if the string is not reasonable for lower case hex // calculate if we have a remainder // create out buffer // set current character // create string position iter // index for the output string // iterate through the string and switch on the character // set current pointer index // create and zero a temporary hex buffer // set temporary hex buffer // set the current character // set the current character // destroy the converted lower hex // set the blob length if provided // return the final converted buffer