linux版本中间件

base64.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 use this file except in compliance with
  5. * the License. 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 distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. *
  13. * @author baidu aip
  14. */
  15. #pragma once
  16. #ifndef __AIP_BASE64_H__
  17. #define __AIP_BASE64_H__
  18. #include <iostream>
  19. #include <string>
  20. namespace aip {
  21. static const std::string base64_chars =
  22. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  23. "abcdefghijklmnopqrstuvwxyz"
  24. "0123456789+/";
  25. static inline bool is_base64(const char c)
  26. {
  27. return (isalnum(c) || (c == '+') || (c == '/'));
  28. }
  29. std::string base64_encode(const char * bytes_to_encode, unsigned int in_len)
  30. {
  31. std::string ret;
  32. int i = 0;
  33. int j = 0;
  34. unsigned char char_array_3[3];
  35. unsigned char char_array_4[4];
  36. while (in_len--)
  37. {
  38. char_array_3[i++] = *(bytes_to_encode++);
  39. if(i == 3)
  40. {
  41. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  42. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  43. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  44. char_array_4[3] = char_array_3[2] & 0x3f;
  45. for(i = 0; (i <4) ; i++)
  46. {
  47. ret += base64_chars[char_array_4[i]];
  48. }
  49. i = 0;
  50. }
  51. }
  52. if(i)
  53. {
  54. for(j = i; j < 3; j++)
  55. {
  56. char_array_3[j] = '\0';
  57. }
  58. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  59. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  60. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  61. char_array_4[3] = char_array_3[2] & 0x3f;
  62. for(j = 0; (j < i + 1); j++)
  63. {
  64. ret += base64_chars[char_array_4[j]];
  65. }
  66. while((i++ < 3))
  67. {
  68. ret += '=';
  69. }
  70. }
  71. return ret;
  72. }
  73. std::string base64_decode(std::string const & encoded_string)
  74. {
  75. int in_len = (int) encoded_string.size();
  76. int i = 0;
  77. int j = 0;
  78. int in_ = 0;
  79. unsigned char char_array_4[4], char_array_3[3];
  80. std::string ret;
  81. while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  82. char_array_4[i++] = encoded_string[in_]; in_++;
  83. if (i ==4) {
  84. for (i = 0; i <4; i++)
  85. char_array_4[i] = base64_chars.find(char_array_4[i]);
  86. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  87. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  88. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  89. for (i = 0; (i < 3); i++)
  90. ret += char_array_3[i];
  91. i = 0;
  92. }
  93. }
  94. if (i) {
  95. for (j = i; j <4; j++)
  96. char_array_4[j] = 0;
  97. for (j = 0; j <4; j++)
  98. char_array_4[j] = base64_chars.find(char_array_4[j]);
  99. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  100. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  101. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  102. for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  103. }
  104. return ret;
  105. }
  106. }
  107. #endif