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-2018 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 /*
40  * min/max functions
41  */
42 static inline unsigned int
43 max_uint(unsigned int x, unsigned int y)
44 {
45  if (x > y)
46  {
47  return x;
48  }
49  else
50  {
51  return y;
52  }
53 }
54 
55 static inline unsigned int
56 min_uint(unsigned int x, unsigned int y)
57 {
58  if (x < y)
59  {
60  return x;
61  }
62  else
63  {
64  return y;
65  }
66 }
67 
68 static inline int
69 max_int(int x, int y)
70 {
71  if (x > y)
72  {
73  return x;
74  }
75  else
76  {
77  return y;
78  }
79 }
80 
81 static inline int
82 min_int(int x, int y)
83 {
84  if (x < y)
85  {
86  return x;
87  }
88  else
89  {
90  return y;
91  }
92 }
93 
94 static inline int
95 constrain_int(int x, int min, int max)
96 {
97  if (min > max)
98  {
99  return min;
100  }
101  if (x < min)
102  {
103  return min;
104  }
105  else if (x > max)
106  {
107  return max;
108  }
109  else
110  {
111  return x;
112  }
113 }
114 
115 /*
116  * Functions used for circular buffer index arithmetic.
117  */
118 
119 /*
120  * Return x - y on a circle of circumference mod by shortest path.
121  *
122  * 0 <= x < mod
123  * 0 <= y < mod
124  */
125 static inline int
126 modulo_subtract(int x, int y, int mod)
127 {
128  const int d1 = x - y;
129  const int d2 = (x > y ? -mod : mod) + d1;
130  ASSERT(0 <= x && x < mod && 0 <= y && y < mod);
131  return abs(d1) > abs(d2) ? d2 : d1;
132 }
133 
134 /*
135  * Return x + y on a circle of circumference mod.
136  *
137  * 0 <= x < mod
138  * -mod <= y <= mod
139  */
140 static inline int
141 modulo_add(int x, int y, int mod)
142 {
143  int sum = x + y;
144  ASSERT(0 <= x && x < mod && -mod <= y && y <= mod);
145  if (sum >= mod)
146  {
147  sum -= mod;
148  }
149  if (sum < 0)
150  {
151  sum += mod;
152  }
153  return sum;
154 }
155 
156 /*
157  * Return the next largest power of 2
158  * or u if u is a power of 2.
159  */
160 static inline size_t
162 {
163  size_t ret = 1;
164 
165  while (ret < u)
166  {
167  ret <<= 1;
168  ASSERT(ret > 0);
169  }
170 
171  return ret;
172 }
173 
174 static inline int
175 index_verify(int index, int size, const char *file, int line)
176 {
177  if (index < 0 || index >= size)
178  {
179  msg(M_FATAL, "Assertion Failed: Array index=%d out of bounds for array size=%d in %s:%d",
180  index,
181  size,
182  file,
183  line);
184  }
185  return index;
186 }
187 
188 #endif /* ifndef INTEGER_H */
static int modulo_subtract(int x, int y, int mod)
Definition: integer.h:126
static int modulo_add(int x, int y, int mod)
Definition: integer.h:141
#define ASSERT(x)
Definition: error.h:204
static size_t adjust_power_of_2(size_t u)
Definition: integer.h:161
#define msg(flags,...)
Definition: error.h:153
static int max_int(int x, int y)
Definition: integer.h:69
#define M_FATAL
Definition: error.h:98
static int constrain_int(int x, int min, int max)
Definition: integer.h:95
static unsigned int max_uint(unsigned int x, unsigned int y)
Definition: integer.h:43
static int min_int(int x, int y)
Definition: integer.h:82
static unsigned int min_uint(unsigned int x, unsigned int y)
Definition: integer.h:56
static int index_verify(int index, int size, const char *file, int line)
Definition: integer.h:175