OpenVPN 3 Core Library
Loading...
Searching...
No Matches
number.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// General purpose methods for dealing with numbers.
13
14#ifndef OPENVPN_COMMON_NUMBER_H
15#define OPENVPN_COMMON_NUMBER_H
16
17#include <string>
18#include <limits>
19
22
23namespace openvpn {
24
25OPENVPN_EXCEPTION(number_parse_exception);
26
27// Parse the number of type T in str, returning
28// value in retval. Returns true on success.
29// Note -- currently doesn't detect overflow.
30// If nondigit_term is true, any non-digit char
31// can terminate the numerical value. Otherwise,
32// only '\0' can terminate the value.
33template <typename T>
34inline bool parse_number(const char *str,
35 T &retval,
36 const bool nondigit_term = false)
37{
38 if (!str[0])
39 return false; // empty string
40 bool neg = false;
41 size_t i = 0;
42 if (std::numeric_limits<T>::min() < 0 && str[0] == '-')
43 {
44 neg = true;
45 i = 1;
46 }
47 T ret = T(0);
48 while (true)
49 {
50 const char c = str[i++];
51 if (c >= '0' && c <= '9')
52 {
53 ret *= T(10);
54 ret += T(c - '0');
55 }
56 else if (!c || nondigit_term)
57 {
58 retval = neg ? -ret : ret;
59 return true;
60 }
61 else
62 return false; // non-digit
63 }
64}
65
66// like parse_number above, but accepts std::string
67template <typename T>
68inline bool parse_number(const std::string &str, T &retval)
69{
70 return parse_number<T>(str.c_str(), retval);
71}
72
73template <typename T>
74inline T parse_number_throw(const std::string &str, const std::string &error)
75{
76 T ret;
77 if (parse_number<T>(str.c_str(), ret))
78 return ret;
79 else
80 throw number_parse_exception(error);
81}
82
83template <typename T>
84inline T parse_number_throw(const std::string &str, const char *error)
85{
86 T ret;
87 if (parse_number<T>(str.c_str(), ret))
88 return ret;
89 else
90 throw number_parse_exception(std::string(error));
91}
92
93template <typename T>
94inline T parse_number_throw(const char *str, const char *error)
95{
96 T ret;
97 if (parse_number<T>(str, ret))
98 return ret;
99 else
100 throw number_parse_exception(std::string(error));
101}
102
103template <typename T>
104inline bool parse_number_validate(const std::string &numstr,
105 const size_t max_len,
106 const T minimum,
107 const T maximum,
108 T *value_return = nullptr)
109{
110 if (numstr.length() <= max_len)
111 {
112 T value;
113 if (parse_number<T>(numstr.c_str(), value))
114 {
115 if (value >= minimum && value <= maximum)
116 {
117 if (value_return)
118 *value_return = value;
119 return true;
120 }
121 }
122 }
123 return false;
124}
125
126inline bool is_number(const char *str)
127{
128 char c;
129 bool found_digit = false;
130 while ((c = *str++))
131 {
132 if (c >= '0' && c <= '9')
133 found_digit = true;
134 else
135 return false;
136 }
137 return found_digit;
138}
139
140} // namespace openvpn
141
142#endif // OPENVPN_COMMON_NUMBER_H
#define OPENVPN_EXCEPTION(C)
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
bool parse_number_validate(const std::string &numstr, const size_t max_len, const T minimum, const T maximum, T *value_return=nullptr)
Definition number.hpp:104
bool parse_number(const char *str, T &retval, const bool nondigit_term=false)
Definition number.hpp:34
bool is_number(const char *str)
Definition number.hpp:126
T parse_number_throw(const std::string &str, const std::string &error)
Definition number.hpp:74
std::string ret