Coverage Report

Created: 2025-06-22 07:35

/src/libphdi/libphdi/libphdi_snapshot_values.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Snapshot values functions
3
 *
4
 * Copyright (C) 2015-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 "libphdi_extent_table.h"
27
#include "libphdi_libcerror.h"
28
#include "libphdi_libcnotify.h"
29
#include "libphdi_snapshot_values.h"
30
#include "libphdi_storage_image.h"
31
#include "libphdi_uuid_string.h"
32
33
const uint8_t libphdi_snapshot_values_empty_identifier[ 16 ] = {
34
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
35
36
/* Creates snapshot values
37
 * Make sure the value snapshot_values is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libphdi_snapshot_values_initialize(
41
     libphdi_snapshot_values_t **snapshot_values,
42
     libcerror_error_t **error )
43
0
{
44
0
  static char *function = "libphdi_snapshot_values_initialize";
45
46
0
  if( snapshot_values == NULL )
47
0
  {
48
0
    libcerror_error_set(
49
0
     error,
50
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52
0
     "%s: invalid snapshot values.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
0
  if( *snapshot_values != NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
62
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
63
0
     "%s: invalid snapshot values value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
0
  *snapshot_values = memory_allocate_structure(
69
0
                      libphdi_snapshot_values_t );
70
71
0
  if( *snapshot_values == NULL )
72
0
  {
73
0
    libcerror_error_set(
74
0
     error,
75
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
76
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
77
0
     "%s: unable to create snapshot values.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
0
  if( memory_set(
83
0
       *snapshot_values,
84
0
       0,
85
0
       sizeof( libphdi_snapshot_values_t ) ) == NULL )
86
0
  {
87
0
    libcerror_error_set(
88
0
     error,
89
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
90
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
91
0
     "%s: unable to clear snapshot values.",
92
0
     function );
93
94
0
    memory_free(
95
0
     *snapshot_values );
96
97
0
    *snapshot_values = NULL;
98
99
0
    return( -1 );
100
0
  }
101
0
  if( libphdi_extent_table_initialize(
102
0
       &( ( *snapshot_values )->extent_table ),
103
0
       error ) != 1 )
104
0
  {
105
0
    libcerror_error_set(
106
0
     error,
107
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
108
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
109
0
     "%s: unable to create extent table.",
110
0
     function );
111
112
0
    goto on_error;
113
0
  }
114
0
  return( 1 );
115
116
0
on_error:
117
0
  if( *snapshot_values != NULL )
118
0
  {
119
0
    memory_free(
120
0
     *snapshot_values );
121
122
0
    *snapshot_values = NULL;
123
0
  }
124
0
  return( -1 );
125
0
}
126
127
/* Frees snapshot values
128
 * Returns 1 if successful or -1 on error
129
 */
130
int libphdi_snapshot_values_free(
131
     libphdi_snapshot_values_t **snapshot_values,
132
     libcerror_error_t **error )
133
0
{
134
0
  static char *function = "libphdi_snapshot_values_free";
135
0
  int result            = 1;
136
137
0
  if( snapshot_values == NULL )
138
0
  {
139
0
    libcerror_error_set(
140
0
     error,
141
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
142
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
143
0
     "%s: invalid snapshot values.",
144
0
     function );
145
146
0
    return( -1 );
147
0
  }
148
0
  if( *snapshot_values != NULL )
149
0
  {
150
0
    if( libphdi_extent_table_free(
151
0
         &( ( *snapshot_values )->extent_table ),
152
0
         error ) != 1 )
153
0
    {
154
0
      libcerror_error_set(
155
0
       error,
156
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
157
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
158
0
       "%s: unable to free extent table.",
159
0
       function );
160
161
0
      result = -1;
162
0
    }
163
0
    memory_free(
164
0
     *snapshot_values );
165
166
0
    *snapshot_values = NULL;
167
0
  }
168
0
  return( result );
169
0
}
170
171
/* Sets the identifier
172
 * Returns 1 if successful or -1 on error
173
 */
174
int libphdi_snapshot_values_set_identifier(
175
     libphdi_snapshot_values_t *snapshot_values,
176
     const uint8_t *utf8_string,
177
     size_t utf8_string_length,
178
     libcerror_error_t **error )
179
0
{
180
0
  static char *function = "libphdi_snapshot_values_set_identifier";
181
182
0
  if( snapshot_values == 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 snapshot values.",
189
0
     function );
190
191
0
    return( -1 );
192
0
  }
193
0
  if( libphdi_uuid_string_copy_to_byte_stream(
194
0
       utf8_string,
195
0
       utf8_string_length,
196
0
       snapshot_values->identifier,
197
0
       16,
198
0
       error ) != 1 )
199
0
  {
200
0
    libcerror_error_set(
201
0
     error,
202
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
203
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
204
0
     "%s: unable to copy UUID string to identifier.",
205
0
     function );
206
207
0
    return( -1 );
208
0
  }
209
0
  return( 1 );
210
0
}
211
212
/* Sets the parent identifier
213
 * Returns 1 if successful or -1 on error
214
 */
215
int libphdi_snapshot_values_set_parent_identifier(
216
     libphdi_snapshot_values_t *snapshot_values,
217
     const uint8_t *utf8_string,
218
     size_t utf8_string_length,
219
     libcerror_error_t **error )
220
0
{
221
0
  static char *function = "libphdi_snapshot_values_set_parent_identifier";
222
223
0
  if( snapshot_values == NULL )
224
0
  {
225
0
    libcerror_error_set(
226
0
     error,
227
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
228
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
229
0
     "%s: invalid snapshot values.",
230
0
     function );
231
232
0
    return( -1 );
233
0
  }
234
0
  if( libphdi_uuid_string_copy_to_byte_stream(
235
0
       utf8_string,
236
0
       utf8_string_length,
237
0
       snapshot_values->parent_identifier,
238
0
       16,
239
0
       error ) != 1 )
240
0
  {
241
0
    libcerror_error_set(
242
0
     error,
243
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
244
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
245
0
     "%s: unable to copy UUID string to parent identifier.",
246
0
     function );
247
248
0
    return( -1 );
249
0
  }
250
0
  return( 1 );
251
0
}
252
253
/* Retrieves the identifier
254
 * The identifier is a big-endian GUID and is 16 bytes of size
255
 * Returns 1 if successful or -1 on error
256
 */
257
int libphdi_snapshot_values_get_identifier(
258
     libphdi_snapshot_values_t *snapshot_values,
259
     uint8_t *guid_data,
260
     size_t guid_data_size,
261
     libcerror_error_t **error )
262
0
{
263
0
  static char *function = "libphdi_snapshot_values_get_identifier";
264
265
0
  if( snapshot_values == NULL )
266
0
  {
267
0
    libcerror_error_set(
268
0
     error,
269
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
270
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
271
0
     "%s: invalid snapshot values.",
272
0
     function );
273
274
0
    return( -1 );
275
0
  }
276
0
  if( guid_data == NULL )
277
0
  {
278
0
    libcerror_error_set(
279
0
     error,
280
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
281
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
282
0
     "%s: invalid GUID data.",
283
0
     function );
284
285
0
    return( -1 );
286
0
  }
287
0
  if( ( guid_data_size < 16 )
288
0
   || ( guid_data_size > SSIZE_MAX ) )
289
0
  {
290
0
    libcerror_error_set(
291
0
     error,
292
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
293
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
294
0
     "%s: invalid GUID data size value out of bounds.",
295
0
     function );
296
297
0
    return( -1 );
298
0
  }
299
0
  if( memory_copy(
300
0
       guid_data,
301
0
       snapshot_values->identifier,
302
0
       16 ) == NULL )
303
0
  {
304
0
    libcerror_error_set(
305
0
     error,
306
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
307
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
308
0
     "%s: unable to copy identifier.",
309
0
     function );
310
311
0
    return( -1 );
312
0
  }
313
0
  return( 1 );
314
0
}
315
316
/* Retrieves the parent identifier
317
 * The identifier is a big-endian GUID and is 16 bytes of size
318
 * Returns 1 if successful, 0 if not available or -1 on error
319
 */
320
int libphdi_snapshot_values_get_parent_identifier(
321
     libphdi_snapshot_values_t *snapshot_values,
322
     uint8_t *guid_data,
323
     size_t guid_data_size,
324
     libcerror_error_t **error )
325
0
{
326
0
  static char *function = "libphdi_snapshot_values_get_parent_identifier";
327
328
0
  if( snapshot_values == NULL )
329
0
  {
330
0
    libcerror_error_set(
331
0
     error,
332
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
333
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
334
0
     "%s: invalid snapshot values.",
335
0
     function );
336
337
0
    return( -1 );
338
0
  }
339
0
  if( guid_data == NULL )
340
0
  {
341
0
    libcerror_error_set(
342
0
     error,
343
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
344
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
345
0
     "%s: invalid GUID data.",
346
0
     function );
347
348
0
    return( -1 );
349
0
  }
350
0
  if( ( guid_data_size < 16 )
351
0
   || ( guid_data_size > SSIZE_MAX ) )
352
0
  {
353
0
    libcerror_error_set(
354
0
     error,
355
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
356
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
357
0
     "%s: invalid GUID data size value out of bounds.",
358
0
     function );
359
360
0
    return( -1 );
361
0
  }
362
0
  if( memory_compare(
363
0
       snapshot_values->parent_identifier,
364
0
       libphdi_snapshot_values_empty_identifier,
365
0
       16 ) == 0 )
366
0
  {
367
0
    return( 0 );
368
0
  }
369
0
  if( memory_copy(
370
0
       guid_data,
371
0
       snapshot_values->parent_identifier,
372
0
       16 ) == NULL )
373
0
  {
374
0
    libcerror_error_set(
375
0
     error,
376
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
377
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
378
0
     "%s: unable to copy parent identifier.",
379
0
     function );
380
381
0
    return( -1 );
382
0
  }
383
0
  return( 1 );
384
0
}
385