Skip to content

Commit

Permalink
[C++] Fix wrong Base64 paddings (apache#11578)
Browse files Browse the repository at this point in the history
  • Loading branch information
BewareMyPower authored Aug 6, 2021
1 parent 09944d9 commit 898bb10
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions pulsar-client-cpp/lib/ProtobufNativeSchema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ SchemaInfo createProtobufNativeSchema(const google::protobuf::Descriptor* descri
std::string base64String{base64(bytes.data()), base64(bytes.data() + bytes.size())};
// Pulsar broker only supports decoding Base64 with padding so we need to add padding '=' here
const size_t numPadding = 4 - base64String.size() % 4;
for (size_t i = 0; i < numPadding; i++) {
base64String.push_back('=');
}
if (numPadding <= 2) {
for (size_t i = 0; i < numPadding; i++) {
base64String.push_back('=');
}
} else if (numPadding == 3) {
// The length of encoded Base64 string (without padding) should not be 4N+1
throw std::runtime_error("Unexpected padding number (3), the encoded Base64 string is:\n" +
base64String);
} // else numPadding == 4, which means no padding characters need to be added

const std::string schemaJson =
R"({"fileDescriptorSet":")" + base64String +
R"(","rootMessageTypeName":")" + rootMessageTypeName +
R"(","rootFileDescriptorName":")" + rootFileDescriptorName + R"("})";
const std::string schemaJson = R"({"fileDescriptorSet":")" + base64String +
R"(","rootMessageTypeName":")" + rootMessageTypeName +
R"(","rootFileDescriptorName":")" + rootFileDescriptorName + R"("})";

return SchemaInfo(SchemaType::PROTOBUF_NATIVE, "", schemaJson);
}
Expand Down

0 comments on commit 898bb10

Please sign in to comment.