Disk ARchive  2.4.15
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
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 "special_alloc.hpp"
40 
41 namespace libdar
42 {
43 
46 
48 
56 
58  {
59  public:
61 
66  static bool is_string_secured();
67 
69 
72  secu_string(U_I size = 0) { init(size); };
73 
75 
77  secu_string(const char *ptr, U_I size) { init(size); append(ptr, size); };
78 
80  secu_string(const secu_string & ref) { copy_from(ref); };
81 
82 
84  secu_string & operator = (const secu_string & ref) { clean_and_destroy(); copy_from(ref); return *this; };
85 
86  bool operator != (const std::string & ref) const { return ! (*this == ref); };
87  bool operator != (const secu_string & ref) const { return ! (*this == ref); };
88  bool operator == (const std::string &ref) const { return compare_with(ref.c_str(),(U_I)(ref.size())); };
89  bool operator == (const secu_string &ref) const { return compare_with(ref.mem, *ref.string_size); };
90 
92  ~secu_string() { clean_and_destroy(); };
93 
95 
100  void read(int fd, U_I size);
101 
103 
110  void append(const char *ptr, U_I size);
111 
113  void append(int fd, U_I size);
114 
117  void reduce_string_size_to(U_I pos);
118 
120  void clear() { clean_and_destroy(); init(0); };
121 
123 
125  void clear_and_resize(U_I size) { clean_and_destroy(); init(size); };
126 
127  void clear_and_not_resize() { string_size = 0; };
128 
130 
134  const char*c_str() const { return mem == NULL ? throw SRC_BUG : mem; };
135 
137 
140  U_I size() const { return *string_size; }; // returns the size of the string
141 
142 #ifdef LIBDAR_SPECIAL_ALLOC
143  USE_SPECIAL_ALLOC(secu_string);
144 #endif
145  private:
146  U_I *allocated_size;
147  char *mem;
148  U_I *string_size;
149 
150  void init(U_I size);
151  void copy_from(const secu_string & ref);
152  bool compare_with(const char *ptr, U_I size) const;
153  void clean_and_destroy();
154  };
155 
157 
158 } // end of namespace
159 
160 #endif
secu_string(const char *ptr, U_I size)
constructor
Definition: secu_string.hpp:77
void clear_and_resize(U_I size)
clear and resize the string to the defined allocated size
are defined here basic integer types that tend to be portable
void append(const char *ptr, U_I size)
append some data to the string
secu_string(const secu_string &ref)
the copy constructor
Definition: secu_string.hpp:80
static bool is_string_secured()
to know if secure memory is available
secu_string & operator=(const secu_string &ref)
the assignment operator
Definition: secu_string.hpp:84
~secu_string()
the destructor (set memory to zero before releasing it)
Definition: secu_string.hpp:92
re-definition of new and delete class operatorthis is a set of macro that makes the new and delete op...
void reduce_string_size_to(U_I pos)
void clear()
clear the string (ans set its allocated string to zero)
U_I size() const
get the size of the string
void read(int fd, U_I size)
fill the object with data
class secu_string
Definition: secu_string.hpp:57
secu_string(U_I size=0)
constructor
Definition: secu_string.hpp:72
const char * c_str() const
get access to the secure string