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 throw number_parse_exception(error);
80}
81
82template <typename T>
83inline T parse_number_throw(const std::string &str, const char *error)
84{
85 T ret;
86 if (parse_number<T>(str.c_str(), ret))
87 return ret;
88 throw number_parse_exception(std::string(error));
89}
90
91template <typename T>
92inline T parse_number_throw(const char *str, const char *error)
93{
94 T ret;
95 if (parse_number<T>(str, ret))
96 return ret;
97 throw number_parse_exception(std::string(error));
98}
99
100template <typename T>
101inline bool parse_number_validate(const std::string &numstr,
102 const size_t max_len,
103 const T minimum,
104 const T maximum,
105 T *value_return = nullptr)
106{
107 if (numstr.length() <= max_len)
108 {
109 T value;
110 if (parse_number<T>(numstr.c_str(), value))
111 {
112 if (value >= minimum && value <= maximum)
113 {
114 if (value_return)
115 *value_return = value;
116 return true;
117 }
118 }
119 }
120 return false;
121}
122
123inline bool is_number(const char *str)
124{
125 char c;
126 bool found_digit = false;
127 while ((c = *str++))
128 {
129 if (c >= '0' && c <= '9')
130 found_digit = true;
131 else
132 return false;
133 }
134 return found_digit;
135}
136
137} // namespace openvpn
138
139#endif // OPENVPN_COMMON_NUMBER_H
#define OPENVPN_EXCEPTION(C)
Definition exception.hpp:99
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:101
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:123
T parse_number_throw(const std::string &str, const std::string &error)
Definition number.hpp:74
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::string ret