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