CLewaimaiString.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "../pch/pch.h"
  2. #include "CLewaimaiString.h"
  3. #include <boost/archive/iterators/base64_from_binary.hpp>
  4. #include <boost/archive/iterators/binary_from_base64.hpp>
  5. #include <boost/archive/iterators/transform_width.hpp>
  6. #include <string>
  7. #include <iostream>
  8. #include <sstream>
  9. using namespace std;
  10. using namespace boost::archive::iterators;
  11. unsigned char ToHex(unsigned char x)
  12. {
  13. return x > 9 ? x + 55 : x + 48;
  14. }
  15. unsigned char FromHex(unsigned char x)
  16. {
  17. unsigned char y;
  18. if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
  19. else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
  20. else if (x >= '0' && x <= '9') y = x - '0';
  21. else assert(0);
  22. return y;
  23. }
  24. CLewaimaiString::CLewaimaiString()
  25. {
  26. }
  27. CLewaimaiString::~CLewaimaiString()
  28. {
  29. }
  30. bool CLewaimaiString::base64_encode(const string& input, string* output)
  31. {
  32. typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator;
  33. stringstream result;
  34. try {
  35. copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result));
  36. }
  37. catch (...) {
  38. return false;
  39. }
  40. size_t equal_count = (3 - input.length() % 3) % 3;
  41. for (size_t i = 0; i < equal_count; i++)
  42. {
  43. result.put('=');
  44. }
  45. *output = result.str();
  46. return output->empty() == false;
  47. }
  48. bool CLewaimaiString::base64_decode(const string& input, string* output)
  49. {
  50. typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator;
  51. stringstream result;
  52. try {
  53. copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result));
  54. }
  55. catch (...) {
  56. return false;
  57. }
  58. *output = result.str();
  59. return output->empty() == false;
  60. }
  61. void CLewaimaiString::trim(string &s)
  62. {
  63. if (!s.empty())
  64. {
  65. s.erase(0, s.find_first_not_of(" "));
  66. s.erase(s.find_last_not_of(" ") + 1);
  67. }
  68. }
  69. std::string CLewaimaiString::UrlEncode(const std::string& str)
  70. {
  71. std::string strTemp = "";
  72. size_t length = str.length();
  73. for (size_t i = 0; i < length; i++)
  74. {
  75. if (isalnum((unsigned char)str[i]) ||
  76. (str[i] == '-') ||
  77. (str[i] == '_') ||
  78. (str[i] == '.') ||
  79. (str[i] == '~'))
  80. strTemp += str[i];
  81. else if (str[i] == ' ')
  82. strTemp += "+";
  83. else
  84. {
  85. strTemp += '%';
  86. strTemp += ToHex((unsigned char)str[i] >> 4);
  87. strTemp += ToHex((unsigned char)str[i] % 16);
  88. }
  89. }
  90. return strTemp;
  91. }
  92. std::string CLewaimaiString::UrlDecode(const std::string& str)
  93. {
  94. std::string strTemp = "";
  95. size_t length = str.length();
  96. for (size_t i = 0; i < length; i++)
  97. {
  98. if (str[i] == '+') strTemp += ' ';
  99. else if (str[i] == '%')
  100. {
  101. assert(i + 2 < length);
  102. unsigned char high = FromHex((unsigned char)str[++i]);
  103. unsigned char low = FromHex((unsigned char)str[++i]);
  104. strTemp += high * 16 + low;
  105. }
  106. else strTemp += str[i];
  107. }
  108. return strTemp;
  109. }