/src/open62541_15/deps/parse_num.c
Line | Count | Source |
1 | | /* Originally released by the musl project (http://www.musl-libc.org/) under the |
2 | | * MIT license. Taken and adapted from the file src/stdlib/atoi.c |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to deal |
6 | | * in the Software without restriction, including without limitation the rights |
7 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
8 | | * copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
19 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
20 | | * THE SOFTWARE. |
21 | | */ |
22 | | |
23 | | #include "parse_num.h" |
24 | | |
25 | | #include <string.h> |
26 | | #include <stdlib.h> |
27 | | #include <errno.h> |
28 | | |
29 | | size_t |
30 | 37.2M | parseUInt64(const char *str, size_t size, uint64_t *result) { |
31 | 37.2M | size_t i = 0; |
32 | 37.2M | uint64_t n = 0, prev = 0; |
33 | | |
34 | | /* Hex */ |
35 | 37.2M | if(size > 2 && str[0] == '0' && (str[1] | 32) == 'x') { |
36 | 17.6k | i = 2; |
37 | 3.70M | for(; i < size; i++) { |
38 | 3.68M | uint8_t c = (uint8_t)str[i] | 32; |
39 | 3.68M | if(c >= '0' && c <= '9') |
40 | 3.38M | c = (uint8_t)(c - '0'); |
41 | 307k | else if(c >= 'a' && c <='f') |
42 | 304k | c = (uint8_t)(c - 'a' + 10); |
43 | 3.03k | else if(c >= 'A' && c <='F') |
44 | 0 | c = (uint8_t)(c - 'A' + 10); |
45 | 3.03k | else |
46 | 3.03k | break; |
47 | 3.68M | n = (n << 4) | (c & 0xF); |
48 | 3.68M | if(n < prev) /* Check for overflow */ |
49 | 49 | return 0; |
50 | 3.68M | prev = n; |
51 | 3.68M | } |
52 | 17.5k | *result = n; |
53 | 17.5k | return (i > 2) ? i : 0; /* 2 -> No digit was parsed */ |
54 | 17.6k | } |
55 | | |
56 | | /* Decimal */ |
57 | 95.1M | for(; i < size; i++) { |
58 | 58.0M | if(str[i] < '0' || str[i] > '9') |
59 | 169k | break; |
60 | | /* Fast multiplication: n*10 == (n*8) + (n*2) */ |
61 | 57.8M | n = (n << 3) + (n << 1) + (uint8_t)(str[i] - '0'); |
62 | 57.8M | if(n < prev) /* Check for overflow */ |
63 | 505 | return 0; |
64 | 57.8M | prev = n; |
65 | 57.8M | } |
66 | 37.2M | *result = n; |
67 | 37.2M | return i; |
68 | 37.2M | } |
69 | | |
70 | | size_t |
71 | 6.21M | parseInt64(const char *str, size_t size, int64_t *result) { |
72 | | /* Negative value? */ |
73 | 6.21M | size_t i = 0; |
74 | 6.21M | bool neg = false; |
75 | 6.21M | if(*str == '-' || *str == '+') { |
76 | 391k | neg = (*str == '-'); |
77 | 391k | i++; |
78 | 391k | } |
79 | | |
80 | | /* Parse as unsigned */ |
81 | 6.21M | uint64_t n = 0; |
82 | 6.21M | size_t len = parseUInt64(&str[i], size - i, &n); |
83 | 6.21M | if(len == 0) |
84 | 471 | return 0; |
85 | | |
86 | | /* Check for overflow, adjust and return */ |
87 | 6.21M | if(!neg) { |
88 | 5.84M | if(n > 9223372036854775807UL) |
89 | 250 | return 0; |
90 | 5.84M | *result = (int64_t)n; |
91 | 5.84M | } else { |
92 | | /* n is unsigned, so 9223372036854775808UL == 2^63 fits without |
93 | | * overflow. (int64_t)n would also fit for n in [0, 2^63], but |
94 | | * the negation -(int64_t)(2^63) is undefined because the result |
95 | | * (which is +2^63) cannot be represented. Use the unsigned |
96 | | * representation and reinterpret as int64_t instead. */ |
97 | 370k | if(n > 9223372036854775808UL) |
98 | 6 | return 0; |
99 | 370k | *result = (n == 9223372036854775808UL) |
100 | 370k | ? (int64_t)(-9223372036854775807LL - 1) |
101 | 370k | : -(int64_t)n; |
102 | 370k | } |
103 | 6.21M | return len + i; |
104 | 6.21M | } |
105 | | |
106 | 3.54M | size_t parseDouble(const char *str, size_t size, double *result) { |
107 | 3.54M | char buf[2000]; |
108 | 3.54M | if(size >= 2000) |
109 | 8 | return 0; |
110 | 3.54M | memcpy(buf, str, size); |
111 | 3.54M | buf[size] = 0; |
112 | 3.54M | errno = 0; |
113 | 3.54M | char *endptr; |
114 | 3.54M | *result = strtod(buf, &endptr); |
115 | 3.54M | if(errno != 0 && errno != ERANGE) |
116 | 0 | return 0; |
117 | 3.54M | return (uintptr_t)endptr - (uintptr_t)buf; |
118 | 3.54M | } |