Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
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 // $Id: tronconneuse.hpp,v 1.27 2011/04/17 13:12:30 edrusb Rel $
22 //
23 /*********************************************************************/
24 //
25 
31 
32 #ifndef TRONCONNEUSE_HPP
33 #define TRONCONNEUSE_HPP
34 
35 #include "../my_config.h"
36 #include <string>
37 
38 #include "infinint.hpp"
39 #include "generic_file.hpp"
40 #include "header_version.hpp"
41 
42 namespace libdar
43 {
44 
47 
48 
50 
63  class tronconneuse : public generic_file
64  {
65  public:
67 
75  tronconneuse(U_32 block_size, generic_file & encrypted_side, bool no_initial_shift, const archive_version & reading_ver);
76 
78  tronconneuse(const tronconneuse & ref) : generic_file(ref) { copy_from(ref); };
79 
81  const tronconneuse & operator = (const tronconneuse & ref);
82 
84  virtual ~tronconneuse() { detruit(); }; // must not write pure virtual method from here, directly or not
85 
87  bool skip(const infinint & pos);
89  bool skip_to_eof();
91  bool skip_relative(S_I x);
93  infinint get_position() { if(is_terminated()) throw SRC_BUG; return current_position; };
94 
96 
101  void write_end_of_file() { if(is_terminated()) throw SRC_BUG; flush(); weof = true; };
102 
103 
105 
106  void set_initial_shift(const infinint & x) { initial_shift = x; };
107 
108 
112  void set_callback_trailing_clear_data(infinint (*call_back)(generic_file & below, const archive_version & reading_ver)) { trailing_clear_data = call_back; };
113 
114  private:
115 
117 
119  U_I inherited_read(char *a, U_I size);
120 
122 
124  void inherited_write(const char *a, U_I size);
125 
127  void inherited_sync_write() { flush(); };
128 
130  void inherited_terminate() {};
131 
132  protected:
134 
140  virtual U_32 encrypted_block_size_for(U_32 clear_block_size) = 0;
141 
143 
150  virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size) = 0;
151 
153 
162  virtual U_32 encrypt_data(const infinint & block_num,
163  const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
164  char *crypt_buf, U_32 crypt_size) = 0;
165 
167 
174  virtual U_32 decrypt_data(const infinint & block_num,
175  const char *crypt_buf, const U_32 crypt_size,
176  char *clear_buf, U_32 clear_size) = 0;
177 
178 
179  private:
180  infinint initial_shift; //< the initial_shift first bytes of the underlying file are not encrypted
181  infinint buf_offset; //< offset of the first byte in buf
182  U_32 buf_byte_data; //< number of byte of information in buf (buf_byte_data <= buf_size)
183  U_32 buf_size; //< size of allocated memory for clear data in buf
184  char *buf; //< decrypted data (or data to encrypt)
185  U_32 clear_block_size; //< max amount of data that will be encrypted at once (must stay less than buf_size)
186  infinint current_position; //< position of the next character to read or write
187  infinint block_num; //< block number we next read or write
188  generic_file *encrypted; //< generic_file where is put / get the encrypted data
189  char *encrypted_buf; //< buffer of encrypted data (read or to write)
190  U_32 encrypted_buf_size; //< allocated size of encrypted_buf
191  bool weof; //< whether write_end_of_file() has been called
192  bool reof; //< whether we reached eof while reading
193  archive_version reading_ver;//< archive format we currently read
194  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
195 
196 
197  void detruit();
198  void copy_from(const tronconneuse & ref);
199  U_32 fill_buf(); // returns the position (of the next read op) inside the buffer and fill the buffer with clear data
200  void flush(); // flush any pending data (write mode only) to encrypted device
201  void init_buf(); // initialize if necessary the various buffers that relies on inherited method values
202 
203 
205 
211  void position_clear2crypt(const infinint & pos,
212  infinint & file_buf_start,
213  infinint & clear_buf_start,
214  infinint & pos_in_buf,
215  infinint & block_num);
216 
217  void position_crypt2clear(const infinint & pos, infinint & clear_pos);
218  // gives the position of the next character
219  // of clear data that corresponds to the encrypted data which index is pos
220 
221  bool check_current_position() { reof = false; return fill_buf() < buf_byte_data; };
222  // return true if a there is a byte of information at the given offset
223 
224  infinint check_trailing_clear_data();
225  // returns the offset of the first byte of cleared data found after all cyphered data
226  };
227 
229 
230 } // end of namespace
231 
232 #endif