Files
libyal-libexe/libexe/libexe_file.c
T
2016-03-28 06:56:39 +02:00

1070 lines
23 KiB
C

/*
* File functions
*
* Copyright (c) 2011, Joachim Metz <jbmetz@users.sourceforge.net>
*
* Refer to AUTHORS for acknowledgements.
*
* This software 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 software 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 software. If not, see <http://www.gnu.org/licenses/>.
*/
#include <common.h>
#include <memory.h>
#include <types.h>
#include <libcstring.h>
#include <liberror.h>
#include <libnotify.h>
#include "libexe_array_type.h"
#include "libexe_codepage.h"
#include "libexe_debug.h"
#include "libexe_definitions.h"
#include "libexe_io_handle.h"
#include "libexe_file.h"
#include "libexe_libbfio.h"
#include "libexe_section.h"
#include "libexe_section_descriptor.h"
/* Initializes a file
* Make sure the value file is pointing to is set to NULL
* Returns 1 if successful or -1 on error
*/
int libexe_file_initialize(
libexe_file_t **file,
liberror_error_t **error )
{
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_initialize";
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
if( *file == NULL )
{
internal_file = memory_allocate_structure(
libexe_internal_file_t );
if( internal_file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_MEMORY,
LIBERROR_MEMORY_ERROR_INSUFFICIENT,
"%s: unable to create file.",
function );
goto on_error;
}
if( memory_set(
internal_file,
0,
sizeof( libexe_internal_file_t ) ) == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_MEMORY,
LIBERROR_MEMORY_ERROR_SET_FAILED,
"%s: unable to clear file.",
function );
memory_free(
internal_file );
return( -1 );
}
if( libexe_array_initialize(
&( internal_file->sections_array ),
0,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create sections array.",
function );
goto on_error;
}
if( libexe_io_handle_initialize(
&( internal_file->io_handle ),
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create IO handle.",
function );
goto on_error;
}
*file = (libexe_file_t *) internal_file;
}
return( 1 );
on_error:
if( internal_file != NULL )
{
if( internal_file->sections_array != NULL )
{
libexe_array_free(
&( internal_file->sections_array ),
NULL,
NULL );
}
memory_free(
internal_file );
}
return( -1 );
}
/* Frees a file
* Returns 1 if successful or -1 on error
*/
int libexe_file_free(
libexe_file_t **file,
liberror_error_t **error )
{
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_free";
int result = 1;
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
if( *file != NULL )
{
internal_file = (libexe_internal_file_t *) *file;
if( internal_file->file_io_handle != NULL )
{
if( libexe_file_close(
*file,
error ) != 0 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_IO,
LIBERROR_IO_ERROR_CLOSE_FAILED,
"%s: unable to close file.",
function );
result = -1;
}
}
*file = NULL;
if( libexe_array_free(
&( internal_file->sections_array ),
(int (*)(intptr_t *, liberror_error_t **)) &libexe_section_descriptor_free,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_FINALIZE_FAILED,
"%s: unable to free sections array.",
function );
result = -1;
}
if( libexe_io_handle_free(
&( internal_file->io_handle ),
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_FINALIZE_FAILED,
"%s: unable to free IO handle.",
function );
result = -1;
}
memory_free(
internal_file );
}
return( result );
}
/* Signals the file to abort its current activity
* Returns 1 if successful or -1 on error
*/
int libexe_file_signal_abort(
libexe_file_t *file,
liberror_error_t **error )
{
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_signal_abort";
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
internal_file = (libexe_internal_file_t *) file;
if( internal_file->io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid internal file - missing IO handle.",
function );
return( -1 );
}
internal_file->io_handle->abort = 1;
return( 1 );
}
/* Opens a file
* Returns 1 if successful or -1 on error
*/
int libexe_file_open(
libexe_file_t *file,
const char *filename,
int access_flags,
liberror_error_t **error )
{
libbfio_handle_t *file_io_handle = NULL;
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_open";
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
internal_file = (libexe_internal_file_t *) file;
if( filename == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid filename.",
function );
return( -1 );
}
if( ( ( access_flags & LIBEXE_ACCESS_FLAG_READ ) == 0 )
&& ( ( access_flags & LIBEXE_ACCESS_FLAG_WRITE ) == 0 ) )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: unsupported access flags.",
function );
return( -1 );
}
if( ( access_flags & LIBEXE_ACCESS_FLAG_WRITE ) != 0 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: write access currently not supported.",
function );
return( -1 );
}
if( libbfio_file_initialize(
&file_io_handle,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create file IO handle.",
function );
goto on_error;
}
#if defined( HAVE_DEBUG_OUTPUT )
if( libbfio_handle_set_track_offsets_read(
file_io_handle,
1,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_SET_FAILED,
"%s: unable to set track offsets read in file IO handle.",
function );
goto on_error;
}
#endif
if( libbfio_file_set_name(
file_io_handle,
filename,
libcstring_narrow_string_length(
filename ) + 1,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_SET_FAILED,
"%s: unable to set filename in file IO handle.",
function );
goto on_error;
}
if( libexe_file_open_file_io_handle(
file,
file_io_handle,
access_flags,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_IO,
LIBERROR_IO_ERROR_OPEN_FAILED,
"%s: unable to open file: %s.",
function,
filename );
goto on_error;
}
internal_file->file_io_handle_created_in_library = 1;
return( 1 );
on_error:
if( file_io_handle != NULL )
{
libbfio_handle_free(
&file_io_handle,
NULL );
}
return( -1 );
}
#if defined( HAVE_WIDE_CHARACTER_TYPE )
/* Opens a file
* Returns 1 if successful or -1 on error
*/
int libexe_file_open_wide(
libexe_file_t *file,
const wchar_t *filename,
int access_flags,
liberror_error_t **error )
{
libbfio_handle_t *file_io_handle = NULL;
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_open_wide";
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
internal_file = (libexe_internal_file_t *) file;
if( filename == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid filename.",
function );
return( -1 );
}
if( ( ( access_flags & LIBEXE_ACCESS_FLAG_READ ) == 0 )
&& ( ( access_flags & LIBEXE_ACCESS_FLAG_WRITE ) == 0 ) )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: unsupported access flags.",
function );
return( -1 );
}
if( ( access_flags & LIBEXE_ACCESS_FLAG_WRITE ) != 0 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: write access currently not supported.",
function );
return( -1 );
}
if( libbfio_file_initialize(
&file_io_handle,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create file IO handle.",
function );
goto on_error;
}
#if defined( HAVE_DEBUG_OUTPUT )
if( libbfio_handle_set_track_offsets_read(
file_io_handle,
1,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_SET_FAILED,
"%s: unable to set track offsets read in file IO handle.",
function );
goto on_error;
}
#endif
if( libbfio_file_set_name_wide(
file_io_handle,
filename,
libcstring_wide_string_length(
filename ) + 1,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_SET_FAILED,
"%s: unable to set filename in file IO handle.",
function );
goto on_error;
}
if( libexe_file_open_file_io_handle(
file,
file_io_handle,
access_flags,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_IO,
LIBERROR_IO_ERROR_OPEN_FAILED,
"%s: unable to open file: %ls.",
function,
filename );
goto on_error;
}
internal_file->file_io_handle_created_in_library = 1;
return( 1 );
on_error:
if( file_io_handle != NULL )
{
libbfio_handle_free(
&file_io_handle,
NULL );
}
return( -1 );
}
#endif
/* Opens a file using a Basic File IO (bfio) handle
* Returns 1 if successful or -1 on error
*/
int libexe_file_open_file_io_handle(
libexe_file_t *file,
libbfio_handle_t *file_io_handle,
int access_flags,
liberror_error_t **error )
{
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_open_file_io_handle";
int bfio_access_flags = 0;
int file_io_handle_is_open = 0;
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
internal_file = (libexe_internal_file_t *) file;
if( internal_file->file_io_handle != NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
"%s: invalid internal file - file IO handle already set.",
function );
return( -1 );
}
if( file_io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file IO handle.",
function );
return( -1 );
}
if( ( ( access_flags & LIBEXE_ACCESS_FLAG_READ ) == 0 )
&& ( ( access_flags & LIBEXE_ACCESS_FLAG_WRITE ) == 0 ) )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: unsupported access flags.",
function );
return( -1 );
}
if( ( access_flags & LIBEXE_ACCESS_FLAG_WRITE ) != 0 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: write access currently not supported.",
function );
return( -1 );
}
if( ( access_flags & LIBEXE_ACCESS_FLAG_READ ) != 0 )
{
bfio_access_flags = LIBBFIO_FLAG_READ;
}
internal_file->file_io_handle = file_io_handle;
file_io_handle_is_open = libbfio_handle_is_open(
internal_file->file_io_handle,
error );
if( file_io_handle_is_open == -1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_IO,
LIBERROR_IO_ERROR_OPEN_FAILED,
"%s: unable to open file.",
function );
return( -1 );
}
else if( file_io_handle_is_open == 0 )
{
if( libbfio_handle_open(
internal_file->file_io_handle,
bfio_access_flags,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_IO,
LIBERROR_IO_ERROR_OPEN_FAILED,
"%s: unable to open file IO handle.",
function );
return( -1 );
}
}
if( libexe_file_open_read(
internal_file,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_IO,
LIBERROR_IO_ERROR_READ_FAILED,
"%s: unable to read from file handle.",
function );
return( -1 );
}
return( 1 );
}
/* Closes a file
* Returns 0 if successful or -1 on error
*/
int libexe_file_close(
libexe_file_t *file,
liberror_error_t **error )
{
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_close";
int result = 0;
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
internal_file = (libexe_internal_file_t *) file;
if( internal_file->file_io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid file - missing file IO handle.",
function );
return( -1 );
}
if( internal_file->file_io_handle_created_in_library != 0 )
{
#if defined( HAVE_DEBUG_OUTPUT )
if( libnotify_verbose != 0 )
{
if( libexe_debug_print_read_offsets(
internal_file->file_io_handle,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_PRINT_FAILED,
"%s: unable to print the read offsets.",
function );
result = -1;
}
}
#endif
if( libbfio_handle_close(
internal_file->file_io_handle,
error ) != 0 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_IO,
LIBERROR_IO_ERROR_CLOSE_FAILED,
"%s: unable to close file IO handle.",
function );
result = -1;
}
if( libbfio_handle_free(
&( internal_file->file_io_handle ),
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_FINALIZE_FAILED,
"%s: unable to free file IO handle.",
function );
result = -1;
}
}
internal_file->file_io_handle = NULL;
internal_file->file_io_handle_created_in_library = 0;
if( libexe_array_resize(
internal_file->sections_array,
0,
(int (*)(intptr_t *, liberror_error_t **)) &libexe_section_descriptor_free,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_RESIZE_FAILED,
"%s: unable to resize sections array.",
function );
result = -1;
}
return( result );
}
/* Opens a file for reading
* Returns 1 if successful or -1 on error
*/
int libexe_file_open_read(
libexe_internal_file_t *internal_file,
liberror_error_t **error )
{
static char *function = "libexe_file_open_read";
uint16_t number_of_sections = 0;
if( internal_file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid internal file.",
function );
return( -1 );
}
if( internal_file->io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid internal file - missing IO handle.",
function );
return( -1 );
}
if( internal_file->io_handle->abort != 0 )
{
internal_file->io_handle->abort = 0;
}
#if defined( HAVE_DEBUG_OUTPUT )
if( libnotify_verbose != 0 )
{
libnotify_printf(
"Reading file header:\n" );
}
#endif
if( libexe_io_handle_read_file_header(
internal_file->io_handle,
internal_file->file_io_handle,
&number_of_sections,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_IO,
LIBERROR_IO_ERROR_READ_FAILED,
"%s: unable to read file header.",
function );
return( -1 );
}
#if defined( HAVE_DEBUG_OUTPUT )
if( libnotify_verbose != 0 )
{
libnotify_printf(
"Reading section table:\n" );
}
#endif
if( libexe_io_handle_read_section_table(
internal_file->io_handle,
internal_file->file_io_handle,
number_of_sections,
internal_file->sections_array,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_IO,
LIBERROR_IO_ERROR_READ_FAILED,
"%s: unable to read section table.",
function );
return( -1 );
}
return( 1 );
}
/* Retrieves the file ASCII codepage
* Returns 1 if successful or -1 on error
*/
int libexe_file_get_ascii_codepage(
libexe_file_t *file,
int *ascii_codepage,
liberror_error_t **error )
{
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_get_ascii_codepage";
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
internal_file = (libexe_internal_file_t *) file;
if( internal_file->io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid internal file - missing IO handle.",
function );
return( -1 );
}
if( ascii_codepage == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid ASCII codepage.",
function );
return( -1 );
}
*ascii_codepage = internal_file->io_handle->ascii_codepage;
return( 1 );
}
/* Sets the file ASCII codepage
* Returns 1 if successful or -1 on error
*/
int libexe_file_set_ascii_codepage(
libexe_file_t *file,
int ascii_codepage,
liberror_error_t **error )
{
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_set_ascii_codepage";
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
internal_file = (libexe_internal_file_t *) file;
if( internal_file->io_handle == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid internal file - missing IO handle.",
function );
return( -1 );
}
if( ( ascii_codepage != LIBEXE_CODEPAGE_ASCII )
&& ( ascii_codepage != LIBEXE_CODEPAGE_WINDOWS_874 )
&& ( ascii_codepage != LIBEXE_CODEPAGE_WINDOWS_1250 )
&& ( ascii_codepage != LIBEXE_CODEPAGE_WINDOWS_1251 )
&& ( ascii_codepage != LIBEXE_CODEPAGE_WINDOWS_1252 )
&& ( ascii_codepage != LIBEXE_CODEPAGE_WINDOWS_1253 )
&& ( ascii_codepage != LIBEXE_CODEPAGE_WINDOWS_1254 )
&& ( ascii_codepage != LIBEXE_CODEPAGE_WINDOWS_1256 )
&& ( ascii_codepage != LIBEXE_CODEPAGE_WINDOWS_1257 )
&& ( ascii_codepage != LIBEXE_CODEPAGE_WINDOWS_1258 ) )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: unsupported ASCII codepage.",
function );
return( -1 );
}
internal_file->io_handle->ascii_codepage = ascii_codepage;
return( 1 );
}
/* Retrieves the number of sections
* Returns 1 if successful or -1 on error
*/
int libexe_file_get_number_of_sections(
libexe_file_t *file,
int *number_of_sections,
liberror_error_t **error )
{
libexe_internal_file_t *internal_file = NULL;
static char *function = "libexe_file_get_number_of_sections";
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
internal_file = (libexe_internal_file_t *) file;
if( libexe_array_get_number_of_entries(
internal_file->sections_array,
number_of_sections,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to retrieve number of sections.",
function );
return( -1 );
}
return( 1 );
}
/* Retrieves a specific section
* Returns 1 if successful or -1 on error
*/
int libexe_file_get_section(
libexe_file_t *file,
int section_index,
libexe_section_t **section,
liberror_error_t **error )
{
libexe_internal_file_t *internal_file = NULL;
libexe_section_descriptor_t *section_descriptor = NULL;
static char *function = "libexe_file_get_section";
if( file == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file.",
function );
return( -1 );
}
internal_file = (libexe_internal_file_t *) file;
if( section == NULL )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_ARGUMENTS,
LIBERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid section.",
function );
return( -1 );
}
if( libexe_array_get_entry_by_index(
internal_file->sections_array,
section_index,
(intptr_t **) &section_descriptor,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to retrieve event section descriptor: %d.",
function,
section_index );
return( -1 );
}
if( libexe_section_initialize(
section,
internal_file->io_handle,
internal_file->file_io_handle,
section_descriptor,
LIBEXE_SECTION_FLAGS_DEFAULT,
error ) != 1 )
{
liberror_error_set(
error,
LIBERROR_ERROR_DOMAIN_RUNTIME,
LIBERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create section.",
function );
return( -1 );
}
return( 1 );
}