/src/libfsext/libfsext/libfsext_extents_header.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Extents header functions |
3 | | * |
4 | | * Copyright (C) 2010-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 "libfsext_extents_header.h" |
28 | | #include "libfsext_libcerror.h" |
29 | | #include "libfsext_libcnotify.h" |
30 | | |
31 | | #include "fsext_extents.h" |
32 | | |
33 | | const char *fsext_extents_header_signature = "\x0a\xf3"; |
34 | | |
35 | | /* Creates an extents header |
36 | | * Make sure the value extents_header is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libfsext_extents_header_initialize( |
40 | | libfsext_extents_header_t **extents_header, |
41 | | libcerror_error_t **error ) |
42 | 7.39k | { |
43 | 7.39k | static char *function = "libfsext_extents_header_initialize"; |
44 | | |
45 | 7.39k | if( extents_header == NULL ) |
46 | 0 | { |
47 | 0 | libcerror_error_set( |
48 | 0 | error, |
49 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
50 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
51 | 0 | "%s: invalid extents header.", |
52 | 0 | function ); |
53 | |
|
54 | 0 | return( -1 ); |
55 | 0 | } |
56 | 7.39k | if( *extents_header != NULL ) |
57 | 0 | { |
58 | 0 | libcerror_error_set( |
59 | 0 | error, |
60 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
61 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
62 | 0 | "%s: invalid extents header value already set.", |
63 | 0 | function ); |
64 | |
|
65 | 0 | return( -1 ); |
66 | 0 | } |
67 | 7.39k | *extents_header = memory_allocate_structure( |
68 | 7.39k | libfsext_extents_header_t ); |
69 | | |
70 | 7.39k | if( *extents_header == NULL ) |
71 | 0 | { |
72 | 0 | libcerror_error_set( |
73 | 0 | error, |
74 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
75 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
76 | 0 | "%s: unable to create extents header.", |
77 | 0 | function ); |
78 | |
|
79 | 0 | goto on_error; |
80 | 0 | } |
81 | 7.39k | if( memory_set( |
82 | 7.39k | *extents_header, |
83 | 7.39k | 0, |
84 | 7.39k | sizeof( libfsext_extents_header_t ) ) == NULL ) |
85 | 0 | { |
86 | 0 | libcerror_error_set( |
87 | 0 | error, |
88 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
89 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
90 | 0 | "%s: unable to clear extents header.", |
91 | 0 | function ); |
92 | |
|
93 | 0 | goto on_error; |
94 | 0 | } |
95 | 7.39k | return( 1 ); |
96 | | |
97 | 0 | on_error: |
98 | 0 | if( *extents_header != NULL ) |
99 | 0 | { |
100 | 0 | memory_free( |
101 | 0 | *extents_header ); |
102 | |
|
103 | 0 | *extents_header = NULL; |
104 | 0 | } |
105 | 0 | return( -1 ); |
106 | 7.39k | } |
107 | | |
108 | | /* Frees an extents header |
109 | | * Returns 1 if successful or -1 on error |
110 | | */ |
111 | | int libfsext_extents_header_free( |
112 | | libfsext_extents_header_t **extents_header, |
113 | | libcerror_error_t **error ) |
114 | 7.39k | { |
115 | 7.39k | static char *function = "libfsext_extents_header_free"; |
116 | | |
117 | 7.39k | if( extents_header == NULL ) |
118 | 0 | { |
119 | 0 | libcerror_error_set( |
120 | 0 | error, |
121 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
122 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
123 | 0 | "%s: invalid extents header.", |
124 | 0 | function ); |
125 | |
|
126 | 0 | return( -1 ); |
127 | 0 | } |
128 | 7.39k | if( *extents_header != NULL ) |
129 | 7.39k | { |
130 | 7.39k | memory_free( |
131 | 7.39k | *extents_header ); |
132 | | |
133 | 7.39k | *extents_header = NULL; |
134 | 7.39k | } |
135 | 7.39k | return( 1 ); |
136 | 7.39k | } |
137 | | |
138 | | /* Reads the extents header data |
139 | | * Returns 1 if successful or -1 on error |
140 | | */ |
141 | | int libfsext_extents_header_read_data( |
142 | | libfsext_extents_header_t *extents_header, |
143 | | const uint8_t *data, |
144 | | size_t data_size, |
145 | | libcerror_error_t **error ) |
146 | 7.39k | { |
147 | 7.39k | static char *function = "libfsext_extents_header_read_data"; |
148 | | |
149 | | #if defined( HAVE_DEBUG_OUTPUT ) |
150 | | uint16_t value_16bit = 0; |
151 | | #endif |
152 | | |
153 | 7.39k | if( extents_header == NULL ) |
154 | 0 | { |
155 | 0 | libcerror_error_set( |
156 | 0 | error, |
157 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
158 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
159 | 0 | "%s: invalid extents header.", |
160 | 0 | function ); |
161 | |
|
162 | 0 | return( -1 ); |
163 | 0 | } |
164 | 7.39k | if( data == NULL ) |
165 | 0 | { |
166 | 0 | libcerror_error_set( |
167 | 0 | error, |
168 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
169 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
170 | 0 | "%s: invalid data.", |
171 | 0 | function ); |
172 | |
|
173 | 0 | return( -1 ); |
174 | 0 | } |
175 | 7.39k | if( ( data_size < sizeof( fsext_extents_header_ext4_t ) ) |
176 | 7.39k | || ( data_size > (size_t) SSIZE_MAX ) ) |
177 | 0 | { |
178 | 0 | libcerror_error_set( |
179 | 0 | error, |
180 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
181 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
182 | 0 | "%s: invalid data size value out of bounds.", |
183 | 0 | function ); |
184 | |
|
185 | 0 | return( -1 ); |
186 | 0 | } |
187 | | #if defined( HAVE_DEBUG_OUTPUT ) |
188 | | if( libcnotify_verbose != 0 ) |
189 | | { |
190 | | libcnotify_printf( |
191 | | "%s: extents header data:\n", |
192 | | function ); |
193 | | libcnotify_print_data( |
194 | | data, |
195 | | data_size, |
196 | | 0 ); |
197 | | } |
198 | | #endif |
199 | 7.39k | if( memory_compare( |
200 | 7.39k | ( (fsext_extents_header_ext4_t *) data )->signature, |
201 | 7.39k | fsext_extents_header_signature, |
202 | 7.39k | 2 ) != 0 ) |
203 | 40 | { |
204 | 40 | libcerror_error_set( |
205 | 40 | error, |
206 | 40 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
207 | 40 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
208 | 40 | "%s: invalid signature.", |
209 | 40 | function ); |
210 | | |
211 | 40 | return( -1 ); |
212 | 40 | } |
213 | 7.35k | byte_stream_copy_to_uint16_little_endian( |
214 | 7.35k | ( (fsext_extents_header_ext4_t *) data )->number_of_extents, |
215 | 7.35k | extents_header->number_of_extents ); |
216 | | |
217 | 7.35k | byte_stream_copy_to_uint16_little_endian( |
218 | 7.35k | ( (fsext_extents_header_ext4_t *) data )->depth, |
219 | 7.35k | extents_header->depth ); |
220 | | |
221 | 7.35k | byte_stream_copy_to_uint32_little_endian( |
222 | 7.35k | ( (fsext_extents_header_ext4_t *) data )->generation, |
223 | 7.35k | extents_header->generation ); |
224 | | |
225 | | #if defined( HAVE_DEBUG_OUTPUT ) |
226 | | if( libcnotify_verbose != 0 ) |
227 | | { |
228 | | byte_stream_copy_to_uint16_little_endian( |
229 | | ( (fsext_extents_header_ext4_t *) data )->signature, |
230 | | value_16bit ); |
231 | | libcnotify_printf( |
232 | | "%s: signature\t\t\t\t: 0x%04" PRIx16 "\n", |
233 | | function, |
234 | | value_16bit ); |
235 | | |
236 | | libcnotify_printf( |
237 | | "%s: number of extents\t\t\t: %" PRIu16 "\n", |
238 | | function, |
239 | | extents_header->number_of_extents ); |
240 | | |
241 | | byte_stream_copy_to_uint16_little_endian( |
242 | | ( (fsext_extents_header_ext4_t *) data )->maximum_number_of_extents, |
243 | | value_16bit ); |
244 | | libcnotify_printf( |
245 | | "%s: maximum number of extents\t\t: %" PRIu16 "\n", |
246 | | function, |
247 | | value_16bit ); |
248 | | |
249 | | libcnotify_printf( |
250 | | "%s: depth\t\t\t\t: %" PRIu16 "\n", |
251 | | function, |
252 | | extents_header->depth ); |
253 | | |
254 | | libcnotify_printf( |
255 | | "%s: generation\t\t\t\t: %" PRIu32 "\n", |
256 | | function, |
257 | | extents_header->generation ); |
258 | | |
259 | | libcnotify_printf( |
260 | | "\n" ); |
261 | | } |
262 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
263 | | |
264 | 7.35k | if( extents_header->depth > 5 ) |
265 | 21 | { |
266 | 21 | libcerror_error_set( |
267 | 21 | error, |
268 | 21 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
269 | 21 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
270 | 21 | "%s: invalid depth value out of bounds.", |
271 | 21 | function ); |
272 | | |
273 | 21 | return( -1 ); |
274 | 21 | } |
275 | 7.33k | return( 1 ); |
276 | 7.35k | } |
277 | | |