2014-07-21 05:22:06 +02:00
|
|
|
// OpenVPN -- An application to securely tunnel IP networks
|
|
|
|
// over a single port, with support for SSL/TLS-based
|
|
|
|
// session authentication and key exchange,
|
|
|
|
// packet encryption, packet authentication, and
|
|
|
|
// packet compression.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2017-12-21 22:15:36 +01:00
|
|
|
// Copyright (C) 2012-2017 OpenVPN Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-12-21 21:42:20 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
2014-07-21 05:22:06 +02:00
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// 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
|
2017-12-21 21:42:20 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2014-07-21 05:22:06 +02:00
|
|
|
//
|
2017-12-21 21:42:20 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2014-07-21 05:22:06 +02:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// General purpose methods for dealing with numbers.
|
|
|
|
|
2011-12-18 11:50:08 +01:00
|
|
|
#ifndef OPENVPN_COMMON_NUMBER_H
|
|
|
|
#define OPENVPN_COMMON_NUMBER_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <limits>
|
|
|
|
|
2015-05-17 21:27:34 +02:00
|
|
|
#include <openvpn/common/size.hpp>
|
2012-10-28 11:07:32 +01:00
|
|
|
#include <openvpn/common/exception.hpp>
|
2011-12-18 11:50:08 +01:00
|
|
|
|
|
|
|
namespace openvpn {
|
|
|
|
|
2013-01-20 07:34:49 +01:00
|
|
|
OPENVPN_EXCEPTION(number_parse_exception);
|
2012-10-28 11:07:32 +01:00
|
|
|
|
2012-11-14 03:35:50 +01:00
|
|
|
// Parse the number of type T in str, returning
|
|
|
|
// value in retval. Returns true on success.
|
|
|
|
// Note -- currently doesn't detect overflow.
|
2015-02-10 23:55:32 +01:00
|
|
|
// If nondigit_term is true, any non-digit char
|
|
|
|
// can terminate the numerical value. Otherwise,
|
|
|
|
// only '\0' can terminate the value.
|
2011-12-18 11:50:08 +01:00
|
|
|
template <typename T>
|
2015-02-10 23:55:32 +01:00
|
|
|
inline bool parse_number(const char *str,
|
|
|
|
T& retval,
|
|
|
|
const bool nondigit_term = false)
|
2011-12-18 11:50:08 +01:00
|
|
|
{
|
|
|
|
if (!str[0])
|
2012-11-14 03:35:50 +01:00
|
|
|
return false; // empty string
|
2011-12-18 11:50:08 +01:00
|
|
|
bool neg = false;
|
|
|
|
size_t i = 0;
|
|
|
|
if (std::numeric_limits<T>::min() < 0 && str[0] == '-')
|
|
|
|
{
|
|
|
|
neg = true;
|
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
T ret = T(0);
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
const char c = str[i++];
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
{
|
|
|
|
ret *= T(10);
|
|
|
|
ret += T(c - '0');
|
|
|
|
}
|
2015-02-10 23:55:32 +01:00
|
|
|
else if (!c || nondigit_term)
|
2012-11-14 03:35:50 +01:00
|
|
|
{
|
|
|
|
retval = neg ? -ret : ret;
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-18 11:50:08 +01:00
|
|
|
else
|
2012-11-14 03:35:50 +01:00
|
|
|
return false; // non-digit
|
2011-12-18 11:50:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 07:34:49 +01:00
|
|
|
// like parse_number above, but accepts std::string
|
2012-11-14 03:35:50 +01:00
|
|
|
template <typename T>
|
2013-01-20 07:34:49 +01:00
|
|
|
inline bool parse_number(const std::string& str, T& retval)
|
2012-11-14 03:35:50 +01:00
|
|
|
{
|
2013-01-20 07:34:49 +01:00
|
|
|
return parse_number<T>(str.c_str(), retval);
|
2012-11-14 03:35:50 +01:00
|
|
|
}
|
|
|
|
|
2016-07-02 08:53:17 +02:00
|
|
|
template <typename T>
|
|
|
|
inline T parse_number_throw(const std::string& str, const std::string& error)
|
|
|
|
{
|
|
|
|
T ret;
|
|
|
|
if (parse_number<T>(str.c_str(), ret))
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
throw number_parse_exception(error);
|
|
|
|
}
|
|
|
|
|
2012-11-14 03:35:50 +01:00
|
|
|
template <typename T>
|
2013-01-20 07:34:49 +01:00
|
|
|
inline T parse_number_throw(const std::string& str, const char *error)
|
2012-11-14 03:35:50 +01:00
|
|
|
{
|
2013-01-20 07:34:49 +01:00
|
|
|
T ret;
|
|
|
|
if (parse_number<T>(str.c_str(), ret))
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
throw number_parse_exception(std::string(error));
|
2012-11-14 03:35:50 +01:00
|
|
|
}
|
|
|
|
|
2013-01-20 07:34:49 +01:00
|
|
|
template <typename T>
|
|
|
|
inline T parse_number_throw(const char *str, const char *error)
|
2012-02-19 02:36:50 +01:00
|
|
|
{
|
2013-01-20 07:34:49 +01:00
|
|
|
T ret;
|
|
|
|
if (parse_number<T>(str, ret))
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
throw number_parse_exception(std::string(error));
|
2012-02-19 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
2012-11-14 03:35:50 +01:00
|
|
|
template <typename T>
|
2013-01-20 07:34:49 +01:00
|
|
|
inline bool parse_number_validate(const std::string& numstr,
|
|
|
|
const size_t max_len,
|
|
|
|
const T minimum,
|
|
|
|
const T maximum,
|
2015-05-17 10:53:37 +02:00
|
|
|
T* value_return = nullptr)
|
2012-11-14 03:35:50 +01:00
|
|
|
{
|
|
|
|
if (numstr.length() <= max_len)
|
|
|
|
{
|
|
|
|
T value;
|
|
|
|
if (parse_number<T>(numstr.c_str(), value))
|
|
|
|
{
|
|
|
|
if (value >= minimum && value <= maximum)
|
|
|
|
{
|
|
|
|
if (value_return)
|
|
|
|
*value_return = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-20 07:34:49 +01:00
|
|
|
inline bool is_number(const char *str)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
bool found_digit = false;
|
|
|
|
while ((c = *str++))
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
found_digit = true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return found_digit;
|
|
|
|
}
|
|
|
|
|
2011-12-18 11:50:08 +01:00
|
|
|
} // namespace openvpn
|
|
|
|
|
|
|
|
#endif // OPENVPN_COMMON_NUMBER_H
|