Coverage Report

Created: 2026-04-10 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsapfs/libfsapfs/libfsapfs_btree_entry.c
Line
Count
Source
1
/*
2
 * The B-tree entry 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_btree_entry.h"
28
#include "libfsapfs_libcerror.h"
29
#include "libfsapfs_libcnotify.h"
30
31
#include "fsapfs_btree.h"
32
#include "fsapfs_object.h"
33
34
/* Creates a B-tree entry
35
 * Make sure the value btree_entry is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libfsapfs_btree_entry_initialize(
39
     libfsapfs_btree_entry_t **btree_entry,
40
     libcerror_error_t **error )
41
161k
{
42
161k
  static char *function = "libfsapfs_btree_entry_initialize";
43
44
161k
  if( btree_entry == 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 entry.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
161k
  if( *btree_entry != 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 entry value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
161k
  *btree_entry = memory_allocate_structure(
67
161k
                   libfsapfs_btree_entry_t );
68
69
161k
  if( *btree_entry == 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 entry.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
161k
  if( memory_set(
81
161k
       *btree_entry,
82
161k
       0,
83
161k
       sizeof( libfsapfs_btree_entry_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 entry.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
161k
  return( 1 );
95
96
0
on_error:
97
0
  if( *btree_entry != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *btree_entry );
101
102
0
    *btree_entry = NULL;
103
0
  }
104
0
  return( -1 );
105
161k
}
106
107
/* Frees a B-tree entry
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libfsapfs_btree_entry_free(
111
     libfsapfs_btree_entry_t **btree_entry,
112
     libcerror_error_t **error )
113
161k
{
114
161k
  static char *function = "libfsapfs_btree_entry_free";
115
116
161k
  if( btree_entry == 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 entry.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
161k
  if( *btree_entry != NULL )
128
161k
  {
129
161k
    if( ( *btree_entry )->value_data != NULL )
130
135k
    {
131
135k
      memory_free(
132
135k
       ( *btree_entry )->value_data );
133
135k
    }
134
161k
    if( ( *btree_entry )->key_data != NULL )
135
137k
    {
136
137k
      memory_free(
137
137k
       ( *btree_entry )->key_data );
138
137k
    }
139
161k
    memory_free(
140
161k
     *btree_entry );
141
142
161k
    *btree_entry = NULL;
143
161k
  }
144
161k
  return( 1 );
145
161k
}
146
147
/* Sets the key data
148
 * Returns 1 if successful or -1 on error
149
 */
150
int libfsapfs_btree_entry_set_key_data(
151
     libfsapfs_btree_entry_t *btree_entry,
152
     const uint8_t *key_data,
153
     size_t key_data_size,
154
     libcerror_error_t **error )
155
161k
{
156
161k
  static char *function = "libfsapfs_btree_entry_set_key_data";
157
158
161k
  if( btree_entry == 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 entry.",
165
0
     function );
166
167
0
    return( -1 );
168
0
  }
169
161k
  if( btree_entry->key_data != NULL )
170
0
  {
171
0
    libcerror_error_set(
172
0
     error,
173
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
174
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
175
0
     "%s: invalid B-tree entry - key data value already set.",
176
0
     function );
177
178
0
    return( -1 );
179
0
  }
180
161k
  if( key_data == NULL )
181
0
  {
182
0
    libcerror_error_set(
183
0
     error,
184
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
185
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
186
0
     "%s: invalid key data.",
187
0
     function );
188
189
0
    return( -1 );
190
0
  }
191
161k
  if( key_data_size > 0 )
192
137k
  {
193
137k
    if( key_data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE )
194
0
    {
195
0
      libcerror_error_set(
196
0
       error,
197
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
198
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
199
0
       "%s: invalid key data size value exceeds maximum allocation size.",
200
0
       function );
201
202
0
      return( -1 );
203
0
    }
204
137k
    btree_entry->key_data = (uint8_t *) memory_allocate(
205
137k
                                         sizeof( uint8_t ) * key_data_size );
206
207
137k
    if( btree_entry->key_data == NULL )
208
0
    {
209
0
      libcerror_error_set(
210
0
       error,
211
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
212
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
213
0
       "%s: unable to create key data.",
214
0
       function );
215
216
0
      goto on_error;
217
0
    }
218
137k
    if( memory_copy(
219
137k
         btree_entry->key_data,
220
137k
         key_data,
221
137k
         key_data_size ) == NULL )
222
0
    {
223
0
      libcerror_error_set(
224
0
       error,
225
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
226
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
227
0
       "%s: unable to copy key data.",
228
0
       function );
229
230
0
      goto on_error;
231
0
    }
232
137k
    btree_entry->key_data_size = key_data_size;
233
137k
  }
234
161k
  return( 1 );
235
236
0
on_error:
237
0
  if( btree_entry->key_data != NULL )
238
0
  {
239
0
    memory_free(
240
0
     btree_entry->key_data );
241
242
0
    btree_entry->key_data = NULL;
243
0
  }
244
0
  btree_entry->key_data_size = 0;
245
246
0
  return( -1 );
247
161k
}
248
249
/* Sets the value data
250
 * Returns 1 if successful or -1 on error
251
 */
252
int libfsapfs_btree_entry_set_value_data(
253
     libfsapfs_btree_entry_t *btree_entry,
254
     const uint8_t *value_data,
255
     size_t value_data_size,
256
     libcerror_error_t **error )
257
161k
{
258
161k
  static char *function = "libfsapfs_btree_entry_set_value_data";
259
260
161k
  if( btree_entry == NULL )
261
0
  {
262
0
    libcerror_error_set(
263
0
     error,
264
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
265
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
266
0
     "%s: invalid B-tree entry.",
267
0
     function );
268
269
0
    return( -1 );
270
0
  }
271
161k
  if( btree_entry->value_data != NULL )
272
0
  {
273
0
    libcerror_error_set(
274
0
     error,
275
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
276
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
277
0
     "%s: invalid B-tree entry - value data value already set.",
278
0
     function );
279
280
0
    return( -1 );
281
0
  }
282
161k
  if( value_data == NULL )
283
0
  {
284
0
    libcerror_error_set(
285
0
     error,
286
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
287
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
288
0
     "%s: invalid value data.",
289
0
     function );
290
291
0
    return( -1 );
292
0
  }
293
161k
  if( value_data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE )
294
0
  {
295
0
    libcerror_error_set(
296
0
     error,
297
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
298
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
299
0
     "%s: invalid value data size value exceeds maximum.",
300
0
     function );
301
302
0
    return( -1 );
303
0
  }
304
161k
  if( value_data_size > 0 )
305
135k
  {
306
135k
    btree_entry->value_data = (uint8_t *) memory_allocate(
307
135k
                                           sizeof( uint8_t ) * value_data_size );
308
309
135k
    if( btree_entry->value_data == NULL )
310
0
    {
311
0
      libcerror_error_set(
312
0
       error,
313
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
314
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
315
0
       "%s: unable to create value data.",
316
0
       function );
317
318
0
      goto on_error;
319
0
    }
320
135k
    if( memory_copy(
321
135k
         btree_entry->value_data,
322
135k
         value_data,
323
135k
         value_data_size ) == NULL )
324
0
    {
325
0
      libcerror_error_set(
326
0
       error,
327
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
328
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
329
0
       "%s: unable to copy value data.",
330
0
       function );
331
332
0
      goto on_error;
333
0
    }
334
135k
    btree_entry->value_data_size = value_data_size;
335
135k
  }
336
161k
  return( 1 );
337
338
0
on_error:
339
0
  if( btree_entry->value_data != NULL )
340
0
  {
341
0
    memory_free(
342
0
     btree_entry->value_data );
343
344
0
    btree_entry->value_data = NULL;
345
0
  }
346
0
  btree_entry->value_data_size = 0;
347
348
0
  return( -1 );
349
161k
}
350