Coverage Report

Created: 2024-02-25 07:20

/src/libfsapfs/libfsapfs/libfsapfs_btree_footer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The B-tree footer 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_btree_footer.h"
28
#include "libfsapfs_debug.h"
29
#include "libfsapfs_libcerror.h"
30
#include "libfsapfs_libcnotify.h"
31
32
#include "fsapfs_btree.h"
33
34
/* Creates a B-tree footer
35
 * Make sure the value btree_footer is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libfsapfs_btree_footer_initialize(
39
     libfsapfs_btree_footer_t **btree_footer,
40
     libcerror_error_t **error )
41
15.6k
{
42
15.6k
  static char *function = "libfsapfs_btree_footer_initialize";
43
44
15.6k
  if( btree_footer == 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 B-tree footer.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
15.6k
  if( *btree_footer != 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 B-tree footer value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
15.6k
  *btree_footer = memory_allocate_structure(
67
15.6k
                   libfsapfs_btree_footer_t );
68
69
15.6k
  if( *btree_footer == 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 B-tree footer.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
15.6k
  if( memory_set(
81
15.6k
       *btree_footer,
82
15.6k
       0,
83
15.6k
       sizeof( libfsapfs_btree_footer_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 B-tree footer.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
15.6k
  return( 1 );
95
96
0
on_error:
97
0
  if( *btree_footer != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *btree_footer );
101
102
0
    *btree_footer = NULL;
103
0
  }
104
0
  return( -1 );
105
15.6k
}
106
107
/* Frees a B-tree footer
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libfsapfs_btree_footer_free(
111
     libfsapfs_btree_footer_t **btree_footer,
112
     libcerror_error_t **error )
113
15.6k
{
114
15.6k
  static char *function = "libfsapfs_btree_footer_free";
115
116
15.6k
  if( btree_footer == 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 B-tree footer.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
15.6k
  if( *btree_footer != NULL )
128
15.6k
  {
129
15.6k
    memory_free(
130
15.6k
     *btree_footer );
131
132
15.6k
    *btree_footer = NULL;
133
15.6k
  }
134
15.6k
  return( 1 );
135
15.6k
}
136
137
/* Reads the B-tree footer
138
 * Returns 1 if successful or -1 on error
139
 */
140
int libfsapfs_btree_footer_read_data(
141
     libfsapfs_btree_footer_t *btree_footer,
142
     const uint8_t *data,
143
     size_t data_size,
144
     libcerror_error_t **error )
145
15.6k
{
146
15.6k
  static char *function = "libfsapfs_btree_footer_read_data";
147
148
#if defined( HAVE_DEBUG_OUTPUT )
149
  uint64_t value_64bit  = 0;
150
#endif
151
152
15.6k
  if( btree_footer == NULL )
153
0
  {
154
0
    libcerror_error_set(
155
0
     error,
156
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
157
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
158
0
     "%s: invalid B-tree footer.",
159
0
     function );
160
161
0
    return( -1 );
162
0
  }
163
15.6k
  if( data == NULL )
164
0
  {
165
0
    libcerror_error_set(
166
0
     error,
167
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
168
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
169
0
     "%s: invalid data.",
170
0
     function );
171
172
0
    return( -1 );
173
0
  }
174
15.6k
  if( ( data_size < sizeof( fsapfs_btree_footer_t ) )
175
15.6k
   || ( data_size > (size_t) SSIZE_MAX ) )
176
0
  {
177
0
    libcerror_error_set(
178
0
     error,
179
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
180
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
181
0
     "%s: invalid data size value out of bounds.",
182
0
     function );
183
184
0
    return( -1 );
185
0
  }
186
#if defined( HAVE_DEBUG_OUTPUT )
187
  if( libcnotify_verbose != 0 )
188
  {
189
    libcnotify_printf(
190
     "%s: B-tree footer data:\n",
191
     function );
192
    libcnotify_print_data(
193
     data,
194
     sizeof( fsapfs_btree_footer_t ),
195
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
196
  }
197
#endif
198
15.6k
  byte_stream_copy_to_uint32_little_endian(
199
15.6k
   ( (fsapfs_btree_footer_t *) data )->flags,
200
15.6k
   btree_footer->flags );
201
202
15.6k
  byte_stream_copy_to_uint32_little_endian(
203
15.6k
   ( (fsapfs_btree_footer_t *) data )->node_size,
204
15.6k
   btree_footer->node_size );
205
206
15.6k
  byte_stream_copy_to_uint32_little_endian(
207
15.6k
   ( (fsapfs_btree_footer_t *) data )->key_size,
208
15.6k
   btree_footer->key_size );
209
210
15.6k
  byte_stream_copy_to_uint32_little_endian(
211
15.6k
   ( (fsapfs_btree_footer_t *) data )->value_size,
212
15.6k
   btree_footer->value_size );
213
214
15.6k
  byte_stream_copy_to_uint32_little_endian(
215
15.6k
   ( (fsapfs_btree_footer_t *) data )->maximum_key_size,
216
15.6k
   btree_footer->maximum_key_size );
217
218
15.6k
  byte_stream_copy_to_uint32_little_endian(
219
15.6k
   ( (fsapfs_btree_footer_t *) data )->maximum_value_size,
220
15.6k
   btree_footer->maximum_value_size );
221
222
#if defined( HAVE_DEBUG_OUTPUT )
223
  if( libcnotify_verbose != 0 )
224
  {
225
    libcnotify_printf(
226
     "%s: flags\t\t\t\t\t: 0x%08" PRIx32 "\n",
227
     function,
228
     btree_footer->flags );
229
    libfsapfs_debug_print_btree_flags(
230
     btree_footer->flags );
231
    libcnotify_printf(
232
     "\n" );
233
234
    libcnotify_printf(
235
     "%s: node size\t\t\t\t: %" PRIu32 "\n",
236
     function,
237
     btree_footer->node_size );
238
239
    libcnotify_printf(
240
     "%s: key size\t\t\t\t: %" PRIu32 "\n",
241
     function,
242
     btree_footer->key_size );
243
244
    libcnotify_printf(
245
     "%s: value size\t\t\t\t: %" PRIu32 "\n",
246
     function,
247
     btree_footer->value_size );
248
249
    libcnotify_printf(
250
     "%s: maximum key size\t\t\t: %" PRIu32 "\n",
251
     function,
252
     btree_footer->maximum_key_size );
253
254
    libcnotify_printf(
255
     "%s: maximum value size\t\t\t: %" PRIu32 "\n",
256
     function,
257
     btree_footer->maximum_value_size );
258
259
    byte_stream_copy_to_uint64_little_endian(
260
     ( (fsapfs_btree_footer_t *) data )->total_number_of_keys,
261
     value_64bit );
262
    libcnotify_printf(
263
     "%s: total number of keys\t\t\t: %" PRIu64 "\n",
264
     function,
265
     value_64bit );
266
267
    byte_stream_copy_to_uint64_little_endian(
268
     ( (fsapfs_btree_footer_t *) data )->total_number_of_nodes,
269
     value_64bit );
270
    libcnotify_printf(
271
     "%s: total number of nodes\t\t\t: %" PRIu64 "\n",
272
     function,
273
     value_64bit );
274
275
    libcnotify_printf(
276
     "\n" );
277
  }
278
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
279
280
15.6k
  return( 1 );
281
15.6k
}
282