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