/* Retrieves the media size of the input handle * Returns 1 if successful or -1 on error */ int mount_handle_get_media_size( mount_handle_t *mount_handle, size64_t *size, libcerror_error_t **error ) { static char *function = "mount_handle_get_media_size"; if( mount_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid mount handle.", function ); return( -1 ); } if( libewf_handle_get_media_size( mount_handle->input_handle, size, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve media size from input handle.", function ); return( -1 ); } return( 1 ); }
/* * EwfSize */ static int EwfSize(void *p_handle, uint64_t *p_size) { pts_EwfHandle p_ewf_handle=(pts_EwfHandle)p_handle; #ifdef HAVE_LIBEWF_V2_API if(libewf_handle_get_media_size(p_ewf_handle->h_ewf,p_size,NULL)!=1) { #else if(libewf_get_media_size(p_ewf_handle->h_ewf,p_size)!=1) { #endif return EWF_GET_SIZE_FAILED; } return EWF_OK; } /* * EwfRead */ static int EwfRead(void *p_handle, char *p_buf, off_t offset, size_t count, size_t *p_read, int *p_errno) { pts_EwfHandle p_ewf_handle=(pts_EwfHandle)p_handle; // TODO: Return value of libewf_handle_read_buffer is ssize_t with -1 on error size_t bytes_read; #ifdef HAVE_LIBEWF_V2_API if(libewf_handle_seek_offset(p_ewf_handle->h_ewf, offset, SEEK_SET, NULL)!=-1) #else if(libewf_seek_offset(p_ewf_handle->h_ewf,offset)!=-1) #endif { #ifdef HAVE_LIBEWF_V2_API bytes_read=libewf_handle_read_buffer(p_ewf_handle->h_ewf, p_buf, count, NULL); #else bytes_read=libewf_read_buffer(p_ewf_handle->h_ewf,p_buf,count); #endif if(bytes_read!=count) return EWF_READ_FAILED; } else { return EWF_SEEK_FAILED; } *p_read=bytes_read; return EWF_OK; }
static ut64 ewf__lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) { if (RIOEWF_IS_VALID (fd)) { size64_t media_size; switch (whence) { case SEEK_SET: /* ignore */ break; case SEEK_CUR: offset += io->off; break; case SEEK_END: if (libewf_handle_get_media_size ( RIOEWF_HANDLE (fd), &media_size, NULL)) offset = media_size - offset; break; } libewf_handle_seek_offset (RIOEWF_HANDLE (fd), offset, whence, NULL); return offset; } return (ut64)-1; }
/* Tests seeking in a handle * Returns 1 if successful, 0 if not or -1 on error */ int ewf_test_seek_handle( libcstring_system_character_t *source, libcerror_error_t **error ) { libcstring_system_character_t **filenames = NULL; libewf_handle_t *handle = NULL; size64_t media_size = 0; size_t string_length = 0; int number_of_filenames = 0; int result = 0; #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) string_length = libcstring_wide_string_length( source ); if( libewf_glob_wide( source, string_length, LIBEWF_FORMAT_UNKNOWN, &filenames, &number_of_filenames, error ) != 1 ) #else string_length = libcstring_narrow_string_length( source ); if( libewf_glob( source, string_length, LIBEWF_FORMAT_UNKNOWN, &filenames, &number_of_filenames, error ) != 1 ) #endif { fprintf( stderr, "Unable to glob filenames.\n" ); goto on_error; } if( number_of_filenames < 0 ) { fprintf( stderr, "Invalid number of filenames.\n" ); goto on_error; } else if( number_of_filenames == 0 ) { fprintf( stderr, "Missing filenames.\n" ); goto on_error; } if( libewf_handle_initialize( &handle, error ) != 1 ) { fprintf( stderr, "Unable to create handle.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_open_wide( handle, (wchar_t * const *) filenames, number_of_filenames, LIBEWF_OPEN_READ, error ) != 1 ) #else if( libewf_handle_open( handle, (char * const *) filenames, number_of_filenames, LIBEWF_OPEN_READ, error ) != 1 ) #endif { fprintf( stderr, "Unable to open handle.\n" ); goto on_error; } if( libewf_handle_get_media_size( handle, &media_size, error ) != 1 ) { fprintf( stderr, "Unable to retrieve media size.\n" ); goto on_error; } result = ewf_test_seek( handle, media_size ); if( result == -1 ) { fprintf( stderr, "Unable to seek in handle.\n" ); goto on_error; } if( libewf_handle_close( handle, error ) != 0 ) { fprintf( stderr, "Unable to close handle.\n" ); goto on_error; } if( libewf_handle_free( &handle, error ) != 1 ) { fprintf( stderr, "Unable to free handle.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_glob_wide_free( filenames, number_of_filenames, error ) != 1 ) #else if( libewf_glob_free( filenames, number_of_filenames, error ) != 1 ) #endif { fprintf( stderr, "Unable to free glob.\n" ); goto on_error; } return( result ); on_error: if( handle != NULL ) { libewf_handle_close( handle, NULL ); libewf_handle_free( &handle, NULL ); } if( filenames != NULL ) { #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) libewf_glob_wide_free( filenames, number_of_filenames, NULL ); #else libewf_glob_free( filenames, number_of_filenames, NULL ); #endif } return( -1 ); }
TSK_IMG_INFO * ewf_open(int a_num_img, const TSK_TCHAR * const a_images[], unsigned int a_ssize) { #if defined( HAVE_LIBEWF_V2_API ) char error_string[TSK_EWF_ERROR_STRING_SIZE]; libewf_error_t *ewf_error = NULL; int result = 0; #elif !defined( LIBEWF_STRING_DIGEST_HASH_LENGTH_MD5 ) uint8_t md5_hash[16]; #endif IMG_EWF_INFO *ewf_info = NULL; TSK_IMG_INFO *img_info = NULL; #if !defined( HAVE_LIBEWF_V2_API) if (tsk_verbose) libewf_set_notify_values(stderr, 1); #endif if ((ewf_info = (IMG_EWF_INFO *) tsk_img_malloc(sizeof(IMG_EWF_INFO))) == NULL) { return NULL; } img_info = (TSK_IMG_INFO *) ewf_info; // See if they specified only the first of the set... ewf_info->used_ewf_glob = 0; if (a_num_img == 1) { #if defined( HAVE_LIBEWF_V2_API) #ifdef TSK_WIN32 if (libewf_glob_wide(a_images[0], TSTRLEN(a_images[0]), LIBEWF_FORMAT_UNKNOWN, &ewf_info->images, &ewf_info->num_imgs, &ewf_error) == -1) { #else if (libewf_glob(a_images[0], TSTRLEN(a_images[0]), LIBEWF_FORMAT_UNKNOWN, &ewf_info->images, &ewf_info->num_imgs, &ewf_error) == -1) { #endif tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_MAGIC); getError(ewf_error, error_string); tsk_error_set_errstr("ewf_open: Not an E01 glob name (%s)", error_string); libewf_error_free(&ewf_error); tsk_img_free(ewf_info); return NULL; } #else //use v1 #ifdef TSK_WIN32 ewf_info->num_imgs = libewf_glob_wide(a_images[0], TSTRLEN(a_images[0]), LIBEWF_FORMAT_UNKNOWN, &ewf_info->images); #else ewf_info->num_imgs = libewf_glob(a_images[0], TSTRLEN(a_images[0]), LIBEWF_FORMAT_UNKNOWN, &ewf_info->images); #endif if (ewf_info->num_imgs <= 0) { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_MAGIC); tsk_error_set_errstr("ewf_open: Not an E01 glob name"); tsk_img_free(ewf_info); return NULL; } #endif // end v1 ewf_info->used_ewf_glob = 1; if (tsk_verbose) tsk_fprintf(stderr, "ewf_open: found %d segment files via libewf_glob\n", ewf_info->num_imgs); } else { int i; ewf_info->num_imgs = a_num_img; if ((ewf_info->images = (TSK_TCHAR **) tsk_malloc(a_num_img * sizeof(TSK_TCHAR *))) == NULL) { tsk_img_free(ewf_info); return NULL; } for (i = 0; i < a_num_img; i++) { if ((ewf_info->images[i] = (TSK_TCHAR *) tsk_malloc((TSTRLEN(a_images[i]) + 1) * sizeof(TSK_TCHAR))) == NULL) { tsk_img_free(ewf_info); return NULL; } TSTRNCPY(ewf_info->images[i], a_images[i], TSTRLEN(a_images[i]) + 1); } } #if defined( HAVE_LIBEWF_V2_API ) // Check the file signature before we call the library open #if defined( TSK_WIN32 ) if (libewf_check_file_signature_wide(a_images[0], &ewf_error) != 1) #else if (libewf_check_file_signature(a_images[0], &ewf_error) != 1) #endif { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_MAGIC); getError(ewf_error, error_string); tsk_error_set_errstr("ewf_open: Not an EWF file (%s)", error_string); libewf_error_free(&ewf_error); tsk_img_free(ewf_info); if (tsk_verbose != 0) { tsk_fprintf(stderr, "Not an EWF file\n"); } return (NULL); } if (libewf_handle_initialize(&(ewf_info->handle), &ewf_error) != 1) { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_OPEN); getError(ewf_error, error_string); tsk_error_set_errstr("ewf_open file: %" PRIttocTSK ": Error initializing handle (%s)", a_images[0], error_string); libewf_error_free(&ewf_error); tsk_img_free(ewf_info); if (tsk_verbose != 0) { tsk_fprintf(stderr, "Unable to create EWF handle\n"); } return (NULL); } #if defined( TSK_WIN32 ) if (libewf_handle_open_wide(ewf_info->handle, (wchar_t * const *) ewf_info->images, ewf_info->num_imgs, LIBEWF_OPEN_READ, &ewf_error) != 1) #else if (libewf_handle_open(ewf_info->handle, (char *const *) ewf_info->images, ewf_info->num_imgs, LIBEWF_OPEN_READ, &ewf_error) != 1) #endif { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_OPEN); getError(ewf_error, error_string); tsk_error_set_errstr("ewf_open file: %" PRIttocTSK ": Error opening (%s)", a_images[0], error_string); libewf_error_free(&ewf_error); tsk_img_free(ewf_info); if (tsk_verbose != 0) { tsk_fprintf(stderr, "Error opening EWF file\n"); } return (NULL); } if (libewf_handle_get_media_size(ewf_info->handle, (size64_t *) & (img_info->size), &ewf_error) != 1) { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_OPEN); getError(ewf_error, error_string); tsk_error_set_errstr("ewf_open file: %" PRIttocTSK ": Error getting size of image (%s)", a_images[0], error_string); libewf_error_free(&ewf_error); tsk_img_free(ewf_info); if (tsk_verbose != 0) { tsk_fprintf(stderr, "Error getting size of EWF file\n"); } return (NULL); } result = libewf_handle_get_utf8_hash_value_md5(ewf_info->handle, (uint8_t *) ewf_info->md5hash, 33, &ewf_error); if (result == -1) { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_OPEN); getError(ewf_error, error_string); tsk_error_set_errstr("ewf_open file: %" PRIttocTSK ": Error getting MD5 of image (%s)", a_images[0], error_string); libewf_error_free(&ewf_error); tsk_img_free(ewf_info); if (tsk_verbose != 0) { tsk_fprintf(stderr, "Error getting size of EWF file\n"); } return (NULL); } ewf_info->md5hash_isset = result; #else // V1 API // Check the file signature before we call the library open #if defined( TSK_WIN32 ) if (libewf_check_file_signature_wide(a_images[0]) != 1) #else if (libewf_check_file_signature(a_images[0]) != 1) #endif { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_MAGIC); tsk_error_set_errstr("ewf_open: Not an EWF file"); tsk_img_free(ewf_info); if (tsk_verbose) tsk_fprintf(stderr, "Not an EWF file\n"); return NULL; } #if defined( TSK_WIN32 ) ewf_info->handle = libewf_open_wide( (wchar_t * const *) ewf_info->images, ewf_info->num_imgs, LIBEWF_OPEN_READ); #else ewf_info->handle = libewf_open( (char *const *) ewf_info->images, ewf_info->num_imgs, LIBEWF_OPEN_READ); #endif if (ewf_info->handle == NULL) { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_OPEN); tsk_error_set_errstr("ewf_open file: %" PRIttocTSK ": Error opening", ewf_info->images[0]); tsk_img_free(ewf_info); if (tsk_verbose != 0) { tsk_fprintf(stderr, "Error opening EWF file\n"); } return (NULL); } #if defined( LIBEWF_STRING_DIGEST_HASH_LENGTH_MD5 ) // 2007 version img_info->size = libewf_get_media_size(ewf_info->handle); ewf_info->md5hash_isset = libewf_get_stored_md5_hash(ewf_info->handle, ewf_info->md5hash, LIBEWF_STRING_DIGEST_HASH_LENGTH_MD5); #else // libewf-20080322 version if (libewf_get_media_size(ewf_info->handle, (size64_t *) & (img_info->size)) != 1) { tsk_error_reset(); tsk_error_set_errno(TSK_ERR_IMG_OPEN); tsk_error_set_errstr("ewf_open file: %" PRIttocTSK ": Error getting size of image", ewf_info->images[0]); tsk_img_free(ewf_info); if (tsk_verbose) { tsk_fprintf(stderr, "Error getting size of EWF file\n"); } return (NULL); } if (libewf_get_md5_hash(ewf_info->handle, md5_hash, 16) == 1) { int md5_string_iterator = 0; int md5_hash_iterator = 0; for (md5_hash_iterator = 0; md5_hash_iterator < 16; md5_hash_iterator++) { int digit = md5_hash[md5_hash_iterator] / 16; if (digit <= 9) { ewf_info->md5hash[md5_string_iterator++] = '0' + (char) digit; } else { ewf_info->md5hash[md5_string_iterator++] = 'a' + (char) (digit - 10); } digit = md5_hash[md5_hash_iterator] % 16; if (digit <= 9) { ewf_info->md5hash[md5_string_iterator++] = '0' + (char) digit; } else { ewf_info->md5hash[md5_string_iterator++] = 'a' + (char) (digit - 10); } } ewf_info->md5hash_isset = 1; } #endif /* defined( LIBEWF_STRING_DIGEST_HASH_LENGTH_MD5 ) */ #endif /* defined( HAVE_LIBEWF_V2_API ) */ if (a_ssize != 0) { img_info->sector_size = a_ssize; } else { img_info->sector_size = 512; } img_info->itype = TSK_IMG_TYPE_EWF_EWF; img_info->read = &ewf_image_read; img_info->close = &ewf_image_close; img_info->imgstat = &ewf_image_imgstat; // initialize the read lock tsk_init_lock(&(ewf_info->read_lock)); return (img_info); }
static RIODesc *ewf__open(RIO *io, const char *pathname, int rw, int mode) { RIOEwf *rewf; libewf_handle_t *ewf_h; // XXX filename list should be dynamic. 1024 limit is ugly const char *filenames[1024]; char *ptr,*optr; ut8 hash[1024]; size64_t media_size; uint32_t bytes_per_sector; //uint64_t amount_of_sectors; uint32_t error_granularity; //uint32_t amount_of_acquiry_errors; int8_t compression_level; uint8_t media_type; uint8_t media_flags; uint8_t compress_empty_block; uint8_t format; int i; if (!memcmp (pathname, "els://", 6)) { FILE *fd = r_sandbox_fopen (pathname+6, "r"); ut64 len; char *buf; if (fd == NULL) return NULL; fseek (fd, 0, SEEK_END); len = ftell (fd); fseek(fd, 0, SEEK_SET); buf = (char *)malloc (len); fread (buf, len, 1, fd); ptr = strchr (buf, '\n'); for (i=0, optr = buf; ptr&&(ptr=strchr(ptr, '\n')); optr=ptr) { *ptr = '\0'; ptr++; filenames[i++] = optr; } filenames[i] = NULL; free (buf); fclose (fd); for (i=0; filenames[i]; i++) eprintf ("%02x: %s)\n", i, filenames[i]); } else { filenames[0] = pathname + 6; filenames[1] = NULL; } libewf_handle_initialize (&ewf_h, NULL); if (libewf_handle_open (ewf_h, (char * const *)filenames, (int)1, rw? LIBEWF_OPEN_READ_WRITE: LIBEWF_OPEN_READ, NULL) != 1) return NULL; #if 0 if( ((libewf_internal_plugin_t*)ewf_h)->header_values == NULL ) { fprintf( stream, "\tNo information found in file.\n" ); } else { libewf_get_header_value_examiner_name(ewf_h, hash, 128); eprintf("ExaminerName: %s\n", hash); libewf_get_header_value_case_number(ewf_h, hash, 128); eprintf("CaseNumber: %s\n", hash); } #endif libewf_handle_get_format (ewf_h, &format, NULL); eprintf ("FormatVersion: %d\n", format); libewf_handle_get_compression_values (ewf_h, &compression_level, &compress_empty_block, NULL); eprintf ("CompressionLevel: %d\n", compression_level); libewf_handle_get_error_granularity (ewf_h, &error_granularity, NULL); eprintf ("ErrorGranurality: %d\n", error_granularity); //libewf_handle_get_number_of_sectors (ewf_h, &amount_of_sectors, NULL); //eprintf ("AmountOfSectors: %"PFMT64d"\n", amount_of_sectors); libewf_handle_get_bytes_per_sector (ewf_h, &bytes_per_sector, NULL); eprintf ("BytesPerSector: %d\n", bytes_per_sector); libewf_handle_get_media_size (ewf_h, &media_size, NULL); eprintf ("MediaSize: %"PFMT64d"\n", media_size); libewf_handle_get_media_type (ewf_h, &media_type, NULL); eprintf ("MediaType: %d\n", media_type); libewf_handle_get_media_flags (ewf_h, &media_flags, NULL); eprintf ("MediaFlags: %d\n", media_flags); libewf_handle_get_md5_hash (ewf_h, hash, 128, NULL); eprintf ("CalculatedHash: %s\n", hash); rewf = R_NEW (RIOEwf); rewf->handle = ewf_h; rewf->fd = RIOEWF_TO_FD (rewf); return r_io_desc_new (&r_io_plugin_shm, rewf->fd, pathname, rw, mode, rewf); }
int main( int argc, char * const argv[] ) #endif { libcerror_error_t *error = NULL; libewf_handle_t *handle = NULL; off64_t read_offset = 0; size64_t media_size = 0; size64_t read_size = 0; size32_t chunk_size = 0; if( argc < 2 ) { fprintf( stderr, "Missing filename(s).\n" ); return( EXIT_FAILURE ); } #if defined( HAVE_DEBUG_OUTPUT ) && defined( EWF_TEST_READ_VERBOSE ) libewf_notify_set_verbose( 1 ); libewf_notify_set_stream( stderr, NULL ); #endif /* Initialization */ if( libewf_handle_initialize( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to create handle.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_open_wide( handle, &( argv[ 1 ] ), argc - 1, LIBEWF_OPEN_READ, &error ) != 1 ) #else if( libewf_handle_open( handle, &( argv[ 1 ] ), argc - 1, LIBEWF_OPEN_READ, &error ) != 1 ) #endif { fprintf( stderr, "Unable to open file(s).\n" ); goto on_error; } if( libewf_handle_get_media_size( handle, &media_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve media size.\n" ); goto on_error; } if( media_size > (size64_t) INT64_MAX ) { fprintf( stderr, "Media size exceeds maximum.\n" ); goto on_error; } if( libewf_handle_get_chunk_size( handle, &chunk_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve chunk size.\n" ); goto on_error; } if( chunk_size == 0 ) { fprintf( stderr, "Invalid chunk size.\n" ); goto on_error; } fprintf( stdout, "Media size: %" PRIu64 " bytes\n", media_size ); /* Case 0: test full read */ /* Test: offset: 0 size: <media_size> * Expected result: offset: 0 size: <media_size> */ read_offset = 0; read_size = media_size; if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } /* Case 1: test random read */ /* Test: offset: <media_size / 7> size: <media_size / 2> * Expected result: offset: <media_size / 7> size: <media_size / 2> */ read_offset = (off64_t) ( media_size / 7 ); read_size = media_size / 2; if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } /* Case 2: test read buffer beyond media size */ if( media_size < 1024 ) { /* Test: offset: <media_size - 1024> size: 4096 * Expected result: offset: -1 size: <undetermined> */ read_offset = (off64_t) ( media_size - 1024 ); read_size = 4096; if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } } else { /* Test: offset: <media_size - 1024> size: 4096 * Expected result: offset: <media_size - 1024> size: 1024 */ read_offset = (off64_t) ( media_size - 1024 ); read_size = 4096; if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, 1024 ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, 1024 ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } } fprintf( stdout, "\nChunk size: %" PRIu32 " bytes\n", chunk_size ); /* Case 0: test full read */ /* Test: offset: 0 size: <media_size> * Expected result: offset: 0 size: <media_size> */ read_offset = 0; read_size = media_size; if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } /* Case 1: test random read */ /* Test: offset: <( ( media_size / 7 ) / chunk_size ) * chunk_size> size: <( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size> * Expected result: offset: <( ( media_size / 7 ) / chunk_size ) * chunk_size> size: <( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size> */ read_offset = (off64_t) ( ( media_size / 7 ) / chunk_size ) * chunk_size; read_size = ( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size; if( media_size == 0 ) { if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, 0 ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, 0 ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } } else { if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } } /* Case 2: test read chunk beyond media size */ if( media_size < 1024 ) { /* Test: offset: <media_size - 1024> size: chunk_size * Expected result: offset: -1 size: <undetermined> */ read_offset = (off64_t) ( media_size - 1024 ); read_size = chunk_size; if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } } else { /* Test: offset: <media_size - 1024> size: chunk_size * Expected result: offset: <media_size - 1024> size: chunk size or media_size % chunk_size */ read_offset = (off64_t) ( media_size - 1024 ); read_size = chunk_size; if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, (off64_t) ( media_size - 1024 ), ( ( media_size % chunk_size ) == 0 ) ? chunk_size : media_size % chunk_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, (off64_t) ( media_size - 1024 ), ( ( media_size % chunk_size ) == 0 ) ? chunk_size : media_size % chunk_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } } /* Clean up */ if( libewf_handle_close( handle, &error ) != 0 ) { fprintf( stderr, "Unable to close file(s).\n" ); goto on_error; } if( libewf_handle_free( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to free handle.\n" ); goto on_error; } return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libewf_error_backtrace_fprint( error, stderr ); libewf_error_free( &error ); } if( handle != NULL ) { libewf_handle_close( handle, NULL ); libewf_handle_free( &handle, NULL ); } return( EXIT_FAILURE ); }
disk_t *fewf_init(const char *device, const int mode) { unsigned int num_files=0; char **filenames= NULL; disk_t *disk=NULL; struct info_fewf_struct *data; #if !defined( HAVE_LIBEWF_V2_API ) && defined( HAVE_GLOB_H ) glob_t globbuf; #endif data=(struct info_fewf_struct *)MALLOC(sizeof(struct info_fewf_struct)); memset(data, 0, sizeof(struct info_fewf_struct)); data->file_name = strdup(device); data->handle=NULL; data->mode = mode; #ifdef DEBUG_EWF #if defined( HAVE_LIBEWF_V2_API ) libewf_notify_set_stream( stderr, NULL ); libewf_notify_set_verbose( 1 ); #else libewf_set_notify_values( stderr, 1 ); #endif #endif #if defined( HAVE_LIBEWF_V2_API ) if( libewf_glob( data->file_name, strlen(data->file_name), LIBEWF_FORMAT_UNKNOWN, &filenames, (int *)&num_files, NULL ) != 1 ) { log_error("libewf_glob failed\n"); free(data); return NULL; } #elif defined( HAVE_GLOB_H ) { globbuf.gl_offs = 0; glob(data->file_name, GLOB_DOOFFS, NULL, &globbuf); if(globbuf.gl_pathc>0) { filenames=(char **)MALLOC(globbuf.gl_pathc * sizeof(*filenames)); for (num_files=0; num_files<globbuf.gl_pathc; num_files++) { filenames[num_files]=globbuf.gl_pathv[num_files]; } } } if(filenames==NULL) { globfree(&globbuf); free(data); return NULL; } #else { filenames=(char **)MALLOC(1*sizeof(*filenames)); filenames[num_files] = data->file_name; num_files++; } #endif if((mode&TESTDISK_O_RDWR)==TESTDISK_O_RDWR) { #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_initialize( &( data->handle ), NULL ) != 1 ) { log_error("libewf_handle_initialize failed\n"); libewf_glob_free( filenames, num_files, NULL ); free(data); return NULL; } if( libewf_handle_open( data->handle, filenames, num_files, LIBEWF_OPEN_READ_WRITE, NULL ) != 1 ) { log_error("libewf_handle_open(%s) failed\n", device); } #else data->handle=libewf_open(filenames, num_files, LIBEWF_OPEN_READ_WRITE); if(data->handle==NULL) { log_error("libewf_open(%s) failed\n", device); } #endif /* defined( HAVE_LIBEWF_V2_API ) */ } if(data->handle==NULL) { data->mode&=~TESTDISK_O_RDWR; #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_initialize( &( data->handle ), NULL ) != 1 ) { log_error("libewf_handle_initialize failed\n"); libewf_glob_free( filenames, num_files, NULL ); free(data); return NULL; } if( libewf_handle_open( data->handle, filenames, num_files, LIBEWF_OPEN_READ, NULL ) != 1 ) { log_error("libewf_handle_open(%s) failed\n", device); libewf_handle_free( &( data->handle ), NULL ); libewf_glob_free( filenames, num_files, NULL ); free(data); return NULL; } #else data->handle=libewf_open(filenames, num_files, LIBEWF_OPEN_READ); if(data->handle==NULL) { log_error("libewf_open(%s) failed\n", device); #if defined( HAVE_GLOB_H ) globfree(&globbuf); #endif free(filenames); free(data); return NULL; } #endif /* defined( HAVE_LIBEWF_V2_API ) */ } #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_set_header_values_date_format( data->handle, LIBEWF_DATE_FORMAT_DAYMONTH, NULL ) != 1 ) { log_error("%s Unable to set header values date format\n", device); } #else if( libewf_parse_header_values( data->handle, LIBEWF_DATE_FORMAT_DAYMONTH) != 1 ) { log_error("%s Unable to parse EWF header values\n", device); } #endif disk=(disk_t *)MALLOC(sizeof(*disk)); init_disk(disk); disk->arch=&arch_none; disk->device=strdup(device); disk->data=data; disk->description=fewf_description; disk->description_short=fewf_description_short; disk->pread_fast=fewf_pread_fast; disk->pread=fewf_pread; disk->pwrite=(data->mode&TESTDISK_O_RDWR?fewf_pwrite:fewf_nopwrite); disk->sync=fewf_sync; disk->access_mode=(data->mode&TESTDISK_O_RDWR); disk->clean=fewf_clean; #if defined( HAVE_LIBEWF_V2_API ) || defined( LIBEWF_GET_BYTES_PER_SECTOR_HAVE_TWO_ARGUMENTS ) { uint32_t bytes_per_sector = 0; #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_get_bytes_per_sector( data->handle, &bytes_per_sector, NULL ) != 1 ) #else if( libewf_get_bytes_per_sector(data->handle, &bytes_per_sector)<0) #endif { disk->sector_size=DEFAULT_SECTOR_SIZE; } else { disk->sector_size=bytes_per_sector; } } #else disk->sector_size=libewf_get_bytes_per_sector(data->handle); #endif // printf("libewf_get_bytes_per_sector %u\n",disk->sector_size); if(disk->sector_size==0) disk->sector_size=DEFAULT_SECTOR_SIZE; /* Set geometry */ disk->geom.cylinders=0; disk->geom.heads_per_cylinder=1; disk->geom.sectors_per_head=1; disk->geom.bytes_per_sector=disk->sector_size; /* Get disk_real_size */ #if defined( HAVE_LIBEWF_V2_API ) || defined( LIBEWF_GET_MEDIA_SIZE_HAVE_TWO_ARGUMENTS ) { size64_t media_size = 0; #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_get_media_size( data->handle, &media_size, NULL ) != 1 ) #else if(libewf_get_media_size(data->handle, &media_size)<0) #endif { disk->disk_real_size=0; } else { disk->disk_real_size=media_size; } } #else disk->disk_real_size=libewf_get_media_size(data->handle); #endif update_disk_car_fields(disk); #if defined( HAVE_LIBEWF_V2_API ) libewf_glob_free( filenames, num_files, NULL ); #else #if defined( HAVE_GLOB_H ) globfree(&globbuf); #endif free(filenames); #endif return disk; }
int main( int argc, char * const argv[] ) #endif { libcstring_system_character_t *target_filename = NULL; liberror_error_t *error = NULL; libewf_handle_t *handle = NULL; libcstring_system_integer_t option = 0; off64_t read_offset = 0; size64_t media_size = 0; size64_t read_size = 0; size32_t chunk_size = 0; size_t delta_segment_filename_length = 0; while( ( option = libsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "t:" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case (libcstring_system_integer_t) '?': default: fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM ".\n", argv[ optind - 1 ] ); return( EXIT_FAILURE ); case (libcstring_system_integer_t) 't': target_filename = optarg; break; } } if( optind == argc ) { fprintf( stderr, "Missing EWF image filename(s).\n" ); return( EXIT_FAILURE ); } /* Initialization */ if( libewf_handle_initialize( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to create handle.\n" ); goto on_error; } /* #if defined( HAVE_DEBUG_OUTPUT ) libewf_notify_set_verbose( 1 ); libewf_notify_set_stream( stderr, NULL ); #endif */ #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_open_wide( handle, &( argv[ optind ] ), argc - optind, LIBEWF_OPEN_READ_WRITE, &error ) != 1 ) #else if( libewf_handle_open( handle, &( argv[ optind ] ), argc - optind, LIBEWF_OPEN_READ_WRITE, &error ) != 1 ) #endif { fprintf( stderr, "Unable to open file(s).\n" ); goto on_error; } if( target_filename != NULL ) { delta_segment_filename_length = libcstring_system_string_length( target_filename ); #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_set_delta_segment_filename_wide( handle, target_filename, delta_segment_filename_length, &error ) != 1 ) #else if( libewf_handle_set_delta_segment_filename( handle, target_filename, delta_segment_filename_length, &error ) != 1 ) #endif { fprintf( stderr, "Unable to set delta segment filename.\n" ); goto on_error; } } if( libewf_handle_get_media_size( handle, &media_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve media size.\n" ); goto on_error; } if( media_size > (size64_t) INT64_MAX ) { fprintf( stderr, "Media size exceeds maximum.\n" ); goto on_error; } if( libewf_handle_get_chunk_size( handle, &chunk_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve chunk size.\n" ); goto on_error; } if( chunk_size == 0 ) { fprintf( stderr, "Invalid chunk size.\n" ); goto on_error; } fprintf( stdout, "Media size: %" PRIu64 " bytes\n", media_size ); /* Case 0: test full read */ /* Test: offset: 0 size: <media_size> * Expected result: offset: 0 size: <media_size> */ read_offset = 0; read_size = media_size; if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } /* Case 1: test random read */ /* Test: offset: <media_size / 7> size: <media_size / 2> * Expected result: offset: <media_size / 7> size: <media_size / 2> */ read_offset = (off64_t) ( media_size / 7 ); read_size = media_size / 2; if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } /* Case 2: test read buffer beyond media size */ if( media_size < 1024 ) { /* Test: offset: <media_size - 1024> size: 4096 * Expected result: offset: -1 size: <undetermined> */ read_offset = (off64_t) ( media_size - 1024 ); read_size = 4096; if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } } else { /* Test: offset: <media_size - 1024> size: 4096 * Expected result: offset: <media_size - 1024> size: 1024 */ read_offset = (off64_t) ( media_size - 1024 ); read_size = 4096; if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, 1024 ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, 1024 ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } } fprintf( stdout, "\nChunk size: %" PRIu32 " bytes\n", chunk_size ); /* Case 0: test full read */ /* Test: offset: 0 size: <media_size> * Expected result: offset: 0 size: <media_size> */ read_offset = 0; read_size = media_size; if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } /* Case 1: test random read */ /* Test: offset: <( ( media_size / 7 ) / chunk_size ) * chunk_size> size: <( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size> * Expected result: offset: <( ( media_size / 7 ) / chunk_size ) * chunk_size> size: <( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size> */ read_offset = (off64_t) ( ( media_size / 7 ) / chunk_size ) * chunk_size; read_size = ( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size; if( media_size == 0 ) { if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, 0 ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, 0 ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } } else { if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } } /* Case 2: test read chunk beyond media size */ if( media_size < 1024 ) { /* Test: offset: <media_size - 1024> size: chunk_size * Expected result: offset: -1 size: <undetermined> */ read_offset = (off64_t) ( media_size - 1024 ); read_size = chunk_size; if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } } else { /* Test: offset: <media_size - 1024> size: chunk_size * Expected result: offset: <media_size - 1024> size: chunk size or media_size % chunk_size */ read_offset = (off64_t) ( media_size - 1024 ); read_size = chunk_size; if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, (off64_t) ( media_size - 1024 ), ( ( media_size % chunk_size ) == 0 ) ? chunk_size : media_size % chunk_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, (off64_t) ( media_size - 1024 ), ( ( media_size % chunk_size ) == 0 ) ? chunk_size : media_size % chunk_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } } /* Clean up */ if( libewf_handle_close( handle, &error ) != 0 ) { fprintf( stderr, "Unable to close file(s).\n" ); goto on_error; } if( libewf_handle_free( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to free handle.\n" ); goto on_error; } return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libewf_error_backtrace_fprint( error, stderr ); libewf_error_free( &error ); } if( handle != NULL ) { libewf_handle_close( handle, NULL ); libewf_handle_free( &handle, NULL ); } return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcstring_system_character_t **filenames = NULL; libewf_error_t *error = NULL; libewf_handle_t *handle = NULL; size64_t media_size = 0; int number_of_filenames = 0; if( argc < 2 ) { fprintf( stderr, "Missing filename(s).\n" ); return( EXIT_FAILURE ); } #if defined( HAVE_DEBUG_OUTPUT ) && defined( EWF_TEST_SEEK_VERBOSE ) libewf_notify_set_verbose( 1 ); libewf_notify_set_stream( stderr, NULL ); #endif #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_glob_wide( argv[ 1 ], libcstring_wide_string_length( argv[ 1 ] ), LIBEWF_FORMAT_UNKNOWN, &filenames, &number_of_filenames, &error ) != 1 ) #else if( libewf_glob( argv[ 1 ], libcstring_narrow_string_length( argv[ 1 ] ), LIBEWF_FORMAT_UNKNOWN, &filenames, &number_of_filenames, &error ) != 1 ) #endif { fprintf( stderr, "Unable to glob filenames.\n" ); goto on_error; } if( number_of_filenames < 0 ) { fprintf( stderr, "Invalid number of filenames.\n" ); goto on_error; } else if( number_of_filenames == 0 ) { fprintf( stderr, "Missing filenames.\n" ); goto on_error; } /* Initialization */ if( libewf_handle_initialize( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to create handle.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_open_wide( handle, filenames, number_of_filenames, LIBEWF_OPEN_READ, &error ) != 1 ) #else if( libewf_handle_open( handle, filenames, number_of_filenames, LIBEWF_OPEN_READ, &error ) != 1 ) #endif { fprintf( stderr, "Unable to open handle.\n" ); goto on_error; } if( libewf_handle_get_media_size( handle, &media_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve media size.\n" ); goto on_error; } if( ewf_handle_test_seek( handle, media_size ) != 1 ) { fprintf( stderr, "Unable to seek in handle.\n" ); goto on_error; } /* Clean up */ if( libewf_handle_close( handle, &error ) != 0 ) { fprintf( stderr, "Unable to close handle.\n" ); goto on_error; } if( libewf_handle_free( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to free handle.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_glob_wide_free( filenames, number_of_filenames, &error ) != 1 ) #else if( libewf_glob_free( filenames, number_of_filenames, &error ) != 1 ) #endif { fprintf( stderr, "Unable to free glob.\n" ); goto on_error; } return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libewf_error_backtrace_fprint( error, stderr ); libewf_error_free( &error ); } if( handle != NULL ) { libewf_handle_close( handle, NULL ); libewf_handle_free( &handle, NULL ); } if( filenames != NULL ) { #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) libewf_glob_wide_free( filenames, number_of_filenames, NULL ); #else libewf_glob_free( filenames, number_of_filenames, NULL ); #endif } return( EXIT_FAILURE ); }
/* * EwfOpen */ static int EwfOpen(void *p_handle, const char **pp_filename_arr, uint64_t filename_arr_len) { pts_EwfHandle p_ewf_handle=(pts_EwfHandle)p_handle; // We need at least one file if(filename_arr_len==0) return EWF_NO_INPUT_FILES; // Make sure all files are EWF files for(uint64_t i=0;i<filename_arr_len;i++) { #ifdef HAVE_LIBEWF_V2_API if(libewf_check_file_signature(pp_filename_arr[i],NULL)!=1) #else if(libewf_check_file_signature(pp_filename_arr[i])!=1) #endif { return EWF_INVALID_INPUT_FILES; } } // Open EWF file #ifdef HAVE_LIBEWF_V2_API if(libewf_handle_open(p_ewf_handle->h_ewf, (char* const*)pp_filename_arr, filename_arr_len, libewf_get_access_flags_read(), NULL)!=1) #else p_ewf_handle->h_ewf=libewf_open((char* const*)pp_filename_arr, filename_arr_len, libewf_get_flags_read()); if(p_ewf_handle->h_ewf==NULL) #endif { return EWF_OPEN_FAILED; } #ifdef HAVE_LIBEWF_V2_API // Try to read 1 byte from the image end to verify that all segments were // specified (Only needed because libewf_handle_open() won't fail even if not // all segments were specified!) uint64_t image_size=0; char buf; if(libewf_handle_get_media_size(p_ewf_handle->h_ewf,&image_size,NULL)!=1) { return EWF_GET_SIZE_FAILED; } if(image_size==0) return EWF_OK; LIBXMOUNT_LOG_DEBUG(p_ewf_handle->debug, "Trying to read last byte of image at offset %" PRIu64 " (image size = %" PRIu64 " bytes)\n", image_size-1, image_size); if(libewf_handle_seek_offset(p_ewf_handle->h_ewf, image_size-1, SEEK_SET, NULL)==-1) { return EWF_OPEN_FAILED_SEEK; } if(libewf_handle_read_buffer(p_ewf_handle->h_ewf,&buf,1,NULL)!=1) { return EWF_OPEN_FAILED_READ; } #endif #ifndef HAVE_LIBEWF_V2_API // Parse EWF header if(libewf_parse_header_values(p_ewf_handle->h_ewf, LIBEWF_DATE_FORMAT_ISO8601)!=1) { return EWF_HEADER_PARSING_FAILED; } #endif return EWF_OK; }
/* Retrieves the size of the media data * Returns a Python object holding the offset if successful or NULL on error */ PyObject *pyewf_handle_get_media_size( pyewf_handle_t *pyewf_handle ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; libcerror_error_t *error = NULL; static char *function = "pyewf_handle_get_media_size"; size64_t media_size = 0; int result = 0; if( pyewf_handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid handle.", function ); return( NULL ); } Py_BEGIN_ALLOW_THREADS result = libewf_handle_get_media_size( pyewf_handle->handle, &media_size, &error ); Py_END_ALLOW_THREADS if( result != 1 ) { if( libcerror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to retrieve media size.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to retrieve media size.\n%s", function, error_string ); } libcerror_error_free( &error ); return( NULL ); } #if defined( HAVE_LONG_LONG ) if( media_size > (size64_t) LLONG_MAX ) { PyErr_Format( PyExc_OverflowError, "%s: media size value exceeds maximum.", function ); return( NULL ); } return( PyLong_FromLongLong( (long long) media_size ) ); #else if( media_size > (size64_t) LONG_MAX ) { PyErr_Format( PyExc_OverflowError, "%s: media size value exceeds maximum.", function ); return( NULL ); } return( PyLong_FromLong( (long) media_size ) ); #endif }