1 /*
2 * steghide 0.5.1 - a steganography program
3 * Copyright (C) 1999-2003 Stefan Hetzl <shetzl@chello.at>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21 #ifndef SH_AUTILS_H
22 #define SH_AUTILS_H
23
24 #include <cmath>
25 #ifndef log2
26 // this is in an #ifndef because some cmath implementations #define log2 and some not
27 # define log2(x) (log(x) / log(2.0))
28 #endif
29
30 /**
31 * \class AUtils
32 * \brief provides some generic functions for non-standard arithmetic operations
33 **/
34 class AUtils {
35 public:
36 /**
37 * return the maximum of a and b (needs >)
38 **/
39 template<class T> static T max (T a, T b) ;
40
41 /**
42 * return the minimum of a and b (needs <)
43 **/
44 template<class T> static T min (T a, T b) ;
45
46 /**
47 * returns a divided through b rounded up to nearest "integer" (needs =, --, +, /)
48 **/
49 template<class T> static T div_roundup (T a, T b) ;
50
51 /**
52 * substraction with the modification to return 0 (T()) for negative difference (needs >, -, T())
53 **/
54 template<class T> static T bminus (T a, T b) ;
55
56 /**
57 * addition with the modification to return top for sums that are larger than top
58 **/
59 template<class T, T top> static T bplus (T a, T b) ;
60 template<class T> static T bplus (T a, T b, T top) ;
61
62 /**
63 * calculate the sum s[0]+...s[n-1] modulo m (needs =, +, % for T and =, CTYPE(), <, ++ for CTYPE)
64 **/
65 template<class T, class CTYPE> static T modsum (T* s, CTYPE n, T m) ;
66
67 /**
68 * round up x to nearest integer
69 **/
70 template<class IT, class FT> static IT roundup (FT x) ;
71
72 /**
73 * compute 2-logarithm of n (rounded up to nearest int), i.e. number of bits needed to store values from {0,...,n-1}
74 **/
75 template<class T> static T log2_ceil (T n) ;
76 } ;
77
78 template<class T>
max(T a,T b)79 T AUtils::max (T a, T b)
80 {
81 if (a > b) {
82 return a ;
83 }
84 else {
85 return b ;
86 }
87 }
88
89 template<class T>
min(T a,T b)90 T AUtils::min (T a, T b)
91 {
92 if (a < b) {
93 return a ;
94 }
95 else {
96 return b ;
97 }
98 }
99
100 template<class T>
div_roundup(T a,T b)101 T AUtils::div_roundup (T a, T b)
102 {
103 T c = b-- ;
104 return ((a + b) / c) ;
105 }
106
107 template<class T>
bminus(T a,T b)108 T AUtils::bminus (T a, T b)
109 {
110 if (a > b) {
111 return (a - b) ;
112 }
113 else {
114 return T() ;
115 }
116 }
117
118 template<class T, T top>
bplus(T a,T b)119 T AUtils::bplus (T a, T b)
120 {
121 a += b ;
122 if (a > top) {
123 return top ;
124 }
125 else {
126 return a ;
127 }
128 }
129
130 template<class T>
bplus(T a,T b,T top)131 T AUtils::bplus (T a, T b, T top)
132 {
133 a += b ;
134 if (a > top) {
135 return top ;
136 }
137 else {
138 return a ;
139 }
140 }
141
142 template<class T, class CTYPE>
modsum(T * s,CTYPE n,T m)143 T AUtils::modsum (T* s, CTYPE n, T m)
144 {
145 T retval = 0 ;
146 for (CTYPE i = CTYPE() ; i < n ; ++i) {
147 retval = (retval + s[i]) % m ;
148 }
149 return retval ;
150 }
151
152 template<class IT, class FT>
roundup(FT x)153 IT AUtils::roundup (FT x)
154 {
155 IT retval = 0 ;
156 FT intpart = (FT) ((IT) x) ;
157 if (x - intpart == (FT) 0.0) {
158 retval = (IT) x ;
159 }
160 else {
161 retval = ((IT) x) + 1 ;
162 }
163 return retval ;
164 }
165
166 template<class T>
log2_ceil(T n)167 T AUtils::log2_ceil (T n)
168 {
169 T retval = 0 ;
170 while (n > 1) {
171 n = div_roundup<T> (n, 2) ;
172 ++retval ;
173 }
174 return retval ;
175 }
176
177 #endif // ndef SH_AUTILS_H
178