]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/xstrto.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / compat / xstrto.cc
1 #ifndef SQUID_XSTRTO_C_
2 #define SQUID_XSTRTO_C_
3
4 /*
5 * Shamelessly duplicated from the netfilter iptables sources
6 * for use by the Squid Project under GNU Public License.
7 *
8 * Reason for use as explained by Luciano Coelho:
9 * "I found that there is a bug in strtoul (and strtoull for
10 * that matter) that causes the long to overflow if there are valid digits
11 * after the maximum possible digits for the base. For example if you try
12 * to strtoul 0xfffffffff (with 9 f's) the strtoul will overflow and come
13 * up with a bogus result. I can't easily truncate the string to avoid
14 * this problem, because with decimal or octal, the same valid value would
15 * take more spaces. I could do some magic here, checking whether it's a
16 * hex, dec or oct and truncate appropriately, but that would be very ugly.
17 * So the simplest way I came up with was to use strtoull and return
18 * -EINVAL if the value exceeds 32 bits."
19 *
20 * Update/Maintenance History:
21 *
22 * 12-Sep-2010 : Copied from iptables xtables.c
23 * - xtables_strtoui renamed to xstrtoui
24 * - xtables_strtoul renamed to xstrtoul
25 *
26 * Squid VCS $Id$
27 *
28 * Original License and code follows.
29 */
30
31 #include "squid.h"
32 #include "compat/xstrto.h"
33
34 /*
35 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
36 *
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
41 *
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
46 *
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
50 */
51
52 #if HAVE_ERRNO_H
53 #include <errno.h>
54 #endif
55
56 bool
57 xstrtoul(const char *s, char **end, unsigned long *value,
58 unsigned long min, unsigned long max)
59 {
60 unsigned long v;
61 char *my_end;
62
63 errno = 0;
64 v = strtoul(s, &my_end, 0);
65
66 if (my_end == s)
67 return false;
68 if (end != NULL)
69 *end = my_end;
70
71 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
72 if (value != NULL)
73 *value = v;
74 if (end == NULL)
75 return *my_end == '\0';
76 return true;
77 }
78
79 return false;
80 }
81
82 bool
83 xstrtoui(const char *s, char **end, unsigned int *value,
84 unsigned int min, unsigned int max)
85 {
86 unsigned long v;
87 bool ret;
88
89 ret = xstrtoul(s, end, &v, min, max);
90 if (value != NULL)
91 *value = v;
92 return ret;
93 }
94
95 #endif /* SQUID_XSTRTO_C_ */