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.

140 lines
4.6KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 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. // -----------------------------------------------------------------------
  24. // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html
  25. /*
  26. Copyright (C) 2004-2008 René Nyffenegger
  27. This source code is provided 'as-is', without any express or implied
  28. warranty. In no event will the author be held liable for any damages
  29. arising from the use of this software.
  30. Permission is granted to anyone to use this software for any purpose,
  31. including commercial applications, and to alter it and redistribute it
  32. freely, subject to the following restrictions:
  33. 1. The origin of this source code must not be misrepresented; you must not
  34. claim that you wrote the original source code. If you use this source code
  35. in a product, an acknowledgment in the product documentation would be
  36. appreciated but is not required.
  37. 2. Altered source versions must be plainly marked as such, and must not be
  38. misrepresented as being the original source code.
  39. 3. This notice may not be removed or altered from any source distribution.
  40. René Nyffenegger rene.nyffenegger@adp-gmbh.ch
  41. */
  42. // -----------------------------------------------------------------------
  43. // Helpers
  44. #ifndef DOXYGEN
  45. namespace DistrhoBase64Helpers {
  46. static
  47. std::array<int8_t, 256> createCharIndexTable()
  48. {
  49. std::array<int8_t, 256> table;
  50. table.fill(-1);
  51. int8_t index = 0;
  52. for (uint8_t c = 'A'; c <= 'Z'; ++c) table[c] = index++;
  53. for (uint8_t c = 'a'; c <= 'z'; ++c) table[c] = index++;
  54. for (uint8_t c = '0'; c <= '9'; ++c) table[c] = index++;
  55. table['+'] = index++;
  56. table['/'] = index++;
  57. return table;
  58. }
  59. static const std::array<int8_t, 256> kCharIndexTable = createCharIndexTable();
  60. } // namespace DistrhoBase64Helpers
  61. #endif
  62. // -----------------------------------------------------------------------
  63. static inline
  64. std::vector<uint8_t> d_getChunkFromBase64String(const char* const base64string, std::size_t base64stringLen = ~std::size_t(0))
  65. {
  66. std::vector<uint8_t> ret;
  67. if (! base64string)
  68. return ret;
  69. uint32_t i=0, j=0;
  70. uint32_t charArray3[3], charArray4[4];
  71. if (base64stringLen == ~std::size_t(0))
  72. base64stringLen = std::strlen(base64string);
  73. ret.reserve(base64stringLen*3/4 + 4);
  74. for (std::size_t l=0; l<base64stringLen; ++l)
  75. {
  76. const unsigned char c = base64string[l];
  77. if (c == '\0' || c == '=')
  78. break;
  79. if (DistrhoBase64Helpers::kCharIndexTable[c] == -1)
  80. continue;
  81. charArray4[i++] = static_cast<uint32_t>(c);
  82. if (i == 4)
  83. {
  84. for (i=0; i<4; ++i)
  85. charArray4[i] = static_cast<uint8_t>(DistrhoBase64Helpers::kCharIndexTable[charArray4[i]]);
  86. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  87. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  88. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  89. for (i=0; i<3; ++i)
  90. ret.push_back(static_cast<uint8_t>(charArray3[i]));
  91. i = 0;
  92. }
  93. }
  94. if (i != 0)
  95. {
  96. for (j=0; j<i && j<4; ++j)
  97. charArray4[j] = static_cast<uint8_t>(DistrhoBase64Helpers::kCharIndexTable[charArray4[j]]);
  98. for (j=i; j<4; ++j)
  99. charArray4[j] = 0;
  100. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  101. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  102. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  103. for (j=0; i>0 && j<i-1; j++)
  104. ret.push_back(static_cast<uint8_t>(charArray3[j]));
  105. }
  106. return ret;
  107. }