/src/libevtx/libfwevt/libfwevt_floating_point.c
Line | Count | Source |
1 | | /* |
2 | | * Floating point (IEEE 754) functions |
3 | | * |
4 | | * Copyright (C) 2011-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 <byte_stream.h> |
24 | | #include <types.h> |
25 | | |
26 | | #include "libfwevt_floating_point.h" |
27 | | #include "libfwevt_libcerror.h" |
28 | | |
29 | | /* Deterimes the size of the string of a 32-bit floating point |
30 | | * The string size includes the end of string character |
31 | | * Returns 1 if successful or -1 on error |
32 | | */ |
33 | | int libfwevt_float32_get_string_size( |
34 | | uint32_t value_32bit, |
35 | | size_t *string_size, |
36 | | libcerror_error_t **error ) |
37 | 989k | { |
38 | 989k | static char *function = "libfwevt_float32_get_string_size"; |
39 | 989k | uint8_t is_indeterminate = 0; |
40 | 989k | uint8_t is_infinite = 0; |
41 | 989k | uint8_t is_not_a_number = 0; |
42 | 989k | uint8_t is_negative = 0; |
43 | 989k | uint8_t number_of_characters = 0; |
44 | | |
45 | 989k | if( string_size == NULL ) |
46 | 0 | { |
47 | 0 | libcerror_error_set( |
48 | 0 | error, |
49 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
50 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
51 | 0 | "%s: invalid string size.", |
52 | 0 | function ); |
53 | |
|
54 | 0 | return( -1 ); |
55 | 0 | } |
56 | 989k | is_negative = (uint8_t) ( value_32bit >> 31 ); |
57 | | |
58 | 989k | if( is_negative != 0 ) |
59 | 282k | { |
60 | 282k | value_32bit &= ~( (uint32_t) 1 << 31 ); |
61 | 282k | } |
62 | 989k | if( value_32bit == 0x7f800000UL ) |
63 | 1.23k | { |
64 | 1.23k | is_infinite = 1; |
65 | 1.23k | } |
66 | 988k | else if( ( is_negative != 0 ) |
67 | 282k | && ( value_32bit == 0x7fc00000UL ) ) |
68 | 621 | { |
69 | 621 | is_indeterminate = 1; |
70 | 621 | } |
71 | 987k | else if( ( value_32bit >= 0x7f800001UL ) |
72 | 94.3k | && ( value_32bit <= 0x7fffffffUL ) ) |
73 | 94.3k | { |
74 | 94.3k | is_not_a_number = 1; |
75 | 94.3k | } |
76 | 989k | if( is_indeterminate != 0 ) |
77 | 621 | { |
78 | | /* "Ind" + end-of-string character */ |
79 | 621 | number_of_characters = 4; |
80 | 621 | } |
81 | 989k | else if( is_infinite != 0 ) |
82 | 1.23k | { |
83 | | /* "Inf" + end-of-string character */ |
84 | 1.23k | number_of_characters = 4; |
85 | 1.23k | } |
86 | 987k | else if( is_not_a_number != 0 ) |
87 | 94.3k | { |
88 | | /* "Nan" + end-of-string character */ |
89 | 94.3k | number_of_characters = 4; |
90 | 94.3k | } |
91 | 893k | else |
92 | 893k | { |
93 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
94 | 893k | if( is_negative != 0 ) |
95 | 187k | { |
96 | 187k | number_of_characters = 15; |
97 | 187k | } |
98 | 705k | else |
99 | 705k | { |
100 | 705k | number_of_characters = 14; |
101 | 705k | } |
102 | 893k | } |
103 | 989k | *string_size = number_of_characters; |
104 | | |
105 | 989k | return( 1 ); |
106 | 989k | } |
107 | | |
108 | | /* Copies a 32-bit floating point to an UTF-8 string |
109 | | * The string size should include the end of string character |
110 | | * Returns 1 if successful or -1 on error |
111 | | */ |
112 | | int libfwevt_float32_copy_to_utf8_string_with_index( |
113 | | uint32_t value_32bit, |
114 | | uint8_t *utf8_string, |
115 | | size_t utf8_string_size, |
116 | | size_t *utf8_string_index, |
117 | | libcerror_error_t **error ) |
118 | 279k | { |
119 | 279k | byte_stream_float32_t value_float32; |
120 | | |
121 | 279k | static char *function = "libfwevt_float32_copy_to_utf8_string_with_index"; |
122 | 279k | size_t safe_utf8_string_index = 0; |
123 | 279k | uint32_t divider = 0; |
124 | 279k | uint32_t value_fraction = 0; |
125 | 279k | int16_t exponent10 = 0; |
126 | 279k | int16_t exponent2 = 0; |
127 | 279k | uint8_t digit_index = 0; |
128 | 279k | uint8_t exponent_sign = 0; |
129 | 279k | uint8_t is_indeterminate = 0; |
130 | 279k | uint8_t is_infinite = 0; |
131 | 279k | uint8_t is_not_a_number = 0; |
132 | 279k | uint8_t is_negative = 0; |
133 | 279k | uint8_t number_of_characters = 0; |
134 | 279k | double exponent_value = 0.0; |
135 | 279k | double value_float = 0.0; |
136 | | |
137 | 279k | if( utf8_string == NULL ) |
138 | 0 | { |
139 | 0 | libcerror_error_set( |
140 | 0 | error, |
141 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
142 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
143 | 0 | "%s: invalid UTF-8 string.", |
144 | 0 | function ); |
145 | |
|
146 | 0 | return( -1 ); |
147 | 0 | } |
148 | 279k | if( utf8_string_size > (size_t) SSIZE_MAX ) |
149 | 0 | { |
150 | 0 | libcerror_error_set( |
151 | 0 | error, |
152 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
153 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
154 | 0 | "%s: invalid UTF-8 string size value exceeds maximum.", |
155 | 0 | function ); |
156 | |
|
157 | 0 | return( -1 ); |
158 | 0 | } |
159 | 279k | if( utf8_string_index == NULL ) |
160 | 0 | { |
161 | 0 | libcerror_error_set( |
162 | 0 | error, |
163 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
164 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
165 | 0 | "%s: invalid UTF-8 string index.", |
166 | 0 | function ); |
167 | |
|
168 | 0 | return( -1 ); |
169 | 0 | } |
170 | 279k | safe_utf8_string_index = *utf8_string_index; |
171 | | |
172 | 279k | is_negative = (uint8_t) ( value_32bit >> 31 ); |
173 | | |
174 | 279k | if( is_negative != 0 ) |
175 | 80.0k | { |
176 | 80.0k | value_32bit &= ~( (uint32_t) 1 << 31 ); |
177 | 80.0k | } |
178 | 279k | if( value_32bit == 0x7f800000UL ) |
179 | 525 | { |
180 | 525 | is_infinite = 1; |
181 | 525 | } |
182 | 279k | else if( ( is_negative != 0 ) |
183 | 80.0k | && ( value_32bit == 0x7fc00000UL ) ) |
184 | 201 | { |
185 | 201 | is_indeterminate = 1; |
186 | 201 | } |
187 | 279k | else if( ( value_32bit >= 0x7f800001UL ) |
188 | 27.8k | && ( value_32bit <= 0x7fffffffUL ) ) |
189 | 27.8k | { |
190 | 27.8k | is_not_a_number = 1; |
191 | 27.8k | } |
192 | 251k | else if( value_32bit != 0 ) |
193 | 197k | { |
194 | 197k | value_float32.integer = value_32bit; |
195 | 197k | value_float = (double) value_float32.floating_point; |
196 | | |
197 | 197k | exponent2 = (int16_t) ( value_32bit >> 23 ); |
198 | | |
199 | 197k | if( exponent2 == 0 ) |
200 | 59.1k | { |
201 | 59.1k | exponent2 = -126; |
202 | 59.1k | } |
203 | 138k | else |
204 | 138k | { |
205 | 138k | exponent2 -= 127; |
206 | 138k | } |
207 | 197k | } |
208 | 279k | if( is_indeterminate != 0 ) |
209 | 201 | { |
210 | | /* "Ind" + end-of-string character */ |
211 | 201 | number_of_characters = 4; |
212 | 201 | } |
213 | 279k | else if( is_infinite != 0 ) |
214 | 525 | { |
215 | | /* "Inf" + end-of-string character */ |
216 | 525 | number_of_characters = 4; |
217 | 525 | } |
218 | 279k | else if( is_not_a_number != 0 ) |
219 | 27.8k | { |
220 | | /* "Nan" + end-of-string character */ |
221 | 27.8k | number_of_characters = 4; |
222 | 27.8k | } |
223 | 251k | else |
224 | 251k | { |
225 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
226 | 251k | if( is_negative != 0 ) |
227 | 52.0k | { |
228 | 52.0k | number_of_characters = 15; |
229 | 52.0k | } |
230 | 199k | else |
231 | 199k | { |
232 | 199k | number_of_characters = 14; |
233 | 199k | } |
234 | 251k | } |
235 | 279k | if( ( number_of_characters > utf8_string_size ) |
236 | 279k | || ( safe_utf8_string_index > ( utf8_string_size - number_of_characters ) ) ) |
237 | 0 | { |
238 | 0 | libcerror_error_set( |
239 | 0 | error, |
240 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
241 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
242 | 0 | "%s: invalid UTF-8 string size value too small.", |
243 | 0 | function ); |
244 | |
|
245 | 0 | return( -1 ); |
246 | 0 | } |
247 | 279k | if( is_indeterminate != 0 ) |
248 | 201 | { |
249 | | /* "Ind" + end-of-string character */ |
250 | 201 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'I'; |
251 | 201 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'n'; |
252 | 201 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'd'; |
253 | 201 | } |
254 | 279k | else if( is_infinite != 0 ) |
255 | 525 | { |
256 | | /* "Inf" + end-of-string character */ |
257 | 525 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'I'; |
258 | 525 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'n'; |
259 | 525 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'f'; |
260 | 525 | } |
261 | 279k | else if( is_not_a_number != 0 ) |
262 | 27.8k | { |
263 | | /* "Nan" + end-of-string character */ |
264 | 27.8k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'N'; |
265 | 27.8k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'a'; |
266 | 27.8k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'N'; |
267 | 27.8k | } |
268 | 251k | else |
269 | 251k | { |
270 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
271 | 251k | if( is_negative != 0 ) |
272 | 52.0k | { |
273 | 52.0k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '-'; |
274 | 52.0k | } |
275 | 251k | if( exponent2 < 0 ) |
276 | 144k | { |
277 | 144k | exponent_sign = (uint8_t) '-'; |
278 | 144k | exponent2 *= -1; |
279 | 144k | } |
280 | 106k | else |
281 | 106k | { |
282 | 106k | exponent_sign = (uint8_t) '+'; |
283 | 106k | } |
284 | 251k | exponent_value = 1.0; |
285 | 251k | exponent10 = 0; |
286 | | |
287 | 19.3M | while( exponent2 > 0 ) |
288 | 19.0M | { |
289 | 19.0M | exponent_value *= 2; |
290 | 19.0M | exponent2--; |
291 | | |
292 | 19.0M | if( exponent_value >= 10.0 ) |
293 | 5.61M | { |
294 | 5.61M | exponent_value /= 10.0; |
295 | 5.61M | exponent10++; |
296 | | |
297 | 5.61M | if( exponent_sign == (uint8_t) '-' ) |
298 | 4.37M | { |
299 | 4.37M | value_float *= 10.0; |
300 | 4.37M | } |
301 | 1.24M | else |
302 | 1.24M | { |
303 | 1.24M | value_float /= 10.0; |
304 | 1.24M | } |
305 | 5.61M | } |
306 | 19.0M | } |
307 | 251k | if( value_float != 0.0 ) |
308 | 197k | { |
309 | 549k | while( ( value_float < 1.0 ) |
310 | 207k | || ( value_float >= 10.0 ) ) |
311 | 352k | { |
312 | 352k | exponent10++; |
313 | | |
314 | 352k | if( exponent_sign == (uint8_t) '-' ) |
315 | 342k | { |
316 | 342k | value_float *= 10; |
317 | 342k | } |
318 | 9.78k | else |
319 | 9.78k | { |
320 | 9.78k | value_float /= 10; |
321 | 9.78k | } |
322 | 352k | } |
323 | 197k | } |
324 | 251k | for( digit_index = 0; |
325 | 2.00M | digit_index < 7; |
326 | 1.75M | digit_index++ ) |
327 | 1.75M | { |
328 | 1.75M | value_fraction *= 10; |
329 | 1.75M | value_fraction += (uint8_t) value_float; |
330 | | |
331 | 1.75M | value_float -= (uint8_t) value_float; |
332 | 1.75M | value_float *= 10.0; |
333 | 1.75M | } |
334 | 251k | if( value_float >= 5.0 ) |
335 | 94.5k | { |
336 | 94.5k | value_fraction += 1; |
337 | 94.5k | } |
338 | 251k | divider = 1000000; |
339 | | |
340 | 251k | for( digit_index = 0; |
341 | 2.00M | digit_index < 7; |
342 | 1.75M | digit_index++ ) |
343 | 1.75M | { |
344 | 1.75M | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '0' + (uint8_t) ( value_fraction / divider ); |
345 | | |
346 | 1.75M | if( digit_index == 0 ) |
347 | 251k | { |
348 | 251k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '.'; |
349 | 251k | } |
350 | 1.75M | value_fraction %= divider; |
351 | 1.75M | divider /= 10; |
352 | 1.75M | } |
353 | 251k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'e'; |
354 | 251k | utf8_string[ safe_utf8_string_index++ ] = exponent_sign; |
355 | | |
356 | 251k | divider = 100; |
357 | | |
358 | 251k | for( digit_index = 0; |
359 | 1.00M | digit_index < 3; |
360 | 753k | digit_index++ ) |
361 | 753k | { |
362 | 753k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '0' + (uint8_t) ( exponent10 / divider ); |
363 | | |
364 | 753k | exponent10 %= divider; |
365 | 753k | divider /= 10; |
366 | 753k | } |
367 | 251k | } |
368 | 279k | utf8_string[ safe_utf8_string_index++ ] = 0; |
369 | | |
370 | 279k | *utf8_string_index = safe_utf8_string_index; |
371 | | |
372 | 279k | return( 1 ); |
373 | 279k | } |
374 | | |
375 | | /* Copies a 32-bit floating point to an UTF-16 string |
376 | | * The string size should include the end of string character |
377 | | * Returns 1 if successful or -1 on error |
378 | | */ |
379 | | int libfwevt_float32_copy_to_utf16_string_with_index( |
380 | | uint32_t value_32bit, |
381 | | uint16_t *utf16_string, |
382 | | size_t utf16_string_size, |
383 | | size_t *utf16_string_index, |
384 | | libcerror_error_t **error ) |
385 | 214k | { |
386 | 214k | byte_stream_float32_t value_float32; |
387 | | |
388 | 214k | static char *function = "libfwevt_float32_copy_to_utf16_string_with_index"; |
389 | 214k | size_t safe_utf16_string_index = 0; |
390 | 214k | uint32_t divider = 0; |
391 | 214k | uint32_t value_fraction = 0; |
392 | 214k | uint16_t exponent_sign = 0; |
393 | 214k | int16_t exponent10 = 0; |
394 | 214k | int16_t exponent2 = 0; |
395 | 214k | uint8_t digit_index = 0; |
396 | 214k | uint8_t is_indeterminate = 0; |
397 | 214k | uint8_t is_infinite = 0; |
398 | 214k | uint8_t is_not_a_number = 0; |
399 | 214k | uint8_t is_negative = 0; |
400 | 214k | uint8_t number_of_characters = 0; |
401 | 214k | double exponent_value = 0.0; |
402 | 214k | double value_float = 0.0; |
403 | | |
404 | 214k | if( utf16_string == NULL ) |
405 | 0 | { |
406 | 0 | libcerror_error_set( |
407 | 0 | error, |
408 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
409 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
410 | 0 | "%s: invalid UTF-16 string.", |
411 | 0 | function ); |
412 | |
|
413 | 0 | return( -1 ); |
414 | 0 | } |
415 | 214k | if( utf16_string_size > (size_t) SSIZE_MAX ) |
416 | 0 | { |
417 | 0 | libcerror_error_set( |
418 | 0 | error, |
419 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
420 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
421 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
422 | 0 | function ); |
423 | |
|
424 | 0 | return( -1 ); |
425 | 0 | } |
426 | 214k | if( utf16_string_index == NULL ) |
427 | 0 | { |
428 | 0 | libcerror_error_set( |
429 | 0 | error, |
430 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
431 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
432 | 0 | "%s: invalid UTF-16 string index.", |
433 | 0 | function ); |
434 | |
|
435 | 0 | return( -1 ); |
436 | 0 | } |
437 | 214k | safe_utf16_string_index = *utf16_string_index; |
438 | | |
439 | 214k | is_negative = (uint8_t) ( value_32bit >> 31 ); |
440 | | |
441 | 214k | if( is_negative != 0 ) |
442 | 60.9k | { |
443 | 60.9k | value_32bit &= ~( (uint32_t) 1 << 31 ); |
444 | 60.9k | } |
445 | 214k | if( value_32bit == 0x7f800000UL ) |
446 | 93 | { |
447 | 93 | is_infinite = 1; |
448 | 93 | } |
449 | 214k | else if( ( is_negative != 0 ) |
450 | 60.8k | && ( value_32bit == 0x7fc00000UL ) ) |
451 | 102 | { |
452 | 102 | is_indeterminate = 1; |
453 | 102 | } |
454 | 213k | else if( ( value_32bit >= 0x7f800001UL ) |
455 | 19.2k | && ( value_32bit <= 0x7fffffffUL ) ) |
456 | 19.2k | { |
457 | 19.2k | is_not_a_number = 1; |
458 | 19.2k | } |
459 | 194k | else if( value_32bit != 0 ) |
460 | 147k | { |
461 | 147k | value_float32.integer = value_32bit; |
462 | 147k | value_float = (double) value_float32.floating_point; |
463 | | |
464 | 147k | exponent2 = (int16_t) ( value_32bit >> 23 ); |
465 | | |
466 | 147k | if( exponent2 == 0 ) |
467 | 44.1k | { |
468 | 44.1k | exponent2 = -126; |
469 | 44.1k | } |
470 | 103k | else |
471 | 103k | { |
472 | 103k | exponent2 -= 127; |
473 | 103k | } |
474 | 147k | } |
475 | 214k | if( is_indeterminate != 0 ) |
476 | 102 | { |
477 | | /* "Ind" + end-of-string character */ |
478 | 102 | number_of_characters = 4; |
479 | 102 | } |
480 | 214k | else if( is_infinite != 0 ) |
481 | 93 | { |
482 | | /* "Inf" + end-of-string character */ |
483 | 93 | number_of_characters = 4; |
484 | 93 | } |
485 | 213k | else if( is_not_a_number != 0 ) |
486 | 19.2k | { |
487 | | /* "Nan" + end-of-string character */ |
488 | 19.2k | number_of_characters = 4; |
489 | 19.2k | } |
490 | 194k | else |
491 | 194k | { |
492 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
493 | 194k | if( is_negative != 0 ) |
494 | 41.5k | { |
495 | 41.5k | number_of_characters = 15; |
496 | 41.5k | } |
497 | 153k | else |
498 | 153k | { |
499 | 153k | number_of_characters = 14; |
500 | 153k | } |
501 | 194k | } |
502 | 214k | if( ( number_of_characters > utf16_string_size ) |
503 | 214k | || ( safe_utf16_string_index > ( utf16_string_size - number_of_characters ) ) ) |
504 | 0 | { |
505 | 0 | libcerror_error_set( |
506 | 0 | error, |
507 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
508 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
509 | 0 | "%s: invalid UTF-16 string size value too small.", |
510 | 0 | function ); |
511 | |
|
512 | 0 | return( -1 ); |
513 | 0 | } |
514 | 214k | if( is_indeterminate != 0 ) |
515 | 102 | { |
516 | | /* "Ind" + end-of-string character */ |
517 | 102 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'I'; |
518 | 102 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'n'; |
519 | 102 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'd'; |
520 | 102 | } |
521 | 214k | else if( is_infinite != 0 ) |
522 | 93 | { |
523 | | /* "Inf" + end-of-string character */ |
524 | 93 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'I'; |
525 | 93 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'n'; |
526 | 93 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'f'; |
527 | 93 | } |
528 | 213k | else if( is_not_a_number != 0 ) |
529 | 19.2k | { |
530 | | /* "Nan" + end-of-string character */ |
531 | 19.2k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'N'; |
532 | 19.2k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'a'; |
533 | 19.2k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'N'; |
534 | 19.2k | } |
535 | 194k | else |
536 | 194k | { |
537 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
538 | 194k | if( is_negative != 0 ) |
539 | 41.5k | { |
540 | 41.5k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '-'; |
541 | 41.5k | } |
542 | 194k | if( exponent2 < 0 ) |
543 | 106k | { |
544 | 106k | exponent_sign = (uint16_t) '-'; |
545 | 106k | exponent2 *= -1; |
546 | 106k | } |
547 | 88.6k | else |
548 | 88.6k | { |
549 | 88.6k | exponent_sign = (uint16_t) '+'; |
550 | 88.6k | } |
551 | 194k | exponent_value = 1.0; |
552 | 194k | exponent10 = 0; |
553 | | |
554 | 14.3M | while( exponent2 > 0 ) |
555 | 14.1M | { |
556 | 14.1M | exponent_value *= 2; |
557 | 14.1M | exponent2--; |
558 | | |
559 | 14.1M | if( exponent_value >= 10.0 ) |
560 | 4.16M | { |
561 | 4.16M | exponent_value /= 10.0; |
562 | 4.16M | exponent10++; |
563 | | |
564 | 4.16M | if( exponent_sign == (uint16_t) '-' ) |
565 | 3.18M | { |
566 | 3.18M | value_float *= 10.0; |
567 | 3.18M | } |
568 | 978k | else |
569 | 978k | { |
570 | 978k | value_float /= 10.0; |
571 | 978k | } |
572 | 4.16M | } |
573 | 14.1M | } |
574 | 194k | if( value_float != 0.0 ) |
575 | 147k | { |
576 | 416k | while( ( value_float < 1.0 ) |
577 | 156k | || ( value_float >= 10.0 ) ) |
578 | 269k | { |
579 | 269k | exponent10++; |
580 | | |
581 | 269k | if( exponent_sign == (uint16_t) '-' ) |
582 | 260k | { |
583 | 260k | value_float *= 10; |
584 | 260k | } |
585 | 8.78k | else |
586 | 8.78k | { |
587 | 8.78k | value_float /= 10; |
588 | 8.78k | } |
589 | 269k | } |
590 | 147k | } |
591 | 194k | for( digit_index = 0; |
592 | 1.55M | digit_index < 7; |
593 | 1.36M | digit_index++ ) |
594 | 1.36M | { |
595 | 1.36M | value_fraction *= 10; |
596 | 1.36M | value_fraction += (uint8_t) value_float; |
597 | | |
598 | 1.36M | value_float -= (uint8_t) value_float; |
599 | 1.36M | value_float *= 10.0; |
600 | 1.36M | } |
601 | 194k | if( value_float >= 5.0 ) |
602 | 74.4k | { |
603 | 74.4k | value_fraction += 1; |
604 | 74.4k | } |
605 | 194k | divider = 1000000; |
606 | | |
607 | 194k | for( digit_index = 0; |
608 | 1.55M | digit_index < 7; |
609 | 1.36M | digit_index++ ) |
610 | 1.36M | { |
611 | 1.36M | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '0' + (uint16_t) ( value_fraction / divider ); |
612 | | |
613 | 1.36M | if( digit_index == 0 ) |
614 | 194k | { |
615 | 194k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '.'; |
616 | 194k | } |
617 | 1.36M | value_fraction %= divider; |
618 | 1.36M | divider /= 10; |
619 | 1.36M | } |
620 | 194k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'e'; |
621 | 194k | utf16_string[ safe_utf16_string_index++ ] = exponent_sign; |
622 | | |
623 | 194k | divider = 100; |
624 | | |
625 | 194k | for( digit_index = 0; |
626 | 778k | digit_index < 3; |
627 | 583k | digit_index++ ) |
628 | 583k | { |
629 | 583k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '0' + (uint16_t) ( exponent10 / divider ); |
630 | | |
631 | 583k | exponent10 %= divider; |
632 | 583k | divider /= 10; |
633 | 583k | } |
634 | 194k | } |
635 | 214k | utf16_string[ safe_utf16_string_index++ ] = 0; |
636 | | |
637 | 214k | *utf16_string_index = safe_utf16_string_index; |
638 | | |
639 | 214k | return( 1 ); |
640 | 214k | } |
641 | | |
642 | | /* Deterimes the size of the string of a 64-bit floating point |
643 | | * The string size includes the end of string character |
644 | | * Returns 1 if successful or -1 on error |
645 | | */ |
646 | | int libfwevt_float64_get_string_size( |
647 | | uint64_t value_64bit, |
648 | | size_t *string_size, |
649 | | libcerror_error_t **error ) |
650 | 694k | { |
651 | 694k | static char *function = "libfwevt_float64_get_string_size"; |
652 | 694k | uint8_t is_indeterminate = 0; |
653 | 694k | uint8_t is_infinite = 0; |
654 | 694k | uint8_t is_not_a_number = 0; |
655 | 694k | uint8_t is_negative = 0; |
656 | 694k | uint8_t number_of_characters = 0; |
657 | | |
658 | 694k | if( string_size == NULL ) |
659 | 0 | { |
660 | 0 | libcerror_error_set( |
661 | 0 | error, |
662 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
663 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
664 | 0 | "%s: invalid string size.", |
665 | 0 | function ); |
666 | |
|
667 | 0 | return( -1 ); |
668 | 0 | } |
669 | 694k | is_negative = (uint8_t) ( value_64bit >> 63 ); |
670 | | |
671 | 694k | if( is_negative != 0 ) |
672 | 186k | { |
673 | 186k | value_64bit &= ~( (uint64_t) 1 << 63 ); |
674 | 186k | } |
675 | | #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) |
676 | | if( value_64bit == 0x7ff0000000000000UL ) |
677 | | #else |
678 | 694k | if( value_64bit == 0x7ff0000000000000ULL ) |
679 | 237 | #endif |
680 | 237 | { |
681 | 237 | is_infinite = 1; |
682 | 237 | } |
683 | | #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) |
684 | | else if( ( is_negative != 0 ) |
685 | | && ( value_64bit == 0x7ff8000000000000UL ) ) |
686 | | #else |
687 | 693k | else if( ( is_negative != 0 ) |
688 | 186k | && ( value_64bit == 0x7ff8000000000000ULL ) ) |
689 | 1.13k | #endif |
690 | 1.13k | { |
691 | 1.13k | is_indeterminate = 1; |
692 | 1.13k | } |
693 | | #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) |
694 | | else if( ( value_64bit >= 0x7ff0000000000001UL ) |
695 | | && ( value_64bit <= 0x7fffffffffffffffUL ) ) |
696 | | #else |
697 | 692k | else if( ( value_64bit >= 0x7ff0000000000001ULL ) |
698 | 75.9k | && ( value_64bit <= 0x7fffffffffffffffULL ) ) |
699 | 75.9k | #endif |
700 | 75.9k | { |
701 | 75.9k | is_not_a_number = 1; |
702 | 75.9k | } |
703 | 694k | if( is_indeterminate != 0 ) |
704 | 1.13k | { |
705 | | /* "Ind" + end-of-string character */ |
706 | 1.13k | number_of_characters = 4; |
707 | 1.13k | } |
708 | 692k | else if( is_infinite != 0 ) |
709 | 237 | { |
710 | | /* "Inf" + end-of-string character */ |
711 | 237 | number_of_characters = 4; |
712 | 237 | } |
713 | 692k | else if( is_not_a_number != 0 ) |
714 | 75.9k | { |
715 | | /* "Nan" + end-of-string character */ |
716 | 75.9k | number_of_characters = 4; |
717 | 75.9k | } |
718 | 616k | else |
719 | 616k | { |
720 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
721 | 616k | if( is_negative != 0 ) |
722 | 110k | { |
723 | 110k | number_of_characters = 15; |
724 | 110k | } |
725 | 505k | else |
726 | 505k | { |
727 | 505k | number_of_characters = 14; |
728 | 505k | } |
729 | 616k | } |
730 | 694k | *string_size = number_of_characters; |
731 | | |
732 | 694k | return( 1 ); |
733 | 694k | } |
734 | | |
735 | | /* Copies a 64-bit floating point to an UTF-8 string |
736 | | * The string size should include the end of string character |
737 | | * Returns 1 if successful or -1 on error |
738 | | */ |
739 | | int libfwevt_float64_copy_to_utf8_string_with_index( |
740 | | uint64_t value_64bit, |
741 | | uint8_t *utf8_string, |
742 | | size_t utf8_string_size, |
743 | | size_t *utf8_string_index, |
744 | | libcerror_error_t **error ) |
745 | 182k | { |
746 | 182k | byte_stream_float64_t value_float64; |
747 | | |
748 | 182k | static char *function = "libfwevt_float64_copy_to_utf8_string_with_index"; |
749 | 182k | size_t safe_utf8_string_index = 0; |
750 | 182k | uint64_t divider = 0; |
751 | 182k | uint64_t value_fraction = 0; |
752 | 182k | int16_t exponent10 = 0; |
753 | 182k | int16_t exponent2 = 0; |
754 | 182k | uint8_t digit_index = 0; |
755 | 182k | uint8_t exponent_sign = 0; |
756 | 182k | uint8_t is_indeterminate = 0; |
757 | 182k | uint8_t is_infinite = 0; |
758 | 182k | uint8_t is_not_a_number = 0; |
759 | 182k | uint8_t is_negative = 0; |
760 | 182k | uint8_t number_of_characters = 0; |
761 | 182k | double exponent_value = 0.0; |
762 | 182k | double value_float = 0.0; |
763 | | |
764 | 182k | if( utf8_string == NULL ) |
765 | 0 | { |
766 | 0 | libcerror_error_set( |
767 | 0 | error, |
768 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
769 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
770 | 0 | "%s: invalid UTF-8 string.", |
771 | 0 | function ); |
772 | |
|
773 | 0 | return( -1 ); |
774 | 0 | } |
775 | 182k | if( utf8_string_size > (size_t) SSIZE_MAX ) |
776 | 0 | { |
777 | 0 | libcerror_error_set( |
778 | 0 | error, |
779 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
780 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
781 | 0 | "%s: invalid UTF-8 string size value exceeds maximum.", |
782 | 0 | function ); |
783 | |
|
784 | 0 | return( -1 ); |
785 | 0 | } |
786 | 182k | if( utf8_string_index == NULL ) |
787 | 0 | { |
788 | 0 | libcerror_error_set( |
789 | 0 | error, |
790 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
791 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
792 | 0 | "%s: invalid UTF-8 string index.", |
793 | 0 | function ); |
794 | |
|
795 | 0 | return( -1 ); |
796 | 0 | } |
797 | 182k | safe_utf8_string_index = *utf8_string_index; |
798 | | |
799 | 182k | is_negative = (uint8_t) ( value_64bit >> 63 ); |
800 | | |
801 | 182k | if( is_negative != 0 ) |
802 | 49.5k | { |
803 | 49.5k | value_64bit &= ~( (uint64_t) 1 << 63 ); |
804 | 49.5k | } |
805 | | #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) |
806 | | if( value_64bit == 0x7ff0000000000000UL ) |
807 | | #else |
808 | 182k | if( value_64bit == 0x7ff0000000000000ULL ) |
809 | 81 | #endif |
810 | 81 | { |
811 | 81 | is_infinite = 1; |
812 | 81 | } |
813 | | #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) |
814 | | else if( ( is_negative != 0 ) |
815 | | && ( value_64bit == 0x7ff8000000000000UL ) ) |
816 | | #else |
817 | 182k | else if( ( is_negative != 0 ) |
818 | 49.4k | && ( value_64bit == 0x7ff8000000000000ULL ) ) |
819 | 303 | #endif |
820 | 303 | { |
821 | 303 | is_indeterminate = 1; |
822 | 303 | } |
823 | | #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) |
824 | | else if( ( value_64bit >= 0x7ff0000000000001UL ) |
825 | | && ( value_64bit <= 0x7fffffffffffffffUL ) ) |
826 | | #else |
827 | 182k | else if( ( value_64bit >= 0x7ff0000000000001ULL ) |
828 | 19.9k | && ( value_64bit <= 0x7fffffffffffffffULL ) ) |
829 | 19.9k | #endif |
830 | 19.9k | { |
831 | 19.9k | is_not_a_number = 1; |
832 | 19.9k | } |
833 | 162k | else if( value_64bit != 0 ) |
834 | 148k | { |
835 | 148k | value_float64.integer = value_64bit; |
836 | 148k | value_float = (double) value_float64.floating_point; |
837 | | |
838 | 148k | exponent2 = (int16_t) ( value_64bit >> 52 ); |
839 | | |
840 | 148k | if( exponent2 == 0 ) |
841 | 73.2k | { |
842 | 73.2k | exponent2 = -1023; |
843 | 73.2k | } |
844 | 75.1k | else |
845 | 75.1k | { |
846 | 75.1k | exponent2 -= 1023; |
847 | 75.1k | } |
848 | 148k | } |
849 | 182k | if( is_indeterminate != 0 ) |
850 | 303 | { |
851 | | /* "Ind" + end-of-string character */ |
852 | 303 | number_of_characters = 4; |
853 | 303 | } |
854 | 182k | else if( is_infinite != 0 ) |
855 | 81 | { |
856 | | /* "Inf" + end-of-string character */ |
857 | 81 | number_of_characters = 4; |
858 | 81 | } |
859 | 182k | else if( is_not_a_number != 0 ) |
860 | 19.9k | { |
861 | | /* "Nan" + end-of-string character */ |
862 | 19.9k | number_of_characters = 4; |
863 | 19.9k | } |
864 | 162k | else |
865 | 162k | { |
866 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
867 | 162k | if( is_negative != 0 ) |
868 | 29.6k | { |
869 | 29.6k | number_of_characters = 15; |
870 | 29.6k | } |
871 | 132k | else |
872 | 132k | { |
873 | 132k | number_of_characters = 14; |
874 | 132k | } |
875 | 162k | } |
876 | 182k | if( ( number_of_characters > utf8_string_size ) |
877 | 182k | || ( safe_utf8_string_index > ( utf8_string_size - number_of_characters ) ) ) |
878 | 0 | { |
879 | 0 | libcerror_error_set( |
880 | 0 | error, |
881 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
882 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
883 | 0 | "%s: invalid UTF-8 string size value too small.", |
884 | 0 | function ); |
885 | |
|
886 | 0 | return( -1 ); |
887 | 0 | } |
888 | 182k | if( is_indeterminate != 0 ) |
889 | 303 | { |
890 | | /* "Ind" + end-of-string character */ |
891 | 303 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'I'; |
892 | 303 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'n'; |
893 | 303 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'd'; |
894 | 303 | } |
895 | 182k | else if( is_infinite != 0 ) |
896 | 81 | { |
897 | | /* "Inf" + end-of-string character */ |
898 | 81 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'I'; |
899 | 81 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'n'; |
900 | 81 | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'f'; |
901 | 81 | } |
902 | 182k | else if( is_not_a_number != 0 ) |
903 | 19.9k | { |
904 | | /* "Nan" + end-of-string character */ |
905 | 19.9k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'N'; |
906 | 19.9k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'a'; |
907 | 19.9k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'N'; |
908 | 19.9k | } |
909 | 162k | else |
910 | 162k | { |
911 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
912 | 162k | if( is_negative != 0 ) |
913 | 29.6k | { |
914 | 29.6k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '-'; |
915 | 29.6k | } |
916 | 162k | if( exponent2 < 0 ) |
917 | 119k | { |
918 | 119k | exponent_sign = (uint8_t) '-'; |
919 | 119k | exponent2 *= -1; |
920 | 119k | } |
921 | 43.2k | else |
922 | 43.2k | { |
923 | 43.2k | exponent_sign = (uint8_t) '+'; |
924 | 43.2k | } |
925 | 162k | exponent_value = 1.0; |
926 | 162k | exponent10 = 0; |
927 | | |
928 | 136M | while( exponent2 > 0 ) |
929 | 136M | { |
930 | 136M | exponent_value *= 2; |
931 | 136M | exponent2--; |
932 | | |
933 | 136M | if( exponent_value >= 10.0 ) |
934 | 41.0M | { |
935 | 41.0M | exponent_value /= 10.0; |
936 | 41.0M | exponent10++; |
937 | | |
938 | 41.0M | if( exponent_sign == (uint8_t) '-' ) |
939 | 34.3M | { |
940 | 34.3M | value_float *= 10.0; |
941 | 34.3M | } |
942 | 6.65M | else |
943 | 6.65M | { |
944 | 6.65M | value_float /= 10.0; |
945 | 6.65M | } |
946 | 41.0M | } |
947 | 136M | } |
948 | 162k | if( value_float != 0.0 ) |
949 | 148k | { |
950 | 693k | while( ( value_float < 1.0 ) |
951 | 157k | || ( value_float >= 10.0 ) ) |
952 | 545k | { |
953 | 545k | exponent10++; |
954 | | |
955 | 545k | if( exponent_sign == (uint8_t) '-' ) |
956 | 536k | { |
957 | 536k | value_float *= 10; |
958 | 536k | } |
959 | 8.87k | else |
960 | 8.87k | { |
961 | 8.87k | value_float /= 10; |
962 | 8.87k | } |
963 | 545k | } |
964 | 148k | } |
965 | 162k | for( digit_index = 0; |
966 | 1.29M | digit_index < 7; |
967 | 1.13M | digit_index++ ) |
968 | 1.13M | { |
969 | 1.13M | value_fraction *= 10; |
970 | 1.13M | value_fraction += (uint8_t) value_float; |
971 | | |
972 | 1.13M | value_float -= (uint8_t) value_float; |
973 | 1.13M | value_float *= 10.0; |
974 | 1.13M | } |
975 | 162k | if( value_float >= 5.0 ) |
976 | 61.4k | { |
977 | 61.4k | value_fraction += 1; |
978 | 61.4k | } |
979 | 162k | divider = 1000000; |
980 | | |
981 | 162k | for( digit_index = 0; |
982 | 1.29M | digit_index < 7; |
983 | 1.13M | digit_index++ ) |
984 | 1.13M | { |
985 | 1.13M | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '0' + (uint8_t) ( value_fraction / divider ); |
986 | | |
987 | 1.13M | if( digit_index == 0 ) |
988 | 162k | { |
989 | 162k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '.'; |
990 | 162k | } |
991 | 1.13M | value_fraction %= divider; |
992 | 1.13M | divider /= 10; |
993 | 1.13M | } |
994 | 162k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'e'; |
995 | 162k | utf8_string[ safe_utf8_string_index++ ] = exponent_sign; |
996 | | |
997 | 162k | divider = 100; |
998 | | |
999 | 162k | for( digit_index = 0; |
1000 | 649k | digit_index < 3; |
1001 | 487k | digit_index++ ) |
1002 | 487k | { |
1003 | 487k | utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '0' + (uint8_t) ( exponent10 / divider ); |
1004 | | |
1005 | 487k | exponent10 %= divider; |
1006 | 487k | divider /= 10; |
1007 | 487k | } |
1008 | 162k | } |
1009 | 182k | utf8_string[ safe_utf8_string_index++ ] = 0; |
1010 | | |
1011 | 182k | *utf8_string_index = safe_utf8_string_index; |
1012 | | |
1013 | 182k | return( 1 ); |
1014 | 182k | } |
1015 | | |
1016 | | /* Copies a 64-bit floating point to an UTF-16 string |
1017 | | * The string size should include the end of string character |
1018 | | * Returns 1 if successful or -1 on error |
1019 | | */ |
1020 | | int libfwevt_float64_copy_to_utf16_string_with_index( |
1021 | | uint64_t value_64bit, |
1022 | | uint16_t *utf16_string, |
1023 | | size_t utf16_string_size, |
1024 | | size_t *utf16_string_index, |
1025 | | libcerror_error_t **error ) |
1026 | 164k | { |
1027 | 164k | byte_stream_float64_t value_float64; |
1028 | | |
1029 | 164k | static char *function = "libfwevt_float64_copy_to_utf16_string_with_index"; |
1030 | 164k | size_t safe_utf16_string_index = 0; |
1031 | 164k | uint64_t divider = 0; |
1032 | 164k | uint64_t value_fraction = 0; |
1033 | 164k | uint16_t exponent_sign = 0; |
1034 | 164k | int16_t exponent10 = 0; |
1035 | 164k | int16_t exponent2 = 0; |
1036 | 164k | uint8_t digit_index = 0; |
1037 | 164k | uint8_t is_indeterminate = 0; |
1038 | 164k | uint8_t is_infinite = 0; |
1039 | 164k | uint8_t is_not_a_number = 0; |
1040 | 164k | uint8_t is_negative = 0; |
1041 | 164k | uint8_t number_of_characters = 0; |
1042 | 164k | double exponent_value = 0.0; |
1043 | 164k | double value_float = 0.0; |
1044 | | |
1045 | 164k | if( utf16_string == NULL ) |
1046 | 0 | { |
1047 | 0 | libcerror_error_set( |
1048 | 0 | error, |
1049 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1050 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1051 | 0 | "%s: invalid UTF-16 string.", |
1052 | 0 | function ); |
1053 | |
|
1054 | 0 | return( -1 ); |
1055 | 0 | } |
1056 | 164k | if( utf16_string_size > (size_t) SSIZE_MAX ) |
1057 | 0 | { |
1058 | 0 | libcerror_error_set( |
1059 | 0 | error, |
1060 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1061 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1062 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
1063 | 0 | function ); |
1064 | |
|
1065 | 0 | return( -1 ); |
1066 | 0 | } |
1067 | 164k | if( utf16_string_index == NULL ) |
1068 | 0 | { |
1069 | 0 | libcerror_error_set( |
1070 | 0 | error, |
1071 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1072 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1073 | 0 | "%s: invalid UTF-16 string index.", |
1074 | 0 | function ); |
1075 | |
|
1076 | 0 | return( -1 ); |
1077 | 0 | } |
1078 | 164k | safe_utf16_string_index = *utf16_string_index; |
1079 | | |
1080 | 164k | is_negative = (uint8_t) ( value_64bit >> 63 ); |
1081 | | |
1082 | 164k | if( is_negative != 0 ) |
1083 | 43.6k | { |
1084 | 43.6k | value_64bit &= ~( (uint64_t) 1 << 63 ); |
1085 | 43.6k | } |
1086 | | #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) |
1087 | | if( value_64bit == 0x7ff0000000000000UL ) |
1088 | | #else |
1089 | 164k | if( value_64bit == 0x7ff0000000000000ULL ) |
1090 | 36 | #endif |
1091 | 36 | { |
1092 | 36 | is_infinite = 1; |
1093 | 36 | } |
1094 | | #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) |
1095 | | else if( ( is_negative != 0 ) |
1096 | | && ( value_64bit == 0x7ff8000000000000UL ) ) |
1097 | | #else |
1098 | 163k | else if( ( is_negative != 0 ) |
1099 | 43.5k | && ( value_64bit == 0x7ff8000000000000ULL ) ) |
1100 | 264 | #endif |
1101 | 264 | { |
1102 | 264 | is_indeterminate = 1; |
1103 | 264 | } |
1104 | | #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) |
1105 | | else if( ( value_64bit >= 0x7ff0000000000001UL ) |
1106 | | && ( value_64bit <= 0x7fffffffffffffffUL ) ) |
1107 | | #else |
1108 | 163k | else if( ( value_64bit >= 0x7ff0000000000001ULL ) |
1109 | 17.9k | && ( value_64bit <= 0x7fffffffffffffffULL ) ) |
1110 | 17.9k | #endif |
1111 | 17.9k | { |
1112 | 17.9k | is_not_a_number = 1; |
1113 | 17.9k | } |
1114 | 145k | else if( value_64bit != 0 ) |
1115 | 134k | { |
1116 | 134k | value_float64.integer = value_64bit; |
1117 | 134k | value_float = (double) value_float64.floating_point; |
1118 | | |
1119 | 134k | exponent2 = (int16_t) ( value_64bit >> 52 ); |
1120 | | |
1121 | 134k | if( exponent2 == 0 ) |
1122 | 69.1k | { |
1123 | 69.1k | exponent2 = -1023; |
1124 | 69.1k | } |
1125 | 65.2k | else |
1126 | 65.2k | { |
1127 | 65.2k | exponent2 -= 1023; |
1128 | 65.2k | } |
1129 | 134k | } |
1130 | 164k | if( is_indeterminate != 0 ) |
1131 | 264 | { |
1132 | | /* "Ind" + end-of-string character */ |
1133 | 264 | number_of_characters = 4; |
1134 | 264 | } |
1135 | 163k | else if( is_infinite != 0 ) |
1136 | 36 | { |
1137 | | /* "Inf" + end-of-string character */ |
1138 | 36 | number_of_characters = 4; |
1139 | 36 | } |
1140 | 163k | else if( is_not_a_number != 0 ) |
1141 | 17.9k | { |
1142 | | /* "Nan" + end-of-string character */ |
1143 | 17.9k | number_of_characters = 4; |
1144 | 17.9k | } |
1145 | 145k | else |
1146 | 145k | { |
1147 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
1148 | 145k | if( is_negative != 0 ) |
1149 | 25.7k | { |
1150 | 25.7k | number_of_characters = 15; |
1151 | 25.7k | } |
1152 | 119k | else |
1153 | 119k | { |
1154 | 119k | number_of_characters = 14; |
1155 | 119k | } |
1156 | 145k | } |
1157 | 164k | if( ( number_of_characters > utf16_string_size ) |
1158 | 164k | || ( safe_utf16_string_index > ( utf16_string_size - number_of_characters ) ) ) |
1159 | 0 | { |
1160 | 0 | libcerror_error_set( |
1161 | 0 | error, |
1162 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1163 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1164 | 0 | "%s: invalid UTF-16 string size value too small.", |
1165 | 0 | function ); |
1166 | |
|
1167 | 0 | return( -1 ); |
1168 | 0 | } |
1169 | 164k | if( is_indeterminate != 0 ) |
1170 | 264 | { |
1171 | | /* "Ind" + end-of-string character */ |
1172 | 264 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'I'; |
1173 | 264 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'n'; |
1174 | 264 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'd'; |
1175 | 264 | } |
1176 | 163k | else if( is_infinite != 0 ) |
1177 | 36 | { |
1178 | | /* "Inf" + end-of-string character */ |
1179 | 36 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'I'; |
1180 | 36 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'n'; |
1181 | 36 | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'f'; |
1182 | 36 | } |
1183 | 163k | else if( is_not_a_number != 0 ) |
1184 | 17.9k | { |
1185 | | /* "Nan" + end-of-string character */ |
1186 | 17.9k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'N'; |
1187 | 17.9k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'a'; |
1188 | 17.9k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'N'; |
1189 | 17.9k | } |
1190 | 145k | else |
1191 | 145k | { |
1192 | | /* "[-]0.000000e[+-]000" + end-of-string character */ |
1193 | 145k | if( is_negative != 0 ) |
1194 | 25.7k | { |
1195 | 25.7k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '-'; |
1196 | 25.7k | } |
1197 | 145k | if( exponent2 < 0 ) |
1198 | 109k | { |
1199 | 109k | exponent_sign = (uint16_t) '-'; |
1200 | 109k | exponent2 *= -1; |
1201 | 109k | } |
1202 | 36.2k | else |
1203 | 36.2k | { |
1204 | 36.2k | exponent_sign = (uint16_t) '+'; |
1205 | 36.2k | } |
1206 | 145k | exponent_value = 1.0; |
1207 | 145k | exponent10 = 0; |
1208 | | |
1209 | 124M | while( exponent2 > 0 ) |
1210 | 124M | { |
1211 | 124M | exponent_value *= 2; |
1212 | 124M | exponent2--; |
1213 | | |
1214 | 124M | if( exponent_value >= 10.0 ) |
1215 | 37.4M | { |
1216 | 37.4M | exponent_value /= 10.0; |
1217 | 37.4M | exponent10++; |
1218 | | |
1219 | 37.4M | if( exponent_sign == (uint16_t) '-' ) |
1220 | 31.7M | { |
1221 | 31.7M | value_float *= 10.0; |
1222 | 31.7M | } |
1223 | 5.72M | else |
1224 | 5.72M | { |
1225 | 5.72M | value_float /= 10.0; |
1226 | 5.72M | } |
1227 | 37.4M | } |
1228 | 124M | } |
1229 | 145k | if( value_float != 0.0 ) |
1230 | 134k | { |
1231 | 650k | while( ( value_float < 1.0 ) |
1232 | 141k | || ( value_float >= 10.0 ) ) |
1233 | 516k | { |
1234 | 516k | exponent10++; |
1235 | | |
1236 | 516k | if( exponent_sign == (uint16_t) '-' ) |
1237 | 508k | { |
1238 | 508k | value_float *= 10; |
1239 | 508k | } |
1240 | 7.20k | else |
1241 | 7.20k | { |
1242 | 7.20k | value_float /= 10; |
1243 | 7.20k | } |
1244 | 516k | } |
1245 | 134k | } |
1246 | 145k | for( digit_index = 0; |
1247 | 1.16M | digit_index < 7; |
1248 | 1.01M | digit_index++ ) |
1249 | 1.01M | { |
1250 | 1.01M | value_fraction *= 10; |
1251 | 1.01M | value_fraction += (uint8_t) value_float; |
1252 | | |
1253 | 1.01M | value_float -= (uint8_t) value_float; |
1254 | 1.01M | value_float *= 10.0; |
1255 | 1.01M | } |
1256 | 145k | if( value_float >= 5.0 ) |
1257 | 53.3k | { |
1258 | 53.3k | value_fraction += 1; |
1259 | 53.3k | } |
1260 | 145k | divider = 1000000; |
1261 | | |
1262 | 145k | for( digit_index = 0; |
1263 | 1.16M | digit_index < 7; |
1264 | 1.01M | digit_index++ ) |
1265 | 1.01M | { |
1266 | 1.01M | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '0' + (uint16_t) ( value_fraction / divider ); |
1267 | | |
1268 | 1.01M | if( digit_index == 0 ) |
1269 | 145k | { |
1270 | 145k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '.'; |
1271 | 145k | } |
1272 | 1.01M | value_fraction %= divider; |
1273 | 1.01M | divider /= 10; |
1274 | 1.01M | } |
1275 | 145k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'e'; |
1276 | 145k | utf16_string[ safe_utf16_string_index++ ] = exponent_sign; |
1277 | | |
1278 | 145k | divider = 100; |
1279 | | |
1280 | 145k | for( digit_index = 0; |
1281 | 582k | digit_index < 3; |
1282 | 437k | digit_index++ ) |
1283 | 437k | { |
1284 | 437k | utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '0' + (uint16_t) ( exponent10 / divider ); |
1285 | | |
1286 | 437k | exponent10 %= divider; |
1287 | 437k | divider /= 10; |
1288 | 437k | } |
1289 | 145k | } |
1290 | 164k | utf16_string[ safe_utf16_string_index++ ] = 0; |
1291 | | |
1292 | 164k | *utf16_string_index = safe_utf16_string_index; |
1293 | | |
1294 | 164k | return( 1 ); |
1295 | 164k | } |
1296 | | |