Disk ARchive
2.4.5
|
00001 /*********************************************************************/ 00002 // dar - disk archive - a backup/restoration program 00003 // Copyright (C) 2002-2052 Denis Corbin 00004 // 00005 // This program is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU General Public License 00007 // as published by the Free Software Foundation; either version 2 00008 // of the License, or (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 // 00019 // to contact the author : http://dar.linux.free.fr/email.html 00020 /*********************************************************************/ 00021 // $Id$ 00022 // 00023 /*********************************************************************/ 00024 // 00025 00035 00036 #ifndef SECU_STRING_HPP 00037 #define SECU_STRING_HPP 00038 00039 #include "../my_config.h" 00040 00041 #include <string> 00042 #include "integers.hpp" 00043 #include "special_alloc.hpp" 00044 00045 namespace libdar 00046 { 00047 00050 00052 00060 00061 class secu_string 00062 { 00063 public: 00065 00070 static bool is_string_secured(); 00071 00073 00076 secu_string(U_I size = 0) { init(size); }; 00077 00079 00081 secu_string(const char *ptr, U_I size) { init(size); append(ptr, size); }; 00082 00084 secu_string(const secu_string & ref) { copy_from(ref); }; 00085 00086 00088 secu_string & operator = (const secu_string & ref) { clean_and_destroy(); copy_from(ref); return *this; }; 00089 00090 bool operator != (const std::string & ref) const { return ! (*this == ref); }; 00091 bool operator != (const secu_string & ref) const { return ! (*this == ref); }; 00092 bool operator == (const std::string &ref) const { return compare_with(ref.c_str(),(U_I)(ref.size())); }; 00093 bool operator == (const secu_string &ref) const { return compare_with(ref.mem, *ref.string_size); }; 00094 00096 ~secu_string() { clean_and_destroy(); }; 00097 00099 00104 void read(int fd, U_I size); 00105 00107 00114 void append(const char *ptr, U_I size); 00115 00117 void append(int fd, U_I size); 00118 00121 void reduce_string_size_to(U_I pos); 00122 00124 void clear() { clean_and_destroy(); init(0); }; 00125 00127 00129 void clear_and_resize(U_I size) { clean_and_destroy(); init(size); }; 00130 00131 void clear_and_not_resize() { string_size = 0; }; 00132 00134 00138 const char*c_str() const { return mem == NULL ? throw SRC_BUG : mem; }; 00139 00141 00144 U_I size() const { return *string_size; }; // returns the size of the string 00145 00146 #ifdef LIBDAR_SPECIAL_ALLOC 00147 USE_SPECIAL_ALLOC(secu_string); 00148 #endif 00149 private: 00150 U_I *allocated_size; 00151 char *mem; 00152 U_I *string_size; 00153 00154 void init(U_I size); 00155 void copy_from(const secu_string & ref); 00156 bool compare_with(const char *ptr, U_I size) const; 00157 void clean_and_destroy(); 00158 }; 00159 00161 00162 } // end of namespace 00163 00164 #endif