Utils.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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::string bytesToHexString(std::vector<uint8_t>& v) {
  24. std::stringstream ss;
  25. ss << std::hex << std::setfill('0');
  26. std::vector<uint8_t>::const_iterator it;
  27. for (it = v.begin(); it != v.end(); it++) {
  28. ss << std::setw(2) << static_cast<unsigned>(*it);
  29. }
  30. return ss.str();
  31. }
  32. std::vector<uint8_t> bigNumAdd(std::vector<uint8_t> num, int n)
  33. {
  34. auto carry = n;
  35. for (int x = num.size() - 1; x >= 0; x--)
  36. {
  37. int res = num[x] + carry;
  38. if (res < 256)
  39. {
  40. carry = 0;
  41. num[x] = res;
  42. }
  43. else
  44. {
  45. // Carry the rest of the division
  46. carry = res / 256;
  47. num[x] = res % 256;
  48. // extend the vector at the last index
  49. if (x == 0)
  50. {
  51. num.insert(num.begin(), carry);
  52. return num;
  53. }
  54. }
  55. }
  56. return num;
  57. }
  58. std::vector<uint8_t> bigNumMultiply(std::vector<uint8_t> num, int n)
  59. {
  60. auto carry = 0;
  61. for (int x = num.size() - 1; x >= 0; x--)
  62. {
  63. int res = num[x] * n + carry;
  64. if (res < 256)
  65. {
  66. carry = 0;
  67. num[x] = res;
  68. }
  69. else
  70. {
  71. // Carry the rest of the division
  72. carry = res / 256;
  73. num[x] = res % 256;
  74. // extend the vector at the last index
  75. if (x == 0)
  76. {
  77. num.insert(num.begin(), carry);
  78. return num;
  79. }
  80. }
  81. }
  82. return num;
  83. }
  84. unsigned char h2int(char c)
  85. {
  86. if (c >= '0' && c <='9'){
  87. return((unsigned char)c - '0');
  88. }
  89. if (c >= 'a' && c <='f'){
  90. return((unsigned char)c - 'a' + 10);
  91. }
  92. if (c >= 'A' && c <='F'){
  93. return((unsigned char)c - 'A' + 10);
  94. }
  95. return(0);
  96. }
  97. std::string urlDecode(std::string str)
  98. {
  99. std::string encodedString="";
  100. char c;
  101. char code0;
  102. char code1;
  103. for (int i =0; i < str.length(); i++){
  104. c=str[i];
  105. if (c == '+'){
  106. encodedString+=' ';
  107. }else if (c == '%') {
  108. i++;
  109. code0=str[i];
  110. i++;
  111. code1=str[i];
  112. c = (h2int(code0) << 4) | h2int(code1);
  113. encodedString+=c;
  114. } else{
  115. encodedString+=c;
  116. }
  117. }
  118. return encodedString;
  119. }