Skip to content

Commit

Permalink
Bug fix in Python-bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed Nov 8, 2015
1 parent 24d9785 commit 37d8be7
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 305 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Tests:
- compressed (done)
- (compressed) with parent (done)

20141029
* bug fix in Python-bindings

20141027
* changes for deployment

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AC_PREREQ( 2.59 )

AC_INIT(
[libvmdk],
[20141027],
[20141029],
[joachim.metz@gmail.com])

AC_CONFIG_SRCDIR(
Expand Down
8 changes: 6 additions & 2 deletions include/libvmdk.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,28 @@ int libvmdk_check_file_signature(
libvmdk_error_t **error );

#if defined( LIBVMDK_HAVE_WIDE_CHARACTER_TYPE )

/* Determines if a file is a VMDK file
* Returns 1 if true, 0 if not or -1 on error
*/
LIBVMDK_EXTERN \
int libvmdk_check_file_signature_wide(
const wchar_t *filename,
libvmdk_error_t **error );
#endif

#endif /* defined( LIBVMDK_HAVE_WIDE_CHARACTER_TYPE ) */

#if defined( LIBVMDK_HAVE_BFIO )

/* Determines if a file is a VMDK file using a Basic File IO (bfio) handle
* Returns 1 if true, 0 if not or -1 on error
*/
LIBVMDK_EXTERN \
int libvmdk_check_file_signature_file_io_handle(
libbfio_handle_t *file_io_handle,
libvmdk_error_t **error );
#endif

#endif /* defined( LIBVMDK_HAVE_BFIO ) */

/* -------------------------------------------------------------------------
* Notify functions
Expand Down
129 changes: 60 additions & 69 deletions pyvmdk/pyvmdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ PyObject *pyvmdk_get_version(
errors ) );
}

#if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER )

/* Checks if the file has a VMware Virtual Disk (VMDK) file signature
* Returns a Python object if successful or NULL on error
*/
Expand All @@ -138,17 +136,22 @@ PyObject *pyvmdk_check_file_signature(
libcerror_error_t *error = NULL;
static char *function = "pyvmdk_check_file_signature";
static char *keyword_list[] = { "filename", NULL };
const wchar_t *filename_wide = NULL;
const char *filename_narrow = NULL;
char *error_string = NULL;
int result = 0;

#if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER )
const wchar_t *filename_wide = NULL;
#else
PyObject *utf8_string_object = NULL;
#endif

PYVMDK_UNREFERENCED_PARAMETER( self )

/* Note that PyArg_ParseTupleAndKeywords with "s" will force Unicode strings to be converted to narrow character string.
* On Windows the narrow character strings contains an extended ASCII string with a codepage. Hence we get a conversion
* exception. We cannot use "u" here either since that does not allow us to pass non Unicode string objects and
* Python (at least 2.7) does not seems to automatically upcast them.
* exception. This will also fail if the default encoding is not set correctly. We cannot use "u" here either since that
* does not allow us to pass non Unicode string objects and Python (at least 2.7) does not seems to automatically upcast them.
*/
if( PyArg_ParseTupleAndKeywords(
arguments,
Expand Down Expand Up @@ -202,6 +205,7 @@ PyObject *pyvmdk_check_file_signature(
{
PyErr_Clear();

#if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER )
filename_wide = (wchar_t *) PyUnicode_AsUnicode(
string_object );
Py_BEGIN_ALLOW_THREADS
Expand All @@ -211,7 +215,57 @@ PyObject *pyvmdk_check_file_signature(
&error );

Py_END_ALLOW_THREADS
#else
utf8_string_object = PyUnicode_AsUTF8String(
string_object );

if( utf8_string_object == NULL )
{
PyErr_Fetch(
&exception_type,
&exception_value,
&exception_traceback );

exception_string = PyObject_Repr(
exception_value );

error_string = PyString_AsString(
exception_string );

if( error_string != NULL )
{
PyErr_Format(
PyExc_RuntimeError,
"%s: unable to convert unicode string to UTF-8 with error: %s.",
function,
error_string );
}
else
{
PyErr_Format(
PyExc_RuntimeError,
"%s: unable to convert unicode string to UTF-8.",
function );
}
Py_DecRef(
exception_string );

return( NULL );
}
filename_narrow = PyString_AsString(
utf8_string_object );

Py_BEGIN_ALLOW_THREADS

result = libvmdk_check_file_signature(
filename_narrow,
&error );

Py_END_ALLOW_THREADS

Py_DecRef(
utf8_string_object );
#endif
if( result == -1 )
{
pyvmdk_error_raise(
Expand Down Expand Up @@ -318,75 +372,12 @@ PyObject *pyvmdk_check_file_signature(
}
PyErr_Format(
PyExc_TypeError,
"%s: unsupported string object type",
"%s: unsupported string object type.",
function );

return( NULL );
}

#else

/* Checks if the file has a VMware Virtual Disk (VMDK) file signature
* Returns a Python object if successful or NULL on error
*/
PyObject *pyvmdk_check_file_signature(
PyObject *self PYVMDK_ATTRIBUTE_UNUSED,
PyObject *arguments,
PyObject *keywords )
{
libcerror_error_t *error = NULL;
static char *function = "pyvmdk_check_file_signature";
static char *keyword_list[] = { "filename", NULL };
const char *filename = NULL;
int result = 0;

PYVMDK_UNREFERENCED_PARAMETER( self )

if( PyArg_ParseTupleAndKeywords(
arguments,
keywords,
"|s",
keyword_list,
&filename ) == 0 )
{
return( NULL );
}
Py_BEGIN_ALLOW_THREADS

result = libvmdk_check_file_signature(
filename,
&error );

Py_END_ALLOW_THREADS

if( result == -1 )
{
pyvmdk_error_raise(
error,
PyExc_IOError,
"%s: unable to check file signature.",
function );

libcerror_error_free(
&error );

return( NULL );
}
if( result != 0 )
{
Py_IncRef(
(PyObject *) Py_True );

return( Py_True );
}
Py_IncRef(
(PyObject *) Py_False );

return( Py_False );
}

#endif /* defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) */

/* Checks if the file has a VMware Virtual Disk (VMDK) file signature using a file-like object
* Returns a Python object if successful or NULL on error
*/
Expand Down
Loading

0 comments on commit 37d8be7

Please sign in to comment.