Coverage Report

Created: 2025-08-28 07:10

/src/libnk2/libnk2/libnk2_file_header.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * File header functions
3
 *
4
 * Copyright (C) 2009-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 "libnk2_debug.h"
28
#include "libnk2_file_header.h"
29
#include "libnk2_io_handle.h"
30
#include "libnk2_libcerror.h"
31
#include "libnk2_libcnotify.h"
32
33
#include "nk2_file_header.h"
34
35
/* Creates file header
36
 * Make sure the value file_header is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libnk2_file_header_initialize(
40
     libnk2_file_header_t **file_header,
41
     libcerror_error_t **error )
42
535
{
43
535
  static char *function = "libnk2_file_header_initialize";
44
45
535
  if( file_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 file header.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
535
  if( *file_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 file header value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
535
  *file_header = memory_allocate_structure(
68
535
                  libnk2_file_header_t );
69
70
535
  if( *file_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 file header.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
535
  if( memory_set(
82
535
       *file_header,
83
535
       0,
84
535
       sizeof( libnk2_file_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 header.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
535
  return( 1 );
96
97
0
on_error:
98
0
  if( *file_header != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *file_header );
102
103
0
    *file_header = NULL;
104
0
  }
105
0
  return( -1 );
106
535
}
107
108
/* Frees file header
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libnk2_file_header_free(
112
     libnk2_file_header_t **file_header,
113
     libcerror_error_t **error )
114
535
{
115
535
  static char *function = "libnk2_file_header_free";
116
117
535
  if( file_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 file header.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
535
  if( *file_header != NULL )
129
535
  {
130
535
    memory_free(
131
535
     *file_header );
132
133
535
    *file_header = NULL;
134
535
  }
135
535
  return( 1 );
136
535
}
137
138
/* Reads the file header data
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libnk2_file_header_read_data(
142
     libnk2_file_header_t *file_header,
143
     const uint8_t *data,
144
     size_t data_size,
145
     libcerror_error_t **error )
146
529
{
147
529
  static char *function = "libnk2_file_header_read_data";
148
149
#if defined( HAVE_DEBUG_OUTPUT )
150
  uint32_t value_32bit  = 0;
151
#endif
152
153
529
  if( file_header == NULL )
154
0
  {
155
0
    libcerror_error_set(
156
0
     error,
157
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
158
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
159
0
     "%s: invalid file header.",
160
0
     function );
161
162
0
    return( -1 );
163
0
  }
164
529
  if( data == NULL )
165
0
  {
166
0
    libcerror_error_set(
167
0
     error,
168
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
169
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
170
0
     "%s: invalid data.",
171
0
     function );
172
173
0
    return( -1 );
174
0
  }
175
529
  if( data_size < sizeof( nk2_file_header_t ) )
176
0
  {
177
0
    libcerror_error_set(
178
0
     error,
179
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
180
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
181
0
     "%s: invalid data size value too small.",
182
0
     function );
183
184
0
    return( -1 );
185
0
  }
186
529
  if( data_size > (size_t) SSIZE_MAX )
187
0
  {
188
0
    libcerror_error_set(
189
0
     error,
190
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
191
0
     LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
192
0
     "%s: invalid data size value exceeds maximum.",
193
0
     function );
194
195
0
    return( -1 );
196
0
  }
197
#if defined( HAVE_DEBUG_OUTPUT )
198
  if( libcnotify_verbose != 0 )
199
  {
200
    libcnotify_printf(
201
     "%s: file header:\n",
202
     function );
203
    libcnotify_print_data(
204
     (uint8_t *) data,
205
     sizeof( nk2_file_header_t ),
206
     0 );
207
  }
208
#endif
209
529
  if( memory_compare(
210
529
       ( (nk2_file_header_t *) data )->signature,
211
529
       nk2_file_signature,
212
529
       4 ) != 0 )
213
42
  {
214
42
    libcerror_error_set(
215
42
     error,
216
42
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
217
42
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
218
42
     "%s: invalid file signature.",
219
42
     function );
220
221
42
    return( -1 );
222
42
  }
223
487
  byte_stream_copy_to_uint32_little_endian(
224
487
   ( (nk2_file_header_t *) data )->number_of_items,
225
487
   file_header->number_of_items );
226
227
#if defined( HAVE_DEBUG_OUTPUT )
228
  if( libcnotify_verbose != 0 )
229
  {
230
    byte_stream_copy_to_uint32_little_endian(
231
     ( (nk2_file_header_t *) data )->signature,
232
     value_32bit );
233
    libcnotify_printf(
234
     "%s: signature\t\t\t\t\t: 0x%08" PRIx32 "\n",
235
     function,
236
     value_32bit );
237
238
    byte_stream_copy_to_uint32_little_endian(
239
     ( (nk2_file_header_t *) data )->unknown1,
240
     value_32bit );
241
    libcnotify_printf(
242
     "%s: unknown1\t\t\t\t\t: 0x%08" PRIx32 "\n",
243
     function,
244
     value_32bit );
245
246
    byte_stream_copy_to_uint32_little_endian(
247
     ( (nk2_file_header_t *) data )->unknown2,
248
     value_32bit );
249
    libcnotify_printf(
250
     "%s: unknown2\t\t\t\t\t: 0x%08" PRIx32 "\n",
251
     function,
252
     value_32bit );
253
254
    libcnotify_printf(
255
     "%s: number of items\t\t\t\t: %" PRIu32 "\n",
256
     function,
257
     file_header->number_of_items );
258
259
    libcnotify_printf(
260
     "\n" );
261
  }
262
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
263
487
  return( 1 );
264
529
}
265
266
/* Reads the file header
267
 * Returns 1 if successful or -1 on error
268
 */
269
int libnk2_file_header_read_file_io_handle(
270
     libnk2_file_header_t *file_header,
271
     libbfio_handle_t *file_io_handle,
272
     libcerror_error_t **error )
273
535
{
274
535
  uint8_t file_header_data[ sizeof( nk2_file_header_t ) ];
275
276
535
  static char *function = "libnk2_file_header_read_file_io_handle";
277
535
  ssize_t read_count    = 0;
278
279
535
  if( file_header == NULL )
280
0
  {
281
0
    libcerror_error_set(
282
0
     error,
283
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
284
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
285
0
     "%s: invalid file header.",
286
0
     function );
287
288
0
    return( -1 );
289
0
  }
290
#if defined( HAVE_DEBUG_OUTPUT )
291
  if( libcnotify_verbose != 0 )
292
  {
293
    libcnotify_printf(
294
     "%s: reading file header at offset: 0 (0x00000000)\n",
295
     function );
296
  }
297
#endif
298
535
  read_count = libbfio_handle_read_buffer_at_offset(
299
535
                file_io_handle,
300
535
                file_header_data,
301
535
                sizeof( nk2_file_header_t ),
302
535
                0,
303
535
                error );
304
305
535
  if( read_count != (ssize_t) sizeof( nk2_file_header_t ) )
306
6
  {
307
6
    libcerror_error_set(
308
6
     error,
309
6
     LIBCERROR_ERROR_DOMAIN_IO,
310
6
     LIBCERROR_IO_ERROR_READ_FAILED,
311
6
     "%s: unable to read file header data at offset: 0 (0x00000000).",
312
6
     function );
313
314
6
    return( -1 );
315
6
  }
316
529
  if( libnk2_file_header_read_data(
317
529
       file_header,
318
529
       file_header_data,
319
529
       sizeof( nk2_file_header_t ),
320
529
       error ) != 1 )
321
42
  {
322
42
    libcerror_error_set(
323
42
     error,
324
42
     LIBCERROR_ERROR_DOMAIN_IO,
325
42
     LIBCERROR_IO_ERROR_READ_FAILED,
326
42
     "%s: unable to read file header.",
327
42
     function );
328
329
42
    return( -1 );
330
42
  }
331
487
  return( 1 );
332
529
}
333