Exemplo n.º 1
0
/* Read a buffer from a specific input file
 * Returns the number of bytes read if successful or -1 on error
 */
ssize_t mount_handle_read_buffer(
         mount_handle_t *mount_handle,
         int input_file_index,
         uint8_t *buffer,
         size_t size,
         libcerror_error_t **error )
{
	libvhdi_file_t *input_file = NULL;
	static char *function      = "mount_handle_read_buffer";
	ssize_t read_count         = 0;

	if( mount_handle == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid mount handle.",
		 function );

		return( -1 );
	}
	if( libcdata_array_get_entry_by_index(
	     mount_handle->input_files_array,
	     input_file_index,
	     (intptr_t **) &input_file,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
		 "%s: unable to retrieve input file: %d.",
		 function,
		 input_file_index );

		return( -1 );
	}
	read_count = libvhdi_file_read_buffer(
	              input_file,
	              buffer,
	              size,
	              error );

	if( read_count == -1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_IO,
		 LIBCERROR_IO_ERROR_READ_FAILED,
		 "%s: unable to read buffer from input file: %d.",
		 function,
		 input_file_index );

		return( -1 );
	}
	return( read_count );
}
Exemplo n.º 2
0
/* Tests libvhdi_file_read_buffer
 * Returns 1 if successful, 0 if not or -1 on error
 */
int vhdi_test_read_buffer(
     libvhdi_file_t *file,
     size64_t input_size,
     size64_t expected_size )
{
	uint8_t buffer[ VHDI_TEST_READ_BUFFER_SIZE ];

	libvhdi_error_t *error  = NULL;
	size64_t remaining_size = 0;
	size64_t result_size    = 0;
	size_t read_size        = 0;
	ssize_t read_count      = 0;
	int result              = 0;

	if( file == NULL )
	{
		return( -1 );
	}
	remaining_size = input_size;

	while( remaining_size > 0 )
	{
		read_size = VHDI_TEST_READ_BUFFER_SIZE;

		if( remaining_size < (size64_t) read_size )
		{
			read_size = (size_t) remaining_size;
		}
		read_count = libvhdi_file_read_buffer(
			      file,
			      buffer,
			      read_size,
			      &error );

		if( read_count < 0 )
		{
			break;
		}
		remaining_size -= (size64_t) read_count;
		result_size    += (size64_t) read_count;

		if( read_count != (ssize_t) read_size )
		{
			break;
		}
	}
	if( expected_size != result_size )
	{
		fprintf(
		 stderr,
		 "Unexpected read count: %" PRIu64 "\n",
		 result_size );
	}
	else
	{
		result = 1;
	}
	if( error != NULL )
	{
		if( result != 1 )
		{
			libvhdi_error_backtrace_fprint(
			 error,
			 stderr );
		}
		libvhdi_error_free(
		 &error );
	}
	return( result );
}