Coverage Report

Created: 2025-12-05 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsapfs/libfsapfs/libfsapfs_fusion_middle_tree.c
Line
Count
Source
1
/*
2
 * The Fusion middle tree 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_fusion_middle_tree.h"
28
#include "libfsapfs_libbfio.h"
29
#include "libfsapfs_libcerror.h"
30
#include "libfsapfs_libcnotify.h"
31
32
#include "fsapfs_fusion_middle_tree.h"
33
34
/* Creates a Fusion middle tree
35
 * Make sure the value fusion_middle_tree is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libfsapfs_fusion_middle_tree_initialize(
39
     libfsapfs_fusion_middle_tree_t **fusion_middle_tree,
40
     libcerror_error_t **error )
41
0
{
42
0
  static char *function = "libfsapfs_fusion_middle_tree_initialize";
43
44
0
  if( fusion_middle_tree == 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 Fusion middle tree.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
0
  if( *fusion_middle_tree != 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 Fusion middle tree value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
0
  *fusion_middle_tree = memory_allocate_structure(
67
0
                         libfsapfs_fusion_middle_tree_t );
68
69
0
  if( *fusion_middle_tree == 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 Fusion middle tree.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
0
  if( memory_set(
81
0
       *fusion_middle_tree,
82
0
       0,
83
0
       sizeof( libfsapfs_fusion_middle_tree_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 Fusion middle tree.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
0
  return( 1 );
95
96
0
on_error:
97
0
  if( *fusion_middle_tree != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *fusion_middle_tree );
101
102
0
    *fusion_middle_tree = NULL;
103
0
  }
104
0
  return( -1 );
105
0
}
106
107
/* Frees a Fusion middle tree
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libfsapfs_fusion_middle_tree_free(
111
     libfsapfs_fusion_middle_tree_t **fusion_middle_tree,
112
     libcerror_error_t **error )
113
0
{
114
0
  static char *function = "libfsapfs_fusion_middle_tree_free";
115
116
0
  if( fusion_middle_tree == 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 Fusion middle tree.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
0
  if( *fusion_middle_tree != NULL )
128
0
  {
129
0
    memory_free(
130
0
     *fusion_middle_tree );
131
132
0
    *fusion_middle_tree = NULL;
133
0
  }
134
0
  return( 1 );
135
0
}
136
137
/* Reads the Fusion middle tree
138
 * Returns 1 if successful or -1 on error
139
 */
140
int libfsapfs_fusion_middle_tree_read_file_io_handle(
141
     libfsapfs_fusion_middle_tree_t *fusion_middle_tree,
142
     libbfio_handle_t *file_io_handle,
143
     off64_t file_offset,
144
     libcerror_error_t **error )
145
0
{
146
0
  uint8_t fusion_middle_tree_data[ 4096 ];
147
148
0
  static char *function = "libfsapfs_fusion_middle_tree_read_file_io_handle";
149
0
  ssize_t read_count    = 0;
150
151
0
  if( fusion_middle_tree == 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 Fusion middle tree.",
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 Fusion middle tree at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
167
     function,
168
     file_offset,
169
     file_offset );
170
  }
171
#endif
172
0
  read_count = libbfio_handle_read_buffer_at_offset(
173
0
                file_io_handle,
174
0
                fusion_middle_tree_data,
175
0
                4096,
176
0
                file_offset,
177
0
                error );
178
179
0
  if( read_count != (ssize_t) 4096 )
180
0
  {
181
0
    libcerror_error_set(
182
0
     error,
183
0
     LIBCERROR_ERROR_DOMAIN_IO,
184
0
     LIBCERROR_IO_ERROR_READ_FAILED,
185
0
     "%s: unable to read Fusion middle tree data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
186
0
     function,
187
0
     file_offset,
188
0
     file_offset );
189
190
0
    return( -1 );
191
0
  }
192
0
  if( libfsapfs_fusion_middle_tree_read_data(
193
0
       fusion_middle_tree,
194
0
       fusion_middle_tree_data,
195
0
       4096,
196
0
       error ) != 1 )
197
0
  {
198
0
    libcerror_error_set(
199
0
     error,
200
0
     LIBCERROR_ERROR_DOMAIN_IO,
201
0
     LIBCERROR_IO_ERROR_READ_FAILED,
202
0
     "%s: unable to read Fusion middle tree data.",
203
0
     function );
204
205
0
    return( -1 );
206
0
  }
207
0
  return( 1 );
208
0
}
209
210
/* Reads the Fusion middle tree
211
 * Returns 1 if successful or -1 on error
212
 */
213
int libfsapfs_fusion_middle_tree_read_data(
214
     libfsapfs_fusion_middle_tree_t *fusion_middle_tree,
215
     const uint8_t *data,
216
     size_t data_size,
217
     libcerror_error_t **error )
218
0
{
219
0
  static char *function   = "libfsapfs_fusion_middle_tree_read_data";
220
0
  uint32_t object_subtype = 0;
221
0
  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
0
  if( fusion_middle_tree == 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 Fusion middle tree.",
235
0
     function );
236
237
0
    return( -1 );
238
0
  }
239
0
  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
0
  if( ( data_size < sizeof( fsapfs_fusion_middle_tree_t ) )
251
0
   || ( 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: Fusion middle tree data:\n",
267
     function );
268
    libcnotify_print_data(
269
     data,
270
     data_size,
271
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
272
  }
273
#endif
274
0
  byte_stream_copy_to_uint32_little_endian(
275
0
   ( (fsapfs_fusion_middle_tree_t *) data )->object_type,
276
0
   object_type );
277
278
0
  if( object_type != 0x40000002UL )
279
0
  {
280
0
    libcerror_error_set(
281
0
     error,
282
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
283
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
284
0
     "%s: invalid object type: 0x%08" PRIx32 ".",
285
0
     function,
286
0
     object_type );
287
288
0
    return( -1 );
289
0
  }
290
0
  byte_stream_copy_to_uint32_little_endian(
291
0
   ( (fsapfs_fusion_middle_tree_t *) data )->object_subtype,
292
0
   object_subtype );
293
294
0
  if( object_subtype != 0x00000015UL )
295
0
  {
296
0
    libcerror_error_set(
297
0
     error,
298
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
299
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
300
0
     "%s: invalid object subtype: 0x%08" PRIx32 ".",
301
0
     function,
302
0
     object_subtype );
303
304
0
    return( -1 );
305
0
  }
306
#if defined( HAVE_DEBUG_OUTPUT )
307
  if( libcnotify_verbose != 0 )
308
  {
309
    byte_stream_copy_to_uint64_little_endian(
310
     ( (fsapfs_fusion_middle_tree_t *) data )->object_checksum,
311
     value_64bit );
312
    libcnotify_printf(
313
     "%s: object checksum\t\t\t: 0x%08" PRIx64 "\n",
314
     function,
315
     value_64bit );
316
317
    byte_stream_copy_to_uint64_little_endian(
318
     ( (fsapfs_fusion_middle_tree_t *) data )->object_identifier,
319
     value_64bit );
320
    libcnotify_printf(
321
     "%s: object identifier\t\t: %" PRIu64 "\n",
322
     function,
323
     value_64bit );
324
325
    byte_stream_copy_to_uint64_little_endian(
326
     ( (fsapfs_fusion_middle_tree_t *) data )->object_transaction_identifier,
327
     value_64bit );
328
    libcnotify_printf(
329
     "%s: object transaction identifier\t: %" PRIu64 "\n",
330
     function,
331
     value_64bit );
332
333
    libcnotify_printf(
334
     "%s: object type\t\t\t: 0x%08" PRIx32 "\n",
335
     function,
336
     object_type );
337
338
    libcnotify_printf(
339
     "%s: object subtype\t\t\t: 0x%08" PRIx32 "\n",
340
     function,
341
     object_subtype );
342
343
    byte_stream_copy_to_uint32_little_endian(
344
     ( (fsapfs_fusion_middle_tree_t *) data )->unknown1,
345
     value_32bit );
346
    libcnotify_printf(
347
     "%s: unknown1\t\t\t: 0x%08" PRIx32 "\n",
348
     function,
349
     value_32bit );
350
351
    libcnotify_printf(
352
     "\n" );
353
  }
354
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
355
356
0
  return( 1 );
357
0
}
358