?? libewf_section.c
字號:
/* * libewf file reading * * 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 <libewf/libewf_definitions.h>#include "libewf_common.h"#include "libewf_debug.h"#include "libewf_endian.h"#include "libewf_header_values.h"#include "libewf_notify.h"#include "libewf_segment_table.h"#include "ewf_chunk.h"#include "ewf_compress.h"#include "ewf_crc.h"#include "ewf_data.h"#include "ewf_definitions.h"#include "ewf_error2.h"#include "ewf_file_header.h"#include "ewf_hash.h"#include "ewf_header.h"#include "ewf_header2.h"#include "ewf_ltree.h"#include "ewf_section.h"#include "ewf_volume.h"#include "ewf_volume_smart.h"#include "ewf_table.h"/* Reads and processes a section start * Returns the section start, or NULL on error */EWF_SECTION *libewf_section_start_read( LIBEWF_INTERNAL_HANDLE *internal_handle, int file_descriptor ){ EWF_SECTION *section = NULL; EWF_CRC calculated_crc = 0; EWF_CRC stored_crc = 0; if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_start_read: invalid handle.\n" ); return( NULL ); } section = (EWF_SECTION *) libewf_common_alloc( EWF_SECTION_SIZE ); if( section == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_start_read: unable to allocate section start.\n" ); return( NULL ); } if( ewf_section_read( section, file_descriptor ) <= -1 ) { LIBEWF_WARNING_PRINT( "libewf_section_start_read: unable to read section start.\n" ); libewf_common_free( section ); return( NULL ); } LIBEWF_VERBOSE_EXEC( libewf_debug_section_fprint( stderr, section ); );#ifdef HAVE_DEBUG_OUTPUT LIBEWF_VERBOSE_EXEC( libewf_dump_data( section->padding, 40 ); );#endif if( ewf_crc_calculate( &calculated_crc, (uint8_t *) section, ( EWF_SECTION_SIZE - EWF_CRC_SIZE ), 1 ) != 1 ) { LIBEWF_WARNING_PRINT( "libewf_section_start_read: unable to calculate CRC.\n" ); libewf_common_free( section ); return( NULL ); } if( libewf_endian_convert_32bit( &stored_crc, section->crc ) != 1 ) { LIBEWF_WARNING_PRINT( "libewf_section_start_read: unable to convert stored CRC value.\n" ); libewf_common_free( section ); return( NULL ); } if( stored_crc != calculated_crc ) { LIBEWF_WARNING_PRINT( "libewf_section_start_read: CRC does not match (in file: %" PRIu32 ", calculated: %" PRIu32 ").\n", stored_crc, calculated_crc ); if( internal_handle->error_tollerance < LIBEWF_ERROR_TOLLERANCE_COMPENSATE ) { libewf_common_free( section ); return( NULL ); } } return( section );}/* Write a section start to file * Returns the amount of bytes written, or -1 on error */ssize_t libewf_section_start_write( LIBEWF_INTERNAL_HANDLE *internal_handle, int file_descriptor, EWF_CHAR *section_type, size_t section_data_size, off_t start_offset ){ EWF_SECTION *section = NULL; ssize_t write_count = 0; size_t section_type_size = 0; uint64_t section_size = 0; uint64_t section_offset = 0; if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_start_write: invalid handle.\n" ); return( -1 ); } section_type_size = ewf_string_length( section_type ); if( section_type_size == 0 ) { LIBEWF_WARNING_PRINT( "libewf_section_start_write: section type is empty.\n" ); return( -1 ); } if( section_type_size >= 16 ) { LIBEWF_WARNING_PRINT( "libewf_section_start_write: section type is too long.\n" ); return( -1 ); } section = (EWF_SECTION *) libewf_common_alloc( EWF_SECTION_SIZE ); if( section == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_start_write: unable to create section.\n" ); return( -1 ); } if( libewf_common_memset( section, 0, EWF_SECTION_SIZE ) == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_start_write: unable to clear section.\n" ); libewf_common_free( section ); return( -1 ); } /* Add one character for the end of string */ if( ewf_string_copy( section->type, section_type, ( section_type_size + 1 ) ) == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_start_write: unable to set section type.\n" ); libewf_common_free( section ); return( -1 ); } section_size = EWF_SECTION_SIZE + section_data_size; section_offset = start_offset + section_size; if( libewf_endian_revert_64bit( section_size, section->size ) != 1 ) { LIBEWF_WARNING_PRINT( "libewf_section_start_write: unable to revert size value.\n" ); libewf_common_free( section ); return( -1 ); } if( libewf_endian_revert_64bit( section_offset, section->next ) != 1 ) { LIBEWF_WARNING_PRINT( "libewf_section_start_write: unable to revert next offset value.\n" ); libewf_common_free( section ); return( -1 ); } write_count = ewf_section_write( section, file_descriptor ); libewf_common_free( section ); if( write_count == -1 ) { LIBEWF_WARNING_PRINT( "libewf_section_start_write: unable to write section to file.\n" ); return( -1 ); } return( write_count );}/* Write a compressed string section to file * Returns the amount of bytes written, or -1 on error */ssize_t libewf_section_compressed_string_write( LIBEWF_INTERNAL_HANDLE *internal_handle, int file_descriptor, off_t start_offset, EWF_CHAR *section_type, EWF_CHAR *uncompressed_string, size_t size, int8_t compression_level ){ EWF_CHAR *compressed_string = NULL; ssize_t section_write_count = 0; ssize_t string_write_count = 0; if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_compressed_string_write: invalid handle.\n" ); return( -1 ); } if( section_type == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_compressed_string_write: invalid section type.\n" ); return( -1 ); } if( uncompressed_string == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_compressed_string_write: invalid uncompressed string.\n" ); return( -1 ); } LIBEWF_VERBOSE_PRINT( "libewf_section_compressed_string_write: String:\n" ); LIBEWF_VERBOSE_EXEC( libewf_debug_header_fprint( stderr, uncompressed_string, size ); ); compressed_string = ewf_string_compress( uncompressed_string, &size, compression_level ); if( compressed_string == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_compressed_string_write: unable to compress string.\n" ); return( -1 ); } section_write_count = libewf_section_start_write( internal_handle, file_descriptor, section_type, size, start_offset ); if( section_write_count == -1 ) { LIBEWF_WARNING_PRINT( "libewf_section_compressed_string_write: unable to write section to file.\n" ); libewf_common_free( compressed_string ); return( -1 ); } string_write_count = ewf_string_write_from_buffer( compressed_string, file_descriptor, size ); libewf_common_free( compressed_string ); if( string_write_count == -1 ) { LIBEWF_WARNING_PRINT( "libewf_section_compressed_string_write: unable to write string to file.\n" ); return( -1 ); } return( section_write_count + string_write_count );}/* Reads a header section * Returns the amount of bytes read, or -1 on error */ssize_t libewf_section_header_read( LIBEWF_INTERNAL_HANDLE *internal_handle, int file_descriptor, size_t size ){ EWF_HEADER *header = NULL; ssize_t read_count = (ssize_t) size; if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_header_read: invalid handle.\n" ); return( -1 ); } header = ewf_header_read( file_descriptor, &size ); if( header == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_header_read: unable to read header.\n" ); return( -1 ); } LIBEWF_VERBOSE_PRINT( "libewf_section_header_read: Header:\n" ); LIBEWF_VERBOSE_EXEC( libewf_debug_header_fprint( stderr, header, size ); ); if( libewf_internal_handle_is_set_header( internal_handle ) == 0 ) { if( libewf_internal_handle_set_header( internal_handle, header, size ) != 1 ) { LIBEWF_WARNING_PRINT( "libewf_section_header_read: unable to set header in handle.\n" ); libewf_common_free( header ); if( internal_handle->error_tollerance < LIBEWF_ERROR_TOLLERANCE_COMPENSATE ) { return( -1 ); } } } else { libewf_common_free( header ); } internal_handle->amount_of_header_sections++; return( read_count );}/* Write a header section to file * Returns the amount of bytes written, or -1 on error */ssize_t libewf_section_header_write( LIBEWF_INTERNAL_HANDLE *internal_handle, int file_descriptor, off_t start_offset, EWF_HEADER *header, size_t size, int8_t compression_level ){ ssize_t section_write_count = 0; section_write_count = libewf_section_compressed_string_write( internal_handle, file_descriptor, start_offset, (EWF_CHAR *) "header", header, size, compression_level ); if( section_write_count != -1 ) { internal_handle->amount_of_header_sections++; } return( section_write_count );}/* Reads a header2 section * Returns the amount of bytes read, or -1 on error */ssize_t libewf_section_header2_read( LIBEWF_INTERNAL_HANDLE *internal_handle, int file_descriptor, size_t size ){ EWF_HEADER *header2 = NULL; ssize_t read_count = (ssize_t) size; if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_header2_read: invalid handle.\n" ); return( -1 ); } header2 = ewf_header2_read( file_descriptor, &size ); if( header2 == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_header2_read: unable to read header2.\n" ); return( -1 ); } LIBEWF_VERBOSE_PRINT( "libewf_section_header2_read: Header2:\n" ); LIBEWF_VERBOSE_EXEC( libewf_debug_header2_fprint( stderr, header2, size ); ); if( libewf_internal_handle_is_set_header2( internal_handle ) == 0 ) { if( libewf_internal_handle_set_header2( internal_handle, header2, size ) != 1 ) { LIBEWF_WARNING_PRINT( "libewf_section_header2_read: unable to set header2 in handle.\n" ); libewf_common_free( header2 ); if( internal_handle->error_tollerance < LIBEWF_ERROR_TOLLERANCE_COMPENSATE ) { return( -1 ); } } } else { libewf_common_free( header2 ); } internal_handle->amount_of_header_sections++; return( read_count );}/* Write a header2 section to file * Returns the amount of bytes written, or -1 on error */ssize_t libewf_section_header2_write( LIBEWF_INTERNAL_HANDLE *internal_handle, int file_descriptor, off_t start_offset, EWF_HEADER2 *header2, size_t size, int8_t compression_level ){ ssize_t section_write_count = 0; section_write_count = libewf_section_compressed_string_write( internal_handle, file_descriptor, start_offset, (EWF_CHAR *) "header2", header2, size, compression_level ); if( section_write_count != -1 ) { internal_handle->amount_of_header_sections++; } return( section_write_count );}/* Reads an EWF-S01 (SMART) volume section * Returns the amount of bytes read, or -1 on error */ssize_t libewf_section_volume_s01_read( LIBEWF_INTERNAL_HANDLE *internal_handle, int file_descriptor, size_t size ){ EWF_VOLUME_SMART *volume_smart = NULL; EWF_CRC calculated_crc = 0; EWF_CRC stored_crc = 0; int32_t bytes_per_chunk = 0; if( internal_handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_section_volume_s01_read: invalid handle.\n" ); return( -1 );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -