File: xstrto.cc

package info (click to toggle)
squid3 3.4.8-6
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 31,084 kB
  • sloc: cpp: 165,325; ansic: 21,998; sh: 12,166; makefile: 5,964; perl: 2,153; sql: 322; awk: 118
file content (96 lines) | stat: -rw-r--r-- 2,953 bytes parent folder | download | duplicates (3)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef SQUID_XSTRTO_C_
#define SQUID_XSTRTO_C_

/*
 *  Shamelessly duplicated from the netfilter iptables sources
 *  for use by the Squid Project under GNU Public License.
 *
 * Reason for use as explained by Luciano Coelho:
 * "I found that there is a bug in strtoul (and strtoull for
 * that matter) that causes the long to overflow if there are valid digits
 * after the maximum possible digits for the base.  For example if you try
 * to strtoul 0xfffffffff (with 9 f's) the strtoul will overflow and come
 * up with a bogus result.  I can't easily truncate the string to avoid
 * this problem, because with decimal or octal, the same valid value would
 * take more spaces.  I could do some magic here, checking whether it's a
 * hex, dec or oct and truncate appropriately, but that would be very ugly.
 * So the simplest way I came up with was to use strtoull and return
 * -EINVAL if the value exceeds 32 bits."
 *
 * Update/Maintenance History:
 *
 *    12-Sep-2010 : Copied from iptables xtables.c
 * 			- xtables_strtoui renamed to xstrtoui
 * 			- xtables_strtoul renamed to xstrtoul
 *
 *  Original License and code follows.
 */

#include "squid.h"
#include "compat/xstrto.h"

/*
 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program 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 General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#if HAVE_ERRNO_H
#include <errno.h>
#endif

bool
xstrtoul(const char *s, char **end, unsigned long *value,
         unsigned long min, unsigned long max)
{
    char *my_end = NULL;

    errno = 0;
    unsigned long v = strtoul(s, &my_end, 0);

    if (my_end == s)
        return false;
    if (end != NULL)
        *end = my_end;

    if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
        if (value != NULL)
            *value = v;
        if (end == NULL)
            return *my_end == '\0';
        return true;
    }

    return false;
}

bool
xstrtoui(const char *s, char **end, unsigned int *value,
         unsigned int min, unsigned int max)
{
    unsigned long v = 0;
    bool ret = xstrtoul(s, end, &v, min, max);
    if (value != NULL) {
        *value = v;

        if (v != static_cast<unsigned long>(*value)) {
            return false;
        }
    }

    return ret;
}

#endif /* SQUID_XSTRTO_C_ */