Coverage Report

Created: 2025-09-05 06:58

/src/libfsntfs/libfsntfs/libfsntfs_index_entry_header.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The NTFS index entry header functions
3
 *
4
 * Copyright (C) 2010-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 "libfsntfs_debug.h"
28
#include "libfsntfs_index_entry_header.h"
29
#include "libfsntfs_libcerror.h"
30
#include "libfsntfs_libcnotify.h"
31
32
#include "fsntfs_index.h"
33
34
const char fsntfs_index_entry_signature[ 4 ] = { 'I', 'N', 'D', 'X' };
35
36
/* Creates an index entry header
37
 * Make sure the value index_entry_header is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libfsntfs_index_entry_header_initialize(
41
     libfsntfs_index_entry_header_t **index_entry_header,
42
     libcerror_error_t **error )
43
962
{
44
962
  static char *function = "libfsntfs_index_entry_header_initialize";
45
46
962
  if( index_entry_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 index entry header.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
962
  if( *index_entry_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 index entry header value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
962
  *index_entry_header = memory_allocate_structure(
69
962
                         libfsntfs_index_entry_header_t );
70
71
962
  if( *index_entry_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 index entry header.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
962
  if( memory_set(
83
962
       *index_entry_header,
84
962
       0,
85
962
       sizeof( libfsntfs_index_entry_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 index entry header.",
92
0
     function );
93
94
0
    goto on_error;
95
0
  }
96
962
  return( 1 );
97
98
0
on_error:
99
0
  if( *index_entry_header != NULL )
100
0
  {
101
0
    memory_free(
102
0
     *index_entry_header );
103
104
0
    *index_entry_header = NULL;
105
0
  }
106
0
  return( -1 );
107
962
}
108
109
/* Frees an index entry header
110
 * Returns 1 if successful or -1 on error
111
 */
112
int libfsntfs_index_entry_header_free(
113
     libfsntfs_index_entry_header_t **index_entry_header,
114
     libcerror_error_t **error )
115
962
{
116
962
  static char *function = "libfsntfs_index_entry_header_free";
117
118
962
  if( index_entry_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 index entry header.",
125
0
     function );
126
127
0
    return( -1 );
128
0
  }
129
962
  if( *index_entry_header != NULL )
130
962
  {
131
962
    memory_free(
132
962
     *index_entry_header );
133
134
962
    *index_entry_header = NULL;
135
962
  }
136
962
  return( 1 );
137
962
}
138
139
/* Reads the index entry header
140
 * Returns 1 if successful or -1 on error
141
 */
142
int libfsntfs_index_entry_header_read_data(
143
     libfsntfs_index_entry_header_t *index_entry_header,
144
     const uint8_t *data,
145
     size_t data_size,
146
     libcerror_error_t **error )
147
962
{
148
962
  static char *function = "libfsntfs_index_entry_header_read_data";
149
150
#if defined( HAVE_DEBUG_OUTPUT )
151
  uint64_t value_64bit  = 0;
152
#endif
153
154
962
  if( index_entry_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 index entry header.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
962
  if( data == NULL )
166
0
  {
167
0
    libcerror_error_set(
168
0
     error,
169
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
170
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
171
0
     "%s: invalid data.",
172
0
     function );
173
174
0
    return( -1 );
175
0
  }
176
962
  if( ( data_size < sizeof( fsntfs_index_entry_header_t ) )
177
962
   || ( data_size > (size_t) SSIZE_MAX ) )
178
0
  {
179
0
    libcerror_error_set(
180
0
     error,
181
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
182
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
183
0
     "%s: invalid data size value out of bounds.",
184
0
     function );
185
186
0
    return( -1 );
187
0
  }
188
#if defined( HAVE_DEBUG_OUTPUT )
189
  if( libcnotify_verbose != 0 )
190
  {
191
    libcnotify_printf(
192
     "%s: index entry header data:\n",
193
     function );
194
    libcnotify_print_data(
195
     data,
196
     sizeof( fsntfs_index_entry_header_t ),
197
     0 );
198
  }
199
#endif
200
962
  if( memory_compare(
201
962
       ( (fsntfs_index_entry_header_t *) data )->signature,
202
962
       fsntfs_index_entry_signature,
203
962
       4 ) != 0 )
204
9
  {
205
9
    libcerror_error_set(
206
9
     error,
207
9
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
208
9
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
209
9
     "%s: invalid index entry signature.",
210
9
     function );
211
212
9
    return( -1 );
213
9
  }
214
953
  byte_stream_copy_to_uint16_little_endian(
215
953
   ( (fsntfs_index_entry_header_t *) data )->fixup_values_offset,
216
953
   index_entry_header->fixup_values_offset );
217
218
953
  byte_stream_copy_to_uint16_little_endian(
219
953
   ( (fsntfs_index_entry_header_t *) data )->number_of_fixup_values,
220
953
   index_entry_header->number_of_fixup_values );
221
222
#if defined( HAVE_DEBUG_OUTPUT )
223
  if( libcnotify_verbose != 0 )
224
  {
225
    libcnotify_printf(
226
     "%s: signature\t\t\t: %c%c%c%c\n",
227
     function,
228
     ( (fsntfs_index_entry_header_t *) data )->signature[ 0 ],
229
     ( (fsntfs_index_entry_header_t *) data )->signature[ 1 ],
230
     ( (fsntfs_index_entry_header_t *) data )->signature[ 2 ],
231
     ( (fsntfs_index_entry_header_t *) data )->signature[ 3 ] );
232
233
    libcnotify_printf(
234
     "%s: fixup values offset\t\t: %" PRIu16 "\n",
235
     function,
236
     index_entry_header->fixup_values_offset );
237
238
    libcnotify_printf(
239
     "%s: number of fixup values\t\t: %" PRIu16 "\n",
240
     function,
241
     index_entry_header->number_of_fixup_values );
242
243
    byte_stream_copy_to_uint64_little_endian(
244
     ( (fsntfs_index_entry_header_t *) data )->journal_sequence_number,
245
     value_64bit );
246
    libcnotify_printf(
247
     "%s: journal sequence number\t\t: %" PRIu64 "\n",
248
     function,
249
     value_64bit );
250
251
    byte_stream_copy_to_uint64_little_endian(
252
     ( (fsntfs_index_entry_header_t *) data )->vcn,
253
     value_64bit );
254
    libcnotify_printf(
255
     "%s: VCN\t\t\t\t: %" PRIu64 "\n",
256
     function,
257
     value_64bit );
258
259
    libcnotify_printf(
260
     "\n" );
261
  }
262
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
263
264
953
  if( index_entry_header->fixup_values_offset < ( sizeof( fsntfs_index_entry_header_t ) + sizeof( fsntfs_index_node_header_t ) ) )
265
1
  {
266
1
    libcerror_error_set(
267
1
     error,
268
1
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
269
1
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
270
1
     "%s: invalid fix-up values offset value out of bounds.",
271
1
     function );
272
273
1
    return( -1 );
274
1
  }
275
952
  return( 1 );
276
953
}
277
278
/* Retrieves the fix-up values offset
279
 * Returns 1 if successful or -1 on error
280
 */
281
int libfsntfs_index_entry_header_get_fixup_values_offset(
282
     libfsntfs_index_entry_header_t *index_entry_header,
283
     uint16_t *fixup_values_offset,
284
     libcerror_error_t **error )
285
951
{
286
951
  static char *function = "libfsntfs_index_entry_header_get_fixup_values_offset";
287
288
951
  if( index_entry_header == NULL )
289
0
  {
290
0
    libcerror_error_set(
291
0
     error,
292
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
293
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
294
0
     "%s: invalid index entry header.",
295
0
     function );
296
297
0
    return( -1 );
298
0
  }
299
951
  if( fixup_values_offset == NULL )
300
0
  {
301
0
    libcerror_error_set(
302
0
     error,
303
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
304
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
305
0
     "%s: invalid fix-up values offset.",
306
0
     function );
307
308
0
    return( -1 );
309
0
  }
310
951
  *fixup_values_offset = index_entry_header->fixup_values_offset;
311
312
951
  return( 1 );
313
951
}
314
315
/* Retrieves the number of fix-up values
316
 * Returns 1 if successful or -1 on error
317
 */
318
int libfsntfs_index_entry_header_get_number_of_fixup_values(
319
     libfsntfs_index_entry_header_t *index_entry_header,
320
     uint16_t *number_of_fixup_values,
321
     libcerror_error_t **error )
322
949
{
323
949
  static char *function = "libfsntfs_index_entry_header_get_number_of_fixup_values";
324
325
949
  if( index_entry_header == NULL )
326
0
  {
327
0
    libcerror_error_set(
328
0
     error,
329
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
330
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
331
0
     "%s: invalid index entry header.",
332
0
     function );
333
334
0
    return( -1 );
335
0
  }
336
949
  if( number_of_fixup_values == NULL )
337
0
  {
338
0
    libcerror_error_set(
339
0
     error,
340
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
341
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
342
0
     "%s: invalid number of fix-up values.",
343
0
     function );
344
345
0
    return( -1 );
346
0
  }
347
949
  *number_of_fixup_values = index_entry_header->number_of_fixup_values;
348
349
949
  return( 1 );
350
949
}
351