Coverage Report

Created: 2025-06-24 07:14

/src/libfsapfs/libfsapfs/libfsapfs_object_map_descriptor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The object map descriptor 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_object_map_descriptor.h"
28
#include "libfsapfs_libcerror.h"
29
#include "libfsapfs_libcnotify.h"
30
31
#include "fsapfs_object_map.h"
32
33
/* Creates physical_map_entry
34
 * Make sure the value object_map_descriptor is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfsapfs_object_map_descriptor_initialize(
38
     libfsapfs_object_map_descriptor_t **object_map_descriptor,
39
     libcerror_error_t **error )
40
10.2k
{
41
10.2k
  static char *function = "libfsapfs_object_map_descriptor_initialize";
42
43
10.2k
  if( object_map_descriptor == NULL )
44
0
  {
45
0
    libcerror_error_set(
46
0
     error,
47
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
48
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
49
0
     "%s: invalid object map descriptor.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
10.2k
  if( *object_map_descriptor != NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
59
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
60
0
     "%s: invalid object map descriptor value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
10.2k
  *object_map_descriptor = memory_allocate_structure(
66
10.2k
                            libfsapfs_object_map_descriptor_t );
67
68
10.2k
  if( *object_map_descriptor == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
73
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
74
0
     "%s: unable to create object map descriptor.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
10.2k
  if( memory_set(
80
10.2k
       *object_map_descriptor,
81
10.2k
       0,
82
10.2k
       sizeof( libfsapfs_object_map_descriptor_t ) ) == NULL )
83
0
  {
84
0
    libcerror_error_set(
85
0
     error,
86
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
87
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
88
0
     "%s: unable to clear object map descriptor.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
10.2k
  return( 1 );
94
95
0
on_error:
96
0
  if( *object_map_descriptor != NULL )
97
0
  {
98
0
    memory_free(
99
0
     *object_map_descriptor );
100
101
0
    *object_map_descriptor = NULL;
102
0
  }
103
0
  return( -1 );
104
10.2k
}
105
106
/* Frees object map descriptor
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libfsapfs_object_map_descriptor_free(
110
     libfsapfs_object_map_descriptor_t **object_map_descriptor,
111
     libcerror_error_t **error )
112
10.2k
{
113
10.2k
  static char *function = "libfsapfs_object_map_descriptor_free";
114
115
10.2k
  if( object_map_descriptor == NULL )
116
0
  {
117
0
    libcerror_error_set(
118
0
     error,
119
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
120
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
121
0
     "%s: invalid object map descriptor.",
122
0
     function );
123
124
0
    return( -1 );
125
0
  }
126
10.2k
  if( *object_map_descriptor != NULL )
127
10.2k
  {
128
10.2k
    memory_free(
129
10.2k
     *object_map_descriptor );
130
131
10.2k
    *object_map_descriptor = NULL;
132
10.2k
  }
133
10.2k
  return( 1 );
134
10.2k
}
135
136
/* Reads the object map descriptor B-tree key data
137
 * Returns 1 if successful or -1 on error
138
 */
139
int libfsapfs_object_map_descriptor_read_key_data(
140
     libfsapfs_object_map_descriptor_t *object_map_descriptor,
141
     const uint8_t *data,
142
     size_t data_size,
143
     libcerror_error_t **error )
144
10.2k
{
145
10.2k
  static char *function = "libfsapfs_object_map_descriptor_read_key_data";
146
147
10.2k
  if( object_map_descriptor == NULL )
148
0
  {
149
0
    libcerror_error_set(
150
0
     error,
151
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
152
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
153
0
     "%s: invalid object map descriptor.",
154
0
     function );
155
156
0
    return( -1 );
157
0
  }
158
10.2k
  if( data == NULL )
159
0
  {
160
0
    libcerror_error_set(
161
0
     error,
162
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
163
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
164
0
     "%s: invalid data.",
165
0
     function );
166
167
0
    return( -1 );
168
0
  }
169
10.2k
  if( ( data_size < sizeof( fsapfs_object_map_btree_key_t ) )
170
10.2k
   || ( data_size > (size_t) SSIZE_MAX ) )
171
0
  {
172
0
    libcerror_error_set(
173
0
     error,
174
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
175
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
176
0
     "%s: invalid data size value out of bounds.",
177
0
     function );
178
179
0
    return( -1 );
180
0
  }
181
#if defined( HAVE_DEBUG_OUTPUT )
182
  if( libcnotify_verbose != 0 )
183
  {
184
    libcnotify_printf(
185
     "%s: object map B-tree key data:\n",
186
     function );
187
    libcnotify_print_data(
188
     data,
189
     sizeof( fsapfs_object_map_btree_key_t ),
190
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
191
  }
192
#endif
193
10.2k
  byte_stream_copy_to_uint64_little_endian(
194
10.2k
   ( (fsapfs_object_map_btree_key_t *) data )->object_identifier,
195
10.2k
   object_map_descriptor->identifier );
196
197
10.2k
  byte_stream_copy_to_uint64_little_endian(
198
10.2k
   ( (fsapfs_object_map_btree_key_t *) data )->object_transaction_identifier,
199
10.2k
   object_map_descriptor->transaction_identifier );
200
201
#if defined( HAVE_DEBUG_OUTPUT )
202
  if( libcnotify_verbose != 0 )
203
  {
204
    libcnotify_printf(
205
     "%s: object identifier\t\t: %" PRIu64 "\n",
206
     function,
207
     object_map_descriptor->identifier );
208
209
    libcnotify_printf(
210
     "%s: object transaction identifier\t: %" PRIu64 "\n",
211
     function,
212
     object_map_descriptor->transaction_identifier );
213
214
    libcnotify_printf(
215
     "\n" );
216
  }
217
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
218
219
10.2k
  return( 1 );
220
10.2k
}
221
222
/* Reads the object map descriptor B-tree value data
223
 * Returns 1 if successful or -1 on error
224
 */
225
int libfsapfs_object_map_descriptor_read_value_data(
226
     libfsapfs_object_map_descriptor_t *object_map_descriptor,
227
     const uint8_t *data,
228
     size_t data_size,
229
     libcerror_error_t **error )
230
10.2k
{
231
10.2k
  static char *function = "libfsapfs_object_map_descriptor_read_value_data";
232
233
10.2k
  if( object_map_descriptor == NULL )
234
0
  {
235
0
    libcerror_error_set(
236
0
     error,
237
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
238
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
239
0
     "%s: invalid object map descriptor.",
240
0
     function );
241
242
0
    return( -1 );
243
0
  }
244
10.2k
  if( data == NULL )
245
0
  {
246
0
    libcerror_error_set(
247
0
     error,
248
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
249
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
250
0
     "%s: invalid data.",
251
0
     function );
252
253
0
    return( -1 );
254
0
  }
255
10.2k
  if( ( data_size < sizeof( fsapfs_object_map_btree_value_t ) )
256
10.2k
   || ( data_size > (size_t) SSIZE_MAX ) )
257
0
  {
258
0
    libcerror_error_set(
259
0
     error,
260
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
261
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
262
0
     "%s: invalid data size value out of bounds.",
263
0
     function );
264
265
0
    return( -1 );
266
0
  }
267
#if defined( HAVE_DEBUG_OUTPUT )
268
  if( libcnotify_verbose != 0 )
269
  {
270
    libcnotify_printf(
271
     "%s: object map B-tree value data:\n",
272
     function );
273
    libcnotify_print_data(
274
     data,
275
     sizeof( fsapfs_object_map_btree_value_t ),
276
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
277
  }
278
#endif
279
10.2k
  byte_stream_copy_to_uint32_little_endian(
280
10.2k
   ( (fsapfs_object_map_btree_value_t *) data )->object_flags,
281
10.2k
   object_map_descriptor->flags );
282
283
10.2k
  byte_stream_copy_to_uint32_little_endian(
284
10.2k
   ( (fsapfs_object_map_btree_value_t *) data )->object_size,
285
10.2k
   object_map_descriptor->size );
286
287
10.2k
  byte_stream_copy_to_uint64_little_endian(
288
10.2k
   ( (fsapfs_object_map_btree_value_t *) data )->object_physical_address,
289
10.2k
   object_map_descriptor->physical_address );
290
291
#if defined( HAVE_DEBUG_OUTPUT )
292
  if( libcnotify_verbose != 0 )
293
  {
294
    libcnotify_printf(
295
     "%s: object flags\t\t\t: 0x%04" PRIx32 "\n",
296
     function,
297
     object_map_descriptor->flags );
298
299
    libcnotify_printf(
300
     "%s: object size\t\t\t: %" PRIu32 "\n",
301
     function,
302
     object_map_descriptor->size );
303
304
    libcnotify_printf(
305
     "%s: object physical address\t: %" PRIu64 "\n",
306
     function,
307
     object_map_descriptor->physical_address );
308
309
    libcnotify_printf(
310
     "\n" );
311
  }
312
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
313
314
10.2k
  return( 1 );
315
10.2k
}
316