Skip to content

Commit

Permalink
Worked on Python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed Jul 14, 2023
1 parent ca19482 commit 1a49668
Show file tree
Hide file tree
Showing 15 changed files with 516 additions and 23 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -2,7 +2,7 @@ AC_PREREQ([2.71])

AC_INIT(
[libscca],
[20230709],
[20230714],
[joachim.metz@gmail.com])

AC_CONFIG_SRCDIR(
Expand Down
5 changes: 5 additions & 0 deletions documentation/Windows Prefetch File (PF) format.asciidoc
Expand Up @@ -432,6 +432,11 @@ The offset is relative to the start of the file

The entries with a bold offset and size were changed since version 30 - variant 1.

=== Hash string

[yellow-background]*TODO: rename to something more appropriate*
[yellow-background]*TODO: describe possible strings array*

=== File metrics array

The file metrics array consists of entries containing metrics about the files
Expand Down
11 changes: 6 additions & 5 deletions libscca/libscca_filename_strings.c
Expand Up @@ -260,13 +260,14 @@ int libscca_filename_strings_read_data(

return( -1 );
}
if( data_size > (size_t) SSIZE_MAX )
if( ( data_size < 2 )
|| ( data_size > (size_t) SSIZE_MAX ) )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid data size value exceeds maximum.",
LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
"%s: invalid data size value out of bounds.",
function );

return( -1 );
Expand All @@ -285,7 +286,7 @@ int libscca_filename_strings_read_data(

goto on_error;
}
while( last_data_offset < data_size )
while( last_data_offset < ( data_size - 1 ) )
{
if( filename_strings_index > LIBSCCA_MAXIMUM_NUMBER_OF_FILENAME_STRINGS )
{
Expand All @@ -299,7 +300,7 @@ int libscca_filename_strings_read_data(
goto on_error;
}
for( data_offset = last_data_offset;
data_offset < data_size;
data_offset < ( data_size - 1 );
data_offset += 2 )
{
if( ( data[ data_offset ] == 0 )
Expand Down
12 changes: 12 additions & 0 deletions msvscpp/pyscca/pyscca.vcproj
Expand Up @@ -205,6 +205,10 @@
RelativePath="..\..\pyscca\pyscca_integer.c"
>
</File>
<File
RelativePath="..\..\pyscca\pyscca_string.c"
>
</File>
<File
RelativePath="..\..\pyscca\pyscca_volume_information.c"
>
Expand Down Expand Up @@ -271,10 +275,18 @@
RelativePath="..\..\pyscca\pyscca_libscca.h"
>
</File>
<File
RelativePath="..\..\pyscca\pyscca_libuna.h"
>
</File>
<File
RelativePath="..\..\pyscca\pyscca_python.h"
>
</File>
<File
RelativePath="..\..\pyscca\pyscca_string.h"
>
</File>
<File
RelativePath="..\..\pyscca\pyscca_unused.h"
>
Expand Down
2 changes: 2 additions & 0 deletions pyscca-python2/Makefile.am
Expand Up @@ -34,7 +34,9 @@ BUILT_SOURCES = \
pyscca_libcerror.h \
pyscca_libclocale.h \
pyscca_libscca.h \
pyscca_libuna.h \
pyscca_python.h \
pyscca_string.c pyscca_string.h \
pyscca_unused.h \
pyscca_volume_information.c pyscca_volume_information.h \
pyscca_volumes.c pyscca_volumes.h
Expand Down
2 changes: 2 additions & 0 deletions pyscca-python3/Makefile.am
Expand Up @@ -34,7 +34,9 @@ BUILT_SOURCES = \
pyscca_libcerror.h \
pyscca_libclocale.h \
pyscca_libscca.h \
pyscca_libuna.h \
pyscca_python.h \
pyscca_string.c pyscca_string.h \
pyscca_unused.h \
pyscca_volume_information.c pyscca_volume_information.h \
pyscca_volumes.c pyscca_volumes.h
Expand Down
2 changes: 2 additions & 0 deletions pyscca/Makefile.am
Expand Up @@ -28,7 +28,9 @@ pyscca_la_SOURCES = \
pyscca_libcerror.h \
pyscca_libclocale.h \
pyscca_libscca.h \
pyscca_libuna.h \
pyscca_python.h \
pyscca_string.c pyscca_string.h \
pyscca_unused.h \
pyscca_volume_information.c pyscca_volume_information.h \
pyscca_volumes.c pyscca_volumes.h
Expand Down
14 changes: 12 additions & 2 deletions pyscca/pyscca.c
Expand Up @@ -100,7 +100,6 @@ PyObject *pyscca_get_version(
PyObject *self PYSCCA_ATTRIBUTE_UNUSED,
PyObject *arguments PYSCCA_ATTRIBUTE_UNUSED )
{
const char *errors = NULL;
const char *version_string = NULL;
size_t version_string_length = 0;

Expand All @@ -123,7 +122,7 @@ PyObject *pyscca_get_version(
return( PyUnicode_DecodeUTF8(
version_string,
(Py_ssize_t) version_string_length,
errors ) );
NULL ) );
}

/* Checks if a file has a Windows Prefetch File (SCCA) signature
Expand Down Expand Up @@ -183,15 +182,26 @@ PyObject *pyscca_check_file_signature(
PyErr_Clear();

#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
filename_wide = (wchar_t *) PyUnicode_AsWideCharString(
string_object,
NULL );
#else
filename_wide = (wchar_t *) PyUnicode_AsUnicode(
string_object );
#endif
Py_BEGIN_ALLOW_THREADS

result = libscca_check_file_signature_wide(
filename_wide,
&error );

Py_END_ALLOW_THREADS

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
PyMem_Free(
filename_wide );
#endif
#else
utf8_string_object = PyUnicode_AsUTF8String(
string_object );
Expand Down
46 changes: 36 additions & 10 deletions pyscca/pyscca_file.c
Expand Up @@ -38,6 +38,7 @@
#include "pyscca_libcerror.h"
#include "pyscca_libscca.h"
#include "pyscca_python.h"
#include "pyscca_string.h"
#include "pyscca_unused.h"
#include "pyscca_volume_information.h"
#include "pyscca_volumes.h"
Expand Down Expand Up @@ -574,8 +575,14 @@ PyObject *pyscca_file_open(
PyErr_Clear();

#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
filename_wide = (wchar_t *) PyUnicode_AsWideCharString(
string_object,
NULL );
#else
filename_wide = (wchar_t *) PyUnicode_AsUnicode(
string_object );
#endif
Py_BEGIN_ALLOW_THREADS

result = libscca_file_open_wide(
Expand All @@ -585,6 +592,11 @@ PyObject *pyscca_file_open(
&error );

Py_END_ALLOW_THREADS

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
PyMem_Free(
filename_wide );
#endif
#else
utf8_string_object = PyUnicode_AsUTF8String(
string_object );
Expand Down Expand Up @@ -976,7 +988,6 @@ PyObject *pyscca_file_get_executable_filename(
{
PyObject *string_object = NULL;
libcerror_error_t *error = NULL;
const char *errors = NULL;
static char *function = "pyscca_file_get_executable_filename";
char *utf8_string = NULL;
size_t utf8_string_size = 0;
Expand Down Expand Up @@ -1037,6 +1048,8 @@ PyObject *pyscca_file_get_executable_filename(
}
Py_BEGIN_ALLOW_THREADS

/* Using RFC 2279 UTF-8 to support unpaired UTF-16 surrogates
*/
result = libscca_file_get_utf8_executable_filename(
pyscca_file->file,
(uint8_t *) utf8_string,
Expand All @@ -1058,14 +1071,20 @@ PyObject *pyscca_file_get_executable_filename(

goto on_error;
}
/* Pass the string length to PyUnicode_DecodeUTF8 otherwise it makes
* the end of string character is part of the string
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
string_object = pyscca_string_new_from_utf8_rfc2279(
(uint8_t *) utf8_string,
utf8_string_size );
#else
/* Pass the string length to PyUnicode_DecodeUTF8
* otherwise it makes the end of string character is part
* of the string
*/
string_object = PyUnicode_DecodeUTF8(
utf8_string,
(Py_ssize_t) utf8_string_size - 1,
errors );

NULL );
#endif
if( string_object == NULL )
{
PyErr_Format(
Expand Down Expand Up @@ -1624,7 +1643,6 @@ PyObject *pyscca_file_get_filename_by_index(
PyObject *string_object = NULL;
libcerror_error_t *error = NULL;
uint8_t *utf8_string = NULL;
const char *errors = NULL;
static char *function = "pyscca_file_get_filename_by_index";
size_t utf8_string_size = 0;
int result = 0;
Expand Down Expand Up @@ -1684,6 +1702,8 @@ PyObject *pyscca_file_get_filename_by_index(
}
Py_BEGIN_ALLOW_THREADS

/* Using RFC 2279 UTF-8 to support unpaired UTF-16 surrogates
*/
result = libscca_file_get_utf8_filename(
( (pyscca_file_t *) pyscca_file )->file,
filename_index,
Expand All @@ -1707,14 +1727,20 @@ PyObject *pyscca_file_get_filename_by_index(

goto on_error;
}
/* Pass the string length to PyUnicode_DecodeUTF8 otherwise it makes
* the end of string character is part of the string
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
string_object = pyscca_string_new_from_utf8_rfc2279(
utf8_string,
utf8_string_size );
#else
/* Pass the string length to PyUnicode_DecodeUTF8
* otherwise it makes the end of string character is part
* of the string
*/
string_object = PyUnicode_DecodeUTF8(
(char *) utf8_string,
(Py_ssize_t) utf8_string_size - 1,
errors );

NULL );
#endif
if( string_object == NULL )
{
PyErr_Format(
Expand Down
3 changes: 1 addition & 2 deletions pyscca/pyscca_file_metrics.c
Expand Up @@ -329,7 +329,6 @@ PyObject *pyscca_file_metrics_get_filename(
{
PyObject *string_object = NULL;
libcerror_error_t *error = NULL;
const char *errors = NULL;
static char *function = "pyscca_file_metrics_get_filename";
char *utf8_string = NULL;
size_t utf8_string_size = 0;
Expand Down Expand Up @@ -417,7 +416,7 @@ PyObject *pyscca_file_metrics_get_filename(
string_object = PyUnicode_DecodeUTF8(
utf8_string,
(Py_ssize_t) utf8_string_size - 1,
errors );
NULL );

if( string_object == NULL )
{
Expand Down
2 changes: 1 addition & 1 deletion pyscca/pyscca_file_object_io_handle.c
Expand Up @@ -578,7 +578,7 @@ ssize_t pyscca_file_object_read_buffer(
error,
LIBCERROR_ERROR_DOMAIN_MEMORY,
LIBCERROR_MEMORY_ERROR_COPY_FAILED,
"%s: unable to data to buffer.",
"%s: unable to copy data to buffer.",
function );

goto on_error;
Expand Down
60 changes: 60 additions & 0 deletions pyscca/pyscca_libuna.h
@@ -0,0 +1,60 @@
/*
* The libuna header wrapper
*
* Copyright (C) 2011-2023, Joachim Metz <joachim.metz@gmail.com>
*
* Refer to AUTHORS for acknowledgements.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#if !defined( _PYSCCA_LIBUNA_H )
#define _PYSCCA_LIBUNA_H

#include <common.h>

/* Define HAVE_LOCAL_LIBUNA for local use of libuna
*/
#if defined( HAVE_LOCAL_LIBUNA )

#include <libuna_base16_stream.h>
#include <libuna_base32_stream.h>
#include <libuna_base64_stream.h>
#include <libuna_byte_stream.h>
#include <libuna_unicode_character.h>
#include <libuna_url_stream.h>
#include <libuna_utf16_stream.h>
#include <libuna_utf16_string.h>
#include <libuna_utf32_stream.h>
#include <libuna_utf32_string.h>
#include <libuna_utf7_stream.h>
#include <libuna_utf8_stream.h>
#include <libuna_utf8_string.h>
#include <libuna_types.h>

#else

/* If libtool DLL support is enabled set LIBUNA_DLL_IMPORT
* before including libuna.h
*/
#if defined( _WIN32 ) && defined( DLL_IMPORT )
#define LIBUNA_DLL_IMPORT
#endif

#include <libuna.h>

#endif

#endif /* !defined( _PYSCCA_LIBUNA_H ) */

0 comments on commit 1a49668

Please sign in to comment.