Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpff/libpff/libpff_local_descriptor_value.c
Line
Count
Source
1
/*
2
 * Local descriptor value functions
3
 *
4
 * Copyright (C) 2008-2026, 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 "libpff_definitions.h"
28
#include "libpff_io_handle.h"
29
#include "libpff_libcerror.h"
30
#include "libpff_libcnotify.h"
31
#include "libpff_local_descriptor_value.h"
32
33
#include "pff_local_descriptors_node.h"
34
35
/* Creates a local descriptor value
36
 * Make sure the value local_descriptor_value is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libpff_local_descriptor_value_initialize(
40
     libpff_local_descriptor_value_t **local_descriptor_value,
41
     libcerror_error_t **error )
42
17.9k
{
43
17.9k
  static char *function = "libpff_local_descriptor_value_initialize";
44
45
17.9k
  if( local_descriptor_value == NULL )
46
0
  {
47
0
    libcerror_error_set(
48
0
     error,
49
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51
0
     "%s: invalid local descriptor value.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
17.9k
  if( *local_descriptor_value != NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
61
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62
0
     "%s: invalid local descriptor value value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
17.9k
  *local_descriptor_value = memory_allocate_structure(
68
17.9k
                             libpff_local_descriptor_value_t );
69
70
17.9k
  if( *local_descriptor_value == NULL )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
75
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
76
0
     "%s: unable to create local descriptor value.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
17.9k
  if( memory_set(
82
17.9k
       *local_descriptor_value,
83
17.9k
       0,
84
17.9k
       sizeof( libpff_local_descriptor_value_t ) ) == NULL )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
89
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
90
0
     "%s: unable to clear local descriptor value.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
17.9k
  return( 1 );
96
97
0
on_error:
98
0
  if( *local_descriptor_value != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *local_descriptor_value );
102
103
0
    *local_descriptor_value = NULL;
104
0
  }
105
0
  return( -1 );
106
17.9k
}
107
108
/* Frees a local descriptor value
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libpff_local_descriptor_value_free(
112
     libpff_local_descriptor_value_t **local_descriptor_value,
113
     libcerror_error_t **error )
114
17.9k
{
115
17.9k
  static char *function = "libpff_local_descriptor_value_free";
116
117
17.9k
  if( local_descriptor_value == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid local descriptor value.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
17.9k
  if( *local_descriptor_value != NULL )
129
17.9k
  {
130
17.9k
    memory_free(
131
17.9k
     *local_descriptor_value );
132
133
17.9k
    *local_descriptor_value = NULL;
134
17.9k
  }
135
17.9k
  return( 1 );
136
17.9k
}
137
138
/* Reads the local descriptor value data
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libpff_local_descriptor_value_read_data(
142
     libpff_local_descriptor_value_t *local_descriptor_value,
143
     libpff_io_handle_t *io_handle,
144
     const uint8_t *data,
145
     size_t data_size,
146
     libcerror_error_t **error )
147
17.9k
{
148
17.9k
  static char *function = "libpff_local_descriptor_value_read_data";
149
17.9k
  size_t value_size     = 0;
150
151
17.9k
  if( local_descriptor_value == NULL )
152
0
  {
153
0
    libcerror_error_set(
154
0
     error,
155
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
156
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
157
0
     "%s: invalid local descriptor value.",
158
0
     function );
159
160
0
    return( -1 );
161
0
  }
162
17.9k
  if( io_handle == NULL )
163
0
  {
164
0
    libcerror_error_set(
165
0
     error,
166
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
167
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
168
0
     "%s: invalid IO handle.",
169
0
     function );
170
171
0
    return( -1 );
172
0
  }
173
17.9k
  if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
174
0
  {
175
0
    value_size = sizeof( pff_local_descriptor_leaf_node_entry_type_32bit_t );
176
0
  }
177
17.9k
  else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
178
0
        || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
179
17.9k
  {
180
17.9k
    value_size = sizeof( pff_local_descriptor_leaf_node_entry_type_64bit_t );
181
17.9k
  }
182
17.9k
  if( data == NULL )
183
0
  {
184
0
    libcerror_error_set(
185
0
     error,
186
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
187
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
188
0
     "%s: invalid data.",
189
0
     function );
190
191
0
    return( -1 );
192
0
  }
193
17.9k
  if( ( data_size < value_size )
194
17.9k
   || ( data_size > SSIZE_MAX ) )
195
0
  {
196
0
    libcerror_error_set(
197
0
     error,
198
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
199
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
200
0
     "%s: unsupported data size value out of bounds.",
201
0
     function );
202
203
0
    return( -1 );
204
0
  }
205
#if defined( HAVE_DEBUG_OUTPUT )
206
  if( libcnotify_verbose != 0 )
207
  {
208
    libcnotify_printf(
209
     "%s: local descriptor value data:\n",
210
     function );
211
    libcnotify_print_data(
212
     data,
213
     value_size,
214
     0 );
215
  }
216
#endif
217
17.9k
  if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
218
0
  {
219
0
    byte_stream_copy_to_uint32_little_endian(
220
0
     ( (pff_local_descriptor_leaf_node_entry_type_32bit_t *) data )->identifier,
221
0
     local_descriptor_value->identifier );
222
223
0
    byte_stream_copy_to_uint32_little_endian(
224
0
     ( (pff_local_descriptor_leaf_node_entry_type_32bit_t *) data )->data_identifier,
225
0
     local_descriptor_value->data_identifier );
226
227
0
    byte_stream_copy_to_uint32_little_endian(
228
0
     ( (pff_local_descriptor_leaf_node_entry_type_32bit_t *) data )->local_descriptors_identifier,
229
0
     local_descriptor_value->local_descriptors_identifier );
230
0
  }
231
17.9k
  else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
232
0
        || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
233
17.9k
  {
234
17.9k
    byte_stream_copy_to_uint64_little_endian(
235
17.9k
     ( (pff_local_descriptor_leaf_node_entry_type_64bit_t *) data )->identifier,
236
17.9k
     local_descriptor_value->identifier );
237
238
17.9k
    byte_stream_copy_to_uint64_little_endian(
239
17.9k
     ( (pff_local_descriptor_leaf_node_entry_type_64bit_t *) data )->data_identifier,
240
17.9k
     local_descriptor_value->data_identifier );
241
242
17.9k
    byte_stream_copy_to_uint64_little_endian(
243
17.9k
     ( (pff_local_descriptor_leaf_node_entry_type_64bit_t *) data )->local_descriptors_identifier,
244
17.9k
     local_descriptor_value->local_descriptors_identifier );
245
17.9k
  }
246
  /* Ignore the upper 32-bit of descriptor identifiers
247
   */
248
17.9k
  local_descriptor_value->identifier &= 0xffffffffUL;
249
250
#if defined( HAVE_DEBUG_OUTPUT )
251
  if( libcnotify_verbose != 0 )
252
  {
253
    libcnotify_printf(
254
     "%s: identifier\t\t\t\t: %" PRIu64 " (0x%08" PRIx64 ")\n",
255
     function,
256
     local_descriptor_value->identifier,
257
     local_descriptor_value->identifier );
258
259
    libcnotify_printf(
260
     "%s: data identifier\t\t\t\t: %" PRIu64 " (0x%08" PRIx64 ")\n",
261
     function,
262
     local_descriptor_value->data_identifier,
263
     local_descriptor_value->data_identifier );
264
265
    libcnotify_printf(
266
     "%s: local descriptors identifier\t\t\t: %" PRIu64 " (0x%08" PRIx64 ")\n",
267
     function,
268
     local_descriptor_value->local_descriptors_identifier,
269
     local_descriptor_value->local_descriptors_identifier );
270
271
    libcnotify_printf(
272
     "\n" );
273
  }
274
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
275
276
17.9k
  return( 1 );
277
17.9k
}
278