BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file bsize.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Arithmetic with overflow detection.
32 */
33  
34 #ifndef BADVPN_MISC_BSIZE_H
35 #define BADVPN_MISC_BSIZE_H
36  
37 #include <stddef.h>
38 #include <limits.h>
39 #include <stdint.h>
40  
41 typedef struct {
42 int is_overflow;
43 size_t value;
44 } bsize_t;
45  
46 static bsize_t bsize_fromsize (size_t v);
47 static bsize_t bsize_fromint (int v);
48 static bsize_t bsize_overflow (void);
49 static int bsize_tosize (bsize_t s, size_t *out);
50 static int bsize_toint (bsize_t s, int *out);
51 static bsize_t bsize_add (bsize_t s1, bsize_t s2);
52 static bsize_t bsize_max (bsize_t s1, bsize_t s2);
53 static bsize_t bsize_mul (bsize_t s1, bsize_t s2);
54  
55 bsize_t bsize_fromsize (size_t v)
56 {
57 bsize_t s = {0, v};
58 return s;
59 }
60  
61 bsize_t bsize_fromint (int v)
62 {
63 bsize_t s = {(v < 0 || v > SIZE_MAX), v};
64 return s;
65 }
66  
67 static bsize_t bsize_overflow (void)
68 {
69 bsize_t s = {1, 0};
70 return s;
71 }
72  
73 int bsize_tosize (bsize_t s, size_t *out)
74 {
75 if (s.is_overflow) {
76 return 0;
77 }
78  
79 if (out) {
80 *out = s.value;
81 }
82  
83 return 1;
84 }
85  
86 int bsize_toint (bsize_t s, int *out)
87 {
88 if (s.is_overflow || s.value > INT_MAX) {
89 return 0;
90 }
91  
92 if (out) {
93 *out = s.value;
94 }
95  
96 return 1;
97 }
98  
99 bsize_t bsize_add (bsize_t s1, bsize_t s2)
100 {
101 bsize_t s = {(s1.is_overflow || s2.is_overflow || s2.value > SIZE_MAX - s1.value), (s1.value + s2.value)};
102 return s;
103 }
104  
105 bsize_t bsize_max (bsize_t s1, bsize_t s2)
106 {
107 bsize_t s = {(s1.is_overflow || s2.is_overflow), ((s1.value > s2.value) * s1.value + (s1.value <= s2.value) * s2.value)};
108 return s;
109 }
110  
111 bsize_t bsize_mul (bsize_t s1, bsize_t s2)
112 {
113 bsize_t s = {(s1.is_overflow || s2.is_overflow || (s1.value != 0 && s2.value > SIZE_MAX / s1.value)), (s1.value * s2.value)};
114 return s;
115 }
116  
117 #endif