BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file parse_number.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Numeric string parsing.
32 */
33  
34 #ifndef BADVPN_MISC_PARSE_NUMBER_H
35 #define BADVPN_MISC_PARSE_NUMBER_H
36  
37 #include <stdint.h>
38 #include <string.h>
39 #include <stddef.h>
40 #include <limits.h>
41  
42 #include <misc/memref.h>
43 #include <misc/debug.h>
44  
45 // public parsing functions
46 static int decode_decimal_digit (char c);
47 static int decode_hex_digit (char c);
48 static int parse_unsigned_integer (MemRef str, uintmax_t *out) WARN_UNUSED;
49 static int parse_unsigned_hex_integer (MemRef str, uintmax_t *out) WARN_UNUSED;
50 static int parse_signmag_integer (MemRef str, int *out_sign, uintmax_t *out_mag) WARN_UNUSED;
51  
52 // public generation functions
53 static int compute_decimal_repr_size (uintmax_t x);
54 static void generate_decimal_repr (uintmax_t x, char *out, int repr_size);
55 static int generate_decimal_repr_string (uintmax_t x, char *out);
56  
57 // implementation follows
58  
59 // decimal representation of UINTMAX_MAX
60 static const char parse_number__uintmax_max_str[] = "18446744073709551615";
61  
62 // make sure UINTMAX_MAX is what we think it is
63 static const char parse_number__uintmax_max_str_assert[(UINTMAX_MAX == UINTMAX_C(18446744073709551615)) ? 1 : -1];
64  
65 static int decode_decimal_digit (char c)
66 {
67 switch (c) {
68 case '0': return 0;
69 case '1': return 1;
70 case '2': return 2;
71 case '3': return 3;
72 case '4': return 4;
73 case '5': return 5;
74 case '6': return 6;
75 case '7': return 7;
76 case '8': return 8;
77 case '9': return 9;
78 }
79  
80 return -1;
81 }
82  
83 static int decode_hex_digit (char c)
84 {
85 switch (c) {
86 case '0': return 0;
87 case '1': return 1;
88 case '2': return 2;
89 case '3': return 3;
90 case '4': return 4;
91 case '5': return 5;
92 case '6': return 6;
93 case '7': return 7;
94 case '8': return 8;
95 case '9': return 9;
96 case 'A': case 'a': return 10;
97 case 'B': case 'b': return 11;
98 case 'C': case 'c': return 12;
99 case 'D': case 'd': return 13;
100 case 'E': case 'e': return 14;
101 case 'F': case 'f': return 15;
102 }
103  
104 return -1;
105 }
106  
107 static int parse__no_overflow (const char *str, size_t str_len, uintmax_t *out)
108 {
109 uintmax_t n = 0;
110  
111 while (str_len > 0) {
112 if (*str < '0' || *str > '9') {
113 return 0;
114 }
115  
116 n = 10 * n + (*str - '0');
117  
118 str++;
119 str_len--;
120 }
121  
122 *out = n;
123 return 1;
124 }
125  
126 int parse_unsigned_integer (MemRef str, uintmax_t *out)
127 {
128 // we do not allow empty strings
129 if (str.len == 0) {
130 return 0;
131 }
132  
133 // remove leading zeros
134 while (str.len > 0 && *str.ptr == '0') {
135 str.ptr++;
136 str.len--;
137 }
138  
139 // detect overflow
140 if (str.len > sizeof(parse_number__uintmax_max_str) - 1 ||
141 (str.len == sizeof(parse_number__uintmax_max_str) - 1 && memcmp(str.ptr, parse_number__uintmax_max_str, sizeof(parse_number__uintmax_max_str) - 1) > 0)) {
142 return 0;
143 }
144  
145 // will not overflow (but can still have invalid characters)
146 return parse__no_overflow(str.ptr, str.len, out);
147 }
148  
149 int parse_unsigned_hex_integer (MemRef str, uintmax_t *out)
150 {
151 uintmax_t n = 0;
152  
153 if (str.len == 0) {
154 return 0;
155 }
156  
157 while (str.len > 0) {
158 int digit = decode_hex_digit(*str.ptr);
159 if (digit < 0) {
160 return 0;
161 }
162  
163 if (n > UINTMAX_MAX / 16) {
164 return 0;
165 }
166 n *= 16;
167  
168 if (digit > UINTMAX_MAX - n) {
169 return 0;
170 }
171 n += digit;
172  
173 str.ptr++;
174 str.len--;
175 }
176  
177 *out = n;
178 return 1;
179 }
180  
181 int parse_signmag_integer (MemRef str, int *out_sign, uintmax_t *out_mag)
182 {
183 int sign = 1;
184 if (str.len > 0 && (str.ptr[0] == '+' || str.ptr[0] == '-')) {
185 sign = 1 - 2 * (str.ptr[0] == '-');
186 str.ptr++;
187 str.len--;
188 }
189  
190 if (!parse_unsigned_integer(str, out_mag)) {
191 return 0;
192 }
193  
194 *out_sign = sign;
195 return 1;
196 }
197  
198 int compute_decimal_repr_size (uintmax_t x)
199 {
200 int size = 0;
201  
202 do {
203 size++;
204 x /= 10;
205 } while (x > 0);
206  
207 return size;
208 }
209  
210 void generate_decimal_repr (uintmax_t x, char *out, int repr_size)
211 {
212 ASSERT(out)
213 ASSERT(repr_size == compute_decimal_repr_size(x))
214  
215 out += repr_size;
216  
217 do {
218 *(--out) = '0' + (x % 10);
219 x /= 10;
220 } while (x > 0);
221 }
222  
223 int generate_decimal_repr_string (uintmax_t x, char *out)
224 {
225 ASSERT(out)
226  
227 int repr_size = compute_decimal_repr_size(x);
228 generate_decimal_repr(x, out, repr_size);
229 out[repr_size] = '\0';
230  
231 return repr_size;
232 }
233  
234 #endif