base64.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "encryption/util/include/crypto/base64.h"
  15. using std::string;
  16. namespace baidu {
  17. namespace base {
  18. namespace base64 {
  19. namespace {
  20. const string base64_chars = // NOLINT
  21. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  22. "abcdefghijklmnopqrstuvwxyz"
  23. "0123456789+/";
  24. inline bool is_base64(unsigned char c) {
  25. return isalnum(c) || (c == '+') || (c == '/');
  26. }
  27. inline size_t encode_len(size_t input_len) { return (input_len + 2) / 3 * 4; }
  28. void encode_char_array(unsigned char *encode_block,
  29. const unsigned char *decode_block) {
  30. encode_block[0] = (decode_block[0] & 0xfc) >> 2;
  31. encode_block[1] =
  32. ((decode_block[0] & 0x03) << 4) + ((decode_block[1] & 0xf0) >> 4);
  33. encode_block[2] =
  34. ((decode_block[1] & 0x0f) << 2) + ((decode_block[2] & 0xc0) >> 6);
  35. encode_block[3] = decode_block[2] & 0x3f;
  36. }
  37. void decode_char_array(unsigned char *encode_block,
  38. unsigned char *decode_block) {
  39. for (int i = 0; i < 4; ++i) {
  40. encode_block[i] = base64_chars.find(encode_block[i]);
  41. }
  42. decode_block[0] = (encode_block[0] << 2) + ((encode_block[1] & 0x30) >> 4);
  43. decode_block[1] =
  44. ((encode_block[1] & 0xf) << 4) + ((encode_block[2] & 0x3c) >> 2);
  45. decode_block[2] = ((encode_block[2] & 0x3) << 6) + encode_block[3];
  46. }
  47. } // namespace
  48. string base64_encode(const string &input) {
  49. string output;
  50. size_t i = 0;
  51. unsigned char decode_block[3];
  52. unsigned char encode_block[4];
  53. for (string::size_type len = 0; len != input.size(); ++len) {
  54. decode_block[i++] = input[len];
  55. if (i == 3) {
  56. encode_char_array(encode_block, decode_block);
  57. for (i = 0; i < 4; ++i) {
  58. output += base64_chars[encode_block[i]];
  59. }
  60. i = 0;
  61. }
  62. }
  63. if (i > 0) {
  64. for (size_t j = i; j < 3; ++j) {
  65. decode_block[j] = '\0';
  66. }
  67. encode_char_array(encode_block, decode_block);
  68. for (size_t j = 0; j < i + 1; ++j) {
  69. output += base64_chars[encode_block[j]];
  70. }
  71. while (i++ < 3) {
  72. output += '=';
  73. }
  74. }
  75. return output;
  76. }
  77. string base64_decode(const string &encoded_string) {
  78. int in_len = encoded_string.size();
  79. int i = 0;
  80. int len = 0;
  81. unsigned char encode_block[4];
  82. unsigned char decode_block[3];
  83. string output;
  84. while (in_len-- && (encoded_string[len] != '=') &&
  85. is_base64(encoded_string[len])) {
  86. encode_block[i++] = encoded_string[len];
  87. len++;
  88. if (i == 4) {
  89. decode_char_array(encode_block, decode_block);
  90. for (int j = 0; j < 3; ++j) {
  91. output += decode_block[j];
  92. }
  93. i = 0;
  94. }
  95. }
  96. if (i > 0) {
  97. for (int j = i; j < 4; ++j) {
  98. encode_block[j] = 0;
  99. }
  100. decode_char_array(encode_block, decode_block);
  101. for (int j = 0; j < i - 1; ++j) {
  102. output += decode_block[j];
  103. }
  104. }
  105. return output;
  106. }
  107. } // namespace base64
  108. } // namespace base
  109. } // namespace baidu
  110. // #include <string>
  111. // #include <cassert>
  112. // #include <limits>
  113. // #include <stdexcept>
  114. // #include <ctype.h>
  115. // #include "base64_utils.h"
  116. // namespace util {
  117. // namespace crypto {
  118. // static const char b64_table[65] =
  119. // "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  120. // static const char reverse_table[128] = {
  121. // 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  122. // 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  123. // 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  124. // 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  125. // 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  126. // 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  127. // 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  128. // 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64
  129. // };
  130. // std::string Base64Utils::encode(const ::std::string& data) {
  131. // try {
  132. // if (data.size() > (std::numeric_limits<std::string::size_type>::max()
  133. // / 4u) * 3u) {
  134. // throw ::std::length_error("Converting too large a string to
  135. // base64.");
  136. // }
  137. // } catch (std::length_error& e) {
  138. // printf("%s\n", e.what());
  139. // return "";
  140. // }
  141. // const std::size_t binlen = data.size();
  142. // std::string retval((((binlen + 2) / 3) * 4), '=');
  143. // std::size_t outpos = 0;
  144. // int bits_collected = 0;
  145. // unsigned int accumulator = 0;
  146. // const std::string::const_iterator binend = data.end();
  147. // for (std::string::const_iterator i = data.begin(); i != binend; ++i) {
  148. // accumulator = (accumulator << 8) | (*i & 0xffu);
  149. // bits_collected += 8;
  150. // while (bits_collected >= 6) {
  151. // bits_collected -= 6;
  152. // retval[outpos++] = b64_table[(accumulator >> bits_collected) &
  153. // 0x3fu];
  154. // }
  155. // }
  156. // if (bits_collected > 0) { // Any trailing bits that are missing.
  157. // assert(bits_collected < 6);
  158. // accumulator <<= 6 - bits_collected;
  159. // retval[outpos++] = b64_table[accumulator & 0x3fu];
  160. // }
  161. // assert(outpos >= (retval.size() - 2));
  162. // assert(outpos <= retval.size());
  163. // return retval;
  164. // }
  165. // std::string Base64Utils::decode(const std::string& data) {
  166. // std::string retval;
  167. // const std::string::const_iterator last = data.end();
  168. // int bits_collected = 0;
  169. // unsigned int accumulator = 0;
  170. // try {
  171. // for (std::string::const_iterator i = data.begin(); i != last; ++i) {
  172. // const int c = *i;
  173. // if (isspace(c) || c == '=') {
  174. // continue;
  175. // }
  176. // if ((c > 127) || (c < 0) || (reverse_table[c] > 63)) {
  177. // throw ::std::invalid_argument("This contains characters not
  178. // legal in a base64 encoded string.");
  179. // }
  180. // accumulator = (accumulator << 6) | reverse_table[c];
  181. // bits_collected += 6;
  182. // if (bits_collected >= 8) {
  183. // bits_collected -= 8;
  184. // retval += static_cast<char>((accumulator >> bits_collected) &
  185. // 0xffu);
  186. // }
  187. // }
  188. // } catch (std::invalid_argument& e) {
  189. // printf("%s\n", e.what());
  190. // return "";
  191. // }
  192. // return retval;
  193. // }
  194. // }
  195. // }