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.

114 lines
3.4KB

  1. /*
  2. * Carla base64 utils, based on http://www.adp-gmbh.ch/cpp/common/base64.html
  3. * Copyright (C) 2004-2008 René Nyffenegger
  4. * Copyright (C) 2014 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. */
  18. #ifndef CARLA_BASE64_UTILS_HPP_INCLUDED
  19. #define CARLA_BASE64_UTILS_HPP_INCLUDED
  20. #include "CarlaUtils.hpp"
  21. #include <cctype>
  22. #include <vector>
  23. // -----------------------------------------------------------------------
  24. // Helpers
  25. namespace CarlaBase64Helpers {
  26. static const char* const kBase64Chars =
  27. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  28. "abcdefghijklmnopqrstuvwxyz"
  29. "0123456789+/";
  30. static inline
  31. uint8_t findBase64CharIndex(const char c)
  32. {
  33. static const uint8_t kBase64CharsLen(static_cast<uint8_t>(std::strlen(kBase64Chars)));
  34. for (uint8_t i=0; i<kBase64CharsLen; ++i)
  35. {
  36. if (kBase64Chars[i] == c)
  37. return i;
  38. }
  39. return 0;
  40. }
  41. static inline
  42. bool isBase64Char(const char c)
  43. {
  44. return (std::isalnum(c) || (c == '+') || (c == '/'));
  45. }
  46. } // namespace CarlaBase64Helpers
  47. // -----------------------------------------------------------------------
  48. static inline
  49. std::vector<uint8_t> carla_getChunkFromBase64String(const char* const base64string)
  50. {
  51. CARLA_SAFE_ASSERT_RETURN(base64string != nullptr, std::vector<uint8_t>());
  52. uint i=0, j=0;
  53. uint charArray3[3], charArray4[4];
  54. std::vector<uint8_t> ret;
  55. ret.reserve(std::strlen(base64string)*3/4 + 4);
  56. for (std::size_t l=0, len=std::strlen(base64string); l<len && base64string[l] != '=' && CarlaBase64Helpers::isBase64Char(base64string[l]); ++l)
  57. {
  58. charArray4[i++] = static_cast<uint>(base64string[l]);
  59. if (i == 4)
  60. {
  61. for (i=0; i<4; ++i)
  62. charArray4[i] = CarlaBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[i]));
  63. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  64. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  65. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  66. for (i=0; i<3; ++i)
  67. ret.push_back(static_cast<uint8_t>(charArray3[i]));
  68. i = 0;
  69. }
  70. }
  71. if (i != 0)
  72. {
  73. for (j=i; j<4; ++j)
  74. charArray4[j] = 0;
  75. for (j=0; j<4; ++j)
  76. charArray4[j] = CarlaBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[j]));
  77. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  78. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  79. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  80. for (j=0; i>0 && j<i-1; j++)
  81. ret.push_back(static_cast<uint8_t>(charArray3[j]));
  82. }
  83. return ret;
  84. }
  85. // -----------------------------------------------------------------------
  86. #endif // CARLA_BASE64_UTILS_HPP_INCLUDED