library/src/utility/BP-StringUtilities.cc
// checks if a string is an IPv4 address (doesn't use preg)
checks if a string is an IPv4 address (doesn't use preg)
checks if a string is an IPv4 address ( doesn ' t use preg )
BP_ERROR_T BP_StringIsIPv4Address(char * str_value)
{
	if(!str_value)
		return ERR_FAILURE;
	if(!BP_StringIsReasonablePrintableString(str_value, bpstrlen(str_value), BP_FALSE, BP_FALSE))
		return ERR_FAILURE;
	BP_BUFFER_ANALYSIS_ASCII_CONFIGURATION ascii_config;
	BP_ZERO_STACK(ascii_config);
	ascii_config.count_ascii                      = BP_TRUE;
	ascii_config.count_extended                   = BP_TRUE;
	ascii_config.count_individual_characters      = BP_TRUE;
	P_BP_BUFFER_ANALYSIS buff_analysis = BP_CreateBufferAnalysis
	(
			(unsigned char *) str_value,
			bpstrlen(str_value),
			&ascii_config
	);
	if(!buff_analysis)
	{
			BP_DestroyBufferAnalysis(buff_analysis);
			return ERR_FAILURE;
	}
	if(buff_analysis->ascii->char_counts['.'] != 3)
	{
		BP_DestroyBufferAnalysis(buff_analysis);
		return ERR_FAILURE;
	}
	BP_STRING_CONFORMITY_FLAGS_T c_flags = BP_StringConformsAs
	(
			str_value,
			bpstrlen(str_value),
			".",
			1
	);
	if(!c_flags)
	{
		BP_DestroyBufferAnalysis(buff_analysis);
		return ERR_FAILURE;
	}
	if(!BP_FLAG_IS_SET(c_flags, BP_STRING_CONFORMS_AS_NUMERIC_ONLY))
	{
		BP_DestroyBufferAnalysis(buff_analysis);
		return ERR_FAILURE;
	}
	%%%%%%%
	%%%%%%%
	size_t max_ip_len = 15;
	size_t string_len = bpstrlen(str_value);
	if(string_len > max_ip_len)
	{
		BP_DestroyBufferAnalysis(buff_analysis);
		return ERR_FAILURE;
	}
	size_t digit_len = 0;
	char *curr_read_pos = str_value;
	unsigned long octet_val = 0;
	size_t octet_counter = 0;
	char tmp_buff[16];
	BP_ZERO_STACK(tmp_buff);
	size_t str_iter = 0;
	do
	{
		digit_len = 0;
		octet_val = 0;
		BP_ZERO_STACK(tmp_buff);
		if(!isdigit(curr_read_pos[0]))
		{
			BP_DestroyBufferAnalysis(buff_analysis);
			return ERR_FAILURE;
		}
		if(isdigit(curr_read_pos[digit_len]))
			digit_len++;
		if(isdigit(curr_read_pos[digit_len]))
			digit_len++;
		if(isdigit(curr_read_pos[digit_len]))
			digit_len++;
		if(isdigit(curr_read_pos[digit_len]))
		{
			BP_DestroyBufferAnalysis(buff_analysis);
			return ERR_FAILURE;
		}
		memcpy((char *) &tmp_buff, curr_read_pos, digit_len);
		if(!BP_StringToUnsignedLongBase10(tmp_buff, digit_len, &octet_val))
		{
			BP_DestroyBufferAnalysis(buff_analysis);
			return ERR_FAILURE;
		}
		if(octet_val > 255)
		{
			BP_DestroyBufferAnalysis(buff_analysis);
			return ERR_FAILURE;
		}
		curr_read_pos = (char *) &curr_read_pos[digit_len+1];
		octet_counter++;
	} while(octet_counter < 4);
	BP_DestroyBufferAnalysis(buff_analysis);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_StringIsIPv4Address ( char * str_value ) { if ( ! str_value ) return ERR_FAILURE ; if ( ! BP_StringIsReasonablePrintableString ( str_value , bpstrlen ( str_value ) , BP_FALSE , BP_FALSE ) ) return ERR_FAILURE ; BP_BUFFER_ANALYSIS_ASCII_CONFIGURATION ascii_config ; BP_ZERO_STACK ( ascii_config ) ; ascii_config . count_ascii = BP_TRUE ; ascii_config . count_extended = BP_TRUE ; ascii_config . count_individual_characters = BP_TRUE ; P_BP_BUFFER_ANALYSIS buff_analysis = BP_CreateBufferAnalysis ( ( unsigned char * ) str_value , bpstrlen ( str_value ) , & ascii_config ) ; if ( ! buff_analysis ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } if ( buff_analysis -> ascii -> char_counts [ ' ' ] != 3 ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } BP_STRING_CONFORMITY_FLAGS_T c_flags = BP_StringConformsAs ( str_value , bpstrlen ( str_value ) , " " , 1 ) ; if ( ! c_flags ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } if ( ! BP_FLAG_IS_SET ( c_flags , BP_STRING_CONFORMS_AS_NUMERIC_ONLY ) ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } size_t max_ip_len = 15 ; size_t string_len = bpstrlen ( str_value ) ; if ( string_len > max_ip_len ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } size_t digit_len = 0 ; char * curr_read_pos = str_value ; unsigned long octet_val = 0 ; size_t octet_counter = 0 ; char tmp_buff [ 16 ] ; BP_ZERO_STACK ( tmp_buff ) ; size_t str_iter = 0 ; do { digit_len = 0 ; octet_val = 0 ; BP_ZERO_STACK ( tmp_buff ) ; if ( ! isdigit ( curr_read_pos [ 0 ] ) ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } if ( isdigit ( curr_read_pos [ digit_len ] ) ) digit_len ++ ; if ( isdigit ( curr_read_pos [ digit_len ] ) ) digit_len ++ ; if ( isdigit ( curr_read_pos [ digit_len ] ) ) digit_len ++ ; if ( isdigit ( curr_read_pos [ digit_len ] ) ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } memcpy ( ( char * ) & tmp_buff , curr_read_pos , digit_len ) ; if ( ! BP_StringToUnsignedLongBase10 ( tmp_buff , digit_len , & octet_val ) ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } if ( octet_val > 255 ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } curr_read_pos = ( char * ) & curr_read_pos [ digit_len + 1 ] ; octet_counter ++ ; } while ( octet_counter < 4 ) ; BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_SUCCESS ; }
checks if a string is an IPv4 address (doesn't use preg)
checks if a string is an IPv4 address ( doesn ' t use preg )
// ensure we have a value // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Analyze Buffer %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create ascii configuration // zero out the structure (automagic macro) // set some flags in the structure (default is BP_FALSE aka 0) // attempt to generate analysis // ensure we h ave a buffer analysis // must have three periods in it or we've failed sanity // check if string is just numbers and periods // ensure we have conformity flags // ensure it's numeric only except for the dots // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Specific Analysis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // ip addresses can't be longer than 15 chars long // calculate the string length // if the string length is greater than the max for an ip address, fail // digit length // current read position // octet val // octet count // temporary buffer used for octet conversion // iterate through string and check values // set digit length/octet val to zero on loop // zero out the octet buffer on loop // first char of a read should always be a number // calculate length of first octet // if the fourth item isn't a digit, we have a bad value and can exit here // copy data into the buffer // attempt to convert value to base 10 // if the octet value is ever > 255, return failure // set the current read position reference on loop // increment the octet counter on loop // destroy buffer analysis before exit // return indicating success