mirror of
https://github.com/libyal/libexe
synced 2026-06-08 15:29:55 +00:00
431 lines
9.0 KiB
C
431 lines
9.0 KiB
C
/*
|
|
* Support functions
|
|
*
|
|
* Copyright (C) 2011-2026, 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/>.
|
|
*/
|
|
|
|
#include <common.h>
|
|
#include <memory.h>
|
|
#include <narrow_string.h>
|
|
#include <types.h>
|
|
#include <wide_string.h>
|
|
|
|
#include "libexe_definitions.h"
|
|
#include "libexe_libbfio.h"
|
|
#include "libexe_libcerror.h"
|
|
#include "libexe_libclocale.h"
|
|
#include "libexe_support.h"
|
|
|
|
#include "exe_mz_header.h"
|
|
|
|
#if !defined( HAVE_LOCAL_LIBEXE )
|
|
|
|
/* Returns the library version
|
|
*/
|
|
const char *libexe_get_version(
|
|
void )
|
|
{
|
|
return( (const char *) LIBEXE_VERSION_STRING );
|
|
}
|
|
|
|
/* Returns the access flags for reading
|
|
*/
|
|
int libexe_get_access_flags_read(
|
|
void )
|
|
{
|
|
return( (int) LIBEXE_ACCESS_FLAG_READ );
|
|
}
|
|
|
|
/* Retrieves the narrow system string codepage
|
|
* A value of 0 represents no codepage, UTF-8 encoding is used instead
|
|
* Returns 1 if successful or -1 on error
|
|
*/
|
|
int libexe_get_codepage(
|
|
int *codepage,
|
|
libcerror_error_t **error )
|
|
{
|
|
static char *function = "libexe_get_codepage";
|
|
|
|
if( libclocale_codepage_get(
|
|
codepage,
|
|
error ) != 1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
|
|
"%s: unable to retrieve codepage.",
|
|
function );
|
|
|
|
return( -1 );
|
|
}
|
|
return( 1 );
|
|
}
|
|
|
|
/* Sets the narrow system string codepage
|
|
* A value of 0 represents no codepage, UTF-8 encoding is used instead
|
|
* Returns 1 if successful or -1 on error
|
|
*/
|
|
int libexe_set_codepage(
|
|
int codepage,
|
|
libcerror_error_t **error )
|
|
{
|
|
static char *function = "libexe_set_codepage";
|
|
|
|
if( libclocale_codepage_set(
|
|
codepage,
|
|
error ) != 1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_SET_FAILED,
|
|
"%s: unable to set codepage.",
|
|
function );
|
|
|
|
return( -1 );
|
|
}
|
|
return( 1 );
|
|
}
|
|
|
|
#endif /* !defined( HAVE_LOCAL_LIBEXE ) */
|
|
|
|
/* Determines if a file contains an EXE file signature
|
|
* Returns 1 if true, 0 if not or -1 on error
|
|
*/
|
|
int libexe_check_file_signature(
|
|
const char *filename,
|
|
libcerror_error_t **error )
|
|
{
|
|
libbfio_handle_t *file_io_handle = NULL;
|
|
static char *function = "libexe_check_file_signature";
|
|
size_t filename_length = 0;
|
|
int result = 0;
|
|
|
|
if( filename == NULL )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
|
|
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
|
|
"%s: invalid filename.",
|
|
function );
|
|
|
|
return( -1 );
|
|
}
|
|
filename_length = narrow_string_length(
|
|
filename );
|
|
|
|
if( filename_length == 0 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
|
|
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
|
|
"%s: invalid filename.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
if( libbfio_file_initialize(
|
|
&file_io_handle,
|
|
error ) != 1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
|
|
"%s: unable to create file IO handle.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
if( libbfio_file_set_name(
|
|
file_io_handle,
|
|
filename,
|
|
filename_length,
|
|
error ) != 1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_SET_FAILED,
|
|
"%s: unable to set filename in file IO handle.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
result = libexe_check_file_signature_file_io_handle(
|
|
file_io_handle,
|
|
error );
|
|
|
|
if( result == -1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
|
|
"%s: unable to check file signature using a file handle.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
if( libbfio_handle_free(
|
|
&file_io_handle,
|
|
error ) != 1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
|
|
"%s: unable to free file IO handle.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
return( result );
|
|
|
|
on_error:
|
|
if( file_io_handle != NULL )
|
|
{
|
|
libbfio_handle_free(
|
|
&file_io_handle,
|
|
NULL );
|
|
}
|
|
return( -1 );
|
|
}
|
|
|
|
#if defined( HAVE_WIDE_CHARACTER_TYPE )
|
|
|
|
/* Determines if a file contains an EXE file signature
|
|
* Returns 1 if true, 0 if not or -1 on error
|
|
*/
|
|
int libexe_check_file_signature_wide(
|
|
const wchar_t *filename,
|
|
libcerror_error_t **error )
|
|
{
|
|
libbfio_handle_t *file_io_handle = NULL;
|
|
static char *function = "libexe_check_file_signature_wide";
|
|
size_t filename_length = 0;
|
|
int result = 0;
|
|
|
|
if( filename == NULL )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
|
|
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
|
|
"%s: invalid filename.",
|
|
function );
|
|
|
|
return( -1 );
|
|
}
|
|
filename_length = wide_string_length(
|
|
filename );
|
|
|
|
if( filename_length == 0 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
|
|
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
|
|
"%s: invalid filename.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
if( libbfio_file_initialize(
|
|
&file_io_handle,
|
|
error ) != 1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
|
|
"%s: unable to create file IO handle.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
if( libbfio_file_set_name_wide(
|
|
file_io_handle,
|
|
filename,
|
|
filename_length,
|
|
error ) != 1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_SET_FAILED,
|
|
"%s: unable to set filename in file IO handle.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
result = libexe_check_file_signature_file_io_handle(
|
|
file_io_handle,
|
|
error );
|
|
|
|
if( result == -1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
|
|
"%s: unable to check file signature using a file handle.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
if( libbfio_handle_free(
|
|
&file_io_handle,
|
|
error ) != 1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_RUNTIME,
|
|
LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
|
|
"%s: unable to free file IO handle.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
return( result );
|
|
|
|
on_error:
|
|
if( file_io_handle != NULL )
|
|
{
|
|
libbfio_handle_free(
|
|
&file_io_handle,
|
|
NULL );
|
|
}
|
|
return( -1 );
|
|
}
|
|
|
|
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
|
|
|
|
/* Determines if a file contains an EXE file signature using a Basic File IO (bfio) handle
|
|
* Returns 1 if true, 0 if not or -1 on error
|
|
*/
|
|
int libexe_check_file_signature_file_io_handle(
|
|
libbfio_handle_t *file_io_handle,
|
|
libcerror_error_t **error )
|
|
{
|
|
uint8_t signature[ 2 ];
|
|
|
|
static char *function = "libexe_check_file_signature_file_io_handle";
|
|
ssize_t read_count = 0;
|
|
int file_io_handle_is_open = 0;
|
|
|
|
if( file_io_handle == NULL )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
|
|
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
|
|
"%s: invalid file IO handle.",
|
|
function );
|
|
|
|
return( -1 );
|
|
}
|
|
file_io_handle_is_open = libbfio_handle_is_open(
|
|
file_io_handle,
|
|
error );
|
|
|
|
if( file_io_handle_is_open == -1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_IO,
|
|
LIBCERROR_IO_ERROR_OPEN_FAILED,
|
|
"%s: unable to open file.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
else if( file_io_handle_is_open == 0 )
|
|
{
|
|
if( libbfio_handle_open(
|
|
file_io_handle,
|
|
LIBBFIO_OPEN_READ,
|
|
error ) != 1 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_IO,
|
|
LIBCERROR_IO_ERROR_OPEN_FAILED,
|
|
"%s: unable to open file.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
}
|
|
read_count = libbfio_handle_read_buffer_at_offset(
|
|
file_io_handle,
|
|
signature,
|
|
2,
|
|
0,
|
|
error );
|
|
|
|
if( read_count != 2 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_IO,
|
|
LIBCERROR_IO_ERROR_READ_FAILED,
|
|
"%s: unable to read signature at offset: 0 (0x00000000).",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
if( file_io_handle_is_open == 0 )
|
|
{
|
|
if( libbfio_handle_close(
|
|
file_io_handle,
|
|
error ) != 0 )
|
|
{
|
|
libcerror_error_set(
|
|
error,
|
|
LIBCERROR_ERROR_DOMAIN_IO,
|
|
LIBCERROR_IO_ERROR_CLOSE_FAILED,
|
|
"%s: unable to close file.",
|
|
function );
|
|
|
|
goto on_error;
|
|
}
|
|
}
|
|
if( memory_compare(
|
|
signature,
|
|
EXE_MZ_SIGNATURE,
|
|
2 ) == 0 )
|
|
{
|
|
return( 1 );
|
|
}
|
|
return( 0 );
|
|
|
|
on_error:
|
|
if( file_io_handle_is_open == 0 )
|
|
{
|
|
libbfio_handle_close(
|
|
file_io_handle,
|
|
NULL );
|
|
}
|
|
return( -1 );
|
|
}
|
|
|