Coverage Report

Created: 2024-10-02 06:58

/src/libfsxfs/libfsxfs/libfsxfs_btree_header.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * B+ tree header functions
3
 *
4
 * Copyright (C) 2020-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 "libfsxfs_btree_header.h"
28
#include "libfsxfs_debug.h"
29
#include "libfsxfs_libcerror.h"
30
#include "libfsxfs_libcnotify.h"
31
#include "libfsxfs_libfguid.h"
32
33
#include "fsxfs_btree.h"
34
35
/* Creates a B+tree header
36
 * Make sure the value btree_header is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libfsxfs_btree_header_initialize(
40
     libfsxfs_btree_header_t **btree_header,
41
     libcerror_error_t **error )
42
10.0k
{
43
10.0k
  static char *function = "libfsxfs_btree_header_initialize";
44
45
10.0k
  if( btree_header == NULL )
46
0
  {
47
0
    libcerror_error_set(
48
0
     error,
49
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51
0
     "%s: invalid B+ tree header.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
10.0k
  if( *btree_header != NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
61
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62
0
     "%s: invalid B+ tree header value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
10.0k
  *btree_header = memory_allocate_structure(
68
10.0k
                   libfsxfs_btree_header_t );
69
70
10.0k
  if( *btree_header == NULL )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
75
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
76
0
     "%s: unable to create B+ tree header.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
10.0k
  if( memory_set(
82
10.0k
       *btree_header,
83
10.0k
       0,
84
10.0k
       sizeof( libfsxfs_btree_header_t ) ) == NULL )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
89
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
90
0
     "%s: unable to clear B+ tree header.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
10.0k
  return( 1 );
96
97
0
on_error:
98
0
  if( *btree_header != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *btree_header );
102
103
0
    *btree_header = NULL;
104
0
  }
105
0
  return( -1 );
106
10.0k
}
107
108
/* Frees a B+tree header
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libfsxfs_btree_header_free(
112
     libfsxfs_btree_header_t **btree_header,
113
     libcerror_error_t **error )
114
10.0k
{
115
10.0k
  static char *function = "libfsxfs_btree_header_free";
116
117
10.0k
  if( btree_header == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid B+ tree header.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
10.0k
  if( *btree_header != NULL )
129
10.0k
  {
130
10.0k
    memory_free(
131
10.0k
     *btree_header );
132
133
10.0k
    *btree_header = NULL;
134
10.0k
  }
135
10.0k
  return( 1 );
136
10.0k
}
137
138
/* Reads the B+tree header data
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libfsxfs_btree_header_read_data(
142
     libfsxfs_btree_header_t *btree_header,
143
     libfsxfs_io_handle_t *io_handle,
144
     const uint8_t *data,
145
     size_t data_size,
146
     size_t block_number_data_size,
147
     libcerror_error_t **error )
148
10.0k
{
149
10.0k
  static char *function   = "libfsxfs_btree_header_read_data";
150
10.0k
  size_t header_data_size = 0;
151
152
#if defined( HAVE_DEBUG_OUTPUT )
153
  uint64_t value_64bit    = 0;
154
  uint32_t value_32bit    = 0;
155
  int result              = 1;
156
#endif
157
158
10.0k
  if( btree_header == NULL )
159
0
  {
160
0
    libcerror_error_set(
161
0
     error,
162
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
163
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
164
0
     "%s: invalid B+ tree header.",
165
0
     function );
166
167
0
    return( -1 );
168
0
  }
169
10.0k
  if( io_handle == NULL )
170
0
  {
171
0
    libcerror_error_set(
172
0
     error,
173
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
174
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
175
0
     "%s: invalid IO handle.",
176
0
     function );
177
178
0
    return( -1 );
179
0
  }
180
10.0k
  if( ( block_number_data_size != 4 )
181
10.0k
   && ( block_number_data_size != 8 ) )
182
0
  {
183
0
    libcerror_error_set(
184
0
     error,
185
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
186
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
187
0
     "%s: unsupported block number data size.",
188
0
     function );
189
190
0
    return( -1 );
191
0
  }
192
10.0k
  if( io_handle->format_version == 5 )
193
4.62k
  {
194
4.62k
    if( block_number_data_size == 8 )
195
378
    {
196
378
      header_data_size = sizeof( fsxfs_btree_header_v5_64bit_t );
197
378
    }
198
4.24k
    else
199
4.24k
    {
200
4.24k
      header_data_size = sizeof( fsxfs_btree_header_v5_32bit_t );
201
4.24k
    }
202
4.62k
  }
203
5.42k
  else
204
5.42k
  {
205
5.42k
    if( block_number_data_size == 8 )
206
1.86k
    {
207
1.86k
      header_data_size = sizeof( fsxfs_btree_header_v1_64bit_t );
208
1.86k
    }
209
3.55k
    else
210
3.55k
    {
211
3.55k
      header_data_size = sizeof( fsxfs_btree_header_v1_32bit_t );
212
3.55k
    }
213
5.42k
  }
214
10.0k
  if( data == NULL )
215
0
  {
216
0
    libcerror_error_set(
217
0
     error,
218
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
219
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
220
0
     "%s: invalid data.",
221
0
     function );
222
223
0
    return( -1 );
224
0
  }
225
10.0k
  if( ( data_size < header_data_size )
226
10.0k
   || ( data_size > (size_t) SSIZE_MAX ) )
227
0
  {
228
0
    libcerror_error_set(
229
0
     error,
230
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
231
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
232
0
     "%s: invalid data size value out of bounds.",
233
0
     function );
234
235
0
    return( -1 );
236
0
  }
237
#if defined( HAVE_DEBUG_OUTPUT )
238
  if( libcnotify_verbose != 0 )
239
  {
240
    libcnotify_printf(
241
     "%s: B+ tree header data:\n",
242
     function );
243
    libcnotify_print_data(
244
     data,
245
     header_data_size,
246
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
247
  }
248
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
249
250
10.0k
  if( memory_copy(
251
10.0k
       btree_header->signature,
252
10.0k
       ( (fsxfs_btree_header_v1_32bit_t *) data )->signature,
253
10.0k
       4 ) == NULL )
254
0
  {
255
0
    libcerror_error_set(
256
0
     error,
257
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
258
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
259
0
     "%s: unable to copy signature.",
260
0
     function );
261
262
0
    return( -1 );
263
0
  }
264
10.0k
  byte_stream_copy_to_uint16_big_endian(
265
10.0k
   ( (fsxfs_btree_header_v1_32bit_t *) data )->level,
266
10.0k
   btree_header->level );
267
268
10.0k
  byte_stream_copy_to_uint16_big_endian(
269
10.0k
   ( (fsxfs_btree_header_v1_32bit_t *) data )->number_of_records,
270
10.0k
   btree_header->number_of_records );
271
272
#if defined( HAVE_DEBUG_OUTPUT )
273
  if( libcnotify_verbose != 0 )
274
  {
275
    libcnotify_printf(
276
     "%s: signature\t\t\t\t: %c%c%c%c\n",
277
     function,
278
     ( (fsxfs_btree_header_v1_32bit_t *) data )->signature[ 0 ],
279
     ( (fsxfs_btree_header_v1_32bit_t *) data )->signature[ 1 ],
280
     ( (fsxfs_btree_header_v1_32bit_t *) data )->signature[ 2 ],
281
     ( (fsxfs_btree_header_v1_32bit_t *) data )->signature[ 3 ] );
282
283
    libcnotify_printf(
284
     "%s: level\t\t\t\t\t: %" PRIu16 "\n",
285
     function,
286
     btree_header->level );
287
288
    libcnotify_printf(
289
     "%s: number of records\t\t\t: %" PRIu16 "\n",
290
     function,
291
     btree_header->number_of_records );
292
293
    if( block_number_data_size == 4 )
294
    {
295
      byte_stream_copy_to_uint32_big_endian(
296
       ( (fsxfs_btree_header_v1_32bit_t *) data )->previous_btree_block_number,
297
       value_64bit );
298
    }
299
    else
300
    {
301
      byte_stream_copy_to_uint64_big_endian(
302
       ( (fsxfs_btree_header_v1_64bit_t *) data )->previous_btree_block_number,
303
       value_64bit );
304
    }
305
    libcnotify_printf(
306
     "%s: previous B+ tree block number\t\t: %" PRIu64 "\n",
307
     function,
308
     value_64bit );
309
310
    if( block_number_data_size == 4 )
311
    {
312
      byte_stream_copy_to_uint32_big_endian(
313
       ( (fsxfs_btree_header_v1_32bit_t *) data )->next_btree_block_number,
314
       value_64bit );
315
    }
316
    else
317
    {
318
      byte_stream_copy_to_uint64_big_endian(
319
       ( (fsxfs_btree_header_v1_64bit_t *) data )->next_btree_block_number,
320
       value_64bit );
321
    }
322
    libcnotify_printf(
323
     "%s: next B+ tree block number\t\t: %" PRIu64 "\n",
324
     function,
325
     value_64bit );
326
  }
327
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
328
329
10.0k
  if( io_handle->format_version == 5 )
330
4.62k
  {
331
#if defined( HAVE_DEBUG_OUTPUT )
332
    if( libcnotify_verbose != 0 )
333
    {
334
      if( block_number_data_size == 4 )
335
      {
336
        byte_stream_copy_to_uint64_big_endian(
337
         ( (fsxfs_btree_header_v5_32bit_t *) data )->block_number,
338
         value_64bit );
339
      }
340
      else
341
      {
342
        byte_stream_copy_to_uint64_big_endian(
343
         ( (fsxfs_btree_header_v5_64bit_t *) data )->block_number,
344
         value_64bit );
345
      }
346
      libcnotify_printf(
347
       "%s: block number\t\t\t\t: %" PRIu64 "\n",
348
       function,
349
       value_64bit );
350
351
      if( block_number_data_size == 4 )
352
      {
353
        byte_stream_copy_to_uint64_big_endian(
354
         ( (fsxfs_btree_header_v5_32bit_t *) data )->log_sequence_number,
355
         value_64bit );
356
      }
357
      else
358
      {
359
        byte_stream_copy_to_uint64_big_endian(
360
         ( (fsxfs_btree_header_v5_64bit_t *) data )->log_sequence_number,
361
         value_64bit );
362
      }
363
      libcnotify_printf(
364
       "%s: log sequence number\t\t\t: 0x%08" PRIx64 "\n",
365
       function,
366
       value_64bit );
367
368
      if( block_number_data_size == 4 )
369
      {
370
        result = libfsxfs_debug_print_guid_value(
371
                  function,
372
                  "block type identifier\t\t\t",
373
                  ( (fsxfs_btree_header_v5_32bit_t *) data )->block_type_identifier,
374
                  16,
375
                  LIBFGUID_ENDIAN_BIG,
376
                  LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
377
                  error );
378
      }
379
      else
380
      {
381
        result = libfsxfs_debug_print_guid_value(
382
                  function,
383
                  "block type identifier\t\t\t",
384
                  ( (fsxfs_btree_header_v5_64bit_t *) data )->block_type_identifier,
385
                  16,
386
                  LIBFGUID_ENDIAN_BIG,
387
                  LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
388
                  error );
389
      }
390
      if( result != 1 )
391
      {
392
        libcerror_error_set(
393
         error,
394
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
395
         LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
396
         "%s: unable to print GUID value.",
397
         function );
398
399
        return( -1 );
400
      }
401
      if( block_number_data_size == 4 )
402
      {
403
        byte_stream_copy_to_uint32_big_endian(
404
         ( (fsxfs_btree_header_v5_32bit_t *) data )->owner_allocation_group,
405
         value_32bit );
406
      }
407
      else
408
      {
409
        byte_stream_copy_to_uint32_big_endian(
410
         ( (fsxfs_btree_header_v5_64bit_t *) data )->owner_allocation_group,
411
         value_32bit );
412
      }
413
      libcnotify_printf(
414
       "%s: owner allocation group\t\t\t: %" PRIu32 "\n",
415
       function,
416
       value_32bit );
417
418
      if( block_number_data_size == 4 )
419
      {
420
        byte_stream_copy_to_uint32_big_endian(
421
         ( (fsxfs_btree_header_v5_32bit_t *) data )->checksum,
422
         value_32bit );
423
      }
424
      else
425
      {
426
        byte_stream_copy_to_uint32_big_endian(
427
         ( (fsxfs_btree_header_v5_64bit_t *) data )->checksum,
428
         value_32bit );
429
      }
430
      libcnotify_printf(
431
       "%s: checksum\t\t\t\t: 0x%08" PRIx32 "\n",
432
       function,
433
       value_32bit );
434
435
      if( block_number_data_size == 8 )
436
      {
437
        byte_stream_copy_to_uint32_big_endian(
438
         ( (fsxfs_btree_header_v5_64bit_t *) data )->unknown1,
439
         value_32bit );
440
        libcnotify_printf(
441
         "%s: unknown1\t\t\t\t: 0x%08" PRIx32 "\n",
442
         function,
443
         value_32bit );
444
      }
445
    }
446
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
447
4.62k
  }
448
#if defined( HAVE_DEBUG_OUTPUT )
449
  if( libcnotify_verbose != 0 )
450
  {
451
    libcnotify_printf(
452
     "\n" );
453
  }
454
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
455
456
10.0k
  return( 1 );
457
10.0k
}
458