# 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 : dar.linux@free.fr
    00020 /*********************************************************************/
    00021 // $Id: tronconneuse.hpp,v 1.7.4.1 2007/07/22 16:35:00 edrusb Rel $
    00022 //
    00023 /*********************************************************************/
    00024 //
    00025
    00030
    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
    00041 namespace libdar
    00042 {
    00043
    00045
    00058     class tronconneuse : public generic_file
    00059     {
    00060     public:
    00062
    00068         tronconneuse(user_interaction & dialog, U_32 block_size, generic_file & encrypted_side);
    00069
    00071         tronconneuse(const tronconneuse & ref) : generic_file(ref) { copy_from(ref); };
    00072
    00074         tronconneuse & operator = (const tronconneuse & ref);
    00075
    00077         virtual ~tronconneuse() { detruit(); };
    00078
    00080         bool skip(const infinint & pos);
    00082         bool skip_to_eof();
    00084         bool skip_relative(S_I x);
    00086         infinint get_position() { return current_position; };
    00087
    00089
    00093         void write_end_of_file() { flush(); weof = true; };
    00094
    00095     private:
    00096
    00098
    00100         S_I inherited_read(char *a, size_t size);
    00101
    00103
    00105         S_I inherited_write(const char *a, size_t size);
    00106
    00107     protected:
    00109
    00115         virtual U_32 encrypted_block_size_for(U_32 clear_block_size) = 0;
    00116
    00118
    00125         virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size) = 0;
    00126
    00128
    00137         virtual U_32 encrypt_data(const infinint & block_num,
    00138                                   const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
    00139                                   char *crypt_buf, U_32 crypt_size) = 0;
    00140
    00142
    00149         virtual U_32 decrypt_data(const infinint & block_num,
    00150                                   const char *crypt_buf, const U_32 crypt_size,
    00151                                   char *clear_buf, U_32 clear_size) = 0;
    00152
    00153
    00154     private:
    00155         infinint initial_shift;    // the initial_shift first bytes of the underlying file are not encrypted
    00156         infinint buf_offset;       // offset of the first byte in buf
    00157         U_32 buf_byte_data;        // number of byte of information in buf (buf_byte_data <= buf_size)
    00158         U_32 buf_size;             // size of allocated memory for clear data in buf
    00159         char *buf;                 // decrypted data (or data to encrypt)
    00160         U_32 clear_block_size;     // max amount of data that will be encrypted (must stay less than buf_size)
    00161         infinint current_position; // position of the next character to read or write
    00162         infinint block_num;        // block number we next read or write
    00163         generic_file *encrypted;   // generic_file where is put / get the encrypted data
    00164         char *encrypted_buf;       // buffer of encrypted data (read or to write)
    00165         U_32 encrypted_buf_size;   // allocated size of encrypted_buf
    00166         bool weof;                 // whether write_end_of_file() has been called
    00167
    00168
    00169         void detruit();
    00170         void copy_from(const tronconneuse & ref);
    00171         U_32 fill_buf();       // returns the position (of the next read) inside the buffer and fill the buffer with clear data
    00172         void flush();          // flush any pending data (write mode only) to encrypted device
    00173         void init_buf();       // initialize if necessary the various buffers that relies on inherited method values
    00174
    00175         void position_clear2crypt(const infinint & pos, infinint & file_buf_start,
    00176                                   infinint & clear_buf_start, infinint & pos_in_buf, infinint & block_num);
    00177             // pos (input) is the position in the clear data
    00178             // file_buf_start (output) is the position of the begining of the crypted block where can be found the data
    00179             // file_buf_clear (output) is the position of the beginning of the corresponding clear block
    00180             // pos_in_buf (output) is the position in the clear block of the 'pos' offset
    00181             // block_num (output) is the block number we have our requested position inside
    00182
    00183         void position_crypt2clear(const infinint & pos, infinint & clear_pos);
    00184             // gives the position of the next character
    00185             // of clear data that corresponds to the encrypted data which index is pos
    00186
    00187         bool check_current_position() { return fill_buf() < buf_byte_data; };
    00188             // return true if a there is a byte of information at the given offset
    00189     };
    00190
    00191
    00192 } // end of namespace
    00193
    00194 #endif