Disk ARchive  2.4.5
 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 // $Id: crc.hpp,v 1.8.2.2 2012/02/19 17:25:08 edrusb Rel $
22 //
23 /*********************************************************************/
24 
28 
29 #ifndef CRC_HPP
30 #define CRC_HPP
31 
32 #include "../my_config.h"
33 
34 #include <string>
35 #include <list>
36 
37 #include "integers.hpp"
38 #include "storage.hpp"
39 #include "infinint.hpp"
40 
41 namespace libdar
42 {
43 
46 
47  class crc
48  {
49  public:
50  static const U_I OLD_CRC_SIZE = 2;
51 
52  virtual ~crc() {};
53 
54  virtual bool operator == (const crc & ref) const = 0;
55  bool operator != (const crc & ref) const { return ! (*this == ref); };
56 
57  virtual void compute(const infinint & offset, const char *buffer, U_I length) = 0;
58  virtual void compute(const char *buffer, U_I length) = 0; // for sequential read only
59  virtual void clear() = 0;
60  virtual void dump(generic_file & f) const = 0;
61  virtual std::string crc2str() const = 0;
62  virtual infinint get_size() const = 0;
63  virtual crc *clone() const = 0;
64  };
65 
66  extern crc *create_crc_from_file(generic_file & f, bool old = false);
67  extern crc *create_crc_from_size(infinint width);
68 
69  class crc_i : public crc
70  {
71  public:
72  crc_i(const infinint & width);
73  crc_i(const infinint & width, generic_file & f);
74  crc_i(const crc_i & ref) : size(ref.size), cyclic(ref.size) { copy_data_from(ref); pointer = cyclic.begin(); };
75  const crc_i & operator = (const crc_i & ref) { copy_from(ref); return *this; };
76 
77  bool operator == (const crc & ref) const;
78 
79  void compute(const infinint & offset, const char *buffer, U_I length);
80  void compute(const char *buffer, U_I length); // for sequential read only
81  void clear();
82  void dump(generic_file & f) const;
83  std::string crc2str() const;
84  infinint get_size() const { return size; };
85 
86 #ifdef LIBDAR_SPECIAL_ALLOC
87  USE_SPECIAL_ALLOC(crc_i);
88 #endif
89 
90 
91  protected:
92  crc *clone() const { return new crc_i(*this); };
93 
94  private:
95 
96  infinint size; //< size of the checksum
97  storage::iterator pointer; //< points to the next byte to modify
98  storage cyclic; //< the checksum storage
99 
100  void copy_from(const crc_i & ref);
101  void copy_data_from(const crc_i & ref);
102  };
103 
104 
105  class crc_n : public crc
106  {
107  public:
108 
109  crc_n(U_I width);
110  crc_n(U_I width, generic_file & f);
111  crc_n(const crc_n & ref) { copy_from(ref); };
112  const crc_n & operator = (const crc_n & ref);
113  ~crc_n() { destroy(); };
114 
115  bool operator == (const crc & ref) const;
116 
117  void compute(const infinint & offset, const char *buffer, U_I length);
118  void compute(const char *buffer, U_I length); // for sequential read only
119  void clear();
120  void dump(generic_file & f) const;
121  std::string crc2str() const;
122  infinint get_size() const { return size; };
123 
124 #ifdef LIBDAR_SPECIAL_ALLOC
125  USE_SPECIAL_ALLOC(crc_n);
126 #endif
127 
128  protected:
129  crc *clone() const { return new crc_n(*this); };
130 
131  private:
132 
133  U_I size; //< size of checksum (non infinint mode)
134  unsigned char *pointer; //< points to the next byte to modify (non infinint mode)
135  unsigned char *cyclic; //< the checksum storage (non infinint mode)
136 
137  void alloc(U_I width);
138  void copy_from(const crc_n & ref);
139  void copy_data_from(const crc_n & ref);
140  void destroy();
141  };
142 
143 
145 
146 } // end of namespace
147 
148 
149 #endif