Coverage Report

Created: 2025-06-13 07:22

/src/libpff/libpff/libpff_local_descriptors_tree.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Local descriptors tree functions
3
 *
4
 * Copyright (C) 2008-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 <memory.h>
24
#include <types.h>
25
26
#include "libpff_definitions.h"
27
#include "libpff_index_value.h"
28
#include "libpff_libbfio.h"
29
#include "libpff_libcerror.h"
30
#include "libpff_libcnotify.h"
31
#include "libpff_local_descriptor_value.h"
32
#include "libpff_local_descriptors.h"
33
#include "libpff_local_descriptors_tree.h"
34
#include "libpff_offsets_index.h"
35
36
/* Creates a local descriptors tree
37
 * Make sure the value local_descriptors_tree is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libpff_local_descriptors_tree_initialize(
41
     libpff_local_descriptors_tree_t **local_descriptors_tree,
42
     libpff_io_handle_t *io_handle,
43
     libpff_offsets_index_t *offsets_index,
44
     uint32_t descriptor_identifier,
45
     uint64_t root_data_identifier,
46
     uint8_t recovered,
47
     int recovered_value_index,
48
     libcerror_error_t **error )
49
5.83k
{
50
5.83k
  static char *function = "libpff_local_descriptors_tree_initialize";
51
52
5.83k
  if( local_descriptors_tree == NULL )
53
0
  {
54
0
    libcerror_error_set(
55
0
     error,
56
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
57
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
58
0
     "%s: invalid local descriptors tree.",
59
0
     function );
60
61
0
    return( -1 );
62
0
  }
63
5.83k
  if( *local_descriptors_tree != NULL )
64
0
  {
65
0
    libcerror_error_set(
66
0
     error,
67
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
68
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
69
0
     "%s: invalid local descriptors tree value already set.",
70
0
     function );
71
72
0
    return( -1 );
73
0
  }
74
5.83k
  *local_descriptors_tree = memory_allocate_structure(
75
5.83k
                             libpff_local_descriptors_tree_t );
76
77
5.83k
  if( *local_descriptors_tree == NULL )
78
0
  {
79
0
    libcerror_error_set(
80
0
     error,
81
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
82
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
83
0
     "%s: unable to create local descriptors tree.",
84
0
     function );
85
86
0
    goto on_error;
87
0
  }
88
5.83k
  if( memory_set(
89
5.83k
       *local_descriptors_tree,
90
5.83k
       0,
91
5.83k
       sizeof( libpff_local_descriptors_tree_t ) ) == NULL )
92
0
  {
93
0
    libcerror_error_set(
94
0
     error,
95
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
96
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
97
0
     "%s: unable to clear file.",
98
0
     function );
99
100
0
    memory_free(
101
0
     *local_descriptors_tree );
102
103
0
    *local_descriptors_tree = NULL;
104
105
0
    return( -1 );
106
0
  }
107
5.83k
  if( libpff_local_descriptors_initialize(
108
5.83k
       &( ( *local_descriptors_tree )->local_descriptors ),
109
5.83k
       io_handle,
110
5.83k
       offsets_index,
111
5.83k
       descriptor_identifier,
112
5.83k
       root_data_identifier,
113
5.83k
       recovered,
114
5.83k
       error ) != 1 )
115
0
  {
116
0
    libcerror_error_set(
117
0
     error,
118
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
119
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
120
0
     "%s: unable to create local descriptors.",
121
0
     function );
122
123
0
    goto on_error;
124
0
  }
125
5.83k
  ( *local_descriptors_tree )->io_handle             = io_handle;
126
5.83k
  ( *local_descriptors_tree )->offsets_index         = offsets_index;
127
5.83k
  ( *local_descriptors_tree )->descriptor_identifier = descriptor_identifier;
128
5.83k
  ( *local_descriptors_tree )->root_data_identifier  = root_data_identifier;
129
5.83k
  ( *local_descriptors_tree )->recovered             = recovered;
130
5.83k
  ( *local_descriptors_tree )->recovered_value_index = recovered_value_index;
131
132
5.83k
  return( 1 );
133
134
0
on_error:
135
0
  if( *local_descriptors_tree != NULL )
136
0
  {
137
0
    memory_free(
138
0
     *local_descriptors_tree );
139
140
0
    *local_descriptors_tree = NULL;
141
0
  }
142
0
  return( -1 );
143
5.83k
}
144
145
/* Frees a local descriptors tree
146
 * Returns 1 if successful or -1 on error
147
 */
148
int libpff_local_descriptors_tree_free(
149
     libpff_local_descriptors_tree_t **local_descriptors_tree,
150
     libcerror_error_t **error )
151
5.83k
{
152
5.83k
  static char *function = "libpff_local_descriptors_tree_free";
153
5.83k
  int result            = 1;
154
155
5.83k
  if( local_descriptors_tree == NULL )
156
0
  {
157
0
    libcerror_error_set(
158
0
     error,
159
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
160
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
161
0
     "%s: invalid local descriptors tree.",
162
0
     function );
163
164
0
    return( -1 );
165
0
  }
166
5.83k
  if( *local_descriptors_tree != NULL )
167
5.83k
  {
168
5.83k
    if( libpff_local_descriptors_free(
169
5.83k
         &( ( *local_descriptors_tree )->local_descriptors ),
170
5.83k
         error ) != 1 )
171
0
    {
172
0
      libcerror_error_set(
173
0
       error,
174
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
175
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
176
0
       "%s: unable to free local descriptors.",
177
0
       function );
178
179
0
      result = -1;
180
0
    }
181
5.83k
    memory_free(
182
5.83k
     *local_descriptors_tree );
183
184
5.83k
    *local_descriptors_tree = NULL;
185
5.83k
  }
186
5.83k
  return( result );
187
5.83k
}
188
189
/* Clones a record set
190
 * Make sure the value destination_local_descriptors_tree is referencing, is set to NULL
191
 * Returns 1 if successful or -1 on error
192
 */
193
int libpff_local_descriptors_tree_clone(
194
     libpff_local_descriptors_tree_t **destination_local_descriptors_tree,
195
     libpff_local_descriptors_tree_t *source_local_descriptors_tree,
196
     libcerror_error_t **error )
197
0
{
198
0
  static char *function = "libpff_local_descriptors_tree_clone";
199
200
0
  if( destination_local_descriptors_tree == NULL )
201
0
  {
202
0
    libcerror_error_set(
203
0
     error,
204
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
205
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
206
0
     "%s: invalid destination local descriptors tree.",
207
0
     function );
208
209
0
    return( -1 );
210
0
  }
211
0
  if( *destination_local_descriptors_tree != NULL )
212
0
  {
213
0
    libcerror_error_set(
214
0
     error,
215
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
216
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
217
0
     "%s: invalid destination local descriptors tree value already set.",
218
0
     function );
219
220
0
    return( -1 );
221
0
  }
222
0
  if( source_local_descriptors_tree == NULL )
223
0
  {
224
0
    *destination_local_descriptors_tree = NULL;
225
226
0
    return( 1 );
227
0
  }
228
0
  if( libpff_local_descriptors_tree_initialize(
229
0
       destination_local_descriptors_tree,
230
0
       source_local_descriptors_tree->io_handle,
231
0
       source_local_descriptors_tree->offsets_index,
232
0
       source_local_descriptors_tree->descriptor_identifier,
233
0
       source_local_descriptors_tree->root_data_identifier,
234
0
       source_local_descriptors_tree->recovered,
235
0
       source_local_descriptors_tree->recovered_value_index,
236
0
       error ) != 1 )
237
0
  {
238
0
    libcerror_error_set(
239
0
     error,
240
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
241
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
242
0
     "%s: unable to create destination local descriptors tree",
243
0
     function );
244
245
0
    goto on_error;
246
0
  }
247
0
  return( 1 );
248
249
0
on_error:
250
0
  if( *destination_local_descriptors_tree != NULL )
251
0
  {
252
0
    libpff_local_descriptors_tree_free(
253
0
     destination_local_descriptors_tree,
254
0
     NULL );
255
0
  }
256
0
  return( -1 );
257
0
}
258
259
/* Retrieves the local descriptor value for the specific identifier
260
 * Returns 1 if successful, 0 if no value was found or -1 on error
261
 */
262
int libpff_local_descriptors_tree_get_value_by_identifier(
263
     libpff_local_descriptors_tree_t *local_descriptors_tree,
264
     libbfio_handle_t *file_io_handle,
265
     uint64_t identifier,
266
     libpff_local_descriptor_value_t **local_descriptor_value,
267
     libcerror_error_t **error )
268
13.5k
{
269
13.5k
  static char *function = "libpff_local_descriptors_tree_get_value_by_identifier";
270
13.5k
  int result            = 0;
271
272
13.5k
  if( local_descriptors_tree == NULL )
273
12
  {
274
12
    libcerror_error_set(
275
12
     error,
276
12
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
277
12
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
278
12
     "%s: invalid local descriptors tree.",
279
12
     function );
280
281
12
    return( -1 );
282
12
  }
283
13.5k
  result = libpff_local_descriptors_get_value_by_identifier(
284
13.5k
      local_descriptors_tree->local_descriptors,
285
13.5k
      local_descriptors_tree->io_handle,
286
13.5k
      file_io_handle,
287
13.5k
      identifier,
288
13.5k
      local_descriptor_value,
289
13.5k
      error );
290
291
13.5k
  if( result == -1 )
292
615
  {
293
615
    libcerror_error_set(
294
615
     error,
295
615
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
296
615
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
297
615
     "%s: unable to retrieve index value: %" PRIu32 " from index.",
298
615
     function,
299
615
     identifier );
300
301
615
    return( -1 );
302
615
  }
303
12.9k
  return( result );
304
13.5k
}
305