home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / Base64.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  2KB  |  85 lines

  1.  
  2. #ifndef __BASE64_H__
  3. #define __BASE64_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     base64
  9.  
  10. ===============================================================================
  11. */
  12.  
  13. class idBase64 {
  14. public:
  15.                 idBase64( void );
  16.                 idBase64( const idStr &s );
  17.                 ~idBase64( void );
  18.  
  19.     void        Encode( const byte *from, int size );
  20.     void        Encode( const idStr &src );
  21.     int            DecodeLength( void ) const; // minimum size in bytes of destination buffer for decoding
  22.     int            Decode( byte *to ) const; // does not append a \0 - needs a DecodeLength() bytes buffer
  23.     void        Decode( idStr &dest ) const; // decodes the binary content to an idStr (a bit dodgy, \0 and other non-ascii are possible in the decoded content)
  24.     void        Decode( idFile *dest ) const;
  25.  
  26.     const char    *c_str() const;
  27.  
  28.     void         operator=( const idStr &s );
  29.  
  30. private:
  31.     byte *        data;
  32.     int            len;
  33.     int            alloced;
  34.  
  35.     void        Init( void );
  36.     void        Release( void );
  37.     void        EnsureAlloced( int size );
  38. };
  39.  
  40. ID_INLINE idBase64::idBase64( void ) {
  41.     Init();
  42. }
  43.  
  44. ID_INLINE idBase64::idBase64( const idStr &s ) {
  45.     Init();
  46.     *this = s;
  47. }
  48.  
  49. ID_INLINE idBase64::~idBase64( void ) {
  50.     Release();
  51. }
  52.  
  53. ID_INLINE const char *idBase64::c_str( void ) const {
  54.     return (const char *)data;
  55. }
  56.  
  57. ID_INLINE void idBase64::Init( void ) {
  58.     len = 0;
  59.     alloced = 0;
  60.     data = NULL;
  61. }
  62.  
  63. ID_INLINE void idBase64::Release( void ) {
  64.     if ( data ) {
  65.         delete[] data;
  66.     }
  67.     Init();
  68. }
  69.  
  70. ID_INLINE void idBase64::EnsureAlloced( int size ) {
  71.     if ( size > alloced ) {
  72.         Release();
  73.     }
  74.     data = new byte[size];
  75.     alloced = size;
  76. }
  77.  
  78. ID_INLINE void idBase64::operator=( const idStr &s ) {
  79.     EnsureAlloced( s.Length()+1 ); // trailing \0 - beware, this does a Release
  80.     strcpy( (char *)data, s.c_str() );
  81.     len = s.Length();
  82. }
  83.  
  84. #endif /* !__BASE64_H__ */
  85.