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.

CarlaBase64Utils.hpp 3.6KB

10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. carla_stderr2("findBase64CharIndex('%c') - failed", c);
  40. return 0;
  41. }
  42. static inline
  43. bool isBase64Char(const char c)
  44. {
  45. return (std::isalnum(c) || (c == '+') || (c == '/'));
  46. }
  47. } // namespace CarlaBase64Helpers
  48. // -----------------------------------------------------------------------
  49. static inline
  50. std::vector<uint8_t> carla_getChunkFromBase64String(const char* const base64string)
  51. {
  52. CARLA_SAFE_ASSERT_RETURN(base64string != nullptr, std::vector<uint8_t>());
  53. uint i=0, j=0;
  54. uint charArray3[3], charArray4[4];
  55. std::vector<uint8_t> ret;
  56. ret.reserve(std::strlen(base64string)*3/4 + 4);
  57. for (std::size_t l=0, len=std::strlen(base64string); l<len; ++l)
  58. {
  59. const char c = base64string[l];
  60. if (c == '\0' || c == '=')
  61. break;
  62. if (c == ' ' || c == '\n')
  63. continue;
  64. CARLA_SAFE_ASSERT_CONTINUE(CarlaBase64Helpers::isBase64Char(c));
  65. charArray4[i++] = static_cast<uint>(c);
  66. if (i == 4)
  67. {
  68. for (i=0; i<4; ++i)
  69. charArray4[i] = CarlaBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[i]));
  70. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  71. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  72. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  73. for (i=0; i<3; ++i)
  74. ret.push_back(static_cast<uint8_t>(charArray3[i]));
  75. i = 0;
  76. }
  77. }
  78. if (i != 0)
  79. {
  80. for (j=0; j<i && j<4; ++j)
  81. charArray4[j] = CarlaBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[j]));
  82. for (j=i; j<4; ++j)
  83. charArray4[j] = 0;
  84. charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  85. charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  86. charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  87. for (j=0; i>0 && j<i-1; j++)
  88. ret.push_back(static_cast<uint8_t>(charArray3[j]));
  89. }
  90. return ret;
  91. }
  92. // -----------------------------------------------------------------------
  93. #endif // CARLA_BASE64_UTILS_HPP_INCLUDED