static int fewf_pread(disk_t *disk, void *buffer, const unsigned int count, const uint64_t offset) { struct info_fewf_struct *data=(struct info_fewf_struct *)disk->data; int64_t taille; #if defined( HAVE_LIBEWF_V2_API ) taille = libewf_handle_read_random( data->handle, buffer, count, offset, NULL ); #else taille=libewf_read_random(data->handle, buffer, count, offset); #endif if(taille!=count) { log_error("fewf_pread(xxx,%u,buffer,%lu(%u/%u/%u)) read err: ", (unsigned)(count/disk->sector_size), (long unsigned)(offset/disk->sector_size), offset2cylinder(disk,offset), offset2head(disk,offset), offset2sector(disk,offset)); if(taille<0) log_error("%s\n", strerror(errno)); else if(taille==0) log_error("read after end of file\n"); else log_error("Partial read\n"); if(taille<=0) return -1; } return taille; }
static SSIZE_T ewf_image_read_random(IMG_INFO * img_info, SSIZE_T vol_offset, char *buf, OFF_T len, OFF_T offset) { SSIZE_T cnt; IMG_EWF_INFO *ewf_info = (IMG_EWF_INFO *) img_info; OFF_T tot_offset = offset + vol_offset; if (verbose) fprintf(stderr, "ewf_read_random: byte offset: %" PRIuOFF " len: %" PRIuOFF "\n", offset, len); cnt = libewf_read_random(ewf_info->handle, buf, len, tot_offset); if (cnt == -1) { // @@@ Add more specific error message tsk_errno = TSK_ERR_IMG_READ; snprintf(tsk_errstr, TSK_ERRSTR_L, "ewf_read_random - offset: %" PRIuOFF " - len: %" PRIuOFF " - %s", tot_offset, len, strerror(errno)); tsk_errstr2[0] = '\0'; return -1; } return cnt; }
static ssize_t ewf_image_read(TSK_IMG_INFO * img_info, TSK_OFF_T offset, char *buf, size_t len) { ssize_t cnt; IMG_EWF_INFO *ewf_info = (IMG_EWF_INFO *) img_info; if (tsk_verbose) tsk_fprintf(stderr, "ewf_read: byte offset: %" PRIuOFF " len: %" PRIuSIZE "\n", offset, len); if (offset > img_info->size) { tsk_error_reset(); tsk_errno = TSK_ERR_IMG_READ_OFF; snprintf(tsk_errstr, TSK_ERRSTR_L, "split_read - %" PRIuOFF, offset); return -1; } cnt = libewf_read_random(ewf_info->handle, buf, len, offset); if (cnt < 0) { tsk_error_reset(); // @@@ Add more specific error message tsk_error_reset(); tsk_errno = TSK_ERR_IMG_READ; snprintf(tsk_errstr, TSK_ERRSTR_L, "ewf_read - offset: %" PRIuOFF " - len: %" PRIuSIZE " - %s", offset, len, strerror(errno)); return -1; } return cnt; }
static ssize_t ewf_image_read(TSK_IMG_INFO * img_info, TSK_OFF_T offset, char *buf, size_t len) { #if defined( HAVE_LIBEWF_V2_API ) char error_string[TSK_EWF_ERROR_STRING_SIZE]; libewf_error_t *ewf_error = NULL; #endif ssize_t cnt; IMG_EWF_INFO *ewf_info = (IMG_EWF_INFO *) img_info; if (tsk_verbose) tsk_fprintf(stderr, "ewf_image_read: byte offset: %" PRIuOFF " len: %" PRIuSIZE "\n", offset, len); if (offset > img_info->size) { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_READ_OFF); tsk_error_set_errstr("ewf_image_read - %" PRIuOFF, offset); return -1; } tsk_take_lock(&(ewf_info->read_lock)); #if defined( HAVE_LIBEWF_V2_API ) cnt = libewf_handle_read_random(ewf_info->handle, buf, len, offset, &ewf_error); if (cnt < 0) { char *errmsg = NULL; tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_READ); if (getError(ewf_error, error_string)) errmsg = strerror(errno); else errmsg = error_string; tsk_error_set_errstr("ewf_image_read - offset: %" PRIuOFF " - len: %" PRIuSIZE " - %s", offset, len, errmsg); tsk_release_lock(&(ewf_info->read_lock)); return -1; } #else cnt = libewf_read_random(ewf_info->handle, buf, len, offset); if (cnt < 0) { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_READ); tsk_error_set_errstr("ewf_image_read - offset: %" PRIuOFF " - len: %" PRIuSIZE " - %s", offset, len, strerror(errno)); tsk_release_lock(&(ewf_info->read_lock)); return -1; } #endif tsk_release_lock(&(ewf_info->read_lock)); return cnt; }
/* Calculates the MD5 hash and compares this with the MD5 hash stored in the image */ char *libewf_calculate_md5hash( LIBEWF_HANDLE *handle ) { char *data; char *calculated_md5hash_string; uint64_t iterator; EWF_MD5HASH *calculated_md5hash; LIBEWF_MD5_CTX md5; if( handle == NULL ) { LIBEWF_FATAL_PRINT( "libewf_check_md5hash: incorrect handle\n" ); } if( handle->index_build == 0 ) { LIBEWF_FATAL_PRINT( "libewf_check_md5hash: index was not build.\n" ); } data = (char *) malloc( handle->chunk_size ); if( data == NULL ) { LIBEWF_FATAL_PRINT( "libewf_check_md5hash: unable to allocate data\n" ); } LIBEWF_MD5_INIT( &md5 ); for( iterator = 0; iterator <= handle->offset_table->last; iterator++ ) { uint64_t offset = iterator * handle->chunk_size; int64_t count = libewf_read_random( handle, data, handle->chunk_size, offset ); LIBEWF_MD5_UPDATE( &md5, data, count ); } free( data ) ; calculated_md5hash = ewf_md5hash_alloc(); LIBEWF_MD5_FINAL( calculated_md5hash, &md5 ); calculated_md5hash_string = ewf_md5hash_to_string( calculated_md5hash ); ewf_md5hash_free( calculated_md5hash ); return( calculated_md5hash_string ); }
/* Calculates the MD5 hash and creates a printable string of the calculated md5 hash * Returns 1 if successful, -1 on error */ int8_t libewf_calculate_md5_hash( LIBEWF_HANDLE *handle, LIBEWF_CHAR *string, size_t length ) { LIBEWF_INTERNAL_HANDLE *internal_handle = NULL; uint8_t *data = NULL; off_t offset = 0; ssize_t count = 0; uint32_t iterator = 0; if( handle == NULL ) { LIBEWF_WARNING_PRINT( "libewf_calculate_md5_hash: invalid handle.\n" ); return( -1 ); } internal_handle = (LIBEWF_INTERNAL_HANDLE *) internal_handle; if( internal_handle->media == NULL ) { LIBEWF_WARNING_PRINT( "libewf_calculate_md5_hash: invalid handle - missing subhandle media.\n" ); return( -1 ); } if( string == NULL ) { LIBEWF_VERBOSE_PRINT( "libewf_calculate_md5_hash: invalid string.\n" ); return( -1 ); } if( length < LIBEWF_STRING_DIGEST_HASH_LENGTH_MD5 ) { LIBEWF_VERBOSE_PRINT( "libewf_calculate_md5_hash: string too small.\n" ); return( -1 ); } if( internal_handle->index_build == 0 ) { LIBEWF_WARNING_PRINT( "libewf_calculate_md5_hash: index was not build.\n" ); return( -1 ); } if( internal_handle->calculated_md5_hash == NULL ) { data = (uint8_t *) libewf_common_alloc( internal_handle->media->chunk_size * sizeof( uint8_t ) ); if( data == NULL ) { LIBEWF_WARNING_PRINT( "libewf_calculate_md5_hash: unable to allocate data.\n" ); return( -1 ); } for( iterator = 0; iterator <= internal_handle->offset_table->last; iterator++ ) { offset = iterator * internal_handle->media->chunk_size; count = libewf_read_random( handle, data, internal_handle->media->chunk_size, offset ); if( count == -1 ) { LIBEWF_WARNING_PRINT( "libewf_calculate_md5_hash: unable to read chunk.\n" ); libewf_common_free( data ); return( -1 ); } if( count > (ssize_t) INT32_MAX ) { LIBEWF_WARNING_PRINT( "libewf_calculate_md5_hash: invalid count - only values below 2^32 supported.\n" ); libewf_common_free( data ); return( -1 ); } } libewf_common_free( data ) ; } else { LIBEWF_VERBOSE_PRINT( "libewf_calculate_md5_hash: MD5 hash already calculated.\n" ); } if( libewf_string_copy_from_digest_hash( string, length, internal_handle->calculated_md5_hash, EWF_DIGEST_HASH_SIZE_MD5 ) != 1 ) { LIBEWF_WARNING_PRINT( "libewf_calculate_md5_hash: unable to set MD5 hash string.\n" ); return( -1 ); } return( 1 ); }