/src/libfsntfs/libfsntfs/libfsntfs_sds_index_value.c
Line | Count | Source |
1 | | /* |
2 | | * $SDS index value functions |
3 | | * |
4 | | * Copyright (C) 2010-2025, 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_libcerror.h" |
28 | | #include "libfsntfs_libcnotify.h" |
29 | | #include "libfsntfs_sds_index_value.h" |
30 | | |
31 | | #include "fsntfs_secure.h" |
32 | | |
33 | | /* Creates $SDS index value |
34 | | * Make sure the value sds_index_value is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libfsntfs_sds_index_value_initialize( |
38 | | libfsntfs_sds_index_value_t **sds_index_value, |
39 | | libcerror_error_t **error ) |
40 | 0 | { |
41 | 0 | static char *function = "libfsntfs_sds_index_value_initialize"; |
42 | |
|
43 | 0 | if( sds_index_value == 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 $SDS index value.", |
50 | 0 | function ); |
51 | |
|
52 | 0 | return( -1 ); |
53 | 0 | } |
54 | 0 | if( *sds_index_value != 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 $SDS index value value already set.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 0 | *sds_index_value = memory_allocate_structure( |
66 | 0 | libfsntfs_sds_index_value_t ); |
67 | |
|
68 | 0 | if( *sds_index_value == 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 $SDS index value.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | goto on_error; |
78 | 0 | } |
79 | 0 | if( memory_set( |
80 | 0 | *sds_index_value, |
81 | 0 | 0, |
82 | 0 | sizeof( libfsntfs_sds_index_value_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 $SDS index value.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | goto on_error; |
92 | 0 | } |
93 | 0 | return( 1 ); |
94 | | |
95 | 0 | on_error: |
96 | 0 | if( *sds_index_value != NULL ) |
97 | 0 | { |
98 | 0 | memory_free( |
99 | 0 | *sds_index_value ); |
100 | |
|
101 | 0 | *sds_index_value = NULL; |
102 | 0 | } |
103 | 0 | return( -1 ); |
104 | 0 | } |
105 | | |
106 | | /* Frees $SDS index value |
107 | | * Returns 1 if successful or -1 on error |
108 | | */ |
109 | | int libfsntfs_sds_index_value_free( |
110 | | libfsntfs_sds_index_value_t **sds_index_value, |
111 | | libcerror_error_t **error ) |
112 | 0 | { |
113 | 0 | static char *function = "libfsntfs_sds_index_value_free"; |
114 | |
|
115 | 0 | if( sds_index_value == 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 $SDS index value.", |
122 | 0 | function ); |
123 | |
|
124 | 0 | return( -1 ); |
125 | 0 | } |
126 | 0 | if( *sds_index_value != NULL ) |
127 | 0 | { |
128 | 0 | memory_free( |
129 | 0 | *sds_index_value ); |
130 | |
|
131 | 0 | *sds_index_value = NULL; |
132 | 0 | } |
133 | 0 | return( 1 ); |
134 | 0 | } |
135 | | |
136 | | /* Reads the $SDS index value |
137 | | * Returns 1 if successful or -1 on error |
138 | | */ |
139 | | int libfsntfs_sds_index_value_read_data( |
140 | | libfsntfs_sds_index_value_t *sds_index_value, |
141 | | const uint8_t *data, |
142 | | size_t data_size, |
143 | | libcerror_error_t **error ) |
144 | 0 | { |
145 | 0 | static char *function = "libfsntfs_sds_index_value_read_data"; |
146 | |
|
147 | | #if defined( HAVE_DEBUG_OUTPUT ) |
148 | | uint64_t value_64bit = 0; |
149 | | uint32_t value_32bit = 0; |
150 | | #endif |
151 | |
|
152 | 0 | if( sds_index_value == NULL ) |
153 | 0 | { |
154 | 0 | libcerror_error_set( |
155 | 0 | error, |
156 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
157 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
158 | 0 | "%s: invalid $SDS index value.", |
159 | 0 | function ); |
160 | |
|
161 | 0 | return( -1 ); |
162 | 0 | } |
163 | 0 | if( data == NULL ) |
164 | 0 | { |
165 | 0 | libcerror_error_set( |
166 | 0 | error, |
167 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
168 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
169 | 0 | "%s: invalid data.", |
170 | 0 | function ); |
171 | |
|
172 | 0 | return( -1 ); |
173 | 0 | } |
174 | 0 | if( data_size > (size_t) SSIZE_MAX ) |
175 | 0 | { |
176 | 0 | libcerror_error_set( |
177 | 0 | error, |
178 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
179 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
180 | 0 | "%s: invalid data size value out of bounds.", |
181 | 0 | function ); |
182 | |
|
183 | 0 | return( -1 ); |
184 | 0 | } |
185 | | #if defined( HAVE_DEBUG_OUTPUT ) |
186 | | if( libcnotify_verbose != 0 ) |
187 | | { |
188 | | libcnotify_printf( |
189 | | "%s: $SDS index value:\n", |
190 | | function ); |
191 | | libcnotify_print_data( |
192 | | data, |
193 | | data_size, |
194 | | 0 ); |
195 | | } |
196 | | #endif |
197 | 0 | if( data_size != sizeof( fsntfs_secure_index_value_t ) ) |
198 | 0 | { |
199 | 0 | libcerror_error_set( |
200 | 0 | error, |
201 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
202 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
203 | 0 | "%s: unsupported $SDS index value size: %" PRIzd "\n", |
204 | 0 | function, |
205 | 0 | data_size ); |
206 | |
|
207 | 0 | return( -1 ); |
208 | 0 | } |
209 | | #if defined( HAVE_DEBUG_OUTPUT ) |
210 | | if( libcnotify_verbose != 0 ) |
211 | | { |
212 | | byte_stream_copy_to_uint32_little_endian( |
213 | | ( (fsntfs_secure_index_value_t *) data )->hash, |
214 | | value_32bit ); |
215 | | libcnotify_printf( |
216 | | "%s: hash\t\t\t\t: 0x%08" PRIx32 "\n", |
217 | | function, |
218 | | value_32bit ); |
219 | | |
220 | | byte_stream_copy_to_uint32_little_endian( |
221 | | ( (fsntfs_secure_index_value_t *) data )->identifier, |
222 | | value_32bit ); |
223 | | libcnotify_printf( |
224 | | "%s: identifier\t\t\t\t: %" PRIu32 "\n", |
225 | | function, |
226 | | value_32bit ); |
227 | | |
228 | | byte_stream_copy_to_uint64_little_endian( |
229 | | ( (fsntfs_secure_index_value_t *) data )->data_offset, |
230 | | value_64bit ); |
231 | | libcnotify_printf( |
232 | | "%s: data offset\t\t\t: 0x%08" PRIx64 "\n", |
233 | | function, |
234 | | value_64bit ); |
235 | | |
236 | | byte_stream_copy_to_uint32_little_endian( |
237 | | ( (fsntfs_secure_index_value_t *) data )->data_size, |
238 | | value_32bit ); |
239 | | libcnotify_printf( |
240 | | "%s: data size\t\t\t\t: %" PRIu32 "\n", |
241 | | function, |
242 | | value_32bit ); |
243 | | |
244 | | libcnotify_printf( |
245 | | "\n" ); |
246 | | } |
247 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
248 | | |
249 | 0 | return( 1 ); |
250 | 0 | } |
251 | | |