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.

128 lines
3.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 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. // Copyright (C) 2004-2008 René Nyffenegger
  24. // -----------------------------------------------------------------------
  25. // Helpers
  26. #ifndef DOXYGEN
  27. namespace DistrhoBase64Helpers {
  28. static const char* const kBase64Chars =
  29. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  30. "abcdefghijklmnopqrstuvwxyz"
  31. "0123456789+/";
  32. static inline
  33. uint8_t findBase64CharIndex(const char c)
  34. {
  35. static const uint8_t kBase64CharsLen(static_cast<uint8_t>(std::strlen(kBase64Chars)));
  36. for (uint8_t i=0; i<kBase64CharsLen; ++i)
  37. {
  38. if (kBase64Chars[i] == c)
  39. return i;
  40. }
  41. d_stderr2("findBase64CharIndex('%c') - failed", c);
  42. return 0;
  43. }
  44. static inline
  45. bool isBase64Char(const char c)
  46. {
  47. return (std::isalnum(c) || (c == '+') || (c == '/'));
  48. }
  49. } // namespace DistrhoBase64Helpers
  50. #endif
  51. // -----------------------------------------------------------------------
  52. static inline
  53. std::vector<uint8_t> d_getChunkFromBase64String(const char* const base64string)
  54. {
  55. DISTRHO_SAFE_ASSERT_RETURN(base64string != nullptr, std::vector<uint8_t>());
  56. uint i=0, j=0;
  57. uint charArray3[3], charArray4[4];
  58. std::vector<uint8_t> ret;
  59. ret.reserve(std::strlen(base64string)*3/4 + 4);
  60. for (std::size_t l=0, len=std::strlen(base64string); l<len; ++l)
  61. {
  62. const char c = base64string[l];
  63. if (c == '\0' || c == '=')
  64. break;
  65. if (c == ' ' || c == '\n')
  66. continue;
  67. DISTRHO_SAFE_ASSERT_CONTINUE(CarlaBase64Helpers::isBase64Char(c));
  68. charArray4[i++] = static_cast<uint>(c);
  69. if (i == 4)
  70. {
  71. for (i=0; i<4; ++i)
  72. charArray4[i] = DistrhoBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[i]));
  73. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  74. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  75. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  76. for (i=0; i<3; ++i)
  77. ret.push_back(static_cast<uint8_t>(charArray3[i]));
  78. i = 0;
  79. }
  80. }
  81. if (i != 0)
  82. {
  83. for (j=0; j<i && j<4; ++j)
  84. charArray4[j] = DistrhoBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[j]));
  85. for (j=i; j<4; ++j)
  86. charArray4[j] = 0;
  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 (j=0; i>0 && j<i-1; j++)
  91. ret.push_back(static_cast<uint8_t>(charArray3[j]));
  92. }
  93. return ret;
  94. }
  95. // -----------------------------------------------------------------------
  96. #endif // DISTRHO_BASE64_HPP_INCLUDED