Coverage Report

Created: 2024-02-25 07:20

/src/libfsapfs/libfsapfs/libfsapfs_checkpoint_map.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The checkpoint map functions
3
 *
4
 * Copyright (C) 2018-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 "libfsapfs_checkpoint_map.h"
28
#include "libfsapfs_checkpoint_map_entry.h"
29
#include "libfsapfs_checksum.h"
30
#include "libfsapfs_debug.h"
31
#include "libfsapfs_libbfio.h"
32
#include "libfsapfs_libcdata.h"
33
#include "libfsapfs_libcerror.h"
34
#include "libfsapfs_libcnotify.h"
35
36
#include "fsapfs_checkpoint_map.h"
37
38
/* Creates a checkpoint map
39
 * Make sure the value checkpoint_map is referencing, is set to NULL
40
 * Returns 1 if successful or -1 on error
41
 */
42
int libfsapfs_checkpoint_map_initialize(
43
     libfsapfs_checkpoint_map_t **checkpoint_map,
44
     libcerror_error_t **error )
45
10.1k
{
46
10.1k
  static char *function = "libfsapfs_checkpoint_map_initialize";
47
48
10.1k
  if( checkpoint_map == NULL )
49
0
  {
50
0
    libcerror_error_set(
51
0
     error,
52
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
53
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
54
0
     "%s: invalid checkpoint map.",
55
0
     function );
56
57
0
    return( -1 );
58
0
  }
59
10.1k
  if( *checkpoint_map != NULL )
60
0
  {
61
0
    libcerror_error_set(
62
0
     error,
63
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
64
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
65
0
     "%s: invalid checkpoint map value already set.",
66
0
     function );
67
68
0
    return( -1 );
69
0
  }
70
10.1k
  *checkpoint_map = memory_allocate_structure(
71
10.1k
                     libfsapfs_checkpoint_map_t );
72
73
10.1k
  if( *checkpoint_map == NULL )
74
0
  {
75
0
    libcerror_error_set(
76
0
     error,
77
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
78
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
79
0
     "%s: unable to create checkpoint map.",
80
0
     function );
81
82
0
    goto on_error;
83
0
  }
84
10.1k
  if( memory_set(
85
10.1k
       *checkpoint_map,
86
10.1k
       0,
87
10.1k
       sizeof( libfsapfs_checkpoint_map_t ) ) == NULL )
88
0
  {
89
0
    libcerror_error_set(
90
0
     error,
91
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
92
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
93
0
     "%s: unable to clear checkpoint map.",
94
0
     function );
95
96
0
    memory_free(
97
0
     *checkpoint_map );
98
99
0
    *checkpoint_map = NULL;
100
101
0
    return( -1 );
102
0
  }
103
10.1k
  if( libcdata_array_initialize(
104
10.1k
       &( ( *checkpoint_map )->entries_array ),
105
10.1k
       0,
106
10.1k
       error ) != 1 )
107
0
  {
108
0
    libcerror_error_set(
109
0
     error,
110
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
111
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
112
0
     "%s: unable to create entries array.",
113
0
     function );
114
115
0
    goto on_error;
116
0
  }
117
10.1k
  return( 1 );
118
119
0
on_error:
120
0
  if( *checkpoint_map != NULL )
121
0
  {
122
0
    memory_free(
123
0
     *checkpoint_map );
124
125
0
    *checkpoint_map = NULL;
126
0
  }
127
0
  return( -1 );
128
10.1k
}
129
130
/* Frees a checkpoint map
131
 * Returns 1 if successful or -1 on error
132
 */
133
int libfsapfs_checkpoint_map_free(
134
     libfsapfs_checkpoint_map_t **checkpoint_map,
135
     libcerror_error_t **error )
136
10.1k
{
137
10.1k
  static char *function = "libfsapfs_checkpoint_map_free";
138
10.1k
  int result            = 1;
139
140
10.1k
  if( checkpoint_map == NULL )
141
0
  {
142
0
    libcerror_error_set(
143
0
     error,
144
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
145
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
146
0
     "%s: invalid checkpoint map.",
147
0
     function );
148
149
0
    return( -1 );
150
0
  }
151
10.1k
  if( *checkpoint_map != NULL )
152
10.1k
  {
153
10.1k
    if( libcdata_array_free(
154
10.1k
         &( ( *checkpoint_map )->entries_array ),
155
10.1k
         (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_checkpoint_map_entry_free,
156
10.1k
         error ) != 1 )
157
0
    {
158
0
      libcerror_error_set(
159
0
       error,
160
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
161
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
162
0
       "%s: unable to free entries array.",
163
0
       function );
164
165
0
      result = -1;
166
0
    }
167
10.1k
    memory_free(
168
10.1k
     *checkpoint_map );
169
170
10.1k
    *checkpoint_map = NULL;
171
10.1k
  }
172
10.1k
  return( result );
173
10.1k
}
174
175
/* Reads the checkpoint map
176
 * Returns 1 if successful or -1 on error
177
 */
178
int libfsapfs_checkpoint_map_read_file_io_handle(
179
     libfsapfs_checkpoint_map_t *checkpoint_map,
180
     libbfio_handle_t *file_io_handle,
181
     off64_t file_offset,
182
     libcerror_error_t **error )
183
10.1k
{
184
10.1k
  uint8_t checkpoint_map_data[ 4096 ];
185
186
10.1k
  static char *function = "libfsapfs_checkpoint_map_read_file_io_handle";
187
10.1k
  ssize_t read_count    = 0;
188
189
10.1k
  if( checkpoint_map == NULL )
190
0
  {
191
0
    libcerror_error_set(
192
0
     error,
193
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
194
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
195
0
     "%s: invalid checkpoint map.",
196
0
     function );
197
198
0
    return( -1 );
199
0
  }
200
#if defined( HAVE_DEBUG_OUTPUT )
201
  if( libcnotify_verbose != 0 )
202
  {
203
    libcnotify_printf(
204
     "%s: reading checkpoint map at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
205
     function,
206
     file_offset,
207
     file_offset );
208
  }
209
#endif
210
10.1k
  read_count = libbfio_handle_read_buffer_at_offset(
211
10.1k
                file_io_handle,
212
10.1k
                (uint8_t *) &checkpoint_map_data,
213
10.1k
                4096,
214
10.1k
                file_offset,
215
10.1k
                error );
216
217
10.1k
  if( read_count != (ssize_t) 4096 )
218
317
  {
219
317
    libcerror_error_set(
220
317
     error,
221
317
     LIBCERROR_ERROR_DOMAIN_IO,
222
317
     LIBCERROR_IO_ERROR_READ_FAILED,
223
317
     "%s: unable to read checkpoint map data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
224
317
     function,
225
317
     file_offset,
226
317
     file_offset );
227
228
317
    return( -1 );
229
317
  }
230
9.85k
  if( libfsapfs_checkpoint_map_read_data(
231
9.85k
       checkpoint_map,
232
9.85k
       (uint8_t *) &checkpoint_map_data,
233
9.85k
       4096,
234
9.85k
       error ) != 1 )
235
818
  {
236
818
    libcerror_error_set(
237
818
     error,
238
818
     LIBCERROR_ERROR_DOMAIN_IO,
239
818
     LIBCERROR_IO_ERROR_READ_FAILED,
240
818
     "%s: unable to read checkpoint map data.",
241
818
     function );
242
243
818
    return( -1 );
244
818
  }
245
9.03k
  return( 1 );
246
9.85k
}
247
248
/* Reads the checkpoint map
249
 * Returns 1 if successful or -1 on error
250
 */
251
int libfsapfs_checkpoint_map_read_data(
252
     libfsapfs_checkpoint_map_t *checkpoint_map,
253
     const uint8_t *data,
254
     size_t data_size,
255
     libcerror_error_t **error )
256
9.85k
{
257
9.85k
  libfsapfs_checkpoint_map_entry_t *map_entry = NULL;
258
9.85k
  static char *function                       = "libfsapfs_checkpoint_map_read_data";
259
9.85k
  size_t data_offset                          = 0;
260
9.85k
  uint64_t calculated_checksum                = 0;
261
9.85k
  uint64_t stored_checksum                    = 0;
262
9.85k
  uint32_t map_entry_index                    = 0;
263
9.85k
  uint32_t number_of_map_entries              = 0;
264
9.85k
  uint32_t object_subtype                     = 0;
265
9.85k
  uint32_t object_type                        = 0;
266
9.85k
  int entry_index                             = 0;
267
268
#if defined( HAVE_DEBUG_OUTPUT )
269
  uint64_t value_64bit                        = 0;
270
  uint32_t value_32bit                        = 0;
271
#endif
272
273
9.85k
  if( checkpoint_map == NULL )
274
0
  {
275
0
    libcerror_error_set(
276
0
     error,
277
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
278
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
279
0
     "%s: invalid checkpoint map.",
280
0
     function );
281
282
0
    return( -1 );
283
0
  }
284
9.85k
  if( data == NULL )
285
0
  {
286
0
    libcerror_error_set(
287
0
     error,
288
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
289
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
290
0
     "%s: invalid data.",
291
0
     function );
292
293
0
    return( -1 );
294
0
  }
295
9.85k
  if( ( data_size < sizeof( fsapfs_checkpoint_map_t ) )
296
9.85k
   || ( data_size > (size_t) SSIZE_MAX ) )
297
0
  {
298
0
    libcerror_error_set(
299
0
     error,
300
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
301
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
302
0
     "%s: invalid data size value out of bounds.",
303
0
     function );
304
305
0
    return( -1 );
306
0
  }
307
#if defined( HAVE_DEBUG_OUTPUT )
308
  if( libcnotify_verbose != 0 )
309
  {
310
    libcnotify_printf(
311
     "%s: checkpoint map data:\n",
312
     function );
313
    libcnotify_print_data(
314
     data,
315
     sizeof( fsapfs_checkpoint_map_t ),
316
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
317
  }
318
#endif
319
9.85k
  byte_stream_copy_to_uint64_little_endian(
320
9.85k
   ( (fsapfs_checkpoint_map_t *) data )->object_checksum,
321
9.85k
   stored_checksum );
322
323
9.85k
  byte_stream_copy_to_uint32_little_endian(
324
9.85k
   ( (fsapfs_checkpoint_map_t *) data )->object_type,
325
9.85k
   object_type );
326
327
9.85k
  if( object_type != 0x4000000cUL )
328
192
  {
329
192
    libcerror_error_set(
330
192
     error,
331
192
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
332
192
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
333
192
     "%s: invalid object type: 0x%08" PRIx32 ".",
334
192
     function,
335
192
     object_type );
336
337
192
    goto on_error;
338
192
  }
339
9.66k
  byte_stream_copy_to_uint32_little_endian(
340
9.66k
   ( (fsapfs_checkpoint_map_t *) data )->object_subtype,
341
9.66k
   object_subtype );
342
343
9.66k
  if( object_subtype != 0x00000000UL )
344
116
  {
345
116
    libcerror_error_set(
346
116
     error,
347
116
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
348
116
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
349
116
     "%s: invalid object subtype: 0x%08" PRIx32 ".",
350
116
     function,
351
116
     object_subtype );
352
353
116
    goto on_error;
354
116
  }
355
9.54k
  byte_stream_copy_to_uint32_little_endian(
356
9.54k
   ( (fsapfs_checkpoint_map_t *) data )->number_of_entries,
357
9.54k
   number_of_map_entries );
358
359
#if defined( HAVE_DEBUG_OUTPUT )
360
  if( libcnotify_verbose != 0 )
361
  {
362
    libcnotify_printf(
363
     "%s: object checksum\t\t\t: 0x%08" PRIx64 "\n",
364
     function,
365
     stored_checksum );
366
367
    byte_stream_copy_to_uint64_little_endian(
368
     ( (fsapfs_checkpoint_map_t *) data )->object_identifier,
369
     value_64bit );
370
    libcnotify_printf(
371
     "%s: object identifier\t\t\t: %" PRIu64 "\n",
372
     function,
373
     value_64bit );
374
375
    byte_stream_copy_to_uint64_little_endian(
376
     ( (fsapfs_checkpoint_map_t *) data )->object_transaction_identifier,
377
     value_64bit );
378
    libcnotify_printf(
379
     "%s: object transaction identifier\t: %" PRIu64 "\n",
380
     function,
381
     value_64bit );
382
383
    libcnotify_printf(
384
     "%s: object type\t\t\t\t: 0x%08" PRIx32 "\n",
385
     function,
386
     object_type );
387
388
    libcnotify_printf(
389
     "%s: object subtype\t\t\t: 0x%08" PRIx32 "\n",
390
     function,
391
     object_subtype );
392
393
    byte_stream_copy_to_uint32_little_endian(
394
     ( (fsapfs_checkpoint_map_t *) data )->flags,
395
     value_32bit );
396
    libcnotify_printf(
397
     "%s: flags\t\t\t\t: 0x%08" PRIx32 "\n",
398
     function,
399
     value_32bit );
400
    libfsapfs_debug_print_checkpoint_flags(
401
     value_32bit );
402
    libcnotify_printf(
403
     "\n" );
404
405
    libcnotify_printf(
406
     "%s: number of entries\t\t\t: %" PRIu32 "\n",
407
     function,
408
     number_of_map_entries );
409
410
    libcnotify_printf(
411
     "\n" );
412
  }
413
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
414
415
9.54k
  if( libfsapfs_checksum_calculate_fletcher64(
416
9.54k
       &calculated_checksum,
417
9.54k
       &( data[ 8 ] ),
418
9.54k
       data_size - 8,
419
9.54k
       0,
420
9.54k
       error ) != 1 )
421
0
  {
422
0
    libcerror_error_set(
423
0
     error,
424
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
425
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
426
0
     "%s: unable to calculate Fletcher-64 checksum.",
427
0
     function );
428
429
0
    goto on_error;
430
0
  }
431
9.54k
  if( stored_checksum != calculated_checksum )
432
385
  {
433
385
    libcerror_error_set(
434
385
     error,
435
385
     LIBCERROR_ERROR_DOMAIN_INPUT,
436
385
     LIBCERROR_INPUT_ERROR_CHECKSUM_MISMATCH,
437
385
     "%s: mismatch in checksum ( 0x%08" PRIx64 " != 0x%08" PRIx64 " ).\n",
438
385
     function,
439
385
     stored_checksum,
440
385
     calculated_checksum );
441
442
385
    goto on_error;
443
385
  }
444
9.16k
  data_offset = 40;
445
446
9.16k
  if( number_of_map_entries > 101 )
447
125
  {
448
125
    libcerror_error_set(
449
125
     error,
450
125
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
451
125
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
452
125
     "%s: invalid number of map entries value out of bounds.",
453
125
     function );
454
455
125
    goto on_error;
456
125
  }
457
9.03k
  for( map_entry_index = 0;
458
46.0k
       map_entry_index < number_of_map_entries;
459
36.9k
       map_entry_index++ )
460
36.9k
  {
461
36.9k
    if( libfsapfs_checkpoint_map_entry_initialize(
462
36.9k
         &map_entry,
463
36.9k
         error ) != 1 )
464
0
    {
465
0
      libcerror_error_set(
466
0
       error,
467
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
468
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
469
0
       "%s: unable to create checkpoint map entry.",
470
0
       function );
471
472
0
      goto on_error;
473
0
    }
474
#if defined( HAVE_DEBUG_OUTPUT )
475
    if( libcnotify_verbose != 0 )
476
    {
477
      libcnotify_printf(
478
       "%s: entry: %" PRIu32 "\n",
479
       function,
480
       map_entry_index );
481
    }
482
#endif
483
36.9k
    if( libfsapfs_checkpoint_map_entry_read_data(
484
36.9k
         map_entry,
485
36.9k
         &( data[ data_offset ] ),
486
36.9k
         data_size - data_offset,
487
36.9k
         error ) != 1 )
488
0
    {
489
0
      libcerror_error_set(
490
0
       error,
491
0
       LIBCERROR_ERROR_DOMAIN_IO,
492
0
       LIBCERROR_IO_ERROR_READ_FAILED,
493
0
       "%s: unable to read checkpoint map entry: %" PRIu32 ".",
494
0
       function,
495
0
       map_entry_index );
496
497
0
      goto on_error;
498
0
    }
499
36.9k
    data_offset += sizeof( fsapfs_checkpoint_map_entry_t );
500
501
36.9k
    if( libcdata_array_append_entry(
502
36.9k
         checkpoint_map->entries_array,
503
36.9k
         &entry_index,
504
36.9k
         (intptr_t *) map_entry,
505
36.9k
         error ) != 1 )
506
0
    {
507
0
      libcerror_error_set(
508
0
       error,
509
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
510
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
511
0
       "%s: unable to append map entry: %" PRIu32 " to array.",
512
0
       function,
513
0
       map_entry_index );
514
515
0
      goto on_error;
516
0
    }
517
36.9k
    map_entry = NULL;
518
36.9k
  }
519
9.03k
  return( 1 );
520
521
818
on_error:
522
818
  if( map_entry != NULL )
523
0
  {
524
0
    libfsapfs_checkpoint_map_entry_free(
525
0
     &map_entry,
526
0
     NULL );
527
0
  }
528
818
  return( -1 );
529
9.03k
}
530
531
/* Retrieves the physical address of a specific object identifier
532
 * Returns 1 if successful, 0 if no such value or -1 on error
533
 */
534
int libfsapfs_checkpoint_map_get_physical_address_by_object_identifier(
535
     libfsapfs_checkpoint_map_t *checkpoint_map,
536
     uint64_t object_identifier,
537
     uint64_t *physical_address,
538
     libcerror_error_t **error )
539
0
{
540
0
  libfsapfs_checkpoint_map_entry_t *map_entry = NULL;
541
0
  static char *function                       = "libfsapfs_checkpoint_map_get_physical_address_by_object_identifier";
542
0
  int entry_index                             = 0;
543
0
  int number_of_entries                       = 0;
544
545
0
  if( checkpoint_map == NULL )
546
0
  {
547
0
    libcerror_error_set(
548
0
     error,
549
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
550
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
551
0
     "%s: invalid checkpoint map.",
552
0
     function );
553
554
0
    return( -1 );
555
0
  }
556
0
  if( physical_address == NULL )
557
0
  {
558
0
    libcerror_error_set(
559
0
     error,
560
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
561
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
562
0
     "%s: invalid physical address.",
563
0
     function );
564
565
0
    return( -1 );
566
0
  }
567
0
  if( libcdata_array_get_number_of_entries(
568
0
       checkpoint_map->entries_array,
569
0
       &number_of_entries,
570
0
       error ) != 1 )
571
0
  {
572
0
    libcerror_error_set(
573
0
     error,
574
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
575
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
576
0
     "%s: unable to retrieve number of entries from array.",
577
0
     function );
578
579
0
    return( -1 );
580
0
  }
581
0
  for( entry_index = 0;
582
0
       entry_index < number_of_entries;
583
0
       entry_index++ )
584
0
  {
585
0
    if( libcdata_array_get_entry_by_index(
586
0
         checkpoint_map->entries_array,
587
0
         entry_index,
588
0
         (intptr_t **) &map_entry,
589
0
         error ) != 1 )
590
0
    {
591
0
      libcerror_error_set(
592
0
       error,
593
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
594
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
595
0
       "%s: unable to retrieve entry: %d from array.",
596
0
       function,
597
0
       entry_index );
598
599
0
      return( -1 );
600
0
    }
601
0
    if( map_entry == NULL )
602
0
    {
603
0
      libcerror_error_set(
604
0
       error,
605
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
606
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
607
0
       "%s: missing entry: %d.",
608
0
       function,
609
0
       entry_index );
610
611
0
      return( -1 );
612
0
    }
613
0
    if( object_identifier == map_entry->object_identifier )
614
0
    {
615
0
      *physical_address = map_entry->physical_address;
616
617
0
      return( 1 );
618
0
    }
619
0
    map_entry = NULL;
620
0
  }
621
0
  return( 0 );
622
0
}
623