Example #1
0
static int ewf__close(RIODesc *fd) {
	if (RIOEWF_IS_VALID (fd)) {
		libewf_handle_close (RIOEWF_HANDLE (fd), NULL);
		return 0;
	}
	return -1;
}
Example #2
0
File: ewf.c Project: 0xkasun/OpenDF
static void
ewf_image_close(TSK_IMG_INFO * img_info)
{
    int i;
    IMG_EWF_INFO *ewf_info = (IMG_EWF_INFO *) img_info;

#if defined ( HAVE_LIBEWF_V2_API)
    libewf_handle_close(ewf_info->handle, NULL);
    libewf_handle_free(&(ewf_info->handle), NULL);

#else
    libewf_close(ewf_info->handle);
#endif

    // this stuff crashes if we used glob. v2 of the API has a free method.
    // not clear from the docs what we should do in v1...
    // @@@ Probably a memory leak in v1 unless libewf_close deals with it
    if (ewf_info->used_ewf_glob == 0) {
        for (i = 0; i < ewf_info->num_imgs; i++) {
            free(ewf_info->images[i]);
        }
        free(ewf_info->images);
    }
    else {
        libewf_error_t *error;
        libewf_glob_free( ewf_info->images, ewf_info->num_imgs, &error);
    }

    tsk_deinit_lock(&(ewf_info->read_lock));
    tsk_img_free(img_info);
}
Example #3
0
/* Closes the mount handle
 * Returns the 0 if succesful or -1 on error
 */
int mount_handle_close(
     mount_handle_t *mount_handle,
     libcerror_error_t **error )
{
	static char *function = "mount_handle_close";

	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_close(
	     mount_handle->input_handle,
	     error ) != 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_IO,
		 LIBCERROR_IO_ERROR_CLOSE_FAILED,
		 "%s: unable to close input handle.",
		 function );

		return( -1 );
	}
	return( 0 );
}
Example #4
0
File: ewf.c Project: Tom9X/TestDisk
static int fewf_clean(disk_t *disk)
{
  if(disk->data!=NULL)
  {
    struct info_fewf_struct *data=(struct info_fewf_struct *)disk->data;
#if defined( HAVE_LIBEWF_V2_API )
    libewf_handle_close(
     data->handle,
     NULL);
    libewf_handle_free(
     &( data->handle ),
     NULL );
#else
    libewf_close(data->handle);
#endif
    free(data->file_name);
    data->file_name=NULL;

    free(data->buffer);
    data->buffer=NULL;

    free(disk->data);
    disk->data=NULL;
  }
  return 0;
}
Example #5
0
/* Closes EWF file(s)
 * Returns a Python object if successful or NULL on error
 */
PyObject *pyewf_handle_close(
           pyewf_handle_t *pyewf_handle )
{
	char error_string[ PYEWF_ERROR_STRING_SIZE ];

	libcerror_error_t *error = NULL;
	static char *function    = "pyewf_handle_close";
	int result               = 0;

	if( pyewf_handle == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid pyewf handle.",
		 function );

		return( NULL );
	}
	Py_BEGIN_ALLOW_THREADS

	result = libewf_handle_close(
	          pyewf_handle->handle,
	          &error );

	Py_END_ALLOW_THREADS

	if( result != 0 )
	{
		if( libcerror_error_backtrace_sprint(
		     error,
		     error_string,
		     PYEWF_ERROR_STRING_SIZE ) == -1 )
		{
			PyErr_Format(
			 PyExc_IOError,
			 "%s: unable to close handle.",
			 function );
		}
		else
		{
			PyErr_Format(
			 PyExc_IOError,
			 "%s: unable to close handle.\n%s",
			 function,
			 error_string );
		}
		libcerror_error_free(
		 &error );

		return( NULL );
	}
	Py_IncRef(
	 Py_None );

	return( Py_None );
}
Example #6
0
/* Closes EWF file(s)
 * Returns a Python object if successful or NULL on error
 */
PyObject *pyewf_handle_close(
           pyewf_handle_t *pyewf_handle )
{
	char error_string[ PYEWF_ERROR_STRING_SIZE ];

	liberror_error_t *error = NULL;
	static char *function   = "pyewf_handle_close";

	if( pyewf_handle == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid pyewf handle.",
		 function );

		return( NULL );
	}
	if( pyewf_handle->handle == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid pyewf handle - missing libewf handle.",
		 function );

		return( NULL );
	}
	if( libewf_handle_close(
	     pyewf_handle->handle,
	     &error ) != 0 )
	{
		if( liberror_error_backtrace_sprint(
		     error,
		     error_string,
		     PYEWF_ERROR_STRING_SIZE ) == -1 )
		{
			PyErr_Format(
			 PyExc_IOError,
			 "%s: unable to close handle.",
			 function );
		}
		else
		{
			PyErr_Format(
			 PyExc_IOError,
			 "%s: unable to close handle.\n%s",
			 function,
			 error_string );
		}
		liberror_error_free(
		 &error );

		return( NULL );
	}
	return( Py_None );
}
/*
 * EwfClose
 */
static int EwfClose(void *p_handle) {
  pts_EwfHandle p_ewf_handle=(pts_EwfHandle)p_handle;

  // Close EWF handle
#ifdef HAVE_LIBEWF_V2_API
  if(libewf_handle_close(p_ewf_handle->h_ewf,NULL)!=0)
#else
  if(libewf_close(p_ewf_handle->h_ewf)!=0)
#endif
  {
    return EWF_CLOSE_FAILED;
  }

  return EWF_OK;
}
Example #8
0
/* Tests writing data of media size to EWF file(s) with a maximum segment size
 * Return 1 if successful, 0 if not or -1 on error
 */
int ewf_test_write_chunk(
     const libcstring_system_character_t *filename,
     size64_t media_size,
     size64_t maximum_segment_size,
     int8_t compression_level,
     uint8_t compression_flags,
     libcerror_error_t **error )
{
	libewf_handle_t *handle             = NULL;
	uint8_t *checksum_buffer            = NULL;
	uint8_t *chunk_buffer               = NULL;
	uint8_t *compressed_chunk_buffer    = NULL;
	static char *function               = "ewf_test_write_chunk";
	size_t chunk_buffer_size            = 0;
	size_t compressed_chunk_buffer_size = 0;
	ssize_t process_count               = 0;
	ssize_t write_count                 = 0;
	uint32_t chunk_checksum             = 0;
	uint32_t sectors_per_chunk          = 0;
	int8_t is_compressed                = 0;
	int8_t process_checksum             = 0;
	int sector_iterator                 = 0;

	if( libewf_handle_initialize(
	     &handle,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
		 "%s: unable to create handle.",
		 function );

		goto on_error;
	}
#if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER )
	if( libewf_handle_open_wide(
	     handle,
	     (wchar_t * const *) &filename,
	     1,
	     LIBEWF_OPEN_WRITE,
	     error ) != 1 )
#else
	if( libewf_handle_open(
	     handle,
	     (char * const *) &filename,
	     1,
	     LIBEWF_OPEN_WRITE,
	     error ) != 1 )
#endif
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_IO,
		 LIBCERROR_IO_ERROR_OPEN_FAILED,
		 "%s: unable to open handle.",
		 function );

		goto on_error;
	}
	if( media_size > 0 )
	{
		if( libewf_handle_set_media_size(
		     handle,
		     media_size,
		     error ) != 1 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
			 "%s: unable set media size.",
			 function );

			goto on_error;
		}
	}
	if( maximum_segment_size > 0 )
	{
		if( libewf_handle_set_maximum_segment_size(
		     handle,
		     maximum_segment_size,
		     error ) != 1 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
			 "%s: unable set maximum segment size.",
			 function );

			goto on_error;
		}
	}
	if( libewf_handle_set_compression_values(
	     handle,
	     compression_level,
	     compression_flags,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
		 "%s: unable set compression values.",
		 function );

		goto on_error;
	}
	sectors_per_chunk = 64;

	if( libewf_handle_set_sectors_per_chunk(
	     handle,
	     sectors_per_chunk,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
		 "%s: unable set sectors per chunk.",
		 function );

		goto on_error;
	}
	chunk_buffer_size = sectors_per_chunk * 512;

	/* Use the chunck buffer also as checksum buffer
	 */
	chunk_buffer = (uint8_t *) memory_allocate(
	                            sizeof( uint8_t ) * ( chunk_buffer_size + 4 ) );

	/* The compressed data can become larger than the uncompressed data
	 */
	compressed_chunk_buffer_size = chunk_buffer_size * 2;

	compressed_chunk_buffer = (uint8_t *) memory_allocate(
	                                       sizeof( uint8_t ) * compressed_chunk_buffer_size );

	if( chunk_buffer != NULL )
	{
		checksum_buffer = &( chunk_buffer[ chunk_buffer_size - 1 ] );
	}
	for( sector_iterator = 0;
	     sector_iterator < 26;
	     sector_iterator++ )
	{
		if( memory_set(
		     chunk_buffer,
		     (int) 'A' + sector_iterator,
		     chunk_buffer_size ) == NULL )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_MEMORY,
			 LIBCERROR_MEMORY_ERROR_SET_FAILED,
			 "%s: unable set value in chunk buffer.",
			 function );

			goto on_error;
		}
		process_count = libewf_handle_prepare_write_chunk(
				 handle,
				 chunk_buffer,
				 chunk_buffer_size,
				 compressed_chunk_buffer,
				 &compressed_chunk_buffer_size,
				 &is_compressed,
				 &chunk_checksum,
				 &process_checksum,
				 error );

		if( process_count == -1 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
			 "%s: unable to prepare chunk buffer before writing.",
			 function );

			goto on_error;
		}
		if( is_compressed == 0 )
		{
			write_count = libewf_handle_write_chunk(
				       handle,
				       chunk_buffer,
				       chunk_buffer_size,
				       chunk_buffer_size,
				       is_compressed,
				       checksum_buffer,
				       chunk_checksum,
				       process_checksum,
				       error );
		}
		else
		{
			write_count = libewf_handle_write_chunk(
				       handle,
				       compressed_chunk_buffer,
				       compressed_chunk_buffer_size,
				       chunk_buffer_size,
				       is_compressed,
				       checksum_buffer,
				       chunk_checksum,
				       process_checksum,
				       error );
		}
		if( write_count < 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_IO,
			 LIBCERROR_IO_ERROR_WRITE_FAILED,
			 "%s: unable write chunk of size: %" PRIzd ".",
			 function,
			 chunk_buffer_size );

			goto on_error;
		}
		if( media_size > (size64_t) chunk_buffer_size )
		{
			media_size -= chunk_buffer_size;
		}
		else if( media_size > 0 )
		{
			media_size = 0;
		}
		if( media_size == 0 )
		{
			break;
		}
	}
	memory_free(
	 compressed_chunk_buffer );

	compressed_chunk_buffer = NULL;
	
	memory_free(
	 chunk_buffer );

	chunk_buffer = NULL;
	
	if( libewf_handle_close(
	     handle,
	     error ) != 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_IO,
		 LIBCERROR_IO_ERROR_CLOSE_FAILED,
		 "%s: unable to close handle.",
		 function );

		goto on_error;
	}
	if( libewf_handle_free(
	     &handle,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
		 "%s: unable to free handle.",
		 function );

		goto on_error;
	}
	return( 1 );

on_error:
	if( compressed_chunk_buffer != NULL )
	{
		memory_free(
		 compressed_chunk_buffer );
	}
	if( chunk_buffer != NULL )
	{
		memory_free(
		 chunk_buffer );
	}
	if( handle != NULL )
	{
		libewf_handle_close(
		 handle,
		 NULL );
		libewf_handle_free(
		 &handle,
		 NULL );
	}
	return( -1 );
}
Example #9
0
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 );
}
Example #10
0
/* Closes the mount handle
 * Returns the 0 if succesful or -1 on error
 */
int mount_handle_close(
     mount_handle_t *mount_handle,
     libcerror_error_t **error )
{
	libewf_handle_t *ewf_handle = NULL;
	static char *function       = "mount_handle_close";

	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( mount_file_system_get_handle(
	     mount_handle->file_system,
	     &ewf_handle,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
		 "%s: unable to retrieve handle from file system.",
		 function );

		goto on_error;
	}
	if( mount_file_system_set_handle(
	     mount_handle->file_system,
	     NULL,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
		 "%s: unable to set handle in file system.",
		 function );

		ewf_handle = NULL;

		goto on_error;
	}
	if( libewf_handle_close(
	     ewf_handle,
	     error ) != 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_IO,
		 LIBCERROR_IO_ERROR_CLOSE_FAILED,
		 "%s: unable to close handle.",
		 function );

		goto on_error;
	}
	if( libewf_handle_free(
	     &ewf_handle,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
		 "%s: unable to free handle.",
		 function );

		goto on_error;
	}
	return( 0 );

on_error:
	if( ewf_handle != NULL )
	{
		libewf_handle_free(
		 &ewf_handle,
		 NULL );
	}
	return( -1 );
}
Example #11
0
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 );
}
Example #12
0
int main( int argc, char * const argv[] )
{
	libewf_error_t *error   = NULL;
	libewf_handle_t *handle = NULL;

	if( argc <= 1 )
	{
		fprintf(
		 stderr,
		 "Usage: ./open_close filename(s)\n" );

		return( EXIT_FAILURE );
	}
	/* The function will return 1 if successful or -1 on error.
	 * On error the error 'object' is created by the library.
	 *
	 * handle must refer to NULL to create a new libewf handle 'object'.
	 *
	 * If error is NULL e.g. libewf_handle_initialize( &handle, NULL )
	 * no error 'object' is created
	 *
	 * The error 'object' can be freed by libewf_error_free()
	 */
	if( libewf_handle_initialize(
	     &handle,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to initialize handle.\n" );

		goto on_error;
	}
	if( libewf_handle_open(
	     handle,
	     &( argv[ 1 ] ),
	     argc - 1,
	     LIBEWF_OPEN_READ,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to open file(s).\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;
	}
	return( EXIT_SUCCESS );

on_error:
	if( error != NULL )
	{
		libewf_error_backtrace_fprint(
		 error,
		 stderr );
		libewf_error_free(
		 &error );
	}
	if( handle != NULL )
	{
		libewf_handle_free(
		 &handle,
		 NULL );
	}
	return( EXIT_FAILURE );
}
Example #13
0
/* Tests writing data of media size to EWF file(s) with a maximum segment size
 * Return 1 if successful, 0 if not or -1 on error
 */
int ewf_test_write(
     const char *filename,
     size64_t media_size,
     size64_t maximum_segment_size,
     int8_t compression_level,
     uint8_t compression_flags,
     liberror_error_t **error )
{
	libewf_handle_t *handle = NULL;
	uint8_t *buffer         = NULL;
	static char *function   = "ewf_test_write";
	size_t write_size       = 0;
	ssize_t write_count     = 0;
	int sector_iterator     = 0;

	if( libewf_handle_initialize(
	     &handle,
	     error ) != 1 )
	{
		liberror_error_set(
		 error,
		 LIBERROR_ERROR_DOMAIN_RUNTIME,
		 LIBERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
		 "%s: unable to create handle.",
		 function );

		goto on_error;
	}
#if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER )
	if( libewf_handle_open_wide(
	     handle,
	     (wchar_t * const *) &filename,
	     1,
	     LIBEWF_OPEN_WRITE,
	     error ) != 1 )
#else
	if( libewf_handle_open(
	     handle,
	     (char * const *) &filename,
	     1,
	     LIBEWF_OPEN_WRITE,
	     error ) != 1 )
#endif
	{
		liberror_error_set(
		 error,
		 LIBERROR_ERROR_DOMAIN_IO,
		 LIBERROR_IO_ERROR_OPEN_FAILED,
		 "%s: unable to open handle.",
		 function );

		goto on_error;
	}
	if( media_size > 0 )
	{
		if( libewf_handle_set_media_size(
		     handle,
		     media_size,
		     error ) != 1 )
		{
			liberror_error_set(
			 error,
			 LIBERROR_ERROR_DOMAIN_RUNTIME,
			 LIBERROR_RUNTIME_ERROR_SET_FAILED,
			 "%s: unable set media size.",
			 function );

			goto on_error;
		}
	}
	if( maximum_segment_size > 0 )
	{
		if( libewf_handle_set_maximum_segment_size(
		     handle,
		     maximum_segment_size,
		     error ) != 1 )
		{
			liberror_error_set(
			 error,
			 LIBERROR_ERROR_DOMAIN_RUNTIME,
			 LIBERROR_RUNTIME_ERROR_SET_FAILED,
			 "%s: unable set maximum segment size.",
			 function );

			goto on_error;
		}
	}
	if( libewf_handle_set_compression_values(
	     handle,
	     compression_level,
	     compression_flags,
	     error ) != 1 )
	{
		liberror_error_set(
		 error,
		 LIBERROR_ERROR_DOMAIN_RUNTIME,
		 LIBERROR_RUNTIME_ERROR_SET_FAILED,
		 "%s: unable set compression values.",
		 function );

		goto on_error;
	}
	buffer = (uint8_t *) memory_allocate(
	                      EWF_TEST_BUFFER_SIZE );

	if( buffer == NULL )
	{
		liberror_error_set(
		 error,
		 LIBERROR_ERROR_DOMAIN_MEMORY,
		 LIBERROR_MEMORY_ERROR_INSUFFICIENT,
		 "%s: unable created buffer.",
		 function );

		goto on_error;
	}
	write_size = 512;

	for( sector_iterator = 0;
	     sector_iterator < 26;
	     sector_iterator++ )
	{
		if( memory_set(
		     buffer,
		     (int) 'A' + sector_iterator,
		     write_size ) == NULL )
		{
			liberror_error_set(
			 error,
			 LIBERROR_ERROR_DOMAIN_MEMORY,
			 LIBERROR_MEMORY_ERROR_SET_FAILED,
			 "%s: unable set value in buffer.",
			 function );

			goto on_error;
		}
		write_count = libewf_handle_write_buffer(
			       handle,
			       buffer,
			       write_size,
			       error );

		if( write_count < 0 )
		{
			liberror_error_set(
			 error,
			 LIBERROR_ERROR_DOMAIN_IO,
			 LIBERROR_IO_ERROR_WRITE_FAILED,
			 "%s: unable write buffer of size: %" PRIzd ".",
			 function,
			 write_size );

			goto on_error;
		}
		if( write_count != (ssize_t) write_size )
		{
			if( (size64_t) write_count != media_size )
			{
				liberror_error_set(
				 error,
				 LIBERROR_ERROR_DOMAIN_IO,
				 LIBERROR_IO_ERROR_WRITE_FAILED,
				 "%s: unable write buffer of size: %" PRIzd ".",
				 function,
				 write_size );

				goto on_error;
			}
		}
		if( media_size > 0 )
		{
			media_size -= write_count;
		}
	}
	write_size = 3751;

	for( sector_iterator = 0;
	     sector_iterator < 26;
	     sector_iterator++ )
	{
		if( memory_set(
		     buffer,
		     (int) 'a' + sector_iterator,
		     write_size ) == NULL )
		{
			liberror_error_set(
			 error,
			 LIBERROR_ERROR_DOMAIN_MEMORY,
			 LIBERROR_MEMORY_ERROR_SET_FAILED,
			 "%s: unable set value in buffer.",
			 function );

			goto on_error;
		}
		write_count = libewf_handle_write_buffer(
			       handle,
			       buffer,
			       write_size,
			       error );

		if( write_count < 0 )
		{
			liberror_error_set(
			 error,
			 LIBERROR_ERROR_DOMAIN_IO,
			 LIBERROR_IO_ERROR_WRITE_FAILED,
			 "%s: unable write buffer of size: %" PRIzd ".",
			 function,
			 write_size );

			goto on_error;
		}
		if( write_count != (ssize_t) write_size )
		{
			if( (size64_t) write_count != media_size )
			{
				liberror_error_set(
				 error,
				 LIBERROR_ERROR_DOMAIN_IO,
				 LIBERROR_IO_ERROR_WRITE_FAILED,
				 "%s: unable write buffer of size: %" PRIzd ".",
				 function,
				 write_size );

				goto on_error;
			}
		}
		if( media_size > 0 )
		{
			media_size -= write_count;
		}
	}
	memory_free(
	 buffer );

	buffer = NULL;
	
	if( libewf_handle_close(
	     handle,
	     error ) != 0 )
	{
		liberror_error_set(
		 error,
		 LIBERROR_ERROR_DOMAIN_IO,
		 LIBERROR_IO_ERROR_CLOSE_FAILED,
		 "%s: unable to close handle.",
		 function );

		goto on_error;
	}
	if( libewf_handle_free(
	     &handle,
	     error ) != 1 )
	{
		liberror_error_set(
		 error,
		 LIBERROR_ERROR_DOMAIN_RUNTIME,
		 LIBERROR_RUNTIME_ERROR_FINALIZE_FAILED,
		 "%s: unable to free handle.",
		 function );

		goto on_error;
	}
	return( 1 );

on_error:
	if( buffer != NULL )
	{
		memory_free(
		 buffer );
	}
	if( handle != NULL )
	{
		libewf_handle_close(
		 handle,
		 NULL );
		libewf_handle_free(
		 &handle,
		 NULL );
	}
	return( -1 );
}
/* Tests reading/writing data of a specific size at a specific offset
 * Return 1 if successful, 0 if not or -1 on error
 */
int ewf_test_read_write_delta(
     libcstring_system_character_t * const filenames[],
     int number_of_filenames,
     const libcstring_system_character_t *delta_segment_filename,
     off64_t write_offset,
     size64_t write_size,
     libcerror_error_t **error )
{
	libewf_handle_t *handle              = NULL;
	uint8_t *buffer                      = NULL;
	static char *function                = "ewf_test_read_write_delta";
	size_t delta_segment_filename_length = 0;
	size_t read_size                     = 0;
	ssize_t read_count                   = 0;
	ssize_t write_count                  = 0;

	if( libewf_handle_initialize(
	     &handle,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
		 "%s: unable to create handle.",
		 function );

		goto on_error;
	}
#if defined( HAVE_DEBUG_OUTPUT ) && defined( EWF_TEST_READ_WRITE_DELTA_VERBOSE )
	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,
	     filenames,
	     number_of_filenames,
	     LIBEWF_OPEN_READ_WRITE,
	     error ) != 1 )
#else
	if( libewf_handle_open(
	     handle,
	     filenames,
	     number_of_filenames,
	     LIBEWF_OPEN_READ_WRITE,
	     error ) != 1 )
#endif
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_IO,
		 LIBCERROR_IO_ERROR_OPEN_FAILED,
		 "%s: unable to open handle.",
		 function );

		goto on_error;
	}
	if( delta_segment_filename != NULL )
	{
		delta_segment_filename_length = libcstring_system_string_length(
		                                 delta_segment_filename );

#if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER )
		if( libewf_handle_set_delta_segment_filename_wide(
		     handle,
		     delta_segment_filename,
		     delta_segment_filename_length,
		     error ) != 1 )
#else
		if( libewf_handle_set_delta_segment_filename(
		     handle,
		     delta_segment_filename,
		     delta_segment_filename_length,
		     error ) != 1 )
#endif
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
			 "%s: unable to set delta segment filename.",
			 function );

			goto on_error;
		}
	}
	if( libewf_handle_seek_offset(
	     handle,
	     write_offset,
	     SEEK_SET,
	     error ) == -1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_IO,
		 LIBCERROR_IO_ERROR_OPEN_FAILED,
		 "%s: unable to seek offset: %" PRIi64 ".",
		 function,
		 write_offset );

		goto on_error;
	}
	buffer = (uint8_t *) memory_allocate(
	                      EWF_TEST_BUFFER_SIZE );

	while( write_size > 0 )
	{
		if( write_size > (size64_t) EWF_TEST_BUFFER_SIZE )
		{
			read_size = EWF_TEST_BUFFER_SIZE;
		}
		else
		{
			read_size = (size_t) write_size;
		}
		read_count = libewf_handle_read_buffer(
			      handle,
			      buffer,
			      read_size,
			      error );

		if( read_count < 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_IO,
			 LIBCERROR_IO_ERROR_WRITE_FAILED,
			 "%s: unable read buffer of size: %" PRIzd ".",
			 function,
			 read_size );

			goto on_error;
		}
		if( memory_set(
		     buffer,
		     (int) 'X',
		     (size_t) read_count ) == NULL )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_MEMORY,
			 LIBCERROR_MEMORY_ERROR_SET_FAILED,
			 "%s: unable set value in buffer.",
			 function );

			goto on_error;
		}
		if( libewf_handle_seek_offset(
		     handle,
		     -1 * (off64_t) read_size,
		     SEEK_CUR,
		     error ) == -1 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_IO,
			 LIBCERROR_IO_ERROR_OPEN_FAILED,
			 "%s: unable to seek previous offset.",
			 function );

			goto on_error;
		}
		write_count = libewf_handle_write_buffer(
			       handle,
			       buffer,
			       (size_t) read_count,
			       error );

		if( write_count < 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_IO,
			 LIBCERROR_IO_ERROR_WRITE_FAILED,
			 "%s: unable write buffer of size: %" PRIzd ".",
			 function,
			 read_count );

			goto on_error;
		}
		if( write_count != read_count )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_IO,
			 LIBCERROR_IO_ERROR_WRITE_FAILED,
			 "%s: unable write buffer of size: %" PRIzd ".",
			 function,
			 read_count );

			goto on_error;
		}
		write_offset += write_count;
		write_size   -= write_count;
	}
	memory_free(
	 buffer );

	buffer = NULL;
	
	if( libewf_handle_close(
	     handle,
	     error ) != 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_IO,
		 LIBCERROR_IO_ERROR_CLOSE_FAILED,
		 "%s: unable to close handle.",
		 function );

		goto on_error;
	}
	if( libewf_handle_free(
	     &handle,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
		 "%s: unable to free handle.",
		 function );

		goto on_error;
	}
	return( 1 );

on_error:
	if( buffer != NULL )
	{
		memory_free(
		 buffer );
	}
	if( handle != NULL )
	{
		libewf_handle_close(
		 handle,
		 NULL );
		libewf_handle_free(
		 &handle,
		 NULL );
	}
	return( -1 );
}
Example #15
0
/* 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 );
}
Example #16
0
int main( int argc, char * const argv[] )
#endif
{
#if !defined( HAVE_GLOB_H )
	libcsystem_glob_t *glob                                  = NULL;
#endif

	libcerror_error_t *error                                 = NULL;

	libcstring_system_character_t * const *source_filenames = NULL;
	libcstring_system_character_t **ewf_filenames           = NULL;

	libcstring_system_character_t *option_header_codepage   = NULL;
	libcstring_system_character_t *program                  = _LIBCSTRING_SYSTEM_STRING( "ewfdebug" );

	libcstring_system_integer_t option                      = 0;
	size_t first_filename_length                            = 0;
	uint8_t verbose                                         = 0;
	int number_of_filenames                                 = 0;
	int header_codepage                                     = LIBEWF_CODEPAGE_ASCII;
	int result                                              = 0;

	libcnotify_stream_set(
	 stderr,
	 NULL );
	libcnotify_verbose_set(
	 1 );

	if( libclocale_initialize(
             "ewftools",
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to initialize locale values.\n" );

		goto on_error;
	}
	if( libcsystem_initialize(
	     _IONBF,
	     &error ) != 1 )
	{
		ewfoutput_version_fprint(
		 stdout,
		 program );

		fprintf(
		 stderr,
		 "Unable to initialize system values.\n" );

		goto on_error;
	}
	while( ( option = libcsystem_getopt(
			   argc,
			   argv,
			   _LIBCSTRING_SYSTEM_STRING( "A:hqvV" ) ) ) != (libcstring_system_integer_t) -1 )
	{
		switch( option )
		{
			case (libcstring_system_integer_t) '?':
			default:
				ewfoutput_version_fprint(
				 stdout,
				 program );

				fprintf(
				 stderr,
				 "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM ".\n",
				 argv[ optind - 1 ] );

				usage_fprint(
				 stdout );

				return( EXIT_FAILURE );

			case (libcstring_system_integer_t) 'A':
				option_header_codepage = optarg;

				break;

			case (libcstring_system_integer_t) 'h':
				ewfoutput_version_fprint(
				 stdout,
				 program );

				usage_fprint(
				 stdout );

				return( EXIT_SUCCESS );

			case (libcstring_system_integer_t) 'q':
				break;

			case (libcstring_system_integer_t) 'v':
				verbose = 1;

				break;

			case (libcstring_system_integer_t) 'V':
				ewfoutput_version_fprint(
				 stdout,
				 program );

				ewfoutput_copyright_fprint(
				 stdout );

				return( EXIT_SUCCESS );
		}
	}
	if( optind == argc )
	{
		ewfoutput_version_fprint(
		 stdout,
		 program );

		fprintf(
		 stderr,
		 "Missing EWF image file(s).\n" );

		usage_fprint(
		 stdout );

		goto on_error;
	}
	ewfoutput_version_fprint(
	 stdout,
	 program );

	libcnotify_verbose_set(
	 verbose );

#if !defined( HAVE_LOCAL_LIBEWF )
	libewf_notify_set_verbose(
	 verbose );
	libewf_notify_set_stream(
	 stderr,
	 NULL );
#endif

	if( option_header_codepage != NULL )
	{
		if( ewfinput_determine_header_codepage(
		     option_header_codepage,
		     &header_codepage,
		     &error ) != 1 )
		{
			libcnotify_print_error_backtrace(
			 error );
			libcerror_error_free(
			 &error );

			fprintf(
			 stderr,
			 "Unsupported header codepage defaulting to: ascii.\n" );

			header_codepage = LIBEWF_CODEPAGE_ASCII;
		}
	}
	if( libcsystem_signal_attach(
	     ewfdebug_signal_handler,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to attach signal handler.\n" );

		libcnotify_print_error_backtrace(
		 error );
		libcerror_error_free(
		 &error );
	}
#if !defined( HAVE_GLOB_H )
	if( libcsystem_glob_initialize(
	     &glob,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to initialize glob.\n" );

		goto on_error;
	}
	if( libcsystem_glob_resolve(
	     glob,
	     &( argv[ optind ] ),
	     argc - optind,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to resolve glob.\n" );

		goto on_error;
	}
	if( libcsystem_glob_get_results(
	     glob,
	     &number_of_filenames,
	     (libcstring_system_character_t ***) &source_filenames,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to retrieve glob results.\n" );

		goto on_error;
	}
#else
	source_filenames    = &( argv[ optind ] );
	number_of_filenames = argc - optind;
#endif

	if( number_of_filenames == 1 )
	{
		first_filename_length = libcstring_system_string_length(
		                         source_filenames[ 0 ] );

#if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER )
		if( libewf_glob_wide(
		     source_filenames[ 0 ],
		     first_filename_length,
		     LIBEWF_FORMAT_UNKNOWN,
		     &ewf_filenames,
		     &number_of_filenames,
		     &error ) != 1 )
#else
		if( libewf_glob(
		     source_filenames[ 0 ],
		     first_filename_length,
		     LIBEWF_FORMAT_UNKNOWN,
		     &ewf_filenames,
		     &number_of_filenames,
		     &error ) != 1 )
#endif
		{
			fprintf(
			 stderr,
			 "Unable to resolve ewf file(s).\n" );

			goto on_error;
		}
		source_filenames = (libcstring_system_character_t * const *) ewf_filenames;
	}
	if( libewf_handle_initialize(
	     &ewfdebug_input_handle,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to initialize input handle.\n" );

		goto on_error;
	}
#if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER )
	result = libewf_handle_open_wide(
	          ewfdebug_input_handle,
	          source_filenames,
	          number_of_filenames,
	          LIBEWF_OPEN_READ,
	          &error );
#else
	result = libewf_handle_open(
	          ewfdebug_input_handle,
	          source_filenames,
	          number_of_filenames,
	          LIBEWF_OPEN_READ,
	          &error );
#endif
#if !defined( HAVE_GLOB_H )
	if( libcsystem_glob_free(
	     &glob,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to free glob.\n" );

		goto on_error;
	}
#endif
	if( ewf_filenames != NULL )
	{
		for( ; number_of_filenames > 0; number_of_filenames-- )
		{
			memory_free(
			 ewf_filenames[ number_of_filenames - 1 ] );
		}
		memory_free(
		 ewf_filenames );
	}
	if( ( ewfdebug_abort == 0 )
	 && ( result != 1 ) )
	{
		fprintf(
		 stderr,
		 "Unable to open EWF file(s).\n" );

		libewf_handle_close(
		 ewfdebug_input_handle,
		 NULL );
		libewf_handle_free(
		 &ewfdebug_input_handle,
		 NULL );

		return( EXIT_FAILURE );
	}
	/* TODO */

	if( libewf_handle_close(
	     ewfdebug_input_handle,
	     &error ) != 0 )
	{
		fprintf(
		 stderr,
		 "Unable to close EWF file(s).\n" );

		libewf_handle_free(
		 &ewfdebug_input_handle,
		 NULL );

		return( EXIT_FAILURE );
	}
	if( libewf_handle_free(
	     &ewfdebug_input_handle,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to free input handle.\n" );

		return( EXIT_FAILURE );
	}
	if( libcsystem_signal_detach(
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to detach signal handler.\n" );

		libcnotify_print_error_backtrace(
		 error );
		libcerror_error_free(
		 &error );
	}
	if( ewfdebug_abort != 0 )
	{
		fprintf(
		 stdout,
		 "%" PRIs_LIBCSTRING_SYSTEM ": ABORTED\n",
		 program );

		return( EXIT_FAILURE );
	}
	fprintf(
	 stdout,
	 "Debug completed.\n" );

	return( EXIT_SUCCESS );

on_error:
	if( error != NULL )
	{
		libcnotify_print_error_backtrace(
		 error );
		libcerror_error_free(
		 &error );
	}
#if !defined( HAVE_GLOB_H )
	if( glob != NULL )
	{
		libcsystem_glob_free(
		 &glob,
		 NULL );
	}
#endif
	return( EXIT_FAILURE );
}
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 );
}