Coverage Report

Created: 2026-03-05 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfshfs/libfshfs/libfshfs_fork_descriptor.c
Line
Count
Source
1
/*
2
 * Fork descriptor functions
3
 *
4
 * Copyright (C) 2009-2026, 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 "libfshfs_extent.h"
28
#include "libfshfs_fork_descriptor.h"
29
#include "libfshfs_libcerror.h"
30
#include "libfshfs_libcnotify.h"
31
32
#include "fshfs_fork_descriptor.h"
33
34
/* Creates a fork descriptor
35
 * Make sure the value fork_descriptor is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libfshfs_fork_descriptor_initialize(
39
     libfshfs_fork_descriptor_t **fork_descriptor,
40
     libcerror_error_t **error )
41
33.0k
{
42
33.0k
  static char *function = "libfshfs_fork_descriptor_initialize";
43
44
33.0k
  if( fork_descriptor == 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 fork descriptor.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
33.0k
  if( *fork_descriptor != 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 fork descriptor value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
33.0k
  *fork_descriptor = memory_allocate_structure(
67
33.0k
                      libfshfs_fork_descriptor_t );
68
69
33.0k
  if( *fork_descriptor == 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 fork descriptor.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
33.0k
  if( memory_set(
81
33.0k
       *fork_descriptor,
82
33.0k
       0,
83
33.0k
       sizeof( libfshfs_fork_descriptor_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 fork descriptor.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
33.0k
  return( 1 );
95
96
0
on_error:
97
0
  if( *fork_descriptor != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *fork_descriptor );
101
102
0
    *fork_descriptor = NULL;
103
0
  }
104
0
  return( -1 );
105
33.0k
}
106
107
/* Frees fork descriptor
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libfshfs_fork_descriptor_free(
111
     libfshfs_fork_descriptor_t **fork_descriptor,
112
     libcerror_error_t **error )
113
36.0k
{
114
36.0k
  static char *function = "libfshfs_fork_descriptor_free";
115
116
36.0k
  if( fork_descriptor == 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 fork descriptor.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
36.0k
  if( *fork_descriptor != NULL )
128
35.8k
  {
129
35.8k
    memory_free(
130
35.8k
     *fork_descriptor );
131
132
35.8k
    *fork_descriptor = NULL;
133
35.8k
  }
134
36.0k
  return( 1 );
135
36.0k
}
136
137
/* Clones a fork descriptor
138
 * Returns 1 if successful or -1 on error
139
 */
140
int libfshfs_fork_descriptor_clone(
141
     libfshfs_fork_descriptor_t **destination_fork_descriptor,
142
     libfshfs_fork_descriptor_t *source_fork_descriptor,
143
     libcerror_error_t **error )
144
2.99k
{
145
2.99k
  static char *function = "libfshfs_fork_descriptor_clone";
146
147
2.99k
  if( destination_fork_descriptor == NULL )
148
0
  {
149
0
    libcerror_error_set(
150
0
     error,
151
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
152
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
153
0
     "%s: invalid destination fork descriptor.",
154
0
     function );
155
156
0
    return( -1 );
157
0
  }
158
2.99k
  if( *destination_fork_descriptor != NULL )
159
0
  {
160
0
    libcerror_error_set(
161
0
     error,
162
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
163
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
164
0
     "%s: invalid destination fork descriptor value already set.",
165
0
     function );
166
167
0
    return( -1 );
168
0
  }
169
2.99k
  if( source_fork_descriptor == NULL )
170
158
  {
171
158
    *destination_fork_descriptor = NULL;
172
173
158
    return( 1 );
174
158
  }
175
2.83k
  *destination_fork_descriptor = memory_allocate_structure(
176
2.83k
                                  libfshfs_fork_descriptor_t );
177
178
2.83k
  if( *destination_fork_descriptor == NULL )
179
0
  {
180
0
    libcerror_error_set(
181
0
     error,
182
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
183
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
184
0
     "%s: unable to create destination fork descriptor.",
185
0
     function );
186
187
0
    goto on_error;
188
0
  }
189
2.83k
  if( memory_copy(
190
2.83k
       *destination_fork_descriptor,
191
2.83k
       source_fork_descriptor,
192
2.83k
       sizeof( libfshfs_fork_descriptor_t ) ) == NULL )
193
0
  {
194
0
    libcerror_error_set(
195
0
     error,
196
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
197
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
198
0
     "%s: unable to copy source to destination fork descriptor.",
199
0
     function );
200
201
0
    goto on_error;
202
0
  }
203
2.83k
  return( 1 );
204
205
0
on_error:
206
0
  if( *destination_fork_descriptor != NULL )
207
0
  {
208
0
    memory_free(
209
0
     *destination_fork_descriptor );
210
211
0
    *destination_fork_descriptor = NULL;
212
0
  }
213
0
  return( -1 );
214
2.83k
}
215
216
/* Reads the fork descriptor
217
 * Returns 1 if successful or -1 on error
218
 */
219
int libfshfs_fork_descriptor_read_data(
220
     libfshfs_fork_descriptor_t *fork_descriptor,
221
     const uint8_t *data,
222
     size_t data_size,
223
     libcerror_error_t **error )
224
28.0k
{
225
28.0k
  static char *function            = "libfshfs_fork_descriptor_read_data";
226
28.0k
  size_t extent_data_offset        = 0;
227
28.0k
  uint32_t extent_block_number     = 0;
228
28.0k
  uint32_t extent_number_of_blocks = 0;
229
28.0k
  int extent_index                 = 0;
230
231
#if defined( HAVE_DEBUG_OUTPUT )
232
  uint32_t value_32bit             = 0;
233
#endif
234
235
28.0k
  if( fork_descriptor == NULL )
236
0
  {
237
0
    libcerror_error_set(
238
0
     error,
239
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
240
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
241
0
     "%s: invalid fork descriptor.",
242
0
     function );
243
244
0
    return( -1 );
245
0
  }
246
28.0k
  if( data == NULL )
247
0
  {
248
0
    libcerror_error_set(
249
0
     error,
250
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
251
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
252
0
     "%s: invalid data.",
253
0
     function );
254
255
0
    return( -1 );
256
0
  }
257
28.0k
  if( data_size > (size_t) SSIZE_MAX )
258
0
  {
259
0
    libcerror_error_set(
260
0
     error,
261
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
262
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
263
0
     "%s: invalid data size value out of bounds.",
264
0
     function );
265
266
0
    return( -1 );
267
0
  }
268
#if defined( HAVE_DEBUG_OUTPUT )
269
  if( libcnotify_verbose != 0 )
270
  {
271
    libcnotify_printf(
272
     "%s: fork descriptor data:\n",
273
     function );
274
    libcnotify_print_data(
275
     data,
276
     data_size,
277
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
278
  }
279
#endif
280
28.0k
  if( data_size != 80 )
281
0
  {
282
0
    libcerror_error_set(
283
0
     error,
284
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
285
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
286
0
     "%s: unsupported fork descriptor data size: %" PRIzd "\n",
287
0
     function,
288
0
     data_size );
289
290
0
    return( -1 );
291
0
  }
292
28.0k
  byte_stream_copy_to_uint64_big_endian(
293
28.0k
   ( (fshfs_fork_descriptor_t *) data )->logical_size,
294
28.0k
   fork_descriptor->size );
295
296
28.0k
  byte_stream_copy_to_uint32_big_endian(
297
28.0k
   ( (fshfs_fork_descriptor_t *) data )->number_of_blocks,
298
28.0k
   fork_descriptor->number_of_blocks );
299
300
#if defined( HAVE_DEBUG_OUTPUT )
301
  if( libcnotify_verbose != 0 )
302
  {
303
    libcnotify_printf(
304
     "%s: logical size\t\t\t: %" PRIu64 "\n",
305
     function,
306
     fork_descriptor->size );
307
308
    byte_stream_copy_to_uint32_big_endian(
309
     ( (fshfs_fork_descriptor_t *) data )->clump_size,
310
     value_32bit );
311
    libcnotify_printf(
312
     "%s: clump size\t\t\t\t: %" PRIu32 "\n",
313
     function,
314
     value_32bit );
315
316
    libcnotify_printf(
317
     "%s: number of blocks\t\t\t: %" PRIu32 "\n",
318
     function,
319
     fork_descriptor->number_of_blocks );
320
  }
321
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
322
323
252k
  while( extent_data_offset < 64 )
324
224k
  {
325
224k
    byte_stream_copy_to_uint32_big_endian(
326
224k
     &( ( ( (fshfs_fork_descriptor_t *) data )->extents )[ extent_data_offset ] ),
327
224k
     extent_block_number );
328
329
224k
    extent_data_offset += 4;
330
331
224k
    byte_stream_copy_to_uint32_big_endian(
332
224k
     &( ( ( (fshfs_fork_descriptor_t *) data )->extents )[ extent_data_offset ] ),
333
224k
     extent_number_of_blocks );
334
335
224k
    extent_data_offset += 4;
336
337
#if defined( HAVE_DEBUG_OUTPUT )
338
    if( libcnotify_verbose != 0 )
339
    {
340
      libcnotify_printf(
341
       "%s: extent: %d block number\t\t: %" PRIu32 "\n",
342
       function,
343
       extent_index,
344
       extent_block_number );
345
346
      libcnotify_printf(
347
       "%s: extent: %d number of blocks\t\t: %" PRIu32 "\n",
348
       function,
349
       extent_index,
350
       extent_number_of_blocks );
351
    }
352
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
353
354
224k
    fork_descriptor->extents[ extent_index ][ 0 ] = extent_block_number;
355
224k
    fork_descriptor->extents[ extent_index ][ 1 ] = extent_number_of_blocks;
356
357
224k
    fork_descriptor->number_of_blocks_in_extents += extent_number_of_blocks;
358
359
224k
    extent_index++;
360
224k
  }
361
#if defined( HAVE_DEBUG_OUTPUT )
362
  if( libcnotify_verbose != 0 )
363
  {
364
    libcnotify_printf(
365
     "\n" );
366
  }
367
#endif
368
28.0k
  return( 1 );
369
28.0k
}
370
371
/* Determines if the fork has extents in the extents (overflow) file.
372
 * Returns 1 if the fork has extents in the extents (overflow) file, 0 if not or -1 on error
373
 */
374
int libfshfs_fork_descriptor_has_extents_overflow(
375
     libfshfs_fork_descriptor_t *fork_descriptor,
376
     libcerror_error_t **error )
377
13.3k
{
378
13.3k
  static char *function = "libfshfs_fork_descriptor_has_extents_overflow";
379
380
13.3k
  if( fork_descriptor == NULL )
381
0
  {
382
0
    libcerror_error_set(
383
0
     error,
384
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
385
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
386
0
     "%s: invalid fork descriptor.",
387
0
     function );
388
389
0
    return( -1 );
390
0
  }
391
13.3k
  if( fork_descriptor->number_of_blocks_in_extents < fork_descriptor->number_of_blocks )
392
2.63k
  {
393
2.63k
    return( 1 );
394
2.63k
  }
395
10.7k
  return( 0 );
396
13.3k
}
397
398
/* Retrieves the extents
399
 * Returns 1 if successful or -1 on error
400
 */
401
int libfshfs_fork_descriptor_get_extents(
402
     libfshfs_fork_descriptor_t *fork_descriptor,
403
     libcdata_array_t *extents,
404
     libcerror_error_t **error )
405
12.9k
{
406
12.9k
  libfshfs_extent_t *extent        = NULL;
407
12.9k
  static char *function            = "libfshfs_fork_descriptor_get_extents";
408
12.9k
  uint32_t extent_block_number     = 0;
409
12.9k
  uint32_t extent_number_of_blocks = 0;
410
12.9k
  int entry_index                  = 0;
411
12.9k
  int extent_index                 = 0;
412
413
12.9k
  if( fork_descriptor == NULL )
414
0
  {
415
0
    libcerror_error_set(
416
0
     error,
417
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
418
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
419
0
     "%s: invalid fork descriptor.",
420
0
     function );
421
422
0
    return( -1 );
423
0
  }
424
12.9k
  for( extent_index = 0;
425
62.8k
       extent_index < 8;
426
49.8k
       extent_index++ )
427
59.6k
  {
428
59.6k
    extent_block_number     = fork_descriptor->extents[ extent_index ][ 0 ];
429
59.6k
    extent_number_of_blocks = fork_descriptor->extents[ extent_index ][ 1 ];
430
431
59.6k
    if( ( extent_block_number == 0 )
432
52.4k
     || ( extent_number_of_blocks == 0 ) )
433
9.70k
    {
434
9.70k
      break;
435
9.70k
    }
436
49.8k
    if( libfshfs_extent_initialize(
437
49.8k
         &extent,
438
49.8k
         error ) != 1 )
439
0
    {
440
0
      libcerror_error_set(
441
0
       error,
442
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
443
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
444
0
       "%s: unable to create extent.",
445
0
       function );
446
447
0
      goto on_error;
448
0
    }
449
49.8k
    extent->block_number     = extent_block_number;
450
49.8k
    extent->number_of_blocks = extent_number_of_blocks;
451
452
49.8k
    if( libcdata_array_append_entry(
453
49.8k
         extents,
454
49.8k
         &entry_index,
455
49.8k
         (intptr_t *) extent,
456
49.8k
         error ) != 1 )
457
0
    {
458
0
      libcerror_error_set(
459
0
       error,
460
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
461
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
462
0
       "%s: unable to append extent to array.",
463
0
       function );
464
465
0
      goto on_error;
466
0
    }
467
49.8k
    extent = NULL;
468
49.8k
  }
469
12.9k
  return( 1 );
470
471
0
on_error:
472
0
  if( extent != NULL )
473
0
  {
474
0
    libfshfs_extent_free(
475
0
     &extent,
476
0
     NULL );
477
0
  }
478
0
  libcdata_array_empty(
479
0
   extents,
480
0
   (int (*)(intptr_t **, libcerror_error_t **)) &libfshfs_extent_free,
481
0
   NULL );
482
483
0
  return( -1 );
484
12.9k
}
485