/src/libvhdi/libvhdi/libvhdi_region_table_entry.c
Line | Count | Source |
1 | | /* |
2 | | * Region table entry functions |
3 | | * |
4 | | * Copyright (C) 2012-2025, 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 "libvhdi_debug.h" |
28 | | #include "libvhdi_libcerror.h" |
29 | | #include "libvhdi_libcnotify.h" |
30 | | #include "libvhdi_libfguid.h" |
31 | | #include "libvhdi_region_table_entry.h" |
32 | | #include "libvhdi_region_type_identifier.h" |
33 | | |
34 | | #include "vhdi_region_table.h" |
35 | | |
36 | | /* Creates region table entry |
37 | | * Make sure the value region_table_entry is referencing, is set to NULL |
38 | | * Returns 1 if successful or -1 on error |
39 | | */ |
40 | | int libvhdi_region_table_entry_initialize( |
41 | | libvhdi_region_table_entry_t **region_table_entry, |
42 | | libcerror_error_t **error ) |
43 | 1.99M | { |
44 | 1.99M | static char *function = "libvhdi_region_table_entry_initialize"; |
45 | | |
46 | 1.99M | if( region_table_entry == 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 region table entry.", |
53 | 0 | function ); |
54 | |
|
55 | 0 | return( -1 ); |
56 | 0 | } |
57 | 1.99M | if( *region_table_entry != 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 region table entry value already set.", |
64 | 0 | function ); |
65 | |
|
66 | 0 | return( -1 ); |
67 | 0 | } |
68 | 1.99M | *region_table_entry = memory_allocate_structure( |
69 | 1.99M | libvhdi_region_table_entry_t ); |
70 | | |
71 | 1.99M | if( *region_table_entry == 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 region table entry.", |
78 | 0 | function ); |
79 | |
|
80 | 0 | goto on_error; |
81 | 0 | } |
82 | 1.99M | if( memory_set( |
83 | 1.99M | *region_table_entry, |
84 | 1.99M | 0, |
85 | 1.99M | sizeof( libvhdi_region_table_entry_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 region table entry.", |
92 | 0 | function ); |
93 | |
|
94 | 0 | goto on_error; |
95 | 0 | } |
96 | 1.99M | return( 1 ); |
97 | | |
98 | 0 | on_error: |
99 | 0 | if( *region_table_entry != NULL ) |
100 | 0 | { |
101 | 0 | memory_free( |
102 | 0 | *region_table_entry ); |
103 | |
|
104 | 0 | *region_table_entry = NULL; |
105 | 0 | } |
106 | 0 | return( -1 ); |
107 | 1.99M | } |
108 | | |
109 | | /* Frees region table entry |
110 | | * Returns 1 if successful or -1 on error |
111 | | */ |
112 | | int libvhdi_region_table_entry_free( |
113 | | libvhdi_region_table_entry_t **region_table_entry, |
114 | | libcerror_error_t **error ) |
115 | 1.99M | { |
116 | 1.99M | static char *function = "libvhdi_region_table_entry_free"; |
117 | | |
118 | 1.99M | if( region_table_entry == NULL ) |
119 | 0 | { |
120 | 0 | libcerror_error_set( |
121 | 0 | error, |
122 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
123 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
124 | 0 | "%s: invalid region table entry.", |
125 | 0 | function ); |
126 | |
|
127 | 0 | return( -1 ); |
128 | 0 | } |
129 | 1.99M | if( *region_table_entry != NULL ) |
130 | 1.99M | { |
131 | 1.99M | memory_free( |
132 | 1.99M | *region_table_entry ); |
133 | | |
134 | 1.99M | *region_table_entry = NULL; |
135 | 1.99M | } |
136 | 1.99M | return( 1 ); |
137 | 1.99M | } |
138 | | |
139 | | /* Reads the region table entry data |
140 | | * Returns 1 if successful or -1 on error |
141 | | */ |
142 | | int libvhdi_region_table_entry_read_data( |
143 | | libvhdi_region_table_entry_t *region_table_entry, |
144 | | const uint8_t *data, |
145 | | size_t data_size, |
146 | | libcerror_error_t **error ) |
147 | 1.99M | { |
148 | 1.99M | static char *function = "libvhdi_region_table_entry_read_data"; |
149 | | |
150 | 1.99M | if( region_table_entry == 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 region table entry.", |
157 | 0 | function ); |
158 | |
|
159 | 0 | return( -1 ); |
160 | 0 | } |
161 | 1.99M | if( 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 data.", |
168 | 0 | function ); |
169 | |
|
170 | 0 | return( -1 ); |
171 | 0 | } |
172 | 1.99M | if( ( data_size < sizeof( vhdi_region_table_entry_t ) ) |
173 | 1.99M | || ( 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 data size value out of bounds.", |
180 | 0 | function ); |
181 | |
|
182 | 0 | return( -1 ); |
183 | 0 | } |
184 | | #if defined( HAVE_DEBUG_OUTPUT ) |
185 | | if( libcnotify_verbose != 0 ) |
186 | | { |
187 | | libcnotify_printf( |
188 | | "%s: region table entry data:\n", |
189 | | function ); |
190 | | libcnotify_print_data( |
191 | | data, |
192 | | sizeof( vhdi_region_table_entry_t ), |
193 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
194 | | } |
195 | | #endif |
196 | 1.99M | if( memory_copy( |
197 | 1.99M | region_table_entry->type_identifier, |
198 | 1.99M | ( (vhdi_region_table_entry_t *) data )->type_identifier, |
199 | 1.99M | 16 ) == NULL ) |
200 | 0 | { |
201 | 0 | libcerror_error_set( |
202 | 0 | error, |
203 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
204 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
205 | 0 | "%s: unable to copy type identifier.", |
206 | 0 | function ); |
207 | |
|
208 | 0 | return( -1 ); |
209 | 0 | } |
210 | 1.99M | byte_stream_copy_to_uint64_little_endian( |
211 | 1.99M | ( (vhdi_region_table_entry_t *) data )->data_offset, |
212 | 1.99M | region_table_entry->data_offset ); |
213 | | |
214 | 1.99M | byte_stream_copy_to_uint32_little_endian( |
215 | 1.99M | ( (vhdi_region_table_entry_t *) data )->data_size, |
216 | 1.99M | region_table_entry->data_size ); |
217 | | |
218 | 1.99M | byte_stream_copy_to_uint32_little_endian( |
219 | 1.99M | ( (vhdi_region_table_entry_t *) data )->is_required_flag, |
220 | 1.99M | region_table_entry->is_required_flag ); |
221 | | |
222 | | #if defined( HAVE_DEBUG_OUTPUT ) |
223 | | if( libcnotify_verbose != 0 ) |
224 | | { |
225 | | if( libvhdi_debug_print_guid_value( |
226 | | function, |
227 | | "type identifier\t\t\t", |
228 | | ( (vhdi_region_table_entry_t *) data )->type_identifier, |
229 | | 16, |
230 | | LIBFGUID_ENDIAN_LITTLE, |
231 | | LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE, |
232 | | error ) != 1 ) |
233 | | { |
234 | | libcerror_error_set( |
235 | | error, |
236 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
237 | | LIBCERROR_RUNTIME_ERROR_PRINT_FAILED, |
238 | | "%s: unable to print GUID value.", |
239 | | function ); |
240 | | |
241 | | return( -1 ); |
242 | | } |
243 | | libcnotify_printf( |
244 | | "%s: type description\t\t\t: %s\n", |
245 | | function, |
246 | | libvhdi_region_type_identifier_get_description( |
247 | | ( (vhdi_region_table_entry_t *) data )->type_identifier ) ); |
248 | | |
249 | | libcnotify_printf( |
250 | | "%s: data offset\t\t\t: 0x%08" PRIx64 "\n", |
251 | | function, |
252 | | region_table_entry->data_offset ); |
253 | | |
254 | | libcnotify_printf( |
255 | | "%s: data size\t\t\t\t: %" PRIu32 "\n", |
256 | | function, |
257 | | region_table_entry->data_size ); |
258 | | |
259 | | libcnotify_printf( |
260 | | "%s: is required flag\t\t\t: %" PRIu32 "\n", |
261 | | function, |
262 | | region_table_entry->is_required_flag ); |
263 | | |
264 | | libcnotify_printf( |
265 | | "\n" ); |
266 | | } |
267 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
268 | | |
269 | 1.99M | return( 1 ); |
270 | 1.99M | } |
271 | | |