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