Coverage Report

Created: 2025-06-13 07:22

/src/libmdmp/libmdmp/libmdmp_file_header.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * File header functions
3
 *
4
 * Copyright (C) 2014-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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libmdmp_debug.h"
28
#include "libmdmp_file_header.h"
29
#include "libmdmp_io_handle.h"
30
#include "libmdmp_libcerror.h"
31
#include "libmdmp_libcnotify.h"
32
#include "libmdmp_libfdatetime.h"
33
34
#include "mdmp_file_header.h"
35
36
/* Creates file header
37
 * Make sure the value file_header is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libmdmp_file_header_initialize(
41
     libmdmp_file_header_t **file_header,
42
     libcerror_error_t **error )
43
374
{
44
374
  static char *function = "libmdmp_file_header_initialize";
45
46
374
  if( file_header == NULL )
47
0
  {
48
0
    libcerror_error_set(
49
0
     error,
50
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52
0
     "%s: invalid file header.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
374
  if( *file_header != NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
62
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
63
0
     "%s: invalid file header value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
374
  *file_header = memory_allocate_structure(
69
374
                  libmdmp_file_header_t );
70
71
374
  if( *file_header == NULL )
72
0
  {
73
0
    libcerror_error_set(
74
0
     error,
75
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
76
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
77
0
     "%s: unable to create file header.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
374
  if( memory_set(
83
374
       *file_header,
84
374
       0,
85
374
       sizeof( libmdmp_file_header_t ) ) == NULL )
86
0
  {
87
0
    libcerror_error_set(
88
0
     error,
89
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
90
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
91
0
     "%s: unable to clear file header.",
92
0
     function );
93
94
0
    goto on_error;
95
0
  }
96
374
  return( 1 );
97
98
0
on_error:
99
0
  if( *file_header != NULL )
100
0
  {
101
0
    memory_free(
102
0
     *file_header );
103
104
0
    *file_header = NULL;
105
0
  }
106
0
  return( -1 );
107
374
}
108
109
/* Frees file header
110
 * Returns 1 if successful or -1 on error
111
 */
112
int libmdmp_file_header_free(
113
     libmdmp_file_header_t **file_header,
114
     libcerror_error_t **error )
115
374
{
116
374
  static char *function = "libmdmp_file_header_free";
117
118
374
  if( file_header == NULL )
119
0
  {
120
0
    libcerror_error_set(
121
0
     error,
122
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
123
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
124
0
     "%s: invalid file header.",
125
0
     function );
126
127
0
    return( -1 );
128
0
  }
129
374
  if( *file_header != NULL )
130
374
  {
131
374
    memory_free(
132
374
     *file_header );
133
134
374
    *file_header = NULL;
135
374
  }
136
374
  return( 1 );
137
374
}
138
139
/* Reads the file header data
140
 * Returns 1 if successful or -1 on error
141
 */
142
int libmdmp_file_header_read_data(
143
     libmdmp_file_header_t *file_header,
144
     const uint8_t *data,
145
     size_t data_size,
146
     libcerror_error_t **error )
147
363
{
148
363
  static char *function = "libmdmp_file_header_read_data";
149
150
#if defined( HAVE_DEBUG_OUTPUT )
151
  uint32_t value_32bit  = 0;
152
  uint16_t value_16bit  = 0;
153
#endif
154
155
363
  if( file_header == NULL )
156
0
  {
157
0
    libcerror_error_set(
158
0
     error,
159
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
160
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
161
0
     "%s: invalid file header.",
162
0
     function );
163
164
0
    return( -1 );
165
0
  }
166
363
  if( data == NULL )
167
0
  {
168
0
    libcerror_error_set(
169
0
     error,
170
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
171
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
172
0
     "%s: invalid data.",
173
0
     function );
174
175
0
    return( -1 );
176
0
  }
177
363
  if( ( data_size < sizeof( mdmp_file_header_t ) )
178
363
   || ( data_size > (size_t) SSIZE_MAX ) )
179
0
  {
180
0
    libcerror_error_set(
181
0
     error,
182
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
183
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
184
0
     "%s: invalid data size value out of bounds.",
185
0
     function );
186
187
0
    return( -1 );
188
0
  }
189
#if defined( HAVE_DEBUG_OUTPUT )
190
  if( libcnotify_verbose != 0 )
191
  {
192
    libcnotify_printf(
193
     "%s: file header data:\n",
194
     function );
195
    libcnotify_print_data(
196
     data,
197
     sizeof( mdmp_file_header_t ),
198
     0 );
199
  }
200
#endif
201
363
  if( memory_compare(
202
363
       ( (mdmp_file_header_t *) data )->signature,
203
363
       mdmp_file_signature,
204
363
       4 ) != 0 )
205
29
  {
206
29
    libcerror_error_set(
207
29
     error,
208
29
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
209
29
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
210
29
     "%s: invalid signature.",
211
29
     function );
212
213
29
    return( -1 );
214
29
  }
215
334
  byte_stream_copy_to_uint16_little_endian(
216
334
   ( (mdmp_file_header_t *) data )->version,
217
334
   file_header->version );
218
219
334
  byte_stream_copy_to_uint32_little_endian(
220
334
   ( (mdmp_file_header_t *) data )->number_of_streams,
221
334
   file_header->number_of_streams );
222
223
334
  byte_stream_copy_to_uint32_little_endian(
224
334
   ( (mdmp_file_header_t *) data )->streams_directory_rva,
225
334
   file_header->streams_directory_offset );
226
227
#if defined( HAVE_DEBUG_OUTPUT )
228
  if( libcnotify_verbose != 0 )
229
  {
230
    libcnotify_printf(
231
     "%s: signature\t\t\t\t: %c%c%c%c\n",
232
     function,
233
     ( (mdmp_file_header_t *) data )->signature[ 0 ],
234
     ( (mdmp_file_header_t *) data )->signature[ 1 ],
235
     ( (mdmp_file_header_t *) data )->signature[ 2 ],
236
     ( (mdmp_file_header_t *) data )->signature[ 3 ] );
237
238
    libcnotify_printf(
239
     "%s: version\t\t\t\t: 0x%04" PRIx16 "\n",
240
     function,
241
     file_header->version );
242
243
    byte_stream_copy_to_uint16_little_endian(
244
     ( (mdmp_file_header_t *) data )->implementation_version,
245
     value_16bit );
246
    libcnotify_printf(
247
     "%s: implementation version\t\t: 0x%04" PRIx16 "\n",
248
     function,
249
     value_16bit );
250
251
    libcnotify_printf(
252
     "%s: number of streams\t\t\t: 0x%08" PRIx32 "\n",
253
     function,
254
     file_header->number_of_streams );
255
256
    libcnotify_printf(
257
     "%s: streams directory RVA\t\t: 0x%08" PRIx32 "\n",
258
     function,
259
     file_header->streams_directory_offset );
260
261
    byte_stream_copy_to_uint32_little_endian(
262
     ( (mdmp_file_header_t *) data )->checksum,
263
     value_32bit );
264
    libcnotify_printf(
265
     "%s: checksum\t\t\t\t: 0x%08" PRIx32 "\n",
266
     function,
267
     value_32bit );
268
269
/* TODO print date time value */
270
    byte_stream_copy_to_uint32_little_endian(
271
     ( (mdmp_file_header_t *) data )->timestamp,
272
     value_32bit );
273
    libcnotify_printf(
274
     "%s: timestamp\t\t\t\t: 0x%08" PRIx32 "\n",
275
     function,
276
     value_32bit );
277
278
    byte_stream_copy_to_uint32_little_endian(
279
     ( (mdmp_file_header_t *) data )->file_flags,
280
     value_32bit );
281
    libcnotify_printf(
282
     "%s: file flags\t\t\t\t: 0x%08" PRIx32 "\n",
283
     function,
284
     value_32bit );
285
    libmdmp_debug_print_file_flags(
286
     value_32bit );
287
    libcnotify_printf(
288
     "\n" );
289
290
    libcnotify_printf(
291
     "\n" );
292
  }
293
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
294
295
334
  return( 1 );
296
363
}
297
298
/* Reads the file header
299
 * Returns 1 if successful or -1 on error
300
 */
301
int libmdmp_file_header_read_file_io_handle(
302
     libmdmp_file_header_t *file_header,
303
     libbfio_handle_t *file_io_handle,
304
     libcerror_error_t **error )
305
374
{
306
374
  uint8_t file_header_data[ sizeof( mdmp_file_header_t ) ];
307
308
374
  static char *function = "libmdmp_file_header_read_file_io_handle";
309
374
  ssize_t read_count    = 0;
310
311
374
  if( file_header == NULL )
312
0
  {
313
0
    libcerror_error_set(
314
0
     error,
315
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
316
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
317
0
     "%s: invalid file header.",
318
0
     function );
319
320
0
    return( -1 );
321
0
  }
322
#if defined( HAVE_DEBUG_OUTPUT )
323
  if( libcnotify_verbose != 0 )
324
  {
325
    libcnotify_printf(
326
     "%s: reading file header at offset: 0 (0x00000000)\n",
327
     function );
328
  }
329
#endif
330
374
  read_count = libbfio_handle_read_buffer_at_offset(
331
374
                file_io_handle,
332
374
                file_header_data,
333
374
                sizeof( mdmp_file_header_t ),
334
374
                0,
335
374
                error );
336
337
374
  if( read_count != (ssize_t) sizeof( mdmp_file_header_t ) )
338
11
  {
339
11
    libcerror_error_set(
340
11
     error,
341
11
     LIBCERROR_ERROR_DOMAIN_IO,
342
11
     LIBCERROR_IO_ERROR_READ_FAILED,
343
11
     "%s: unable to read file header data at offset: 0 (0x00000000).",
344
11
     function );
345
346
11
    return( -1 );
347
11
  }
348
363
  if( libmdmp_file_header_read_data(
349
363
       file_header,
350
363
       file_header_data,
351
363
       sizeof( mdmp_file_header_t ),
352
363
       error ) != 1 )
353
29
  {
354
29
    libcerror_error_set(
355
29
     error,
356
29
     LIBCERROR_ERROR_DOMAIN_IO,
357
29
     LIBCERROR_IO_ERROR_READ_FAILED,
358
29
     "%s: unable to read file header.",
359
29
     function );
360
361
29
    return( -1 );
362
29
  }
363
334
  return( 1 );
364
363
}
365