⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libewf_write.c

📁 sleuthit-2.09 一个磁盘的工具集
💻 C
📖 第 1 页 / 共 5 页
字号:
			}			/* Adjust the chunk data buffer if necessary			 */			if( ( chunk_cache_used == 1 ) && ( chunk_data != internal_handle->chunk_cache->data ) )			{				chunk_data = internal_handle->chunk_cache->data;			}			result = ewf_chunk_compress( internal_handle->chunk_cache->compressed, &compressed_data_size, chunk_data, size, internal_handle->compression_level );		}		if( result != 1 )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunk: unable to compress chunk: %" PRIu32 ".\n", chunk );			return( -1 );		}	}	if( ( internal_handle->ewf_format == EWF_FORMAT_S01 )	 || ( ( compressed_data_size > 0 ) && ( compressed_data_size < internal_handle->media->chunk_size ) ) )	{		data_write_size = compressed_data_size;		chunk_data      = internal_handle->chunk_cache->compressed;		/* Zlib creates its own CRC		 */		if( libewf_common_memcpy( &calculated_crc, &chunk_data[ compressed_data_size - EWF_CRC_SIZE ], EWF_CRC_SIZE ) == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunk: unable to set CRC.\n" );			return( -1 );		}		LIBEWF_VERBOSE_PRINT( "libewf_write_segment_file_chunk: writing COMPRESSED chunk: %" PRIu32 " at offset: %jd with size: %zu, with CRC: %" PRIu32 ".\n", ( chunk + 1 ), start_offset, data_write_size, calculated_crc );		if( libewf_offset_table_set_values( internal_handle->offset_table, internal_handle->write->amount_of_chunks, file_descriptor, 1, start_offset, compressed_data_size, segment_number ) == -1 )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunk: unable to set offset value in offset table.\n" );			return( -1 );		}	}	else	{		if( ewf_crc_calculate( &calculated_crc, (uint8_t *) chunk_data, size, 1 ) != 1 )		{			LIBEWF_VERBOSE_PRINT( "libewf_write_segment_file_chunk: unable to calculate CRC.\n" );			return( -1 );		}		data_write_size = size;		/* If the chunk cache data is used add the CRC		 */		if( chunk_data == internal_handle->chunk_cache->data )		{			if( libewf_endian_revert_32bit( calculated_crc, (uint8_t *) &chunk_data[ size ] ) != 1 )			{				LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunk: unable to revert CRC value.\n" );				return( -1 );			}			data_write_size += EWF_CRC_SIZE;		}		else		{			write_crc = 1;		}		LIBEWF_VERBOSE_PRINT( "libewf_write_segment_file_chunk: writing UNCOMPRESSED chunk: %" PRIu32 " at offset: %jd with size: %zu, with CRC: %" PRIu32 ".\n", ( chunk + 1 ), start_offset, data_write_size, calculated_crc );		if( libewf_offset_table_set_values( internal_handle->offset_table, internal_handle->write->amount_of_chunks, file_descriptor, 0, start_offset, ( size + EWF_CRC_SIZE ), segment_number ) == -1 )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunk: unable to set offset value in offset table.\n" );			return( -1 );		}	}	write_count = ewf_chunk_write( chunk_data, file_descriptor, data_write_size );	if( write_count != (int32_t) data_write_size )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunk: unable to write data.\n" );		return( -1 );	}	start_offset      += write_count;	total_write_count += write_count;	/* Write the CRC if necessary	 */	if( write_crc == 1 )	{		write_count = ewf_crc_write( &calculated_crc, file_descriptor );		if( write_count != (int32_t) EWF_CRC_SIZE )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunk: unable to write CRC.\n" );			return( -1 );		}		start_offset      += write_count;		total_write_count += write_count;	}	return( total_write_count );}/* Correct the sections before the actual data chunks * Also write the necessary sections after the actual data chunks to file (like table and table2 sections for EWF-E01 format) * Returns the amount of bytes written, or -1 on error */ssize_t libewf_write_segment_file_chunks_section_correction( LIBEWF_INTERNAL_HANDLE *internal_handle, int file_descriptor, off_t start_offset, LIBEWF_SECTION_LIST *section_list, off_t chunks_section_offset, size_t chunks_section_size ){	EWF_CHAR *table_section_string = NULL;	ssize_t total_write_count      = 0;	ssize_t write_count            = 0;	if( internal_handle == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: invalid handle.\n" );		return( -1 );	}	if( section_list == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: invalid section list.\n" );		return( -1 );	}	if( internal_handle->write == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: invalid handle - missing subhandle write.\n" );		return( -1 );	}	if( file_descriptor == -1 )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: invalid file descriptor.\n" );		return( -1 );	}	/* Seek the start of the data chunks	*/	LIBEWF_VERBOSE_PRINT( "libewf_write_segment_file_chunks_section_correction: setting file descriptor to start of chunks section offset: %" PRIu32 ".\n", chunks_section_offset );	if( libewf_common_lseek( file_descriptor, chunks_section_offset, SEEK_SET ) == -1 )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to find offset to correct sectors size.\n" );		return( -1 );	}	if( ( internal_handle->ewf_format == EWF_FORMAT_S01 )	 || ( internal_handle->format == LIBEWF_FORMAT_ENCASE1 ) )	{		LIBEWF_VERBOSE_PRINT( "libewf_write_segment_file_chunks_section_correction: correcting table section size: %zu offset: %jd.\n", chunks_section_size, chunks_section_offset );		/* Rewrite table section start		 */		write_count = libewf_section_table_write( internal_handle, file_descriptor, chunks_section_offset, internal_handle->offset_table, ( internal_handle->write->amount_of_chunks - internal_handle->write->section_amount_of_chunks ), internal_handle->write->section_amount_of_chunks, (EWF_CHAR *) "table", chunks_section_size );		if( write_count == -1 )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to rewrite %" PRIs_EWF " section.\n", table_section_string );			return( -1 );		}		if( libewf_section_list_append( section_list, (EWF_CHAR *) "table", chunks_section_offset, ( chunks_section_offset + write_count ) ) == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to append table section to section list.\n" );			return( -1 );		}	}	else if( internal_handle->ewf_format == EWF_FORMAT_E01 )	{		LIBEWF_VERBOSE_PRINT( "libewf_write_segment_file_chunks_section_correction: correcting sectors section size: %zu offset: %jd.\n", chunks_section_size, chunks_section_offset );		/* Rewrite sectors section start		 */		write_count = libewf_section_start_write( internal_handle, file_descriptor, (EWF_CHAR *) "sectors", chunks_section_size, chunks_section_offset );		if( write_count == -1 )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to rewrite sectors section.\n" );			return( -1 );		}		if( libewf_section_list_append( section_list, (EWF_CHAR *) "sectors", chunks_section_offset, ( chunks_section_offset + write_count ) ) == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to append sectors section to section list.\n" );			return( -1 );		}	}	/* Seek the end of the chunks section	*/	LIBEWF_VERBOSE_PRINT( "libewf_write_segment_file_chunks_section_correction: setting file descriptor back to end of data at offset: %" PRIu32 ".\n", start_offset );	if( libewf_common_lseek( file_descriptor, start_offset, SEEK_SET ) == -1 )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to find offset to continue.\n" );		return( -1 );	}	if( ( internal_handle->ewf_format == EWF_FORMAT_E01 ) && ( internal_handle->format != LIBEWF_FORMAT_ENCASE1 ) )	{		/* Write table section start		 */		write_count = libewf_section_table_write( internal_handle, file_descriptor, start_offset, internal_handle->offset_table, ( internal_handle->write->amount_of_chunks - internal_handle->write->section_amount_of_chunks ), internal_handle->write->section_amount_of_chunks, (EWF_CHAR *) "table", 0 );		if( write_count == -1 )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to rewrite table section.\n" );			return( -1 );		}		if( libewf_section_list_append( section_list, (EWF_CHAR *) "table", start_offset, ( start_offset + write_count ) ) == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to append table section to section list.\n" );			return( -1 );		}		start_offset      += write_count;		total_write_count += write_count;		/* Write table2 section start		 */		write_count = libewf_section_table_write( internal_handle, file_descriptor, start_offset, internal_handle->offset_table, ( internal_handle->write->amount_of_chunks - internal_handle->write->section_amount_of_chunks ), internal_handle->write->section_amount_of_chunks, (EWF_CHAR *) "table2", 0 );		if( write_count == -1 )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to rewrite table2 section.\n" );			return( -1 );		}		if( libewf_section_list_append( section_list, (EWF_CHAR *) "table2", start_offset, ( start_offset + write_count ) ) == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_chunks_section_correction: unable to append table2 section to section list.\n" );			return( -1 );		}		start_offset      += write_count;		total_write_count += write_count;	}	return( total_write_count );}/* Write the necessary sections at the end of the segment file * Returns the amount of bytes written, or -1 on error */ssize_t libewf_write_segment_file_end( LIBEWF_INTERNAL_HANDLE *internal_handle, LIBEWF_SECTION_LIST *section_list, int file_descriptor, off_t start_offset, uint8_t last_segment_file ){	LIBEWF_CHAR *md5_hash_string = NULL;	ssize_t total_write_count    = 0;	ssize_t write_count          = 0;	if( internal_handle == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: invalid handle.\n" );		return( -1 );	}	if( section_list == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: invalid section list.\n" );		return( -1 );	}	if( file_descriptor == -1 )	{		LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: invalid file descriptor.\n" );		return( -1 );	}	if( last_segment_file == 0 )	{		/* Write next section		 */		write_count = libewf_section_last_write( internal_handle, file_descriptor, (EWF_CHAR *) "next", start_offset );		if( write_count == -1 )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to write next section.\n" );			return( -1 );		}		if( libewf_section_list_append( section_list, (EWF_CHAR *) "next", start_offset, ( start_offset + write_count ) ) == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to append next section to section list.\n" );			return( -1 );		}		start_offset      += write_count;		total_write_count += write_count;	}	else	{		if( internal_handle->calculated_md5_hash != NULL )		{			/* Write the hash section			 */			write_count = libewf_section_hash_write( internal_handle, file_descriptor, start_offset, internal_handle->calculated_md5_hash );			if( write_count == -1 )			{				LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to write hash section.\n" );				return( -1 );			}			if( libewf_section_list_append( section_list, (EWF_CHAR *) "hash", start_offset, ( start_offset + write_count ) ) == NULL )			{				LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to append hash section to section list.\n" );				return( -1 );			}			start_offset      += write_count;			total_write_count += write_count;			if( libewf_internal_handle_set_stored_md5_hash( internal_handle, internal_handle->calculated_md5_hash ) != 1 )			{				LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to set stored MD5 hash in handle.\n" );				return( -1 );			}			if( internal_handle->format == LIBEWF_FORMAT_EWFX )			{				md5_hash_string = (LIBEWF_CHAR *) libewf_common_alloc( LIBEWF_CHAR_SIZE * LIBEWF_STRING_DIGEST_HASH_LENGTH_MD5 );				if( md5_hash_string == NULL )				{					LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to create MD5 hash string.\n" );					return( -1 );				}				if( libewf_string_copy_from_digest_hash( md5_hash_string, LIBEWF_STRING_DIGEST_HASH_LENGTH_MD5, internal_handle->stored_md5_hash, EWF_DIGEST_HASH_SIZE_MD5 ) != 1 )				{					LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to set MD5 hash string.\n" );					libewf_common_free( md5_hash_string );					return( -1 );				}				if( libewf_internal_handle_set_hash_value( internal_handle, _S_LIBEWF_CHAR( "MD5" ), md5_hash_string, LIBEWF_STRING_DIGEST_HASH_LENGTH_MD5 ) != 1 )				{					LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to set MD5 hash string in hash values.\n" );					libewf_common_free( md5_hash_string );					return( -1 );				}				libewf_common_free( md5_hash_string );			}		}		/* Write the xhash section		 */		if( internal_handle->format == LIBEWF_FORMAT_EWFX )		{			if( internal_handle->xhash != NULL )			{				LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: xhash already set - cleaning previous defintion.\n" );				libewf_common_free( internal_handle->xhash );			}			internal_handle->xhash = libewf_hash_values_generate_xhash_string_ewfx( internal_handle->hash_values, &internal_handle->xhash_size );			if( internal_handle->xhash == NULL )			{				LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to generate xhash.\n" );				return( -1 );			}			write_count = libewf_section_xhash_write( internal_handle, file_descriptor, start_offset, internal_handle->xhash, internal_handle->xhash_size, EWF_COMPRESSION_DEFAULT );			if( write_count == -1 )			{				LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to write xhash section.\n" );				return( -1 );			}			if( libewf_section_list_append( section_list, (EWF_CHAR *) "xhash", start_offset, ( start_offset + write_count ) ) == NULL )			{				LIBEWF_WARNING_PRINT( "libewf_write_segment_file_end: unable to append xhash section to section list.\n" );				return( -1 );			}			start_offset      += w

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -