Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Base64.hpp 4.8KB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_BASE64_HPP_INCLUDED
  17. #define DISTRHO_BASE64_HPP_INCLUDED
  18. #include "../DistrhoUtils.hpp"
  19. #include <cctype>
  20. #include <vector>
  21. // -----------------------------------------------------------------------
  22. // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html
  23. /*
  24. Copyright (C) 2004-2008 René Nyffenegger
  25. This source code is provided 'as-is', without any express or implied
  26. warranty. In no event will the author be held liable for any damages
  27. arising from the use of this software.
  28. Permission is granted to anyone to use this software for any purpose,
  29. including commercial applications, and to alter it and redistribute it
  30. freely, subject to the following restrictions:
  31. 1. The origin of this source code must not be misrepresented; you must not
  32. claim that you wrote the original source code. If you use this source code
  33. in a product, an acknowledgment in the product documentation would be
  34. appreciated but is not required.
  35. 2. Altered source versions must be plainly marked as such, and must not be
  36. misrepresented as being the original source code.
  37. 3. This notice may not be removed or altered from any source distribution.
  38. René Nyffenegger rene.nyffenegger@adp-gmbh.ch
  39. */
  40. // -----------------------------------------------------------------------
  41. // Helpers
  42. #ifndef DOXYGEN
  43. namespace DistrhoBase64Helpers {
  44. static const char* const kBase64Chars =
  45. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  46. "abcdefghijklmnopqrstuvwxyz"
  47. "0123456789+/";
  48. static inline
  49. uint8_t findBase64CharIndex(const char c)
  50. {
  51. static const uint8_t kBase64CharsLen(static_cast<uint8_t>(std::strlen(kBase64Chars)));
  52. for (uint8_t i=0; i<kBase64CharsLen; ++i)
  53. {
  54. if (kBase64Chars[i] == c)
  55. return i;
  56. }
  57. d_stderr2("findBase64CharIndex('%c') - failed", c);
  58. return 0;
  59. }
  60. static inline
  61. bool isBase64Char(const char c)
  62. {
  63. return (std::isalnum(c) || (c == '+') || (c == '/'));
  64. }
  65. } // namespace DistrhoBase64Helpers
  66. #endif
  67. // -----------------------------------------------------------------------
  68. static inline
  69. std::vector<uint8_t> d_getChunkFromBase64String(const char* const base64string)
  70. {
  71. DISTRHO_SAFE_ASSERT_RETURN(base64string != nullptr, std::vector<uint8_t>());
  72. uint i=0, j=0;
  73. uint charArray3[3], charArray4[4];
  74. std::vector<uint8_t> ret;
  75. ret.reserve(std::strlen(base64string)*3/4 + 4);
  76. for (std::size_t l=0, len=std::strlen(base64string); l<len; ++l)
  77. {
  78. const char c = base64string[l];
  79. if (c == '\0' || c == '=')
  80. break;
  81. if (c == ' ' || c == '\n')
  82. continue;
  83. DISTRHO_SAFE_ASSERT_CONTINUE(DistrhoBase64Helpers::isBase64Char(c));
  84. charArray4[i++] = static_cast<uint>(c);
  85. if (i == 4)
  86. {
  87. for (i=0; i<4; ++i)
  88. charArray4[i] = DistrhoBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[i]));
  89. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  90. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  91. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  92. for (i=0; i<3; ++i)
  93. ret.push_back(static_cast<uint8_t>(charArray3[i]));
  94. i = 0;
  95. }
  96. }
  97. if (i != 0)
  98. {
  99. for (j=0; j<i && j<4; ++j)
  100. charArray4[j] = DistrhoBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[j]));
  101. for (j=i; j<4; ++j)
  102. charArray4[j] = 0;
  103. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  104. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  105. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  106. for (j=0; i>0 && j<i-1; j++)
  107. ret.push_back(static_cast<uint8_t>(charArray3[j]));
  108. }
  109. return ret;
  110. }
  111. // -----------------------------------------------------------------------
  112. #endif // DISTRHO_BASE64_HPP_INCLUDED