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