2020-02-09 01:12:59 +08:00
|
|
|
// Copyright (C) 2017-2020 Thomas Fussell
|
2017-04-12 05:49:07 +08:00
|
|
|
// Copyright (C) 2013 Tomas Kislan
|
|
|
|
// Copyright (C) 2013 Adam Rudd
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
2017-04-12 05:55:52 +08:00
|
|
|
// THE SOFTWARE.
|
2017-04-12 05:49:07 +08:00
|
|
|
|
2017-04-21 02:03:03 +08:00
|
|
|
#include <detail/cryptography/base64.hpp>
|
2019-12-27 01:03:12 +08:00
|
|
|
#include <array>
|
2017-04-12 05:49:07 +08:00
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
std::string encode_base64(const std::vector<std::uint8_t> &input)
|
|
|
|
{
|
|
|
|
auto encoded_length = (input.size() + 2 - ((input.size() + 2) % 3)) / 3 * 4;
|
|
|
|
auto output = std::string(encoded_length, '\0');
|
|
|
|
auto input_iterator = input.begin();
|
|
|
|
auto output_iterator = output.begin();
|
2017-09-13 20:48:22 +08:00
|
|
|
auto i = std::size_t(0);
|
|
|
|
auto j = std::size_t(0);
|
|
|
|
std::array<std::uint8_t, 3> a3{{0}};
|
|
|
|
std::array<std::uint8_t, 4> a4{{0}};
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
const std::string alphabet(
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
2017-09-13 20:48:22 +08:00
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"0123456789+/");
|
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
while (input_iterator != input.end())
|
|
|
|
{
|
|
|
|
a3[i++] = *input_iterator++;
|
|
|
|
|
|
|
|
if (i == 3)
|
|
|
|
{
|
|
|
|
a4[0] = (a3[0] & 0xfc) >> 2;
|
2017-09-13 20:48:22 +08:00
|
|
|
a4[1] = static_cast<std::uint8_t>(((a3[0] & 0x03) << 4)
|
|
|
|
+ ((a3[1] & 0xf0) >> 4));
|
|
|
|
a4[2] = static_cast<std::uint8_t>(((a3[1] & 0x0f) << 2)
|
|
|
|
+ ((a3[2] & 0xc0) >> 6));
|
2017-04-12 05:49:07 +08:00
|
|
|
a4[3] = (a3[2] & 0x3f);
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
2017-09-13 20:48:22 +08:00
|
|
|
*output_iterator++ = alphabet[a4[i]];
|
2017-04-12 05:49:07 +08:00
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
|
|
|
if (i != 0)
|
2017-04-12 05:49:07 +08:00
|
|
|
{
|
|
|
|
for (j = i; j < 3; j++)
|
|
|
|
{
|
2017-09-13 20:48:22 +08:00
|
|
|
a3[j] = 0;
|
2017-04-12 05:49:07 +08:00
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
a4[0] = (a3[0] & 0xfc) >> 2;
|
2017-09-13 20:48:22 +08:00
|
|
|
a4[1] = static_cast<std::uint8_t>(((a3[0] & 0x03) << 4)
|
|
|
|
+ ((a3[1] & 0xf0) >> 4));
|
|
|
|
a4[2] = static_cast<std::uint8_t>(((a3[1] & 0x0f) << 2)
|
|
|
|
+ ((a3[2] & 0xc0) >> 6));
|
2017-04-12 05:49:07 +08:00
|
|
|
a4[3] = (a3[2] & 0x3f);
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
for (j = 0; j < i + 1; j++)
|
|
|
|
{
|
2017-09-13 20:48:22 +08:00
|
|
|
*output_iterator++ = alphabet[a4[j]];
|
2017-04-12 05:49:07 +08:00
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
|
|
|
while (i++ < 3)
|
2017-04-12 05:49:07 +08:00
|
|
|
{
|
|
|
|
*output_iterator++ = '=';
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::uint8_t> decode_base64(const std::string &input)
|
|
|
|
{
|
2017-09-13 20:48:22 +08:00
|
|
|
std::size_t padding_count = 0;
|
2017-04-12 05:49:07 +08:00
|
|
|
auto in_end = input.data() + input.size();
|
2017-09-13 20:48:22 +08:00
|
|
|
|
|
|
|
while (*--in_end == '=')
|
|
|
|
{
|
|
|
|
++padding_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto decoded_length = ((6 * input.size()) / 8) - padding_count;
|
2017-04-12 05:49:07 +08:00
|
|
|
auto output = std::vector<std::uint8_t>(decoded_length);
|
|
|
|
auto input_iterator = input.begin();
|
|
|
|
auto output_iterator = output.begin();
|
2017-09-13 20:48:22 +08:00
|
|
|
auto i = std::size_t(0);
|
|
|
|
auto j = std::size_t(0);
|
|
|
|
std::array<std::uint8_t, 3> a3{{0}};
|
|
|
|
std::array<std::uint8_t, 4> a4{{0}};
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
auto b64_lookup = [](std::uint8_t c) -> std::uint8_t {
|
|
|
|
if (c >= 'A' && c <= 'Z') return c - 'A';
|
|
|
|
if (c >= 'a' && c <= 'z') return c - 71;
|
|
|
|
if (c >= '0' && c <= '9') return c + 4;
|
|
|
|
if (c == '+') return 62;
|
|
|
|
if (c == '/') return 63;
|
2017-04-12 05:49:07 +08:00
|
|
|
return 255;
|
|
|
|
};
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
while (input_iterator != input.end())
|
|
|
|
{
|
|
|
|
if (*input_iterator == '=')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
a4[i++] = static_cast<std::uint8_t>(*(input_iterator++));
|
|
|
|
|
|
|
|
if (i == 4)
|
|
|
|
{
|
2017-09-13 20:48:22 +08:00
|
|
|
for (i = 0; i < 4; i++)
|
2017-04-12 05:49:07 +08:00
|
|
|
{
|
|
|
|
a4[i] = b64_lookup(a4[i]);
|
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
|
|
|
a3[0] = static_cast<std::uint8_t>(a4[0] << 2)
|
|
|
|
+ static_cast<std::uint8_t>((a4[1] & 0x30) >> 4);
|
|
|
|
a3[1] = static_cast<std::uint8_t>((a4[1] & 0xf) << 4)
|
|
|
|
+ static_cast<std::uint8_t>((a4[2] & 0x3c) >> 2);
|
|
|
|
a3[2] = static_cast<std::uint8_t>((a4[2] & 0x3) << 6)
|
|
|
|
+ static_cast<std::uint8_t>(a4[3]);
|
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
*output_iterator++ = a3[i];
|
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
|
|
|
if (i != 0)
|
2017-04-12 05:49:07 +08:00
|
|
|
{
|
|
|
|
for (j = i; j < 4; j++)
|
|
|
|
{
|
2017-09-13 20:48:22 +08:00
|
|
|
a4[j] = 0;
|
2017-04-12 05:49:07 +08:00
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
for (j = 0; j < 4; j++)
|
|
|
|
{
|
|
|
|
a4[j] = b64_lookup(a4[j]);
|
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
|
|
|
a3[0] = static_cast<std::uint8_t>(a4[0] << 2)
|
|
|
|
+ static_cast<std::uint8_t>((a4[1] & 0x30) >> 4);
|
|
|
|
a3[1] = static_cast<std::uint8_t>((a4[1] & 0xf) << 4)
|
|
|
|
+ static_cast<std::uint8_t>((a4[2] & 0x3c) >> 2);
|
|
|
|
a3[2] = static_cast<std::uint8_t>((a4[2] & 0x3) << 6)
|
|
|
|
+ static_cast<std::uint8_t>(a4[3]);
|
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
for (j = 0; j < i - 1; j++)
|
|
|
|
{
|
|
|
|
*output_iterator++ = a3[j];
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2017-04-12 05:49:07 +08:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace xlnt
|