Coverage Report

Created: 2025-12-05 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsapfs/libfsapfs/libfsapfs_object_map.c
Line
Count
Source
1
/*
2
 * The object map functions
3
 *
4
 * Copyright (C) 2018-2025, 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_libbfio.h"
28
#include "libfsapfs_libcerror.h"
29
#include "libfsapfs_libcnotify.h"
30
#include "libfsapfs_object_map.h"
31
32
#include "fsapfs_object_map.h"
33
34
/* Creates a object map
35
 * Make sure the value object_map is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libfsapfs_object_map_initialize(
39
     libfsapfs_object_map_t **object_map,
40
     libcerror_error_t **error )
41
8.24k
{
42
8.24k
  static char *function = "libfsapfs_object_map_initialize";
43
44
8.24k
  if( object_map == 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 object map.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
8.24k
  if( *object_map != 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 object map value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
8.24k
  *object_map = memory_allocate_structure(
67
8.24k
                 libfsapfs_object_map_t );
68
69
8.24k
  if( *object_map == 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 object map.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
8.24k
  if( memory_set(
81
8.24k
       *object_map,
82
8.24k
       0,
83
8.24k
       sizeof( libfsapfs_object_map_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 object map.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
8.24k
  return( 1 );
95
96
0
on_error:
97
0
  if( *object_map != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *object_map );
101
102
0
    *object_map = NULL;
103
0
  }
104
0
  return( -1 );
105
8.24k
}
106
107
/* Frees a object map
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libfsapfs_object_map_free(
111
     libfsapfs_object_map_t **object_map,
112
     libcerror_error_t **error )
113
8.24k
{
114
8.24k
  static char *function = "libfsapfs_object_map_free";
115
116
8.24k
  if( object_map == 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 object map.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
8.24k
  if( *object_map != NULL )
128
8.24k
  {
129
8.24k
    memory_free(
130
8.24k
     *object_map );
131
132
8.24k
    *object_map = NULL;
133
8.24k
  }
134
8.24k
  return( 1 );
135
8.24k
}
136
137
/* Reads the object map
138
 * Returns 1 if successful or -1 on error
139
 */
140
int libfsapfs_object_map_read_file_io_handle(
141
     libfsapfs_object_map_t *object_map,
142
     libbfio_handle_t *file_io_handle,
143
     off64_t file_offset,
144
     libcerror_error_t **error )
145
8.24k
{
146
8.24k
  fsapfs_object_map_t object_map_data;
147
148
8.24k
  static char *function = "libfsapfs_object_map_read_file_io_handle";
149
8.24k
  ssize_t read_count    = 0;
150
151
8.24k
  if( object_map == NULL )
152
0
  {
153
0
    libcerror_error_set(
154
0
     error,
155
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
156
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
157
0
     "%s: invalid object map.",
158
0
     function );
159
160
0
    return( -1 );
161
0
  }
162
#if defined( HAVE_DEBUG_OUTPUT )
163
  if( libcnotify_verbose != 0 )
164
  {
165
    libcnotify_printf(
166
     "%s: reading object map at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
167
     function,
168
     file_offset,
169
     file_offset );
170
  }
171
#endif
172
8.24k
  read_count = libbfio_handle_read_buffer_at_offset(
173
8.24k
                file_io_handle,
174
8.24k
                (uint8_t *) &object_map_data,
175
8.24k
                sizeof( fsapfs_object_map_t ),
176
8.24k
                file_offset,
177
8.24k
                error );
178
179
8.24k
  if( read_count != (ssize_t) sizeof( fsapfs_object_map_t ) )
180
221
  {
181
221
    libcerror_error_set(
182
221
     error,
183
221
     LIBCERROR_ERROR_DOMAIN_IO,
184
221
     LIBCERROR_IO_ERROR_READ_FAILED,
185
221
     "%s: unable to read object map data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
186
221
     function,
187
221
     file_offset,
188
221
     file_offset );
189
190
221
    return( -1 );
191
221
  }
192
8.01k
  if( libfsapfs_object_map_read_data(
193
8.01k
       object_map,
194
8.01k
       (uint8_t *) &object_map_data,
195
8.01k
       sizeof( fsapfs_object_map_t ),
196
8.01k
       error ) != 1 )
197
278
  {
198
278
    libcerror_error_set(
199
278
     error,
200
278
     LIBCERROR_ERROR_DOMAIN_IO,
201
278
     LIBCERROR_IO_ERROR_READ_FAILED,
202
278
     "%s: unable to read object map data.",
203
278
     function );
204
205
278
    return( -1 );
206
278
  }
207
7.74k
  return( 1 );
208
8.01k
}
209
210
/* Reads the object map
211
 * Returns 1 if successful or -1 on error
212
 */
213
int libfsapfs_object_map_read_data(
214
     libfsapfs_object_map_t *object_map,
215
     const uint8_t *data,
216
     size_t data_size,
217
     libcerror_error_t **error )
218
8.01k
{
219
8.01k
  static char *function   = "libfsapfs_object_map_read_data";
220
8.01k
  uint32_t object_subtype = 0;
221
8.01k
  uint32_t object_type    = 0;
222
223
#if defined( HAVE_DEBUG_OUTPUT )
224
  uint64_t value_64bit    = 0;
225
  uint32_t value_32bit    = 0;
226
#endif
227
228
8.01k
  if( object_map == NULL )
229
0
  {
230
0
    libcerror_error_set(
231
0
     error,
232
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
233
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
234
0
     "%s: invalid object map.",
235
0
     function );
236
237
0
    return( -1 );
238
0
  }
239
8.01k
  if( data == NULL )
240
0
  {
241
0
    libcerror_error_set(
242
0
     error,
243
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
244
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
245
0
     "%s: invalid data.",
246
0
     function );
247
248
0
    return( -1 );
249
0
  }
250
8.01k
  if( ( data_size < sizeof( fsapfs_object_map_t ) )
251
8.01k
   || ( data_size > (size_t) SSIZE_MAX ) )
252
0
  {
253
0
    libcerror_error_set(
254
0
     error,
255
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
256
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
257
0
     "%s: invalid data size value out of bounds.",
258
0
     function );
259
260
0
    return( -1 );
261
0
  }
262
#if defined( HAVE_DEBUG_OUTPUT )
263
  if( libcnotify_verbose != 0 )
264
  {
265
    libcnotify_printf(
266
     "%s: object map data:\n",
267
     function );
268
    libcnotify_print_data(
269
     data,
270
     sizeof( fsapfs_object_map_t ),
271
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
272
  }
273
#endif
274
8.01k
  byte_stream_copy_to_uint32_little_endian(
275
8.01k
   ( (fsapfs_object_map_t *) data )->object_type,
276
8.01k
   object_type );
277
278
8.01k
  if( object_type != 0x4000000bUL )
279
156
  {
280
156
    libcerror_error_set(
281
156
     error,
282
156
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
283
156
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
284
156
     "%s: invalid object type: 0x%08" PRIx32 ".",
285
156
     function,
286
156
     object_type );
287
288
156
    return( -1 );
289
156
  }
290
7.86k
  byte_stream_copy_to_uint32_little_endian(
291
7.86k
   ( (fsapfs_object_map_t *) data )->object_subtype,
292
7.86k
   object_subtype );
293
294
7.86k
  if( object_subtype != 0x00000000UL )
295
122
  {
296
122
    libcerror_error_set(
297
122
     error,
298
122
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
299
122
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
300
122
     "%s: invalid object subtype: 0x%08" PRIx32 ".",
301
122
     function,
302
122
     object_subtype );
303
304
122
    return( -1 );
305
122
  }
306
7.74k
  byte_stream_copy_to_uint32_little_endian(
307
7.74k
   ( (fsapfs_object_map_t *) data )->number_of_snapshots,
308
7.74k
   object_map->number_of_snapshots );
309
310
7.74k
  byte_stream_copy_to_uint64_little_endian(
311
7.74k
   ( (fsapfs_object_map_t *) data )->btree_block_number,
312
7.74k
   object_map->btree_block_number );
313
314
7.74k
  byte_stream_copy_to_uint64_little_endian(
315
7.74k
   ( (fsapfs_object_map_t *) data )->snapshots_btree_block_number,
316
7.74k
   object_map->snapshots_btree_block_number );
317
318
7.74k
  byte_stream_copy_to_uint64_little_endian(
319
7.74k
   ( (fsapfs_object_map_t *) data )->most_recent_snapshot_identifier,
320
7.74k
   object_map->most_recent_snapshot_identifier );
321
322
#if defined( HAVE_DEBUG_OUTPUT )
323
  if( libcnotify_verbose != 0 )
324
  {
325
    byte_stream_copy_to_uint64_little_endian(
326
     ( (fsapfs_object_map_t *) data )->object_checksum,
327
     value_64bit );
328
    libcnotify_printf(
329
     "%s: object checksum\t\t\t\t: 0x%08" PRIx64 "\n",
330
     function,
331
     value_64bit );
332
333
    byte_stream_copy_to_uint64_little_endian(
334
     ( (fsapfs_object_map_t *) data )->object_identifier,
335
     value_64bit );
336
    libcnotify_printf(
337
     "%s: object identifier\t\t\t: %" PRIu64 "\n",
338
     function,
339
     value_64bit );
340
341
    byte_stream_copy_to_uint64_little_endian(
342
     ( (fsapfs_object_map_t *) data )->object_transaction_identifier,
343
     value_64bit );
344
    libcnotify_printf(
345
     "%s: object transaction identifier\t\t: %" PRIu64 "\n",
346
     function,
347
     value_64bit );
348
349
    libcnotify_printf(
350
     "%s: object type\t\t\t\t: 0x%08" PRIx32 "\n",
351
     function,
352
     object_type );
353
354
    libcnotify_printf(
355
     "%s: object subtype\t\t\t\t: 0x%08" PRIx32 "\n",
356
     function,
357
     object_subtype );
358
359
    byte_stream_copy_to_uint32_little_endian(
360
     ( (fsapfs_object_map_t *) data )->flags,
361
     value_32bit );
362
    libcnotify_printf(
363
     "%s: flags\t\t\t\t\t: 0x%08" PRIx32 "\n",
364
     function,
365
     value_32bit );
366
367
    libcnotify_printf(
368
     "%s: number of snapshots\t\t\t: %" PRIu32 "\n",
369
     function,
370
     object_map->number_of_snapshots );
371
372
    byte_stream_copy_to_uint32_little_endian(
373
     ( (fsapfs_object_map_t *) data )->btree_type,
374
     value_32bit );
375
    libcnotify_printf(
376
     "%s: B-tree type\t\t\t\t: 0x%08" PRIx32 "\n",
377
     function,
378
     value_32bit );
379
380
    byte_stream_copy_to_uint32_little_endian(
381
     ( (fsapfs_object_map_t *) data )->snaphots_btree_type,
382
     value_32bit );
383
    libcnotify_printf(
384
     "%s: snapshots B-tree type\t\t\t: 0x%08" PRIx32 "\n",
385
     function,
386
     value_32bit );
387
388
    libcnotify_printf(
389
     "%s: B-tree block number\t\t\t: %" PRIu64 "\n",
390
     function,
391
     object_map->btree_block_number );
392
393
    libcnotify_printf(
394
     "%s: snapshots B-tree block number\t\t: %" PRIu64 "\n",
395
     function,
396
     object_map->snapshots_btree_block_number );
397
398
    libcnotify_printf(
399
     "%s: most recent snapshot identifier\t\t: %" PRIu64 "\n",
400
     function,
401
     object_map->most_recent_snapshot_identifier );
402
403
    byte_stream_copy_to_uint64_little_endian(
404
     ( (fsapfs_object_map_t *) data )->unknown2,
405
     value_64bit );
406
    libcnotify_printf(
407
     "%s: unknown2\t\t\t\t: %" PRIu64 "\n",
408
     function,
409
     value_64bit );
410
411
    byte_stream_copy_to_uint64_little_endian(
412
     ( (fsapfs_object_map_t *) data )->unknown3,
413
     value_64bit );
414
    libcnotify_printf(
415
     "%s: unknown3\t\t\t\t: %" PRIu64 "\n",
416
     function,
417
     value_64bit );
418
419
    libcnotify_printf(
420
     "\n" );
421
  }
422
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
423
424
7.74k
  return( 1 );
425
7.86k
}
426