library/src/utility/BP-StringUtilities.cc
// uses regular expressions to determine if a string is a valid email address.
uses regular expressions to determine if a string is a valid email address.
uses regular expressions to determine if a string is a valid email address .
BP_ERROR_T BP_StringIsValidEmailAddress(char *str_value)
{
	if(!str_value)
		return ERR_FAILURE;
	if(!BP_StringIsReasonablePrintableString(str_value, bpstrlen(str_value)))
		return ERR_FAILURE;
	char len = bpstrlen(str_value);
	if(len < 6)
		return ERR_FAILURE;
	if(len > 254)
		return ERR_FAILURE;
	if(!isalnum(str_value[len-1]))
		return ERR_FAILURE;
	if(!isalnum(str_value[0]))
		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,
			(size_t) bpstrlen(str_value),
			&ascii_config
	);
	if(!buff_analysis)
	{
		return ERR_FAILURE;
	}
	if(!buff_analysis->ascii->char_counts['.'])
	{
		BP_DestroyBufferAnalysis(buff_analysis);
		return ERR_FAILURE;
	}
	if(buff_analysis->ascii->char_counts['.'] >= 10)
	{
		BP_DestroyBufferAnalysis(buff_analysis);
		return ERR_FAILURE;
	}
	if(buff_analysis->ascii->char_counts['@'] != 1)
	{
		BP_DestroyBufferAnalysis(buff_analysis);
		return ERR_FAILURE;
	}
	BP_DestroyBufferAnalysis(buff_analysis);
	char ** front_and_domain_parts = BP_ExplodeBufferBin(str_value, len, "@", 1, 2);
	if(!front_and_domain_parts)
		return ERR_FAILURE;
	if(BP_CountNullTerminatedPtrArray(front_and_domain_parts) != 2)
	{
		BP_DestroyPointerArrayNullTerminated((void **) front_and_domain_parts);
		return ERR_FAILURE;
	}
	if(bpstrlen(front_and_domain_parts[0]) > 64)
		return ERR_FAILURE;
	if(bpstrlen(front_and_domain_parts[1]) > 254)
		return ERR_FAILURE;
	BP_DestroyPointerArrayNullTerminated((void **) front_and_domain_parts);
 	char * regexp = "[\\w-]+(?:\\.[\\w-]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7}";
	BP_REGEX_MATCH_INFO match_info = {0};
	size_t matched = BP_MatchRegularExpressionPCRE
	(
		str_value,
		len,
		regexp,
		0,
		1024,
		&match_info,
		true
	);
	if(matched == ERR_SUCCESS)
	{
		BP_DestroyRegexpMatchInfo(&match_info);
		return ERR_SUCCESS;
	}
	BP_DestroyRegexpMatchInfo(&match_info);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_StringIsValidEmailAddress ( char * str_value ) { if ( ! str_value ) return ERR_FAILURE ; if ( ! BP_StringIsReasonablePrintableString ( str_value , bpstrlen ( str_value ) ) ) return ERR_FAILURE ; char len = bpstrlen ( str_value ) ; if ( len < 6 ) return ERR_FAILURE ; if ( len > 254 ) return ERR_FAILURE ; if ( ! isalnum ( str_value [ len - 1 ] ) ) return ERR_FAILURE ; if ( ! isalnum ( str_value [ 0 ] ) ) 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 , ( size_t ) bpstrlen ( str_value ) , & ascii_config ) ; if ( ! buff_analysis ) { return ERR_FAILURE ; } if ( ! buff_analysis -> ascii -> char_counts [ ' ' ] ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } if ( buff_analysis -> ascii -> char_counts [ ' ' ] >= 10 ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } if ( buff_analysis -> ascii -> char_counts [ ' ' ] != 1 ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return ERR_FAILURE ; } BP_DestroyBufferAnalysis ( buff_analysis ) ; char * * front_and_domain_parts = BP_ExplodeBufferBin ( str_value , len , " " , 1 , 2 ) ; if ( ! front_and_domain_parts ) return ERR_FAILURE ; if ( BP_CountNullTerminatedPtrArray ( front_and_domain_parts ) != 2 ) { BP_DestroyPointerArrayNullTerminated ( ( void * * ) front_and_domain_parts ) ; return ERR_FAILURE ; } if ( bpstrlen ( front_and_domain_parts [ 0 ] ) > 64 ) return ERR_FAILURE ; if ( bpstrlen ( front_and_domain_parts [ 1 ] ) > 254 ) return ERR_FAILURE ; BP_DestroyPointerArrayNullTerminated ( ( void * * ) front_and_domain_parts ) ; char * regexp = " \\ \\ \\ \\ \\ " ; BP_REGEX_MATCH_INFO match_info = { 0 } ; size_t matched = BP_MatchRegularExpressionPCRE ( str_value , len , regexp , 0 , 1024 , & match_info , true ) ; if ( matched == ERR_SUCCESS ) { BP_DestroyRegexpMatchInfo ( & match_info ) ; return ERR_SUCCESS ; } BP_DestroyRegexpMatchInfo ( & match_info ) ; return ERR_SUCCESS ; }
uses regular expressions to determine if a string is a valid email address.
uses regular expressions to determine if a string is a valid email address .
// ensure we have a string value // ensure value is reasonable printable string // calculate length after check // rfc 5322 states that the whole address can never be > 254 // must start with and end with alphanumeric values // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Buffer Analysis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create ascii configuration // zero out the structure (automagic macro) // set all counters to true // attempt to generate buffer analysis // if there is no buff analysis, exit now // force exit if analysis failed // check to ensure that there is at least one period // Ensure period count is reasonable.  IP's can have 3, subdomains can add more, top domain can add // another few.  10 is reasonable here. // Ensure that buffer only contains (and always contains) one @ sign. // destroy the buffer analysis // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Local Part/Domain Part Checks %%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // ex: local-part@domain-part // Per RFC 5322/RFC 5321 local part can be 64 bytes long, and domain part can be 255. // That said, it also says that an email can never be longer than 254 bytes long. // explode the buffers // ensure we have 2 entries (should always be the case) // run RFC length checks // destroy front and domain parts // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Regexp Check %%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // loose url grabbing regexp (can result in false positives, but not very often, treat // results from this regexp with regard to that taint effect) // create zeroed out inout structure // get matches // if we have a match we can attempt to insert the email // clear out match info // return indicating success // clear out match info // return indicating failure