ewfcommon.c
来自「sleuthit-2.09 一个磁盘的工具集」· C语言 代码 · 共 2,239 行 · 第 1/5 页
C
2,239 行
return( -1 ); } media_size = libewf_get_media_size( handle ); if( media_size == 0 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: unable to determine media size.\n" ); return( -1 ); } chunk_size = libewf_get_chunk_size( handle ); if( chunk_size == 0 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: unable to determine chunk size.\n" ); return( -1 ); } if( chunk_size > (uint32_t) INT32_MAX ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: invalid chunk size only values below 2^32 are supported.\n" ); return( -1 ); }#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH buffer_size = EWFCOMMON_BUFFER_SIZE; data = (uint8_t *) libewf_common_alloc( buffer_size * sizeof( uint8_t ) ); if( data == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: unable to allocate data.\n" ); return( -1 ); }#else buffer_size = (size_t) chunk_size;#endif if( calculate_sha1 == 1 ) { if( ewfsha1_initialize( &sha1_context ) != 1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: unable to initialize SHA1 digest context.\n" ); return( -1 ); } } while( total_read_count < (int64_t) media_size ) { size = buffer_size; if( ( media_size - total_read_count ) < (uint64_t) size ) { size = (size_t) ( media_size - total_read_count ); }#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH read_count = libewf_read_random( handle, data, size, read_offset );#else read_count = libewf_read_random( handle, ( (LIBEWF_INTERNAL_HANDLE *) handle )->chunk_cache->data, size, read_offset );#endif if( read_count <= -1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: error reading data.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } if( read_count == 0 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: unexpected end of data.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } if( read_count > (ssize_t) size ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: more bytes read than requested.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } if( calculate_sha1 == 1 ) {#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH ewfsha1_update( &sha1_context, data, read_count );#else ewfsha1_update( &sha1_context, ( (LIBEWF_INTERNAL_HANDLE *) handle )->chunk_cache->data, read_count );#endif } read_offset += (off_t) size; total_read_count += (int64_t) read_count; if( callback != NULL ) { callback( total_read_count, media_size ); } }#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif if( calculate_sha1 == 1 ) { sha1_hash_string = (LIBEWF_CHAR *) libewf_common_alloc( LIBEWF_CHAR_SIZE * LIBEWF_STRING_DIGEST_HASH_LENGTH_SHA1 ); if( sha1_hash_string == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: unable to create SHA1 hash string.\n" ); return( -1 ); } if( ewfcommon_get_sha1_hash( &sha1_context, sha1_hash_string, LIBEWF_STRING_DIGEST_HASH_LENGTH_SHA1 ) != 1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: unable to set SHA1 hash string.\n" ); libewf_common_free( sha1_hash_string ); return( -1 ); } if( libewf_set_hash_value( handle, _S_LIBEWF_CHAR( "ewfcommon_calculated_SHA1" ), sha1_hash_string, LIBEWF_STRING_DIGEST_HASH_LENGTH_SHA1 ) != 1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read: unable to set SHA1 hash string in handle.\n" ); libewf_common_free( sha1_hash_string ); return( -1 ); } libewf_common_free( sha1_hash_string ); } return( total_read_count );}/* Reads the data to a file descriptor * Returns a -1 on error, the amount of bytes read on success */int64_t ewfcommon_read_to_file_descriptor( LIBEWF_HANDLE *handle, int output_file_descriptor, uint64_t read_size, uint64_t read_offset, void (*callback)( uint64_t bytes_read, uint64_t bytes_total ) ){#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH uint8_t *data = NULL;#endif ssize_t read_count = 0; ssize_t write_count = 0; size_t size = 0; size_t buffer_size = 0; int64_t total_read_count = 0; uint64_t media_size = 0; uint32_t chunk_size = 0; uint8_t read_all = 0; if( handle == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: invalid handle.\n" ); return( -1 ); } media_size = libewf_get_media_size( handle ); if( media_size == 0 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: unable to determine media size.\n" ); return( -1 ); } chunk_size = libewf_get_chunk_size( handle ); if( chunk_size == 0 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: unable to determine chunk size.\n" ); return( -1 ); } if( ( read_size == 0 ) || ( read_size > media_size ) || ( read_size > (uint64_t) INT64_MAX ) ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: invalid size.\n" ); return( -1 ); } if( read_offset >= media_size ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: invalid offset.\n" ); return( -1 ); } if( ( read_size + read_offset ) > media_size ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: unable to export beyond size of media.\n" ); return( -1 ); } read_all = (uint8_t) ( ( read_size == media_size ) && ( read_offset == 0 ) );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH buffer_size = EWFCOMMON_BUFFER_SIZE; data = (uint8_t *) libewf_common_alloc( buffer_size * sizeof( uint8_t ) ); if( data == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: unable to allocate data.\n" ); return( -1 ); }#else buffer_size = chunk_size;#endif while( total_read_count < (int64_t) read_size ) { size = buffer_size; if( ( media_size - total_read_count ) < (uint64_t) size ) { size = (size_t) ( media_size - total_read_count ); }#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH read_count = libewf_read_random( handle, data, size, (off_t) read_offset );#else read_count = libewf_read_random( handle, ( (LIBEWF_INTERNAL_HANDLE *) handle )->chunk_cache->data, size, (off_t) read_offset );#endif if( read_count <= -1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: error reading data.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } if( read_count == 0 ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: unexpected end of data.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } if( read_count > (ssize_t) size ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: more bytes read than requested.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } read_offset += size;#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH write_count = libewf_common_write( output_file_descriptor, data, (size_t) read_count );#else write_count = libewf_common_write( output_file_descriptor, ( (LIBEWF_INTERNAL_HANDLE *) handle )->chunk_cache->data, (size_t) read_count );#endif if( write_count < read_count ) { LIBEWF_WARNING_PRINT( "ewfcommon_read_to_file_descriptor: error writing data.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } total_read_count += read_count; if( callback != NULL ) { callback( total_read_count, read_size ); } }#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( total_read_count );}/* Writes data in EWF format from a file descriptor * Returns the amount of bytes written, or -1 on error */int64_t ewfcommon_write_from_file_descriptor( LIBEWF_HANDLE *handle, int input_file_descriptor, uint64_t write_size, uint64_t write_offset, uint8_t read_error_retry, uint32_t sector_error_granularity, uint8_t wipe_block_on_read_error, uint8_t seek_on_error, uint8_t calculate_sha1, void (*callback)( uint64_t bytes_read, uint64_t bytes_total ) ){ EWFSHA1_CONTEXT sha1_context; LIBEWF_CHAR *sha1_hash_string = NULL;#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH uint8_t *data = 0;#endif size_t buffer_size = 0; int64_t total_write_count = 0; int64_t write_count = 0; uint32_t chunk_size = 0; int32_t read_count = 0; if( handle == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: invalid handle.\n" ); return( -1 ); } if( input_file_descriptor == -1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: invalid file descriptor.\n" ); return( -1 ); } chunk_size = libewf_get_chunk_size( handle ); if( chunk_size == 0 ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unable to determine chunk media.\n" ); return( -1 ); } if( write_size > 0 ) { if( libewf_set_write_input_size( handle, write_size ) == -1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unable to set input write size in handle.\n" ); return( -1 ); } if( write_offset > 0 ) { if( write_offset >= write_size ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: invalid offset to write.\n" ); return( -1 ); } if( libewf_common_lseek( input_file_descriptor, write_offset, SEEK_SET ) != (int64_t) write_offset ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unable to find write offset.\n" ); return( -1 ); } } } else if( write_offset > 0 ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: ignoring write offset in a stream mode.\n" ); }#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH buffer_size = EWFCOMMON_BUFFER_SIZE; data = (uint8_t *) libewf_common_alloc( buffer_size * sizeof( uint8_t ) ); if( data == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unable to allocate data.\n" ); return( -1 ); }#else if( chunk_size > ( (LIBEWF_INTERNAL_HANDLE *) handle )->chunk_cache->allocated_size ) { /* Add 4 bytes for CRC */ if( libewf_internal_handle_chunk_cache_realloc( (LIBEWF_INTERNAL_HANDLE *) handle, ( chunk_size + EWF_CRC_SIZE ) ) == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unable to reallocate chunk cache.\n" ); return( -1 ); } } buffer_size = (size_t) chunk_size;#endif if( calculate_sha1 == 1 ) { if( ewfsha1_initialize( &sha1_context ) != 1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unable to initialize SHA1 digest context.\n" ); return( -1 ); } } while( ( write_size == 0 ) || ( total_write_count < (int64_t) write_size ) ) { /* Read a chunk from the file descriptor */#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH read_count = ewfcommon_read_input( handle, input_file_descriptor, data, buffer_size, total_write_count, write_size, read_error_retry, sector_error_granularity, wipe_block_on_read_error, seek_on_error );#else read_count = ewfcommon_read_input( handle, input_file_descriptor, ( (LIBEWF_INTERNAL_HANDLE *) handle )->chunk_cache->data, buffer_size, total_write_count, write_size, read_error_retry, sector_error_granularity, wipe_block_on_read_error, seek_on_error );#endif if( read_count <= -1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unable to read chunk from file.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } if( read_count == 0 ) { if( write_size != 0 ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unexpected end of input.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } break; } if( calculate_sha1 == 1 ) {#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH ewfsha1_update( &sha1_context, data, read_count );#else ewfsha1_update( &sha1_context, ( (LIBEWF_INTERNAL_HANDLE *) handle )->chunk_cache->data, read_count );#endif } if( ( write_size != 0 ) && ( ( total_write_count + read_count ) == (int64_t) write_size ) ) { if( calculate_sha1 == 1 ) { sha1_hash_string = (LIBEWF_CHAR *) libewf_common_alloc( LIBEWF_CHAR_SIZE * LIBEWF_STRING_DIGEST_HASH_LENGTH_SHA1 ); if( sha1_hash_string == NULL ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unable to create SHA1 hash string.\n" );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } if( ewfcommon_get_sha1_hash( &sha1_context, sha1_hash_string, LIBEWF_STRING_DIGEST_HASH_LENGTH_SHA1 ) != 1 ) { LIBEWF_WARNING_PRINT( "ewfcommon_write_from_file_descriptor: unable to set SHA1 hash string.\n" ); libewf_common_free( sha1_hash_string );#ifndef HAVE_CHUNK_CACHE_PASSTHROUGH libewf_common_free( data );#endif return( -1 ); } /* The SHA1 hash must be set before the last chunk is writt
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?