Loading...
Searching...
No Matches
tronconneuse.hpp
Go to the documentation of this file.
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2024 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// to contact the author, see the AUTHOR file
/*********************************************************************/
#ifndef TRONCONNEUSE_HPP
#define TRONCONNEUSE_HPP
#include "../my_config.h"
#include <string>
#include "infinint.hpp"
#include "archive_version.hpp"
#include "crypto_module.hpp"
#include "proto_tronco.hpp"
namespace libdar
{
class tronconneuse : public proto_tronco
{
public:
tronconneuse(U_32 block_size,
generic_file & encrypted_side,
const archive_version & reading_ver,
std::unique_ptr<crypto_module> & ptr);
tronconneuse(const tronconneuse & ref) : proto_tronco(ref) { copy_from(ref); };
tronconneuse(tronconneuse && ref) noexcept: proto_tronco(std::move(ref)) { nullifyptr(); move_from(std::move(ref)); };
tronconneuse & operator = (const tronconneuse & ref) { detruit(); proto_tronco::operator = (ref); copy_from(ref); return *this; };
tronconneuse & operator = (tronconneuse && ref) noexcept { proto_tronco::operator = (std::move(ref)); move_from(std::move(ref)); return *this; };
virtual ~tronconneuse() noexcept override { detruit(); }; // must not write pure virtual method from here, directly or not
virtual bool skippable(skippability direction, const infinint & amount) override;
virtual bool skip(const infinint & pos) override;
virtual bool skip_to_eof() override;
virtual bool skip_relative(S_I x) override;
virtual bool truncatable(const infinint & pos) const override { return false; };
virtual infinint get_position() const override { if(is_terminated()) throw SRC_BUG; return current_position; };
virtual void write_end_of_file() override { if(is_terminated()) throw SRC_BUG; flush(); weof = true; };
virtual void set_initial_shift(const infinint & x) override { initial_shift = x; };
virtual void set_callback_trailing_clear_data(trailing_clear_data_callback callback) override { trailing_clear_data = callback; };
virtual U_32 get_clear_block_size() const override { return clear_block_size; };
private:
virtual void inherited_read_ahead(const infinint & amount) override;
virtual U_I inherited_read(char *a, U_I size) override;
virtual void inherited_write(const char *a, U_I size) override;
virtual void inherited_truncate(const infinint & pos) override { throw SRC_BUG; }; // no skippability in write mode, so no truncate possibility neither
virtual void inherited_sync_write() override { flush(); };
virtual void inherited_flush_read() override { buf_byte_data = 0; };
virtual void inherited_terminate() override { flush(); };
protected:
const archive_version & get_reading_version() const { return reading_ver; };
private:
infinint initial_shift;
//
infinint buf_offset;
U_32 buf_byte_data;
U_32 buf_size;
char *buf;
//
U_32 clear_block_size;
infinint current_position;
infinint block_num;
generic_file *encrypted;
//
U_32 encrypted_buf_size;
U_32 encrypted_buf_data;
char *encrypted_buf;
//
infinint extra_buf_offset;
U_32 extra_buf_size;
U_32 extra_buf_data;
char *extra_buf;
//
bool weof;
bool reof;
archive_version reading_ver;
std::unique_ptr<crypto_module> crypto;
trailing_clear_data_callback trailing_clear_data;
void nullifyptr() noexcept;
void detruit() noexcept;
void copy_from(const tronconneuse & ref);
void move_from(tronconneuse && ref) noexcept;
U_32 fill_buf();
void flush();
void init_buf();
void position_clear2crypt(const infinint & pos,
infinint & file_buf_start,
infinint & clear_buf_start,
infinint & pos_in_buf,
infinint & block_num);
void position_crypt2clear(const infinint & pos, infinint & clear_pos);
bool check_current_position() { return fill_buf() < buf_byte_data; };
void remove_trailing_clear_data_from_encrypted_buf(const infinint & crypt_offset);
};
} // end of namespace
#endif
archive_version.hpp
class archive_version that rules which archive format to follow
libdar::archive_version
class archive_version manages the version of the archive format
Definition archive_version.hpp:47
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition generic_file.hpp:77
libdar::generic_file::operator=
generic_file & operator=(const generic_file &ref)
assignment operator
Definition generic_file.hpp:89
libdar::generic_file::is_terminated
bool is_terminated() const
Definition generic_file.hpp:327
libdar::infinint
the arbitrary large positive integer class
Definition real_infinint.hpp:62
libdar::tronconneuse
this is a partial implementation of the generic_file interface to cypher/decypher data block by block...
Definition tronconneuse.hpp:54
libdar::tronconneuse::inherited_read
virtual U_I inherited_read(char *a, U_I size) override
this protected inherited method is now private for inherited classes of tronconneuse
libdar::tronconneuse::~tronconneuse
virtual ~tronconneuse() noexcept override
destructor
Definition tronconneuse.hpp:82
libdar::tronconneuse::skip_to_eof
virtual bool skip_to_eof() override
inherited from generic_file
libdar::tronconneuse::set_initial_shift
virtual void set_initial_shift(const infinint &x) override
this method to modify the initial shift. This overrides the constructor "no_initial_shift" of the con...
Definition tronconneuse.hpp:108
libdar::tronconneuse::tronconneuse
tronconneuse(const tronconneuse &ref)
copy constructor
Definition tronconneuse.hpp:70
libdar::tronconneuse::buf_size
U_32 buf_size
size of allocated memory for clear data in buf
Definition tronconneuse.hpp:155
libdar::tronconneuse::encrypted
generic_file * encrypted
generic_file where is put / get the encrypted data
Definition tronconneuse.hpp:161
libdar::tronconneuse::current_position
infinint current_position
position of the next character to read or write from the upper layer perspective, offset zero is the ...
Definition tronconneuse.hpp:159
libdar::tronconneuse::buf_offset
infinint buf_offset
offset of the first byte in buf
Definition tronconneuse.hpp:153
libdar::tronconneuse::operator=
tronconneuse & operator=(const tronconneuse &ref)
assignment operator
Definition tronconneuse.hpp:76
libdar::tronconneuse::encrypted_buf
char * encrypted_buf
buffer of encrypted data (read or to write)
Definition tronconneuse.hpp:165
libdar::tronconneuse::crypto
std::unique_ptr< crypto_module > crypto
wraps the per block encryption/decryption routines
Definition tronconneuse.hpp:175
libdar::tronconneuse::extra_buf_data
U_32 extra_buf_data
amount of byte of information in extra_buf
Definition tronconneuse.hpp:169
libdar::tronconneuse::buf
char * buf
decrypted data (or data to encrypt)
Definition tronconneuse.hpp:156
libdar::tronconneuse::inherited_truncate
virtual void inherited_truncate(const infinint &pos) override
this prorected inherited method is now private for inherited classed of tronconneuse
Definition tronconneuse.hpp:134
libdar::tronconneuse::encrypted_buf_size
U_32 encrypted_buf_size
allocated size of encrypted_buf
Definition tronconneuse.hpp:163
libdar::tronconneuse::inherited_read_ahead
virtual void inherited_read_ahead(const infinint &amount) override
inherited from generic_file
libdar::tronconneuse::position_clear2crypt
void position_clear2crypt(const infinint &pos, infinint &file_buf_start, infinint &clear_buf_start, infinint &pos_in_buf, infinint &block_num)
convert clear position to corresponding position in the encrypted data
libdar::tronconneuse::initial_shift
infinint initial_shift
the initial_shift first bytes of the underlying file are not encrypted
Definition tronconneuse.hpp:151
libdar::tronconneuse::flush
void flush()
flush any pending data (write mode only) to encrypted device
libdar::tronconneuse::extra_buf
char * extra_buf
additional read encrypted that follow what is in encrypted_buf used to check for clear data after enc...
Definition tronconneuse.hpp:170
libdar::tronconneuse::inherited_terminate
virtual void inherited_terminate() override
this protected inherited method is now private for inherited classes of tronconneuse
Definition tronconneuse.hpp:144
libdar::tronconneuse::block_num
infinint block_num
block number we next read or write
Definition tronconneuse.hpp:160
libdar::tronconneuse::write_end_of_file
virtual void write_end_of_file() override
in write_only mode indicate that end of file is reached
Definition tronconneuse.hpp:103
libdar::tronconneuse::fill_buf
U_32 fill_buf()
returns the position (of the next read op) inside the buffer and fill the buffer with clear data
libdar::tronconneuse::trailing_clear_data
trailing_clear_data_callback trailing_clear_data
callback function that gives the amount of clear data found at the end of the given file
Definition tronconneuse.hpp:176
libdar::tronconneuse::extra_buf_offset
infinint extra_buf_offset
offset of the first byte of extra_buf
Definition tronconneuse.hpp:167
libdar::tronconneuse::tronconneuse
tronconneuse(tronconneuse &&ref) noexcept
move constructor
Definition tronconneuse.hpp:73
libdar::tronconneuse::reof
bool reof
whether we reached eof while reading
Definition tronconneuse.hpp:173
libdar::tronconneuse::skippable
virtual bool skippable(skippability direction, const infinint &amount) override
inherited from generic_file
libdar::tronconneuse::weof
bool weof
whether write_end_of_file() has been called
Definition tronconneuse.hpp:172
libdar::tronconneuse::skip_relative
virtual bool skip_relative(S_I x) override
inherited from generic_file
libdar::tronconneuse::inherited_flush_read
virtual void inherited_flush_read() override
this protected inherited method is now private for inherited classes of tronconneuse
Definition tronconneuse.hpp:141
libdar::tronconneuse::get_clear_block_size
virtual U_32 get_clear_block_size() const override
returns the block size give to constructor
Definition tronconneuse.hpp:116
libdar::tronconneuse::encrypted_buf_data
U_32 encrypted_buf_data
amount of byte of information in encrypted_buf
Definition tronconneuse.hpp:164
libdar::tronconneuse::clear_block_size
U_32 clear_block_size
max amount of data that will be encrypted at once (must stay less than buf_size)
Definition tronconneuse.hpp:158
libdar::tronconneuse::reading_ver
archive_version reading_ver
archive format we currently read
Definition tronconneuse.hpp:174
libdar::tronconneuse::get_position
virtual infinint get_position() const override
inherited from generic_file
Definition tronconneuse.hpp:95
libdar::tronconneuse::remove_trailing_clear_data_from_encrypted_buf
void remove_trailing_clear_data_from_encrypted_buf(const infinint &crypt_offset)
libdar::tronconneuse::check_current_position
bool check_current_position()
return true if a there is a byte of information at the given offset
Definition tronconneuse.hpp:206
libdar::tronconneuse::skip
virtual bool skip(const infinint &pos) override
inherited from generic_file
libdar::tronconneuse::init_buf
void init_buf()
initialize if necessary the various buffers that relies on inherited method values
libdar::tronconneuse::inherited_write
virtual void inherited_write(const char *a, U_I size) override
inherited from generic_file
libdar::tronconneuse::extra_buf_size
U_32 extra_buf_size
allocated size of extra_buf
Definition tronconneuse.hpp:168
libdar::tronconneuse::tronconneuse
tronconneuse(U_32 block_size, generic_file &encrypted_side, const archive_version &reading_ver, std::unique_ptr< crypto_module > &ptr)
This is the constructor.
libdar::tronconneuse::buf_byte_data
U_32 buf_byte_data
number of byte of information in buf (buf_byte_data <= buf_size)
Definition tronconneuse.hpp:154
libdar::tronconneuse::truncatable
virtual bool truncatable(const infinint &pos) const override
inherited from generic_file
Definition tronconneuse.hpp:93
libdar::tronconneuse::set_callback_trailing_clear_data
virtual void set_callback_trailing_clear_data(trailing_clear_data_callback callback) override
Definition tronconneuse.hpp:113
libdar::tronconneuse::inherited_sync_write
virtual void inherited_sync_write() override
this protected inherited method is now private for inherited classes of tronconneuse
Definition tronconneuse.hpp:137
libdar::tronconneuse::position_crypt2clear
void position_crypt2clear(const infinint &pos, infinint &clear_pos)
crypto_module.hpp
per block cryptography implementation
libdar::trailing_clear_data_callback
infinint(* trailing_clear_data_callback)(generic_file &below, const archive_version &reading_ver)
the trailing_clear_data_callback call back is a mean by which the upper layer cat tell when encrypted...
Definition proto_tronco.hpp:58
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
my_config.h
include macro defined by the configure script and some specific additional ones
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47
proto_tronco.hpp
defines common interface for tronconneuse and parallel_tronconneuse