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.

113 lines
3.3KB

  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. // -----------------------------------------------------------------------
  23. // Helpers
  24. namespace CarlaBase64Helpers {
  25. static const char* const kBase64Chars =
  26. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  27. "abcdefghijklmnopqrstuvwxyz"
  28. "0123456789+/";
  29. static inline
  30. uint8_t findBase64CharIndex(const char c)
  31. {
  32. static const uint8_t kBase64CharsLen(std::strlen(kBase64Chars));
  33. for (uint8_t i=0; i<kBase64CharsLen; ++i)
  34. {
  35. if (kBase64Chars[i] == c)
  36. return i;
  37. }
  38. return 0;
  39. }
  40. static inline
  41. bool isBase64Char(const char c)
  42. {
  43. return (std::isalnum(c) || (c == '+') || (c == '/'));
  44. }
  45. }
  46. // -----------------------------------------------------------------------
  47. static inline
  48. std::vector<uint8_t> carla_getChunkFromBase64String(const char* const base64string)
  49. {
  50. CARLA_SAFE_ASSERT_RETURN(base64string != nullptr, std::vector<uint8_t>());
  51. uint i=0, j=0;
  52. uint charArray3[3], charArray4[4];
  53. std::vector<uint8_t> ret;
  54. ret.reserve(std::strlen(base64string)*3/4 + 4);
  55. for (std::size_t l=0, len=std::strlen(base64string); l<len && base64string[l] != '=' && CarlaBase64Helpers::isBase64Char(base64string[l]); ++l)
  56. {
  57. charArray4[i++] = static_cast<uint>(base64string[l]);
  58. if (i == 4)
  59. {
  60. for (i=0; i<4; ++i)
  61. charArray4[i] = CarlaBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[i]));
  62. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  63. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  64. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  65. for (i=0; i<3; ++i)
  66. ret.push_back(static_cast<uint8_t>(charArray3[i]));
  67. i = 0;
  68. }
  69. }
  70. if (i != 0)
  71. {
  72. for (j=i; j<4; ++j)
  73. charArray4[j] = 0;
  74. for (j=0; j<4; ++j)
  75. charArray4[j] = CarlaBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[j]));
  76. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  77. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  78. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  79. for (j=0; i>0 && j<i-1; j++)
  80. ret.push_back(static_cast<uint8_t>(charArray3[j]));
  81. }
  82. return ret;
  83. }
  84. // -----------------------------------------------------------------------
  85. #endif // CARLA_BASE64_UTILS_HPP_INCLUDED