/src/libfvde/libfvde/libfvde_physical_volume_descriptor.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Physical volume descriptor functions |
3 | | * |
4 | | * Copyright (C) 2011-2024, Omar Choudary <choudary.omar@gmail.com> |
5 | | * Joachim Metz <joachim.metz@gmail.com> |
6 | | * |
7 | | * Refer to AUTHORS for acknowledgements. |
8 | | * |
9 | | * This program is free software: you can redistribute it and/or modify |
10 | | * it under the terms of the GNU Lesser General Public License as published by |
11 | | * the Free Software Foundation, either version 3 of the License, or |
12 | | * (at your option) any later version. |
13 | | * |
14 | | * This program is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
21 | | */ |
22 | | |
23 | | #include <common.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libfvde_libcerror.h" |
28 | | #include "libfvde_physical_volume_descriptor.h" |
29 | | |
30 | | /* Creates physical volume descriptor |
31 | | * Make sure the value physical_volume_descriptor is referencing, is set to NULL |
32 | | * Returns 1 if successful or -1 on error |
33 | | */ |
34 | | int libfvde_physical_volume_descriptor_initialize( |
35 | | libfvde_physical_volume_descriptor_t **physical_volume_descriptor, |
36 | | libcerror_error_t **error ) |
37 | 16.6k | { |
38 | 16.6k | static char *function = "libfvde_physical_volume_descriptor_initialize"; |
39 | | |
40 | 16.6k | if( physical_volume_descriptor == NULL ) |
41 | 0 | { |
42 | 0 | libcerror_error_set( |
43 | 0 | error, |
44 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
45 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
46 | 0 | "%s: invalid physical volume descriptor.", |
47 | 0 | function ); |
48 | |
|
49 | 0 | return( -1 ); |
50 | 0 | } |
51 | 16.6k | if( *physical_volume_descriptor != NULL ) |
52 | 0 | { |
53 | 0 | libcerror_error_set( |
54 | 0 | error, |
55 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
56 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
57 | 0 | "%s: invalid physical volume descriptor value already set.", |
58 | 0 | function ); |
59 | |
|
60 | 0 | return( -1 ); |
61 | 0 | } |
62 | 16.6k | *physical_volume_descriptor = memory_allocate_structure( |
63 | 16.6k | libfvde_physical_volume_descriptor_t ); |
64 | | |
65 | 16.6k | if( *physical_volume_descriptor == NULL ) |
66 | 0 | { |
67 | 0 | libcerror_error_set( |
68 | 0 | error, |
69 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
70 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
71 | 0 | "%s: unable to physical volume descriptor.", |
72 | 0 | function ); |
73 | |
|
74 | 0 | goto on_error; |
75 | 0 | } |
76 | 16.6k | if( memory_set( |
77 | 16.6k | *physical_volume_descriptor, |
78 | 16.6k | 0, |
79 | 16.6k | sizeof( libfvde_physical_volume_descriptor_t ) ) == NULL ) |
80 | 0 | { |
81 | 0 | libcerror_error_set( |
82 | 0 | error, |
83 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
84 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
85 | 0 | "%s: unable to clear physical volume descriptor.", |
86 | 0 | function ); |
87 | |
|
88 | 0 | memory_free( |
89 | 0 | *physical_volume_descriptor ); |
90 | |
|
91 | 0 | *physical_volume_descriptor = NULL; |
92 | |
|
93 | 0 | return( -1 ); |
94 | 0 | } |
95 | 16.6k | return( 1 ); |
96 | | |
97 | 0 | on_error: |
98 | 0 | if( *physical_volume_descriptor != NULL ) |
99 | 0 | { |
100 | 0 | memory_free( |
101 | 0 | *physical_volume_descriptor ); |
102 | |
|
103 | 0 | *physical_volume_descriptor = NULL; |
104 | 0 | } |
105 | 0 | return( -1 ); |
106 | 16.6k | } |
107 | | |
108 | | /* Frees physical volume descriptor |
109 | | * Returns 1 if successful or -1 on error |
110 | | */ |
111 | | int libfvde_physical_volume_descriptor_free( |
112 | | libfvde_physical_volume_descriptor_t **physical_volume_descriptor, |
113 | | libcerror_error_t **error ) |
114 | 16.6k | { |
115 | 16.6k | static char *function = "libfvde_physical_volume_descriptor_free"; |
116 | | |
117 | 16.6k | if( physical_volume_descriptor == 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 physical volume descriptor.", |
124 | 0 | function ); |
125 | |
|
126 | 0 | return( -1 ); |
127 | 0 | } |
128 | 16.6k | if( *physical_volume_descriptor != NULL ) |
129 | 16.6k | { |
130 | 16.6k | memory_free( |
131 | 16.6k | *physical_volume_descriptor ); |
132 | | |
133 | 16.6k | *physical_volume_descriptor = NULL; |
134 | 16.6k | } |
135 | 16.6k | return( 1 ); |
136 | 16.6k | } |
137 | | |
138 | | /* Retrieves the identifier |
139 | | * The identifier is a UUID and is 16 bytes of size |
140 | | * Returns 1 if successful or -1 on error |
141 | | */ |
142 | | int libfvde_physical_volume_descriptor_get_identifier( |
143 | | libfvde_physical_volume_descriptor_t *physical_volume_descriptor, |
144 | | uint8_t *uuid_data, |
145 | | size_t uuid_data_size, |
146 | | libcerror_error_t **error ) |
147 | 0 | { |
148 | 0 | static char *function = "libfvde_physical_volume_descriptor_get_identifier"; |
149 | |
|
150 | 0 | if( physical_volume_descriptor == NULL ) |
151 | 0 | { |
152 | 0 | libcerror_error_set( |
153 | 0 | error, |
154 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
155 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
156 | 0 | "%s: invalid physical volume descriptor.", |
157 | 0 | function ); |
158 | |
|
159 | 0 | return( -1 ); |
160 | 0 | } |
161 | 0 | if( uuid_data == NULL ) |
162 | 0 | { |
163 | 0 | libcerror_error_set( |
164 | 0 | error, |
165 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
166 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
167 | 0 | "%s: invalid UUID data.", |
168 | 0 | function ); |
169 | |
|
170 | 0 | return( -1 ); |
171 | 0 | } |
172 | 0 | if( ( uuid_data_size < 16 ) |
173 | 0 | || ( uuid_data_size > (size_t) SSIZE_MAX ) ) |
174 | 0 | { |
175 | 0 | libcerror_error_set( |
176 | 0 | error, |
177 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
178 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
179 | 0 | "%s: invalid UUID data size value out of bounds.", |
180 | 0 | function ); |
181 | |
|
182 | 0 | return( -1 ); |
183 | 0 | } |
184 | 0 | if( memory_copy( |
185 | 0 | uuid_data, |
186 | 0 | physical_volume_descriptor->identifier, |
187 | 0 | 16 ) == NULL ) |
188 | 0 | { |
189 | 0 | libcerror_error_set( |
190 | 0 | error, |
191 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
192 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
193 | 0 | "%s: unable to copy identifier.", |
194 | 0 | function ); |
195 | |
|
196 | 0 | return( -1 ); |
197 | 0 | } |
198 | 0 | return( 1 ); |
199 | 0 | } |
200 | | |