Disk ARchive  2.4.5
 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 // $Id$
00022 //
00023 /*********************************************************************/
00024 //
00025 
00031 
00032 #ifndef TRONCONNEUSE_HPP
00033 #define TRONCONNEUSE_HPP
00034 
00035 #include "../my_config.h"
00036 #include <string>
00037 
00038 #include "infinint.hpp"
00039 #include "generic_file.hpp"
00040 #include "header_version.hpp"
00041 
00042 namespace libdar
00043 {
00044 
00047 
00048 
00050 
00063     class tronconneuse : public generic_file
00064     {
00065     public:
00067 
00075         tronconneuse(U_32 block_size, generic_file & encrypted_side, bool no_initial_shift, const archive_version & reading_ver);
00076 
00078         tronconneuse(const tronconneuse & ref) : generic_file(ref) { copy_from(ref); };
00079 
00081         const tronconneuse & operator = (const tronconneuse & ref);
00082 
00084         virtual ~tronconneuse() { detruit(); }; // must not write pure virtual method from here, directly or not
00085 
00087         bool skip(const infinint & pos);
00089         bool skip_to_eof();
00091         bool skip_relative(S_I x);
00093         infinint get_position() { if(is_terminated()) throw SRC_BUG; return current_position; };
00094 
00096 
00101         void write_end_of_file() { if(is_terminated()) throw SRC_BUG; flush(); weof = true; };
00102 
00103 
00105 
00106         void set_initial_shift(const infinint & x) { initial_shift = x; };
00107 
00108 
00112         void set_callback_trailing_clear_data(infinint (*call_back)(generic_file & below, const archive_version & reading_ver)) { trailing_clear_data = call_back; };
00113 
00114     private:
00115 
00117 
00119         U_I inherited_read(char *a, U_I size);
00120 
00122 
00124         void inherited_write(const char *a, U_I size);
00125 
00127         void inherited_sync_write() { flush(); };
00128 
00130         void inherited_terminate() {};
00131 
00132     protected:
00134 
00140         virtual U_32 encrypted_block_size_for(U_32 clear_block_size) = 0;
00141 
00143 
00150         virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size) = 0;
00151 
00153 
00162         virtual U_32 encrypt_data(const infinint & block_num,
00163                                   const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
00164                                   char *crypt_buf, U_32 crypt_size) = 0;
00165 
00167 
00174         virtual U_32 decrypt_data(const infinint & block_num,
00175                                   const char *crypt_buf, const U_32 crypt_size,
00176                                   char *clear_buf, U_32 clear_size) = 0;
00177 
00178 
00179     private:
00180         infinint initial_shift;    //< the initial_shift first bytes of the underlying file are not encrypted
00181         infinint buf_offset;       //< offset of the first byte in buf
00182         U_32 buf_byte_data;        //< number of byte of information in buf (buf_byte_data <= buf_size)
00183         U_32 buf_size;             //< size of allocated memory for clear data in buf
00184         char *buf;                 //< decrypted data (or data to encrypt)
00185         U_32 clear_block_size;     //< max amount of data that will be encrypted at once (must stay less than buf_size)
00186         infinint current_position; //< position of the next character to read or write
00187         infinint block_num;        //< block number we next read or write
00188         generic_file *encrypted;   //< generic_file where is put / get the encrypted data
00189         char *encrypted_buf;       //< buffer of encrypted data (read or to write)
00190         U_32 encrypted_buf_size;   //< allocated size of encrypted_buf
00191         bool weof;                 //< whether write_end_of_file() has been called
00192         bool reof;                 //< whether we reached eof while reading
00193         archive_version reading_ver;//< archive format we currently read
00194         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
00195 
00196 
00197         void detruit();
00198         void copy_from(const tronconneuse & ref);
00199         U_32 fill_buf();       // returns the position (of the next read op) inside the buffer and fill the buffer with clear data
00200         void flush();          // flush any pending data (write mode only) to encrypted device
00201         void init_buf();       // initialize if necessary the various buffers that relies on inherited method values
00202 
00203 
00205 
00211         void position_clear2crypt(const infinint & pos,
00212                                   infinint & file_buf_start,
00213                                   infinint & clear_buf_start,
00214                                   infinint & pos_in_buf,
00215                                   infinint & block_num);
00216 
00217         void position_crypt2clear(const infinint & pos, infinint & clear_pos);
00218             // gives the position of the next character
00219             // of clear data that corresponds to the encrypted data which index is pos
00220 
00221         bool check_current_position() { reof = false; return fill_buf() < buf_byte_data; };
00222             // return true if a there is a byte of information at the given offset
00223 
00224         infinint check_trailing_clear_data();
00225             // returns the offset of the first byte of cleared data found after all cyphered data
00226     };
00227 
00229 
00230 } // end of namespace
00231 
00232 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines