Disk ARchive  2.4.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
tronconneuse.hpp
Go to the documentation of this file.
00001 /*********************************************************************/
00002 // dar - disk archive - a backup/restoration program
00003 // Copyright (C) 2002-2052 Denis Corbin
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 // to contact the author : http://dar.linux.free.fr/email.html
00020 /*********************************************************************/
00021 
00027 
00028 #ifndef TRONCONNEUSE_HPP
00029 #define TRONCONNEUSE_HPP
00030 
00031 #include "../my_config.h"
00032 #include <string>
00033 
00034 #include "infinint.hpp"
00035 #include "generic_file.hpp"
00036 #include "header_version.hpp"
00037 
00038 namespace libdar
00039 {
00040 
00043 
00044 
00046 
00059     class tronconneuse : public generic_file
00060     {
00061     public:
00063 
00071         tronconneuse(U_32 block_size, generic_file & encrypted_side, bool no_initial_shift, const archive_version & reading_ver);
00072 
00074         tronconneuse(const tronconneuse & ref) : generic_file(ref) { copy_from(ref); };
00075 
00077         const tronconneuse & operator = (const tronconneuse & ref);
00078 
00080         virtual ~tronconneuse() { detruit(); }; // must not write pure virtual method from here, directly or not
00081 
00083         bool skip(const infinint & pos);
00085         bool skip_to_eof();
00087         bool skip_relative(S_I x);
00089         infinint get_position() { if(is_terminated()) throw SRC_BUG; return current_position; };
00090 
00092 
00097         void write_end_of_file() { if(is_terminated()) throw SRC_BUG; flush(); weof = true; };
00098 
00099 
00101 
00102         void set_initial_shift(const infinint & x) { initial_shift = x; };
00103 
00104 
00108         void set_callback_trailing_clear_data(infinint (*call_back)(generic_file & below, const archive_version & reading_ver)) { trailing_clear_data = call_back; };
00109 
00110     private:
00111 
00113 
00115         U_I inherited_read(char *a, U_I size);
00116 
00118 
00120         void inherited_write(const char *a, U_I size);
00121 
00123         void inherited_sync_write() { flush(); };
00124 
00126         void inherited_terminate() {};
00127 
00128     protected:
00130 
00136         virtual U_32 encrypted_block_size_for(U_32 clear_block_size) = 0;
00137 
00139 
00146         virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size) = 0;
00147 
00149 
00158         virtual U_32 encrypt_data(const infinint & block_num,
00159                                   const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
00160                                   char *crypt_buf, U_32 crypt_size) = 0;
00161 
00163 
00170         virtual U_32 decrypt_data(const infinint & block_num,
00171                                   const char *crypt_buf, const U_32 crypt_size,
00172                                   char *clear_buf, U_32 clear_size) = 0;
00173 
00174 
00175     private:
00176         infinint initial_shift;    //< the initial_shift first bytes of the underlying file are not encrypted
00177         infinint buf_offset;       //< offset of the first byte in buf
00178         U_32 buf_byte_data;        //< number of byte of information in buf (buf_byte_data <= buf_size)
00179         U_32 buf_size;             //< size of allocated memory for clear data in buf
00180         char *buf;                 //< decrypted data (or data to encrypt)
00181         U_32 clear_block_size;     //< max amount of data that will be encrypted at once (must stay less than buf_size)
00182         infinint current_position; //< position of the next character to read or write
00183         infinint block_num;        //< block number we next read or write
00184         generic_file *encrypted;   //< generic_file where is put / get the encrypted data
00185         char *encrypted_buf;       //< buffer of encrypted data (read or to write)
00186         U_32 encrypted_buf_size;   //< allocated size of encrypted_buf
00187         bool weof;                 //< whether write_end_of_file() has been called
00188         bool reof;                 //< whether we reached eof while reading
00189         archive_version reading_ver;//< archive format we currently read
00190         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
00191 
00192 
00193         void detruit();
00194         void copy_from(const tronconneuse & ref);
00195         U_32 fill_buf();       // returns the position (of the next read op) inside the buffer and fill the buffer with clear data
00196         void flush();          // flush any pending data (write mode only) to encrypted device
00197         void init_buf();       // initialize if necessary the various buffers that relies on inherited method values
00198 
00199 
00201 
00207         void position_clear2crypt(const infinint & pos,
00208                                   infinint & file_buf_start,
00209                                   infinint & clear_buf_start,
00210                                   infinint & pos_in_buf,
00211                                   infinint & block_num);
00212 
00213         void position_crypt2clear(const infinint & pos, infinint & clear_pos);
00214             // gives the position of the next character
00215             // of clear data that corresponds to the encrypted data which index is pos
00216 
00217         bool check_current_position() { reof = false; return fill_buf() < buf_byte_data; };
00218             // return true if a there is a byte of information at the given offset
00219 
00220         infinint check_trailing_clear_data();
00221             // returns the offset of the first byte of cleared data found after all cyphered data
00222     };
00223 
00225 
00226 } // end of namespace
00227 
00228 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines