/src/libagdb/libagdb/libagdb_compressed_file_header.c
Line | Count | Source |
1 | | /* |
2 | | * Compressed file header functions |
3 | | * |
4 | | * Copyright (C) 2014-2026, 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 "libagdb_compressed_file_header.h" |
28 | | #include "libagdb_definitions.h" |
29 | | #include "libagdb_io_handle.h" |
30 | | #include "libagdb_libbfio.h" |
31 | | #include "libagdb_libcerror.h" |
32 | | #include "libagdb_libcnotify.h" |
33 | | |
34 | | /* Creates a compressed file header |
35 | | * Make sure the value compressed_file_header is referencing, is set to NULL |
36 | | * Returns 1 if successful or -1 on error |
37 | | */ |
38 | | int libagdb_compressed_file_header_initialize( |
39 | | libagdb_compressed_file_header_t **compressed_file_header, |
40 | | libcerror_error_t **error ) |
41 | 2.33k | { |
42 | 2.33k | static char *function = "libagdb_compressed_file_header_initialize"; |
43 | | |
44 | 2.33k | if( compressed_file_header == NULL ) |
45 | 0 | { |
46 | 0 | libcerror_error_set( |
47 | 0 | error, |
48 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
49 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
50 | 0 | "%s: invalid compressed file header.", |
51 | 0 | function ); |
52 | |
|
53 | 0 | return( -1 ); |
54 | 0 | } |
55 | 2.33k | if( *compressed_file_header != NULL ) |
56 | 0 | { |
57 | 0 | libcerror_error_set( |
58 | 0 | error, |
59 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
60 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
61 | 0 | "%s: invalid compressed file header value already set.", |
62 | 0 | function ); |
63 | |
|
64 | 0 | return( -1 ); |
65 | 0 | } |
66 | 2.33k | *compressed_file_header = memory_allocate_structure( |
67 | 2.33k | libagdb_compressed_file_header_t ); |
68 | | |
69 | 2.33k | if( *compressed_file_header == NULL ) |
70 | 0 | { |
71 | 0 | libcerror_error_set( |
72 | 0 | error, |
73 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
74 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
75 | 0 | "%s: unable to create compressed file header.", |
76 | 0 | function ); |
77 | |
|
78 | 0 | goto on_error; |
79 | 0 | } |
80 | 2.33k | if( memory_set( |
81 | 2.33k | *compressed_file_header, |
82 | 2.33k | 0, |
83 | 2.33k | sizeof( libagdb_compressed_file_header_t ) ) == NULL ) |
84 | 0 | { |
85 | 0 | libcerror_error_set( |
86 | 0 | error, |
87 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
88 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
89 | 0 | "%s: unable to clear compressed file header.", |
90 | 0 | function ); |
91 | |
|
92 | 0 | goto on_error; |
93 | 0 | } |
94 | 2.33k | return( 1 ); |
95 | | |
96 | 0 | on_error: |
97 | 0 | if( *compressed_file_header != NULL ) |
98 | 0 | { |
99 | 0 | memory_free( |
100 | 0 | *compressed_file_header ); |
101 | |
|
102 | 0 | *compressed_file_header = NULL; |
103 | 0 | } |
104 | 0 | return( -1 ); |
105 | 2.33k | } |
106 | | |
107 | | /* Frees a compressed file header |
108 | | * Returns 1 if successful or -1 on error |
109 | | */ |
110 | | int libagdb_compressed_file_header_free( |
111 | | libagdb_compressed_file_header_t **compressed_file_header, |
112 | | libcerror_error_t **error ) |
113 | 2.33k | { |
114 | 2.33k | static char *function = "libagdb_compressed_file_header_free"; |
115 | | |
116 | 2.33k | if( compressed_file_header == NULL ) |
117 | 0 | { |
118 | 0 | libcerror_error_set( |
119 | 0 | error, |
120 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
121 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
122 | 0 | "%s: invalid compressed file header.", |
123 | 0 | function ); |
124 | |
|
125 | 0 | return( -1 ); |
126 | 0 | } |
127 | 2.33k | if( *compressed_file_header != NULL ) |
128 | 2.33k | { |
129 | 2.33k | memory_free( |
130 | 2.33k | *compressed_file_header ); |
131 | | |
132 | 2.33k | *compressed_file_header = NULL; |
133 | 2.33k | } |
134 | 2.33k | return( 1 ); |
135 | 2.33k | } |
136 | | |
137 | | /* Reads the compressed file header data |
138 | | * Returns 1 if successful or -1 on error |
139 | | */ |
140 | | int libagdb_compressed_file_header_read_data( |
141 | | libagdb_compressed_file_header_t *compressed_file_header, |
142 | | const uint8_t *data, |
143 | | size_t data_size, |
144 | | libcerror_error_t **error ) |
145 | 2.32k | { |
146 | 2.32k | static char *function = "libagdb_compressed_file_header_read_data"; |
147 | 2.32k | uint32_t uncompressed_data_size = 0; |
148 | 2.32k | uint32_t value_32bit = 0; |
149 | | |
150 | | #if defined( HAVE_DEBUG_OUTPUT ) |
151 | | size_t file_header_size = 0; |
152 | | #endif |
153 | | |
154 | 2.32k | if( compressed_file_header == NULL ) |
155 | 0 | { |
156 | 0 | libcerror_error_set( |
157 | 0 | error, |
158 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
159 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
160 | 0 | "%s: invalid compressed file header.", |
161 | 0 | function ); |
162 | |
|
163 | 0 | return( -1 ); |
164 | 0 | } |
165 | 2.32k | if( data == NULL ) |
166 | 0 | { |
167 | 0 | libcerror_error_set( |
168 | 0 | error, |
169 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
170 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
171 | 0 | "%s: invalid data.", |
172 | 0 | function ); |
173 | |
|
174 | 0 | return( -1 ); |
175 | 0 | } |
176 | 2.32k | if( ( data_size < 8 ) |
177 | 2.32k | || ( data_size > (size_t) SSIZE_MAX ) ) |
178 | 0 | { |
179 | 0 | libcerror_error_set( |
180 | 0 | error, |
181 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
182 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
183 | 0 | "%s: invalid data size value out of bounds.", |
184 | 0 | function ); |
185 | |
|
186 | 0 | return( -1 ); |
187 | 0 | } |
188 | 2.32k | if( memory_compare( |
189 | 2.32k | data, |
190 | 2.32k | agdb_mem_file_signature_vista, |
191 | 2.32k | 4 ) == 0 ) |
192 | 451 | { |
193 | 451 | compressed_file_header->file_type = LIBAGDB_FILE_TYPE_COMPRESSED_VISTA; |
194 | 451 | compressed_file_header->uncompressed_block_size = 4096; |
195 | | |
196 | | #if defined( HAVE_DEBUG_OUTPUT ) |
197 | | file_header_size = 8; |
198 | | #endif |
199 | 451 | } |
200 | 1.87k | else if( memory_compare( |
201 | 1.87k | data, |
202 | 1.87k | agdb_mem_file_signature_win7, |
203 | 1.87k | 4 ) == 0 ) |
204 | 428 | { |
205 | 428 | compressed_file_header->file_type = LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS7; |
206 | 428 | compressed_file_header->uncompressed_block_size = 65536; |
207 | | |
208 | | #if defined( HAVE_DEBUG_OUTPUT ) |
209 | | file_header_size = 8; |
210 | | #endif |
211 | 428 | } |
212 | 1.45k | else if( memory_compare( |
213 | 1.45k | data, |
214 | 1.45k | agdb_mem_file_signature_win8_0, |
215 | 1.45k | 4 ) == 0 ) |
216 | 11 | { |
217 | 11 | compressed_file_header->file_type = LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8_0; |
218 | 11 | compressed_file_header->uncompressed_block_size = 65536; |
219 | | |
220 | | #if defined( HAVE_DEBUG_OUTPUT ) |
221 | | file_header_size = 12; |
222 | | #endif |
223 | 11 | } |
224 | 1.43k | else if( memory_compare( |
225 | 1.43k | data, |
226 | 1.43k | agdb_mam_file_signature_win8_1, |
227 | 1.43k | 4 ) == 0 ) |
228 | 170 | { |
229 | 170 | compressed_file_header->file_type = LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8_1; |
230 | 170 | compressed_file_header->uncompressed_block_size = 1441792; |
231 | | |
232 | | #if defined( HAVE_DEBUG_OUTPUT ) |
233 | | file_header_size = 12; |
234 | | #endif |
235 | 170 | } |
236 | 1.26k | else |
237 | 1.26k | { |
238 | 1.26k | compressed_file_header->file_type = LIBAGDB_FILE_TYPE_UNCOMPRESSED; |
239 | | |
240 | | #if defined( HAVE_DEBUG_OUTPUT ) |
241 | | file_header_size = 12; |
242 | | #endif |
243 | 1.26k | } |
244 | | #if defined( HAVE_DEBUG_OUTPUT ) |
245 | | if( libcnotify_verbose != 0 ) |
246 | | { |
247 | | libcnotify_printf( |
248 | | "%s: compressed file header data:\n", |
249 | | function ); |
250 | | libcnotify_print_data( |
251 | | data, |
252 | | file_header_size, |
253 | | 0 ); |
254 | | } |
255 | | #endif |
256 | | #if defined( HAVE_DEBUG_OUTPUT ) |
257 | | if( libcnotify_verbose != 0 ) |
258 | | { |
259 | | if( ( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_VISTA ) |
260 | | || ( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS7 ) ) |
261 | | { |
262 | | libcnotify_printf( |
263 | | "%s: signature\t\t\t: %c%c%c%c\n", |
264 | | function, |
265 | | data[ 0 ], |
266 | | data[ 1 ], |
267 | | data[ 2 ], |
268 | | data[ 3 ] ); |
269 | | } |
270 | | else if( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8_0 ) |
271 | | { |
272 | | libcnotify_printf( |
273 | | "%s: signature\t\t\t: %c%c%c\\x%02x\n", |
274 | | function, |
275 | | data[ 0 ], |
276 | | data[ 1 ], |
277 | | data[ 2 ], |
278 | | data[ 3 ] ); |
279 | | } |
280 | | else if( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8_1 ) |
281 | | { |
282 | | libcnotify_printf( |
283 | | "%s: signature\t\t\t: %c%c%c\\x%02x\n", |
284 | | function, |
285 | | data[ 0 ], |
286 | | data[ 1 ], |
287 | | data[ 2 ], |
288 | | data[ 3 ] ); |
289 | | } |
290 | | } |
291 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
292 | | |
293 | 2.32k | byte_stream_copy_to_uint32_little_endian( |
294 | 2.32k | &( data[ 4 ] ), |
295 | 2.32k | uncompressed_data_size ); |
296 | | |
297 | 2.32k | if( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_UNCOMPRESSED ) |
298 | 1.26k | { |
299 | 1.26k | byte_stream_copy_to_uint32_little_endian( |
300 | 1.26k | &( data[ 0 ] ), |
301 | 1.26k | value_32bit ); |
302 | | |
303 | | /* TODO improve detection */ |
304 | 1.26k | if( ( value_32bit != 0x00000005UL ) |
305 | 1.26k | && ( value_32bit != 0x0000000eUL ) |
306 | 251 | && ( value_32bit != 0x0000000fUL ) ) |
307 | 108 | { |
308 | 108 | if( compressed_file_header->file_size != (size64_t) uncompressed_data_size ) |
309 | 55 | { |
310 | 55 | libcerror_error_set( |
311 | 55 | error, |
312 | 55 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
313 | 55 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
314 | 55 | "%s: invalid signature.", |
315 | 55 | function ); |
316 | | |
317 | 55 | return( -1 ); |
318 | 55 | } |
319 | 108 | } |
320 | 1.26k | } |
321 | 2.27k | compressed_file_header->uncompressed_data_size = uncompressed_data_size; |
322 | | |
323 | | #if defined( HAVE_DEBUG_OUTPUT ) |
324 | | if( libcnotify_verbose != 0 ) |
325 | | { |
326 | | libcnotify_printf( |
327 | | "%s: uncompressed data size\t: %" PRIu32 "\n", |
328 | | function, |
329 | | compressed_file_header->uncompressed_data_size ); |
330 | | } |
331 | | #endif |
332 | 2.27k | if( ( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8_0 ) |
333 | 2.26k | || ( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8_1 ) ) |
334 | 181 | { |
335 | 181 | if( data_size < 12 ) |
336 | 0 | { |
337 | 0 | libcerror_error_set( |
338 | 0 | error, |
339 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
340 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
341 | 0 | "%s: invalid data size value out of bounds.", |
342 | 0 | function ); |
343 | |
|
344 | 0 | return( -1 ); |
345 | 0 | } |
346 | | #if defined( HAVE_DEBUG_OUTPUT ) |
347 | | if( libcnotify_verbose != 0 ) |
348 | | { |
349 | | byte_stream_copy_to_uint32_little_endian( |
350 | | &( data[ 8 ] ), |
351 | | value_32bit ); |
352 | | libcnotify_printf( |
353 | | "%s: checksum\t\t\t: 0x%08" PRIx32 "\n", |
354 | | function, |
355 | | value_32bit ); |
356 | | } |
357 | | #endif |
358 | 181 | } |
359 | | #if defined( HAVE_DEBUG_OUTPUT ) |
360 | | if( libcnotify_verbose != 0 ) |
361 | | { |
362 | | libcnotify_printf( |
363 | | "\n" ); |
364 | | } |
365 | | #endif |
366 | 2.27k | return( 1 ); |
367 | 2.27k | } |
368 | | |
369 | | /* Reads the compressed file header data |
370 | | * Returns 1 if successful or -1 on error |
371 | | */ |
372 | | int libagdb_compressed_file_header_read_file_io_handle( |
373 | | libagdb_compressed_file_header_t *compressed_file_header, |
374 | | libbfio_handle_t *file_io_handle, |
375 | | libcerror_error_t **error ) |
376 | 2.33k | { |
377 | 2.33k | uint8_t compressed_file_header_data[ 12 ]; |
378 | | |
379 | 2.33k | static char *function = "libagdb_compressed_file_header_read_file_io_handle"; |
380 | 2.33k | ssize_t read_count = 0; |
381 | | |
382 | 2.33k | if( compressed_file_header == NULL ) |
383 | 0 | { |
384 | 0 | libcerror_error_set( |
385 | 0 | error, |
386 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
387 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
388 | 0 | "%s: invalid compressed file header.", |
389 | 0 | function ); |
390 | |
|
391 | 0 | return( -1 ); |
392 | 0 | } |
393 | 2.33k | if( libbfio_handle_get_size( |
394 | 2.33k | file_io_handle, |
395 | 2.33k | &( compressed_file_header->file_size ), |
396 | 2.33k | error ) != 1 ) |
397 | 0 | { |
398 | 0 | libcerror_error_set( |
399 | 0 | error, |
400 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
401 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
402 | 0 | "%s: unable to retrieve file size.", |
403 | 0 | function ); |
404 | |
|
405 | 0 | return( -1 ); |
406 | 0 | } |
407 | | #if defined( HAVE_DEBUG_OUTPUT ) |
408 | | if( libcnotify_verbose != 0 ) |
409 | | { |
410 | | libcnotify_printf( |
411 | | "%s: reading compressed file header at offset: 0 (0x00000000)\n", |
412 | | function ); |
413 | | } |
414 | | #endif |
415 | 2.33k | read_count = libbfio_handle_read_buffer_at_offset( |
416 | 2.33k | file_io_handle, |
417 | 2.33k | compressed_file_header_data, |
418 | 2.33k | 12, |
419 | 2.33k | 0, |
420 | 2.33k | error ); |
421 | | |
422 | 2.33k | if( read_count != (ssize_t) 12 ) |
423 | 7 | { |
424 | 7 | libcerror_error_set( |
425 | 7 | error, |
426 | 7 | LIBCERROR_ERROR_DOMAIN_IO, |
427 | 7 | LIBCERROR_IO_ERROR_READ_FAILED, |
428 | 7 | "%s: unable to read compressed file header data at offset: 0 (0x00000000).", |
429 | 7 | function ); |
430 | | |
431 | 7 | return( -1 ); |
432 | 7 | } |
433 | | /* TODO pass file_size as an argument ? */ |
434 | 2.32k | if( libagdb_compressed_file_header_read_data( |
435 | 2.32k | compressed_file_header, |
436 | 2.32k | compressed_file_header_data, |
437 | 2.32k | 12, |
438 | 2.32k | error ) != 1 ) |
439 | 55 | { |
440 | 55 | libcerror_error_set( |
441 | 55 | error, |
442 | 55 | LIBCERROR_ERROR_DOMAIN_IO, |
443 | 55 | LIBCERROR_IO_ERROR_READ_FAILED, |
444 | 55 | "%s: unable to read compressed file header.", |
445 | 55 | function ); |
446 | | |
447 | 55 | return( -1 ); |
448 | 55 | } |
449 | 2.27k | return( 1 ); |
450 | 2.32k | } |
451 | | |