base64.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "base64.h"
  2. using std::string;
  3. namespace baidu {
  4. namespace base {
  5. namespace base64 {
  6. namespace {
  7. const string base64_chars =
  8. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9. "abcdefghijklmnopqrstuvwxyz"
  10. "0123456789+/";
  11. inline bool is_base64(unsigned char c) {
  12. return isalnum(c) || (c == '+') || (c == '/');
  13. }
  14. inline size_t encode_len(size_t input_len) {
  15. return (input_len + 2) / 3 * 4;
  16. }
  17. void encode_char_array(unsigned char *encode_block, const unsigned char *decode_block) {
  18. encode_block[0] = (decode_block[0] & 0xfc) >> 2;
  19. encode_block[1] = ((decode_block[0] & 0x03) << 4) + ((decode_block[1] & 0xf0) >> 4);
  20. encode_block[2] = ((decode_block[1] & 0x0f) << 2) + ((decode_block[2] & 0xc0) >> 6);
  21. encode_block[3] = decode_block[2] & 0x3f;
  22. }
  23. void decode_char_array(unsigned char *encode_block, unsigned char *decode_block) {
  24. for (int i = 0; i < 4; ++i) {
  25. encode_block[i] = base64_chars.find(encode_block[i]);
  26. }
  27. decode_block[0] = (encode_block[0] << 2) + ((encode_block[1] & 0x30) >> 4);
  28. decode_block[1] = ((encode_block[1] & 0xf) << 4) + ((encode_block[2] & 0x3c) >> 2);
  29. decode_block[2] = ((encode_block[2] & 0x3) << 6) + encode_block[3];
  30. }
  31. }
  32. string base64_encode(const string& input) {
  33. string output;
  34. size_t i = 0;
  35. unsigned char decode_block[3];
  36. unsigned char encode_block[4];
  37. for (string::size_type len = 0; len != input.size(); ++len) {
  38. decode_block[i++] = input[len];
  39. if (i == 3) {
  40. encode_char_array(encode_block, decode_block);
  41. for (i = 0; i < 4; ++i) {
  42. output += base64_chars[encode_block[i]];
  43. }
  44. i = 0;
  45. }
  46. }
  47. if (i > 0) {
  48. for (size_t j = i; j < 3; ++j) {
  49. decode_block[j] = '\0';
  50. }
  51. encode_char_array(encode_block, decode_block);
  52. for (size_t j = 0; j < i + 1; ++j) {
  53. output += base64_chars[encode_block[j]];
  54. }
  55. while (i++ < 3) {
  56. output += '=';
  57. }
  58. }
  59. return output;
  60. }
  61. string base64_decode(const string& encoded_string) {
  62. int in_len = encoded_string.size();
  63. int i = 0;
  64. int len = 0;
  65. unsigned char encode_block[4];
  66. unsigned char decode_block[3];
  67. string output;
  68. while (in_len-- && (encoded_string[len] != '=') && is_base64(encoded_string[len])) {
  69. encode_block[i++] = encoded_string[len];
  70. len++;
  71. if (i == 4) {
  72. decode_char_array(encode_block, decode_block);
  73. for (int j = 0; j < 3; ++j) {
  74. output += decode_block[j];
  75. }
  76. i = 0;
  77. }
  78. }
  79. if (i > 0) {
  80. for (int j = i; j < 4; ++j) {
  81. encode_block[j] = 0;
  82. }
  83. decode_char_array(encode_block, decode_block);
  84. for (int j = 0; j < i - 1; ++j) {
  85. output += decode_block[j];
  86. }
  87. }
  88. return output;
  89. }
  90. }
  91. }
  92. }
  93. // #include <string>
  94. // #include <cassert>
  95. // #include <limits>
  96. // #include <stdexcept>
  97. // #include <ctype.h>
  98. // #include "base64_utils.h"
  99. // namespace util {
  100. // namespace crypto {
  101. // static const char b64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  102. // static const char reverse_table[128] = {
  103. // 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  104. // 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  105. // 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  106. // 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  107. // 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  108. // 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  109. // 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  110. // 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64
  111. // };
  112. // std::string Base64Utils::encode(const ::std::string& data) {
  113. // try {
  114. // if (data.size() > (std::numeric_limits<std::string::size_type>::max() / 4u) * 3u) {
  115. // throw ::std::length_error("Converting too large a string to base64.");
  116. // }
  117. // } catch (std::length_error& e) {
  118. // printf("%s\n", e.what());
  119. // return "";
  120. // }
  121. // const std::size_t binlen = data.size();
  122. // std::string retval((((binlen + 2) / 3) * 4), '=');
  123. // std::size_t outpos = 0;
  124. // int bits_collected = 0;
  125. // unsigned int accumulator = 0;
  126. // const std::string::const_iterator binend = data.end();
  127. // for (std::string::const_iterator i = data.begin(); i != binend; ++i) {
  128. // accumulator = (accumulator << 8) | (*i & 0xffu);
  129. // bits_collected += 8;
  130. // while (bits_collected >= 6) {
  131. // bits_collected -= 6;
  132. // retval[outpos++] = b64_table[(accumulator >> bits_collected) & 0x3fu];
  133. // }
  134. // }
  135. // if (bits_collected > 0) { // Any trailing bits that are missing.
  136. // assert(bits_collected < 6);
  137. // accumulator <<= 6 - bits_collected;
  138. // retval[outpos++] = b64_table[accumulator & 0x3fu];
  139. // }
  140. // assert(outpos >= (retval.size() - 2));
  141. // assert(outpos <= retval.size());
  142. // return retval;
  143. // }
  144. // std::string Base64Utils::decode(const std::string& data) {
  145. // std::string retval;
  146. // const std::string::const_iterator last = data.end();
  147. // int bits_collected = 0;
  148. // unsigned int accumulator = 0;
  149. // try {
  150. // for (std::string::const_iterator i = data.begin(); i != last; ++i) {
  151. // const int c = *i;
  152. // if (isspace(c) || c == '=') {
  153. // continue;
  154. // }
  155. // if ((c > 127) || (c < 0) || (reverse_table[c] > 63)) {
  156. // throw ::std::invalid_argument("This contains characters not legal in a base64 encoded string.");
  157. // }
  158. // accumulator = (accumulator << 6) | reverse_table[c];
  159. // bits_collected += 6;
  160. // if (bits_collected >= 8) {
  161. // bits_collected -= 8;
  162. // retval += static_cast<char>((accumulator >> bits_collected) & 0xffu);
  163. // }
  164. // }
  165. // } catch (std::invalid_argument& e) {
  166. // printf("%s\n", e.what());
  167. // return "";
  168. // }
  169. // return retval;
  170. // }
  171. // }
  172. // }