Utils.h 3.2 KB

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