123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #ifndef UTILS_H
- #define UTILS_H
- #include <cstdio> // for snprintf, size_t
- #include <vector> // for vector
- #ifdef _WIN32
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include "win32shim.h"
- #else
- #endif
- #include <cstdint> // for uint8_t, uint64_t
- #include <cstring> // for memcpy
- #include <memory> // for unique_ptr
- #include <stdexcept> // for runtime_error
- #include <string> // for string
- #define HMAC_SHA1_BLOCKSIZE 64
- unsigned long long getCurrentTimestamp();
- uint64_t hton64(uint64_t value);
- std::vector<uint8_t> bigNumDivide(std::vector<uint8_t> num, int n);
- std::vector<uint8_t> bigNumMultiply(std::vector<uint8_t> num, int n);
- std::vector<uint8_t> bigNumAdd(std::vector<uint8_t> num, int n);
- unsigned char h2int(char c);
- std::string urlDecode(std::string str);
- std::vector<uint8_t> stringHexToBytes(const std::string& s);
- std::string bytesToHexString(const std::vector<uint8_t>& bytes);
- template <typename T>
- T extract(const std::vector<unsigned char>& v, int pos) {
- T value;
- memcpy(&value, &v[pos], sizeof(T));
- return value;
- }
- template <typename T>
- std::vector<uint8_t> pack(T data) {
- std::vector<std::uint8_t> rawData((std::uint8_t*)&data,
- (std::uint8_t*)&(data) + sizeof(T));
- return rawData;
- }
- template <typename... Args>
- std::string string_format(const std::string& format, Args... args) {
- int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) +
- 1;
- if (size_s <= 0) {
- throw std::runtime_error("Error during formatting.");
- }
- auto size = static_cast<size_t>(size_s);
- std::unique_ptr<char[]> buf(new char[size]);
- std::snprintf(buf.get(), size, format.c_str(), args...);
- return std::string(buf.get(),
- buf.get() + size - 1);
- }
- #endif
|