aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_str.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/mhd_str.c')
-rw-r--r--src/microhttpd/mhd_str.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
index bf9101ff..f54b45ed 100644
--- a/src/microhttpd/mhd_str.c
+++ b/src/microhttpd/mhd_str.c
@@ -84,6 +84,19 @@ isasciidigit (char c)
84} 84}
85 85
86/** 86/**
87 * Check whether character is hexadecimal digit in US-ASCII
88 * @param c character to check
89 * @return non-zero if character is decimal digit, zero otherwise
90 */
91_MHD_inline _MHD_bool
92isasciixdigit (char c)
93{
94 return isasciidigit (c) ||
95 (c >= 'A' && c <= 'F') ||
96 (c >= 'a' && c <= 'f');
97}
98
99/**
87 * Check whether character is decimal digit or letter in US-ASCII 100 * Check whether character is decimal digit or letter in US-ASCII
88 * @param c character to check 101 * @param c character to check
89 * @return non-zero if character is decimal digit or letter, zero otherwise 102 * @return non-zero if character is decimal digit or letter, zero otherwise
@@ -122,6 +135,23 @@ toasciiupper (char c)
122 return isasciilower (c) ? (c - 'a' + 'A') : c; 135 return isasciilower (c) ? (c - 'a' + 'A') : c;
123} 136}
124 137
138/**
139 * Convert US-ASCII hexadecimal digit to its value.
140 * @param c character to convert
141 * @return value of hexadecimal digit or -1 if @ c is not hexadecimal digit
142 */
143_MHD_inline int
144toxdigitvalue (char c)
145{
146 if (isasciidigit (c))
147 return (unsigned char)(c - '0');
148 if (c >= 'A' && c <= 'F')
149 return (unsigned char)(c - 'A' + 10);
150 if (c >= 'a' && c <= 'f')
151 return (unsigned char)(c - 'a' + 10);
152
153 return -1;
154}
125#else /* !INLINE_FUNC */ 155#else /* !INLINE_FUNC */
126 156
127/** 157/**
@@ -157,6 +187,16 @@ toasciiupper (char c)
157#define isasciidigit(c) (((char)(c)) >= '0' && ((char)(c)) <= '9') 187#define isasciidigit(c) (((char)(c)) >= '0' && ((char)(c)) <= '9')
158 188
159/** 189/**
190 * Check whether character is hexadecimal digit in US-ASCII
191 * @param c character to check
192 * @return boolean true if character is hexadecimal digit,
193 * boolean false otherwise
194 */
195#define isasciixdigit(c) (isasciidigit((c)) || \
196 (((char)(c)) >= 'A' && ((char)(c)) <= 'F') || \
197 (((char)(c)) >= 'a' && ((char)(c)) <= 'f') )
198
199/**
160 * Check whether character is decimal digit or letter in US-ASCII 200 * Check whether character is decimal digit or letter in US-ASCII
161 * @param c character to check 201 * @param c character to check
162 * @return boolean true if character is decimal digit or letter, 202 * @return boolean true if character is decimal digit or letter,
@@ -183,6 +223,17 @@ toasciiupper (char c)
183 * @return converted to upper case character 223 * @return converted to upper case character
184 */ 224 */
185#define toasciiupper(c) ((isasciilower(c)) ? (((char)(c)) - 'a' + 'A') : ((char)(c))) 225#define toasciiupper(c) ((isasciilower(c)) ? (((char)(c)) - 'a' + 'A') : ((char)(c)))
226
227/**
228 * Convert US-ASCII hexadecimal digit to its value.
229 * @param c character to convert
230 * @return value of hexadecimal digit or -1 if @ c is not hexadecimal digit
231 */
232#define toxdigitvalue(c) ( isasciidigit(c) ? (int)(((char)(c)) - '0') : \
233 ( (((char)(c)) >= 'A' && ((char)(c)) <= 'F') ? \
234 (int)(((unsigned char)(c)) - 'A' + 10) : \
235 ( (((char)(c)) >= 'a' && ((char)(c)) <= 'f') ? \
236 (int)(((unsigned char)(c)) - 'a' + 10) : (int)(-1) )))
186#endif /* !INLINE_FUNC */ 237#endif /* !INLINE_FUNC */
187 238
188/** 239/**
@@ -304,3 +355,85 @@ MHD_str_to_uint64_n_ (const char * str, size_t maxlen, uint64_t * out_val)
304 *out_val= res; 355 *out_val= res;
305 return i; 356 return i;
306} 357}
358
359
360/**
361 * Convert hexadecimal US-ASCII digits in string to number in size_t.
362 * Conversion stopped at first non-digit character.
363 * @param str string to convert
364 * @param out_val pointer to size_t to store result of conversion
365 * @return non-zero number of characters processed on succeed,
366 * zero if no digit is found, resulting value is larger
367 * then possible to store in size_t or @a out_val is NULL
368 */
369size_t
370MHD_strx_to_sizet_ (const char * str, size_t * out_val)
371{
372 const char * const start = str;
373 size_t res;
374 int digit;
375 if (!str || !out_val)
376 return 0;
377
378 res = 0;
379 digit = toxdigitvalue (*str);
380 while (digit >= 0)
381 {
382 if ( (res < (SIZE_MAX / 16)) ||
383 (res == (SIZE_MAX / 16) && digit <= (SIZE_MAX % 16)) )
384 {
385 res *= 16;
386 res += digit;
387 }
388 else
389 return 0;
390 str++;
391 digit = toxdigitvalue (*str);
392 }
393
394 if (str - start > 0)
395 *out_val = res;
396 return str - start;
397}
398
399
400/**
401 * Convert not more then @a maxlen hexadecimal US-ASCII digits in string
402 * to number in size_t.
403 * Conversion stopped at first non-digit character or after @a maxlen
404 * digits.
405 * @param str string to convert
406 * @param maxlen maximum number of characters to process
407 * @param out_val pointer to size_t to store result of conversion
408 * @param next_char pointer to store pointer to character next to last
409 * converted digit, ignored if NULL
410 * @return non-zero number of characters processed on succeed,
411 * zero if no digit is found, resulting value is larger
412 * then possible to store in size_t or @a out_val is NULL
413 */
414size_t
415MHD_strx_to_sizet_n_ (const char * str, size_t maxlen, size_t * out_val)
416{
417 size_t i;
418 size_t res;
419 int digit;
420 if (!str || !out_val)
421 return 0;
422
423 res = 0;
424 i = 0;
425 while (i < maxlen && (digit = toxdigitvalue (str[i])) >= 0)
426 {
427 if ( (res > (SIZE_MAX / 16)) ||
428 (res == (SIZE_MAX / 16) && digit > (SIZE_MAX % 16)) )
429 return 0;
430
431 res *= 16;
432 res += digit;
433 i++;
434 }
435
436 if (i)
437 *out_val = res;
438 return i;
439}