Disk ARchive  2.4.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
real_infinint.hpp
Go to the documentation of this file.
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 
00028 
00029 #ifndef REAL_INFININT_HPP
00030 #define REAL_INFININT_HPP
00031 
00032 #include "../my_config.h"
00033 
00034 extern "C"
00035 {
00036 #if HAVE_SYS_TYPES_H
00037 #include <sys/types.h>
00038 #endif
00039 } // end extern "C"
00040 
00041 #include <typeinfo>
00042 #include <new>
00043 
00044 #include "storage.hpp"
00045 #include "integers.hpp"
00046 #include "int_tools.hpp"
00047 
00048 #define ZEROED_SIZE 50
00049 
00050 namespace libdar
00051 {
00052     class generic_file;
00053     class user_interaction;
00054 
00056 
00059     class infinint
00060     {
00061     public :
00062 
00063 #if SIZEOF_OFF_T > SIZEOF_TIME_T
00064 #if SIZEOF_OFF_T > SIZEOF_SIZE_T
00065         infinint(off_t a = 0)
00066         { infinint_from(a); };
00067 #else
00068         infinint(size_t a = 0)
00069         { infinint_from(a); };
00070 #endif
00071 #else
00072 #if SIZEOF_TIME_T > SIZEOF_SIZE_T
00073         infinint(time_t a = 0)
00074         { infinint_from(a); };
00075 #else
00076         infinint(size_t a = 0)
00077         { infinint_from(a); };
00078 #endif
00079 #endif
00080 
00081         infinint(const infinint & ref)
00082         { copy_from(ref); }
00083 
00084             // read an infinint from a file
00085         infinint(user_interaction & dialog, S_I fd);
00086         infinint(generic_file & x);
00087 
00088         ~infinint()
00089         { detruit(); };
00090 
00091         const infinint & operator = (const infinint & ref)
00092         { detruit(); copy_from(ref); return *this; };
00093 
00094         void dump(user_interaction & dialog, int fd) const; // write byte sequence to file
00095         void dump(generic_file &x) const; // write byte sequence to file
00096         void read(generic_file &f) { detruit(); build_from_file(f); };
00097 
00098         infinint & operator += (const infinint & ref);
00099         infinint & operator -= (const infinint & ref);
00100         infinint & operator *= (unsigned char arg);
00101         infinint & operator *= (const infinint & ref);
00102         template <class T> infinint power(const T & exponent) const;
00103         inline infinint & operator /= (const infinint & ref);
00104         inline infinint & operator %= (const infinint & ref);
00105         infinint & operator &= (const infinint & ref);
00106         infinint & operator |= (const infinint & ref);
00107         infinint & operator ^= (const infinint & ref);
00108         infinint & operator >>= (U_32 bit);
00109         infinint & operator >>= (infinint bit);
00110         infinint & operator <<= (U_32 bit);
00111         infinint & operator <<= (infinint bit);
00112         infinint operator ++(int a)
00113         { infinint ret = *this; ++(*this); return ret; };
00114         infinint operator --(int a)
00115         { infinint ret = *this; --(*this); return ret; };
00116         infinint & operator ++()
00117         { return *this += 1; };
00118         infinint & operator --()
00119         { return *this -= 1; };
00120 
00121         U_32 operator % (U_32 arg) const
00122         { return modulo(arg); };
00123 
00124             // increment the argument up to a legal value for its storage type and decrement the object in consequence
00125             // note that the initial value of the argument is not ignored !
00126             // when the object is null the value of the argument is unchanged
00127         template <class T>void unstack(T &v)
00128         { infinint_unstack_to(v); }
00129 
00130         infinint get_storage_size() const { return field->size(); };
00131             // it returns number of byte of information necessary to store the integer
00132 
00133         unsigned char operator [] (const infinint & position) const;
00134             // return in big endian order the information byte storing the integer
00135 
00136         friend bool operator < (const infinint &, const infinint &);
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 void euclide(infinint a, const infinint &b, infinint &q, infinint &r);
00143 
00144         static bool is_system_big_endian();
00145 
00146 #ifdef LIBDAR_SPECIAL_ALLOC
00147         USE_SPECIAL_ALLOC(infinint);
00148 #endif
00149     private :
00150         static const int TG = 4;
00151 
00152         enum endian { big_endian, little_endian, not_initialized };
00153         typedef unsigned char group[TG];
00154 
00155         storage *field;
00156 
00157         bool is_valid() const;
00158         void build_from_file(generic_file & x);
00159         void reduce(); // put the object in canonical form : no leading byte equal to zero
00160         void copy_from(const infinint & ref);
00161         void detruit();
00162         void make_at_least_as_wider_as(const infinint & ref);
00163         template <class T> void infinint_from(T a);
00164         template <class T> T max_val_of(T x);
00165         template <class T> void infinint_unstack_to(T &a);
00166         template <class T> T modulo(T arg) const;
00167         signed int difference(const infinint & b) const; // gives the sign of (*this - arg) but only the sign !
00168 
00170             // static statments
00171             //
00172         static endian used_endian;
00173         static U_8 zeroed_field[ZEROED_SIZE];
00174         static void setup_endian();
00175     };
00176 
00177 
00178 #define OPERATOR(OP) inline bool operator OP (const infinint &a, const infinint &b) \
00179     {                                                                   \
00180             return a.difference(b) OP 0;                                \
00181     }
00182 
00183     OPERATOR(<)
00184     OPERATOR(>)
00185     OPERATOR(<=)
00186     OPERATOR(>=)
00187     OPERATOR(==)
00188     OPERATOR(!=)
00189 
00190     infinint operator + (const infinint &, const infinint &);
00191     infinint operator - (const infinint &, const infinint &);
00192     infinint operator * (const infinint &, const infinint &);
00193     infinint operator * (const infinint &, const unsigned char);
00194     infinint operator * (const unsigned char, const infinint &);
00195     infinint operator / (const infinint &, const infinint &);
00196     infinint operator % (const infinint &, const infinint &);
00197     infinint operator & (const infinint & a, const infinint & bit);
00198     infinint operator | (const infinint & a, const infinint & bit);
00199     infinint operator ^ (const infinint & a, const infinint & bit);
00200     infinint operator >> (const infinint & a, U_32 bit);
00201     infinint operator >> (const infinint & a, const infinint & bit);
00202     infinint operator << (const infinint & a, U_32 bit);
00203     infinint operator << (const infinint & a, const infinint & bit);
00204     void euclide(infinint a, const infinint &b, infinint &q, infinint &r);
00205     template <class T> inline void euclide(T a, T b, T & q, T &r)
00206     {
00207         q = a/b; r = a%b;
00208     }
00209 
00210     inline infinint & infinint::operator /= (const infinint & ref)
00211     {
00212         *this = *this / ref;
00213         return *this;
00214     }
00215 
00216     inline infinint & infinint::operator %= (const infinint & ref)
00217     {
00218         *this = *this % ref;
00219         return *this;
00220     }
00221 
00222 
00226 
00227     template <class T> infinint infinint::power(const T & exponent) const
00228     {
00229         infinint ret = 1;
00230         for(T count = 0; count < exponent; ++count)
00231             ret *= *this;
00232 
00233         return ret;
00234     }
00235 
00236     template <class T> T infinint::modulo(T arg) const
00237     {
00238         infinint tmp = *this % infinint(arg);
00239         T ret = 0;
00240         unsigned char *debut = (unsigned char *)(&ret);
00241         unsigned char *ptr = debut + sizeof(T) - 1;
00242         storage::iterator it = tmp.field->rbegin();
00243 
00244         while(it != tmp.field->rend() && ptr >= debut)
00245         {
00246             *ptr = *it;
00247             --ptr;
00248             --it;
00249         }
00250 
00251             // checking for overflow (should never occur, but for sanity, we check it anyway)
00252 
00253         while(it != tmp.field->rend()) // field may not be reduced (some zeros are leading)
00254         {
00255             if(*it != 0)
00256                 throw SRC_BUG; // could not put all the data in the returned value !
00257             --it;
00258         }
00259 
00260         if(used_endian == little_endian)
00261             int_tools_swap_bytes(debut, sizeof(T));
00262 
00263         return ret;
00264     }
00265 
00266 
00267     template <class T> void infinint::infinint_from(T a)
00268     {
00269         U_I size = sizeof(a);
00270         S_I direction = +1;
00271         unsigned char *ptr, *fin;
00272 
00273         if(used_endian == not_initialized)
00274             setup_endian();
00275 
00276         if(used_endian == little_endian)
00277         {
00278             direction = -1;
00279             ptr = (unsigned char *)(&a) + (size - 1);
00280             fin = (unsigned char *)(&a) - 1;
00281         }
00282         else
00283         {
00284             direction = +1;
00285             ptr = (unsigned char *)(&a);
00286             fin = (unsigned char *)(&a) + size;
00287         }
00288 
00289         while(ptr != fin && *ptr == 0)
00290         {
00291             ptr += direction;
00292             --size;
00293         }
00294 
00295         if(size == 0)
00296         {
00297             size = 1;
00298             ptr -= direction;
00299         }
00300 
00301         field = new (std::nothrow) storage(size);
00302         if(field != NULL)
00303         {
00304             storage::iterator it = field->begin();
00305 
00306             while(ptr != fin)
00307             {
00308                 *it = *ptr;
00309                 ++it;
00310                 ptr += direction;
00311             }
00312             if(it != field->end())
00313                 throw SRC_BUG; // size mismatch in this algorithm
00314         }
00315         else
00316             throw Ememory("template infinint::infinint_from");
00317     }
00318 
00319     template <class T> T infinint::max_val_of(T x)
00320     {
00321         x = 0;
00322         x = ~x;
00323 
00324         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
00325         {
00326             x = 1;
00327             x = int_tools_rotate_right_one_bit(x);
00328             x = ~x;
00329         }
00330 
00331         return x;
00332     }
00333 
00334     template <class T> void infinint::infinint_unstack_to(T &a)
00335     {
00336             // T is supposed to be an unsigned "integer"
00337             // (ie.: sizeof() returns the width of the storage bit field  and no sign bit is present)
00338             // Note : static here avoids the recalculation of max_T at each call
00339         static const T max_T = max_val_of(a);
00340         infinint step = max_T - a;
00341 
00342         if(*this < step)
00343         {
00344             T transfert = 0;
00345             unsigned char *debut = (unsigned char *)&transfert;
00346             unsigned char *ptr = debut + sizeof(transfert) - 1;
00347             storage::iterator it = field->rbegin();
00348 
00349             while(ptr >= debut && it != field->rend())
00350             {
00351                 *ptr = *it;
00352                 --ptr;
00353                 --it;
00354             }
00355 
00356             if(used_endian == little_endian)
00357                 int_tools_swap_bytes(debut, sizeof(transfert));
00358             a += transfert;
00359             *this -= *this;
00360         }
00361         else
00362         {
00363             *this -= step;
00364             a = max_T;
00365         }
00366     }
00367 
00368 } // end of namespace
00369 
00370 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines