diff --git a/src/binary.cpp b/src/binary.cpp index 6c7e687d7..aed36b692 100644 --- a/src/binary.cpp +++ b/src/binary.cpp @@ -75,6 +75,7 @@ std::vector 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(input[i]))) { // skip newlines @@ -87,7 +88,7 @@ std::vector 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; @@ -95,6 +96,7 @@ std::vector DecodeBase64(const std::string &input) { } else { ++cnt; } + previous_was_padding = input[i] == '='; } if (cnt != 0) { // An invalid number of characters were encountered. diff --git a/test/binary_test.cpp b/test/binary_test.cpp index 30e0e46b0..93c999bb0 100644 --- a/test/binary_test.cpp +++ b/test/binary_test.cpp @@ -19,6 +19,11 @@ TEST(BinaryTest, DecodingTooShort) { EXPECT_TRUE(result.empty()); } +TEST(BinaryTest, DecodingWhitespaceBetweenPadding) { + const std::vector &result = YAML::DecodeBase64("TQ= ="); + EXPECT_EQ(std::vector{'M'}, result); +} + TEST(BinaryTest, EmptyBinary) { YAML::Binary b; EXPECT_TRUE(b.size() == 0);