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.

124 lines
4.8 KiB

4 months ago
  1. #ifndef _MACARON_BASE64_H_
  2. #define _MACARON_BASE64_H_
  3. /**
  4. * The MIT License (MIT)
  5. * Copyright (c) 2016 tomykaira
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include <string>
  27. namespace macaron {
  28. class Base64 {
  29. public:
  30. static std::string Encode(const std::string data) {
  31. static constexpr char sEncodingTable[] = {
  32. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  33. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  34. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  35. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  36. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  37. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  38. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  39. '4', '5', '6', '7', '8', '9', '+', '/'
  40. };
  41. size_t in_len = data.size();
  42. size_t out_len = 4 * ((in_len + 2) / 3);
  43. std::string ret(out_len, '\0');
  44. size_t i;
  45. char *p = const_cast<char*>(ret.c_str());
  46. for (i = 0; i < in_len - 2; i += 3) {
  47. *p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
  48. *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
  49. *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)];
  50. *p++ = sEncodingTable[data[i + 2] & 0x3F];
  51. }
  52. if (i < in_len) {
  53. *p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
  54. if (i == (in_len - 1)) {
  55. *p++ = sEncodingTable[((data[i] & 0x3) << 4)];
  56. *p++ = '=';
  57. }
  58. else {
  59. *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
  60. *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
  61. }
  62. *p++ = '=';
  63. }
  64. return ret;
  65. }
  66. static std::string Decode(const std::string& input, std::string& out) {
  67. static constexpr unsigned char kDecodingTable[] = {
  68. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  69. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  70. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  71. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  72. 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  73. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  74. 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  75. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
  76. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  77. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  78. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  79. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  80. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  81. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  82. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  83. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  84. };
  85. size_t in_len = input.size();
  86. if (in_len % 4 != 0) return "Input data size is not a multiple of 4";
  87. size_t out_len = in_len / 4 * 3;
  88. if (input[in_len - 1] == '=') out_len--;
  89. if (input[in_len - 2] == '=') out_len--;
  90. out.resize(out_len);
  91. for (size_t i = 0, j = 0; i < in_len;) {
  92. uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
  93. uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
  94. uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
  95. uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
  96. uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
  97. if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF;
  98. if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF;
  99. if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF;
  100. }
  101. return "";
  102. }
  103. };
  104. }
  105. #endif /* _MACARON_BASE64_H_ */