Disk ARchive
2.4.5
|
00001 /*********************************************************************/ 00002 // dar - disk archive - a backup/restoration program 00003 // Copyright (C) 2002-2052 Denis Corbin 00004 // 00005 // This program is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU General Public License 00007 // as published by the Free Software Foundation; either version 2 00008 // of the License, or (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 // 00019 // to contact the author : http://dar.linux.free.fr/email.html 00020 /*********************************************************************/ 00021 // $Id$ 00022 // 00023 /*********************************************************************/ 00024 00031 00032 #ifndef REAL_INFININT_HPP 00033 #define REAL_INFININT_HPP 00034 00035 #include "../my_config.h" 00036 00037 extern "C" 00038 { 00039 #if HAVE_SYS_TYPES_H 00040 #include <sys/types.h> 00041 #endif 00042 } // end extern "C" 00043 00044 #include <typeinfo> 00045 #include "storage.hpp" 00046 #include "integers.hpp" 00047 #include "int_tools.hpp" 00048 00049 #define ZEROED_SIZE 50 00050 00051 namespace libdar 00052 { 00053 class generic_file; 00054 class user_interaction; 00055 00057 00060 class infinint 00061 { 00062 public : 00063 00064 #if SIZEOF_OFF_T > SIZEOF_TIME_T 00065 #if SIZEOF_OFF_T > SIZEOF_SIZE_T 00066 infinint(off_t a = 0) 00067 { infinint_from(a); }; 00068 #else 00069 infinint(size_t a = 0) 00070 { infinint_from(a); }; 00071 #endif 00072 #else 00073 #if SIZEOF_TIME_T > SIZEOF_SIZE_T 00074 infinint(time_t a = 0) 00075 { infinint_from(a); }; 00076 #else 00077 infinint(size_t a = 0) 00078 { infinint_from(a); }; 00079 #endif 00080 #endif 00081 00082 infinint(const infinint & ref) 00083 { copy_from(ref); } 00084 00085 // read an infinint from a file 00086 infinint(user_interaction & dialog, S_I fd); 00087 infinint(generic_file & x); 00088 00089 ~infinint() 00090 { detruit(); }; 00091 00092 const infinint & operator = (const infinint & ref) 00093 { detruit(); copy_from(ref); return *this; }; 00094 00095 void dump(user_interaction & dialog, int fd) const; // write byte sequence to file 00096 void dump(generic_file &x) const; // write byte sequence to file 00097 void read(generic_file &f) { detruit(); build_from_file(f); }; 00098 00099 infinint & operator += (const infinint & ref); 00100 infinint & operator -= (const infinint & ref); 00101 infinint & operator *= (unsigned char arg); 00102 infinint & operator *= (const infinint & ref); 00103 template <class T> infinint power(const T & exponent) const; 00104 inline infinint & operator /= (const infinint & ref); 00105 inline infinint & operator %= (const infinint & ref); 00106 infinint & operator &= (const infinint & ref); 00107 infinint & operator |= (const infinint & ref); 00108 infinint & operator ^= (const infinint & ref); 00109 infinint & operator >>= (U_32 bit); 00110 infinint & operator >>= (infinint bit); 00111 infinint & operator <<= (U_32 bit); 00112 infinint & operator <<= (infinint bit); 00113 infinint operator ++(int a) 00114 { infinint ret = *this; ++(*this); return ret; }; 00115 infinint operator --(int a) 00116 { infinint ret = *this; --(*this); return ret; }; 00117 infinint & operator ++() 00118 { return *this += 1; }; 00119 infinint & operator --() 00120 { return *this -= 1; }; 00121 00122 U_32 operator % (U_32 arg) const 00123 { return modulo(arg); }; 00124 00125 // increment the argument up to a legal value for its storage type and decrement the object in consequence 00126 // note that the initial value of the argument is not ignored ! 00127 // when the object is null the value of the argument is unchanged 00128 template <class T>void unstack(T &v) 00129 { infinint_unstack_to(v); } 00130 00131 infinint get_storage_size() const { return field->size(); }; 00132 // it returns number of byte of information necessary to store the integer 00133 00134 unsigned char operator [] (const infinint & position) const; 00135 // return in big endian order the information byte storing the integer 00136 00137 friend bool operator < (const infinint &, const infinint &); 00138 friend bool operator == (const infinint &, const infinint &); 00139 friend bool operator > (const infinint &, const infinint &); 00140 friend bool operator <= (const infinint &, const infinint &); 00141 friend bool operator != (const infinint &, const infinint &); 00142 friend bool operator >= (const infinint &, const infinint &); 00143 friend void euclide(infinint a, const infinint &b, infinint &q, infinint &r); 00144 00145 static bool is_system_big_endian(); 00146 00147 #ifdef LIBDAR_SPECIAL_ALLOC 00148 USE_SPECIAL_ALLOC(infinint); 00149 #endif 00150 private : 00151 static const int TG = 4; 00152 00153 enum endian { big_endian, little_endian, not_initialized }; 00154 typedef unsigned char group[TG]; 00155 00156 storage *field; 00157 00158 bool is_valid() const; 00159 void build_from_file(generic_file & x); 00160 void reduce(); // put the object in canonical form : no leading byte equal to zero 00161 void copy_from(const infinint & ref); 00162 void detruit(); 00163 void make_at_least_as_wider_as(const infinint & ref); 00164 template <class T> void infinint_from(T a); 00165 template <class T> T max_val_of(T x); 00166 template <class T> void infinint_unstack_to(T &a); 00167 template <class T> T modulo(T arg) const; 00168 signed int difference(const infinint & b) const; // gives the sign of (*this - arg) but only the sign ! 00169 00171 // static statments 00172 // 00173 static endian used_endian; 00174 static U_8 zeroed_field[ZEROED_SIZE]; 00175 static void setup_endian(); 00176 }; 00177 00178 00179 #define OPERATOR(OP) inline bool operator OP (const infinint &a, const infinint &b) \ 00180 { \ 00181 return a.difference(b) OP 0; \ 00182 } 00183 00184 OPERATOR(<) 00185 OPERATOR(>) 00186 OPERATOR(<=) 00187 OPERATOR(>=) 00188 OPERATOR(==) 00189 OPERATOR(!=) 00190 00191 infinint operator + (const infinint &, const infinint &); 00192 infinint operator - (const infinint &, const infinint &); 00193 infinint operator * (const infinint &, const infinint &); 00194 infinint operator * (const infinint &, const unsigned char); 00195 infinint operator * (const unsigned char, const infinint &); 00196 infinint operator / (const infinint &, const infinint &); 00197 infinint operator % (const infinint &, const infinint &); 00198 infinint operator & (const infinint & a, const infinint & bit); 00199 infinint operator | (const infinint & a, const infinint & bit); 00200 infinint operator ^ (const infinint & a, const infinint & bit); 00201 infinint operator >> (const infinint & a, U_32 bit); 00202 infinint operator >> (const infinint & a, const infinint & bit); 00203 infinint operator << (const infinint & a, U_32 bit); 00204 infinint operator << (const infinint & a, const infinint & bit); 00205 void euclide(infinint a, const infinint &b, infinint &q, infinint &r); 00206 template <class T> inline void euclide(T a, T b, T & q, T &r) 00207 { 00208 q = a/b; r = a%b; 00209 } 00210 00211 inline infinint & infinint::operator /= (const infinint & ref) 00212 { 00213 *this = *this / ref; 00214 return *this; 00215 } 00216 00217 inline infinint & infinint::operator %= (const infinint & ref) 00218 { 00219 *this = *this % ref; 00220 return *this; 00221 } 00222 00223 00227 00228 template <class T> infinint infinint::power(const T & exponent) const 00229 { 00230 infinint ret = 1; 00231 for(T count = 0; count < exponent; ++count) 00232 ret *= *this; 00233 00234 return ret; 00235 } 00236 00237 template <class T> T infinint::modulo(T arg) const 00238 { 00239 infinint tmp = *this % infinint(arg); 00240 T ret = 0; 00241 unsigned char *debut = (unsigned char *)(&ret); 00242 unsigned char *ptr = debut + sizeof(T) - 1; 00243 storage::iterator it = tmp.field->rbegin(); 00244 00245 while(it != tmp.field->rend() && ptr >= debut) 00246 { 00247 *ptr = *it; 00248 --ptr; 00249 --it; 00250 } 00251 00252 // checking for overflow (should never occur, but for sanity, we check it anyway) 00253 00254 while(it != tmp.field->rend()) // field may not be reduced (some zeros are leading) 00255 { 00256 if(*it != 0) 00257 throw SRC_BUG; // could not put all the data in the returned value ! 00258 --it; 00259 } 00260 00261 if(used_endian == little_endian) 00262 int_tools_swap_bytes(debut, sizeof(T)); 00263 00264 return ret; 00265 } 00266 00267 00268 template <class T> void infinint::infinint_from(T a) 00269 { 00270 U_I size = sizeof(a); 00271 S_I direction = +1; 00272 unsigned char *ptr, *fin; 00273 00274 if(used_endian == not_initialized) 00275 setup_endian(); 00276 00277 if(used_endian == little_endian) 00278 { 00279 direction = -1; 00280 ptr = (unsigned char *)(&a) + (size - 1); 00281 fin = (unsigned char *)(&a) - 1; 00282 } 00283 else 00284 { 00285 direction = +1; 00286 ptr = (unsigned char *)(&a); 00287 fin = (unsigned char *)(&a) + size; 00288 } 00289 00290 while(ptr != fin && *ptr == 0) 00291 { 00292 ptr += direction; 00293 --size; 00294 } 00295 00296 if(size == 0) 00297 { 00298 size = 1; 00299 ptr -= direction; 00300 } 00301 00302 field = new storage(size); 00303 if(field != NULL) 00304 { 00305 storage::iterator it = field->begin(); 00306 00307 while(ptr != fin) 00308 { 00309 *it = *ptr; 00310 ++it; 00311 ptr += direction; 00312 } 00313 if(it != field->end()) 00314 throw SRC_BUG; // size mismatch in this algorithm 00315 } 00316 else 00317 throw Ememory("template infinint::infinint_from"); 00318 } 00319 00320 template <class T> T infinint::max_val_of(T x) 00321 { 00322 x = 0; 00323 x = ~x; 00324 00325 if(x <= 0) // T is a signed integer type. Note that it should be "x < 0" but to avoid compiler warning when T is unsigned it does not hurt having "x <= 0" here 00326 { 00327 x = 1; 00328 x = int_tools_rotate_right_one_bit(x); 00329 x = ~x; 00330 } 00331 00332 return x; 00333 } 00334 00335 template <class T> void infinint::infinint_unstack_to(T &a) 00336 { 00337 // T is supposed to be an unsigned "integer" 00338 // (ie.: sizeof() returns the width of the storage bit field and no sign bit is present) 00339 // Note : static here avoids the recalculation of max_T at each call 00340 static const T max_T = max_val_of(a); 00341 infinint step = max_T - a; 00342 00343 if(*this < step) 00344 { 00345 T transfert = 0; 00346 unsigned char *debut = (unsigned char *)&transfert; 00347 unsigned char *ptr = debut + sizeof(transfert) - 1; 00348 storage::iterator it = field->rbegin(); 00349 00350 while(ptr >= debut && it != field->rend()) 00351 { 00352 *ptr = *it; 00353 --ptr; 00354 --it; 00355 } 00356 00357 if(used_endian == little_endian) 00358 int_tools_swap_bytes(debut, sizeof(transfert)); 00359 a += transfert; 00360 *this -= *this; 00361 } 00362 else 00363 { 00364 *this -= step; 00365 a = max_T; 00366 } 00367 } 00368 00369 } // end of namespace 00370 00371 #endif