Loading...
Searching...
No Matches
secu_string.hpp
Go to the documentation of this file.
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2026 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;
bool operator == (const secu_string &ref) const;
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(get_size(), ptr, size); };
void append(int fd, U_I size) { append_at(get_size(), fd, size); };
void reduce_string_size_to(U_I pos);
void expand_string_size_to(U_I size);
void clear() { if(!zero_length) *string_size = 0; };
void resize(U_I size) { clean_and_destroy(); init(size); };
void randomize(U_I size);
const char* c_str() const { if(zero_length) return nullptr; return mem == nullptr ? throw SRC_BUG : mem; };
char* c_str() { if(zero_length) return nullptr; return mem == nullptr ? throw SRC_BUG : mem; };
void set_size(U_I size);
char * get_array();
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(zero_length) return 0; if(string_size == nullptr) throw SRC_BUG; return *string_size; }; // returns the size of the string
bool empty() const { if(zero_length) return true; if(string_size == nullptr) throw SRC_BUG; return *string_size == 0; };
U_I get_allocated_size() const { if(zero_length) return 0; return zero_length ? 0 : *allocated_size - 1; };
private:
bool zero_length;
U_I *allocated_size;
char *mem;
U_I *string_size;
void nullifyptr() noexcept { allocated_size = string_size = nullptr; mem = nullptr; zero_length = true; };
void init(U_I size);
void copy_from(const secu_string & ref);
void move_from(secu_string && ref) noexcept;
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::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