/* Opens the resource file * Returns 1 if successful or -1 on error */ int resource_file_open( resource_file_t *resource_file, const system_character_t *filename, libcerror_error_t **error ) { static char *function = "resource_file_open"; uint32_t virtual_address = 0; if( resource_file == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid resource file.", function ); return( -1 ); } if( resource_file->is_open != 0 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid resource file already open.", function ); return( -1 ); } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) if( libexe_file_open_wide( resource_file->exe_file, filename, LIBEXE_OPEN_READ, error ) != 1 ) #else if( libexe_file_open( resource_file->exe_file, filename, LIBEXE_OPEN_READ, error ) != 1 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to open EXE file.", function ); goto on_error; } if( libexe_file_get_section_by_name( resource_file->exe_file, ".rsrc", 5, &( resource_file->resource_section ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve resource section.", function ); goto on_error; } if( libexe_section_get_virtual_address( resource_file->resource_section, &virtual_address, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve resource section virtual adress.", function ); goto on_error; } if( libexe_section_get_data_file_io_handle( resource_file->resource_section, &( resource_file->resource_section_file_io_handle ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve resource section file IO handle.", function ); goto on_error; } if( libwrc_stream_set_virtual_address( resource_file->resource_stream, virtual_address, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to set resource stream virtual adress.", function ); goto on_error; } if( libwrc_stream_open_file_io_handle( resource_file->resource_stream, resource_file->resource_section_file_io_handle, LIBWRC_OPEN_READ, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to open resource stream.", function ); goto on_error; } resource_file->is_open = 1; return( 1 ); on_error: if( resource_file->resource_section_file_io_handle != NULL ) { libbfio_handle_free( &( resource_file->resource_section_file_io_handle ), NULL ); } if( resource_file->resource_section != NULL ) { libexe_section_free( &( resource_file->resource_section ), NULL ); } libexe_file_close( resource_file->exe_file, NULL ); return( -1 ); }
/* Tests seeking in a file * Returns 1 if successful, 0 if not or -1 on error */ int exe_test_seek( libcstring_system_character_t *source, libcerror_error_t **error ) { libexe_file_t *file = NULL; libexe_section_t *section = NULL; size64_t section_size = 0; int number_of_sections = 0; int result = 0; int section_index = 0; if( libexe_file_initialize( &file, error ) != 1 ) { fprintf( stderr, "Unable to create file.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libexe_file_open_wide( file, source, LIBEXE_OPEN_READ, error ) != 1 ) #else if( libexe_file_open( file, source, LIBEXE_OPEN_READ, error ) != 1 ) #endif { fprintf( stderr, "Unable to open file.\n" ); goto on_error; } if( libexe_file_get_number_of_sections( file, &number_of_sections, error ) != 1 ) { fprintf( stderr, "Unable to retrieve number of sections.\n" ); goto on_error; } for( section_index = 0; section_index < number_of_sections; section_index++ ) { if( libexe_file_get_section( file, section_index, §ion, error ) != 1 ) { fprintf( stderr, "Unable to retrieve section: %d.\n", section_index ); goto on_error; } if( libexe_section_get_size( section, §ion_size, error ) != 1 ) { fprintf( stderr, "Unable to retrieve section size.\n" ); goto on_error; } fprintf( stdout, "Section: %d size: %" PRIu64 " bytes\n", section_index, section_size ); if( section_size == 0 ) { result = 1; } else { result = exe_test_seek_section( section, section_size ); if( result == -1 ) { fprintf( stderr, "Unable to seek in section: %d.\n", section_index ); goto on_error; } } if( libexe_section_free( §ion, error ) != 1 ) { fprintf( stderr, "Unable to free section: %d.\n", section_index ); goto on_error; } if( result != 1 ) { break; } } if( libexe_file_close( file, error ) != 0 ) { fprintf( stderr, "Unable to close file.\n" ); goto on_error; } if( libexe_file_free( &file, error ) != 1 ) { fprintf( stderr, "Unable to free file.\n" ); goto on_error; } return( result ); on_error: if( section != NULL ) { libexe_section_free( §ion, NULL ); } if( file != NULL ) { libexe_file_close( file, NULL ); libexe_file_free( &file, NULL ); } return( -1 ); }