/src/libfsxfs/libfsxfs/libfsxfs_directory_table_header.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Short-form directory table header functions |
3 | | * |
4 | | * Copyright (C) 2020-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 "libfsxfs_directory_table_header.h" |
28 | | #include "libfsxfs_libcerror.h" |
29 | | #include "libfsxfs_libcnotify.h" |
30 | | |
31 | | #include "fsxfs_btree.h" |
32 | | |
33 | | /* Creates a directory_table_header |
34 | | * Make sure the value directory_table_header is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libfsxfs_directory_table_header_initialize( |
38 | | libfsxfs_directory_table_header_t **directory_table_header, |
39 | | libcerror_error_t **error ) |
40 | 588 | { |
41 | 588 | static char *function = "libfsxfs_directory_table_header_initialize"; |
42 | | |
43 | 588 | if( directory_table_header == 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 directory table header.", |
50 | 0 | function ); |
51 | |
|
52 | 0 | return( -1 ); |
53 | 0 | } |
54 | 588 | if( *directory_table_header != 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 directory table header value already set.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 588 | *directory_table_header = memory_allocate_structure( |
66 | 588 | libfsxfs_directory_table_header_t ); |
67 | | |
68 | 588 | if( *directory_table_header == 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 directory table header.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | goto on_error; |
78 | 0 | } |
79 | 588 | if( memory_set( |
80 | 588 | *directory_table_header, |
81 | 588 | 0, |
82 | 588 | sizeof( libfsxfs_directory_table_header_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 directory table header.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | goto on_error; |
92 | 0 | } |
93 | 588 | return( 1 ); |
94 | | |
95 | 0 | on_error: |
96 | 0 | if( *directory_table_header != NULL ) |
97 | 0 | { |
98 | 0 | memory_free( |
99 | 0 | *directory_table_header ); |
100 | |
|
101 | 0 | *directory_table_header = NULL; |
102 | 0 | } |
103 | 0 | return( -1 ); |
104 | 588 | } |
105 | | |
106 | | /* Frees a directory_table_header |
107 | | * Returns 1 if successful or -1 on error |
108 | | */ |
109 | | int libfsxfs_directory_table_header_free( |
110 | | libfsxfs_directory_table_header_t **directory_table_header, |
111 | | libcerror_error_t **error ) |
112 | 588 | { |
113 | 588 | static char *function = "libfsxfs_directory_table_header_free"; |
114 | | |
115 | 588 | if( directory_table_header == 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 directory table header.", |
122 | 0 | function ); |
123 | |
|
124 | 0 | return( -1 ); |
125 | 0 | } |
126 | 588 | if( *directory_table_header != NULL ) |
127 | 588 | { |
128 | 588 | memory_free( |
129 | 588 | *directory_table_header ); |
130 | | |
131 | 588 | *directory_table_header = NULL; |
132 | 588 | } |
133 | 588 | return( 1 ); |
134 | 588 | } |
135 | | |
136 | | /* Reads the directory_table_header data |
137 | | * Returns 1 if successful or -1 on error |
138 | | */ |
139 | | int libfsxfs_directory_table_header_read_data( |
140 | | libfsxfs_directory_table_header_t *directory_table_header, |
141 | | const uint8_t *data, |
142 | | size_t data_size, |
143 | | libcerror_error_t **error ) |
144 | 588 | { |
145 | 588 | static char *function = "libfsxfs_directory_table_header_read_data"; |
146 | 588 | size_t header_data_size = 0; |
147 | 588 | uint8_t number_of_32bit_entries = 0; |
148 | 588 | uint8_t number_of_64bit_entries = 0; |
149 | | |
150 | 588 | if( directory_table_header == 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 directory table header.", |
157 | 0 | function ); |
158 | |
|
159 | 0 | return( -1 ); |
160 | 0 | } |
161 | 588 | 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 | 588 | if( ( data_size < 2 ) |
173 | 588 | || ( data_size > (size_t) SSIZE_MAX ) ) |
174 | 0 | { |
175 | 0 | libcerror_error_set( |
176 | 0 | error, |
177 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
178 | 0 | LIBCERROR_ARGUMENT_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 | 588 | number_of_32bit_entries = data[ 0 ]; |
185 | 588 | number_of_64bit_entries = data[ 1 ]; |
186 | | |
187 | 588 | if( ( number_of_32bit_entries != 0 ) |
188 | 588 | && ( number_of_64bit_entries != 0 ) ) |
189 | 16 | { |
190 | 16 | libcerror_error_set( |
191 | 16 | error, |
192 | 16 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
193 | 16 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
194 | 16 | "%s: invalid number of 32-bit and 64-bit entries.", |
195 | 16 | function ); |
196 | | |
197 | 16 | return( -1 ); |
198 | 16 | } |
199 | 572 | if( number_of_64bit_entries == 0 ) |
200 | 165 | { |
201 | 165 | header_data_size = 6; |
202 | 165 | } |
203 | 407 | else |
204 | 407 | { |
205 | 407 | header_data_size = 10; |
206 | 407 | } |
207 | 572 | if( data_size < header_data_size ) |
208 | 8 | { |
209 | 8 | libcerror_error_set( |
210 | 8 | error, |
211 | 8 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
212 | 8 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
213 | 8 | "%s: invalid data size value out of bounds.", |
214 | 8 | function ); |
215 | | |
216 | 8 | return( -1 ); |
217 | 8 | } |
218 | | #if defined( HAVE_DEBUG_OUTPUT ) |
219 | | if( libcnotify_verbose != 0 ) |
220 | | { |
221 | | libcnotify_printf( |
222 | | "%s: directory table header data:\n", |
223 | | function ); |
224 | | libcnotify_print_data( |
225 | | data, |
226 | | header_data_size, |
227 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
228 | | } |
229 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
230 | | |
231 | 564 | if( number_of_64bit_entries == 0 ) |
232 | 164 | { |
233 | 164 | directory_table_header->number_of_entries = number_of_32bit_entries; |
234 | | |
235 | 164 | directory_table_header->inode_number_data_size = 4; |
236 | | |
237 | 164 | byte_stream_copy_to_uint32_big_endian( |
238 | 164 | &( data[ 2 ] ), |
239 | 164 | directory_table_header->parent_inode_number ); |
240 | 164 | } |
241 | 400 | else |
242 | 400 | { |
243 | 400 | directory_table_header->number_of_entries = number_of_64bit_entries; |
244 | | |
245 | 400 | directory_table_header->inode_number_data_size = 8; |
246 | | |
247 | 400 | byte_stream_copy_to_uint64_big_endian( |
248 | 400 | &( data[ 2 ] ), |
249 | 400 | directory_table_header->parent_inode_number ); |
250 | 400 | } |
251 | | #if defined( HAVE_DEBUG_OUTPUT ) |
252 | | if( libcnotify_verbose != 0 ) |
253 | | { |
254 | | libcnotify_printf( |
255 | | "%s: number of 32-bit entries\t: %" PRIu8 "\n", |
256 | | function, |
257 | | number_of_32bit_entries ); |
258 | | |
259 | | libcnotify_printf( |
260 | | "%s: number of 64-bit entries\t: %" PRIu8 "\n", |
261 | | function, |
262 | | number_of_64bit_entries ); |
263 | | |
264 | | libcnotify_printf( |
265 | | "%s: parent inode number\t\t: %" PRIu64 "\n", |
266 | | function, |
267 | | directory_table_header->parent_inode_number ); |
268 | | |
269 | | libcnotify_printf( |
270 | | "\n" ); |
271 | | } |
272 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
273 | | |
274 | 564 | return( 1 ); |
275 | 572 | } |
276 | | |