?? libewf_internal_handle.c
字號:
/* * libewf handle * * Copyright (c) 2006-2007, Joachim Metz <forensics@hoffmannbv.nl>, * Hoffmann Investigations. All rights reserved. * * Refer to AUTHORS for acknowledgements. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the creator, related organisations, nor the names of * its contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * - All advertising materials mentioning features or use of this software * must acknowledge the contribution by people stated in the acknowledgements. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER, COMPANY AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#include "libewf_includes.h"#include <time.h>#include <libewf/libewf_definitions.h>#include "libewf_common.h"#include "libewf_notify.h"#include "libewf_internal_handle.h"#include "libewf_string.h"#include "ewf_crc.h"#include "ewf_compress.h"#include "ewf_data.h"#include "ewf_definitions.h"#include "ewf_file_header.h"/* Allocates memory for a new handle struct * Returns a pointer to the new instance, NULL on error */LIBEWF_INTERNAL_HANDLE *libewf_internal_handle_alloc( uint16_t segment_amount, uint8_t flags ){ LIBEWF_INTERNAL_HANDLE *internal_handle = NULL; if( ( flags != LIBEWF_OPEN_READ ) && ( flags != LIBEWF_OPEN_WRITE ) && ( flags != LIBEWF_OPEN_READ_WRITE ) ) { LIBEWF_WARNING_PRINT( "ewf_handle_alloc: invalid flags.\n" ); return( NULL ); } internal_handle = (LIBEWF_INTERNAL_HANDLE *) libewf_common_alloc( LIBEWF_INTERNAL_HANDLE_SIZE ); if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "ewf_handle_alloc: unable to allocate handle.\n" ); return( NULL ); } internal_handle->media = NULL; internal_handle->read = NULL; internal_handle->write = NULL; internal_handle->segment_table = NULL; internal_handle->offset_table = NULL; internal_handle->secondary_offset_table = NULL; internal_handle->chunk_cache = NULL; internal_handle->header = NULL; internal_handle->header_size = 0; internal_handle->header2 = NULL; internal_handle->header2_size = 0; internal_handle->xheader = NULL; internal_handle->xheader_size = 0; internal_handle->xhash = NULL; internal_handle->xhash_size = 0; internal_handle->header_values = NULL; internal_handle->hash_values = NULL; internal_handle->stored_md5_hash = NULL; internal_handle->calculated_md5_hash = NULL; internal_handle->acquiry_error_sectors = NULL; internal_handle->acquiry_amount_of_errors = 0; internal_handle->current_chunk = 0; internal_handle->current_chunk_offset = 0; internal_handle->swap_byte_pairs = 0; internal_handle->compression_level = EWF_COMPRESSION_UNKNOWN; internal_handle->amount_of_header_sections = 0; internal_handle->format = LIBEWF_FORMAT_UNKNOWN; internal_handle->ewf_format = EWF_FORMAT_UNKNOWN; internal_handle->index_build = 0; internal_handle->error_tollerance = LIBEWF_ERROR_TOLLERANCE_COMPENSATE; internal_handle->segment_table = libewf_segment_table_alloc( segment_amount ); if( internal_handle->segment_table == NULL ) { LIBEWF_WARNING_PRINT( "ewf_handle_alloc: unable to create segment table.\n" ); libewf_common_free( internal_handle ); return( NULL ); } internal_handle->chunk_cache = libewf_chunk_cache_alloc( EWF_MINIMUM_CHUNK_SIZE + EWF_CRC_SIZE ); if( internal_handle->chunk_cache == NULL ) { LIBEWF_WARNING_PRINT( "ewf_handle_alloc: unable to create chunk cache.\n" ); libewf_segment_table_free( internal_handle->segment_table ); libewf_common_free( internal_handle ); return( NULL ); } internal_handle->media = libewf_internal_handle_media_alloc(); if( internal_handle->media == NULL ) { LIBEWF_WARNING_PRINT( "ewf_handle_alloc: unable to create media subhandle.\n" ); libewf_chunk_cache_free( internal_handle->chunk_cache ); libewf_segment_table_free( internal_handle->segment_table ); libewf_common_free( internal_handle ); return( NULL ); } if( ( flags == LIBEWF_OPEN_READ ) || ( flags == LIBEWF_OPEN_READ_WRITE ) ) { internal_handle->read = libewf_internal_handle_read_alloc(); if( internal_handle->read == NULL ) { LIBEWF_WARNING_PRINT( "ewf_handle_alloc: unable to create read subhandle.\n" ); libewf_internal_handle_media_free( internal_handle->media ); libewf_chunk_cache_free( internal_handle->chunk_cache ); libewf_segment_table_free( internal_handle->segment_table ); libewf_common_free( internal_handle ); return( NULL ); } } if( ( flags == LIBEWF_OPEN_WRITE ) || ( flags == LIBEWF_OPEN_READ_WRITE ) ) { internal_handle->write = libewf_internal_handle_write_alloc(); if( internal_handle->write == NULL ) { LIBEWF_WARNING_PRINT( "ewf_handle_alloc: unable to create write subhandle.\n" ); if( internal_handle->read != NULL ) { libewf_internal_handle_read_free( internal_handle->read ); } libewf_internal_handle_media_free( internal_handle->media ); libewf_chunk_cache_free( internal_handle->chunk_cache ); libewf_segment_table_free( internal_handle->segment_table ); libewf_common_free( internal_handle ); return( NULL ); } } return( internal_handle );}/* Frees memory of a handle struct including elements */void libewf_internal_handle_free( LIBEWF_INTERNAL_HANDLE *internal_handle ){ if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_internal_handle_free: invalid handle.\n" ); return; } libewf_internal_handle_media_free( internal_handle->media ); if( internal_handle->read != NULL ) { libewf_internal_handle_read_free( internal_handle->read ); } libewf_internal_handle_write_free( internal_handle->write ); if( internal_handle->segment_table != NULL ) { libewf_segment_table_free( internal_handle->segment_table ); } if( internal_handle->offset_table != NULL ) { libewf_offset_table_free( internal_handle->offset_table ); } if( internal_handle->secondary_offset_table != NULL ) { libewf_offset_table_free( internal_handle->secondary_offset_table ); } libewf_common_free( internal_handle->acquiry_error_sectors ); libewf_common_free( internal_handle->header ); libewf_common_free( internal_handle->header2 ); libewf_common_free( internal_handle->xheader ); libewf_common_free( internal_handle->xhash ); if( internal_handle->header_values != NULL ) { libewf_header_values_free( internal_handle->header_values ); } if( internal_handle->hash_values != NULL ) { libewf_hash_values_free( internal_handle->hash_values ); } libewf_common_free( internal_handle->stored_md5_hash ); libewf_common_free( internal_handle->calculated_md5_hash ); if( internal_handle->chunk_cache != NULL ) { libewf_chunk_cache_free( internal_handle->chunk_cache ); } libewf_common_free( internal_handle );}/* Allocates memory for a new handle media struct * Returns a pointer to the new instance, NULL on error */LIBEWF_INTERNAL_HANDLE_MEDIA *libewf_internal_handle_media_alloc( void ){ LIBEWF_INTERNAL_HANDLE_MEDIA *handle_media = NULL; handle_media = (LIBEWF_INTERNAL_HANDLE_MEDIA *) libewf_common_alloc( LIBEWF_INTERNAL_HANDLE_MEDIA_SIZE ); if( handle_media == NULL ) { LIBEWF_WARNING_PRINT( "ewf_handle_media_alloc: unable to allocate handle media.\n" ); return( NULL ); } handle_media->media_size = 0; handle_media->chunk_size = EWF_MINIMUM_CHUNK_SIZE; handle_media->sectors_per_chunk = 0; handle_media->bytes_per_sector = 0; handle_media->amount_of_chunks = 0; handle_media->amount_of_sectors = 0; handle_media->error_granularity = 0; handle_media->media_type = 0; handle_media->media_flags = 0x01; return( handle_media );}/* Allocates memory for a new handle read struct * Returns a pointer to the new instance, NULL on error */LIBEWF_INTERNAL_HANDLE_READ *libewf_internal_handle_read_alloc( void ){ LIBEWF_INTERNAL_HANDLE_READ *handle_read = NULL; handle_read = (LIBEWF_INTERNAL_HANDLE_READ *) libewf_common_alloc( LIBEWF_INTERNAL_HANDLE_READ_SIZE ); if( handle_read == NULL ) { LIBEWF_WARNING_PRINT( "ewf_handle_read_alloc: unable to allocate handle read.\n" ); return( NULL ); } handle_read->crc_error_sectors = NULL; handle_read->crc_amount_of_errors = 0; handle_read->values_initialized = 0; return( handle_read );}/* Frees memory of a handle read struct including elements */void libewf_internal_handle_read_free( LIBEWF_INTERNAL_HANDLE_READ *handle_read ){ if( handle_read == NULL ) { LIBEWF_WARNING_PRINT( "libewf_internal_handle_read_free: invalid handle read.\n" ); return; } libewf_common_free( handle_read->crc_error_sectors ); libewf_common_free( handle_read );}/* Allocates memory for a new handle write struct * Returns a pointer to the new instance, NULL on error */LIBEWF_INTERNAL_HANDLE_WRITE *libewf_internal_handle_write_alloc( void ){ LIBEWF_INTERNAL_HANDLE_WRITE *handle_write = NULL; handle_write = (LIBEWF_INTERNAL_HANDLE_WRITE *) libewf_common_alloc( LIBEWF_INTERNAL_HANDLE_WRITE_SIZE ); if( handle_write == NULL ) { LIBEWF_WARNING_PRINT( "ewf_handle_write_alloc: unable to allocate handle write.\n" ); return( NULL ); } handle_write->input_write_count = 0; handle_write->write_count = 0; handle_write->input_write_size = 0; handle_write->segment_file_size = 0; handle_write->maximum_amount_of_segments = 0; handle_write->chunks_section_write_count = 0; handle_write->amount_of_chunks = 0; handle_write->chunks_per_segment = 0; handle_write->chunks_per_chunks_section = 0; handle_write->segment_amount_of_chunks = 0; handle_write->section_amount_of_chunks = 0; handle_write->chunks_section_offset = 0; handle_write->chunks_section_number = 0; handle_write->compress_empty_block = 0; handle_write->unrestrict_offset_amount = 0; handle_write->values_initialized = 0; handle_write->create_chunks_section = 0; handle_write->write_finalized = 0; return( handle_write );}/* Reallocates the handle chunk cache * Returns a pointer to the instance, NULL on error */LIBEWF_INTERNAL_HANDLE *libewf_internal_handle_chunk_cache_realloc( LIBEWF_INTERNAL_HANDLE *internal_handle, size_t size ){ LIBEWF_CHUNK_CACHE *chunk_cache = NULL; if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_internal_handle_chunk_cache_realloc: invalid handle.\n" ); return( NULL ); } if( internal_handle->chunk_cache == NULL ) { LIBEWF_WARNING_PRINT( "libewf_internal_handle_chunk_cache_realloc: invalid handle - missing chunk cache.\n" ); return( NULL ); } chunk_cache = libewf_chunk_cache_realloc( internal_handle->chunk_cache, size ); if( chunk_cache == NULL ) { LIBEWF_WARNING_PRINT( "libewf_internal_handle_chunk_cache_realloc: unable to reallocate chunk cache.\n" ); return( NULL ); } internal_handle->chunk_cache = chunk_cache; return( internal_handle );}/* Check if the header value is set * Returns 0 if not set, 1 if set, or -1 on error */int8_t libewf_internal_handle_is_set_header( LIBEWF_INTERNAL_HANDLE *internal_handle ){ if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_internal_handle_is_set_header: invalid handle.\n" ); return( -1 ); } return( (uint8_t) ( internal_handle->header != NULL ) );}/* Check if the header2 value is set * Returns 0 if not set, 1 if set, or -1 on error */int8_t libewf_internal_handle_is_set_header2( LIBEWF_INTERNAL_HANDLE *internal_handle ){ if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_internal_handle_is_set_header2: invalid handle.\n" ); return( -1 ); } return( (uint8_t) ( internal_handle->header2 != NULL ) );}/* Check if the xheader value is set * Returns 0 if not set, 1 if set, or -1 on error */int8_t libewf_internal_handle_is_set_xheader( LIBEWF_INTERNAL_HANDLE *internal_handle ){ if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_internal_handle_is_set_xheader: invalid handle.\n" ); return( -1 ); } return( (uint8_t) ( internal_handle->xheader != NULL ) );}/* Check if the xhash value is set * Returns 0 if not set, 1 if set, or -1 on error
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -