Utils.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "Utils.h"
  2. #include <stdlib.h> // for strtol
  3. #include <iomanip> // for operator<<, setfill, setw
  4. #include <iostream> // for basic_ostream, hex
  5. #include <sstream> // for stringstream
  6. #include <string> // for string
  7. #include <type_traits> // for enable_if<>::type
  8. #include <chrono>
  9. #ifndef _WIN32
  10. #include <arpa/inet.h>
  11. #endif
  12. unsigned long long getCurrentTimestamp() {
  13. return std::chrono::duration_cast<std::chrono::milliseconds>(
  14. std::chrono::system_clock::now().time_since_epoch())
  15. .count();
  16. }
  17. uint64_t hton64(uint64_t value) {
  18. int num = 42;
  19. if (*(char*)&num == 42) {
  20. uint32_t high_part = htonl((uint32_t)(value >> 32));
  21. uint32_t low_part = htonl((uint32_t)(value & 0xFFFFFFFFLL));
  22. return (((uint64_t)low_part) << 32) | high_part;
  23. } else {
  24. return value;
  25. }
  26. }
  27. std::vector<uint8_t> stringHexToBytes(const std::string& s) {
  28. std::vector<uint8_t> v;
  29. v.reserve(s.length() / 2);
  30. for (std::string::size_type i = 0; i < s.length(); i += 2) {
  31. std::string byteString = s.substr(i, 2);
  32. uint8_t byte = (uint8_t)strtol(byteString.c_str(), NULL, 16);
  33. v.push_back(byte);
  34. }
  35. return v;
  36. }
  37. std::string bytesToHexString(const std::vector<uint8_t>& v) {
  38. std::stringstream ss;
  39. ss << std::hex << std::setfill('0');
  40. std::vector<uint8_t>::const_iterator it;
  41. for (it = v.begin(); it != v.end(); it++) {
  42. ss << std::setw(2) << static_cast<unsigned>(*it);
  43. }
  44. return ss.str();
  45. }
  46. std::vector<uint8_t> bigNumAdd(std::vector<uint8_t> num, int n) {
  47. auto carry = n;
  48. for (int x = num.size() - 1; x >= 0; x--) {
  49. int res = num[x] + carry;
  50. if (res < 256) {
  51. carry = 0;
  52. num[x] = res;
  53. } else {
  54. // Carry the rest of the division
  55. carry = res / 256;
  56. num[x] = res % 256;
  57. // extend the vector at the last index
  58. if (x == 0) {
  59. num.insert(num.begin(), carry);
  60. return num;
  61. }
  62. }
  63. }
  64. return num;
  65. }
  66. std::vector<uint8_t> bigNumDivide(std::vector<uint8_t> num, int n) {
  67. auto carry = 0;
  68. for (int x = 0; x < num.size(); x++) {
  69. int res = num[x] + carry * 256;
  70. if (res < n) {
  71. carry = res;
  72. num[x] = 0;
  73. } else {
  74. // Carry the rest of the division
  75. carry = res % n;
  76. num[x] = res / n;
  77. }
  78. }
  79. return num;
  80. }
  81. std::vector<uint8_t> bigNumMultiply(std::vector<uint8_t> num, int n) {
  82. auto carry = 0;
  83. for (int x = num.size() - 1; x >= 0; x--) {
  84. int res = num[x] * n + carry;
  85. if (res < 256) {
  86. carry = 0;
  87. num[x] = res;
  88. } else {
  89. // Carry the rest of the division
  90. carry = res / 256;
  91. num[x] = res % 256;
  92. // extend the vector at the last index
  93. if (x == 0) {
  94. num.insert(num.begin(), carry);
  95. return num;
  96. }
  97. }
  98. }
  99. return num;
  100. }
  101. unsigned char h2int(char c) {
  102. if (c >= '0' && c <= '9') {
  103. return ((unsigned char)c - '0');
  104. }
  105. if (c >= 'a' && c <= 'f') {
  106. return ((unsigned char)c - 'a' + 10);
  107. }
  108. if (c >= 'A' && c <= 'F') {
  109. return ((unsigned char)c - 'A' + 10);
  110. }
  111. return (0);
  112. }
  113. std::string urlDecode(std::string str) {
  114. std::string encodedString = "";
  115. char c;
  116. char code0;
  117. char code1;
  118. for (int i = 0; i < str.length(); i++) {
  119. c = str[i];
  120. if (c == '+') {
  121. encodedString += ' ';
  122. } else if (c == '%') {
  123. i++;
  124. code0 = str[i];
  125. i++;
  126. code1 = str[i];
  127. c = (h2int(code0) << 4) | h2int(code1);
  128. encodedString += c;
  129. } else {
  130. encodedString += c;
  131. }
  132. }
  133. return encodedString;
  134. }