Disk ARchive  2.5.4
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-2052 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "on_pool.hpp"
40 
41 namespace libdar
42 {
43 
46 
48 
56 
57  class secu_string : public on_pool
58  {
59  public:
66  static bool is_string_secured();
67 
72  secu_string(U_I storage_size = 0) { init(storage_size); };
73 
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 & operator = (const secu_string & ref) { clean_and_destroy(); copy_from(ref); return *this; };
84 
85  bool operator != (const std::string & ref) const { return ! (*this == ref); };
86  bool operator != (const secu_string & ref) const { return ! (*this == ref); };
87  bool operator == (const std::string &ref) const { return compare_with(ref.c_str(),(U_I)(ref.size())); };
88  bool operator == (const secu_string &ref) const { return compare_with(ref.mem, *(ref.string_size)); };
89 
91  ~secu_string() throw(Ebug) { clean_and_destroy(); };
92 
100  void set(int fd, U_I size);
101 
111  void append_at(U_I offset, const char *ptr, U_I size);
112 
114  void append_at(U_I offset, int fd, U_I size);
115 
117  void append(const char *ptr, U_I size) { append_at(*string_size, ptr, size); };
118 
120  void append(int fd, U_I size) { append_at(*string_size, fd, size); };
121 
125  void reduce_string_size_to(U_I pos);
126 
128  void clear() { *string_size = 0; };
129 
133  void resize(U_I size) { clean_and_destroy(); init(size); };
134 
138  void randomize(U_I size);
139 
141 
144  const char*c_str() const { return mem == nullptr ? throw SRC_BUG : mem; };
145 
149  char & operator[] (U_I index);
150  char operator[](U_I index) const { return (const_cast<secu_string *>(this))->operator[](index); };
151 
153  U_I get_size() const { if(string_size == nullptr) throw SRC_BUG; return *string_size; }; // returns the size of the string
154 
156  U_I get_allocated_size() const { return *allocated_size - 1; };
157 
158  private:
159  U_I *allocated_size;
160  char *mem;
161  U_I *string_size;
162 
163  void init(U_I size); //< to be used at creation time or after clean_and_destroy() only
164  void copy_from(const secu_string & ref); //< to be used at creation time or after clean_and_destroy() only
165  bool compare_with(const char *ptr, U_I size) const; // return true if given sequence is the same as the one stored in "this"
166  void clean_and_destroy();
167  };
168 
170 
171 } // end of namespace
172 
173 #endif
secu_string(const char *ptr, U_I size)
Definition: secu_string.hpp:77
U_I get_allocated_size() const
get the size of the allocated secure space
are defined here basic integer types that tend to be portable
void randomize(U_I size)
void append(const char *ptr, U_I size)
append some data at the end of the string
secu_string(const secu_string &ref)
the copy constructor
Definition: secu_string.hpp:80
static bool is_string_secured()
secu_string & operator=(const secu_string &ref)
the assignment operator
Definition: secu_string.hpp:83
~secu_string()
the destructor (set memory to zero before releasing it)
Definition: secu_string.hpp:91
void reduce_string_size_to(U_I pos)
void clear()
clear the string (set to an empty string)
exception used to signal a bug. A bug is triggered when reaching some code that should never be reach...
Definition: erreurs.hpp:137
class secu_string
Definition: secu_string.hpp:57
this is the base class of object that can be allocated on a memory pool
char & operator[](U_I index)
secu_string(U_I storage_size=0)
Definition: secu_string.hpp:72
void append(int fd, U_I size)
append some data at the end of the string
U_I get_size() const
get the size of the string
void resize(U_I size)
void append_at(U_I offset, const char *ptr, U_I size)
const char * c_str() const
get access to the secure string
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47