Coverage Report

Created: 2023-06-07 06:53

/src/libexe/libexe/libexe_coff_header.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * COFF header functions
3
 *
4
 * Copyright (C) 2011-2023, 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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libexe_coff_header.h"
28
#include "libexe_debug.h"
29
#include "libexe_libcerror.h"
30
#include "libexe_libcnotify.h"
31
#include "libexe_libfdatetime.h"
32
33
#include "exe_file_header.h"
34
35
/* Creates a COFF header
36
 * Make sure the value coff_header is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libexe_coff_header_initialize(
40
     libexe_coff_header_t **coff_header,
41
     libcerror_error_t **error )
42
751
{
43
751
  static char *function = "libexe_coff_header_initialize";
44
45
751
  if( coff_header == NULL )
46
0
  {
47
0
    libcerror_error_set(
48
0
     error,
49
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51
0
     "%s: invalid COFF header.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
751
  if( *coff_header != NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
61
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62
0
     "%s: invalid COFF header value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
751
  *coff_header = memory_allocate_structure(
68
751
                  libexe_coff_header_t );
69
70
751
  if( *coff_header == NULL )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
75
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
76
0
     "%s: unable to create COFF header.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
751
  if( memory_set(
82
751
       *coff_header,
83
751
       0,
84
751
       sizeof( libexe_coff_header_t ) ) == NULL )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
89
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
90
0
     "%s: unable to clear file.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
751
  return( 1 );
96
97
0
on_error:
98
0
  if( *coff_header != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *coff_header );
102
103
0
    *coff_header = NULL;
104
0
  }
105
0
  return( -1 );
106
751
}
107
108
/* Frees a COFF header
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libexe_coff_header_free(
112
     libexe_coff_header_t **coff_header,
113
     libcerror_error_t **error )
114
751
{
115
751
  static char *function = "libexe_coff_header_free";
116
117
751
  if( coff_header == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid COFF header.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
751
  if( *coff_header != NULL )
129
751
  {
130
751
    memory_free(
131
751
     *coff_header );
132
133
751
    *coff_header = NULL;
134
751
  }
135
751
  return( 1 );
136
751
}
137
138
/* Reads the COFF header
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libexe_coff_header_read_data(
142
     libexe_coff_header_t *coff_header,
143
     const uint8_t *data,
144
     size_t data_size,
145
     libcerror_error_t **error )
146
744
{
147
744
  static char *function = "libexe_coff_header_read_data";
148
149
#if defined( HAVE_DEBUG_OUTPUT )
150
  uint32_t value_32bit  = 0;
151
  uint16_t value_16bit  = 0;
152
#endif
153
154
744
  if( coff_header == 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 COFF header.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
744
  if( data == NULL )
166
0
  {
167
0
    libcerror_error_set(
168
0
     error,
169
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
170
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
171
0
     "%s: invalid data.",
172
0
     function );
173
174
0
    return( -1 );
175
0
  }
176
744
  if( data_size < sizeof( exe_coff_header_t ) )
177
0
  {
178
0
    libcerror_error_set(
179
0
     error,
180
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
181
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
182
0
     "%s: invalid data size value too small.",
183
0
     function );
184
185
0
    return( -1 );
186
0
  }
187
744
  if( data_size > (size_t) SSIZE_MAX )
188
0
  {
189
0
    libcerror_error_set(
190
0
     error,
191
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
192
0
     LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
193
0
     "%s: invalid data size value exceeds maximum.",
194
0
     function );
195
196
0
    return( -1 );
197
0
  }
198
#if defined( HAVE_DEBUG_OUTPUT )
199
  if( libcnotify_verbose != 0 )
200
  {
201
    libcnotify_printf(
202
     "%s: COFF header:\n",
203
     function );
204
    libcnotify_print_data(
205
     data,
206
     sizeof( exe_coff_header_t ),
207
     0 );
208
  }
209
#endif
210
744
  byte_stream_copy_to_uint16_little_endian(
211
744
   ( (exe_coff_header_t *) data )->number_of_sections,
212
744
   coff_header->number_of_sections );
213
214
744
  byte_stream_copy_to_uint32_little_endian(
215
744
   ( (exe_coff_header_t *) data )->creation_time,
216
744
   coff_header->creation_time );
217
218
744
  byte_stream_copy_to_uint16_little_endian(
219
744
   ( (exe_coff_header_t *) data )->optional_header_size,
220
744
   coff_header->optional_header_size );
221
222
#if defined( HAVE_DEBUG_OUTPUT )
223
  if( libcnotify_verbose != 0 )
224
  {
225
    byte_stream_copy_to_uint16_little_endian(
226
     ( (exe_coff_header_t *) data )->target_architecture_type,
227
     value_16bit );
228
    libcnotify_printf(
229
     "%s: target architecture type\t\t\t: 0x%04" PRIx16 "\n",
230
     function,
231
     value_16bit );
232
233
    libcnotify_printf(
234
     "%s: number of sections\t\t\t: %" PRIu16 "\n",
235
     function,
236
     coff_header->number_of_sections );
237
238
    if( libexe_debug_print_posix_time_value(
239
         function,
240
         "creation time\t\t\t\t",
241
         ( (exe_coff_header_t *) data )->creation_time,
242
         4,
243
         LIBFDATETIME_ENDIAN_LITTLE,
244
         LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED,
245
         LIBFDATETIME_STRING_FORMAT_TYPE_CTIME | LIBFDATETIME_STRING_FORMAT_FLAG_DATE_TIME,
246
         error ) != 1 )
247
    {
248
      libcerror_error_set(
249
       error,
250
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
251
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
252
       "%s: unable to print POSIX time value.",
253
       function );
254
255
      return( -1 );
256
    }
257
    byte_stream_copy_to_uint32_little_endian(
258
     ( (exe_coff_header_t *) data )->symbol_table_offset,
259
     value_32bit );
260
    libcnotify_printf(
261
     "%s: symbol table offset\t\t\t: 0x%08" PRIx32 "\n",
262
     function,
263
     value_32bit );
264
265
    byte_stream_copy_to_uint32_little_endian(
266
     ( (exe_coff_header_t *) data )->number_of_symbols,
267
     value_32bit );
268
    libcnotify_printf(
269
     "%s: number of symbols\t\t\t\t: %" PRIu32 "\n",
270
     function,
271
     value_32bit );
272
273
    libcnotify_printf(
274
     "%s: optional header size\t\t\t: %" PRIu16 "\n",
275
     function,
276
     coff_header->optional_header_size );
277
278
    byte_stream_copy_to_uint16_little_endian(
279
     ( (exe_coff_header_t *) data )->characteristic_flags,
280
     value_16bit );
281
    libcnotify_printf(
282
     "%s: characteristic flags\t\t\t: 0x%04" PRIx16 "\n",
283
     function,
284
     value_16bit );
285
    libexe_debug_print_file_characteristic_flags(
286
     value_16bit );
287
    libcnotify_printf(
288
     "\n" );
289
  }
290
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
291
292
744
  return( 1 );
293
744
}
294
295
/* Reads the COFF header from a Basic File IO (bfio) handle
296
 * Returns 1 if successful or -1 on error
297
 */
298
int libexe_coff_header_read_file_io_handle(
299
     libexe_coff_header_t *coff_header,
300
     libbfio_handle_t *file_io_handle,
301
     off64_t file_offset,
302
     libcerror_error_t **error )
303
751
{
304
751
  uint8_t data[ sizeof( exe_coff_header_t ) ];
305
306
751
  static char *function = "libexe_coff_header_read_file_io_handle";
307
751
  ssize_t read_count    = 0;
308
309
#if defined( HAVE_DEBUG_OUTPUT )
310
  if( libcnotify_verbose != 0 )
311
  {
312
    libcnotify_printf(
313
     "%s: reading COFF header at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
314
     function,
315
     file_offset,
316
     file_offset );
317
  }
318
#endif
319
751
  read_count = libbfio_handle_read_buffer_at_offset(
320
751
                file_io_handle,
321
751
                data,
322
751
                sizeof( exe_coff_header_t ),
323
751
                file_offset,
324
751
                error );
325
326
751
  if( read_count != (ssize_t) sizeof( exe_coff_header_t ) )
327
7
  {
328
7
    libcerror_error_set(
329
7
     error,
330
7
     LIBCERROR_ERROR_DOMAIN_IO,
331
7
     LIBCERROR_IO_ERROR_READ_FAILED,
332
7
     "%s: unable to read COFF header data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
333
7
     function,
334
7
     file_offset,
335
7
     file_offset );
336
337
7
    return( -1 );
338
7
  }
339
744
  if( libexe_coff_header_read_data(
340
744
       coff_header,
341
744
       data,
342
744
       sizeof( exe_coff_header_t ),
343
744
       error ) != 1 )
344
0
  {
345
0
    libcerror_error_set(
346
0
     error,
347
0
     LIBCERROR_ERROR_DOMAIN_IO,
348
0
     LIBCERROR_IO_ERROR_READ_FAILED,
349
0
     "%s: unable to read COFF header at offset: %" PRIi64 " (0x%08" PRIx64 ").",
350
0
     function,
351
0
     file_offset,
352
0
     file_offset );
353
354
0
    return( -1 );
355
0
  }
356
744
  return( 1 );
357
744
}
358