Coverage Report

Created: 2024-02-25 07:20

/src/libexe/libexe/libexe_import_table.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Import table functions
3
 *
4
 * Copyright (C) 2011-2024, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <memory.h>
24
#include <types.h>
25
26
#include "libexe_import_table.h"
27
#include "libexe_libcerror.h"
28
#include "libexe_libcnotify.h"
29
#include "libexe_libfcache.h"
30
#include "libexe_libfdata.h"
31
32
/* Creates an import table
33
 * Make sure the value import_table is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libexe_import_table_initialize(
37
     libexe_import_table_t **import_table,
38
     libcerror_error_t **error )
39
136
{
40
136
  static char *function = "libexe_import_table_initialize";
41
42
136
  if( import_table == NULL )
43
0
  {
44
0
    libcerror_error_set(
45
0
     error,
46
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
47
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
48
0
     "%s: invalid import table.",
49
0
     function );
50
51
0
    return( -1 );
52
0
  }
53
136
  if( *import_table != NULL )
54
0
  {
55
0
    libcerror_error_set(
56
0
     error,
57
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
58
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
59
0
     "%s: invalid import table value already set.",
60
0
     function );
61
62
0
    return( -1 );
63
0
  }
64
136
  *import_table = memory_allocate_structure(
65
136
                   libexe_import_table_t );
66
67
136
  if( *import_table == NULL )
68
0
  {
69
0
    libcerror_error_set(
70
0
     error,
71
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
72
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
73
0
     "%s: unable to create import table.",
74
0
     function );
75
76
0
    goto on_error;
77
0
  }
78
136
  if( memory_set(
79
136
       *import_table,
80
136
       0,
81
136
       sizeof( libexe_import_table_t ) ) == NULL )
82
0
  {
83
0
    libcerror_error_set(
84
0
     error,
85
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
86
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
87
0
     "%s: unable to clear import table.",
88
0
     function );
89
90
0
    memory_free(
91
0
     *import_table );
92
93
0
    *import_table = NULL;
94
95
0
    return( -1 );
96
0
  }
97
136
  return( 1 );
98
99
0
on_error:
100
0
  if( *import_table != NULL )
101
0
  {
102
0
    memory_free(
103
0
     *import_table );
104
105
0
    *import_table = NULL;
106
0
  }
107
0
  return( -1 );
108
136
}
109
110
/* Frees an import table
111
 * Returns 1 if successful or -1 on error
112
 */
113
int libexe_import_table_free(
114
     libexe_import_table_t **import_table,
115
     libcerror_error_t **error )
116
136
{
117
136
  static char *function = "libexe_import_table_free";
118
119
136
  if( import_table == NULL )
120
0
  {
121
0
    libcerror_error_set(
122
0
     error,
123
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
124
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
125
0
     "%s: invalid import table.",
126
0
     function );
127
128
0
    return( -1 );
129
0
  }
130
136
  if( *import_table != NULL )
131
136
  {
132
136
    memory_free(
133
136
     *import_table );
134
135
136
    *import_table = NULL;
136
136
  }
137
136
  return( 1 );
138
136
}
139
140
/* Reads the import table
141
 * Returns 1 if successful or -1 on error
142
 */
143
int libexe_import_table_read(
144
     libexe_import_table_t *import_table,
145
     libbfio_handle_t *file_io_handle,
146
     uint32_t file_offset,
147
     uint32_t size,
148
     libcerror_error_t **error )
149
136
{
150
136
  uint8_t *data         = NULL;
151
136
  static char *function = "libexe_import_table_read";
152
136
  size_t read_count     = 0;
153
154
136
  if( import_table == NULL )
155
0
  {
156
0
    libcerror_error_set(
157
0
     error,
158
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160
0
     "%s: invalid import table.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
136
  if( ( size == 0 )
166
136
   || ( size > MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
167
34
  {
168
34
    libcerror_error_set(
169
34
     error,
170
34
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
171
34
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
172
34
     "%s: invalid size value out of bounds.",
173
34
     function );
174
175
34
    return( -1 );
176
34
  }
177
102
  data = (uint8_t *) memory_allocate(
178
102
                      sizeof( uint8_t ) * size );
179
180
102
  if( data == NULL )
181
0
  {
182
0
    libcerror_error_set(
183
0
     error,
184
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
185
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
186
0
     "%s: unable to create import table.",
187
0
     function );
188
189
0
    goto on_error;
190
0
  }
191
#if defined( HAVE_DEBUG_OUTPUT )
192
  if( libcnotify_verbose != 0 )
193
  {
194
    libcnotify_printf(
195
     "%s: reading import table at offset: %" PRIu32 " (0x%08" PRIx32 ")\n",
196
     function,
197
     file_offset,
198
     file_offset );
199
  }
200
#endif
201
102
  read_count = libbfio_handle_read_buffer_at_offset(
202
102
                file_io_handle,
203
102
                data,
204
102
                size,
205
102
                (off64_t) file_offset,
206
102
                error );
207
208
102
  if( read_count != (ssize_t) size )
209
67
  {
210
67
    libcerror_error_set(
211
67
     error,
212
67
     LIBCERROR_ERROR_DOMAIN_IO,
213
67
     LIBCERROR_IO_ERROR_READ_FAILED,
214
67
     "%s: unable to read import table at offset: %" PRIu32 " (0x%08" PRIx32 ").",
215
67
     function,
216
67
     file_offset,
217
67
     file_offset );
218
219
67
    goto on_error;
220
67
  }
221
#if defined( HAVE_DEBUG_OUTPUT )
222
  if( libcnotify_verbose != 0 )
223
  {
224
    libcnotify_printf(
225
     "%s: import table:\n",
226
     function );
227
    libcnotify_print_data(
228
     data,
229
     (size_t) size,
230
     0 );
231
  }
232
#endif
233
  /* TODO extract values */
234
235
35
  memory_free(
236
35
   data );
237
238
35
  return( 1 );
239
240
67
on_error:
241
67
  if( data != NULL )
242
67
  {
243
67
    memory_free(
244
67
     data );
245
67
  }
246
67
  return( -1 );
247
102
}
248