OpenVPN
integer.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2025 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef INTEGER_H
24#define INTEGER_H
25
26#include "error.h"
27
28#ifndef htonll
29#define htonll(x) \
30 ((1 == htonl(1)) \
31 ? (x) \
32 : ((uint64_t)htonl((uint32_t)((x) & 0xFFFFFFFF)) << 32) | htonl((uint32_t)((x) >> 32)))
33#endif
34
35#ifndef ntohll
36#define ntohll(x) \
37 ((1 == ntohl(1)) \
38 ? (x) \
39 : ((uint64_t)ntohl((uint32_t)((x) & 0xFFFFFFFF)) << 32) | ntohl((uint32_t)((x) >> 32)))
40#endif
41
42static inline int
44{
45 ASSERT(size <= INT_MAX);
46 return (int)size;
47}
48
49/*
50 * min/max functions
51 */
52static inline unsigned int
53max_uint(unsigned int x, unsigned int y)
54{
55 if (x > y)
56 {
57 return x;
58 }
59 else
60 {
61 return y;
62 }
63}
64
65static inline unsigned int
66min_uint(unsigned int x, unsigned int y)
67{
68 if (x < y)
69 {
70 return x;
71 }
72 else
73 {
74 return y;
75 }
76}
77
78static inline size_t
79min_size(size_t x, size_t y)
80{
81 if (x < y)
82 {
83 return x;
84 }
85 else
86 {
87 return y;
88 }
89}
90
91static inline int
92max_int(int x, int y)
93{
94 if (x > y)
95 {
96 return x;
97 }
98 else
99 {
100 return y;
101 }
102}
103
104static inline int
105min_int(int x, int y)
106{
107 if (x < y)
108 {
109 return x;
110 }
111 else
112 {
113 return y;
114 }
115}
116
117static inline int
118constrain_int(int x, int min, int max)
119{
120 if (min > max)
121 {
122 return min;
123 }
124 if (x < min)
125 {
126 return min;
127 }
128 else if (x > max)
129 {
130 return max;
131 }
132 else
133 {
134 return x;
135 }
136}
137
138static inline unsigned int
139constrain_uint(unsigned int x, unsigned int min, unsigned int max)
140{
141 if (min > max)
142 {
143 return min;
144 }
145 if (x < min)
146 {
147 return min;
148 }
149 else if (x > max)
150 {
151 return max;
152 }
153 else
154 {
155 return x;
156 }
157}
158
159/*
160 * Functions used for circular buffer index arithmetic.
161 */
162
163/*
164 * Return x - y on a circle of circumference mod by shortest path.
165 *
166 * 0 <= x < mod
167 * 0 <= y < mod
168 */
169static inline int
170modulo_subtract(int x, int y, int mod)
171{
172 const int d1 = x - y;
173 const int d2 = (x > y ? -mod : mod) + d1;
174 ASSERT(0 <= x && x < mod && 0 <= y && y < mod);
175 return abs(d1) > abs(d2) ? d2 : d1;
176}
177
178/*
179 * Return x + y on a circle of circumference mod.
180 *
181 * 0 <= x < mod
182 * -mod <= y <= mod
183 */
184static inline int
185modulo_add(int x, int y, int mod)
186{
187 int sum = x + y;
188 ASSERT(0 <= x && x < mod && -mod <= y && y <= mod);
189 if (sum >= mod)
190 {
191 sum -= mod;
192 }
193 if (sum < 0)
194 {
195 sum += mod;
196 }
197 return sum;
198}
199
200/*
201 * Return the next largest power of 2
202 * or u if u is a power of 2.
203 */
204static inline size_t
206{
207 size_t ret = 1;
208
209 while (ret < u)
210 {
211 ret <<= 1;
212 ASSERT(ret > 0);
213 }
214
215 return ret;
216}
217
218static inline int
219index_verify(int index, int size, const char *file, int line)
220{
221 if (index < 0 || index >= size)
222 {
223 msg(M_FATAL, "Assertion Failed: Array index=%d out of bounds for array size=%d in %s:%d",
224 index, size, file, line);
225 }
226 return index;
227}
228
232static inline size_t
233round_down_size(size_t num, size_t multiple)
234{
235 return (num / multiple) * multiple;
236}
237
238#endif /* ifndef INTEGER_H */
static size_t adjust_power_of_2(size_t u)
Definition integer.h:205
static unsigned int constrain_uint(unsigned int x, unsigned int min, unsigned int max)
Definition integer.h:139
static size_t min_size(size_t x, size_t y)
Definition integer.h:79
static unsigned int min_uint(unsigned int x, unsigned int y)
Definition integer.h:66
static int min_int(int x, int y)
Definition integer.h:105
static int modulo_subtract(int x, int y, int mod)
Definition integer.h:170
static size_t round_down_size(size_t num, size_t multiple)
Rounds down num to the nearest multiple of multiple.
Definition integer.h:233
static int clamp_size_to_int(size_t size)
Definition integer.h:43
static int max_int(int x, int y)
Definition integer.h:92
static unsigned int max_uint(unsigned int x, unsigned int y)
Definition integer.h:53
static int modulo_add(int x, int y, int mod)
Definition integer.h:185
static int constrain_int(int x, int min, int max)
Definition integer.h:118
static int index_verify(int index, int size, const char *file, int line)
Definition integer.h:219
#define M_FATAL
Definition error.h:90
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219