utils.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Copyright (c) 2020-2022 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "utils.hpp"
  19. #include "impl/internals.hpp"
  20. #include <algorithm>
  21. #include <cctype>
  22. #include <chrono>
  23. #include <functional>
  24. #include <iterator>
  25. #include <sstream>
  26. #include <thread>
  27. namespace rtc::impl::utils {
  28. using std::to_integer;
  29. std::vector<string> explode(const string &str, char delim) {
  30. std::vector<std::string> result;
  31. std::istringstream ss(str);
  32. string token;
  33. while (std::getline(ss, token, delim))
  34. result.push_back(token);
  35. return result;
  36. }
  37. string implode(const std::vector<string> &tokens, char delim) {
  38. string sdelim(1, delim);
  39. std::ostringstream ss;
  40. std::copy(tokens.begin(), tokens.end(), std::ostream_iterator<string>(ss, sdelim.c_str()));
  41. string result = ss.str();
  42. if (result.size() > 0)
  43. result.resize(result.size() - 1);
  44. return result;
  45. }
  46. string url_decode(const string &str) {
  47. string result;
  48. size_t i = 0;
  49. while (i < str.size()) {
  50. char c = str[i++];
  51. if (c == '%') {
  52. auto value = str.substr(i, 2);
  53. try {
  54. if (value.size() != 2 || !std::isxdigit(value[0]) || !std::isxdigit(value[1]))
  55. throw std::exception();
  56. c = static_cast<char>(std::stoi(value, nullptr, 16));
  57. i += 2;
  58. } catch (...) {
  59. PLOG_WARNING << "Invalid percent-encoded character in URL: \"%" + value + "\"";
  60. }
  61. }
  62. result.push_back(c);
  63. }
  64. return result;
  65. }
  66. string base64_encode(const binary &data) {
  67. static const char tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  68. string out;
  69. out.reserve(3 * ((data.size() + 3) / 4));
  70. int i = 0;
  71. while (data.size() - i >= 3) {
  72. auto d0 = to_integer<uint8_t>(data[i]);
  73. auto d1 = to_integer<uint8_t>(data[i + 1]);
  74. auto d2 = to_integer<uint8_t>(data[i + 2]);
  75. out += tab[d0 >> 2];
  76. out += tab[((d0 & 3) << 4) | (d1 >> 4)];
  77. out += tab[((d1 & 0x0F) << 2) | (d2 >> 6)];
  78. out += tab[d2 & 0x3F];
  79. i += 3;
  80. }
  81. int left = int(data.size() - i);
  82. if (left) {
  83. auto d0 = to_integer<uint8_t>(data[i]);
  84. out += tab[d0 >> 2];
  85. if (left == 1) {
  86. out += tab[(d0 & 3) << 4];
  87. out += '=';
  88. } else { // left == 2
  89. auto d1 = to_integer<uint8_t>(data[i + 1]);
  90. out += tab[((d0 & 3) << 4) | (d1 >> 4)];
  91. out += tab[(d1 & 0x0F) << 2];
  92. }
  93. out += '=';
  94. }
  95. return out;
  96. }
  97. std::seed_seq random_seed() {
  98. std::vector<unsigned int> seed;
  99. // Seed with random device
  100. try {
  101. // On some systems an exception might be thrown if the random_device can't be initialized
  102. std::random_device device;
  103. // 128 bits should be more than enough
  104. std::generate_n(std::back_inserter(seed), 4, std::ref(device));
  105. } catch (...) {
  106. // Ignore
  107. }
  108. // Seed with high-resolution clock
  109. using std::chrono::high_resolution_clock;
  110. seed.push_back(
  111. static_cast<unsigned int>(high_resolution_clock::now().time_since_epoch().count()));
  112. // Seed with thread id
  113. seed.push_back(
  114. static_cast<unsigned int>(std::hash<std::thread::id>{}(std::this_thread::get_id())));
  115. return std::seed_seq(seed.begin(), seed.end());
  116. }
  117. } // namespace rtc::impl::utils