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