/src/libfsntfs/libfsntfs/libfsntfs_index_node_header.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Index node 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 "libfsntfs_debug.h" |
28 | | #include "libfsntfs_definitions.h" |
29 | | #include "libfsntfs_index_node_header.h" |
30 | | #include "libfsntfs_libcerror.h" |
31 | | #include "libfsntfs_libcnotify.h" |
32 | | |
33 | | #include "fsntfs_index.h" |
34 | | |
35 | | /* Creates an index node header |
36 | | * Make sure the value index_node_header is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libfsntfs_index_node_header_initialize( |
40 | | libfsntfs_index_node_header_t **index_node_header, |
41 | | libcerror_error_t **error ) |
42 | 2.05k | { |
43 | 2.05k | static char *function = "libfsntfs_index_node_header_initialize"; |
44 | | |
45 | 2.05k | if( index_node_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 index node header.", |
52 | 0 | function ); |
53 | |
|
54 | 0 | return( -1 ); |
55 | 0 | } |
56 | 2.05k | if( *index_node_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 index node header value already set.", |
63 | 0 | function ); |
64 | |
|
65 | 0 | return( -1 ); |
66 | 0 | } |
67 | 2.05k | *index_node_header = memory_allocate_structure( |
68 | 2.05k | libfsntfs_index_node_header_t ); |
69 | | |
70 | 2.05k | if( *index_node_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 index node header.", |
77 | 0 | function ); |
78 | |
|
79 | 0 | goto on_error; |
80 | 0 | } |
81 | 2.05k | if( memory_set( |
82 | 2.05k | *index_node_header, |
83 | 2.05k | 0, |
84 | 2.05k | sizeof( libfsntfs_index_node_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 index node header.", |
91 | 0 | function ); |
92 | |
|
93 | 0 | memory_free( |
94 | 0 | *index_node_header ); |
95 | |
|
96 | 0 | *index_node_header = NULL; |
97 | |
|
98 | 0 | return( -1 ); |
99 | 0 | } |
100 | 2.05k | return( 1 ); |
101 | | |
102 | 0 | on_error: |
103 | 0 | if( *index_node_header != NULL ) |
104 | 0 | { |
105 | 0 | memory_free( |
106 | 0 | *index_node_header ); |
107 | |
|
108 | 0 | *index_node_header = NULL; |
109 | 0 | } |
110 | 0 | return( -1 ); |
111 | 2.05k | } |
112 | | |
113 | | /* Frees an index node header |
114 | | * Returns 1 if successful or -1 on error |
115 | | */ |
116 | | int libfsntfs_index_node_header_free( |
117 | | libfsntfs_index_node_header_t **index_node_header, |
118 | | libcerror_error_t **error ) |
119 | 2.05k | { |
120 | 2.05k | static char *function = "libfsntfs_index_node_header_free"; |
121 | | |
122 | 2.05k | if( index_node_header == NULL ) |
123 | 0 | { |
124 | 0 | libcerror_error_set( |
125 | 0 | error, |
126 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
127 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
128 | 0 | "%s: invalid index node header.", |
129 | 0 | function ); |
130 | |
|
131 | 0 | return( -1 ); |
132 | 0 | } |
133 | 2.05k | if( *index_node_header != NULL ) |
134 | 2.05k | { |
135 | 2.05k | memory_free( |
136 | 2.05k | *index_node_header ); |
137 | | |
138 | 2.05k | *index_node_header = NULL; |
139 | 2.05k | } |
140 | 2.05k | return( 1 ); |
141 | 2.05k | } |
142 | | |
143 | | /* Reads the index node header header |
144 | | * Returns the number of bytes read if successful or -1 on error |
145 | | */ |
146 | | int libfsntfs_index_node_header_read_data( |
147 | | libfsntfs_index_node_header_t *index_node_header, |
148 | | const uint8_t *data, |
149 | | size_t data_size, |
150 | | libcerror_error_t **error ) |
151 | 2.05k | { |
152 | 2.05k | static char *function = "libfsntfs_index_node_header_read_data"; |
153 | | |
154 | | #if defined( HAVE_DEBUG_OUTPUT ) |
155 | | uint32_t value_32bit = 0; |
156 | | #endif |
157 | | |
158 | 2.05k | if( index_node_header == NULL ) |
159 | 0 | { |
160 | 0 | libcerror_error_set( |
161 | 0 | error, |
162 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
163 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
164 | 0 | "%s: invalid index node header.", |
165 | 0 | function ); |
166 | |
|
167 | 0 | return( -1 ); |
168 | 0 | } |
169 | 2.05k | if( data == NULL ) |
170 | 0 | { |
171 | 0 | libcerror_error_set( |
172 | 0 | error, |
173 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
174 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
175 | 0 | "%s: invalid data.", |
176 | 0 | function ); |
177 | |
|
178 | 0 | return( -1 ); |
179 | 0 | } |
180 | 2.05k | if( ( data_size < sizeof( fsntfs_index_node_header_t ) ) |
181 | 2.05k | || ( data_size > (size_t) SSIZE_MAX ) ) |
182 | 9 | { |
183 | 9 | libcerror_error_set( |
184 | 9 | error, |
185 | 9 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
186 | 9 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
187 | 9 | "%s: invalid data size value out of bounds.", |
188 | 9 | function ); |
189 | | |
190 | 9 | return( -1 ); |
191 | 9 | } |
192 | | #if defined( HAVE_DEBUG_OUTPUT ) |
193 | | if( libcnotify_verbose != 0 ) |
194 | | { |
195 | | libcnotify_printf( |
196 | | "%s: index node header header data:\n", |
197 | | function ); |
198 | | libcnotify_print_data( |
199 | | data, |
200 | | sizeof( fsntfs_index_node_header_t ), |
201 | | 0 ); |
202 | | } |
203 | | #endif |
204 | 2.04k | byte_stream_copy_to_uint32_little_endian( |
205 | 2.04k | ( (fsntfs_index_node_header_t *) data )->index_values_offset, |
206 | 2.04k | index_node_header->index_values_offset ); |
207 | | |
208 | 2.04k | byte_stream_copy_to_uint32_little_endian( |
209 | 2.04k | ( (fsntfs_index_node_header_t *) data )->size, |
210 | 2.04k | index_node_header->size ); |
211 | | |
212 | 2.04k | byte_stream_copy_to_uint32_little_endian( |
213 | 2.04k | ( (fsntfs_index_node_header_t *) data )->flags, |
214 | 2.04k | index_node_header->flags ); |
215 | | |
216 | | #if defined( HAVE_DEBUG_OUTPUT ) |
217 | | if( libcnotify_verbose != 0 ) |
218 | | { |
219 | | libcnotify_printf( |
220 | | "%s: index values offset\t\t: %" PRIu32 "\n", |
221 | | function, |
222 | | index_node_header->index_values_offset ); |
223 | | |
224 | | libcnotify_printf( |
225 | | "%s: size\t\t\t\t: %" PRIu32 "\n", |
226 | | function, |
227 | | index_node_header->size ); |
228 | | |
229 | | byte_stream_copy_to_uint32_little_endian( |
230 | | ( (fsntfs_index_node_header_t *) data )->allocated_size, |
231 | | value_32bit ); |
232 | | libcnotify_printf( |
233 | | "%s: allocated size\t\t\t: %" PRIu32 "\n", |
234 | | function, |
235 | | value_32bit ); |
236 | | |
237 | | libcnotify_printf( |
238 | | "%s: flags\t\t\t\t: 0x%08" PRIx32 "\n", |
239 | | function, |
240 | | index_node_header->flags ); |
241 | | libfsntfs_debug_print_index_node_flags( |
242 | | index_node_header->flags ); |
243 | | libcnotify_printf( |
244 | | "\n" ); |
245 | | } |
246 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
247 | | |
248 | 2.04k | if( index_node_header->size > 0 ) |
249 | 2.03k | { |
250 | 2.03k | if( (size_t) index_node_header->size < sizeof( fsntfs_index_node_header_t ) ) |
251 | 5 | { |
252 | 5 | libcerror_error_set( |
253 | 5 | error, |
254 | 5 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
255 | 5 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
256 | 5 | "%s: invalid index node header size value out of bounds.", |
257 | 5 | function ); |
258 | | |
259 | 5 | return( -1 ); |
260 | 5 | } |
261 | 2.02k | if( ( (size_t) index_node_header->index_values_offset < sizeof( fsntfs_index_node_header_t ) ) |
262 | 2.02k | || ( index_node_header->index_values_offset > index_node_header->size ) |
263 | 2.02k | || ( ( index_node_header->index_values_offset % 8 ) != 0 ) ) |
264 | 72 | { |
265 | 72 | libcerror_error_set( |
266 | 72 | error, |
267 | 72 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
268 | 72 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
269 | 72 | "%s: invalid index values offset value out of bounds.", |
270 | 72 | function ); |
271 | | |
272 | 72 | return( -1 ); |
273 | 72 | } |
274 | 2.02k | } |
275 | 1.96k | return( 1 ); |
276 | 2.04k | } |
277 | | |