1: #ifndef _BYTESTRING_H_ 
   2: #define _BYTESTRING_H_ 
   3: 
   4: #include <vector> 
   5: #include <fstream> 
   6: #include <string> 
   7: 
   8: 
   9: #if defined ( DEBUG ) 
  10: #define DEBUG_CODE( code_to_put_into_source ) { code_to_put_into_source }  
  11: #else  
  12: #define DEBUG_CODE( code_to_put_into_source )  
  13: #endif  
  14: 
  15: typedef 	unsigned char   BYTE;
  16: typedef 	unsigned short  WORD;
  17: typedef 	unsigned int    DWORD;
  18: 
  19: using namespace std;
  20: 
  21: typedef std::vector<BYTE>::const_iterator const_byte_string_iterator;
  22: typedef std::vector<BYTE>::iterator byte_string_iterator;
  23: 
  24: class ByteString : public std::vector<BYTE>
  25: {
  26: public: 
  27: // -----------------------------------------------------------
  28: // 
  29: // -----------------------------------------------------------
  30: ByteString( const size_t mem2alloc = 1024 )
  31:     {
  32:     reserve(mem2alloc);
  33:     }
  34:         
  35: // -----------------------------------------------------------
  36: // 
  37: // -----------------------------------------------------------
  38: inline void dump_buffer( std::ofstream & file )
  39:     {
  40:     file << std::string( reinterpret_cast<const char *> (begin() ), size() ) << flush;
  41:     }
  42: 
  43:         
  44: // -----------------------------------------------------------
  45: // 
  46: // -----------------------------------------------------------
  47: inline bool operator== ( const ByteString& rhs ) const
  48:     {
  49: 	DEBUG_CODE(  cerr << "bool operator== ( const ByteString& rhs )\n"; )
  50:         if ( size() != rhs.size() )
  51:                 {
  52:                 return false;
  53:                 }
  54: 
  55:         for ( register size_t counter = 0; counter != size(); ++counter )
  56:                 {
  57:                 if ( (*this)[counter] != rhs[counter] )
  58:                         {
  59:                         return false;
  60:                         }
  61:                 }
  62:         return true;
  63:     }
  64: 
  65: 	
  66: // -----------------------------------------------------------
  67: // 
  68: // -----------------------------------------------------------
  69: inline bool operator!= ( const ByteString& rhs ) const
  70:     {
  71: 	DEBUG_CODE(  cerr << "bool operator!= ( const ByteString& rhs )\n"; )
  72:         if ( size() != rhs.size() )
  73:                 {
  74:                 return true;
  75:                 }
  76: 
  77:         for ( register size_t counter = 0; counter != size(); ++counter )
  78:                 {
  79:                 if ( (*this)[counter] != rhs[counter] )
  80:                         {
  81:                         return true;
  82:                         }
  83:                 }
  84:         return false;
  85:     }
  86:         
  87: // -----------------------------------------------------------
  88: // 
  89: // -----------------------------------------------------------
  90: inline ByteString& operator= ( const ByteString& rhs )
  91:     {
  92:     if ( this != &rhs )
  93:         {
  94:         clear();
  95:         insert( end(), rhs.begin(), rhs.end() );
  96:         }
  97:     return *this;
  98:     }
  99: 
 100: 	
 101: // -----------------------------------------------------------
 102: // 
 103: // -----------------------------------------------------------
 104: inline ByteString operator+ ( const ByteString& rhs )
 105:     {
 106: 	ByteString str(*this);
 107: 	str += rhs;
 108: 	return str;
 109: 	}
 110: 
 111: // -----------------------------------------------------------
 112: // 
 113: // -----------------------------------------------------------
 114: inline ByteString operator+ ( const char * const rhs )
 115:     {
 116: 	ByteString str(*this);
 117: 	str += rhs;
 118: 	return str;
 119: 	}
 120: 
 121: // -----------------------------------------------------------
 122: // 
 123: // -----------------------------------------------------------
 124: template <typename basic_type>
 125: inline ByteString operator+ ( const basic_type& rhs )
 126:     {
 127: 	ByteString str(*this);
 128: 	str += rhs;
 129: 	return str;
 130: 	}
 131: 
 132: // -----------------------------------------------------------
 133: // 
 134: // -----------------------------------------------------------
 135: inline ByteString& operator+= ( const ByteString& rhs )
 136:     {
 137:     insert( end(), rhs.begin(), rhs.end() );
 138:     return *this;
 139:     }
 140: 
 141: // -----------------------------------------------------------
 142: // 
 143: // -----------------------------------------------------------
 144: inline bool operator== ( const char * const rhs ) const
 145:     {
 146: 	DEBUG_CODE(  cerr << "bool operator== ( const char * const rhs )\n"; )
 147:         if ( size() != strlen(rhs) )
 148:                 {
 149:                 return false;
 150:                 }
 151: 
 152:     const BYTE* pointer = reinterpret_cast<const BYTE*> (rhs);
 153:         const_byte_string_iterator iter = begin();
 154:     while ( *pointer != 0x0 )
 155:         {
 156:                 if ( *pointer != *iter )
 157:                         {
 158:                         return false;
 159:                         }
 160:         ++pointer;
 161:                 ++iter;
 162:         }
 163:     return true;
 164:     }
 165: 
 166:         
 167: // -----------------------------------------------------------
 168: // 
 169: // -----------------------------------------------------------
 170: inline bool operator!= ( const char * const rhs ) const
 171:     {
 172: 	DEBUG_CODE(  cerr << "bool operator!= ( const char * const rhs )\n"; )
 173:         if ( size() != strlen(rhs) )
 174:                 {
 175:                 return true;
 176:                 }
 177: 
 178:     const BYTE* pointer = reinterpret_cast<const BYTE*> (rhs);
 179:         const_byte_string_iterator iter = begin();
 180:     while ( *pointer != 0x0 )
 181:         {
 182:                 if ( *pointer != *iter )
 183:                         {
 184:                         return true;
 185:                         }
 186:         ++pointer;
 187:                 ++iter;
 188:         }
 189:     return false;
 190:     }
 191: 
 192: // -----------------------------------------------------------
 193: // 
 194: // -----------------------------------------------------------
 195: inline ByteString& operator= ( const char * const rhs )
 196:     {
 197:     clear();
 198:     const BYTE* pointer = reinterpret_cast<const BYTE*> (rhs);
 199:     while ( *pointer != 0x0 )
 200:         {
 201:         push_back(*pointer);
 202:         ++pointer;
 203:         }
 204:     return *this;
 205:     }
 206: // -----------------------------------------------------------
 207: // 
 208: // -----------------------------------------------------------
 209: inline ByteString& operator+= ( const char * const rhs )
 210:     {
 211:     const BYTE* pointer = reinterpret_cast<const BYTE*> (rhs);
 212:     while ( *pointer != 0x0 )
 213:         {
 214:         push_back(*pointer);
 215:         ++pointer;
 216:         }
 217:     return *this;
 218:     }
 219: 
 220: // -----------------------------------------------------------
 221: // 
 222: // -----------------------------------------------------------
 223: inline void append( const void* buffer, size_t n_bytes )
 224:     {
 225:     const BYTE* pointer = reinterpret_cast<const BYTE*> (buffer);
 226:     for ( register size_t counter = 0; counter < n_bytes; ++counter )
 227:         {
 228:         push_back(*pointer);
 229:         ++pointer;
 230:         }
 231:     }
 232: 
 233: // -----------------------------------------------------------
 234: // 
 235: // -----------------------------------------------------------
 236: inline void copy( const void* buffer, size_t n_bytes )
 237:     {
 238: 	clear();
 239:     const BYTE* pointer = reinterpret_cast<const BYTE*> (buffer);
 240:     for ( register size_t counter = 0; counter < n_bytes; ++counter )
 241:         {
 242:         push_back( *pointer );
 243:         ++pointer;
 244:         }
 245:     }
 246: 
 247: // -----------------------------------------------------------
 248: // 
 249: // -----------------------------------------------------------
 250: inline ByteString& operator= ( const BYTE rhs )
 251:     {
 252:     clear();
 253:     push_back(rhs);
 254:     return *this;
 255:     }
 256: 
 257: // -----------------------------------------------------------
 258: // 
 259: // -----------------------------------------------------------
 260: inline ByteString& operator+= ( const BYTE rhs )
 261:     {
 262:     push_back(rhs);
 263:     return *this;
 264:     }
 265: 
 266: // -----------------------------------------------------------
 267: // 
 268: // -----------------------------------------------------------
 269: template <typename basic_type>
 270: inline bool operator== ( const basic_type& rhs ) const
 271:     {
 272: 	DEBUG_CODE(  cerr << "bool operator== ( const basic_type rhs )\n"; )
 273:         if ( size() != sizeof(rhs) )
 274:                 {
 275:                 return false;
 276:                 }
 277: 
 278:     const BYTE* pointer = reinterpret_cast<const BYTE*> (&rhs);
 279:     for( register size_t i = 0; i < sizeof(rhs); ++i )
 280:         {
 281:                 if ( (*this)[i] != *pointer )
 282:                         {
 283:                         return false;
 284:                         }
 285:         ++pointer;
 286:         }
 287: 
 288:     return true;
 289:     }
 290: 
 291: // -----------------------------------------------------------
 292: // 
 293: // -----------------------------------------------------------
 294: template <typename basic_type>
 295: inline bool operator!= ( const basic_type& rhs ) const
 296:     {
 297: 	DEBUG_CODE(  cerr << "bool operator!= ( const basic_type rhs )\n"; )
 298:         if ( size() != sizeof(rhs) )
 299:                 {
 300:                 return true;
 301:                 }
 302: 
 303:     const BYTE* pointer = reinterpret_cast<const BYTE*> (&rhs);
 304:     for( register size_t i = 0; i < sizeof(rhs); ++i )
 305:         {
 306:                 if ( (*this)[i] != *pointer )
 307:                         {
 308:                         return true;
 309:                         }
 310:         ++pointer;
 311:         }
 312: 
 313:     return false;
 314:     }
 315: 
 316: // -----------------------------------------------------------
 317: // 
 318: // -----------------------------------------------------------
 319: template <typename basic_type>
 320: inline ByteString& operator= ( const basic_type& rhs )
 321:     {
 322:     clear();
 323:     const BYTE* pointer = reinterpret_cast<const BYTE*> (&rhs);
 324:     for( register size_t i = 0; i < sizeof(rhs); ++i )
 325:         {
 326:         push_back(*pointer);
 327:         ++pointer;
 328:         }
 329:     return *this;
 330:     }
 331: 
 332: // -----------------------------------------------------------
 333: // 
 334: // -----------------------------------------------------------
 335: template <typename basic_type>
 336: inline ByteString& operator+= ( const basic_type& rhs )
 337:     {
 338:     const BYTE* pointer = reinterpret_cast<const BYTE*> (&rhs);
 339:     for( register size_t i = 0; i < sizeof(rhs); ++i )
 340:         {
 341:         push_back(*pointer);
 342:         ++pointer;
 343:         }
 344:     return *this;
 345:     }
 346: 
 347: };
 348: 
 349: 
 350: // -----------------------------------------------------------
 351: // 
 352: // -----------------------------------------------------------
 353: inline std::ostream& operator << ( std::ostream& strm, const ByteString& bs )
 354: 	{
 355:     // Attention: data is binary !!
 356:     return strm << std::string( reinterpret_cast<const char*> ( bs.begin() ), bs.size() );
 357: 	}
 358: 
 359: 
 360: #endif // _BYTESTRING_H_ 
 361: