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