2
0

Utils.cpp 3.7 KB

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