Disk ARchive  2.5.2
Full featured and portable backup and archiving tool
secu_string.hpp
Go to the documentation of this file.
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 
00031 
00032 #ifndef SECU_STRING_HPP
00033 #define SECU_STRING_HPP
00034 
00035 #include "../my_config.h"
00036 
00037 #include <string>
00038 #include "integers.hpp"
00039 #include "on_pool.hpp"
00040 
00041 namespace libdar
00042 {
00043 
00046 
00048 
00056 
00057     class secu_string : public on_pool
00058     {
00059     public:
00066     static bool is_string_secured();
00067 
00072     secu_string(U_I storage_size = 0) { init(storage_size); };
00073 
00077     secu_string(const char *ptr, U_I size) { init(size); append_at(0, ptr, size); };
00078 
00080     secu_string(const secu_string & ref) { copy_from(ref); };
00081 
00083     secu_string & operator = (const secu_string & ref) { clean_and_destroy(); copy_from(ref); return *this; };
00084 
00085     bool operator != (const std::string & ref) const { return ! (*this == ref); };
00086     bool operator != (const secu_string & ref) const { return ! (*this == ref); };
00087     bool operator == (const std::string &ref) const { return compare_with(ref.c_str(),(U_I)(ref.size())); };
00088     bool operator == (const secu_string &ref) const { return compare_with(ref.mem, *(ref.string_size)); };
00089 
00091     ~secu_string() throw(Ebug) { clean_and_destroy(); };
00092 
00100     void set(int fd, U_I size);
00101 
00111     void append_at(U_I offset, const char *ptr, U_I size);
00112 
00114     void append_at(U_I offset, int fd, U_I size);
00115 
00117     void append(const char *ptr, U_I size) { append_at(*string_size, ptr, size); };
00118 
00120     void append(int fd, U_I size) { append_at(*string_size, fd, size); };
00121 
00125     void reduce_string_size_to(U_I pos);
00126 
00128     void clear() { *string_size = 0; };
00129 
00133     void resize(U_I size) { clean_and_destroy(); init(size); };
00134 
00138     void randomize(U_I size);
00139 
00141 
00144     const char*c_str() const { return mem == nullptr ? throw SRC_BUG : mem; };
00145 
00149     char & operator[] (U_I index);
00150     char operator[](U_I index) const { return (const_cast<secu_string *>(this))->operator[](index); };
00151 
00153     U_I get_size() const { if(string_size == nullptr) throw SRC_BUG; return *string_size; }; // returns the size of the string
00154 
00156     U_I get_allocated_size() const { return *allocated_size - 1; };
00157 
00158     private:
00159     U_I *allocated_size;
00160     char *mem;
00161     U_I *string_size;
00162 
00163     void init(U_I size);   //< to be used at creation time or after clean_and_destroy() only
00164     void copy_from(const secu_string & ref); //< to be used at creation time or after clean_and_destroy() only
00165     bool compare_with(const char *ptr, U_I size) const; // return true if given sequence is the same as the one stored in "this"
00166     void clean_and_destroy();
00167     };
00168 
00170 
00171 } // end of namespace
00172 
00173 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines