Disk ARchive  2.5.4
Full featured and portable backup and archiving tool
tronconneuse.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 
27 
28 #ifndef TRONCONNEUSE_HPP
29 #define TRONCONNEUSE_HPP
30 
31 #include "../my_config.h"
32 #include <string>
33 
34 #include "infinint.hpp"
35 #include "generic_file.hpp"
36 #include "archive_version.hpp"
37 
38 namespace libdar
39 {
40 
43 
44 
46 
59  class tronconneuse : public generic_file
60  {
61  public:
63 
71  tronconneuse(U_32 block_size, generic_file & encrypted_side, bool no_initial_shift, const archive_version & reading_ver);
72 
74  tronconneuse(const tronconneuse & ref) : generic_file(ref) { copy_from(ref); };
75 
77  const tronconneuse & operator = (const tronconneuse & ref);
78 
80  virtual ~tronconneuse() { detruit(); }; // must not write pure virtual method from here, directly or not
81 
83  bool skippable(skippability direction, const infinint & amount);
85  bool skip(const infinint & pos);
87  bool skip_to_eof();
89  bool skip_relative(S_I x);
91  infinint get_position() const { if(is_terminated()) throw SRC_BUG; return current_position; };
92 
94 
99  void write_end_of_file() { if(is_terminated()) throw SRC_BUG; flush(); weof = true; };
100 
101 
103 
104  void set_initial_shift(const infinint & x) { initial_shift = x; };
105 
109  void set_callback_trailing_clear_data(infinint (*call_back)(generic_file & below, const archive_version & reading_ver)) { trailing_clear_data = call_back; };
110 
112  U_32 get_clear_block_size() const { return clear_block_size; };
113 
114  private:
115 
117 
119  void inherited_read_ahead(const infinint & amount);
120 
122  U_I inherited_read(char *a, U_I size);
123 
125 
127  void inherited_write(const char *a, U_I size);
128 
130  void inherited_sync_write() { flush(); };
131 
133  void inherited_flush_read() { buf_byte_data = 0; };
134 
136  void inherited_terminate() {};
137 
138  protected:
140 
146  virtual U_32 encrypted_block_size_for(U_32 clear_block_size) = 0;
147 
149 
156  virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size) = 0;
157 
159 
168  virtual U_32 encrypt_data(const infinint & block_num,
169  const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
170  char *crypt_buf, U_32 crypt_size) = 0;
171 
173 
180  virtual U_32 decrypt_data(const infinint & block_num,
181  const char *crypt_buf, const U_32 crypt_size,
182  char *clear_buf, U_32 clear_size) = 0;
183 
184  protected:
185  const archive_version & get_reading_version() const { return reading_ver; };
186 
187 
188  private:
189  infinint initial_shift; //< the initial_shift first bytes of the underlying file are not encrypted
190  //
191  infinint buf_offset; //< offset of the first byte in buf
192  U_32 buf_byte_data; //< number of byte of information in buf (buf_byte_data <= buf_size)
193  U_32 buf_size; //< size of allocated memory for clear data in buf
194  char *buf; //< decrypted data (or data to encrypt)
195  //
196  U_32 clear_block_size; //< max amount of data that will be encrypted at once (must stay less than buf_size)
197  infinint current_position; //< position of the next character to read or write from the upper layer perspective, offset zero is the first encrypted byte, thus the first byte after initial_shift
198  infinint block_num; //< block number we next read or write
199  generic_file *encrypted; //< generic_file where is put / get the encrypted data
200  //
201  U_32 encrypted_buf_size; //< allocated size of encrypted_buf
202  U_32 encrypted_buf_data; //< amount of byte of information in encrypted_buf
203  char *encrypted_buf; //< buffer of encrypted data (read or to write)
204  //
205  infinint extra_buf_offset; //< offset of the first byte of extra_buf
206  U_32 extra_buf_size; //< allocated size of extra_buf
207  U_32 extra_buf_data; //< amount of byte of information in extra_buf
208  char *extra_buf; //< additional read encrypted that follow what is in encrypted_buf used to check for clear data after encrypted data
209  //
210  bool weof; //< whether write_end_of_file() has been called
211  bool reof; //< whether we reached eof while reading
212  archive_version reading_ver;//< archive format we currently read
213  infinint (*trailing_clear_data)(generic_file & below, const archive_version & reading_ver); //< callback function that gives the amount of clear data found at the end of the given file
214 
215 
216  void detruit();
217  void copy_from(const tronconneuse & ref);
218  U_32 fill_buf(); // returns the position (of the next read op) inside the buffer and fill the buffer with clear data
219  void flush(); // flush any pending data (write mode only) to encrypted device
220  void init_buf(); // initialize if necessary the various buffers that relies on inherited method values
221 
222 
224 
230  void position_clear2crypt(const infinint & pos,
231  infinint & file_buf_start,
232  infinint & clear_buf_start,
233  infinint & pos_in_buf,
234  infinint & block_num);
235 
236  void position_crypt2clear(const infinint & pos, infinint & clear_pos);
237  // gives the position of the next character
238  // of clear data that corresponds to the encrypted data which index is pos
239 
240  bool check_current_position() { return fill_buf() < buf_byte_data; };
241  // return true if a there is a byte of information at the given offset
242 
246  void remove_trailing_clear_data_from_encrypted_buf(const infinint & crypt_offset);
247 
248  };
249 
251 
252 } // end of namespace
253 
254 #endif
bool skip_relative(S_I x)
inherited from generic_file
bool skip_to_eof()
inherited from generic_file
virtual ~tronconneuse()
destructor
void set_initial_shift(const infinint &x)
this method to modify the initial shift. This overrides the constructor "no_initial_shift" of the con...
void set_callback_trailing_clear_data(infinint(*call_back)(generic_file &below, const archive_version &reading_ver))
this is a partial implementation of the generic_file interface to cypher/decypher data block by block...
virtual U_32 encrypt_data(const infinint &block_num, const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated, char *crypt_buf, U_32 crypt_size)=0
this method encrypts the clear data given
class generic_file is defined here as well as class fichierthe generic_file interface is widely used ...
const tronconneuse & operator=(const tronconneuse &ref)
assignment operator
bool skip(const infinint &pos)
inherited from generic_file
U_32 get_clear_block_size() const
returns the block size give to constructor
void write_end_of_file()
in write_only mode indicate that end of file is reached
bool is_terminated() const
virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size)=0
it may be necessary by the inherited class have few more bytes allocated after the clear data given f...
virtual U_32 encrypted_block_size_for(U_32 clear_block_size)=0
defines the size necessary to encrypt a given amount of clear data
infinint get_position() const
inherited from generic_file
virtual U_32 decrypt_data(const infinint &block_num, const char *crypt_buf, const U_32 crypt_size, char *clear_buf, U_32 clear_size)=0
this method decyphers data
tronconneuse(U_32 block_size, generic_file &encrypted_side, bool no_initial_shift, const archive_version &reading_ver)
This is the constructor.
switch module to limitint (32 ou 64 bits integers) or infinint
this is the interface class from which all other data transfer classes inherit
class archive_version that rules which archive format to follow
the arbitrary large positive integer class
class archive_version manages the version of the archive format
tronconneuse(const tronconneuse &ref)
copy constructor
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
bool skippable(skippability direction, const infinint &amount)
inherited from generic_file