tiencr.c
/*
 * \brief Read the contents of a file to a buffer and sets the passed in pointer to it
 *
 * \param file_path Path to file to read
 * \param buffer Pointer to buffer pointer that will be set once read is complete
 * \param buf_size Size of buffer once read is complete
 */
\brief Read the contents of a file to a buffer and sets the passed in pointer to it
\param file_path Path to file to read
\param buffer Pointer to buffer pointer that will be set once read is complete
\param buf_size Size of buffer once read is complete
\ brief Read the contents of a file to a buffer and sets the passed in pointer to it \ param file_path Path to file to read \ param buffer Pointer to buffer pointer that will be set once read is complete \ param buf_size Size of buffer once read is complete
int read_file_to_buffer(const char* file_path, uint8_t** buffer, size_t* buf_size)
{
	int ret = 0;
	FILE *fp = NULL;
	size_t file_size;
	uint8_t* buf_ptr;
	fp = fopen(file_path,"rb");
	if( fp == NULL )
	{
		printf("Error opening .encr file\n");
		ret = ERR_FILE_IO;
		goto err;
	}
	fseek(fp,0,SEEK_END);
	file_size = ftell(fp);
	fseek(fp,0,SEEK_SET);
	if( !file_size )
	{
		ret = ERR_FILE_READ;
		goto err;
	}
	buf_ptr = (uint8_t *)malloc(file_size);
	if( buf_ptr == NULL )
	{
		ret= ERR_MEMORY;
		goto err;
	}
	ret = fread(buf_ptr, file_size, 1, fp);
	if( ret != 1 )
	{
		ret = ERR_FILE_READ;
		goto err;
	}
	*buffer = buf_ptr;
	*buf_size = file_size;
	fclose(fp);
	return ret;
err:
	free(buffer);
	fclose(fp);
	return ret;
}
int read_file_to_buffer ( const char * file_path , uint8_t * * buffer , size_t * buf_size ) { int ret = 0 ; FILE * fp = NULL ; size_t file_size ; uint8_t * buf_ptr ; fp = fopen ( file_path , " " ) ; if ( fp == NULL ) { printf ( " \n " ) ; ret = ERR_FILE_IO ; goto err ; } fseek ( fp , 0 , SEEK_END ) ; file_size = ftell ( fp ) ; fseek ( fp , 0 , SEEK_SET ) ; if ( ! file_size ) { ret = ERR_FILE_READ ; goto err ; } buf_ptr = ( uint8_t * ) malloc ( file_size ) ; if ( buf_ptr == NULL ) { ret = ERR_MEMORY ; goto err ; } ret = fread ( buf_ptr , file_size , 1 , fp ) ; if ( ret != 1 ) { ret = ERR_FILE_READ ; goto err ; } * buffer = buf_ptr ; * buf_size = file_size ; fclose ( fp ) ; return ret ; err : free ( buffer ) ; fclose ( fp ) ; return ret ; }
\brief Read the contents of a file to a buffer and sets the passed in pointer to it
 \param file_path Path to file to read
 \param buffer Pointer to buffer pointer that will be set once read is complete
 \param buf_size Size of buffer once read is complete
\ brief Read the contents of a file to a buffer and sets the passed in pointer to it \ param file_path Path to file to read \ param buffer Pointer to buffer pointer that will be set once read is complete \ param buf_size Size of buffer once read is complete