Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ std::vector<unsigned char> DecodeBase64(const std::string &input) {

unsigned value = 0;
std::size_t cnt = 0;
bool previous_was_padding = false;
for (std::size_t i = 0; i < input.size(); i++) {
if (std::isspace(static_cast<unsigned char>(input[i]))) {
// skip newlines
Expand All @@ -87,14 +88,15 @@ std::vector<unsigned char> DecodeBase64(const std::string &input) {
value = (value << 6) | d;
if (cnt == 3) {
*out++ = value >> 16;
if (i > 0 && input[i - 1] != '=')
if (!previous_was_padding)
*out++ = value >> 8;
if (input[i] != '=')
*out++ = value;
cnt = 0;
} else {
++cnt;
}
previous_was_padding = input[i] == '=';
}
if (cnt != 0) {
// An invalid number of characters were encountered.
Expand Down
5 changes: 5 additions & 0 deletions test/binary_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ TEST(BinaryTest, DecodingTooShort) {
EXPECT_TRUE(result.empty());
}

TEST(BinaryTest, DecodingWhitespaceBetweenPadding) {
const std::vector<unsigned char> &result = YAML::DecodeBase64("TQ= =");
EXPECT_EQ(std::vector<unsigned char>{'M'}, result);
}

TEST(BinaryTest, EmptyBinary) {
YAML::Binary b;
EXPECT_TRUE(b.size() == 0);
Expand Down
Loading