library/src/filesystem/BP-Filesystem.cc
// Attempt to retrieve the entirety of a files contents.  Result must be bpfreed.
Attempt to retrieve the entirety of a files contents.  Result must be bpfreed.
Attempt to retrieve the entirety of a files contents . Result must be bpfreed .
char * BP_FileGetContents
(
		char *file_path,
		P_BP_FILE_LOAD_EXTRA_INFO extra_data_in_out
)
{
	BP_FILE_LOAD_EXTRA_INFO tmp_info;
	BP_ZERO_STACK(tmp_info);
	if(!extra_data_in_out)
		extra_data_in_out = (P_BP_FILE_LOAD_EXTRA_INFO) &tmp_info;
	extra_data_in_out->file_path_is_reasonable_for_ext4_filename = BP_StringIsReasonableForUnixEXT4Filename(file_path, bpstrlen(file_path), BP_TRUE);
	if(!extra_data_in_out->file_path_is_reasonable_for_ext4_filename)
		return NULL;
	extra_data_in_out->is_readable   = BP_FileAtPathExistsAndIsReadable  (file_path) ? BP_TRUE : BP_FALSE;
	extra_data_in_out->is_writable   = BP_FileAtPathExistsAndIsWritable  (file_path) ? BP_TRUE : BP_FALSE;
	extra_data_in_out->is_executable = BP_FileAtPathExistsAndIsExecutable(file_path) ? BP_TRUE : BP_FALSE;
	if
	(
		extra_data_in_out->is_readable ||
		extra_data_in_out->is_writable ||
		extra_data_in_out->is_executable
	)
	{
		extra_data_in_out->file_exists = BP_TRUE;
	}
	extra_data_in_out->file_size = BP_FGetSize(file_path);
	if(!extra_data_in_out->file_size)
	{
		extra_data_in_out->is_empty = BP_TRUE;
		return NULL;
	}
	char * new_mem = (char *) bpcalloc(extra_data_in_out->file_size+16, 1);
	if(!new_mem)
	{
		extra_data_in_out->failed_to_alloc_buff = BP_TRUE;
		return NULL;
	}
	FILE * open_file = BP_FOpen(file_path, "r");
	if(open_file)
	{
		extra_data_in_out->failed_to_open = BP_TRUE;
	}
	int tmp_file_read_size = (size_t) BP_FRead(open_file, new_mem, extra_data_in_out->file_size);
	if(tmp_file_read_size < 0)
	{
		extra_data_in_out->failed_to_read = BP_TRUE;
		if(!BP_FClose(open_file))
		{
			extra_data_in_out->failed_to_close = BP_TRUE;
		}
		bpfree(new_mem);
		return NULL;
	}
	extra_data_in_out->file_read_size = (size_t) tmp_file_read_size;
	if(!BP_FClose(open_file))
	{
		extra_data_in_out->failed_to_close = BP_TRUE;
	}
	 NOTE: At this point we have loaded data from the buffer and we can
	       run data checks.
	 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	 %%% Run After Load Checks %%%%%%%%%%%%%%%%%%%%%%%%
	 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	 set checkall
	if(extra_data_in_out->check_all)
		extra_data_in_out->check_buffer_is_reasonable_printable = BP_TRUE;
	 check contents sha1 hash
	if(extra_data_in_out->check_all)
		extra_data_in_out->check_contents_sha1 = BP_TRUE;
	 run reasonable printable checks if set
	if(extra_data_in_out->check_buffer_is_reasonable_printable)
	{
		 check printable with newlines
		extra_data_in_out->is_reasonable_printable_with_newlines = BP_StringIsReasonablePrintableString
		(
			new_mem,
			extra_data_in_out->file_size,
			BP_FALSE,
			BP_TRUE
		);
		 check printable with carriage returns
		extra_data_in_out->is_reasonable_printable_with_carriage_returns = BP_StringIsReasonablePrintableString
		(
			new_mem,
			extra_data_in_out->file_size,
			BP_TRUE,
			BP_FALSE
		);
		 check if printable with both
		extra_data_in_out->is_reasonable_printable_with_carriage_returns_and_newlines = BP_StringIsReasonablePrintableString
		(
			new_mem,
			extra_data_in_out->file_size,
			BP_TRUE,
			BP_TRUE
		);
		 check if printable with neither
		extra_data_in_out->is_reasonable_printable_without_carriage_returns_and_newlines = BP_StringIsReasonablePrintableString
		(
			new_mem,
			extra_data_in_out->file_size,
			BP_FALSE,
			BP_FALSE
		);
	}
	 run buffer sha1 generation
	if(!extra_data_in_out->is_empty)
	{
		 if we're supposed to check sha1 sum, do it now
		if(extra_data_in_out->check_contents_sha1)
		{
			 generate sum
			char *tmp_sha1 = BP_GenSHA1(new_mem, extra_data_in_out->file_size);
			 if we could generate our sha1, store it
			if(tmp_sha1)
			{
				 mark sum as having generated ok
				extra_data_in_out->sha1_sum_generated_ok = BP_TRUE;
				 copy in the sha1
				memcpy(extra_data_in_out->sha1_sum, tmp_sha1, bpstrlen(tmp_sha1));
				 free tmp buffer
				bpfree(tmp_sha1);
			}
		}
	}
	 return the new buffer
	return new_mem;
}
char * BP_FileGetContents ( char * file_path , P_BP_FILE_LOAD_EXTRA_INFO extra_data_in_out ) { BP_FILE_LOAD_EXTRA_INFO tmp_info ; BP_ZERO_STACK ( tmp_info ) ; if ( ! extra_data_in_out ) extra_data_in_out = ( P_BP_FILE_LOAD_EXTRA_INFO ) & tmp_info ; extra_data_in_out -> file_path_is_reasonable_for_ext4_filename = BP_StringIsReasonableForUnixEXT4Filename ( file_path , bpstrlen ( file_path ) , BP_TRUE ) ; if ( ! extra_data_in_out -> file_path_is_reasonable_for_ext4_filename ) return NULL ; extra_data_in_out -> is_readable = BP_FileAtPathExistsAndIsReadable ( file_path ) ? BP_TRUE : BP_FALSE ; extra_data_in_out -> is_writable = BP_FileAtPathExistsAndIsWritable ( file_path ) ? BP_TRUE : BP_FALSE ; extra_data_in_out -> is_executable = BP_FileAtPathExistsAndIsExecutable ( file_path ) ? BP_TRUE : BP_FALSE ; if ( extra_data_in_out -> is_readable || extra_data_in_out -> is_writable || extra_data_in_out -> is_executable ) { extra_data_in_out -> file_exists = BP_TRUE ; } extra_data_in_out -> file_size = BP_FGetSize ( file_path ) ; if ( ! extra_data_in_out -> file_size ) { extra_data_in_out -> is_empty = BP_TRUE ; return NULL ; } char * new_mem = ( char * ) bpcalloc ( extra_data_in_out -> file_size + 16 , 1 ) ; if ( ! new_mem ) { extra_data_in_out -> failed_to_alloc_buff = BP_TRUE ; return NULL ; } FILE * open_file = BP_FOpen ( file_path , " " ) ; if ( open_file ) { extra_data_in_out -> failed_to_open = BP_TRUE ; } int tmp_file_read_size = ( size_t ) BP_FRead ( open_file , new_mem , extra_data_in_out -> file_size ) ; if ( tmp_file_read_size < 0 ) { extra_data_in_out -> failed_to_read = BP_TRUE ; if ( ! BP_FClose ( open_file ) ) { extra_data_in_out -> failed_to_close = BP_TRUE ; } bpfree ( new_mem ) ; return NULL ; } extra_data_in_out -> file_read_size = ( size_t ) tmp_file_read_size ; if ( ! BP_FClose ( open_file ) ) { extra_data_in_out -> failed_to_close = BP_TRUE ; } if ( extra_data_in_out -> check_all ) extra_data_in_out -> check_buffer_is_reasonable_printable = BP_TRUE ; if ( extra_data_in_out -> check_all ) extra_data_in_out -> check_contents_sha1 = BP_TRUE ; if ( extra_data_in_out -> check_buffer_is_reasonable_printable ) { extra_data_in_out -> is_reasonable_printable_with_newlines = BP_StringIsReasonablePrintableString ( new_mem , extra_data_in_out -> file_size , BP_FALSE , BP_TRUE ) ; extra_data_in_out -> is_reasonable_printable_with_carriage_returns = BP_StringIsReasonablePrintableString ( new_mem , extra_data_in_out -> file_size , BP_TRUE , BP_FALSE ) ; extra_data_in_out -> is_reasonable_printable_with_carriage_returns_and_newlines = BP_StringIsReasonablePrintableString ( new_mem , extra_data_in_out -> file_size , BP_TRUE , BP_TRUE ) ; extra_data_in_out -> is_reasonable_printable_without_carriage_returns_and_newlines = BP_StringIsReasonablePrintableString ( new_mem , extra_data_in_out -> file_size , BP_FALSE , BP_FALSE ) ; } if ( ! extra_data_in_out -> is_empty ) { if ( extra_data_in_out -> check_contents_sha1 ) { char * tmp_sha1 = BP_GenSHA1 ( new_mem , extra_data_in_out -> file_size ) ; if ( tmp_sha1 ) { extra_data_in_out -> sha1_sum_generated_ok = BP_TRUE ; memcpy ( extra_data_in_out -> sha1_sum , tmp_sha1 , bpstrlen ( tmp_sha1 ) ) ; bpfree ( tmp_sha1 ) ; } } } return new_mem ; }
Attempt to retrieve the entirety of a files contents.
Attempt to retrieve the entirety of a files contents .
// create tmp info for use in case no extra data is passed in // overide tmp info if nothing is passed in // ensure the path is reasonable and that the file is there // if the path is not reasonable we must exit immediately to prevent any potential // shenanigans. // check rwx for current user // mark file existing if it's readable/writable/executable // retrieve size // mark file as empty if the size is zero // create new memory to hold file contents (or fail if memory cannot be created) // open file // read the data into the new buffer // ensure we have a file read size 0 or higher // mark it as a failed read // close the file since somehow we were able to open it but not read // destroy the allocated memory since the read failed // return a null pointer // set the file read size by typecasting to sizet // close the file // // NOTE: At this point we have loaded data from the buffer and we can //       run data checks. // // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run After Load Checks %%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set checkall // check contents sha1 hash // run reasonable printable checks if set // check printable with newlines // check printable with carriage returns // check if printable with both // check if printable with neither // run buffer sha1 generation // if we're supposed to check sha1 sum, do it now // generate sum // if we could generate our sha1, store it // mark sum as having generated ok // copy in the sha1 // free tmp buffer // return the new buffer