base64.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_view>
  27. namespace macaron {
  28. class Base64 {
  29. public:
  30. static size_t Encode(std::string_view data, char *p) {
  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. if (p == nullptr) return out_len;
  44. size_t i;
  45. for (i = 0; i < in_len - 2; i += 3) {
  46. *p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
  47. *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
  48. *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)];
  49. *p++ = sEncodingTable[data[i + 2] & 0x3F];
  50. }
  51. if (i < in_len) {
  52. *p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
  53. if (i == (in_len - 1)) {
  54. *p++ = sEncodingTable[((data[i] & 0x3) << 4)];
  55. *p++ = '=';
  56. } else {
  57. *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
  58. *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
  59. }
  60. *p++ = '=';
  61. }
  62. return out_len;
  63. }
  64. static size_t Decode(std::string_view input, char *out) {
  65. static constexpr unsigned char kDecodingTable[] = {
  66. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  67. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  68. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  69. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  70. 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  71. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  72. 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  73. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
  74. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  75. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 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. };
  83. size_t in_len = input.size();
  84. if (in_len % 4 != 0) return 0;
  85. size_t out_len = in_len / 4 * 3;
  86. if (input[in_len - 1] == '=') out_len--;
  87. if (input[in_len - 2] == '=') out_len--;
  88. if (out == nullptr) return out_len;
  89. for (size_t i = 0, j = 0; i < in_len;) {
  90. uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
  91. uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
  92. uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
  93. uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
  94. uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
  95. if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF;
  96. if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF;
  97. if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF;
  98. }
  99. return out_len;
  100. }
  101. };
  102. }
  103. #endif /* _MACARON_BASE64_H_ */