/src/libnk2/libnk2/libnk2_file_footer.c
Line | Count | Source |
1 | | /* |
2 | | * File footer functions |
3 | | * |
4 | | * Copyright (C) 2009-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 "libnk2_debug.h" |
28 | | #include "libnk2_file_footer.h" |
29 | | #include "libnk2_libcerror.h" |
30 | | #include "libnk2_libcnotify.h" |
31 | | #include "libnk2_libfdatetime.h" |
32 | | |
33 | | #include "nk2_file_footer.h" |
34 | | |
35 | | /* Creates file footer |
36 | | * Make sure the value file_footer is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libnk2_file_footer_initialize( |
40 | | libnk2_file_footer_t **file_footer, |
41 | | libcerror_error_t **error ) |
42 | 14 | { |
43 | 14 | static char *function = "libnk2_file_footer_initialize"; |
44 | | |
45 | 14 | if( file_footer == 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 file footer.", |
52 | 0 | function ); |
53 | |
|
54 | 0 | return( -1 ); |
55 | 0 | } |
56 | 14 | if( *file_footer != 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 file footer value already set.", |
63 | 0 | function ); |
64 | |
|
65 | 0 | return( -1 ); |
66 | 0 | } |
67 | 14 | *file_footer = memory_allocate_structure( |
68 | 14 | libnk2_file_footer_t ); |
69 | | |
70 | 14 | if( *file_footer == 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 file footer.", |
77 | 0 | function ); |
78 | |
|
79 | 0 | goto on_error; |
80 | 0 | } |
81 | 14 | if( memory_set( |
82 | 14 | *file_footer, |
83 | 14 | 0, |
84 | 14 | sizeof( libnk2_file_footer_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 file footer.", |
91 | 0 | function ); |
92 | |
|
93 | 0 | goto on_error; |
94 | 0 | } |
95 | 14 | return( 1 ); |
96 | | |
97 | 0 | on_error: |
98 | 0 | if( *file_footer != NULL ) |
99 | 0 | { |
100 | 0 | memory_free( |
101 | 0 | *file_footer ); |
102 | |
|
103 | 0 | *file_footer = NULL; |
104 | 0 | } |
105 | 0 | return( -1 ); |
106 | 14 | } |
107 | | |
108 | | /* Frees file footer |
109 | | * Returns 1 if successful or -1 on error |
110 | | */ |
111 | | int libnk2_file_footer_free( |
112 | | libnk2_file_footer_t **file_footer, |
113 | | libcerror_error_t **error ) |
114 | 14 | { |
115 | 14 | static char *function = "libnk2_file_footer_free"; |
116 | | |
117 | 14 | if( file_footer == 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 file footer.", |
124 | 0 | function ); |
125 | |
|
126 | 0 | return( -1 ); |
127 | 0 | } |
128 | 14 | if( *file_footer != NULL ) |
129 | 14 | { |
130 | 14 | memory_free( |
131 | 14 | *file_footer ); |
132 | | |
133 | 14 | *file_footer = NULL; |
134 | 14 | } |
135 | 14 | return( 1 ); |
136 | 14 | } |
137 | | |
138 | | /* Reads the file footer data |
139 | | * Returns 1 if successful or -1 on error |
140 | | */ |
141 | | int libnk2_file_footer_read_data( |
142 | | libnk2_file_footer_t *file_footer, |
143 | | const uint8_t *data, |
144 | | size_t data_size, |
145 | | libcerror_error_t **error ) |
146 | 2 | { |
147 | 2 | static char *function = "libnk2_file_footer_read_data"; |
148 | | |
149 | | #if defined( HAVE_DEBUG_OUTPUT ) |
150 | | uint32_t value_32bit = 0; |
151 | | #endif |
152 | | |
153 | 2 | if( file_footer == 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 file footer.", |
160 | 0 | function ); |
161 | |
|
162 | 0 | return( -1 ); |
163 | 0 | } |
164 | 2 | 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 | 2 | if( data_size < sizeof( nk2_file_footer_t ) ) |
176 | 0 | { |
177 | 0 | libcerror_error_set( |
178 | 0 | error, |
179 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
180 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
181 | 0 | "%s: invalid data size value too small.", |
182 | 0 | function ); |
183 | |
|
184 | 0 | return( -1 ); |
185 | 0 | } |
186 | 2 | if( data_size > (size_t) SSIZE_MAX ) |
187 | 0 | { |
188 | 0 | libcerror_error_set( |
189 | 0 | error, |
190 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
191 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM, |
192 | 0 | "%s: invalid data size value exceeds maximum.", |
193 | 0 | function ); |
194 | |
|
195 | 0 | return( -1 ); |
196 | 0 | } |
197 | | #if defined( HAVE_DEBUG_OUTPUT ) |
198 | | if( libcnotify_verbose != 0 ) |
199 | | { |
200 | | libcnotify_printf( |
201 | | "%s: file footer:\n", |
202 | | function ); |
203 | | libcnotify_print_data( |
204 | | (uint8_t *) data, |
205 | | sizeof( nk2_file_footer_t ), |
206 | | 0 ); |
207 | | } |
208 | | #endif |
209 | 2 | byte_stream_copy_to_uint64_little_endian( |
210 | 2 | ( (nk2_file_footer_t *) data )->modification_time, |
211 | 2 | file_footer->modification_time ); |
212 | | |
213 | | #if defined( HAVE_DEBUG_OUTPUT ) |
214 | | if( libcnotify_verbose != 0 ) |
215 | | { |
216 | | byte_stream_copy_to_uint32_little_endian( |
217 | | ( (nk2_file_footer_t *) data )->unknown1, |
218 | | value_32bit ); |
219 | | libcnotify_printf( |
220 | | "%s: unknown1\t\t\t\t\t: 0x%08" PRIx32 "\n", |
221 | | function, |
222 | | value_32bit ); |
223 | | |
224 | | if( libnk2_debug_print_filetime_value( |
225 | | function, |
226 | | "modification time\t\t\t\t", |
227 | | ( (nk2_file_footer_t *) data )->modification_time, |
228 | | 8, |
229 | | LIBFDATETIME_ENDIAN_LITTLE, |
230 | | LIBFDATETIME_STRING_FORMAT_TYPE_CTIME | LIBFDATETIME_STRING_FORMAT_FLAG_DATE_TIME_NANO_SECONDS, |
231 | | error ) != 1 ) |
232 | | { |
233 | | libcerror_error_set( |
234 | | error, |
235 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
236 | | LIBCERROR_RUNTIME_ERROR_PRINT_FAILED, |
237 | | "%s: unable to print FILETIME value.", |
238 | | function ); |
239 | | |
240 | | return( -1 ); |
241 | | } |
242 | | libcnotify_printf( |
243 | | "\n" ); |
244 | | } |
245 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
246 | 2 | return( 1 ); |
247 | 2 | } |
248 | | |
249 | | /* Reads the file footer |
250 | | * Returns 1 if successful or -1 on error |
251 | | */ |
252 | | int libnk2_file_footer_read_file_io_handle( |
253 | | libnk2_file_footer_t *file_footer, |
254 | | libbfio_handle_t *file_io_handle, |
255 | | libcerror_error_t **error ) |
256 | 14 | { |
257 | 14 | uint8_t file_footer_data[ sizeof( nk2_file_footer_t ) ]; |
258 | | |
259 | 14 | static char *function = "libnk2_file_footer_read_file_io_handle"; |
260 | 14 | ssize_t read_count = 0; |
261 | | |
262 | 14 | if( file_footer == NULL ) |
263 | 0 | { |
264 | 0 | libcerror_error_set( |
265 | 0 | error, |
266 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
267 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
268 | 0 | "%s: invalid file footer.", |
269 | 0 | function ); |
270 | |
|
271 | 0 | return( -1 ); |
272 | 0 | } |
273 | 14 | read_count = libbfio_handle_read_buffer( |
274 | 14 | file_io_handle, |
275 | 14 | file_footer_data, |
276 | 14 | sizeof( nk2_file_footer_t ), |
277 | 14 | error ); |
278 | | |
279 | 14 | if( read_count != (ssize_t) sizeof( nk2_file_footer_t ) ) |
280 | 12 | { |
281 | 12 | libcerror_error_set( |
282 | 12 | error, |
283 | 12 | LIBCERROR_ERROR_DOMAIN_IO, |
284 | 12 | LIBCERROR_IO_ERROR_READ_FAILED, |
285 | 12 | "%s: unable to read file footer data.", |
286 | 12 | function ); |
287 | | |
288 | 12 | return( -1 ); |
289 | 12 | } |
290 | 2 | if( libnk2_file_footer_read_data( |
291 | 2 | file_footer, |
292 | 2 | file_footer_data, |
293 | 2 | sizeof( nk2_file_footer_t ), |
294 | 2 | error ) != 1 ) |
295 | 0 | { |
296 | 0 | libcerror_error_set( |
297 | 0 | error, |
298 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
299 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
300 | 0 | "%s: unable to read file footer.", |
301 | 0 | function ); |
302 | |
|
303 | 0 | return( -1 ); |
304 | 0 | } |
305 | 2 | return( 1 ); |
306 | 2 | } |
307 | | |