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
138/*
139 * Functions used for circular buffer index arithmetic.
140 */
141
142/*
143 * Return x - y on a circle of circumference mod by shortest path.
144 *
145 * 0 <= x < mod
146 * 0 <= y < mod
147 */
148static inline int
149modulo_subtract(int x, int y, int mod)
150{
151 const int d1 = x - y;
152 const int d2 = (x > y ? -mod : mod) + d1;
153 ASSERT(0 <= x && x < mod && 0 <= y && y < mod);
154 return abs(d1) > abs(d2) ? d2 : d1;
155}
156
157/*
158 * Return x + y on a circle of circumference mod.
159 *
160 * 0 <= x < mod
161 * -mod <= y <= mod
162 */
163static inline int
164modulo_add(int x, int y, int mod)
165{
166 int sum = x + y;
167 ASSERT(0 <= x && x < mod && -mod <= y && y <= mod);
168 if (sum >= mod)
169 {
170 sum -= mod;
171 }
172 if (sum < 0)
173 {
174 sum += mod;
175 }
176 return sum;
177}
178
179/*
180 * Return the next largest power of 2
181 * or u if u is a power of 2.
182 */
183static inline size_t
185{
186 size_t ret = 1;
187
188 while (ret < u)
189 {
190 ret <<= 1;
191 ASSERT(ret > 0);
192 }
193
194 return ret;
195}
196
197static inline int
198index_verify(int index, int size, const char *file, int line)
199{
200 if (index < 0 || index >= size)
201 {
202 msg(M_FATAL, "Assertion Failed: Array index=%d out of bounds for array size=%d in %s:%d",
203 index, size, file, line);
204 }
205 return index;
206}
207
211static inline size_t
212round_down_size(size_t num, size_t multiple)
213{
214 return (num / multiple) * multiple;
215}
216
217#endif /* ifndef INTEGER_H */
static size_t adjust_power_of_2(size_t u)
Definition integer.h:184
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:149
static size_t round_down_size(size_t num, size_t multiple)
Rounds down num to the nearest multiple of multiple.
Definition integer.h:212
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:164
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:198
#define M_FATAL
Definition error.h:88
#define msg(flags,...)
Definition error.h:150
#define ASSERT(x)
Definition error.h:217