base64.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * base64.h - namespace base64 with methods for encoding/decoding binary data
  3. * to/from base64
  4. *
  5. * Copyright (c) 2006-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef _BASE64_H
  26. #define _BASE64_H
  27. #include <QtCore/QByteArray>
  28. #include <QtCore/QString>
  29. #include <QtCore/QVariant>
  30. namespace base64
  31. {
  32. inline void encode( const char * _data, const int _size,
  33. QString & _dst )
  34. {
  35. _dst = QByteArray( _data, _size ).toBase64();
  36. }
  37. template<class T>
  38. inline void decode( const QString & _b64, T * * _data, int * _size )
  39. {
  40. QByteArray data = QByteArray::fromBase64( _b64.toUtf8() );
  41. *_size = data.size();
  42. *_data = new T[*_size / sizeof(T)];
  43. memcpy( *_data, data.constData(), *_size );
  44. }
  45. // for compatibility-code only
  46. QVariant decode( const QString & _b64,
  47. QVariant::Type _force_type = QVariant::Invalid );
  48. }
  49. #endif