Utils.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef UTILS_H
  2. #define UTILS_H
  3. #include <vector>
  4. #ifdef _WIN32
  5. #include <winsock2.h>
  6. #include <ws2tcpip.h>
  7. #include "win32shim.h"
  8. #else
  9. #include <unistd.h>
  10. #include "sys/socket.h"
  11. #include <netdb.h>
  12. #include <netinet/in.h>
  13. #endif
  14. #include <cstdint>
  15. #include <cstring>
  16. #include <memory>
  17. #include <chrono>
  18. #include <string>
  19. #include <sstream>
  20. #include <iostream>
  21. #include <iomanip>
  22. #include <memory>
  23. #include <string>
  24. #include <stdexcept>
  25. #define HMAC_SHA1_BLOCKSIZE 64
  26. /**
  27. * @brief Returns current timestamp
  28. *
  29. * @return unsigned long long resulting timestamp in milliseconds from unix time zero
  30. */
  31. unsigned long long getCurrentTimestamp();
  32. /**
  33. * @brief portable 64bit equivalent of htons / htonl. aka endianess swap
  34. *
  35. * @param value input value to swap
  36. * @return uint64_t swapped result
  37. */
  38. uint64_t hton64(uint64_t value);
  39. std::vector<uint8_t> bigNumDivide(std::vector<uint8_t> num, int n);
  40. /**
  41. * @brief Performs big number multiplication on two numbers
  42. *
  43. * @param num big num in vector format
  44. * @param n secondary number
  45. * @return std::vector<uint8_t> resulting number
  46. */
  47. std::vector<uint8_t> bigNumMultiply(std::vector<uint8_t> num, int n);
  48. /**
  49. * @brief Performs big number addition on two numbers
  50. *
  51. * @param num big num in vector format
  52. * @param n secondary number
  53. * @return std::vector<uint8_t> resulting number
  54. */
  55. std::vector<uint8_t> bigNumAdd(std::vector<uint8_t> num, int n);
  56. unsigned char h2int(char c);
  57. std::string urlDecode(std::string str);
  58. /**
  59. * @brief Converts provided hex string into binary data
  60. *
  61. * @param s string containing hex data
  62. * @return std::vector<uint8_t> vector containing binary data
  63. */
  64. std::vector<uint8_t> stringHexToBytes(const std::string &s);
  65. /**
  66. * @brief Converts provided bytes into a human readable hex string
  67. *
  68. * @param bytes vector containing binary data
  69. * @return std::string string containing hex representation of inputted data
  70. */
  71. std::string bytesToHexString(const std::vector<uint8_t> &bytes);
  72. /**
  73. * @brief Extracts given type from binary data
  74. *
  75. * @tparam T type to extract
  76. * @param v vector containing binary data to extract from
  77. * @param pos position offset
  78. * @return T extracted type
  79. */
  80. template <typename T>
  81. T extract(const std::vector<unsigned char> &v, int pos)
  82. {
  83. T value;
  84. memcpy(&value, &v[pos], sizeof(T));
  85. return value;
  86. }
  87. /**
  88. * @brief Packs given type into binary data
  89. *
  90. * @tparam T type of data to pack
  91. * @param data data to pack
  92. * @return std::vector<uint8_t> resulting vector containing binary data
  93. */
  94. template <typename T>
  95. std::vector<uint8_t> pack(T data)
  96. {
  97. std::vector<std::uint8_t> rawData( (std::uint8_t*)&data, (std::uint8_t*)&(data) + sizeof(T));
  98. return rawData;
  99. }
  100. template<typename ... Args>
  101. std::string string_format( const std::string& format, Args ... args )
  102. {
  103. int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
  104. if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); }
  105. auto size = static_cast<size_t>( size_s );
  106. std::unique_ptr<char[]> buf( new char[ size ] );
  107. std::snprintf( buf.get(), size, format.c_str(), args ... );
  108. return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
  109. }
  110. #endif