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-2024 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef INTEGER_H
25#define INTEGER_H
26
27#include "error.h"
28
29#ifndef htonll
30#define htonll(x) ((1==htonl(1)) ? (x) : \
31 ((uint64_t)htonl((uint32_t)((x) & 0xFFFFFFFF)) << 32) | htonl((uint32_t)((x) >> 32)))
32#endif
33
34#ifndef ntohll
35#define ntohll(x) ((1==ntohl(1)) ? (x) : \
36 ((uint64_t)ntohl((uint32_t)((x) & 0xFFFFFFFF)) << 32) | ntohl((uint32_t)((x) >> 32)))
37#endif
38
39static inline int
41{
42 ASSERT(size <= INT_MAX);
43 return (int)size;
44}
45
46/*
47 * min/max functions
48 */
49static inline unsigned int
50max_uint(unsigned int x, unsigned int y)
51{
52 if (x > y)
53 {
54 return x;
55 }
56 else
57 {
58 return y;
59 }
60}
61
62static inline unsigned int
63min_uint(unsigned int x, unsigned int y)
64{
65 if (x < y)
66 {
67 return x;
68 }
69 else
70 {
71 return y;
72 }
73}
74
75static inline size_t
76min_size(size_t x, size_t y)
77{
78 if (x < y)
79 {
80 return x;
81 }
82 else
83 {
84 return y;
85 }
86}
87
88static inline int
89max_int(int x, int y)
90{
91 if (x > y)
92 {
93 return x;
94 }
95 else
96 {
97 return y;
98 }
99}
100
101static inline int
102min_int(int x, int y)
103{
104 if (x < y)
105 {
106 return x;
107 }
108 else
109 {
110 return y;
111 }
112}
113
114static inline int
115constrain_int(int x, int min, int max)
116{
117 if (min > max)
118 {
119 return min;
120 }
121 if (x < min)
122 {
123 return min;
124 }
125 else if (x > max)
126 {
127 return max;
128 }
129 else
130 {
131 return x;
132 }
133}
134
135/*
136 * Functions used for circular buffer index arithmetic.
137 */
138
139/*
140 * Return x - y on a circle of circumference mod by shortest path.
141 *
142 * 0 <= x < mod
143 * 0 <= y < mod
144 */
145static inline int
146modulo_subtract(int x, int y, int mod)
147{
148 const int d1 = x - y;
149 const int d2 = (x > y ? -mod : mod) + d1;
150 ASSERT(0 <= x && x < mod && 0 <= y && y < mod);
151 return abs(d1) > abs(d2) ? d2 : d1;
152}
153
154/*
155 * Return x + y on a circle of circumference mod.
156 *
157 * 0 <= x < mod
158 * -mod <= y <= mod
159 */
160static inline int
161modulo_add(int x, int y, int mod)
162{
163 int sum = x + y;
164 ASSERT(0 <= x && x < mod && -mod <= y && y <= mod);
165 if (sum >= mod)
166 {
167 sum -= mod;
168 }
169 if (sum < 0)
170 {
171 sum += mod;
172 }
173 return sum;
174}
175
176/*
177 * Return the next largest power of 2
178 * or u if u is a power of 2.
179 */
180static inline size_t
182{
183 size_t ret = 1;
184
185 while (ret < u)
186 {
187 ret <<= 1;
188 ASSERT(ret > 0);
189 }
190
191 return ret;
192}
193
194static inline int
195index_verify(int index, int size, const char *file, int line)
196{
197 if (index < 0 || index >= size)
198 {
199 msg(M_FATAL, "Assertion Failed: Array index=%d out of bounds for array size=%d in %s:%d",
200 index,
201 size,
202 file,
203 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:181
static size_t min_size(size_t x, size_t y)
Definition integer.h:76
static unsigned int min_uint(unsigned int x, unsigned int y)
Definition integer.h:63
static int min_int(int x, int y)
Definition integer.h:102
static int modulo_subtract(int x, int y, int mod)
Definition integer.h:146
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:40
static int max_int(int x, int y)
Definition integer.h:89
static unsigned int max_uint(unsigned int x, unsigned int y)
Definition integer.h:50
static int modulo_add(int x, int y, int mod)
Definition integer.h:161
static int constrain_int(int x, int min, int max)
Definition integer.h:115
static int index_verify(int index, int size, const char *file, int line)
Definition integer.h:195
#define M_FATAL
Definition error.h:89
#define msg(flags,...)
Definition error.h:144
#define ASSERT(x)
Definition error.h:195