Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F7713349
libewf_read_io_handle.c
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Size
9 KB
Referenced Files
None
Subscribers
None
libewf_read_io_handle.c
View Options
/*
* Low level reading functions
*
* Copyright (c) 2006-2011, Joachim Metz <jbmetz@users.sourceforge.net>
*
* Refer to AUTHORS for acknowledgements.
*
* This software is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
#include <common.h>
#include <types.h>
#include <liberror.h>
#include "libewf_chunk_data.h"
#include "libewf_definitions.h"
#include "libewf_libbfio.h"
#include "libewf_libmfdata.h"
#include "libewf_media_values.h"
#include "libewf_read_io_handle.h"
#include "libewf_sector_list.h"
/* Initialize the read IO handle
* Returns 1 if successful or -1 on error
*/
int libewf_read_io_handle_initialize(
libewf_read_io_handle_t **read_io_handle,
liberror_error_t **error )
{
static char *function = "libewf_read_io_handle_initialize";
if( read_io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid read IO handle.",
function );
return( -1 );
}
if( *read_io_handle == NULL )
{
*read_io_handle = memory_allocate_structure(
libewf_read_io_handle_t );
if( *read_io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_MEMORY,
LIBERROR_MEMORY_ERROR_INSUFFICIENT,
"%s: unable to create read IO handle.",
function );
goto on_error;
}
if( memory_set(
*read_io_handle,
0,
sizeof( libewf_read_io_handle_t ) ) == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_MEMORY,
LIBERROR_MEMORY_ERROR_SET_FAILED,
"%s: unable to clear read IO handle.",
function );
goto on_error;
}
if( libewf_sector_list_initialize(
&( ( *read_io_handle )->checksum_errors ),
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create checksum errors sector list.",
function );
goto on_error;
}
( *read_io_handle )->zero_on_error = 1;
}
return( 1 );
on_error:
if( *read_io_handle != NULL )
{
memory_free(
*read_io_handle );
*read_io_handle = NULL;
}
return( -1 );
}
/* Frees the read IO handle including elements
* Returns 1 if successful or -1 on error
*/
int libewf_read_io_handle_free(
libewf_read_io_handle_t **read_io_handle,
liberror_error_t **error )
{
static char *function = "libewf_read_io_handle_free";
int result = 1;
if( read_io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid read IO handle.",
function );
return( 1 );
}
if( *read_io_handle != NULL )
{
if( libewf_sector_list_free(
&( ( *read_io_handle )->checksum_errors ),
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_FINALIZE_FAILED,
"%s: unable to free checksum errors sector list.",
function );
result = -1;
}
memory_free(
*read_io_handle );
*read_io_handle = NULL;
}
return( result );
}
/* Clones the read IO handle
* Returns 1 if successful or -1 on error
*/
int libewf_read_io_handle_clone(
libewf_read_io_handle_t **destination_read_io_handle,
libewf_read_io_handle_t *source_read_io_handle,
liberror_error_t **error )
{
static char *function = "libewf_read_io_handle_clone";
if( destination_read_io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid destination read IO handle.",
function );
return( -1 );
}
if( *destination_read_io_handle != NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
"%s: invalid destination read IO handle value already set.",
function );
return( -1 );
}
if( source_read_io_handle == NULL )
{
*destination_read_io_handle = NULL;
return( 1 );
}
*destination_read_io_handle = memory_allocate_structure(
libewf_read_io_handle_t );
if( *destination_read_io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_MEMORY,
LIBERROR_MEMORY_ERROR_INSUFFICIENT,
"%s: unable to create destination read IO handle.",
function );
goto on_error;
}
if( memory_set(
*destination_read_io_handle,
0,
sizeof( libewf_read_io_handle_t ) ) == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_MEMORY,
LIBERROR_MEMORY_ERROR_SET_FAILED,
"%s: unable to clear source to destination read IO handle.",
function );
goto on_error;
}
if( libewf_sector_list_clone(
&( ( *destination_read_io_handle )->checksum_errors ),
source_read_io_handle->checksum_errors,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create destination checksum errors.",
function );
goto on_error;
}
( *destination_read_io_handle )->zero_on_error = source_read_io_handle->zero_on_error;
return( 1 );
on_error:
if( *destination_read_io_handle != NULL )
{
memory_free(
*destination_read_io_handle );
*destination_read_io_handle = NULL;
}
return( -1 );
}
/* Reads a certain chunk of data
* Adds a checksum error if the data is corrupted
* Returns 1 if successful or -1 on error
*/
int libewf_read_io_handle_read_chunk_data(
libewf_read_io_handle_t *read_io_handle,
libbfio_pool_t *file_io_pool,
libewf_media_values_t *media_values,
libmfdata_list_t *chunk_table_list,
libmfdata_cache_t *chunk_table_cache,
int chunk_index,
libewf_chunk_data_t **chunk_data,
liberror_error_t **error )
{
static char *function = "libewf_read_io_handle_read_chunk_data";
uint64_t start_sector = 0;
uint32_t number_of_sectors = 0;
if( read_io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid read IO handle.",
function );
return( -1 );
}
if( media_values == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid media values.",
function );
return( -1 );
}
if( chunk_data == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid chunk data.",
function );
return( -1 );
}
/* This function will expand element groups
*/
if( libmfdata_list_get_element_value_by_index(
chunk_table_list,
file_io_pool,
chunk_table_cache,
chunk_index,
(intptr_t **) chunk_data,
0,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to retrieve chunk data: %d.",
function,
chunk_index );
return( -1 );
}
if( *chunk_data == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: missing chunk data: %d.",
function,
chunk_index );
return( -1 );
}
if( libewf_chunk_data_unpack(
*chunk_data,
media_values->chunk_size,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_GENERIC,
"%s: unable to unpack chunk data: %d.",
function,
chunk_index );
return( -1 );
}
if( ( *chunk_data )->is_corrupt != 0 )
{
/* Add checksum error
*/
start_sector = (uint64_t) chunk_index * (uint64_t) media_values->sectors_per_chunk;
number_of_sectors = media_values->sectors_per_chunk;
if( ( start_sector + number_of_sectors ) > (uint64_t) media_values->number_of_sectors )
{
number_of_sectors = (uint32_t) ( (uint64_t) media_values->number_of_sectors - start_sector );
}
if( libewf_sector_list_append_sector(
read_io_handle->checksum_errors,
start_sector,
number_of_sectors,
1,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_APPEND_FAILED,
"%s: unable to append checksum error to sectors list.",
function );
return( -1 );
}
if( read_io_handle->zero_on_error != 0 )
{
if( memory_set(
( *chunk_data )->data,
0,
( *chunk_data )->data_size ) == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_MEMORY,
LIBERROR_MEMORY_ERROR_SET_FAILED,
"%s: unable to zero chunk data.",
function );
return( -1 );
}
}
}
return( 1 );
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Fri, Nov 21, 9:53 PM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1361530
Default Alt Text
libewf_read_io_handle.c (9 KB)
Attached To
Mode
rXMOUNT xmount
Attached
Detach File
Event Timeline
Log In to Comment