Coverage Report

Created: 2023-06-07 06:53

/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-2023, 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.0k
{
42
15.0k
  static char *function = "libfsapfs_btree_footer_initialize";
43
44
15.0k
  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.0k
  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.0k
  *btree_footer = memory_allocate_structure(
67
15.0k
                   libfsapfs_btree_footer_t );
68
69
15.0k
  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.0k
  if( memory_set(
81
15.0k
       *btree_footer,
82
15.0k
       0,
83
15.0k
       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.0k
  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.0k
}
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.0k
{
114
15.0k
  static char *function = "libfsapfs_btree_footer_free";
115
116
15.0k
  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.0k
  if( *btree_footer != NULL )
128
15.0k
  {
129
15.0k
    memory_free(
130
15.0k
     *btree_footer );
131
132
15.0k
    *btree_footer = NULL;
133
15.0k
  }
134
15.0k
  return( 1 );
135
15.0k
}
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.0k
{
146
15.0k
  static char *function = "libfsapfs_btree_footer_read_data";
147
148
#if defined( HAVE_DEBUG_OUTPUT )
149
  uint64_t value_64bit  = 0;
150
  uint32_t value_32bit  = 0;
151
#endif
152
153
15.0k
  if( btree_footer == NULL )
154
0
  {
155
0
    libcerror_error_set(
156
0
     error,
157
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
158
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
159
0
     "%s: invalid B-tree footer.",
160
0
     function );
161
162
0
    return( -1 );
163
0
  }
164
15.0k
  if( data == NULL )
165
0
  {
166
0
    libcerror_error_set(
167
0
     error,
168
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
169
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
170
0
     "%s: invalid data.",
171
0
     function );
172
173
0
    return( -1 );
174
0
  }
175
15.0k
  if( ( data_size < sizeof( fsapfs_btree_footer_t ) )
176
15.0k
   || ( data_size > (size_t) SSIZE_MAX ) )
177
0
  {
178
0
    libcerror_error_set(
179
0
     error,
180
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
181
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
182
0
     "%s: invalid data size value out of bounds.",
183
0
     function );
184
185
0
    return( -1 );
186
0
  }
187
#if defined( HAVE_DEBUG_OUTPUT )
188
  if( libcnotify_verbose != 0 )
189
  {
190
    libcnotify_printf(
191
     "%s: B-tree footer data:\n",
192
     function );
193
    libcnotify_print_data(
194
     data,
195
     sizeof( fsapfs_btree_footer_t ),
196
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
197
  }
198
#endif
199
15.0k
  byte_stream_copy_to_uint32_little_endian(
200
15.0k
   ( (fsapfs_btree_footer_t *) data )->node_size,
201
15.0k
   btree_footer->node_size );
202
203
15.0k
  byte_stream_copy_to_uint32_little_endian(
204
15.0k
   ( (fsapfs_btree_footer_t *) data )->key_size,
205
15.0k
   btree_footer->key_size );
206
207
15.0k
  byte_stream_copy_to_uint32_little_endian(
208
15.0k
   ( (fsapfs_btree_footer_t *) data )->value_size,
209
15.0k
   btree_footer->value_size );
210
211
15.0k
  byte_stream_copy_to_uint32_little_endian(
212
15.0k
   ( (fsapfs_btree_footer_t *) data )->maximum_key_size,
213
15.0k
   btree_footer->maximum_key_size );
214
215
15.0k
  byte_stream_copy_to_uint32_little_endian(
216
15.0k
   ( (fsapfs_btree_footer_t *) data )->maximum_value_size,
217
15.0k
   btree_footer->maximum_value_size );
218
219
#if defined( HAVE_DEBUG_OUTPUT )
220
  if( libcnotify_verbose != 0 )
221
  {
222
    byte_stream_copy_to_uint32_little_endian(
223
     ( (fsapfs_btree_footer_t *) data )->flags,
224
     value_32bit );
225
    libcnotify_printf(
226
     "%s: flags\t\t\t\t\t: 0x%08" PRIx32 "\n",
227
     function,
228
     value_32bit );
229
    libfsapfs_debug_print_btree_flags(
230
     value_32bit );
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.0k
  return( 1 );
281
15.0k
}
282