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