Disk ARchive 2.8.0
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-2025 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
41namespace libdar
42{
43
46
48
52
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
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:
177 char *mem;
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
class secu_string
Definition: secu_string.hpp:54
void resize(U_I size)
clear and resize the string to the defined allocated size
void append_at(U_I offset, const char *ptr, U_I size)
append some data to the string at a given offset
void append(int fd, U_I size)
append some data at the end of the string
bool empty() const
tell whether string is empty
const char * c_str() const
get access to the secure string
U_I get_allocated_size() const
get the size of the allocated secure space
void set(int fd, U_I size)
fill the object with data
secu_string(const secu_string &ref)
the copy constructor
Definition: secu_string.hpp:76
void randomize(U_I size)
set the string to randomize string of given size
~secu_string() noexcept
the destructor (set memory to zero before releasing it)
Definition: secu_string.hpp:88
secu_string(const char *ptr, U_I size)
constructor 2
Definition: secu_string.hpp:73
U_I get_size() const
get the size of the string
secu_string & operator=(const secu_string &ref)
the assignment operator
Definition: secu_string.hpp:82
void clear()
clear the string (set to an empty string)
char & operator[](U_I index)
get access to the secure string by index
secu_string(secu_string &&ref) noexcept
the move constructor
Definition: secu_string.hpp:79
void append_at(U_I offset, int fd, U_I size)
append some data to the string
void expand_string_size_to(U_I size)
set the string size within the allocated secure memory
secu_string(U_I storage_size=0)
constructor 1
Definition: secu_string.hpp:68
static bool is_string_secured()
to know if secure memory is available
U_I * allocated_size
stores the allocated size of the secu_string
char * mem
pointer to allocated block of data
U_I * string_size
stores the string info size in the secu string (*string_size < *allocated_size)
bool zero_length
true if none of the other field are set due to zero byte length string requested
char * get_array()
non constant flavor of direct secure memory access
void reduce_string_size_to(U_I pos)
void append(const char *ptr, U_I size)
append some data at the end of the string
void init(U_I size)
to be used at creation time or after clean_and_destroy() only
void copy_from(const secu_string &ref)
to be used at creation time or after clean_and_destroy() only
contains all the excetion class thrown by libdar
are defined here basic integer types that tend to be portable
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47