| Disk ARchive 2.6.12 Full featured and portable backup and archiving tool

tronconneuse.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2020 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21
27
28 #ifndef TRONCONNEUSE_HPP
29 #define TRONCONNEUSE_HPP
30
31 #include "../my_config.h"
32 #include <string>
33
34 #include "infinint.hpp"
35 #include "generic_file.hpp"
36 #include "archive_version.hpp"
37
38 namespace libdar
39 {
40
43
44
46
58 class tronconneuse : public generic_file
59 {
60 public:
62
70 tronconneuse(U_32 block_size, generic_file & encrypted_side, bool no_initial_shift, const archive_version & reading_ver);
71
73 tronconneuse(const tronconneuse & ref) : generic_file(ref) { copy_from(ref); };
74
76 tronconneuse(tronconneuse && ref) noexcept: generic_file(std::move(ref)) { nullifyptr(); move_from(std::move(ref)); };
77
79 tronconneuse & operator = (const tronconneuse & ref);
80
82 tronconneuse & operator = (tronconneuse && ref) noexcept { generic_file::operator = (std::move(ref)); move_from(std::move(ref)); return *this; };
83
85 virtual ~tronconneuse() override { detruit(); }; // must not write pure virtual method from here, directly or not
86
88 virtual bool skippable(skippability direction, const infinint & amount) override;
90 virtual bool skip(const infinint & pos) override;
92 virtual bool skip_to_eof() override;
94 virtual bool skip_relative(S_I x) override;
96 virtual infinint get_position() const override { if(is_terminated()) throw SRC_BUG; return current_position; };
97
99
104 void write_end_of_file() { if(is_terminated()) throw SRC_BUG; flush(); weof = true; };
105
106
108
109 void set_initial_shift(const infinint & x) { initial_shift = x; };
110
114 void set_callback_trailing_clear_data(infinint (*call_back)(generic_file & below, const archive_version & reading_ver)) { trailing_clear_data = call_back; };
115
117 U_32 get_clear_block_size() const { return clear_block_size; };
118
119 private:
120
122
124 virtual void inherited_read_ahead(const infinint & amount) override;
125
127 virtual U_I inherited_read(char *a, U_I size) override;
128
130
132 virtual void inherited_write(const char *a, U_I size) override;
133
135 virtual void inherited_sync_write() override { flush(); };
136
138 virtual void inherited_flush_read() override { buf_byte_data = 0; };
139
141 virtual void inherited_terminate() override {};
142
143 protected:
145
151 virtual U_32 encrypted_block_size_for(U_32 clear_block_size) = 0;
152
154
161 virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size) = 0;
162
164
173 virtual U_32 encrypt_data(const infinint & block_num,
174 const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
175 char *crypt_buf, U_32 crypt_size) = 0;
176
178
185 virtual U_32 decrypt_data(const infinint & block_num,
186 const char *crypt_buf, const U_32 crypt_size,
187 char *clear_buf, U_32 clear_size) = 0;
188
189 protected:
190 const archive_version & get_reading_version() const { return reading_ver; };
191
192
193 private:
194 infinint initial_shift;
195 //
196 infinint buf_offset;
197 U_32 buf_byte_data;
198 U_32 buf_size;
199 char *buf;
200 //
201 U_32 clear_block_size;
202 infinint current_position;
203 infinint block_num;
204 generic_file *encrypted;
205 //
206 U_32 encrypted_buf_size;
207 U_32 encrypted_buf_data;
208 char *encrypted_buf;
209 //
210 infinint extra_buf_offset;
211 U_32 extra_buf_size;
212 U_32 extra_buf_data;
213 char *extra_buf;
214 //
215 bool weof;
216 bool reof;
217 archive_version reading_ver;
218 infinint (*trailing_clear_data)(generic_file & below, const archive_version & reading_ver);
219
220
221 void nullifyptr() noexcept;
222 void detruit() noexcept;
223 void copy_from(const tronconneuse & ref);
224 void move_from(tronconneuse && ref) noexcept;
225 U_32 fill_buf();
226 void flush();
227 void init_buf();
228
229
231
237 void position_clear2crypt(const infinint & pos,
238 infinint & file_buf_start,
239 infinint & clear_buf_start,
240 infinint & pos_in_buf,
241 infinint & block_num);
242
245 void position_crypt2clear(const infinint & pos, infinint & clear_pos);
246
248 bool check_current_position() { return fill_buf() < buf_byte_data; };
249
253 void remove_trailing_clear_data_from_encrypted_buf(const infinint & crypt_offset);
254
255 };
256
258
259 } // end of namespace
260
261 #endif
libdar::tronconneuse::extra_buf_size
U_32 extra_buf_size
allocated size of extra_buf
Definition: tronconneuse.hpp:211
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::generic_file::generic_file
generic_file(gf_mode m)
main constructor
Definition: generic_file.hpp:80
libdar::tronconneuse::buf_size
U_32 buf_size
size of allocated memory for clear data in buf
Definition: tronconneuse.hpp:198
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::tronconneuse
tronconneuse(U_32 block_size, generic_file &encrypted_side, bool no_initial_shift, const archive_version &reading_ver)
This is the constructor.
libdar::tronconneuse::init_buf
void init_buf()
initialize if necessary the various buffers that relies on inherited method values
libdar::tronconneuse::operator=
tronconneuse & operator=(const tronconneuse &ref)
assignment operator
libdar::tronconneuse::clear_block_allocated_size_for
virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size)=0
it may be necessary by the inherited class have few more bytes allocated after the clear data given f...
libdar::tronconneuse::extra_buf_data
U_32 extra_buf_data
amount of byte of information in extra_buf
Definition: tronconneuse.hpp:212
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
libdar::infinint
the arbitrary large positive integer class
Definition: real_infinint.hpp:62
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:213
archive_version.hpp
class archive_version that rules which archive format to follow
libdar::tronconneuse::reading_ver
archive_version reading_ver
archive format we currently read
Definition: tronconneuse.hpp:217
libdar::tronconneuse::skippable
virtual bool skippable(skippability direction, const infinint &amount) override
inherited from generic_file
libdar::tronconneuse::reof
bool reof
whether we reached eof while reading
Definition: tronconneuse.hpp:216
libdar::tronconneuse::encrypted_buf_data
U_32 encrypted_buf_data
amount of byte of information in encrypted_buf
Definition: tronconneuse.hpp:207
libdar::tronconneuse::skip_to_eof
virtual bool skip_to_eof() override
inherited from generic_file
libdar::generic_file::is_terminated
bool is_terminated() const
Definition: generic_file.hpp:300
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
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
Definition: tronconneuse.hpp:218
libdar::archive_version
class archive_version manages the version of the archive format
Definition: archive_version.hpp:47
libdar::tronconneuse::tronconneuse
tronconneuse(const tronconneuse &ref)
copy constructor
Definition: tronconneuse.hpp:73
libdar::tronconneuse::encrypted_buf_size
U_32 encrypted_buf_size
allocated size of encrypted_buf
Definition: tronconneuse.hpp:206
libdar::tronconneuse::position_crypt2clear
void position_crypt2clear(const infinint &pos, infinint &clear_pos)
libdar::tronconneuse::encrypted_buf
char * encrypted_buf
buffer of encrypted data (read or to write)
Definition: tronconneuse.hpp:208
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:138
generic_file.hpp
class generic_file is defined here as well as class fichier
libdar::tronconneuse::skip
virtual bool skip(const infinint &pos) override
inherited from generic_file
libdar::tronconneuse::inherited_terminate
virtual void inherited_terminate() override
this protected inherited method is now private for inherited classes of tronconneuse
Definition: tronconneuse.hpp:141
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:248
libdar::tronconneuse::skip_relative
virtual bool skip_relative(S_I x) override
inherited from generic_file
libdar::tronconneuse::set_callback_trailing_clear_data
void set_callback_trailing_clear_data(infinint(*call_back)(generic_file &below, const archive_version &reading_ver))
Definition: tronconneuse.hpp:114
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:201
libdar::tronconneuse::weof
bool weof
whether write_end_of_file() has been called
Definition: tronconneuse.hpp:215
libdar::tronconneuse::initial_shift
infinint initial_shift
the initial_shift first bytes of the underlying file are not encrypted
Definition: tronconneuse.hpp:190
libdar::tronconneuse::flush
void flush()
flush any pending data (write mode only) to encrypted device
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition: generic_file.hpp:77
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:135
libdar::tronconneuse::inherited_write
virtual void inherited_write(const char *a, U_I size) override
inherited from generic_file
libdar::tronconneuse::get_position
virtual infinint get_position() const override
inherited from generic_file
Definition: tronconneuse.hpp:96
libdar::tronconneuse::buf_offset
infinint buf_offset
offset of the first byte in buf
Definition: tronconneuse.hpp:196
libdar::tronconneuse
this is a partial implementation of the generic_file interface to cypher/decypher data block by block...
Definition: tronconneuse.hpp:59
libdar::tronconneuse::~tronconneuse
virtual ~tronconneuse() override
destructor
Definition: tronconneuse.hpp:85
libdar::tronconneuse::encrypt_data
virtual U_32 encrypt_data(const infinint &block_num, const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated, char *crypt_buf, U_32 crypt_size)=0
this method encrypts the clear data given
libdar::tronconneuse::set_initial_shift
void set_initial_shift(const infinint &x)
this method to modify the initial shift. This overrides the constructor "no_initial_shift" of the con...
Definition: tronconneuse.hpp:109
libdar::tronconneuse::write_end_of_file
void write_end_of_file()
in write_only mode indicate that end of file is reached
Definition: tronconneuse.hpp:104
libdar::tronconneuse::get_clear_block_size
U_32 get_clear_block_size() const
returns the block size give to constructor
Definition: tronconneuse.hpp:117
libdar::tronconneuse::inherited_read_ahead
virtual void inherited_read_ahead(const infinint &amount) override
inherited from generic_file
libdar::tronconneuse::tronconneuse
tronconneuse(tronconneuse &&ref) noexcept
move constructor
Definition: tronconneuse.hpp:76
libdar::tronconneuse::extra_buf_offset
infinint extra_buf_offset
offset of the first byte of extra_buf
Definition: tronconneuse.hpp:210
libdar::generic_file::operator=
generic_file & operator=(const generic_file &ref)
assignment operator
Definition: generic_file.hpp:89
libdar::tronconneuse::buf
char * buf
decrypted data (or data to encrypt)
Definition: tronconneuse.hpp:199
libdar::tronconneuse::block_num
infinint block_num
block number we next read or write
Definition: tronconneuse.hpp:203
libdar::tronconneuse::encrypted
generic_file * encrypted
generic_file where is put / get the encrypted data
Definition: tronconneuse.hpp:204
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:202
libdar::tronconneuse::decrypt_data
virtual U_32 decrypt_data(const infinint &block_num, const char *crypt_buf, const U_32 crypt_size, char *clear_buf, U_32 clear_size)=0
this method decyphers data
libdar::tronconneuse::remove_trailing_clear_data_from_encrypted_buf
void remove_trailing_clear_data_from_encrypted_buf(const infinint &crypt_offset)
libdar::tronconneuse::encrypted_block_size_for
virtual U_32 encrypted_block_size_for(U_32 clear_block_size)=0
defines the size necessary to encrypt a given amount of clear data
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:197
libdar
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47