aboutsummaryrefslogtreecommitdiff
path: root/src/tools/mhd_tool_str_to_uint.h
blob: 5fc91b58510ac2ffcf2a5ed206db96077d002631 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 This file is part of GNU libmicrohttpd
  Copyright (C) 2023 Evgeny Grin (Karlson2k)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * @file tools/mhd_tool_str_to_uint.c
 * @brief  Implementation of HTTP server optimised for fast replies
 *         based on MHD.
 * @author Karlson2k (Evgeny Grin)
 */

#ifndef MHD_TOOL_STR_TO_UINT_H_
#define MHD_TOOL_STR_TO_UINT_H_

#include <stddef.h>

/**
 * Convert decimal string to unsigned int.
 * Function stops at the end of the string or on first non-digit character.
 * @param str the string to convert
 * @param[out] value the pointer to put the result
 * @return return the number of digits converted or
 *         zero if no digits found or result would overflow the output
 *         variable (the output set to UINT_MAX in this case).
 */
static size_t
mhd_tool_str_to_uint (const char *str, unsigned int *value)
{
  size_t i;
  unsigned int v = 0;
  *value = 0;

  for (i = 0; 0 != str[i]; ++i)
  {
    const char chr = str[i];
    unsigned int digit;
    if (('0' > chr) || ('9' < chr))
      break;
    digit = (unsigned char) (chr - '0');
    if ((((0U - 1) / 10) < v) || ((v * 10 + digit) < v))
    {
      /* Overflow */
      *value = 0U - 1;
      return 0;
    }
    v *= 10;
    v += digit;
  }
  *value = v;
  return i;
}


#endif /* MHD_TOOL_STR_TO_UINT_H_ */