/src/libfsxfs/libfsxfs/libfsxfs_directory_table.c
Line | Count | Source |
1 | | /* |
2 | | * Short-form directory table functions |
3 | | * |
4 | | * Copyright (C) 2020-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 "libfsxfs_debug.h" |
28 | | #include "libfsxfs_definitions.h" |
29 | | #include "libfsxfs_directory_entry.h" |
30 | | #include "libfsxfs_directory_table.h" |
31 | | #include "libfsxfs_directory_table_header.h" |
32 | | #include "libfsxfs_io_handle.h" |
33 | | #include "libfsxfs_libbfio.h" |
34 | | #include "libfsxfs_libcdata.h" |
35 | | #include "libfsxfs_libcerror.h" |
36 | | #include "libfsxfs_libcnotify.h" |
37 | | |
38 | | /* Creates directory table |
39 | | * Make sure the value directory_table is referencing, is set to NULL |
40 | | * Returns 1 if successful or -1 on error |
41 | | */ |
42 | | int libfsxfs_directory_table_initialize( |
43 | | libfsxfs_directory_table_t **directory_table, |
44 | | libcerror_error_t **error ) |
45 | 652 | { |
46 | 652 | static char *function = "libfsxfs_directory_table_initialize"; |
47 | | |
48 | 652 | if( directory_table == NULL ) |
49 | 0 | { |
50 | 0 | libcerror_error_set( |
51 | 0 | error, |
52 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
53 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
54 | 0 | "%s: invalid directory table.", |
55 | 0 | function ); |
56 | |
|
57 | 0 | return( -1 ); |
58 | 0 | } |
59 | 652 | if( *directory_table != NULL ) |
60 | 0 | { |
61 | 0 | libcerror_error_set( |
62 | 0 | error, |
63 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
64 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
65 | 0 | "%s: invalid directory table value already set.", |
66 | 0 | function ); |
67 | |
|
68 | 0 | return( -1 ); |
69 | 0 | } |
70 | 652 | *directory_table = memory_allocate_structure( |
71 | 652 | libfsxfs_directory_table_t ); |
72 | | |
73 | 652 | if( *directory_table == NULL ) |
74 | 0 | { |
75 | 0 | libcerror_error_set( |
76 | 0 | error, |
77 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
78 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
79 | 0 | "%s: unable to create directory table.", |
80 | 0 | function ); |
81 | |
|
82 | 0 | goto on_error; |
83 | 0 | } |
84 | 652 | if( memory_set( |
85 | 652 | *directory_table, |
86 | 652 | 0, |
87 | 652 | sizeof( libfsxfs_directory_table_t ) ) == NULL ) |
88 | 0 | { |
89 | 0 | libcerror_error_set( |
90 | 0 | error, |
91 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
92 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
93 | 0 | "%s: unable to clear directory table.", |
94 | 0 | function ); |
95 | |
|
96 | 0 | goto on_error; |
97 | 0 | } |
98 | 652 | return( 1 ); |
99 | | |
100 | 0 | on_error: |
101 | 0 | if( *directory_table != NULL ) |
102 | 0 | { |
103 | 0 | memory_free( |
104 | 0 | *directory_table ); |
105 | |
|
106 | 0 | *directory_table = NULL; |
107 | 0 | } |
108 | 0 | return( -1 ); |
109 | 652 | } |
110 | | |
111 | | /* Frees directory table |
112 | | * Returns 1 if successful or -1 on error |
113 | | */ |
114 | | int libfsxfs_directory_table_free( |
115 | | libfsxfs_directory_table_t **directory_table, |
116 | | libcerror_error_t **error ) |
117 | 652 | { |
118 | 652 | static char *function = "libfsxfs_directory_table_free"; |
119 | 652 | int result = 1; |
120 | | |
121 | 652 | if( directory_table == NULL ) |
122 | 0 | { |
123 | 0 | libcerror_error_set( |
124 | 0 | error, |
125 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
126 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
127 | 0 | "%s: invalid directory table.", |
128 | 0 | function ); |
129 | |
|
130 | 0 | return( -1 ); |
131 | 0 | } |
132 | 652 | if( *directory_table != NULL ) |
133 | 652 | { |
134 | 652 | if( ( *directory_table )->header != NULL ) |
135 | 558 | { |
136 | 558 | if( libfsxfs_directory_table_header_free( |
137 | 558 | &( ( *directory_table )->header ), |
138 | 558 | error ) != 1 ) |
139 | 0 | { |
140 | 0 | libcerror_error_set( |
141 | 0 | error, |
142 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
143 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
144 | 0 | "%s: unable to free header.", |
145 | 0 | function ); |
146 | |
|
147 | 0 | result = -1; |
148 | 0 | } |
149 | 558 | } |
150 | 652 | memory_free( |
151 | 652 | *directory_table ); |
152 | | |
153 | 652 | *directory_table = NULL; |
154 | 652 | } |
155 | 652 | return( result ); |
156 | 652 | } |
157 | | |
158 | | /* Reads the directory table |
159 | | * Returns 1 if successful or -1 on error |
160 | | */ |
161 | | int libfsxfs_directory_table_read_data( |
162 | | libfsxfs_directory_table_t *directory_table, |
163 | | libfsxfs_io_handle_t *io_handle, |
164 | | const uint8_t *data, |
165 | | size_t data_size, |
166 | | libcdata_array_t *entries_array, |
167 | | libcerror_error_t **error ) |
168 | 652 | { |
169 | 652 | libfsxfs_directory_entry_t *directory_entry = NULL; |
170 | 652 | static char *function = "libfsxfs_directory_table_read_data"; |
171 | 652 | size_t data_offset = 0; |
172 | 652 | size_t name_offset = 0; |
173 | 652 | size_t entry_data_size = 0; |
174 | 652 | uint32_t directory_entry_index = 0; |
175 | 652 | uint8_t name_size = 0; |
176 | 652 | int entry_index = 0; |
177 | | |
178 | | #if defined( HAVE_DEBUG_OUTPUT ) |
179 | | uint16_t value_16bit = 0; |
180 | | #endif |
181 | | |
182 | 652 | if( directory_table == NULL ) |
183 | 0 | { |
184 | 0 | libcerror_error_set( |
185 | 0 | error, |
186 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
187 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
188 | 0 | "%s: invalid directory table.", |
189 | 0 | function ); |
190 | |
|
191 | 0 | return( -1 ); |
192 | 0 | } |
193 | 652 | if( directory_table->header != NULL ) |
194 | 0 | { |
195 | 0 | libcerror_error_set( |
196 | 0 | error, |
197 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
198 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
199 | 0 | "%s: invalid directory table - header already set.", |
200 | 0 | function ); |
201 | |
|
202 | 0 | return( -1 ); |
203 | 0 | } |
204 | 652 | if( io_handle == NULL ) |
205 | 0 | { |
206 | 0 | libcerror_error_set( |
207 | 0 | error, |
208 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
209 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
210 | 0 | "%s: invalid IO handle.", |
211 | 0 | function ); |
212 | |
|
213 | 0 | return( -1 ); |
214 | 0 | } |
215 | 652 | if( data == NULL ) |
216 | 0 | { |
217 | 0 | libcerror_error_set( |
218 | 0 | error, |
219 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
220 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
221 | 0 | "%s: invalid data.", |
222 | 0 | function ); |
223 | |
|
224 | 0 | return( -1 ); |
225 | 0 | } |
226 | 652 | if( ( data_size < 2 ) |
227 | 648 | || ( data_size > (size_t) SSIZE_MAX ) ) |
228 | 4 | { |
229 | 4 | libcerror_error_set( |
230 | 4 | error, |
231 | 4 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
232 | 4 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
233 | 4 | "%s: invalid data size value out of bounds.", |
234 | 4 | function ); |
235 | | |
236 | 4 | return( -1 ); |
237 | 4 | } |
238 | 648 | if( libfsxfs_directory_table_header_initialize( |
239 | 648 | &( directory_table->header ), |
240 | 648 | error ) != 1 ) |
241 | 0 | { |
242 | 0 | libcerror_error_set( |
243 | 0 | error, |
244 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
245 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
246 | 0 | "%s: unable to create header.", |
247 | 0 | function ); |
248 | |
|
249 | 0 | goto on_error; |
250 | 0 | } |
251 | 648 | if( libfsxfs_directory_table_header_read_data( |
252 | 648 | directory_table->header, |
253 | 648 | io_handle, |
254 | 648 | data, |
255 | 648 | data_size, |
256 | 648 | error ) != 1 ) |
257 | 17 | { |
258 | 17 | libcerror_error_set( |
259 | 17 | error, |
260 | 17 | LIBCERROR_ERROR_DOMAIN_IO, |
261 | 17 | LIBCERROR_IO_ERROR_READ_FAILED, |
262 | 17 | "%s: unable to read directory table header.", |
263 | 17 | function ); |
264 | | |
265 | 17 | goto on_error; |
266 | 17 | } |
267 | 631 | if( ( io_handle->feature_flags & LIBFSXFS_FEATURE_FLAG_DIRECTORY_V2 ) == 0 ) |
268 | 61 | { |
269 | 61 | data_offset = 9; |
270 | 61 | } |
271 | 570 | else if( directory_table->header->inode_number_data_size == 4 ) |
272 | 143 | { |
273 | 143 | data_offset = 6; |
274 | 143 | } |
275 | 427 | else |
276 | 427 | { |
277 | 427 | data_offset = 10; |
278 | 427 | } |
279 | 631 | for( directory_entry_index = 0; |
280 | 4.68k | directory_entry_index < directory_table->header->number_of_entries; |
281 | 4.05k | directory_entry_index++ ) |
282 | 4.12k | { |
283 | 4.12k | entry_data_size = directory_table->header->inode_number_data_size + 1; |
284 | 4.12k | name_offset = data_offset; |
285 | | |
286 | 4.12k | if( ( io_handle->feature_flags & LIBFSXFS_FEATURE_FLAG_DIRECTORY_V2 ) == 0 ) |
287 | 638 | { |
288 | 638 | name_offset += directory_table->header->inode_number_data_size; |
289 | 638 | } |
290 | 3.48k | else if( ( io_handle->format_version == 5 ) |
291 | 2.85k | || ( ( io_handle->secondary_feature_flags & LIBFSXFS_SECONDARY_FEATURE_FLAG_FILE_TYPE ) != 0 ) ) |
292 | 1.38k | { |
293 | 1.38k | entry_data_size += 3; |
294 | 1.38k | } |
295 | 2.10k | else |
296 | 2.10k | { |
297 | 2.10k | entry_data_size += 2; |
298 | 2.10k | } |
299 | 4.12k | if( name_offset >= data_size ) |
300 | 17 | { |
301 | 17 | libcerror_error_set( |
302 | 17 | error, |
303 | 17 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
304 | 17 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
305 | 17 | "%s: invalid directory entry: %d name offset value out of bounds.", |
306 | 17 | function, |
307 | 17 | directory_entry_index ); |
308 | | |
309 | 17 | goto on_error; |
310 | 17 | } |
311 | 4.10k | name_size = data[ name_offset ]; |
312 | | |
313 | 4.10k | entry_data_size += name_size; |
314 | | |
315 | 4.10k | if( entry_data_size > ( data_size - data_offset ) ) |
316 | 56 | { |
317 | 56 | libcerror_error_set( |
318 | 56 | error, |
319 | 56 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
320 | 56 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
321 | 56 | "%s: invalid directory entry: %d data size value out of bounds.", |
322 | 56 | function, |
323 | 56 | directory_entry_index ); |
324 | | |
325 | 56 | goto on_error; |
326 | 56 | } |
327 | | #if defined( HAVE_DEBUG_OUTPUT ) |
328 | | if( libcnotify_verbose != 0 ) |
329 | | { |
330 | | libcnotify_printf( |
331 | | "%s: directory entry: %d data:\n", |
332 | | function, |
333 | | directory_entry_index ); |
334 | | libcnotify_print_data( |
335 | | &( data[ data_offset ] ), |
336 | | entry_data_size, |
337 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
338 | | } |
339 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
340 | | |
341 | 4.05k | if( libfsxfs_directory_entry_initialize( |
342 | 4.05k | &directory_entry, |
343 | 4.05k | error ) != 1 ) |
344 | 0 | { |
345 | 0 | libcerror_error_set( |
346 | 0 | error, |
347 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
348 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
349 | 0 | "%s: unable to create entry.", |
350 | 0 | function ); |
351 | |
|
352 | 0 | goto on_error; |
353 | 0 | } |
354 | 4.05k | if( ( io_handle->feature_flags & LIBFSXFS_FEATURE_FLAG_DIRECTORY_V2 ) == 0 ) |
355 | 614 | { |
356 | 614 | if( directory_table->header->inode_number_data_size == 4 ) |
357 | 0 | { |
358 | 0 | byte_stream_copy_to_uint32_big_endian( |
359 | 0 | &( data[ data_offset ] ), |
360 | 0 | directory_entry->inode_number ); |
361 | |
|
362 | 0 | data_offset += 4; |
363 | 0 | } |
364 | 614 | else |
365 | 614 | { |
366 | 614 | byte_stream_copy_to_uint64_big_endian( |
367 | 614 | &( data[ data_offset ] ), |
368 | 614 | directory_entry->inode_number ); |
369 | | |
370 | 614 | data_offset += 8; |
371 | 614 | } |
372 | | #if defined( HAVE_DEBUG_OUTPUT ) |
373 | | if( libcnotify_verbose != 0 ) |
374 | | { |
375 | | libcnotify_printf( |
376 | | "%s: inode number\t\t\t: %" PRIu64 "\n", |
377 | | function, |
378 | | directory_entry->inode_number ); |
379 | | |
380 | | libcnotify_printf( |
381 | | "\n" ); |
382 | | } |
383 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
384 | 614 | } |
385 | 4.05k | data_offset++; |
386 | | |
387 | | #if defined( HAVE_DEBUG_OUTPUT ) |
388 | | if( libcnotify_verbose != 0 ) |
389 | | { |
390 | | libcnotify_printf( |
391 | | "%s: name size\t\t\t\t: %" PRIu8 "\n", |
392 | | function, |
393 | | name_size ); |
394 | | } |
395 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
396 | | |
397 | 4.05k | if( ( io_handle->feature_flags & LIBFSXFS_FEATURE_FLAG_DIRECTORY_V2 ) != 0 ) |
398 | 3.43k | { |
399 | | #if defined( HAVE_DEBUG_OUTPUT ) |
400 | | if( libcnotify_verbose != 0 ) |
401 | | { |
402 | | byte_stream_copy_to_uint16_big_endian( |
403 | | &( data[ data_offset ] ), |
404 | | value_16bit ); |
405 | | libcnotify_printf( |
406 | | "%s: tag offset\t\t\t\t: %" PRIu16 "\n", |
407 | | function, |
408 | | value_16bit ); |
409 | | } |
410 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
411 | | |
412 | 3.43k | data_offset += 2; |
413 | 3.43k | } |
414 | | #if defined( HAVE_DEBUG_OUTPUT ) |
415 | | if( libcnotify_verbose != 0 ) |
416 | | { |
417 | | if( libfsxfs_debug_print_utf8_string_value( |
418 | | function, |
419 | | "name\t\t\t\t", |
420 | | &( data[ data_offset ] ), |
421 | | name_size, |
422 | | error ) != 1 ) |
423 | | { |
424 | | libcerror_error_set( |
425 | | error, |
426 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
427 | | LIBCERROR_RUNTIME_ERROR_PRINT_FAILED, |
428 | | "%s: unable to print UTF-8 string value.", |
429 | | function ); |
430 | | |
431 | | goto on_error; |
432 | | } |
433 | | } |
434 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
435 | | |
436 | 4.05k | if( memory_copy( |
437 | 4.05k | directory_entry->name, |
438 | 4.05k | &( data[ data_offset ] ), |
439 | 4.05k | (size_t) name_size ) == NULL ) |
440 | 0 | { |
441 | 0 | libcerror_error_set( |
442 | 0 | error, |
443 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
444 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
445 | 0 | "%s: unable to copy name.", |
446 | 0 | function ); |
447 | |
|
448 | 0 | goto on_error; |
449 | 0 | } |
450 | 4.05k | directory_entry->name_size = name_size; |
451 | | |
452 | 4.05k | data_offset += name_size; |
453 | | |
454 | 4.05k | if( ( io_handle->feature_flags & LIBFSXFS_FEATURE_FLAG_DIRECTORY_V2 ) != 0 ) |
455 | 3.43k | { |
456 | 3.43k | if( ( io_handle->format_version == 5 ) |
457 | 2.81k | || ( ( io_handle->secondary_feature_flags & LIBFSXFS_SECONDARY_FEATURE_FLAG_FILE_TYPE ) != 0 ) ) |
458 | 1.35k | { |
459 | | #if defined( HAVE_DEBUG_OUTPUT ) |
460 | | if( libcnotify_verbose != 0 ) |
461 | | { |
462 | | libcnotify_printf( |
463 | | "%s: file type\t\t\t\t: %" PRIu8 "\n", |
464 | | function, |
465 | | data[ data_offset ] ); |
466 | | } |
467 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
468 | | |
469 | 1.35k | data_offset++; |
470 | 1.35k | } |
471 | 3.43k | if( directory_table->header->inode_number_data_size == 4 ) |
472 | 2.04k | { |
473 | 2.04k | byte_stream_copy_to_uint32_big_endian( |
474 | 2.04k | &( data[ data_offset ] ), |
475 | 2.04k | directory_entry->inode_number ); |
476 | | |
477 | 2.04k | data_offset += 4; |
478 | 2.04k | } |
479 | 1.39k | else |
480 | 1.39k | { |
481 | 1.39k | byte_stream_copy_to_uint64_big_endian( |
482 | 1.39k | &( data[ data_offset ] ), |
483 | 1.39k | directory_entry->inode_number ); |
484 | | |
485 | 1.39k | data_offset += 8; |
486 | 1.39k | } |
487 | | #if defined( HAVE_DEBUG_OUTPUT ) |
488 | | if( libcnotify_verbose != 0 ) |
489 | | { |
490 | | libcnotify_printf( |
491 | | "%s: inode number\t\t\t: %" PRIu64 "\n", |
492 | | function, |
493 | | directory_entry->inode_number ); |
494 | | |
495 | | libcnotify_printf( |
496 | | "\n" ); |
497 | | } |
498 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
499 | 3.43k | } |
500 | 4.05k | if( libcdata_array_append_entry( |
501 | 4.05k | entries_array, |
502 | 4.05k | &entry_index, |
503 | 4.05k | (intptr_t *) directory_entry, |
504 | 4.05k | error ) != 1 ) |
505 | 0 | { |
506 | 0 | libcerror_error_set( |
507 | 0 | error, |
508 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
509 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
510 | 0 | "%s: unable to append directory entry to entries array.", |
511 | 0 | function ); |
512 | |
|
513 | 0 | goto on_error; |
514 | 0 | } |
515 | 4.05k | directory_entry = NULL; |
516 | 4.05k | } |
517 | 558 | return( 1 ); |
518 | | |
519 | 90 | on_error: |
520 | 90 | if( directory_entry != NULL ) |
521 | 0 | { |
522 | 0 | libfsxfs_directory_entry_free( |
523 | 0 | &directory_entry, |
524 | 0 | NULL ); |
525 | 0 | } |
526 | 90 | if( directory_table->header != NULL ) |
527 | 90 | { |
528 | 90 | libfsxfs_directory_table_header_free( |
529 | 90 | &( directory_table->header ), |
530 | 90 | NULL ); |
531 | 90 | } |
532 | 90 | libcdata_array_empty( |
533 | 90 | entries_array, |
534 | 90 | (int (*)(intptr_t **, libcerror_error_t **)) &libfsxfs_directory_entry_free, |
535 | 90 | NULL ); |
536 | | |
537 | 90 | return( -1 ); |
538 | 631 | } |
539 | | |