/src/libfwsi/libfwsi/libfwsi_item_list.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Windows Shell Item list 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 "libfwsi_codepage.h" |
28 | | #include "libfwsi_item.h" |
29 | | #include "libfwsi_item_list.h" |
30 | | #include "libfwsi_libcdata.h" |
31 | | #include "libfwsi_libcerror.h" |
32 | | #include "libfwsi_libcnotify.h" |
33 | | #include "libfwsi_types.h" |
34 | | |
35 | | /* Creates an item list |
36 | | * Make sure the value X is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libfwsi_item_list_initialize( |
40 | | libfwsi_item_list_t **item_list, |
41 | | libcerror_error_t **error ) |
42 | 41 | { |
43 | 41 | libfwsi_internal_item_list_t *internal_item_list = NULL; |
44 | 41 | static char *function = "libfwsi_item_list_initialize"; |
45 | | |
46 | 41 | if( item_list == NULL ) |
47 | 0 | { |
48 | 0 | libcerror_error_set( |
49 | 0 | error, |
50 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
51 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
52 | 0 | "%s: invalid item list.", |
53 | 0 | function ); |
54 | |
|
55 | 0 | return( -1 ); |
56 | 0 | } |
57 | 41 | if( *item_list != NULL ) |
58 | 0 | { |
59 | 0 | libcerror_error_set( |
60 | 0 | error, |
61 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
62 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
63 | 0 | "%s: invalid item list value already set.", |
64 | 0 | function ); |
65 | |
|
66 | 0 | return( -1 ); |
67 | 0 | } |
68 | 41 | internal_item_list = memory_allocate_structure( |
69 | 41 | libfwsi_internal_item_list_t ); |
70 | | |
71 | 41 | if( internal_item_list == NULL ) |
72 | 0 | { |
73 | 0 | libcerror_error_set( |
74 | 0 | error, |
75 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
76 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
77 | 0 | "%s: unable to create item list.", |
78 | 0 | function ); |
79 | |
|
80 | 0 | goto on_error; |
81 | 0 | } |
82 | 41 | if( memory_set( |
83 | 41 | internal_item_list, |
84 | 41 | 0, |
85 | 41 | sizeof( libfwsi_internal_item_list_t ) ) == NULL ) |
86 | 0 | { |
87 | 0 | libcerror_error_set( |
88 | 0 | error, |
89 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
90 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
91 | 0 | "%s: unable to clear item list.", |
92 | 0 | function ); |
93 | |
|
94 | 0 | goto on_error; |
95 | 0 | } |
96 | 41 | if( libcdata_array_initialize( |
97 | 41 | &( internal_item_list->items_array ), |
98 | 41 | 0, |
99 | 41 | error ) != 1 ) |
100 | 0 | { |
101 | 0 | libcerror_error_set( |
102 | 0 | error, |
103 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
104 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
105 | 0 | "%s: unable to create items array.", |
106 | 0 | function ); |
107 | |
|
108 | 0 | goto on_error; |
109 | 0 | } |
110 | 41 | *item_list = (libfwsi_item_list_t *) internal_item_list; |
111 | | |
112 | 41 | return( 1 ); |
113 | | |
114 | 0 | on_error: |
115 | 0 | if( internal_item_list != NULL ) |
116 | 0 | { |
117 | 0 | memory_free( |
118 | 0 | internal_item_list ); |
119 | 0 | } |
120 | 0 | return( -1 ); |
121 | 41 | } |
122 | | |
123 | | /* Frees an item list |
124 | | * Returns 1 if successful or -1 on error |
125 | | */ |
126 | | int libfwsi_item_list_free( |
127 | | libfwsi_item_list_t **item_list, |
128 | | libcerror_error_t **error ) |
129 | 41 | { |
130 | 41 | libfwsi_internal_item_list_t *internal_item_list = NULL; |
131 | 41 | static char *function = "libfwsi_item_list_free"; |
132 | 41 | int result = 1; |
133 | | |
134 | 41 | if( item_list == NULL ) |
135 | 0 | { |
136 | 0 | libcerror_error_set( |
137 | 0 | error, |
138 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
139 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
140 | 0 | "%s: invalid item list.", |
141 | 0 | function ); |
142 | |
|
143 | 0 | return( -1 ); |
144 | 0 | } |
145 | 41 | if( *item_list != NULL ) |
146 | 41 | { |
147 | 41 | internal_item_list = (libfwsi_internal_item_list_t *) *item_list; |
148 | 41 | *item_list = NULL; |
149 | | |
150 | 41 | if( libcdata_array_free( |
151 | 41 | &( internal_item_list->items_array ), |
152 | 41 | (int (*)(intptr_t **, libcerror_error_t **)) &libfwsi_internal_item_free, |
153 | 41 | error ) != 1 ) |
154 | 0 | { |
155 | 0 | libcerror_error_set( |
156 | 0 | error, |
157 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
158 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
159 | 0 | "%s: unable to free the items array.", |
160 | 0 | function ); |
161 | |
|
162 | 0 | result = -1; |
163 | 0 | } |
164 | 41 | memory_free( |
165 | 41 | internal_item_list ); |
166 | 41 | } |
167 | 41 | return( result ); |
168 | 41 | } |
169 | | |
170 | | /* Copies a shell item list from a byte stream |
171 | | * Returns 1 if successful or -1 on error |
172 | | */ |
173 | | int libfwsi_item_list_copy_from_byte_stream( |
174 | | libfwsi_item_list_t *item_list, |
175 | | const uint8_t *byte_stream, |
176 | | size_t byte_stream_size, |
177 | | int ascii_codepage, |
178 | | libcerror_error_t **error ) |
179 | 41 | { |
180 | 41 | libfwsi_internal_item_t *internal_item = NULL; |
181 | 41 | libfwsi_internal_item_list_t *internal_item_list = NULL; |
182 | 41 | libfwsi_item_t *parent_item = NULL; |
183 | 41 | static char *function = "libfwsi_item_list_copy_from_byte_stream"; |
184 | 41 | uint16_t shell_item_size = 0; |
185 | 41 | int item_list_index = 0; |
186 | 41 | int shell_item_index = 0; |
187 | | |
188 | 41 | if( item_list == NULL ) |
189 | 0 | { |
190 | 0 | libcerror_error_set( |
191 | 0 | error, |
192 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
193 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
194 | 0 | "%s: invalid item list.", |
195 | 0 | function ); |
196 | |
|
197 | 0 | return( -1 ); |
198 | 0 | } |
199 | 41 | internal_item_list = (libfwsi_internal_item_list_t *) item_list; |
200 | | |
201 | 41 | if( byte_stream == NULL ) |
202 | 0 | { |
203 | 0 | libcerror_error_set( |
204 | 0 | error, |
205 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
206 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
207 | 0 | "%s: invalid byte stream.", |
208 | 0 | function ); |
209 | |
|
210 | 0 | return( -1 ); |
211 | 0 | } |
212 | 41 | if( byte_stream_size < 2 ) |
213 | 1 | { |
214 | 1 | libcerror_error_set( |
215 | 1 | error, |
216 | 1 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
217 | 1 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
218 | 1 | "%s: byte stream too small.", |
219 | 1 | function ); |
220 | | |
221 | 1 | return( -1 ); |
222 | 1 | } |
223 | 40 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
224 | 0 | { |
225 | 0 | libcerror_error_set( |
226 | 0 | error, |
227 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
228 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
229 | 0 | "%s: byte stream size exceeds maximum.", |
230 | 0 | function ); |
231 | |
|
232 | 0 | return( -1 ); |
233 | 0 | } |
234 | 40 | if( ( ascii_codepage != LIBFWSI_CODEPAGE_ASCII ) |
235 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_874 ) |
236 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_932 ) |
237 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_936 ) |
238 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_949 ) |
239 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_950 ) |
240 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_1250 ) |
241 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_1251 ) |
242 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_1252 ) |
243 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_1253 ) |
244 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_1254 ) |
245 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_1255 ) |
246 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_1256 ) |
247 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_1257 ) |
248 | 40 | && ( ascii_codepage != LIBFWSI_CODEPAGE_WINDOWS_1258 ) ) |
249 | 40 | { |
250 | 40 | libcerror_error_set( |
251 | 40 | error, |
252 | 40 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
253 | 40 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
254 | 40 | "%s: unsupported ASCII codepage.", |
255 | 40 | function ); |
256 | | |
257 | 40 | return( -1 ); |
258 | 40 | } |
259 | 0 | while( byte_stream_size >= 2 ) |
260 | 0 | { |
261 | 0 | byte_stream_copy_to_uint16_little_endian( |
262 | 0 | byte_stream, |
263 | 0 | shell_item_size ); |
264 | |
|
265 | | #if defined( HAVE_DEBUG_OUTPUT ) |
266 | | if( libcnotify_verbose != 0 ) |
267 | | { |
268 | | libcnotify_printf( |
269 | | "%s: shell item: %d size\t\t: %" PRIu16 "\n", |
270 | | function, |
271 | | shell_item_index, |
272 | | shell_item_size ); |
273 | | } |
274 | | #endif |
275 | 0 | if( shell_item_size > byte_stream_size ) |
276 | 0 | { |
277 | 0 | libcerror_error_set( |
278 | 0 | error, |
279 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
280 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
281 | 0 | "%s: invalid shell item size value out of bounds.", |
282 | 0 | function ); |
283 | |
|
284 | 0 | goto on_error; |
285 | 0 | } |
286 | 0 | if( shell_item_size == 0 ) |
287 | 0 | { |
288 | 0 | byte_stream += 2; |
289 | 0 | byte_stream_size -= 2; |
290 | 0 | internal_item_list->data_size += 2; |
291 | |
|
292 | 0 | break; |
293 | 0 | } |
294 | 0 | if( libfwsi_internal_item_initialize( |
295 | 0 | &internal_item, |
296 | 0 | error ) != 1 ) |
297 | 0 | { |
298 | 0 | libcerror_error_set( |
299 | 0 | error, |
300 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
301 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
302 | 0 | "%s: unable to create shell item.", |
303 | 0 | function ); |
304 | |
|
305 | 0 | goto on_error; |
306 | 0 | } |
307 | 0 | if( libfwsi_item_set_parent_item( |
308 | 0 | (libfwsi_item_t *) internal_item, |
309 | 0 | parent_item, |
310 | 0 | error ) != 1 ) |
311 | 0 | { |
312 | 0 | libcerror_error_set( |
313 | 0 | error, |
314 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
315 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
316 | 0 | "%s: unable to set parent shell item.", |
317 | 0 | function ); |
318 | |
|
319 | 0 | goto on_error; |
320 | 0 | } |
321 | 0 | if( internal_item == NULL ) |
322 | 0 | { |
323 | 0 | libcerror_error_set( |
324 | 0 | error, |
325 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
326 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
327 | 0 | "%s: missing shell item.", |
328 | 0 | function ); |
329 | |
|
330 | 0 | goto on_error; |
331 | 0 | } |
332 | 0 | internal_item->is_managed = 1; |
333 | |
|
334 | 0 | if( libfwsi_item_copy_from_byte_stream( |
335 | 0 | (libfwsi_item_t *) internal_item, |
336 | 0 | byte_stream, |
337 | 0 | byte_stream_size, |
338 | 0 | ascii_codepage, |
339 | 0 | error ) != 1 ) |
340 | 0 | { |
341 | 0 | libcerror_error_set( |
342 | 0 | error, |
343 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
344 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
345 | 0 | "%s: unable to copy byte stream to shell item.", |
346 | 0 | function ); |
347 | |
|
348 | 0 | goto on_error; |
349 | 0 | } |
350 | 0 | byte_stream += shell_item_size; |
351 | 0 | byte_stream_size -= shell_item_size; |
352 | 0 | internal_item_list->data_size += (size_t) shell_item_size; |
353 | |
|
354 | 0 | if( libcdata_array_append_entry( |
355 | 0 | internal_item_list->items_array, |
356 | 0 | &item_list_index, |
357 | 0 | (intptr_t *) internal_item, |
358 | 0 | error ) != 1 ) |
359 | 0 | { |
360 | 0 | libcerror_error_set( |
361 | 0 | error, |
362 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
363 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
364 | 0 | "%s: unable to append shell item: %d.", |
365 | 0 | function, |
366 | 0 | shell_item_index ); |
367 | |
|
368 | 0 | goto on_error; |
369 | 0 | } |
370 | 0 | parent_item = (libfwsi_item_t *) internal_item; |
371 | 0 | internal_item = NULL; |
372 | |
|
373 | 0 | shell_item_index++; |
374 | 0 | } |
375 | | #if defined( HAVE_DEBUG_OUTPUT ) |
376 | | if( libcnotify_verbose != 0 ) |
377 | | { |
378 | | libcnotify_printf( |
379 | | "\n" ); |
380 | | } |
381 | | #endif |
382 | 0 | internal_item_list->ascii_codepage = ascii_codepage; |
383 | |
|
384 | 0 | return( 1 ); |
385 | | |
386 | 0 | on_error: |
387 | 0 | if( internal_item != NULL ) |
388 | 0 | { |
389 | 0 | libfwsi_internal_item_free( |
390 | 0 | &internal_item, |
391 | 0 | NULL ); |
392 | 0 | } |
393 | 0 | return( -1 ); |
394 | 0 | } |
395 | | |
396 | | /* Retrieves the data size |
397 | | * Returns 1 if successful or -1 on error |
398 | | */ |
399 | | int libfwsi_item_list_get_data_size( |
400 | | libfwsi_item_list_t *item_list, |
401 | | size_t *data_size, |
402 | | libcerror_error_t **error ) |
403 | 0 | { |
404 | 0 | libfwsi_internal_item_list_t *internal_item_list = NULL; |
405 | 0 | static char *function = "libfwsi_item_list_get_data_size"; |
406 | |
|
407 | 0 | if( item_list == NULL ) |
408 | 0 | { |
409 | 0 | libcerror_error_set( |
410 | 0 | error, |
411 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
412 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
413 | 0 | "%s: invalid item list.", |
414 | 0 | function ); |
415 | |
|
416 | 0 | return( -1 ); |
417 | 0 | } |
418 | 0 | internal_item_list = (libfwsi_internal_item_list_t *) item_list; |
419 | |
|
420 | 0 | if( data_size == NULL ) |
421 | 0 | { |
422 | 0 | libcerror_error_set( |
423 | 0 | error, |
424 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
425 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
426 | 0 | "%s: invalid data size.", |
427 | 0 | function ); |
428 | |
|
429 | 0 | return( -1 ); |
430 | 0 | } |
431 | 0 | *data_size = internal_item_list->data_size; |
432 | |
|
433 | 0 | return( 1 ); |
434 | 0 | } |
435 | | |
436 | | /* Retrieves the number of items |
437 | | * Returns 1 if successful or -1 on error |
438 | | */ |
439 | | int libfwsi_item_list_get_number_of_items( |
440 | | libfwsi_item_list_t *item_list, |
441 | | int *number_of_items, |
442 | | libcerror_error_t **error ) |
443 | 0 | { |
444 | 0 | libfwsi_internal_item_list_t *internal_item_list = NULL; |
445 | 0 | static char *function = "libfwsi_item_list_get_number_of_items"; |
446 | |
|
447 | 0 | if( item_list == NULL ) |
448 | 0 | { |
449 | 0 | libcerror_error_set( |
450 | 0 | error, |
451 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
452 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
453 | 0 | "%s: invalid item list.", |
454 | 0 | function ); |
455 | |
|
456 | 0 | return( -1 ); |
457 | 0 | } |
458 | 0 | internal_item_list = (libfwsi_internal_item_list_t *) item_list; |
459 | |
|
460 | 0 | if( libcdata_array_get_number_of_entries( |
461 | 0 | internal_item_list->items_array, |
462 | 0 | number_of_items, |
463 | 0 | error ) != 1 ) |
464 | 0 | { |
465 | 0 | libcerror_error_set( |
466 | 0 | error, |
467 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
468 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
469 | 0 | "%s: unable to retrieve number of entries.", |
470 | 0 | function ); |
471 | |
|
472 | 0 | return( -1 ); |
473 | 0 | } |
474 | 0 | return( 1 ); |
475 | 0 | } |
476 | | |
477 | | /* Retrieves a specific item |
478 | | * Returns 1 if successful or -1 on error |
479 | | */ |
480 | | int libfwsi_item_list_get_item( |
481 | | libfwsi_item_list_t *item_list, |
482 | | int item_index, |
483 | | libfwsi_item_t **item, |
484 | | libcerror_error_t **error ) |
485 | 0 | { |
486 | 0 | libfwsi_internal_item_list_t *internal_item_list = NULL; |
487 | 0 | static char *function = "libfwsi_item_list_get_item"; |
488 | |
|
489 | 0 | if( item_list == NULL ) |
490 | 0 | { |
491 | 0 | libcerror_error_set( |
492 | 0 | error, |
493 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
494 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
495 | 0 | "%s: invalid item list.", |
496 | 0 | function ); |
497 | |
|
498 | 0 | return( -1 ); |
499 | 0 | } |
500 | 0 | internal_item_list = (libfwsi_internal_item_list_t *) item_list; |
501 | |
|
502 | 0 | if( libcdata_array_get_entry_by_index( |
503 | 0 | internal_item_list->items_array, |
504 | 0 | item_index, |
505 | 0 | (intptr_t **) item, |
506 | 0 | error ) != 1 ) |
507 | 0 | { |
508 | 0 | libcerror_error_set( |
509 | 0 | error, |
510 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
511 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
512 | 0 | "%s: unable to retrieve list item: %d.", |
513 | 0 | function, |
514 | 0 | item_index ); |
515 | |
|
516 | 0 | return( -1 ); |
517 | 0 | } |
518 | 0 | return( 1 ); |
519 | 0 | } |
520 | | |