aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-04-12 14:36:11 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-04-12 14:36:11 +0000
commit095d09c986c895f197d32a8aef67f9f42efcdebe (patch)
tree612709af43dcfcd53f7d312235156de144179d2c
parent36d82b1a0d34dc1b436abeeae9984b101718086c (diff)
downloadlibmicrohttpd-095d09c986c895f197d32a8aef67f9f42efcdebe.tar.gz
libmicrohttpd-095d09c986c895f197d32a8aef67f9f42efcdebe.zip
mhd_str: added MHD_strx_to_uint32_(), MHD_strx_to_uint32_n_(), MHD_strx_to_uint64_() and
MHD_strx_to_uint64_n_() functions
-rw-r--r--src/microhttpd/mhd_limits.h4
-rw-r--r--src/microhttpd/mhd_str.c164
-rw-r--r--src/microhttpd/mhd_str.h68
3 files changed, 236 insertions, 0 deletions
diff --git a/src/microhttpd/mhd_limits.h b/src/microhttpd/mhd_limits.h
index c8eff094..7afa13a5 100644
--- a/src/microhttpd/mhd_limits.h
+++ b/src/microhttpd/mhd_limits.h
@@ -44,6 +44,10 @@
44#define INT32_MAX ((int32_t)0x7FFFFFFF) 44#define INT32_MAX ((int32_t)0x7FFFFFFF)
45#endif /* !INT32_MAX */ 45#endif /* !INT32_MAX */
46 46
47#ifndef UINT32_MAX
48#define UINT32_MAX ((int32_t)0xFFFFFFFF)
49#endif /* !INT32_MAX */
50
47#ifndef UINT64_MAX 51#ifndef UINT64_MAX
48#define UINT64_MAX ((uint64_t)0xFFFFFFFFFFFFFFFF) 52#define UINT64_MAX ((uint64_t)0xFFFFFFFFFFFFFFFF)
49#endif /* !INT32_MAX */ 53#endif /* !INT32_MAX */
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
index f54b45ed..be8249fb 100644
--- a/src/microhttpd/mhd_str.c
+++ b/src/microhttpd/mhd_str.c
@@ -437,3 +437,167 @@ MHD_strx_to_sizet_n_ (const char * str, size_t maxlen, size_t * out_val)
437 *out_val = res; 437 *out_val = res;
438 return i; 438 return i;
439} 439}
440
441
442/**
443 * Convert hexadecimal US-ASCII digits in string to number in uint32_t.
444 * Conversion stopped at first non-digit character.
445 * @param str string to convert
446 * @param out_val pointer to uint32_t to store result of conversion
447 * @return non-zero number of characters processed on succeed,
448 * zero if no digit is found, resulting value is larger
449 * then possible to store in uint32_t or @a out_val is NULL
450 */
451size_t
452MHD_strx_to_uint32_ (const char * str, uint32_t * out_val)
453{
454 const char * const start = str;
455 uint32_t res;
456 int digit;
457 if (!str || !out_val)
458 return 0;
459
460 res = 0;
461 digit = toxdigitvalue (*str);
462 while (digit >= 0)
463 {
464 if ( (res < (UINT32_MAX / 16)) ||
465 (res == (UINT32_MAX / 16) && digit <= (UINT32_MAX % 16)) )
466 {
467 res *= 16;
468 res += digit;
469 }
470 else
471 return 0;
472 str++;
473 digit = toxdigitvalue (*str);
474 }
475
476 if (str - start > 0)
477 *out_val = res;
478 return str - start;
479}
480
481
482/**
483 * Convert not more then @a maxlen hexadecimal US-ASCII digits in string
484 * to number in uint32_t.
485 * Conversion stopped at first non-digit character or after @a maxlen
486 * digits.
487 * @param str string to convert
488 * @param maxlen maximum number of characters to process
489 * @param out_val pointer to uint32_t to store result of conversion
490 * @param next_char pointer to store pointer to character next to last
491 * converted digit, ignored if NULL
492 * @return non-zero number of characters processed on succeed,
493 * zero if no digit is found, resulting value is larger
494 * then possible to store in uint32_t or @a out_val is NULL
495 */
496size_t
497MHD_strx_to_uint32_n_ (const char * str, size_t maxlen, uint32_t * out_val)
498{
499 size_t i;
500 uint32_t res;
501 int digit;
502 if (!str || !out_val)
503 return 0;
504
505 res = 0;
506 i = 0;
507 while (i < maxlen && (digit = toxdigitvalue (str[i])) >= 0)
508 {
509 if ( (res > (UINT32_MAX / 16)) ||
510 (res == (UINT32_MAX / 16) && digit > (UINT32_MAX % 16)) )
511 return 0;
512
513 res *= 16;
514 res += digit;
515 i++;
516 }
517
518 if (i)
519 *out_val = res;
520 return i;
521}
522
523
524/**
525 * Convert hexadecimal US-ASCII digits in string to number in uint64_t.
526 * Conversion stopped at first non-digit character.
527 * @param str string to convert
528 * @param out_val pointer to uint64_t to store result of conversion
529 * @return non-zero number of characters processed on succeed,
530 * zero if no digit is found, resulting value is larger
531 * then possible to store in uint64_t or @a out_val is NULL
532 */
533size_t
534MHD_strx_to_uint64_ (const char * str, uint64_t * out_val)
535{
536 const char * const start = str;
537 uint64_t res;
538 int digit;
539 if (!str || !out_val)
540 return 0;
541
542 res = 0;
543 digit = toxdigitvalue (*str);
544 while (digit >= 0)
545 {
546 if ( (res < (UINT64_MAX / 16)) ||
547 (res == (UINT64_MAX / 16) && digit <= (UINT64_MAX % 16)) )
548 {
549 res *= 16;
550 res += digit;
551 }
552 else
553 return 0;
554 str++;
555 digit = toxdigitvalue (*str);
556 }
557
558 if (str - start > 0)
559 *out_val = res;
560 return str - start;
561}
562
563
564/**
565 * Convert not more then @a maxlen hexadecimal US-ASCII digits in string
566 * to number in uint64_t.
567 * Conversion stopped at first non-digit character or after @a maxlen
568 * digits.
569 * @param str string to convert
570 * @param maxlen maximum number of characters to process
571 * @param out_val pointer to uint64_t to store result of conversion
572 * @param next_char pointer to store pointer to character next to last
573 * converted digit, ignored if NULL
574 * @return non-zero number of characters processed on succeed,
575 * zero if no digit is found, resulting value is larger
576 * then possible to store in uint64_t or @a out_val is NULL
577 */
578size_t
579MHD_strx_to_uint64_n_ (const char * str, size_t maxlen, uint64_t * out_val)
580{
581 size_t i;
582 uint64_t res;
583 int digit;
584 if (!str || !out_val)
585 return 0;
586
587 res = 0;
588 i = 0;
589 while (i < maxlen && (digit = toxdigitvalue (str[i])) >= 0)
590 {
591 if ( (res > (UINT64_MAX / 16)) ||
592 (res == (UINT64_MAX / 16) && digit > (UINT64_MAX % 16)) )
593 return 0;
594
595 res *= 16;
596 res += digit;
597 i++;
598 }
599
600 if (i)
601 *out_val = res;
602 return i;
603}
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h
index 5c519568..2572c050 100644
--- a/src/microhttpd/mhd_str.h
+++ b/src/microhttpd/mhd_str.h
@@ -124,4 +124,72 @@ MHD_strx_to_sizet_n_ (const char * str,
124 size_t maxlen, 124 size_t maxlen,
125 size_t * out_val); 125 size_t * out_val);
126 126
127
128/**
129 * Convert hexadecimal US-ASCII digits in string to number in uint32_t.
130 * Conversion stopped at first non-digit character.
131 * @param str string to convert
132 * @param out_val pointer to uint32_t to store result of conversion
133 * @return non-zero number of characters processed on succeed,
134 * zero if no digit is found, resulting value is larger
135 * then possible to store in uint32_t or @a out_val is NULL
136 */
137size_t
138MHD_strx_to_uint32_ (const char * str,
139 uint32_t * out_val);
140
141
142/**
143 * Convert not more then @a maxlen hexadecimal US-ASCII digits in string
144 * to number in uint32_t.
145 * Conversion stopped at first non-digit character or after @a maxlen
146 * digits.
147 * @param str string to convert
148 * @param maxlen maximum number of characters to process
149 * @param out_val pointer to uint32_t to store result of conversion
150 * @param next_char pointer to store pointer to character next to last
151 * converted digit, ignored if NULL
152 * @return non-zero number of characters processed on succeed,
153 * zero if no digit is found, resulting value is larger
154 * then possible to store in uint32_t or @a out_val is NULL
155 */
156size_t
157MHD_strx_to_uint32_n_ (const char * str,
158 size_t maxlen,
159 uint32_t * out_val);
160
161
162/**
163 * Convert hexadecimal US-ASCII digits in string to number in uint64_t.
164 * Conversion stopped at first non-digit character.
165 * @param str string to convert
166 * @param out_val pointer to uint64_t to store result of conversion
167 * @return non-zero number of characters processed on succeed,
168 * zero if no digit is found, resulting value is larger
169 * then possible to store in uint64_t or @a out_val is NULL
170 */
171size_t
172MHD_strx_to_uint64_ (const char * str,
173 uint64_t * out_val);
174
175
176/**
177 * Convert not more then @a maxlen hexadecimal US-ASCII digits in string
178 * to number in uint64_t.
179 * Conversion stopped at first non-digit character or after @a maxlen
180 * digits.
181 * @param str string to convert
182 * @param maxlen maximum number of characters to process
183 * @param out_val pointer to uint64_t to store result of conversion
184 * @param next_char pointer to store pointer to character next to last
185 * converted digit, ignored if NULL
186 * @return non-zero number of characters processed on succeed,
187 * zero if no digit is found, resulting value is larger
188 * then possible to store in uint64_t or @a out_val is NULL
189 */
190size_t
191MHD_strx_to_uint64_n_ (const char * str,
192 size_t maxlen,
193 uint64_t * out_val);
194
127#endif /* MHD_STR_H */ 195#endif /* MHD_STR_H */