Base64.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __BASE64_H__
  21. #define __BASE64_H__
  22. /*
  23. ===============================================================================
  24. base64
  25. ===============================================================================
  26. */
  27. class idBase64 {
  28. public:
  29. idBase64();
  30. idBase64( const idStr &s );
  31. ~idBase64();
  32. void Encode( const byte *from, int size );
  33. void Encode( const idStr &src );
  34. int DecodeLength() const; // minimum size in bytes of destination buffer for decoding
  35. int Decode( byte *to ) const; // does not append a \0 - needs a DecodeLength() bytes buffer
  36. 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)
  37. void Decode( idFile *dest ) const;
  38. const char *c_str() const;
  39. void operator=( const idStr &s );
  40. private:
  41. byte * data;
  42. int len;
  43. int alloced;
  44. void Init();
  45. void Release();
  46. void EnsureAlloced( int size );
  47. };
  48. ID_INLINE idBase64::idBase64() {
  49. Init();
  50. }
  51. ID_INLINE idBase64::idBase64( const idStr &s ) {
  52. Init();
  53. *this = s;
  54. }
  55. ID_INLINE idBase64::~idBase64() {
  56. Release();
  57. }
  58. ID_INLINE const char *idBase64::c_str() const {
  59. return (const char *)data;
  60. }
  61. ID_INLINE void idBase64::Init() {
  62. len = 0;
  63. alloced = 0;
  64. data = NULL;
  65. }
  66. ID_INLINE void idBase64::Release() {
  67. if ( data ) {
  68. delete[] data;
  69. }
  70. Init();
  71. }
  72. ID_INLINE void idBase64::EnsureAlloced( int size ) {
  73. if ( size > alloced ) {
  74. Release();
  75. }
  76. data = new (TAG_IDLIB) byte[size];
  77. alloced = size;
  78. }
  79. ID_INLINE void idBase64::operator=( const idStr &s ) {
  80. EnsureAlloced( s.Length()+1 ); // trailing \0 - beware, this does a Release
  81. strcpy( (char *)data, s.c_str() );
  82. len = s.Length();
  83. }
  84. #endif /* !__BASE64_H__ */