Loading...
Searching...
No Matches
secu_string.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 SECU_STRING_HPP
#define SECU_STRING_HPP
#include "../my_config.h"
#include <string>
#include "integers.hpp"
#include "erreurs.hpp"
namespace libdar
{
class secu_string
{
public:
static bool is_string_secured();
secu_string(U_I storage_size = 0) { init(storage_size); };
secu_string(const char *ptr, U_I size) { init(size); append_at(0, ptr, size); };
secu_string(const secu_string & ref) { copy_from(ref); };
secu_string(secu_string && ref) noexcept { nullifyptr(); move_from(std::move(ref)); };
secu_string & operator = (const secu_string & ref) { clean_and_destroy(); copy_from(ref); return *this; };
secu_string & operator = (secu_string && ref) noexcept { move_from(std::move(ref)); return *this; };
~secu_string() noexcept { clean_and_destroy(); };
bool operator != (const std::string & ref) const { return ! (*this == ref); };
bool operator != (const secu_string & ref) const { return ! (*this == ref); };
bool operator == (const std::string &ref) const { return compare_with(ref.c_str(),(U_I)(ref.size())); };
bool operator == (const secu_string &ref) const { return compare_with(ref.mem, *(ref.string_size)); };
void set(int fd, U_I size);
void append_at(U_I offset, const char *ptr, U_I size);
void append_at(U_I offset, int fd, U_I size);
void append(const char *ptr, U_I size) { append_at(*string_size, ptr, size); };
void append(int fd, U_I size) { append_at(*string_size, fd, size); };
void reduce_string_size_to(U_I pos);
void expand_string_size_to(U_I size);
void clear() { *string_size = 0; };
void resize(U_I size) { clean_and_destroy(); init(size); };
void randomize(U_I size);
const char* c_str() const { return mem == nullptr ? throw SRC_BUG : mem; };
char* c_str() { return mem == nullptr ? throw SRC_BUG : mem; };
void set_size(U_I size);
char * get_array() { return mem == nullptr ? throw SRC_BUG : mem; };
char & operator[] (U_I index);
char operator[](U_I index) const { return (const_cast<secu_string *>(this))->operator[](index); };
U_I get_size() const { if(string_size == nullptr) throw SRC_BUG; return *string_size; }; // returns the size of the string
bool empty() const { if(string_size == nullptr) throw SRC_BUG; return *string_size == 0; };
U_I get_allocated_size() const { return *allocated_size - 1; };
private:
U_I *allocated_size;
char *mem;
U_I *string_size;
void nullifyptr() noexcept { allocated_size = string_size = nullptr; mem = nullptr; };
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
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); };
bool compare_with(const char *ptr, U_I size) const; // return true if given sequence is the same as the one stored in "this"
void clean_and_destroy();
};
} // end of namespace
#endif
libdar::secu_string
class secu_string
Definition secu_string.hpp:54
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::get_array
char * get_array()
non constant flavor of direct secure memory access
Definition secu_string.hpp:157
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
erreurs.hpp
contains all the excetion class thrown by libdar
integers.hpp
are defined here basic integer types that tend to be portable
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47