Disk ARchive  2.7.14
Full featured and portable backup and archiving tool
crc.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2024 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // to contact the author, see the AUTHOR file
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 "integers.hpp"
34 #include "storage.hpp"
35 #include "infinint.hpp"
36 #include "proto_generic_file.hpp"
37 
38 namespace libdar
39 {
40 
43 
45 
46  class crc
47  {
48  public:
49  static constexpr U_I OLD_CRC_SIZE = 2;
50 
51  crc() {};
52  crc(const crc & ref) = default;
53  crc(crc && ref) noexcept = default;
54  crc & operator = (const crc & ref) = default;
55  crc & operator = (crc && ref) noexcept = default;
56  virtual ~crc() = default;
57 
58  virtual bool operator == (const crc & ref) const = 0;
59  bool operator != (const crc & ref) const { return ! (*this == ref); };
60 
61  virtual void compute(const infinint & offset, const char *buffer, U_I length) = 0;
62  virtual void compute(const char *buffer, U_I length) = 0; // for sequential read only
63  virtual void clear() = 0;
64  virtual void dump(proto_generic_file & f) const = 0;
65  virtual std::string crc2str() const = 0;
66  virtual infinint get_size() const = 0;
67  virtual crc *clone() const = 0;
68  };
69 
71  extern crc *create_crc_from_file(proto_generic_file & f, bool old = false);
72 
75 
77 
78  class crc_i : public crc
79  {
80  public:
81  crc_i(const infinint & width);
82  crc_i(const infinint & width, proto_generic_file & f);
83  crc_i(const crc_i & ref) : size(ref.size), cyclic(ref.size) { copy_data_from(ref); pointer = cyclic.begin(); };
84  crc_i(crc_i && ref) noexcept = default;
85  crc_i & operator = (const crc_i & ref) { copy_from(ref); return *this; };
86  crc_i & operator = (crc_i && ref) noexcept = default;
87  ~crc_i() = default;
88 
89  bool operator == (const crc & ref) const override;
90 
91  virtual void compute(const infinint & offset, const char *buffer, U_I length) override;
92  virtual void compute(const char *buffer, U_I length) override; // for sequential read only
93  virtual void clear() override;
94  virtual void dump(proto_generic_file & f) const override;
95  virtual std::string crc2str() const override;
96  virtual infinint get_size() const override { return size; };
97 
98  protected:
99  virtual crc *clone() const override { crc *tmp = new (std::nothrow) crc_i(*this); if(tmp == nullptr) throw Ememory("crc"); return tmp; };
100 
101  private:
102 
104  storage::iterator pointer;
106 
107  void copy_from(const crc_i & ref);
108  void copy_data_from(const crc_i & ref);
109  };
110 
111 
113 
114  class crc_n : public crc
115  {
116  public:
117 
118  crc_n(U_I width);
119  crc_n(U_I width, proto_generic_file & f);
120  crc_n(const crc_n & ref) { copy_from(ref); };
121  crc_n(crc_n && ref) noexcept = default;
122  crc_n & operator = (const crc_n & ref);
123  crc_n & operator = (crc_n && ref) noexcept = default;
124  ~crc_n() { destroy(); };
125 
126  bool operator == (const crc & ref) const override;
127 
128  virtual void compute(const infinint & offset, const char *buffer, U_I length) override;
129  virtual void compute(const char *buffer, U_I length) override; // for sequential read only
130  virtual void clear() override;
131  virtual void dump(proto_generic_file & f) const override;
132  virtual std::string crc2str() const override;
133  virtual infinint get_size() const override { return size; };
134 
135  protected:
136  virtual crc *clone() const override { crc *tmp = new (std::nothrow) crc_n(*this); if(tmp == nullptr) throw Ememory("crc"); return tmp; };
137 
138  private:
139 
140  U_I size;
141  unsigned char *pointer;
142  unsigned char *cyclic;
143 
144  void alloc(U_I width);
145  void copy_from(const crc_n & ref);
146  void copy_data_from(const crc_n & ref);
147  void destroy();
148  };
149 
150 
152 
153 } // end of namespace
154 
155 
156 #endif
exception used when memory has been exhausted
Definition: erreurs.hpp:127
crc implementation based on infinint
Definition: crc.hpp:79
storage cyclic
the checksum storage
Definition: crc.hpp:105
storage::iterator pointer
points to the next byte to modify
Definition: crc.hpp:104
infinint size
size of the checksum
Definition: crc.hpp:99
crc implementation based on U_I
Definition: crc.hpp:115
unsigned char * cyclic
the checksum storage (non infinint mode)
Definition: crc.hpp:142
U_I size
size of checksum (non infinint mode)
Definition: crc.hpp:136
unsigned char * pointer
points to the next byte to modify (non infinint mode)
Definition: crc.hpp:141
pure virtual class defining interface of a CRC object
Definition: crc.hpp:47
the arbitrary large positive integer class
ancestor class of generic_file
arbitrary large storage structure
Definition: storage.hpp:53
crc * create_crc_from_file(proto_generic_file &f, bool old=false)
generate a CRC object reading it from file
crc * create_crc_from_size(infinint width)
generate a CRC object with adhoc width based on a file size
switch module to limitint (32 ou 64 bits integers) or infinint
are defined here basic integer types that tend to be portable
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
precursor class of generic_file used to avoid cyclic dependencies with storage and infinint
contains a class that permits arbitrary large data storage