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

secu_string.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
31
32 #ifndef SECU_STRING_HPP
33 #define SECU_STRING_HPP
34
35 #include "../my_config.h"
36
37 #include <string>
38 #include "integers.hpp"
39 #include "erreurs.hpp"
40
41 namespace libdar
42 {
43
46
48
56
57 class secu_string
58 {
59 public:
61
66 static bool is_string_secured();
67
69
72 secu_string(U_I storage_size = 0) { init(storage_size); };
73
75
77 secu_string(const char *ptr, U_I size) { init(size); append_at(0, ptr, size); };
78
80 secu_string(const secu_string & ref) { copy_from(ref); };
81
83 secu_string(secu_string && ref) noexcept { nullifyptr(); move_from(std::move(ref)); };
84
86 secu_string & operator = (const secu_string & ref) { clean_and_destroy(); copy_from(ref); return *this; };
87
89 secu_string & operator = (secu_string && ref) noexcept { move_from(std::move(ref)); return *this; };
90
92 ~secu_string() noexcept(false) { clean_and_destroy(); };
93
94
95 bool operator != (const std::string & ref) const { return ! (*this == ref); };
96 bool operator != (const secu_string & ref) const { return ! (*this == ref); };
97 bool operator == (const std::string &ref) const { return compare_with(ref.c_str(),(U_I)(ref.size())); };
98 bool operator == (const secu_string &ref) const { return compare_with(ref.mem, *(ref.string_size)); };
99
100
102
108 void set(int fd, U_I size);
109
111
119 void append_at(U_I offset, const char *ptr, U_I size);
120
122 void append_at(U_I offset, int fd, U_I size);
123
125 void append(const char *ptr, U_I size) { append_at(*string_size, ptr, size); };
126
128 void append(int fd, U_I size) { append_at(*string_size, fd, size); };
129
133 void reduce_string_size_to(U_I pos);
134
136 void clear() { *string_size = 0; };
137
139
141 void resize(U_I size) { clean_and_destroy(); init(size); };
142
144
146 void randomize(U_I size);
147
149
152 const char*c_str() const { return mem == nullptr ? throw SRC_BUG : mem; };
153
155 char * get_array() { return mem == nullptr ? throw SRC_BUG : mem; };
156
158
160 char & operator[] (U_I index);
161 char operator[](U_I index) const { return (const_cast<secu_string *>(this))->operator[](index); };
162
164 U_I get_size() const { if(string_size == nullptr) throw SRC_BUG; return *string_size; }; // returns the size of the string
165
167 bool empty() const { if(string_size == nullptr) throw SRC_BUG; return *string_size == 0; };
168
170 U_I get_allocated_size() const { return *allocated_size - 1; };
171
172 private:
173 U_I *allocated_size;
174 char *mem;
175 U_I *string_size;
176
177 void nullifyptr() noexcept { allocated_size = string_size = nullptr; mem = nullptr; };
178 void init(U_I size); //< to be used at creation time or after clean_and_destroy() only
179 void copy_from(const secu_string & ref); //< to be used at creation time or after clean_and_destroy() only
180 void move_from(secu_string && ref) noexcept { std::swap(allocated_size, ref.allocated_size); std::swap(mem, ref.mem); std::swap(string_size, ref.string_size); };
181 bool compare_with(const char *ptr, U_I size) const; // return true if given sequence is the same as the one stored in "this"
182 void clean_and_destroy();
183 };
184
186
187 } // end of namespace
188
189 #endif
libdar::secu_string::randomize
void randomize(U_I size)
set the string to randomize string of given size
libdar::secu_string::append
void append(const char *ptr, U_I size)
append some data at the end of the string
Definition: secu_string.hpp:125
libdar::secu_string::get_allocated_size
U_I get_allocated_size() const
get the size of the allocated secure space
Definition: secu_string.hpp:170
libdar::secu_string::empty
bool empty() const
tell whether string is empty
Definition: secu_string.hpp:167
integers.hpp
are defined here basic integer types that tend to be portable
libdar::secu_string::c_str
const char * c_str() const
get access to the secure string
Definition: secu_string.hpp:152
libdar::secu_string::append
void append(int fd, U_I size)
append some data at the end of the string
Definition: secu_string.hpp:128
libdar::secu_string::reduce_string_size_to
void reduce_string_size_to(U_I pos)
libdar::secu_string::operator=
secu_string & operator=(const secu_string &ref)
the assignment operator
Definition: secu_string.hpp:86
libdar::secu_string::secu_string
secu_string(const secu_string &ref)
the copy constructor
Definition: secu_string.hpp:80
erreurs.hpp
contains all the excetion class thrown by libdar
libdar::secu_string::get_size
U_I get_size() const
get the size of the string
Definition: secu_string.hpp:164
libdar::secu_string::operator[]
char & operator[](U_I index)
get access to the secure string by index
libdar::secu_string::set
void set(int fd, U_I size)
fill the object with data
libdar::secu_string::resize
void resize(U_I size)
clear and resize the string to the defined allocated size
Definition: secu_string.hpp:141
libdar::secu_string::is_string_secured
static bool is_string_secured()
to know if secure memory is available
libdar::secu_string::clear
void clear()
clear the string (set to an empty string)
Definition: secu_string.hpp:136
libdar::secu_string::secu_string
secu_string(U_I storage_size=0)
constructor 1
Definition: secu_string.hpp:72
libdar::secu_string::~secu_string
~secu_string() noexcept(false)
the destructor (set memory to zero before releasing it)
Definition: secu_string.hpp:92
libdar::secu_string
class secu_string
Definition: secu_string.hpp:58
libdar::secu_string::append_at
void append_at(U_I offset, const char *ptr, U_I size)
append some data to the string at a given offset
libdar::secu_string::secu_string
secu_string(const char *ptr, U_I size)
constructor 2
Definition: secu_string.hpp:77
libdar::secu_string::get_array
char * get_array()
non constant flavor of direct secure memory access
Definition: secu_string.hpp:155
libdar::secu_string::secu_string
secu_string(secu_string &&ref) noexcept
the move constructor
Definition: secu_string.hpp:83
libdar::secu_string::append_at
void append_at(U_I offset, int fd, U_I size)
append some data to the string
libdar
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47