Coverage Report

Created: 2024-10-02 06:58

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