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.

195 lines
6.1KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. * Copyright (C) 2022 Jean Pierre Cimalando <jp-dev@inbox.ru>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  7. * or without fee is hereby granted, provided that the above copyright notice and this
  8. * permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  11. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  12. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #pragma once
  18. #include <cstdint>
  19. #include <cstring>
  20. #include <cassert>
  21. #include <array>
  22. #include <vector>
  23. #include <string>
  24. // -----------------------------------------------------------------------
  25. // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html
  26. /*
  27. Copyright (C) 2004-2008 René Nyffenegger
  28. This source code is provided 'as-is', without any express or implied
  29. warranty. In no event will the author be held liable for any damages
  30. arising from the use of this software.
  31. Permission is granted to anyone to use this software for any purpose,
  32. including commercial applications, and to alter it and redistribute it
  33. freely, subject to the following restrictions:
  34. 1. The origin of this source code must not be misrepresented; you must not
  35. claim that you wrote the original source code. If you use this source code
  36. in a product, an acknowledgment in the product documentation would be
  37. appreciated but is not required.
  38. 2. Altered source versions must be plainly marked as such, and must not be
  39. misrepresented as being the original source code.
  40. 3. This notice may not be removed or altered from any source distribution.
  41. René Nyffenegger rene.nyffenegger@adp-gmbh.ch
  42. */
  43. // -----------------------------------------------------------------------
  44. // Helpers
  45. #ifndef DOXYGEN
  46. namespace DistrhoBase64Helpers {
  47. static
  48. std::array<int8_t, 256> createCharIndexTable()
  49. {
  50. std::array<int8_t, 256> table;
  51. table.fill(-1);
  52. int8_t index = 0;
  53. for (uint8_t c = 'A'; c <= 'Z'; ++c) table[c] = index++;
  54. for (uint8_t c = 'a'; c <= 'z'; ++c) table[c] = index++;
  55. for (uint8_t c = '0'; c <= '9'; ++c) table[c] = index++;
  56. table['+'] = index++;
  57. table['/'] = index++;
  58. return table;
  59. }
  60. static const std::array<int8_t, 256> kCharIndexTable = createCharIndexTable();
  61. } // namespace DistrhoBase64Helpers
  62. #endif
  63. // -----------------------------------------------------------------------
  64. static inline
  65. std::vector<uint8_t> d_getChunkFromBase64String(const char* const base64string, std::size_t base64stringLen = ~std::size_t(0))
  66. {
  67. std::vector<uint8_t> ret;
  68. if (! base64string)
  69. return ret;
  70. uint32_t i=0, j=0;
  71. uint32_t charArray3[3], charArray4[4];
  72. if (base64stringLen == ~std::size_t(0))
  73. base64stringLen = std::strlen(base64string);
  74. ret.reserve(base64stringLen*3/4 + 4);
  75. for (std::size_t l=0; l<base64stringLen; ++l)
  76. {
  77. const unsigned char c = base64string[l];
  78. if (c == '\0' || c == '=')
  79. break;
  80. if (DistrhoBase64Helpers::kCharIndexTable[c] == -1)
  81. continue;
  82. charArray4[i++] = static_cast<uint32_t>(c);
  83. if (i == 4)
  84. {
  85. for (i=0; i<4; ++i)
  86. charArray4[i] = static_cast<uint8_t>(DistrhoBase64Helpers::kCharIndexTable[charArray4[i]]);
  87. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  88. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  89. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  90. for (i=0; i<3; ++i)
  91. ret.push_back(static_cast<uint8_t>(charArray3[i]));
  92. i = 0;
  93. }
  94. }
  95. if (i != 0)
  96. {
  97. for (j=0; j<i && j<4; ++j)
  98. charArray4[j] = static_cast<uint8_t>(DistrhoBase64Helpers::kCharIndexTable[charArray4[j]]);
  99. for (j=i; j<4; ++j)
  100. charArray4[j] = 0;
  101. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  102. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  103. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  104. for (j=0; i>0 && j<i-1; j++)
  105. ret.push_back(static_cast<uint8_t>(charArray3[j]));
  106. }
  107. return ret;
  108. }
  109. static inline
  110. std::string d_getBase64StringFromChunk(const void* const data, std::size_t dataSize)
  111. {
  112. static const char* const kBase64Chars =
  113. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  114. "abcdefghijklmnopqrstuvwxyz"
  115. "0123456789+/";
  116. const uint8_t* bytesToEncode((const uint8_t*)data);
  117. uint32_t i=0, j=0;
  118. uint32_t charArray3[3], charArray4[4];
  119. std::string ret;
  120. ret.reserve(((dataSize + 2) / 3) * 4);
  121. for (std::size_t s=0; s<dataSize; ++s)
  122. {
  123. charArray3[i++] = *(bytesToEncode++);
  124. if (i == 3)
  125. {
  126. charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  127. charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  128. charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  129. charArray4[3] = charArray3[2] & 0x3f;
  130. for (i=0; i<4; ++i)
  131. ret.push_back(kBase64Chars[charArray4[i]]);
  132. i = 0;
  133. }
  134. }
  135. if (i != 0)
  136. {
  137. for (j=i; j<3; ++j)
  138. charArray3[j] = '\0';
  139. charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  140. charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  141. charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  142. charArray4[3] = charArray3[2] & 0x3f;
  143. for (j=0; j<4 && i<3 && j<i+1; ++j)
  144. ret.push_back(kBase64Chars[charArray4[j]]);
  145. for (; i++ < 3;)
  146. ret.push_back('=');
  147. }
  148. return ret;
  149. }