library/src/utility/BP-StringUtilities.cc
BP_BOOL BP_PasswordExistsInFile(char *pass_file, char *password, size_t * line_number_in_out)
{

	// pass file and password
	if(!pass_file)
		return BP_FALSE;
	if(!password)
		return BP_FALSE;

	// ensure password is reasonable printable
	if(!BP_StringIsReasonablePrintableString(password, bpstrlen(password), BP_FALSE, BP_FALSE))
		return BP_FALSE;

	// ensure pass file exists
	if(!BP_StringIsReasonableForUnixEXT4Filename(pass_file, bpstrlen(pass_file), BP_TRUE))
		return BP_FALSE;

	// open file
	FILE * tmp_file = fopen( pass_file, "r");
	if(!tmp_file)
		return BP_FALSE;

	// create a 1k buffer for reading line by line
	char tmp_line_buff[BP_KILOBYTE];
	BP_ZERO_STACK(tmp_line_buff);

	// holds line number on iteration
	size_t line_number = 0;

	// iterate through lines in file (max 1k line length)
	while
	(
		fgets((char *) &tmp_line_buff, sizeof(tmp_line_buff)-10, tmp_file)
	)
	{

		// truncate
		if(tmp_line_buff[bpstrlen(tmp_line_buff)-1] == '\n')
			tmp_line_buff[bpstrlen(tmp_line_buff)-1] = 0x00;

		// Attempt a match.
		if(BP_StringsMatchAsReasonableStrings(tmp_line_buff, password))
		{

			// cloase the tmp file
			fclose(tmp_file);

			// mark the line number if specified
			if(line_number_in_out)
				*line_number_in_out = line_number;

			// exit indicating a match
			return BP_TRUE;

		}


		// increment the line number/index
		line_number++;

		// calculate length of string
		size_t tmp_bpstrlen = bpstrlen(tmp_line_buff);
		if(!tmp_bpstrlen)
			continue;

		// zero only the length of the string (saves cpu cycles over zeroing the entire buff)
		memset(tmp_line_buff, 0x00, tmp_bpstrlen);


	}



	// close the file
	fclose(tmp_file);

	// exit indicating false
	return BP_FALSE;

}
// Attempts to open the pass file and read it line by line, checking password.
Attempts to open the pass file and read it line by line, checking password.
Attempts to open the pass file and read it line by line checking password .
BP_BOOL BP_PasswordExistsInFile(char *pass_file, char *password, size_t * line_number_in_out)
{
	if(!pass_file)
		return BP_FALSE;
	if(!password)
		return BP_FALSE;
	if(!BP_StringIsReasonablePrintableString(password, bpstrlen(password), BP_FALSE, BP_FALSE))
		return BP_FALSE;
	if(!BP_StringIsReasonableForUnixEXT4Filename(pass_file, bpstrlen(pass_file), BP_TRUE))
		return BP_FALSE;
	FILE * tmp_file = fopen( pass_file, "r");
	if(!tmp_file)
		return BP_FALSE;
	char tmp_line_buff[BP_KILOBYTE];
	BP_ZERO_STACK(tmp_line_buff);
	size_t line_number = 0;
	while
	(
		fgets((char *) &tmp_line_buff, sizeof(tmp_line_buff)-10, tmp_file)
	)
	{
		if(tmp_line_buff[bpstrlen(tmp_line_buff)-1] == '\n')
			tmp_line_buff[bpstrlen(tmp_line_buff)-1] = 0x00;
		if(BP_StringsMatchAsReasonableStrings(tmp_line_buff, password))
		{
			fclose(tmp_file);
			if(line_number_in_out)
				*line_number_in_out = line_number;
			return BP_TRUE;
		}
		line_number++;
		size_t tmp_bpstrlen = bpstrlen(tmp_line_buff);
		if(!tmp_bpstrlen)
			continue;
		memset(tmp_line_buff, 0x00, tmp_bpstrlen);
	}
	fclose(tmp_file);
	return BP_FALSE;
}
BP_BOOL BP_PasswordExistsInFile ( char * pass_file , char * password , size_t * line_number_in_out ) { if ( ! pass_file ) return BP_FALSE ; if ( ! password ) return BP_FALSE ; if ( ! BP_StringIsReasonablePrintableString ( password , bpstrlen ( password ) , BP_FALSE , BP_FALSE ) ) return BP_FALSE ; if ( ! BP_StringIsReasonableForUnixEXT4Filename ( pass_file , bpstrlen ( pass_file ) , BP_TRUE ) ) return BP_FALSE ; FILE * tmp_file = fopen ( pass_file , " " ) ; if ( ! tmp_file ) return BP_FALSE ; char tmp_line_buff [ BP_KILOBYTE ] ; BP_ZERO_STACK ( tmp_line_buff ) ; size_t line_number = 0 ; while ( fgets ( ( char * ) & tmp_line_buff , sizeof ( tmp_line_buff ) - 10 , tmp_file ) ) { if ( tmp_line_buff [ bpstrlen ( tmp_line_buff ) - 1 ] == ' \n ' ) tmp_line_buff [ bpstrlen ( tmp_line_buff ) - 1 ] = 0x00 ; if ( BP_StringsMatchAsReasonableStrings ( tmp_line_buff , password ) ) { fclose ( tmp_file ) ; if ( line_number_in_out ) * line_number_in_out = line_number ; return BP_TRUE ; } line_number ++ ; size_t tmp_bpstrlen = bpstrlen ( tmp_line_buff ) ; if ( ! tmp_bpstrlen ) continue ; memset ( tmp_line_buff , 0x00 , tmp_bpstrlen ) ; } fclose ( tmp_file ) ; return BP_FALSE ; }
Attempts to open the pass file and read it line by line, checking password.
Attempts to open the pass file and read it line by line checking password .
// pass file and password // ensure password is reasonable printable // ensure pass file exists // open file // create a 1k buffer for reading line by line // holds line number on iteration // iterate through lines in file (max 1k line length) // truncate // Attempt a match. // cloase the tmp file // mark the line number if specified // exit indicating a match // increment the line number/index // calculate length of string // zero only the length of the string (saves cpu cycles over zeroing the entire buff) // close the file // exit indicating false