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

Loading...
Searching...
No Matches
secu_string.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// dar - disk archive - a backup/restoration program
3// Copyright (C) 2002-2026 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, see the AUTHOR file
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
52
53 class secu_string
54 {
55 public:
57
62 static bool is_string_secured();
63
65
68 secu_string(U_I storage_size = 0) { init(storage_size); };
69
71
73 secu_string(const char *ptr, U_I size) { init(size); append_at(0, ptr, size); };
74
76 secu_string(const secu_string & ref) { copy_from(ref); };
77
79 secu_string(secu_string && ref) noexcept { nullifyptr(); move_from(std::move(ref)); };
80
82 secu_string & operator = (const secu_string & ref) { clean_and_destroy(); copy_from(ref); return *this; };
83
85 secu_string & operator = (secu_string && ref) noexcept { move_from(std::move(ref)); return *this; };
86
88 ~secu_string() noexcept { clean_and_destroy(); };
89
90
91 bool operator != (const std::string & ref) const { return ! (*this == ref); };
92 bool operator != (const secu_string & ref) const { return ! (*this == ref); };
93 bool operator == (const std::string &ref) const;
94 bool operator == (const secu_string &ref) const;
95
96
98
104 void set(int fd, U_I size);
105
107
115 void append_at(U_I offset, const char *ptr, U_I size);
116
118 void append_at(U_I offset, int fd, U_I size);
119
121 void append(const char *ptr, U_I size) { append_at(get_size(), ptr, size); };
122
124 void append(int fd, U_I size) { append_at(get_size(), fd, size); };
125
129 void reduce_string_size_to(U_I pos);
130
132 void expand_string_size_to(U_I size);
133
134
136 void clear() { if(!zero_length) *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 { if(zero_length) return nullptr; return mem == nullptr ? throw SRC_BUG : mem; };
153 char* c_str() { if(zero_length) return nullptr; return mem == nullptr ? throw SRC_BUG : mem; };
154 void set_size(U_I size);
155
157 char * get_array();
158
160
162 char & operator[] (U_I index);
163 char operator[](U_I index) const { return (const_cast<secu_string *>(this))->operator[](index); };
164
166 U_I get_size() const { if(zero_length) return 0; if(string_size == nullptr) throw SRC_BUG; return *string_size; }; // returns the size of the string
167
169 bool empty() const { if(zero_length) return true; if(string_size == nullptr) throw SRC_BUG; return *string_size == 0; };
170
172 U_I get_allocated_size() const { if(zero_length) return 0; return zero_length ? 0 : *allocated_size - 1; };
173
174 private:
175 bool zero_length;
176 U_I *allocated_size;
177 char *mem;
178 U_I *string_size;
179
180 void nullifyptr() noexcept { allocated_size = string_size = nullptr; mem = nullptr; zero_length = true; };
181 void init(U_I size);
182 void copy_from(const secu_string & ref);
183 void move_from(secu_string && ref) noexcept;
184 bool compare_with(const char *ptr, U_I size) const; // return true if given sequence is the same as the one stored in "this"
185 void clean_and_destroy();
186 };
187
189
190} // end of namespace
191
192#endif
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::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::append
void append(int fd, U_I size)
append some data at the end of the string
Definition secu_string.hpp:124
libdar::secu_string::empty
bool empty() const
tell whether string is empty
Definition secu_string.hpp:169
libdar::secu_string::c_str
const char * c_str() const
get access to the secure string
Definition secu_string.hpp:152
libdar::secu_string::get_allocated_size
U_I get_allocated_size() const
get the size of the allocated secure space
Definition secu_string.hpp:172
libdar::secu_string::set
void set(int fd, U_I size)
fill the object with data
libdar::secu_string::secu_string
secu_string(const secu_string &ref)
the copy constructor
Definition secu_string.hpp:76
libdar::secu_string::randomize
void randomize(U_I size)
set the string to randomize string of given size
libdar::secu_string::~secu_string
~secu_string() noexcept
the destructor (set memory to zero before releasing it)
Definition secu_string.hpp:88
libdar::secu_string::secu_string
secu_string(const char *ptr, U_I size)
constructor 2
Definition secu_string.hpp:73
libdar::secu_string::get_size
U_I get_size() const
get the size of the string
Definition secu_string.hpp:166
libdar::secu_string::operator=
secu_string & operator=(const secu_string &ref)
the assignment operator
Definition secu_string.hpp:82
libdar::secu_string::clear
void clear()
clear the string (set to an empty string)
Definition secu_string.hpp:136
libdar::secu_string::operator[]
char & operator[](U_I index)
get access to the secure string by index
libdar::secu_string::secu_string
secu_string(secu_string &&ref) noexcept
the move constructor
Definition secu_string.hpp:79
libdar::secu_string::append_at
void append_at(U_I offset, int fd, U_I size)
append some data to the string
libdar::secu_string::expand_string_size_to
void expand_string_size_to(U_I size)
set the string size within the allocated secure memory
libdar::secu_string::secu_string
secu_string(U_I storage_size=0)
constructor 1
Definition secu_string.hpp:68
libdar::secu_string::is_string_secured
static bool is_string_secured()
to know if secure memory is available
libdar::secu_string::allocated_size
U_I * allocated_size
stores the allocated size of the secu_string
Definition secu_string.hpp:176
libdar::secu_string::mem
char * mem
pointer to allocated block of data
Definition secu_string.hpp:177
libdar::secu_string::string_size
U_I * string_size
stores the string info size in the secu string (*string_size < *allocated_size)
Definition secu_string.hpp:178
libdar::secu_string::zero_length
bool zero_length
true if none of the other field are set due to zero byte length string requested
Definition secu_string.hpp:175
libdar::secu_string::get_array
char * get_array()
non constant flavor of direct secure memory access
libdar::secu_string::reduce_string_size_to
void reduce_string_size_to(U_I pos)
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:121
libdar::secu_string::init
void init(U_I size)
to be used at creation time or after clean_and_destroy() only
libdar::secu_string::copy_from
void copy_from(const secu_string &ref)
to be used at creation time or after clean_and_destroy() only
erreurs.hpp
contains all the excetion class thrown by libdar
integers.hpp
are defined here basic integer types that tend to be portable
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