Disk ARchive  2.5.2
Full featured and portable backup and archiving tool
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 "archive_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 skippable(skippability direction, const infinint & amount);
00085     bool skip(const infinint & pos);
00087     bool skip_to_eof();
00089     bool skip_relative(S_I x);
00091     infinint get_position() const { if(is_terminated()) throw SRC_BUG; return current_position; };
00092 
00094 
00099     void write_end_of_file() { if(is_terminated()) throw SRC_BUG; flush(); weof = true; };
00100 
00101 
00103 
00104     void set_initial_shift(const infinint & x) { initial_shift = x; };
00105 
00109     void set_callback_trailing_clear_data(infinint (*call_back)(generic_file & below, const archive_version & reading_ver)) { trailing_clear_data = call_back; };
00110 
00112     U_32 get_clear_block_size() const { return clear_block_size; };
00113 
00114     private:
00115 
00117 
00119     void inherited_read_ahead(const infinint & amount);
00120 
00122     U_I inherited_read(char *a, U_I size);
00123 
00125 
00127     void inherited_write(const char *a, U_I size);
00128 
00130     void inherited_sync_write() { flush(); };
00131 
00133     void inherited_flush_read() { buf_byte_data = 0; };
00134 
00136     void inherited_terminate() {};
00137 
00138     protected:
00140 
00146     virtual U_32 encrypted_block_size_for(U_32 clear_block_size) = 0;
00147 
00149 
00156     virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size) = 0;
00157 
00159 
00168     virtual U_32 encrypt_data(const infinint & block_num,
00169                   const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
00170                   char *crypt_buf, U_32 crypt_size) = 0;
00171 
00173 
00180     virtual U_32 decrypt_data(const infinint & block_num,
00181                   const char *crypt_buf, const U_32 crypt_size,
00182                   char *clear_buf, U_32 clear_size) = 0;
00183 
00184     protected:
00185     const archive_version & get_reading_version() const { return reading_ver; };
00186 
00187 
00188     private:
00189     infinint initial_shift;    //< the initial_shift first bytes of the underlying file are not encrypted
00190         //
00191     infinint buf_offset;       //< offset of the first byte in buf
00192     U_32 buf_byte_data;        //< number of byte of information in buf (buf_byte_data <= buf_size)
00193     U_32 buf_size;             //< size of allocated memory for clear data in buf
00194     char *buf;                 //< decrypted data (or data to encrypt)
00195         //
00196     U_32 clear_block_size;     //< max amount of data that will be encrypted at once (must stay less than buf_size)
00197     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
00198     infinint block_num;        //< block number we next read or write
00199     generic_file *encrypted;   //< generic_file where is put / get the encrypted data
00200         //
00201     U_32 encrypted_buf_size;   //< allocated size of encrypted_buf
00202     U_32 encrypted_buf_data;   //< amount of byte of information in encrypted_buf
00203     char *encrypted_buf;       //< buffer of encrypted data (read or to write)
00204         //
00205     infinint extra_buf_offset; //< offset of the first byte of extra_buf
00206     U_32 extra_buf_size;       //< allocated size of extra_buf
00207     U_32 extra_buf_data;       //< amount of byte of information in extra_buf
00208     char *extra_buf;           //< additional read encrypted that follow what is in encrypted_buf used to check for clear data after encrypted data
00209         //
00210     bool weof;                 //< whether write_end_of_file() has been called
00211     bool reof;                 //< whether we reached eof while reading
00212     archive_version reading_ver;//< archive format we currently read
00213     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
00214 
00215 
00216     void detruit();
00217     void copy_from(const tronconneuse & ref);
00218     U_32 fill_buf();       // returns the position (of the next read op) inside the buffer and fill the buffer with clear data
00219     void flush();          // flush any pending data (write mode only) to encrypted device
00220     void init_buf();       // initialize if necessary the various buffers that relies on inherited method values
00221 
00222 
00224 
00230     void position_clear2crypt(const infinint & pos,
00231                   infinint & file_buf_start,
00232                   infinint & clear_buf_start,
00233                   infinint & pos_in_buf,
00234                   infinint & block_num);
00235 
00236     void position_crypt2clear(const infinint & pos, infinint & clear_pos);
00237         // gives the position of the next character
00238         // of clear data that corresponds to the encrypted data which index is pos
00239 
00240     bool check_current_position() { return fill_buf() < buf_byte_data; };
00241         // return true if a there is a byte of information at the given offset
00242 
00246     void remove_trailing_clear_data_from_encrypted_buf(const infinint & crypt_offset);
00247 
00248     };
00249 
00251 
00252 } // end of namespace
00253 
00254 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines