Coverage Report

Created: 2025-06-24 07:14

/src/libfsntfs/libfsntfs/libfsntfs_index_root_header.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The NTFS index root 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_root_header.h"
29
#include "libfsntfs_libcerror.h"
30
#include "libfsntfs_libcnotify.h"
31
32
#include "fsntfs_index.h"
33
34
/* Creates an index root header
35
 * Make sure the value index_root_header is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libfsntfs_index_root_header_initialize(
39
     libfsntfs_index_root_header_t **index_root_header,
40
     libcerror_error_t **error )
41
1.87k
{
42
1.87k
  static char *function = "libfsntfs_index_root_header_initialize";
43
44
1.87k
  if( index_root_header == NULL )
45
0
  {
46
0
    libcerror_error_set(
47
0
     error,
48
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50
0
     "%s: invalid index root header.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
1.87k
  if( *index_root_header != NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
60
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61
0
     "%s: invalid index root header value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
1.87k
  *index_root_header = memory_allocate_structure(
67
1.87k
                        libfsntfs_index_root_header_t );
68
69
1.87k
  if( *index_root_header == NULL )
70
0
  {
71
0
    libcerror_error_set(
72
0
     error,
73
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
74
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
75
0
     "%s: unable to create index root header.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
1.87k
  if( memory_set(
81
1.87k
       *index_root_header,
82
1.87k
       0,
83
1.87k
       sizeof( libfsntfs_index_root_header_t ) ) == NULL )
84
0
  {
85
0
    libcerror_error_set(
86
0
     error,
87
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
88
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
89
0
     "%s: unable to clear index root header.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
1.87k
  return( 1 );
95
96
0
on_error:
97
0
  if( *index_root_header != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *index_root_header );
101
102
0
    *index_root_header = NULL;
103
0
  }
104
0
  return( -1 );
105
1.87k
}
106
107
/* Frees an index root header
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libfsntfs_index_root_header_free(
111
     libfsntfs_index_root_header_t **index_root_header,
112
     libcerror_error_t **error )
113
1.87k
{
114
1.87k
  static char *function = "libfsntfs_index_root_header_free";
115
116
1.87k
  if( index_root_header == NULL )
117
0
  {
118
0
    libcerror_error_set(
119
0
     error,
120
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
121
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
122
0
     "%s: invalid index root header.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
1.87k
  if( *index_root_header != NULL )
128
1.87k
  {
129
1.87k
    memory_free(
130
1.87k
     *index_root_header );
131
132
1.87k
    *index_root_header = NULL;
133
1.87k
  }
134
1.87k
  return( 1 );
135
1.87k
}
136
137
/* Reads the index root header
138
 * Returns 1 if successful or -1 on error
139
 */
140
int libfsntfs_index_root_header_read_data(
141
     libfsntfs_index_root_header_t *index_root_header,
142
     const uint8_t *data,
143
     size_t data_size,
144
     libcerror_error_t **error )
145
1.87k
{
146
1.87k
  static char *function = "libfsntfs_index_root_header_read_data";
147
148
#if defined( HAVE_DEBUG_OUTPUT )
149
  uint32_t value_32bit  = 0;
150
#endif
151
152
1.87k
  if( index_root_header == NULL )
153
0
  {
154
0
    libcerror_error_set(
155
0
     error,
156
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
157
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
158
0
     "%s: invalid index root header.",
159
0
     function );
160
161
0
    return( -1 );
162
0
  }
163
1.87k
  if( data == NULL )
164
3
  {
165
3
    libcerror_error_set(
166
3
     error,
167
3
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
168
3
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
169
3
     "%s: invalid data.",
170
3
     function );
171
172
3
    return( -1 );
173
3
  }
174
1.86k
  if( ( data_size < sizeof( fsntfs_index_root_header_t ) )
175
1.86k
   || ( data_size > (size_t) SSIZE_MAX ) )
176
5
  {
177
5
    libcerror_error_set(
178
5
     error,
179
5
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
180
5
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
181
5
     "%s: invalid data size value out of bounds.",
182
5
     function );
183
184
5
    return( -1 );
185
5
  }
186
#if defined( HAVE_DEBUG_OUTPUT )
187
  if( libcnotify_verbose != 0 )
188
  {
189
    libcnotify_printf(
190
     "%s: index root header data:\n",
191
     function );
192
    libcnotify_print_data(
193
     data,
194
     sizeof( fsntfs_index_root_header_t ),
195
     0 );
196
  }
197
#endif
198
1.86k
  byte_stream_copy_to_uint32_little_endian(
199
1.86k
   ( (fsntfs_index_root_header_t *) data )->attribute_type,
200
1.86k
   index_root_header->attribute_type );
201
202
1.86k
  byte_stream_copy_to_uint32_little_endian(
203
1.86k
   ( (fsntfs_index_root_header_t *) data )->collation_type,
204
1.86k
   index_root_header->collation_type );
205
206
1.86k
  byte_stream_copy_to_uint32_little_endian(
207
1.86k
   ( (fsntfs_index_root_header_t *) data )->index_entry_size,
208
1.86k
   index_root_header->index_entry_size );
209
210
#if defined( HAVE_DEBUG_OUTPUT )
211
  if( libcnotify_verbose != 0 )
212
  {
213
    libcnotify_printf(
214
     "%s: attribute type\t\t\t\t: 0x%08" PRIx32 " (%s)\n",
215
     function,
216
     index_root_header->attribute_type,
217
     libfsntfs_debug_print_attribute_type(
218
      index_root_header->attribute_type ) );
219
220
    libcnotify_printf(
221
     "%s: collation type\t\t\t\t: 0x%08" PRIx32 " (%s)\n",
222
     function,
223
     index_root_header->collation_type,
224
     libfsntfs_debug_print_collation_type(
225
      index_root_header->collation_type ) );
226
227
    libcnotify_printf(
228
     "%s: index entry size\t\t\t\t: %" PRIu32 "\n",
229
     function,
230
     index_root_header->index_entry_size );
231
232
    byte_stream_copy_to_uint32_little_endian(
233
     ( (fsntfs_index_root_header_t *) data )->index_entry_number_of_cluster_blocks,
234
     value_32bit );
235
    libcnotify_printf(
236
     "%s: index entry number of cluster blocks\t: %" PRIu32 "\n",
237
     function,
238
     value_32bit );
239
240
    libcnotify_printf(
241
     "\n" );
242
  }
243
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
244
245
1.86k
  return( 1 );
246
1.86k
}
247
248
/* Retrieves the index entry size
249
 * Returns 1 if successful or -1 on error
250
 */
251
int libfsntfs_index_root_header_get_index_entry_size(
252
     libfsntfs_index_root_header_t *index_root_header,
253
     uint32_t *index_entry_size,
254
     libcerror_error_t **error )
255
1.53k
{
256
1.53k
  static char *function = "libfsntfs_index_root_header_get_index_entry_size";
257
258
1.53k
  if( index_root_header == NULL )
259
2
  {
260
2
    libcerror_error_set(
261
2
     error,
262
2
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
263
2
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
264
2
     "%s: invalid index root header.",
265
2
     function );
266
267
2
    return( -1 );
268
2
  }
269
1.53k
  if( index_entry_size == NULL )
270
0
  {
271
0
    libcerror_error_set(
272
0
     error,
273
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
274
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
275
0
     "%s: invalid index entry size.",
276
0
     function );
277
278
0
    return( -1 );
279
0
  }
280
1.53k
  *index_entry_size = index_root_header->index_entry_size;
281
282
1.53k
  return( 1 );
283
1.53k
}
284
285
/* Retrieves the attribute type
286
 * Returns 1 if successful or -1 on error
287
 */
288
int libfsntfs_index_root_header_get_attribute_type(
289
     libfsntfs_index_root_header_t *index_root_header,
290
     uint32_t *attribute_type,
291
     libcerror_error_t **error )
292
511
{
293
511
  static char *function = "libfsntfs_index_root_header_get_attribute_type";
294
295
511
  if( index_root_header == NULL )
296
2
  {
297
2
    libcerror_error_set(
298
2
     error,
299
2
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
300
2
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
301
2
     "%s: invalid index root header.",
302
2
     function );
303
304
2
    return( -1 );
305
2
  }
306
509
  if( attribute_type == NULL )
307
0
  {
308
0
    libcerror_error_set(
309
0
     error,
310
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
311
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
312
0
     "%s: invalid attribute type.",
313
0
     function );
314
315
0
    return( -1 );
316
0
  }
317
509
  *attribute_type = index_root_header->attribute_type;
318
319
509
  return( 1 );
320
509
}
321
322
/* Retrieves the collation type
323
 * Returns 1 if successful or -1 on error
324
 */
325
int libfsntfs_index_root_header_get_collation_type(
326
     libfsntfs_index_root_header_t *index_root_header,
327
     uint32_t *collation_type,
328
     libcerror_error_t **error )
329
425
{
330
425
  static char *function = "libfsntfs_index_root_header_get_collation_type";
331
332
425
  if( index_root_header == NULL )
333
0
  {
334
0
    libcerror_error_set(
335
0
     error,
336
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
337
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
338
0
     "%s: invalid index root header.",
339
0
     function );
340
341
0
    return( -1 );
342
0
  }
343
425
  if( collation_type == NULL )
344
0
  {
345
0
    libcerror_error_set(
346
0
     error,
347
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
348
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
349
0
     "%s: invalid collation type.",
350
0
     function );
351
352
0
    return( -1 );
353
0
  }
354
425
  *collation_type = index_root_header->collation_type;
355
356
425
  return( 1 );
357
425
}
358