/src/libfsfat/libfsfat/libfsfat_directory.c
Line | Count | Source |
1 | | /* |
2 | | * The directory functions |
3 | | * |
4 | | * Copyright (C) 2021-2026, 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 <memory.h> |
24 | | #include <types.h> |
25 | | |
26 | | #include "libfsfat_definitions.h" |
27 | | #include "libfsfat_directory.h" |
28 | | #include "libfsfat_directory_entry.h" |
29 | | #include "libfsfat_libcdata.h" |
30 | | #include "libfsfat_libcerror.h" |
31 | | #include "libfsfat_libuna.h" |
32 | | |
33 | | /* Creates a directory |
34 | | * Make sure the value directory is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libfsfat_directory_initialize( |
38 | | libfsfat_directory_t **directory, |
39 | | libcerror_error_t **error ) |
40 | 1.68k | { |
41 | 1.68k | static char *function = "libfsfat_directory_initialize"; |
42 | | |
43 | 1.68k | if( directory == 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 directory.", |
50 | 0 | function ); |
51 | |
|
52 | 0 | return( -1 ); |
53 | 0 | } |
54 | 1.68k | if( *directory != 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 directory value already set.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 1.68k | *directory = memory_allocate_structure( |
66 | 1.68k | libfsfat_directory_t ); |
67 | | |
68 | 1.68k | if( *directory == 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 directory.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | goto on_error; |
78 | 0 | } |
79 | 1.68k | if( memory_set( |
80 | 1.68k | *directory, |
81 | 1.68k | 0, |
82 | 1.68k | sizeof( libfsfat_directory_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 directory.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | memory_free( |
92 | 0 | *directory ); |
93 | |
|
94 | 0 | *directory = NULL; |
95 | |
|
96 | 0 | return( -1 ); |
97 | 0 | } |
98 | 1.68k | if( libcdata_array_initialize( |
99 | 1.68k | &( ( *directory )->entries_array ), |
100 | 1.68k | 0, |
101 | 1.68k | error ) != 1 ) |
102 | 0 | { |
103 | 0 | libcerror_error_set( |
104 | 0 | error, |
105 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
106 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
107 | 0 | "%s: unable to create entries array.", |
108 | 0 | function ); |
109 | |
|
110 | 0 | goto on_error; |
111 | 0 | } |
112 | 1.68k | if( libcdata_array_initialize( |
113 | 1.68k | &( ( *directory )->file_entries_array ), |
114 | 1.68k | 0, |
115 | 1.68k | error ) != 1 ) |
116 | 0 | { |
117 | 0 | libcerror_error_set( |
118 | 0 | error, |
119 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
120 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
121 | 0 | "%s: unable to create file entries array.", |
122 | 0 | function ); |
123 | |
|
124 | 0 | goto on_error; |
125 | 0 | } |
126 | 1.68k | return( 1 ); |
127 | | |
128 | 0 | on_error: |
129 | 0 | if( *directory != NULL ) |
130 | 0 | { |
131 | 0 | if( ( *directory )->entries_array != NULL ) |
132 | 0 | { |
133 | 0 | libcdata_array_free( |
134 | 0 | &( ( *directory )->entries_array ), |
135 | 0 | NULL, |
136 | 0 | NULL ); |
137 | 0 | } |
138 | 0 | memory_free( |
139 | 0 | *directory ); |
140 | |
|
141 | 0 | *directory = NULL; |
142 | 0 | } |
143 | 0 | return( -1 ); |
144 | 1.68k | } |
145 | | |
146 | | /* Frees a directory |
147 | | * Returns 1 if successful or -1 on error |
148 | | */ |
149 | | int libfsfat_directory_free( |
150 | | libfsfat_directory_t **directory, |
151 | | libcerror_error_t **error ) |
152 | 1.68k | { |
153 | 1.68k | static char *function = "libfsfat_directory_free"; |
154 | 1.68k | int result = 1; |
155 | | |
156 | 1.68k | if( directory == NULL ) |
157 | 0 | { |
158 | 0 | libcerror_error_set( |
159 | 0 | error, |
160 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
161 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
162 | 0 | "%s: invalid directory.", |
163 | 0 | function ); |
164 | |
|
165 | 0 | return( -1 ); |
166 | 0 | } |
167 | 1.68k | if( *directory != NULL ) |
168 | 1.68k | { |
169 | 1.68k | if( libcdata_array_free( |
170 | 1.68k | &( ( *directory )->file_entries_array ), |
171 | 1.68k | NULL, |
172 | 1.68k | error ) != 1 ) |
173 | 0 | { |
174 | 0 | libcerror_error_set( |
175 | 0 | error, |
176 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
177 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
178 | 0 | "%s: unable to free file entries array.", |
179 | 0 | function ); |
180 | |
|
181 | 0 | result = -1; |
182 | 0 | } |
183 | 1.68k | if( libcdata_array_free( |
184 | 1.68k | &( ( *directory )->entries_array ), |
185 | 1.68k | (int (*)(intptr_t **, libcerror_error_t **)) &libfsfat_directory_entry_free, |
186 | 1.68k | error ) != 1 ) |
187 | 0 | { |
188 | 0 | libcerror_error_set( |
189 | 0 | error, |
190 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
191 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
192 | 0 | "%s: unable to free entries array.", |
193 | 0 | function ); |
194 | |
|
195 | 0 | result = -1; |
196 | 0 | } |
197 | 1.68k | memory_free( |
198 | 1.68k | *directory ); |
199 | | |
200 | 1.68k | *directory = NULL; |
201 | 1.68k | } |
202 | 1.68k | return( result ); |
203 | 1.68k | } |
204 | | |
205 | | /* Retrieves the number of file entries |
206 | | * Returns 1 if successful or -1 on error |
207 | | */ |
208 | | int libfsfat_directory_get_number_of_file_entries( |
209 | | libfsfat_directory_t *directory, |
210 | | int *number_of_file_entries, |
211 | | libcerror_error_t **error ) |
212 | 252 | { |
213 | 252 | static char *function = "libfsfat_directory_get_number_of_file_entries"; |
214 | | |
215 | 252 | if( directory == NULL ) |
216 | 0 | { |
217 | 0 | libcerror_error_set( |
218 | 0 | error, |
219 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
220 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
221 | 0 | "%s: invalid directory.", |
222 | 0 | function ); |
223 | |
|
224 | 0 | return( -1 ); |
225 | 0 | } |
226 | 252 | if( libcdata_array_get_number_of_entries( |
227 | 252 | directory->file_entries_array, |
228 | 252 | number_of_file_entries, |
229 | 252 | error ) != 1 ) |
230 | 0 | { |
231 | 0 | libcerror_error_set( |
232 | 0 | error, |
233 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
234 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
235 | 0 | "%s: unable to retrieve number of file entries.", |
236 | 0 | function ); |
237 | |
|
238 | 0 | return( -1 ); |
239 | 0 | } |
240 | 252 | return( 1 ); |
241 | 252 | } |
242 | | |
243 | | /* Retrieves a specific file directory entry |
244 | | * Returns 1 if successful or -1 on error |
245 | | */ |
246 | | int libfsfat_directory_get_file_entry_by_index( |
247 | | libfsfat_directory_t *directory, |
248 | | int file_entry_index, |
249 | | libfsfat_directory_entry_t **directory_entry, |
250 | | libcerror_error_t **error ) |
251 | 219 | { |
252 | 219 | static char *function = "libfsfat_directory_get_file_entry_by_index"; |
253 | | |
254 | 219 | if( directory == NULL ) |
255 | 0 | { |
256 | 0 | libcerror_error_set( |
257 | 0 | error, |
258 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
259 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
260 | 0 | "%s: invalid directory.", |
261 | 0 | function ); |
262 | |
|
263 | 0 | return( -1 ); |
264 | 0 | } |
265 | 219 | if( libcdata_array_get_entry_by_index( |
266 | 219 | directory->file_entries_array, |
267 | 219 | file_entry_index, |
268 | 219 | (intptr_t **) directory_entry, |
269 | 219 | error ) != 1 ) |
270 | 0 | { |
271 | 0 | libcerror_error_set( |
272 | 0 | error, |
273 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
274 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
275 | 0 | "%s: unable to retrieve directory entry: %d from file entries array.", |
276 | 0 | function, |
277 | 0 | file_entry_index ); |
278 | |
|
279 | 0 | return( -1 ); |
280 | 0 | } |
281 | 219 | return( 1 ); |
282 | 219 | } |
283 | | |
284 | | /* Retrieves the file directory entry for an UTF-8 encoded name |
285 | | * Returns 1 if successful, 0 if not found or -1 on error |
286 | | */ |
287 | | int libfsfat_directory_get_file_entry_by_utf8_name( |
288 | | libfsfat_directory_t *directory, |
289 | | const uint8_t *utf8_string, |
290 | | size_t utf8_string_length, |
291 | | libfsfat_directory_entry_t **directory_entry, |
292 | | libcerror_error_t **error ) |
293 | 0 | { |
294 | 0 | libfsfat_directory_entry_t *safe_directory_entry = NULL; |
295 | 0 | static char *function = "libfsfat_directory_get_file_entry_by_utf8_name"; |
296 | 0 | int entry_index = 0; |
297 | 0 | int number_of_entries = 0; |
298 | 0 | int result = 0; |
299 | |
|
300 | 0 | if( directory == NULL ) |
301 | 0 | { |
302 | 0 | libcerror_error_set( |
303 | 0 | error, |
304 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
305 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
306 | 0 | "%s: invalid directory.", |
307 | 0 | function ); |
308 | |
|
309 | 0 | return( -1 ); |
310 | 0 | } |
311 | 0 | if( libcdata_array_get_number_of_entries( |
312 | 0 | directory->file_entries_array, |
313 | 0 | &number_of_entries, |
314 | 0 | error ) != 1 ) |
315 | 0 | { |
316 | 0 | libcerror_error_set( |
317 | 0 | error, |
318 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
319 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
320 | 0 | "%s: unable to retrieve number of file entries.", |
321 | 0 | function ); |
322 | |
|
323 | 0 | return( -1 ); |
324 | 0 | } |
325 | 0 | for( entry_index = 0; |
326 | 0 | entry_index < number_of_entries; |
327 | 0 | entry_index++ ) |
328 | 0 | { |
329 | 0 | if( libcdata_array_get_entry_by_index( |
330 | 0 | directory->file_entries_array, |
331 | 0 | entry_index, |
332 | 0 | (intptr_t **) &safe_directory_entry, |
333 | 0 | error ) != 1 ) |
334 | 0 | { |
335 | 0 | libcerror_error_set( |
336 | 0 | error, |
337 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
338 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
339 | 0 | "%s: unable to retrieve file entry: %d.", |
340 | 0 | function, |
341 | 0 | entry_index ); |
342 | |
|
343 | 0 | return( -1 ); |
344 | 0 | } |
345 | 0 | result = libfsfat_directory_entry_compare_with_utf8_string( |
346 | 0 | safe_directory_entry, |
347 | 0 | utf8_string, |
348 | 0 | utf8_string_length, |
349 | 0 | error ); |
350 | |
|
351 | 0 | if( result == -1 ) |
352 | 0 | { |
353 | 0 | libcerror_error_set( |
354 | 0 | error, |
355 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
356 | 0 | LIBCERROR_RUNTIME_ERROR_GENERIC, |
357 | 0 | "%s: unable to compare UTF-8 string with directory entry: %d.", |
358 | 0 | function, |
359 | 0 | entry_index ); |
360 | |
|
361 | 0 | return( -1 ); |
362 | 0 | } |
363 | 0 | else if( result == LIBUNA_COMPARE_EQUAL ) |
364 | 0 | { |
365 | 0 | *directory_entry = safe_directory_entry; |
366 | |
|
367 | 0 | return( 1 ); |
368 | 0 | } |
369 | 0 | } |
370 | 0 | *directory_entry = NULL; |
371 | |
|
372 | 0 | return( 0 ); |
373 | 0 | } |
374 | | |
375 | | /* Retrieves the file directory entry for an UTF-16 encoded name |
376 | | * Returns 1 if successful, 0 if not found or -1 on error |
377 | | */ |
378 | | int libfsfat_directory_get_file_entry_by_utf16_name( |
379 | | libfsfat_directory_t *directory, |
380 | | const uint16_t *utf16_string, |
381 | | size_t utf16_string_length, |
382 | | libfsfat_directory_entry_t **directory_entry, |
383 | | libcerror_error_t **error ) |
384 | 0 | { |
385 | 0 | libfsfat_directory_entry_t *safe_directory_entry = NULL; |
386 | 0 | static char *function = "libfsfat_directory_get_file_entry_by_utf16_name"; |
387 | 0 | int entry_index = 0; |
388 | 0 | int number_of_entries = 0; |
389 | 0 | int result = 0; |
390 | |
|
391 | 0 | if( directory == NULL ) |
392 | 0 | { |
393 | 0 | libcerror_error_set( |
394 | 0 | error, |
395 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
396 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
397 | 0 | "%s: invalid directory.", |
398 | 0 | function ); |
399 | |
|
400 | 0 | return( -1 ); |
401 | 0 | } |
402 | 0 | if( libcdata_array_get_number_of_entries( |
403 | 0 | directory->file_entries_array, |
404 | 0 | &number_of_entries, |
405 | 0 | error ) != 1 ) |
406 | 0 | { |
407 | 0 | libcerror_error_set( |
408 | 0 | error, |
409 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
410 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
411 | 0 | "%s: unable to retrieve number of file entries.", |
412 | 0 | function ); |
413 | |
|
414 | 0 | return( -1 ); |
415 | 0 | } |
416 | 0 | for( entry_index = 0; |
417 | 0 | entry_index < number_of_entries; |
418 | 0 | entry_index++ ) |
419 | 0 | { |
420 | 0 | if( libcdata_array_get_entry_by_index( |
421 | 0 | directory->file_entries_array, |
422 | 0 | entry_index, |
423 | 0 | (intptr_t **) &safe_directory_entry, |
424 | 0 | error ) != 1 ) |
425 | 0 | { |
426 | 0 | libcerror_error_set( |
427 | 0 | error, |
428 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
429 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
430 | 0 | "%s: unable to retrieve file entry: %d.", |
431 | 0 | function, |
432 | 0 | entry_index ); |
433 | |
|
434 | 0 | return( -1 ); |
435 | 0 | } |
436 | 0 | result = libfsfat_directory_entry_compare_with_utf16_string( |
437 | 0 | safe_directory_entry, |
438 | 0 | utf16_string, |
439 | 0 | utf16_string_length, |
440 | 0 | error ); |
441 | |
|
442 | 0 | if( result == -1 ) |
443 | 0 | { |
444 | 0 | libcerror_error_set( |
445 | 0 | error, |
446 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
447 | 0 | LIBCERROR_RUNTIME_ERROR_GENERIC, |
448 | 0 | "%s: unable to compare UTF-16 string with directory entry: %d.", |
449 | 0 | function, |
450 | 0 | entry_index ); |
451 | |
|
452 | 0 | return( -1 ); |
453 | 0 | } |
454 | 0 | else if( result == LIBUNA_COMPARE_EQUAL ) |
455 | 0 | { |
456 | 0 | *directory_entry = safe_directory_entry; |
457 | |
|
458 | 0 | return( 1 ); |
459 | 0 | } |
460 | 0 | } |
461 | 0 | *directory_entry = NULL; |
462 | |
|
463 | 0 | return( 0 ); |
464 | 0 | } |
465 | | |
466 | | /* Retrieves the size of the UTF-8 encoded volume label |
467 | | * The returned size includes the end of string character |
468 | | * Returns 1 if successful or -1 on error |
469 | | */ |
470 | | int libfsfat_directory_get_utf8_volume_label_size( |
471 | | libfsfat_directory_t *directory, |
472 | | size_t *utf8_string_size, |
473 | | libcerror_error_t **error ) |
474 | 0 | { |
475 | 0 | static char *function = "libfsfat_directory_get_utf8_volume_label_size"; |
476 | 0 | size_t safe_utf8_string_size = 0; |
477 | |
|
478 | 0 | if( directory == NULL ) |
479 | 0 | { |
480 | 0 | libcerror_error_set( |
481 | 0 | error, |
482 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
483 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
484 | 0 | "%s: invalid directory.", |
485 | 0 | function ); |
486 | |
|
487 | 0 | return( -1 ); |
488 | 0 | } |
489 | 0 | if( utf8_string_size == 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 UTF-8 string size.", |
496 | 0 | function ); |
497 | |
|
498 | 0 | return( -1 ); |
499 | 0 | } |
500 | 0 | if( directory->volume_label_entry != NULL ) |
501 | 0 | { |
502 | 0 | if( libfsfat_directory_entry_get_utf8_name_size( |
503 | 0 | directory->volume_label_entry, |
504 | 0 | &safe_utf8_string_size, |
505 | 0 | error ) != 1 ) |
506 | 0 | { |
507 | 0 | libcerror_error_set( |
508 | 0 | error, |
509 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
510 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
511 | 0 | "%s: unable to retrieve size of UTF-8 name from volume label entry.", |
512 | 0 | function ); |
513 | |
|
514 | 0 | return( -1 ); |
515 | 0 | } |
516 | 0 | } |
517 | 0 | if( safe_utf8_string_size < 1 ) |
518 | 0 | { |
519 | 0 | safe_utf8_string_size = 1; |
520 | 0 | } |
521 | 0 | *utf8_string_size = safe_utf8_string_size; |
522 | |
|
523 | 0 | return( 1 ); |
524 | 0 | } |
525 | | |
526 | | /* Retrieves the UTF-8 encoded volume label |
527 | | * The size should include the end of string character |
528 | | * Returns 1 if successful or -1 on error |
529 | | */ |
530 | | int libfsfat_directory_get_utf8_volume_label( |
531 | | libfsfat_directory_t *directory, |
532 | | uint8_t *utf8_string, |
533 | | size_t utf8_string_size, |
534 | | libcerror_error_t **error ) |
535 | 0 | { |
536 | 0 | static char *function = "libfsfat_directory_get_utf8_volume_label"; |
537 | |
|
538 | 0 | if( directory == NULL ) |
539 | 0 | { |
540 | 0 | libcerror_error_set( |
541 | 0 | error, |
542 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
543 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
544 | 0 | "%s: invalid directory.", |
545 | 0 | function ); |
546 | |
|
547 | 0 | return( -1 ); |
548 | 0 | } |
549 | 0 | if( utf8_string == NULL ) |
550 | 0 | { |
551 | 0 | libcerror_error_set( |
552 | 0 | error, |
553 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
554 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
555 | 0 | "%s: invalid UTF-8 string.", |
556 | 0 | function ); |
557 | |
|
558 | 0 | return( -1 ); |
559 | 0 | } |
560 | 0 | if( utf8_string_size == 0 ) |
561 | 0 | { |
562 | 0 | libcerror_error_set( |
563 | 0 | error, |
564 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
565 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
566 | 0 | "%s: invalid UTF-8 string size value too small.", |
567 | 0 | function ); |
568 | |
|
569 | 0 | return( -1 ); |
570 | 0 | } |
571 | 0 | if( utf8_string_size > (size_t) SSIZE_MAX ) |
572 | 0 | { |
573 | 0 | libcerror_error_set( |
574 | 0 | error, |
575 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
576 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
577 | 0 | "%s: invalid UTF-8 string size value exceeds maximum.", |
578 | 0 | function ); |
579 | |
|
580 | 0 | return( -1 ); |
581 | 0 | } |
582 | 0 | if( directory->volume_label_entry == NULL ) |
583 | 0 | { |
584 | 0 | utf8_string[ 0 ] = 0; |
585 | 0 | } |
586 | 0 | else if( libfsfat_directory_entry_get_utf8_name( |
587 | 0 | directory->volume_label_entry, |
588 | 0 | utf8_string, |
589 | 0 | utf8_string_size, |
590 | 0 | error ) != 1 ) |
591 | 0 | { |
592 | 0 | libcerror_error_set( |
593 | 0 | error, |
594 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
595 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
596 | 0 | "%s: unable to retrieve UTF-8 name from volume label entry.", |
597 | 0 | function ); |
598 | |
|
599 | 0 | return( -1 ); |
600 | 0 | } |
601 | 0 | return( 1 ); |
602 | 0 | } |
603 | | |
604 | | /* Retrieves the size of the UTF-16 encoded volume label |
605 | | * The returned size includes the end of string character |
606 | | * Returns 1 if successful or -1 on error |
607 | | */ |
608 | | int libfsfat_directory_get_utf16_volume_label_size( |
609 | | libfsfat_directory_t *directory, |
610 | | size_t *utf16_string_size, |
611 | | libcerror_error_t **error ) |
612 | 0 | { |
613 | 0 | static char *function = "libfsfat_directory_get_utf16_volume_label_size"; |
614 | 0 | size_t safe_utf16_string_size = 0; |
615 | |
|
616 | 0 | if( directory == NULL ) |
617 | 0 | { |
618 | 0 | libcerror_error_set( |
619 | 0 | error, |
620 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
621 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
622 | 0 | "%s: invalid directory.", |
623 | 0 | function ); |
624 | |
|
625 | 0 | return( -1 ); |
626 | 0 | } |
627 | 0 | if( utf16_string_size == NULL ) |
628 | 0 | { |
629 | 0 | libcerror_error_set( |
630 | 0 | error, |
631 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
632 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
633 | 0 | "%s: invalid UTF-16 string size.", |
634 | 0 | function ); |
635 | |
|
636 | 0 | return( -1 ); |
637 | 0 | } |
638 | 0 | if( directory->volume_label_entry != NULL ) |
639 | 0 | { |
640 | 0 | if( libfsfat_directory_entry_get_utf16_name_size( |
641 | 0 | directory->volume_label_entry, |
642 | 0 | &safe_utf16_string_size, |
643 | 0 | error ) != 1 ) |
644 | 0 | { |
645 | 0 | libcerror_error_set( |
646 | 0 | error, |
647 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
648 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
649 | 0 | "%s: unable to retrieve size of UTF-16 name from volume label entry.", |
650 | 0 | function ); |
651 | |
|
652 | 0 | return( -1 ); |
653 | 0 | } |
654 | 0 | } |
655 | 0 | if( safe_utf16_string_size < 1 ) |
656 | 0 | { |
657 | 0 | safe_utf16_string_size = 1; |
658 | 0 | } |
659 | 0 | *utf16_string_size = safe_utf16_string_size; |
660 | |
|
661 | 0 | return( 1 ); |
662 | 0 | } |
663 | | |
664 | | /* Retrieves the UTF-16 encoded volume label |
665 | | * The size should include the end of string character |
666 | | * Returns 1 if successful or -1 on error |
667 | | */ |
668 | | int libfsfat_directory_get_utf16_volume_label( |
669 | | libfsfat_directory_t *directory, |
670 | | uint16_t *utf16_string, |
671 | | size_t utf16_string_size, |
672 | | libcerror_error_t **error ) |
673 | 0 | { |
674 | 0 | static char *function = "libfsfat_directory_get_utf16_volume_label"; |
675 | |
|
676 | 0 | if( directory == NULL ) |
677 | 0 | { |
678 | 0 | libcerror_error_set( |
679 | 0 | error, |
680 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
681 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
682 | 0 | "%s: invalid directory.", |
683 | 0 | function ); |
684 | |
|
685 | 0 | return( -1 ); |
686 | 0 | } |
687 | 0 | if( utf16_string == NULL ) |
688 | 0 | { |
689 | 0 | libcerror_error_set( |
690 | 0 | error, |
691 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
692 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
693 | 0 | "%s: invalid UTF-16 string.", |
694 | 0 | function ); |
695 | |
|
696 | 0 | return( -1 ); |
697 | 0 | } |
698 | 0 | if( utf16_string_size == 0 ) |
699 | 0 | { |
700 | 0 | libcerror_error_set( |
701 | 0 | error, |
702 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
703 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
704 | 0 | "%s: invalid UTF-16 string size value too small.", |
705 | 0 | function ); |
706 | |
|
707 | 0 | return( -1 ); |
708 | 0 | } |
709 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
710 | 0 | { |
711 | 0 | libcerror_error_set( |
712 | 0 | error, |
713 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
714 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
715 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
716 | 0 | function ); |
717 | |
|
718 | 0 | return( -1 ); |
719 | 0 | } |
720 | 0 | if( directory->volume_label_entry == NULL ) |
721 | 0 | { |
722 | 0 | utf16_string[ 0 ] = 0; |
723 | 0 | } |
724 | 0 | else if( libfsfat_directory_entry_get_utf16_name( |
725 | 0 | directory->volume_label_entry, |
726 | 0 | utf16_string, |
727 | 0 | utf16_string_size, |
728 | 0 | error ) != 1 ) |
729 | 0 | { |
730 | 0 | libcerror_error_set( |
731 | 0 | error, |
732 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
733 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
734 | 0 | "%s: unable to retrieve UTF-16 name from volume label entry.", |
735 | 0 | function ); |
736 | |
|
737 | 0 | return( -1 ); |
738 | 0 | } |
739 | 0 | return( 1 ); |
740 | 0 | } |
741 | | |