library/src/utility/BP-StringUtilities.cc
// 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