Disk ARchive  2.4.2
 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 2011/02/11 20:23:42 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  crc(U_I width = 1);
53  crc(const infinint & width);
54  crc(const crc & ref) : i_cyclic(1) { copy_from(ref); };
55  const crc & operator = (const crc & ref) { destroy(); copy_from(ref); return *this; };
56  ~crc() { destroy(); };
57 
58  bool operator == (const crc & ref) const;
59  bool operator != (const crc & ref) const { return ! (*this == ref); };
60 
61  void compute(const infinint & offset, const char *buffer, U_I length);
62  void compute(const char *buffer, U_I length); // for sequential read only
63  void clear();
64  void dump(generic_file & f) const;
65  void read(generic_file & f);
66  void old_read(generic_file & f);
67  std::string crc2str() const;
68  void resize(const infinint & width);
69  void resize(U_I width) { resize_non_infinint(width); };
70  void resize_based_on(const crc & ref) { resize(ref.get_size()); };
71  infinint get_size() const { return infinint_mode ? i_size : n_size; };
72 
73  static void set_crc_pointer(crc * & dst, const crc *src);
74 
75  private:
76 
77  bool infinint_mode; //< true for infinint mode
78  // ---
79  infinint i_size; //< size of the checksum (infinint mode)
80  storage::iterator i_pointer; //< points to the next byte to modify (infinint mode)
81  storage i_cyclic; //< the checksum storage (infinint mode)
82  // ---
83  U_I n_size; //< size of checksum (non infinint mode)
84  unsigned char *n_pointer; //< points to the next byte to modify (non infinint mode)
85  unsigned char *n_cyclic; //< the checksum storage (non infinint mode)
86 
87  void copy_from(const crc & ref);
88  void destroy();
89  void resize_infinint(const infinint & width);
90  void resize_non_infinint(U_I width);
91  };
92 
93 
95 
96 } // end of namespace
97 
98 
99 #endif