/src/libewf/libewf/libewf_serialized_string.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Serialized (file) object functions |
3 | | * |
4 | | * Copyright (C) 2006-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 "libewf_definitions.h" |
28 | | #include "libewf_libcerror.h" |
29 | | #include "libewf_libuna.h" |
30 | | #include "libewf_serialized_string.h" |
31 | | |
32 | | /* Creates a serialized string |
33 | | * Make sure the value serialized_string is referencing, is set to NULL |
34 | | * Returns 1 if successful or -1 on error |
35 | | */ |
36 | | int libewf_serialized_string_initialize( |
37 | | libewf_serialized_string_t **serialized_string, |
38 | | libcerror_error_t **error ) |
39 | 0 | { |
40 | 0 | static char *function = "libewf_serialized_string_initialize"; |
41 | |
|
42 | 0 | if( serialized_string == NULL ) |
43 | 0 | { |
44 | 0 | libcerror_error_set( |
45 | 0 | error, |
46 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
47 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
48 | 0 | "%s: invalid serialized string.", |
49 | 0 | function ); |
50 | |
|
51 | 0 | return( -1 ); |
52 | 0 | } |
53 | 0 | if( *serialized_string != NULL ) |
54 | 0 | { |
55 | 0 | libcerror_error_set( |
56 | 0 | error, |
57 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
58 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
59 | 0 | "%s: invalid serialized string value already set.", |
60 | 0 | function ); |
61 | |
|
62 | 0 | return( -1 ); |
63 | 0 | } |
64 | 0 | *serialized_string = memory_allocate_structure( |
65 | 0 | libewf_serialized_string_t ); |
66 | |
|
67 | 0 | if( *serialized_string == NULL ) |
68 | 0 | { |
69 | 0 | libcerror_error_set( |
70 | 0 | error, |
71 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
72 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
73 | 0 | "%s: unable to create serialized string.", |
74 | 0 | function ); |
75 | |
|
76 | 0 | goto on_error; |
77 | 0 | } |
78 | 0 | if( memory_set( |
79 | 0 | *serialized_string, |
80 | 0 | 0, |
81 | 0 | sizeof( libewf_serialized_string_t ) ) == NULL ) |
82 | 0 | { |
83 | 0 | libcerror_error_set( |
84 | 0 | error, |
85 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
86 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
87 | 0 | "%s: unable to clear serialized string.", |
88 | 0 | function ); |
89 | |
|
90 | 0 | goto on_error; |
91 | 0 | } |
92 | 0 | return( 1 ); |
93 | | |
94 | 0 | on_error: |
95 | 0 | if( *serialized_string != NULL ) |
96 | 0 | { |
97 | 0 | memory_free( |
98 | 0 | *serialized_string ); |
99 | |
|
100 | 0 | *serialized_string = NULL; |
101 | 0 | } |
102 | 0 | return( -1 ); |
103 | 0 | } |
104 | | |
105 | | /* Frees a serialized string |
106 | | * Returns 1 if successful or -1 on error |
107 | | */ |
108 | | int libewf_serialized_string_free( |
109 | | libewf_serialized_string_t **serialized_string, |
110 | | libcerror_error_t **error ) |
111 | 0 | { |
112 | 0 | static char *function = "libewf_serialized_string_free"; |
113 | |
|
114 | 0 | if( serialized_string == NULL ) |
115 | 0 | { |
116 | 0 | libcerror_error_set( |
117 | 0 | error, |
118 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
119 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
120 | 0 | "%s: invalid serialized string.", |
121 | 0 | function ); |
122 | |
|
123 | 0 | return( -1 ); |
124 | 0 | } |
125 | 0 | if( *serialized_string != NULL ) |
126 | 0 | { |
127 | 0 | if( ( *serialized_string )->data != NULL ) |
128 | 0 | { |
129 | 0 | memory_free( |
130 | 0 | ( *serialized_string )->data ); |
131 | 0 | } |
132 | 0 | memory_free( |
133 | 0 | *serialized_string ); |
134 | |
|
135 | 0 | *serialized_string = NULL; |
136 | 0 | } |
137 | 0 | return( 1 ); |
138 | 0 | } |
139 | | |
140 | | /* Clones the serialized string |
141 | | * Returns 1 if successful or -1 on error |
142 | | */ |
143 | | int libewf_serialized_string_clone( |
144 | | libewf_serialized_string_t **destination_serialized_string, |
145 | | libewf_serialized_string_t *source_serialized_string, |
146 | | libcerror_error_t **error ) |
147 | 0 | { |
148 | 0 | static char *function = "libewf_serialized_string_clone"; |
149 | |
|
150 | 0 | if( destination_serialized_string == NULL ) |
151 | 0 | { |
152 | 0 | libcerror_error_set( |
153 | 0 | error, |
154 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
155 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
156 | 0 | "%s: invalid destination serialized string.", |
157 | 0 | function ); |
158 | |
|
159 | 0 | return( -1 ); |
160 | 0 | } |
161 | 0 | if( *destination_serialized_string != NULL ) |
162 | 0 | { |
163 | 0 | libcerror_error_set( |
164 | 0 | error, |
165 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
166 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
167 | 0 | "%s: invalid destination serialized string value already set.", |
168 | 0 | function ); |
169 | |
|
170 | 0 | return( -1 ); |
171 | 0 | } |
172 | 0 | if( source_serialized_string == NULL ) |
173 | 0 | { |
174 | 0 | *destination_serialized_string = NULL; |
175 | |
|
176 | 0 | return( 1 ); |
177 | 0 | } |
178 | 0 | *destination_serialized_string = memory_allocate_structure( |
179 | 0 | libewf_serialized_string_t ); |
180 | |
|
181 | 0 | if( *destination_serialized_string == NULL ) |
182 | 0 | { |
183 | 0 | libcerror_error_set( |
184 | 0 | error, |
185 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
186 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
187 | 0 | "%s: unable to create destination serialized string.", |
188 | 0 | function ); |
189 | |
|
190 | 0 | goto on_error; |
191 | 0 | } |
192 | 0 | if( memory_copy( |
193 | 0 | *destination_serialized_string, |
194 | 0 | source_serialized_string, |
195 | 0 | sizeof( libewf_serialized_string_t ) ) == NULL ) |
196 | 0 | { |
197 | 0 | libcerror_error_set( |
198 | 0 | error, |
199 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
200 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
201 | 0 | "%s: unable to copy source to destination serialized string.", |
202 | 0 | function ); |
203 | |
|
204 | 0 | memory_free( |
205 | 0 | *destination_serialized_string ); |
206 | |
|
207 | 0 | *destination_serialized_string = NULL; |
208 | |
|
209 | 0 | return( -1 ); |
210 | 0 | } |
211 | 0 | ( *destination_serialized_string )->data = NULL; |
212 | |
|
213 | 0 | if( source_serialized_string->data != NULL ) |
214 | 0 | { |
215 | 0 | ( *destination_serialized_string )->data = (uint8_t *) memory_allocate( |
216 | 0 | sizeof( uint8_t ) * source_serialized_string->data_size ); |
217 | |
|
218 | 0 | if( ( *destination_serialized_string )->data == NULL ) |
219 | 0 | { |
220 | 0 | libcerror_error_set( |
221 | 0 | error, |
222 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
223 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
224 | 0 | "%s: unable to create destination data.", |
225 | 0 | function ); |
226 | |
|
227 | 0 | goto on_error; |
228 | 0 | } |
229 | 0 | if( memory_copy( |
230 | 0 | ( *destination_serialized_string )->data, |
231 | 0 | source_serialized_string->data, |
232 | 0 | source_serialized_string->data_size ) == NULL ) |
233 | 0 | { |
234 | 0 | libcerror_error_set( |
235 | 0 | error, |
236 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
237 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
238 | 0 | "%s: unable to copy source to destination data.", |
239 | 0 | function ); |
240 | |
|
241 | 0 | goto on_error; |
242 | 0 | } |
243 | 0 | ( *destination_serialized_string )->data_size = source_serialized_string->data_size; |
244 | 0 | } |
245 | 0 | return( 1 ); |
246 | | |
247 | 0 | on_error: |
248 | 0 | if( *destination_serialized_string != NULL ) |
249 | 0 | { |
250 | 0 | if( ( *destination_serialized_string )->data != NULL ) |
251 | 0 | { |
252 | 0 | memory_free( |
253 | 0 | ( *destination_serialized_string )->data ); |
254 | 0 | } |
255 | 0 | memory_free( |
256 | 0 | *destination_serialized_string ); |
257 | |
|
258 | 0 | *destination_serialized_string = NULL; |
259 | 0 | } |
260 | 0 | return( -1 ); |
261 | 0 | } |
262 | | |
263 | | /* Reads a serialized string |
264 | | * Returns 1 if successful or -1 on error |
265 | | */ |
266 | | int libewf_serialized_string_read_data( |
267 | | libewf_serialized_string_t *serialized_string, |
268 | | const uint8_t *data, |
269 | | size_t data_size, |
270 | | int data_type, |
271 | | libcerror_error_t **error ) |
272 | 0 | { |
273 | 0 | static char *function = "libewf_serialized_string_read_data"; |
274 | |
|
275 | 0 | if( serialized_string == NULL ) |
276 | 0 | { |
277 | 0 | libcerror_error_set( |
278 | 0 | error, |
279 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
280 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
281 | 0 | "%s: invalid serialized string.", |
282 | 0 | function ); |
283 | |
|
284 | 0 | return( -1 ); |
285 | 0 | } |
286 | 0 | if( serialized_string->data != NULL ) |
287 | 0 | { |
288 | 0 | libcerror_error_set( |
289 | 0 | error, |
290 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
291 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
292 | 0 | "%s: invalid serialized string - data value already set.", |
293 | 0 | function ); |
294 | |
|
295 | 0 | return( -1 ); |
296 | 0 | } |
297 | 0 | if( data == NULL ) |
298 | 0 | { |
299 | 0 | libcerror_error_set( |
300 | 0 | error, |
301 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
302 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
303 | 0 | "%s: invalid data.", |
304 | 0 | function ); |
305 | |
|
306 | 0 | return( -1 ); |
307 | 0 | } |
308 | 0 | if( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) |
309 | 0 | { |
310 | 0 | libcerror_error_set( |
311 | 0 | error, |
312 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
313 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
314 | 0 | "%s: invalid data size value out of bounds.", |
315 | 0 | function ); |
316 | |
|
317 | 0 | return( -1 ); |
318 | 0 | } |
319 | 0 | if( ( data_size >= 1 ) |
320 | 0 | && ( data[ data_size - 1 ] == 0 ) ) |
321 | 0 | { |
322 | 0 | data_size -= 1; |
323 | 0 | } |
324 | 0 | serialized_string->data = (uint8_t *) memory_allocate( |
325 | 0 | sizeof( uint8_t ) * ( data_size + 1 ) ); |
326 | |
|
327 | 0 | if( serialized_string->data == NULL ) |
328 | 0 | { |
329 | 0 | libcerror_error_set( |
330 | 0 | error, |
331 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
332 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
333 | 0 | "%s: unable to create data.", |
334 | 0 | function ); |
335 | |
|
336 | 0 | goto on_error; |
337 | 0 | } |
338 | 0 | serialized_string->data_size = data_size + 1; |
339 | 0 | serialized_string->data_type = data_type; |
340 | |
|
341 | 0 | if( memory_copy( |
342 | 0 | serialized_string->data, |
343 | 0 | data, |
344 | 0 | data_size ) == NULL ) |
345 | 0 | { |
346 | 0 | libcerror_error_set( |
347 | 0 | error, |
348 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
349 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
350 | 0 | "%s: unable to copy data to string.", |
351 | 0 | function ); |
352 | |
|
353 | 0 | goto on_error; |
354 | 0 | } |
355 | 0 | serialized_string->data[ data_size ] = 0; |
356 | |
|
357 | 0 | return( 1 ); |
358 | | |
359 | 0 | on_error: |
360 | 0 | if( serialized_string->data != NULL ) |
361 | 0 | { |
362 | 0 | memory_free( |
363 | 0 | serialized_string->data ); |
364 | |
|
365 | 0 | serialized_string->data = NULL; |
366 | 0 | } |
367 | 0 | serialized_string->data_size = 0; |
368 | |
|
369 | 0 | return( -1 ); |
370 | 0 | } |
371 | | |
372 | | /* Reads a hexadecimal serialized string |
373 | | * Returns 1 if successful or -1 on error |
374 | | */ |
375 | | int libewf_serialized_string_read_hexadecimal_data( |
376 | | libewf_serialized_string_t *serialized_string, |
377 | | const uint8_t *data, |
378 | | size_t data_size, |
379 | | int data_type, |
380 | | libcerror_error_t **error ) |
381 | 0 | { |
382 | 0 | static char *function = "libewf_serialized_string_read_hexadecimal_data"; |
383 | 0 | size_t data_offset = 0; |
384 | 0 | size_t internal_data_offset = 0; |
385 | 0 | uint16_t character_value = 0; |
386 | 0 | int zero_values_only = 0; |
387 | |
|
388 | 0 | if( serialized_string == NULL ) |
389 | 0 | { |
390 | 0 | libcerror_error_set( |
391 | 0 | error, |
392 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
393 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
394 | 0 | "%s: invalid serialized string.", |
395 | 0 | function ); |
396 | |
|
397 | 0 | return( -1 ); |
398 | 0 | } |
399 | 0 | if( serialized_string->data != NULL ) |
400 | 0 | { |
401 | 0 | libcerror_error_set( |
402 | 0 | error, |
403 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
404 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
405 | 0 | "%s: invalid serialized string - data value already set.", |
406 | 0 | function ); |
407 | |
|
408 | 0 | return( -1 ); |
409 | 0 | } |
410 | 0 | if( data == NULL ) |
411 | 0 | { |
412 | 0 | libcerror_error_set( |
413 | 0 | error, |
414 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
415 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
416 | 0 | "%s: invalid data.", |
417 | 0 | function ); |
418 | |
|
419 | 0 | return( -1 ); |
420 | 0 | } |
421 | 0 | if( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) |
422 | 0 | { |
423 | 0 | libcerror_error_set( |
424 | 0 | error, |
425 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
426 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
427 | 0 | "%s: invalid data size value out of bounds.", |
428 | 0 | function ); |
429 | |
|
430 | 0 | return( -1 ); |
431 | 0 | } |
432 | 0 | if( data_type == LIBEWF_VALUE_DATA_TYPE_UTF8 ) |
433 | 0 | { |
434 | 0 | if( ( data_size >= 1 ) |
435 | 0 | && ( data[ data_size - 1 ] == 0 ) ) |
436 | 0 | { |
437 | 0 | data_size -= 1; |
438 | 0 | } |
439 | 0 | serialized_string->data_size = data_size + 1; |
440 | |
|
441 | 0 | serialized_string->data = (uint8_t *) memory_allocate( |
442 | 0 | sizeof( uint8_t ) * serialized_string->data_size ); |
443 | |
|
444 | 0 | if( serialized_string->data == NULL ) |
445 | 0 | { |
446 | 0 | libcerror_error_set( |
447 | 0 | error, |
448 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
449 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
450 | 0 | "%s: unable to create data.", |
451 | 0 | function ); |
452 | |
|
453 | 0 | goto on_error; |
454 | 0 | } |
455 | 0 | serialized_string->data_type = LIBEWF_VALUE_DATA_TYPE_UTF8; |
456 | |
|
457 | 0 | zero_values_only = 1; |
458 | |
|
459 | 0 | for( data_offset = 0; |
460 | 0 | data_offset < data_size; |
461 | 0 | data_offset++ ) |
462 | 0 | { |
463 | 0 | if( data[ data_offset ] != (uint8_t) '0' ) |
464 | 0 | { |
465 | 0 | zero_values_only = 0; |
466 | 0 | } |
467 | 0 | if( ( data[ data_offset ] >= (uint8_t) '0' ) |
468 | 0 | && ( data[ data_offset ] <= (uint8_t) '9' ) ) |
469 | 0 | { |
470 | 0 | serialized_string->data[ data_offset ] = data[ data_offset ]; |
471 | 0 | } |
472 | 0 | else if( ( data[ data_offset ] >= (uint8_t) 'A' ) |
473 | 0 | && ( data[ data_offset ] <= (uint8_t) 'F' ) ) |
474 | 0 | { |
475 | 0 | serialized_string->data[ data_offset ] = (uint8_t) ( 'a' - 'A' ) + data[ data_offset ]; |
476 | 0 | } |
477 | 0 | else if( ( data[ data_offset ] >= (uint8_t) 'a' ) |
478 | 0 | && ( data[ data_offset ] <= (uint8_t) 'f' ) ) |
479 | 0 | { |
480 | 0 | serialized_string->data[ data_offset ] = data[ data_offset ]; |
481 | 0 | } |
482 | 0 | else |
483 | 0 | { |
484 | 0 | libcerror_error_set( |
485 | 0 | error, |
486 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
487 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
488 | 0 | "%s: unsupported character in hexadecimal string at offset: %" PRIzd ".", |
489 | 0 | function, |
490 | 0 | data_offset ); |
491 | |
|
492 | 0 | goto on_error; |
493 | 0 | } |
494 | 0 | } |
495 | 0 | serialized_string->data[ data_offset ] = 0; |
496 | 0 | } |
497 | 0 | else |
498 | 0 | { |
499 | 0 | if( ( data_size >= 2 ) |
500 | 0 | && ( data[ data_size - 2 ] == 0 ) |
501 | 0 | && ( data[ data_size - 1 ] == 0 ) ) |
502 | 0 | { |
503 | 0 | data_size -= 2; |
504 | 0 | } |
505 | 0 | serialized_string->data_size = ( data_size / 2 ) + 1; |
506 | |
|
507 | 0 | serialized_string->data = (uint8_t *) memory_allocate( |
508 | 0 | sizeof( uint8_t ) * serialized_string->data_size ); |
509 | |
|
510 | 0 | if( serialized_string->data == NULL ) |
511 | 0 | { |
512 | 0 | libcerror_error_set( |
513 | 0 | error, |
514 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
515 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
516 | 0 | "%s: unable to create data.", |
517 | 0 | function ); |
518 | |
|
519 | 0 | goto on_error; |
520 | 0 | } |
521 | 0 | serialized_string->data_type = LIBEWF_VALUE_DATA_TYPE_UTF8; |
522 | |
|
523 | 0 | zero_values_only = 1; |
524 | |
|
525 | 0 | for( data_offset = 0; |
526 | 0 | data_offset < data_size; |
527 | 0 | data_offset += 2 ) |
528 | 0 | { |
529 | 0 | byte_stream_copy_to_uint16_little_endian( |
530 | 0 | &( data[ data_offset ] ), |
531 | 0 | character_value ); |
532 | |
|
533 | 0 | if( character_value != (uint8_t) '0' ) |
534 | 0 | { |
535 | 0 | zero_values_only = 0; |
536 | 0 | } |
537 | 0 | if( ( character_value >= (uint8_t) '0' ) |
538 | 0 | && ( character_value <= (uint8_t) '9' ) ) |
539 | 0 | { |
540 | 0 | serialized_string->data[ internal_data_offset ] = (uint8_t) character_value; |
541 | 0 | } |
542 | 0 | else if( ( character_value >= (uint8_t) 'A' ) |
543 | 0 | && ( character_value <= (uint8_t) 'F' ) ) |
544 | 0 | { |
545 | 0 | serialized_string->data[ internal_data_offset ] = (uint8_t) ( 'a' - 'A' ) + (uint8_t) character_value; |
546 | 0 | } |
547 | 0 | else if( ( character_value >= (uint8_t) 'a' ) |
548 | 0 | && ( character_value <= (uint8_t) 'f' ) ) |
549 | 0 | { |
550 | 0 | serialized_string->data[ internal_data_offset ] = (uint8_t) character_value; |
551 | 0 | } |
552 | 0 | else |
553 | 0 | { |
554 | 0 | libcerror_error_set( |
555 | 0 | error, |
556 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
557 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
558 | 0 | "%s: unsupported character in hexadecimal string at offset: %" PRIzd ".", |
559 | 0 | function, |
560 | 0 | data_offset ); |
561 | |
|
562 | 0 | goto on_error; |
563 | 0 | } |
564 | 0 | internal_data_offset++; |
565 | 0 | } |
566 | 0 | serialized_string->data[ internal_data_offset ] = 0; |
567 | 0 | } |
568 | 0 | if( zero_values_only != 0 ) |
569 | 0 | { |
570 | 0 | memory_free( |
571 | 0 | serialized_string->data ); |
572 | |
|
573 | 0 | serialized_string->data = NULL; |
574 | 0 | serialized_string->data_size = 0; |
575 | 0 | } |
576 | 0 | return( 1 ); |
577 | | |
578 | 0 | on_error: |
579 | 0 | if( serialized_string->data != NULL ) |
580 | 0 | { |
581 | 0 | memory_free( |
582 | 0 | serialized_string->data ); |
583 | |
|
584 | 0 | serialized_string->data = NULL; |
585 | 0 | } |
586 | 0 | serialized_string->data_size = 0; |
587 | |
|
588 | 0 | return( -1 ); |
589 | 0 | } |
590 | | |
591 | | /* Retrieves the size of the UTF-8 encoded string |
592 | | * The returned size includes the end of string character |
593 | | * Returns 1 if successful, 0 if data is not set or -1 on error |
594 | | */ |
595 | | int libewf_serialized_string_get_utf8_string_size( |
596 | | libewf_serialized_string_t *serialized_string, |
597 | | size_t *utf8_string_size, |
598 | | libcerror_error_t **error ) |
599 | 0 | { |
600 | 0 | static char *function = "libewf_serialized_string_get_utf8_string_size"; |
601 | 0 | int result = 0; |
602 | |
|
603 | 0 | if( serialized_string == NULL ) |
604 | 0 | { |
605 | 0 | libcerror_error_set( |
606 | 0 | error, |
607 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
608 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
609 | 0 | "%s: invalid serialized string.", |
610 | 0 | function ); |
611 | |
|
612 | 0 | return( -1 ); |
613 | 0 | } |
614 | 0 | if( utf8_string_size == NULL ) |
615 | 0 | { |
616 | 0 | libcerror_error_set( |
617 | 0 | error, |
618 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
619 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
620 | 0 | "%s: invalid UTF-8 string size.", |
621 | 0 | function ); |
622 | |
|
623 | 0 | return( -1 ); |
624 | 0 | } |
625 | 0 | if( ( serialized_string->data == NULL ) |
626 | 0 | || ( serialized_string->data_size == 0 ) ) |
627 | 0 | { |
628 | 0 | *utf8_string_size = 0; |
629 | |
|
630 | 0 | return( 0 ); |
631 | 0 | } |
632 | 0 | if( serialized_string->data_type == LIBEWF_VALUE_DATA_TYPE_UTF8 ) |
633 | 0 | { |
634 | 0 | result = libuna_utf8_string_size_from_utf8_stream( |
635 | 0 | serialized_string->data, |
636 | 0 | serialized_string->data_size, |
637 | 0 | utf8_string_size, |
638 | 0 | error ); |
639 | 0 | } |
640 | 0 | else |
641 | 0 | { |
642 | 0 | result = libuna_utf8_string_size_from_utf16_stream( |
643 | 0 | serialized_string->data, |
644 | 0 | serialized_string->data_size, |
645 | 0 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
646 | 0 | utf8_string_size, |
647 | 0 | error ); |
648 | 0 | } |
649 | 0 | if( result != 1 ) |
650 | 0 | { |
651 | 0 | libcerror_error_set( |
652 | 0 | error, |
653 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
654 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
655 | 0 | "%s: unable to retrieve UTF-8 string size.", |
656 | 0 | function ); |
657 | |
|
658 | 0 | return( -1 ); |
659 | 0 | } |
660 | 0 | return( 1 ); |
661 | 0 | } |
662 | | |
663 | | /* Retrieves the UTF-8 encoded string value |
664 | | * The size should include the end of string character |
665 | | * Returns 1 if successful, 0 if data is not set or -1 on error |
666 | | */ |
667 | | int libewf_serialized_string_get_utf8_string( |
668 | | libewf_serialized_string_t *serialized_string, |
669 | | uint8_t *utf8_string, |
670 | | size_t utf8_string_size, |
671 | | libcerror_error_t **error ) |
672 | 0 | { |
673 | 0 | static char *function = "libewf_serialized_string_get_utf8_string"; |
674 | 0 | int result = 0; |
675 | |
|
676 | 0 | if( serialized_string == 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 serialized string.", |
683 | 0 | function ); |
684 | |
|
685 | 0 | return( -1 ); |
686 | 0 | } |
687 | 0 | if( utf8_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-8 string.", |
694 | 0 | function ); |
695 | |
|
696 | 0 | return( -1 ); |
697 | 0 | } |
698 | 0 | if( utf8_string_size > (size_t) SSIZE_MAX ) |
699 | 0 | { |
700 | 0 | libcerror_error_set( |
701 | 0 | error, |
702 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
703 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
704 | 0 | "%s: invalid UTF-8 string size value exceeds maximum.", |
705 | 0 | function ); |
706 | |
|
707 | 0 | return( -1 ); |
708 | 0 | } |
709 | 0 | if( ( serialized_string->data == NULL ) |
710 | 0 | || ( serialized_string->data_size == 0 ) ) |
711 | 0 | { |
712 | 0 | if( utf8_string_size < 1 ) |
713 | 0 | { |
714 | 0 | libcerror_error_set( |
715 | 0 | error, |
716 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
717 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
718 | 0 | "%s: invalid UTF-8 string size value too small.", |
719 | 0 | function ); |
720 | |
|
721 | 0 | return( -1 ); |
722 | 0 | } |
723 | 0 | utf8_string[ 0 ] = 0; |
724 | |
|
725 | 0 | return( 0 ); |
726 | 0 | } |
727 | 0 | if( serialized_string->data_type == LIBEWF_VALUE_DATA_TYPE_UTF8 ) |
728 | 0 | { |
729 | 0 | result = libuna_utf8_string_copy_from_utf8_stream( |
730 | 0 | utf8_string, |
731 | 0 | utf8_string_size, |
732 | 0 | serialized_string->data, |
733 | 0 | serialized_string->data_size, |
734 | 0 | error ); |
735 | 0 | } |
736 | 0 | else |
737 | 0 | { |
738 | 0 | result = libuna_utf8_string_copy_from_utf16_stream( |
739 | 0 | utf8_string, |
740 | 0 | utf8_string_size, |
741 | 0 | serialized_string->data, |
742 | 0 | serialized_string->data_size, |
743 | 0 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
744 | 0 | error ); |
745 | 0 | } |
746 | 0 | if( result != 1 ) |
747 | 0 | { |
748 | 0 | libcerror_error_set( |
749 | 0 | error, |
750 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
751 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
752 | 0 | "%s: unable to copy string to UTF-8 string.", |
753 | 0 | function ); |
754 | |
|
755 | 0 | return( -1 ); |
756 | 0 | } |
757 | 0 | return( 1 ); |
758 | 0 | } |
759 | | |
760 | | /* Retrieves the size of the UTF-16 encoded string |
761 | | * The returned size includes the end of string character |
762 | | * Returns 1 if successful, 0 if data is not set or -1 on error |
763 | | */ |
764 | | int libewf_serialized_string_get_utf16_string_size( |
765 | | libewf_serialized_string_t *serialized_string, |
766 | | size_t *utf16_string_size, |
767 | | libcerror_error_t **error ) |
768 | 0 | { |
769 | 0 | static char *function = "libewf_serialized_string_get_utf16_string_size"; |
770 | 0 | int result = 0; |
771 | |
|
772 | 0 | if( serialized_string == NULL ) |
773 | 0 | { |
774 | 0 | libcerror_error_set( |
775 | 0 | error, |
776 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
777 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
778 | 0 | "%s: invalid serialized string.", |
779 | 0 | function ); |
780 | |
|
781 | 0 | return( -1 ); |
782 | 0 | } |
783 | 0 | if( utf16_string_size == NULL ) |
784 | 0 | { |
785 | 0 | libcerror_error_set( |
786 | 0 | error, |
787 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
788 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
789 | 0 | "%s: invalid UTF-16 string size.", |
790 | 0 | function ); |
791 | |
|
792 | 0 | return( -1 ); |
793 | 0 | } |
794 | 0 | if( ( serialized_string->data == NULL ) |
795 | 0 | || ( serialized_string->data_size == 0 ) ) |
796 | 0 | { |
797 | 0 | *utf16_string_size = 0; |
798 | |
|
799 | 0 | return( 0 ); |
800 | 0 | } |
801 | 0 | if( serialized_string->data_type == LIBEWF_VALUE_DATA_TYPE_UTF8 ) |
802 | 0 | { |
803 | 0 | result = libuna_utf16_string_size_from_utf8_stream( |
804 | 0 | serialized_string->data, |
805 | 0 | serialized_string->data_size, |
806 | 0 | utf16_string_size, |
807 | 0 | error ); |
808 | 0 | } |
809 | 0 | else |
810 | 0 | { |
811 | 0 | result = libuna_utf16_string_size_from_utf16_stream( |
812 | 0 | serialized_string->data, |
813 | 0 | serialized_string->data_size, |
814 | 0 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
815 | 0 | utf16_string_size, |
816 | 0 | error ); |
817 | 0 | } |
818 | 0 | if( result != 1 ) |
819 | 0 | { |
820 | 0 | libcerror_error_set( |
821 | 0 | error, |
822 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
823 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
824 | 0 | "%s: unable to retrieve UTF-16 string size.", |
825 | 0 | function ); |
826 | |
|
827 | 0 | return( -1 ); |
828 | 0 | } |
829 | 0 | return( 1 ); |
830 | 0 | } |
831 | | |
832 | | /* Retrieves the UTF-16 encoded string value |
833 | | * The size should include the end of string character |
834 | | * Returns 1 if successful, 0 if data is not set or -1 on error |
835 | | */ |
836 | | int libewf_serialized_string_get_utf16_string( |
837 | | libewf_serialized_string_t *serialized_string, |
838 | | uint16_t *utf16_string, |
839 | | size_t utf16_string_size, |
840 | | libcerror_error_t **error ) |
841 | 0 | { |
842 | 0 | static char *function = "libewf_serialized_string_get_utf16_string"; |
843 | 0 | int result = 0; |
844 | |
|
845 | 0 | if( serialized_string == NULL ) |
846 | 0 | { |
847 | 0 | libcerror_error_set( |
848 | 0 | error, |
849 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
850 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
851 | 0 | "%s: invalid serialized string.", |
852 | 0 | function ); |
853 | |
|
854 | 0 | return( -1 ); |
855 | 0 | } |
856 | 0 | if( utf16_string == NULL ) |
857 | 0 | { |
858 | 0 | libcerror_error_set( |
859 | 0 | error, |
860 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
861 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
862 | 0 | "%s: invalid UTF-16 string.", |
863 | 0 | function ); |
864 | |
|
865 | 0 | return( -1 ); |
866 | 0 | } |
867 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
868 | 0 | { |
869 | 0 | libcerror_error_set( |
870 | 0 | error, |
871 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
872 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
873 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
874 | 0 | function ); |
875 | |
|
876 | 0 | return( -1 ); |
877 | 0 | } |
878 | 0 | if( ( serialized_string->data == NULL ) |
879 | 0 | || ( serialized_string->data_size == 0 ) ) |
880 | 0 | { |
881 | 0 | if( utf16_string_size < 1 ) |
882 | 0 | { |
883 | 0 | libcerror_error_set( |
884 | 0 | error, |
885 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
886 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
887 | 0 | "%s: invalid UTF-16 string size value too small.", |
888 | 0 | function ); |
889 | |
|
890 | 0 | return( -1 ); |
891 | 0 | } |
892 | 0 | utf16_string[ 0 ] = 0; |
893 | |
|
894 | 0 | return( 0 ); |
895 | 0 | } |
896 | 0 | if( serialized_string->data_type == LIBEWF_VALUE_DATA_TYPE_UTF8 ) |
897 | 0 | { |
898 | 0 | result = libuna_utf16_string_copy_from_utf8_stream( |
899 | 0 | utf16_string, |
900 | 0 | utf16_string_size, |
901 | 0 | serialized_string->data, |
902 | 0 | serialized_string->data_size, |
903 | 0 | error ); |
904 | 0 | } |
905 | 0 | else |
906 | 0 | { |
907 | 0 | result = libuna_utf16_string_copy_from_utf16_stream( |
908 | 0 | utf16_string, |
909 | 0 | utf16_string_size, |
910 | 0 | serialized_string->data, |
911 | 0 | serialized_string->data_size, |
912 | 0 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
913 | 0 | error ); |
914 | 0 | } |
915 | 0 | if( result != 1 ) |
916 | 0 | { |
917 | 0 | libcerror_error_set( |
918 | 0 | error, |
919 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
920 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
921 | 0 | "%s: unable to copy string to UTF-16 string.", |
922 | 0 | function ); |
923 | |
|
924 | 0 | return( -1 ); |
925 | 0 | } |
926 | 0 | return( 1 ); |
927 | 0 | } |
928 | | |