Disk ARchive  2.4.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
crc.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
25 
26 #ifndef CRC_HPP
27 #define CRC_HPP
28 
29 #include "../my_config.h"
30 
31 #include <string>
32 #include <list>
33 #include <new>
34 
35 #include "integers.hpp"
36 #include "storage.hpp"
37 #include "infinint.hpp"
38 
39 namespace libdar
40 {
41 
44 
45  class crc
46  {
47  public:
48  static const U_I OLD_CRC_SIZE = 2;
49 
50  virtual ~crc() {};
51 
52  virtual bool operator == (const crc & ref) const = 0;
53  bool operator != (const crc & ref) const { return ! (*this == ref); };
54 
55  virtual void compute(const infinint & offset, const char *buffer, U_I length) = 0;
56  virtual void compute(const char *buffer, U_I length) = 0; // for sequential read only
57  virtual void clear() = 0;
58  virtual void dump(generic_file & f) const = 0;
59  virtual std::string crc2str() const = 0;
60  virtual infinint get_size() const = 0;
61  virtual crc *clone() const = 0;
62  };
63 
64  extern crc *create_crc_from_file(generic_file & f, bool old = false);
65  extern crc *create_crc_from_size(infinint width);
66 
67  class crc_i : public crc
68  {
69  public:
70  crc_i(const infinint & width);
71  crc_i(const infinint & width, generic_file & f);
72  crc_i(const crc_i & ref) : size(ref.size), cyclic(ref.size) { copy_data_from(ref); pointer = cyclic.begin(); };
73  const crc_i & operator = (const crc_i & ref) { copy_from(ref); return *this; };
74 
75  bool operator == (const crc & ref) const;
76 
77  void compute(const infinint & offset, const char *buffer, U_I length);
78  void compute(const char *buffer, U_I length); // for sequential read only
79  void clear();
80  void dump(generic_file & f) const;
81  std::string crc2str() const;
82  infinint get_size() const { return size; };
83 
84 #ifdef LIBDAR_SPECIAL_ALLOC
85  USE_SPECIAL_ALLOC(crc_i);
86 #endif
87 
88 
89  protected:
90  crc *clone() const { return new (std::nothrow) crc_i(*this); };
91 
92  private:
93 
94  infinint size; //< size of the checksum
95  storage::iterator pointer; //< points to the next byte to modify
96  storage cyclic; //< the checksum storage
97 
98  void copy_from(const crc_i & ref);
99  void copy_data_from(const crc_i & ref);
100  };
101 
102 
103  class crc_n : public crc
104  {
105  public:
106 
107  crc_n(U_I width);
108  crc_n(U_I width, generic_file & f);
109  crc_n(const crc_n & ref) { copy_from(ref); };
110  const crc_n & operator = (const crc_n & ref);
111  ~crc_n() { destroy(); };
112 
113  bool operator == (const crc & ref) const;
114 
115  void compute(const infinint & offset, const char *buffer, U_I length);
116  void compute(const char *buffer, U_I length); // for sequential read only
117  void clear();
118  void dump(generic_file & f) const;
119  std::string crc2str() const;
120  infinint get_size() const { return size; };
121 
122 #ifdef LIBDAR_SPECIAL_ALLOC
123  USE_SPECIAL_ALLOC(crc_n);
124 #endif
125 
126  protected:
127  crc *clone() const { return new (std::nothrow) crc_n(*this); };
128 
129  private:
130 
131  U_I size; //< size of checksum (non infinint mode)
132  unsigned char *pointer; //< points to the next byte to modify (non infinint mode)
133  unsigned char *cyclic; //< the checksum storage (non infinint mode)
134 
135  void alloc(U_I width);
136  void copy_from(const crc_n & ref);
137  void copy_data_from(const crc_n & ref);
138  void destroy();
139  };
140 
141 
143 
144 } // end of namespace
145 
146 
147 #endif
are defined here basic integer types that tend to be portable
contains a class that permits arbitrary large data storage
switch module to limitint (32 ou 64 bits integers) or infinint