tiencr.c
/*
 * \brief Read's the specified encr file and returns a buffer with file size
 *
 * \param file_path Input file
 * \param buffer Pointer to byte buffer to return the allocated buffer to
 * \param size Size of buffer
 */
\brief Read's the specified encr file and returns a buffer with file size
\param file_path Input file
\param buffer Pointer to byte buffer to return the allocated buffer to
\param size Size of buffer
\ brief Read ' s the specified encr file and returns a buffer with file size \ param file_path Input file \ param buffer Pointer to byte buffer to return the allocated buffer to \ param size Size of buffer
int read_encr(const char* file_path, uint8_t** buffer, size_t* size)
{
	int ret = 0;
	FILE *fp = NULL;
	uint8_t header[HEADER_LEN] = {0};
	size_t encode_index = 0;
	size_t key_len = 0;
	uint8_t* key_buf = NULL;
	size_t i = 0;
	size_t file_size;
	size_t res;
	uint8_t* buf_ptr = NULL;
	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,0x00,SEEK_SET);
	res = fread(header,HEADER_LEN,1,fp);
	if( res != 1 )
	{
		printf("File error:Unable to read header data\n");
		ret = ERR_FILE_READ;
		goto err;
	}
	for(i = 0; i < sizeof(header_str_bytes); i++ )
	{
		if(header_str_bytes[i] != header[i])
		{
			printf("File error:File is not valid\n");
			ret = ERR_FILE_READ;
			goto err;
		}
	}
	if (header[6] != 1)
	{
		printf("File error: Unsupported TIENCR format\n");
		ret = ERR_FILE_READ;
		goto err;
	}
	res = fread(header,1,1,fp);
	if( res != 1 )
	{
		printf("File error:Unable to read header data\n");
		ret = ERR_FILE_READ;
		goto err;
	}
	key_len = header[0] ^ header[7];
	key_buf = (uint8_t *)malloc(key_len);
	if( key_buf == NULL )
	{
		ret = ERR_MEMORY;
		goto err;
	}
	res = fread(key_buf,1,key_len,fp);
	xor_key(key_buf, key_len, false);
	file_size -= key_len;
	file_size -= 1;				
	file_size -= HEADER_LEN;
	buf_ptr = (uint8_t*)malloc(file_size);
	i = 0;
	while(!feof(fp) && i < file_size)
	{
		res = fread(&buf_ptr[i],1,1,fp);
		if( res != 1 )
		{
			break;
		}
		buf_ptr[i] = encode_char(key_buf, key_len, buf_ptr[i], &encode_index);
		i++;
	}
	*buffer = buf_ptr;
	*size = file_size;
	goto ok;
err:
	free(buf_ptr);
ok:
	free(key_buf);
	if (fp != NULL)
	{
		fclose(fp);
	}
	return ret;
}
int read_encr ( const char * file_path , uint8_t * * buffer , size_t * size ) { int ret = 0 ; FILE * fp = NULL ; uint8_t header [ HEADER_LEN ] = { 0 } ; size_t encode_index = 0 ; size_t key_len = 0 ; uint8_t * key_buf = NULL ; size_t i = 0 ; size_t file_size ; size_t res ; uint8_t * buf_ptr = NULL ; 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 , 0x00 , SEEK_SET ) ; res = fread ( header , HEADER_LEN , 1 , fp ) ; if ( res != 1 ) { printf ( " \n " ) ; ret = ERR_FILE_READ ; goto err ; } for ( i = 0 ; i < sizeof ( header_str_bytes ) ; i ++ ) { if ( header_str_bytes [ i ] != header [ i ] ) { printf ( " \n " ) ; ret = ERR_FILE_READ ; goto err ; } } if ( header [ 6 ] != 1 ) { printf ( " \n " ) ; ret = ERR_FILE_READ ; goto err ; } res = fread ( header , 1 , 1 , fp ) ; if ( res != 1 ) { printf ( " \n " ) ; ret = ERR_FILE_READ ; goto err ; } key_len = header [ 0 ] ^ header [ 7 ] ; key_buf = ( uint8_t * ) malloc ( key_len ) ; if ( key_buf == NULL ) { ret = ERR_MEMORY ; goto err ; } res = fread ( key_buf , 1 , key_len , fp ) ; xor_key ( key_buf , key_len , false ) ; file_size -= key_len ; file_size -= 1 ; file_size -= HEADER_LEN ; buf_ptr = ( uint8_t * ) malloc ( file_size ) ; i = 0 ; while ( ! feof ( fp ) && i < file_size ) { res = fread ( & buf_ptr [ i ] , 1 , 1 , fp ) ; if ( res != 1 ) { break ; } buf_ptr [ i ] = encode_char ( key_buf , key_len , buf_ptr [ i ] , & encode_index ) ; i ++ ; } * buffer = buf_ptr ; * size = file_size ; goto ok ; err : free ( buf_ptr ) ; ok : free ( key_buf ) ; if ( fp != NULL ) { fclose ( fp ) ; } return ret ; }
\brief Read's the specified encr file and returns a buffer with file size
 \param file_path Input file
 \param buffer Pointer to byte buffer to return the allocated buffer to
 \param size Size of buffer
\ brief Read ' s the specified encr file and returns a buffer with file size \ param file_path Input file \ param buffer Pointer to byte buffer to return the allocated buffer to \ param size Size of buffer
/* Get file size */ /* Back to the header */ /* verify the header */ /* Key length byte is at byte 0x0d(it's xored) */ /* Get the original key len by xoring byte 0x0d with byte 0x07 */ /* Grab the key which is next */ /*key length byte */ /* Read and decode to buffer */