Fix a bug in the Base32 encoder that can cause improper encoding of the first byte (flutter/engine#28328)

This commit is contained in:
Jason Simmons
2021-08-26 13:31:01 -07:00
committed by GitHub
parent bfce29ca8e
commit 8e7f83d912
3 changed files with 8 additions and 2 deletions

View File

@@ -25,7 +25,7 @@ std::pair<bool, std::string> Base32Encode(std::string_view input) {
output.reserve(encoded_length);
Base32EncodeConverter converter;
converter.Append(input[0]);
converter.Append(static_cast<uint8_t>(input[0]));
size_t next_byte_index = 1;
while (converter.CanExtract()) {

View File

@@ -16,7 +16,7 @@ template <int from_length, int to_length, int buffer_length>
class BitConverter {
public:
void Append(int bits) {
FML_DCHECK(bits < (1 << from_length));
FML_DCHECK(bits >= 0 && bits < (1 << from_length));
FML_DCHECK(CanAppend());
lower_free_bits_ -= from_length;
buffer_ |= (bits << lower_free_bits_);

View File

@@ -38,6 +38,12 @@ TEST(Base32Test, CanEncode) {
ASSERT_TRUE(result.first);
ASSERT_EQ(result.second, "NBSWYTDP");
}
{
auto result = fml::Base32Encode("\xff\xfe\x7f\x80\x81");
ASSERT_TRUE(result.first);
ASSERT_EQ(result.second, "777H7AEB");
}
}
TEST(Base32Test, CanEncodeDecodeStrings) {