You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.6 KiB

  1. /**
  2. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. *
  16. * @author baidu aip
  17. */
  18. #ifndef __AIP_BASE64_H__
  19. #define __AIP_BASE64_H__
  20. #include <iostream>
  21. #include <string>
  22. namespace iflytop {
  23. static const std::string base64_chars =
  24. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  25. "abcdefghijklmnopqrstuvwxyz"
  26. "0123456789+/";
  27. static inline bool is_base64(const char c) { return (isalnum(c) || (c == '+') || (c == '/')); }
  28. static inline std::string base64_encode(const char* bytes_to_encode, unsigned int in_len) {
  29. std::string ret;
  30. int i = 0;
  31. int j = 0;
  32. unsigned char char_array_3[3];
  33. unsigned char char_array_4[4];
  34. while (in_len--) {
  35. char_array_3[i++] = *(bytes_to_encode++);
  36. if (i == 3) {
  37. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  38. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  39. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  40. char_array_4[3] = char_array_3[2] & 0x3f;
  41. for (i = 0; (i < 4); i++) {
  42. ret += base64_chars[char_array_4[i]];
  43. }
  44. i = 0;
  45. }
  46. }
  47. if (i) {
  48. for (j = i; j < 3; j++) {
  49. char_array_3[j] = '\0';
  50. }
  51. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  52. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  53. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  54. char_array_4[3] = char_array_3[2] & 0x3f;
  55. for (j = 0; (j < i + 1); j++) {
  56. ret += base64_chars[char_array_4[j]];
  57. }
  58. while ((i++ < 3)) {
  59. ret += '=';
  60. }
  61. }
  62. return ret;
  63. }
  64. static inline std::vector<uint8_t> base64_decode(std::string const& encoded_string) {
  65. int in_len = (int)encoded_string.size();
  66. int i = 0;
  67. int j = 0;
  68. int in_ = 0;
  69. unsigned char char_array_4[4], char_array_3[3];
  70. std::vector<uint8_t> ret;
  71. while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  72. char_array_4[i++] = encoded_string[in_];
  73. in_++;
  74. if (i == 4) {
  75. for (i = 0; i < 4; i++) char_array_4[i] = base64_chars.find(char_array_4[i]);
  76. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  77. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  78. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  79. for (i = 0; (i < 3); i++) {
  80. // ret += char_array_3[i];
  81. ret.push_back(char_array_3[i]);
  82. }
  83. i = 0;
  84. }
  85. }
  86. if (i) {
  87. for (j = i; j < 4; j++) char_array_4[j] = 0;
  88. for (j = 0; j < 4; j++) char_array_4[j] = base64_chars.find(char_array_4[j]);
  89. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  90. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  91. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  92. for (j = 0; (j < i - 1); j++) {
  93. // ret += char_array_3[j];
  94. ret.push_back(char_array_3[j]);
  95. }
  96. }
  97. return ret;
  98. }
  99. } // namespace iflytop
  100. #endif