utils.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. char_array_4[3] = char_array_3[2] & 0x3f;
  50. for (j = 0; j < i + 1; j++)
  51. ret += base64_chars[char_array_4[j]];
  52. while((i++ < 3))
  53. ret += '=';
  54. }
  55. return ret;
  56. }
  57. /**
  58. * @brief Check if a character is a base64 character
  59. *
  60. * @param c The character to check
  61. * @return true if the character is a base64 character, false otherwise
  62. */
  63. inline bool isBase64(unsigned char c) {
  64. return (isalnum(c) || (c == '+') || (c == '/'));
  65. }
  66. /**
  67. * @brief Decode base64 string to binary data
  68. *
  69. * @param encoded_string The base64 encoded string
  70. * @return Decoded binary data
  71. */
  72. inline std::vector<uint8_t> base64Decode(const std::string& encoded_string) {
  73. size_t in_len = encoded_string.size();
  74. size_t i = 0;
  75. size_t j = 0;
  76. int in_ = 0;
  77. uint8_t char_array_4[4], char_array_3[3];
  78. std::vector<uint8_t> ret;
  79. while (in_len-- && (encoded_string[in_] != '=') && isBase64(encoded_string[in_])) {
  80. char_array_4[i++] = encoded_string[in_]; in_++;
  81. if (i == 4) {
  82. for (i = 0; i < 4; i++) {
  83. size_t pos = std::string(base64_chars).find(char_array_4[i]);
  84. if (pos == std::string::npos) {
  85. return {}; // Return empty vector on invalid character
  86. }
  87. char_array_4[i] = static_cast<uint8_t>(pos);
  88. }
  89. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  90. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  91. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  92. for (i = 0; i < 3; i++)
  93. ret.push_back(char_array_3[i]);
  94. i = 0;
  95. }
  96. }
  97. if (i) {
  98. for (j = 0; j < i; j++) {
  99. size_t pos = std::string(base64_chars).find(char_array_4[j]);
  100. if (pos == std::string::npos) {
  101. return {}; // Return empty vector on invalid character
  102. }
  103. char_array_4[j] = static_cast<uint8_t>(pos);
  104. }
  105. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  106. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  107. for (j = 0; j < i - 1; j++)
  108. ret.push_back(char_array_3[j]);
  109. }
  110. return ret;
  111. }
  112. /**
  113. * @brief Check if a string ends with a given suffix
  114. *
  115. * @param str The string to check
  116. * @param suffix The suffix to look for
  117. * @return true if str ends with suffix, false otherwise
  118. */
  119. inline bool endsWith(const std::string& str, const std::string& suffix) {
  120. return str.size() >= suffix.size() &&
  121. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  122. }
  123. /**
  124. * @brief Check if a string starts with a given prefix
  125. *
  126. * @param str The string to check
  127. * @param prefix The prefix to look for
  128. * @return true if str starts with prefix, false otherwise
  129. */
  130. inline bool startsWith(const std::string& str, const std::string& prefix) {
  131. return str.size() >= prefix.size() &&
  132. str.compare(0, prefix.size(), prefix) == 0;
  133. }
  134. /**
  135. * @brief Convert string to lowercase
  136. *
  137. * @param str The string to convert
  138. * @return Lowercase version of the string
  139. */
  140. inline std::string toLower(const std::string& str) {
  141. std::string result = str;
  142. std::transform(result.begin(), result.end(), result.begin(), ::tolower);
  143. return result;
  144. }
  145. /**
  146. * @brief Convert string to uppercase
  147. *
  148. * @param str The string to convert
  149. * @return Uppercase version of the string
  150. */
  151. inline std::string toUpper(const std::string& str) {
  152. std::string result = str;
  153. std::transform(result.begin(), result.end(), result.begin(), ::toupper);
  154. return result;
  155. }
  156. /**
  157. * @brief Trim whitespace from start of string
  158. *
  159. * @param str The string to trim
  160. * @return Trimmed string
  161. */
  162. inline std::string trimLeft(const std::string& str) {
  163. size_t start = str.find_first_not_of(" \t\n\r\f\v");
  164. return (start == std::string::npos) ? "" : str.substr(start);
  165. }
  166. /**
  167. * @brief Trim whitespace from end of string
  168. *
  169. * @param str The string to trim
  170. * @return Trimmed string
  171. */
  172. inline std::string trimRight(const std::string& str) {
  173. size_t end = str.find_last_not_of(" \t\n\r\f\v");
  174. return (end == std::string::npos) ? "" : str.substr(0, end + 1);
  175. }
  176. /**
  177. * @brief Trim whitespace from both ends of string
  178. *
  179. * @param str The string to trim
  180. * @return Trimmed string
  181. */
  182. inline std::string trim(const std::string& str) {
  183. return trimLeft(trimRight(str));
  184. }
  185. /**
  186. * @brief Split a string by delimiter
  187. *
  188. * @param str The string to split
  189. * @param delimiter The delimiter character
  190. * @return Vector of string parts
  191. */
  192. inline std::vector<std::string> split(const std::string& str, char delimiter) {
  193. std::vector<std::string> result;
  194. std::string current;
  195. for (char c : str) {
  196. if (c == delimiter) {
  197. if (!current.empty()) {
  198. result.push_back(current);
  199. current.clear();
  200. }
  201. } else {
  202. current += c;
  203. }
  204. }
  205. if (!current.empty()) {
  206. result.push_back(current);
  207. }
  208. return result;
  209. }
  210. /**
  211. * @brief Join strings with a delimiter
  212. *
  213. * @param parts The strings to join
  214. * @param delimiter The delimiter to use
  215. * @return Joined string
  216. */
  217. inline std::string join(const std::vector<std::string>& parts, const std::string& delimiter) {
  218. if (parts.empty()) return "";
  219. std::string result = parts[0];
  220. for (size_t i = 1; i < parts.size(); i++) {
  221. result += delimiter + parts[i];
  222. }
  223. return result;
  224. }
  225. /**
  226. * @brief URL decode a string
  227. *
  228. * @param encoded The URL-encoded string
  229. * @return Decoded string
  230. */
  231. inline std::string urlDecode(const std::string& encoded) {
  232. std::string decoded;
  233. for (size_t i = 0; i < encoded.length(); ++i) {
  234. if (encoded[i] == '%' && i + 2 < encoded.length()) {
  235. // Convert %XX to character
  236. int hexValue;
  237. std::istringstream hexStream(encoded.substr(i + 1, 2));
  238. if (hexStream >> std::hex >> hexValue) {
  239. decoded += static_cast<char>(hexValue);
  240. i += 2;
  241. } else {
  242. decoded += encoded[i];
  243. }
  244. } else if (encoded[i] == '+') {
  245. // Convert '+' to space
  246. decoded += ' ';
  247. } else {
  248. decoded += encoded[i];
  249. }
  250. }
  251. return decoded;
  252. }
  253. /**
  254. * @brief Check if file is an image based on extension
  255. *
  256. * @param filename The filename to check
  257. * @return true if file is an image, false otherwise
  258. */
  259. inline bool isImageFile(const std::string& filename) {
  260. std::string lowerFilename = toLower(filename);
  261. return endsWith(lowerFilename, ".png") ||
  262. endsWith(lowerFilename, ".jpg") ||
  263. endsWith(lowerFilename, ".jpeg") ||
  264. endsWith(lowerFilename, ".gif") ||
  265. endsWith(lowerFilename, ".webp") ||
  266. endsWith(lowerFilename, ".bmp") ||
  267. endsWith(lowerFilename, ".tiff") ||
  268. endsWith(lowerFilename, ".tif");
  269. }
  270. } // namespace Utils
  271. #endif // UTILS_H