base64.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (C) 2017-2021 Thomas Fussell
  2. // Copyright (C) 2013 Tomas Kislan
  3. // Copyright (C) 2013 Adam Rudd
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #include <detail/cryptography/base64.hpp>
  23. #include <array>
  24. namespace xlnt {
  25. namespace detail {
  26. std::string encode_base64(const std::vector<std::uint8_t> &input)
  27. {
  28. auto encoded_length = (input.size() + 2 - ((input.size() + 2) % 3)) / 3 * 4;
  29. auto output = std::string(encoded_length, '\0');
  30. auto input_iterator = input.begin();
  31. auto output_iterator = output.begin();
  32. auto i = std::size_t(0);
  33. auto j = std::size_t(0);
  34. std::array<std::uint8_t, 3> a3{{0}};
  35. std::array<std::uint8_t, 4> a4{{0}};
  36. const std::string alphabet(
  37. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  38. "abcdefghijklmnopqrstuvwxyz"
  39. "0123456789+/");
  40. while (input_iterator != input.end())
  41. {
  42. a3[i++] = *input_iterator++;
  43. if (i == 3)
  44. {
  45. a4[0] = (a3[0] & 0xfc) >> 2;
  46. a4[1] = static_cast<std::uint8_t>(((a3[0] & 0x03) << 4)
  47. + ((a3[1] & 0xf0) >> 4));
  48. a4[2] = static_cast<std::uint8_t>(((a3[1] & 0x0f) << 2)
  49. + ((a3[2] & 0xc0) >> 6));
  50. a4[3] = (a3[2] & 0x3f);
  51. for (i = 0; i < 4; i++)
  52. {
  53. *output_iterator++ = alphabet[a4[i]];
  54. }
  55. i = 0;
  56. }
  57. }
  58. if (i != 0)
  59. {
  60. for (j = i; j < 3; j++)
  61. {
  62. a3[j] = 0;
  63. }
  64. a4[0] = (a3[0] & 0xfc) >> 2;
  65. a4[1] = static_cast<std::uint8_t>(((a3[0] & 0x03) << 4)
  66. + ((a3[1] & 0xf0) >> 4));
  67. a4[2] = static_cast<std::uint8_t>(((a3[1] & 0x0f) << 2)
  68. + ((a3[2] & 0xc0) >> 6));
  69. a4[3] = (a3[2] & 0x3f);
  70. for (j = 0; j < i + 1; j++)
  71. {
  72. *output_iterator++ = alphabet[a4[j]];
  73. }
  74. while (i++ < 3)
  75. {
  76. *output_iterator++ = '=';
  77. }
  78. }
  79. return output;
  80. }
  81. std::vector<std::uint8_t> decode_base64(const std::string &input)
  82. {
  83. std::size_t padding_count = 0;
  84. auto in_end = input.data() + input.size();
  85. while (*--in_end == '=')
  86. {
  87. ++padding_count;
  88. }
  89. auto decoded_length = ((6 * input.size()) / 8) - padding_count;
  90. auto output = std::vector<std::uint8_t>(decoded_length);
  91. auto input_iterator = input.begin();
  92. auto output_iterator = output.begin();
  93. auto i = std::size_t(0);
  94. auto j = std::size_t(0);
  95. std::array<std::uint8_t, 3> a3{{0}};
  96. std::array<std::uint8_t, 4> a4{{0}};
  97. auto b64_lookup = [](std::uint8_t c) -> std::uint8_t {
  98. if (c >= 'A' && c <= 'Z') return c - 'A';
  99. if (c >= 'a' && c <= 'z') return c - 71;
  100. if (c >= '0' && c <= '9') return c + 4;
  101. if (c == '+') return 62;
  102. if (c == '/') return 63;
  103. return 255;
  104. };
  105. while (input_iterator != input.end())
  106. {
  107. if (*input_iterator == '=')
  108. {
  109. break;
  110. }
  111. a4[i++] = static_cast<std::uint8_t>(*(input_iterator++));
  112. if (i == 4)
  113. {
  114. for (i = 0; i < 4; i++)
  115. {
  116. a4[i] = b64_lookup(a4[i]);
  117. }
  118. a3[0] = static_cast<std::uint8_t>(a4[0] << 2)
  119. + static_cast<std::uint8_t>((a4[1] & 0x30) >> 4);
  120. a3[1] = static_cast<std::uint8_t>((a4[1] & 0xf) << 4)
  121. + static_cast<std::uint8_t>((a4[2] & 0x3c) >> 2);
  122. a3[2] = static_cast<std::uint8_t>((a4[2] & 0x3) << 6)
  123. + static_cast<std::uint8_t>(a4[3]);
  124. for (i = 0; i < 3; i++)
  125. {
  126. *output_iterator++ = a3[i];
  127. }
  128. i = 0;
  129. }
  130. }
  131. if (i != 0)
  132. {
  133. for (j = i; j < 4; j++)
  134. {
  135. a4[j] = 0;
  136. }
  137. for (j = 0; j < 4; j++)
  138. {
  139. a4[j] = b64_lookup(a4[j]);
  140. }
  141. a3[0] = static_cast<std::uint8_t>(a4[0] << 2)
  142. + static_cast<std::uint8_t>((a4[1] & 0x30) >> 4);
  143. a3[1] = static_cast<std::uint8_t>((a4[1] & 0xf) << 4)
  144. + static_cast<std::uint8_t>((a4[2] & 0x3c) >> 2);
  145. a3[2] = static_cast<std::uint8_t>((a4[2] & 0x3) << 6)
  146. + static_cast<std::uint8_t>(a4[3]);
  147. for (j = 0; j < i - 1; j++)
  148. {
  149. *output_iterator++ = a3[j];
  150. }
  151. }
  152. return output;
  153. }
  154. } // namespace detail
  155. } // namespace xlnt