utils.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Copyright (c) 2020-2022 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include "utils.hpp"
  9. #include "impl/internals.hpp"
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <chrono>
  13. #include <functional>
  14. #include <iterator>
  15. #include <sstream>
  16. #include <thread>
  17. #if defined(_WIN32)
  18. #include <Windows.h>
  19. typedef HRESULT(WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
  20. #endif
  21. #if defined(__linux__)
  22. #include <sys/prctl.h> // for prctl(PR_SET_NAME)
  23. #endif
  24. #if defined(__FreeBSD__)
  25. #include <pthread_np.h> // for pthread_set_name_np
  26. #endif
  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 {
  118. void thread_set_name_self(const char *name) {
  119. #if defined(_WIN32)
  120. int name_length = (int)strlen(name);
  121. int wname_length =
  122. MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, name, name_length, nullptr, 0);
  123. if (wname_length > 0) {
  124. std::wstring wname(wname_length, L'\0');
  125. wname_length = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, name, name_length,
  126. &wname[0], wname_length + 1);
  127. HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
  128. if (kernel32 != nullptr) {
  129. auto pSetThreadDescription =
  130. (pfnSetThreadDescription)GetProcAddress(kernel32, "SetThreadDescription");
  131. if (pSetThreadDescription != nullptr) {
  132. pSetThreadDescription(GetCurrentThread(), wname.c_str());
  133. }
  134. }
  135. }
  136. #elif defined(__linux__)
  137. prctl(PR_SET_NAME, name);
  138. #elif defined(__APPLE__)
  139. pthread_setname_np(name);
  140. #elif defined(__FreeBSD__)
  141. pthread_set_name_np(pthread_self(), name);
  142. #else
  143. (void)name;
  144. #endif
  145. }
  146. } // namespace
  147. namespace this_thread {
  148. void set_name(const string &name) { thread_set_name_self(name.c_str()); }
  149. } // namespace this_thread
  150. } // namespace rtc::impl::utils