Disk ARchive  2.4.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
crc.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 
00025 
00026 #ifndef CRC_HPP
00027 #define CRC_HPP
00028 
00029 #include "../my_config.h"
00030 
00031 #include <string>
00032 #include <list>
00033 #include <new>
00034 
00035 #include "integers.hpp"
00036 #include "storage.hpp"
00037 #include "infinint.hpp"
00038 
00039 namespace libdar
00040 {
00041 
00044 
00045     class crc
00046     {
00047     public:
00048         static const U_I OLD_CRC_SIZE = 2;
00049 
00050         virtual ~crc() {};
00051 
00052         virtual bool operator == (const crc & ref) const = 0;
00053         bool operator != (const crc & ref) const { return ! (*this == ref); };
00054 
00055         virtual void compute(const infinint & offset, const char *buffer, U_I length) = 0;
00056         virtual void compute(const char *buffer, U_I length) = 0; // for sequential read only
00057         virtual void clear() = 0;
00058         virtual void dump(generic_file & f) const = 0;
00059         virtual std::string crc2str() const = 0;
00060         virtual infinint get_size() const = 0;
00061         virtual crc *clone() const = 0;
00062     };
00063 
00064     extern crc *create_crc_from_file(generic_file & f, bool old = false);
00065     extern crc *create_crc_from_size(infinint width);
00066 
00067     class crc_i : public crc
00068     {
00069     public:
00070         crc_i(const infinint & width);
00071         crc_i(const infinint & width, generic_file & f);
00072         crc_i(const crc_i & ref) : size(ref.size), cyclic(ref.size) { copy_data_from(ref); pointer = cyclic.begin(); };
00073         const crc_i & operator = (const crc_i & ref) { copy_from(ref); return *this; };
00074 
00075         bool operator == (const crc & ref) const;
00076 
00077         void compute(const infinint & offset, const char *buffer, U_I length);
00078         void compute(const char *buffer, U_I length); // for sequential read only
00079         void clear();
00080         void dump(generic_file & f) const;
00081         std::string crc2str() const;
00082         infinint get_size() const { return size; };
00083 
00084 #ifdef LIBDAR_SPECIAL_ALLOC
00085         USE_SPECIAL_ALLOC(crc_i);
00086 #endif
00087 
00088 
00089     protected:
00090         crc *clone() const { return new (std::nothrow) crc_i(*this); };
00091 
00092     private:
00093 
00094         infinint size;                              //< size of the checksum
00095         storage::iterator pointer;                  //< points to the next byte to modify
00096         storage cyclic;                             //< the checksum storage
00097 
00098         void copy_from(const crc_i & ref);
00099         void copy_data_from(const crc_i & ref);
00100     };
00101 
00102 
00103     class crc_n : public crc
00104     {
00105     public:
00106 
00107         crc_n(U_I width);
00108         crc_n(U_I width, generic_file & f);
00109         crc_n(const crc_n & ref) { copy_from(ref); };
00110         const crc_n & operator = (const crc_n & ref);
00111         ~crc_n() { destroy(); };
00112 
00113         bool operator == (const crc & ref) const;
00114 
00115         void compute(const infinint & offset, const char *buffer, U_I length);
00116         void compute(const char *buffer, U_I length); // for sequential read only
00117         void clear();
00118         void dump(generic_file & f) const;
00119         std::string crc2str() const;
00120         infinint get_size() const { return size; };
00121 
00122 #ifdef LIBDAR_SPECIAL_ALLOC
00123         USE_SPECIAL_ALLOC(crc_n);
00124 #endif
00125 
00126     protected:
00127         crc *clone() const { return new (std::nothrow) crc_n(*this); };
00128 
00129     private:
00130 
00131         U_I size;                                   //< size of checksum (non infinint mode)
00132         unsigned char *pointer;                     //< points to the next byte to modify (non infinint mode)
00133         unsigned char *cyclic;                      //< the checksum storage (non infinint mode)
00134 
00135         void alloc(U_I width);
00136         void copy_from(const crc_n & ref);
00137         void copy_data_from(const crc_n & ref);
00138         void destroy();
00139     };
00140 
00141 
00143 
00144 } // end of namespace
00145 
00146 
00147 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines