utils.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #ifndef UTILS_H
  2. #define UTILS_H
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <cstdint>
  7. #include <sstream>
  8. #include <iomanip>
  9. namespace Utils {
  10. /**
  11. * @brief Base64 encoding table
  12. */
  13. static const char base64_chars[] =
  14. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  15. "abcdefghijklmnopqrstuvwxyz"
  16. "0123456789+/";
  17. /**
  18. * @brief Encode binary data to base64
  19. *
  20. * @param data The binary data to encode
  21. * @return Base64 encoded string
  22. */
  23. inline std::string base64Encode(const std::vector<uint8_t>& data) {
  24. std::string ret;
  25. int i = 0;
  26. int j = 0;
  27. uint8_t char_array_3[3];
  28. uint8_t char_array_4[4];
  29. size_t in_len = data.size();
  30. const uint8_t* bytes_to_encode = data.data();
  31. while (in_len--) {
  32. char_array_3[i++] = *(bytes_to_encode++);
  33. if (i == 3) {
  34. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  35. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  36. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  37. char_array_4[3] = char_array_3[2] & 0x3f;
  38. for(i = 0; i < 4; i++)
  39. ret += base64_chars[char_array_4[i]];
  40. i = 0;
  41. }
  42. }
  43. if (i) {
  44. for(j = i; j < 3; j++)
  45. char_array_3[j] = '\0';
  46. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  47. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  48. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  49. for (j = 0; j < i + 1; j++)
  50. ret += base64_chars[char_array_4[j]];
  51. while((i++ < 3))
  52. ret += '=';
  53. }
  54. return ret;
  55. }
  56. /**
  57. * @brief Check if a character is a base64 character
  58. *
  59. * @param c The character to check
  60. * @return true if the character is a base64 character, false otherwise
  61. */
  62. inline bool isBase64(unsigned char c) {
  63. return (isalnum(c) || (c == '+') || (c == '/'));
  64. }
  65. /**
  66. * @brief Decode base64 string to binary data
  67. *
  68. * @param encoded_string The base64 encoded string
  69. * @return Decoded binary data
  70. */
  71. inline std::vector<uint8_t> base64Decode(const std::string& encoded_string) {
  72. size_t in_len = encoded_string.size();
  73. size_t i = 0;
  74. size_t j = 0;
  75. int in_ = 0;
  76. uint8_t char_array_4[4], char_array_3[3];
  77. std::vector<uint8_t> ret;
  78. while (in_len-- && (encoded_string[in_] != '=') && isBase64(encoded_string[in_])) {
  79. char_array_4[i++] = encoded_string[in_]; in_++;
  80. if (i == 4) {
  81. for (i = 0; i < 4; i++)
  82. char_array_4[i] = static_cast<uint8_t>(std::string(base64_chars).find(char_array_4[i]));
  83. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  84. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  85. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  86. for (i = 0; i < 3; i++)
  87. ret.push_back(char_array_3[i]);
  88. i = 0;
  89. }
  90. }
  91. if (i) {
  92. for (j = 0; j < i; j++)
  93. char_array_4[j] = static_cast<uint8_t>(std::string(base64_chars).find(char_array_4[j]));
  94. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  95. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  96. for (j = 0; j < i - 1; j++)
  97. ret.push_back(char_array_3[j]);
  98. }
  99. return ret;
  100. }
  101. /**
  102. * @brief Check if a string ends with a given suffix
  103. *
  104. * @param str The string to check
  105. * @param suffix The suffix to look for
  106. * @return true if str ends with suffix, false otherwise
  107. */
  108. inline bool endsWith(const std::string& str, const std::string& suffix) {
  109. return str.size() >= suffix.size() &&
  110. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  111. }
  112. /**
  113. * @brief Check if a string starts with a given prefix
  114. *
  115. * @param str The string to check
  116. * @param prefix The prefix to look for
  117. * @return true if str starts with prefix, false otherwise
  118. */
  119. inline bool startsWith(const std::string& str, const std::string& prefix) {
  120. return str.size() >= prefix.size() &&
  121. str.compare(0, prefix.size(), prefix) == 0;
  122. }
  123. /**
  124. * @brief Convert string to lowercase
  125. *
  126. * @param str The string to convert
  127. * @return Lowercase version of the string
  128. */
  129. inline std::string toLower(const std::string& str) {
  130. std::string result = str;
  131. std::transform(result.begin(), result.end(), result.begin(), ::tolower);
  132. return result;
  133. }
  134. /**
  135. * @brief Convert string to uppercase
  136. *
  137. * @param str The string to convert
  138. * @return Uppercase version of the string
  139. */
  140. inline std::string toUpper(const std::string& str) {
  141. std::string result = str;
  142. std::transform(result.begin(), result.end(), result.begin(), ::toupper);
  143. return result;
  144. }
  145. /**
  146. * @brief Trim whitespace from start of string
  147. *
  148. * @param str The string to trim
  149. * @return Trimmed string
  150. */
  151. inline std::string trimLeft(const std::string& str) {
  152. size_t start = str.find_first_not_of(" \t\n\r\f\v");
  153. return (start == std::string::npos) ? "" : str.substr(start);
  154. }
  155. /**
  156. * @brief Trim whitespace from end of string
  157. *
  158. * @param str The string to trim
  159. * @return Trimmed string
  160. */
  161. inline std::string trimRight(const std::string& str) {
  162. size_t end = str.find_last_not_of(" \t\n\r\f\v");
  163. return (end == std::string::npos) ? "" : str.substr(0, end + 1);
  164. }
  165. /**
  166. * @brief Trim whitespace from both ends of string
  167. *
  168. * @param str The string to trim
  169. * @return Trimmed string
  170. */
  171. inline std::string trim(const std::string& str) {
  172. return trimLeft(trimRight(str));
  173. }
  174. /**
  175. * @brief Split a string by delimiter
  176. *
  177. * @param str The string to split
  178. * @param delimiter The delimiter character
  179. * @return Vector of string parts
  180. */
  181. inline std::vector<std::string> split(const std::string& str, char delimiter) {
  182. std::vector<std::string> result;
  183. std::string current;
  184. for (char c : str) {
  185. if (c == delimiter) {
  186. if (!current.empty()) {
  187. result.push_back(current);
  188. current.clear();
  189. }
  190. } else {
  191. current += c;
  192. }
  193. }
  194. if (!current.empty()) {
  195. result.push_back(current);
  196. }
  197. return result;
  198. }
  199. /**
  200. * @brief Join strings with a delimiter
  201. *
  202. * @param parts The strings to join
  203. * @param delimiter The delimiter to use
  204. * @return Joined string
  205. */
  206. inline std::string join(const std::vector<std::string>& parts, const std::string& delimiter) {
  207. if (parts.empty()) return "";
  208. std::string result = parts[0];
  209. for (size_t i = 1; i < parts.size(); i++) {
  210. result += delimiter + parts[i];
  211. }
  212. return result;
  213. }
  214. } // namespace Utils
  215. #endif // UTILS_H