Document Base64Unescape() padding

PiperOrigin-RevId: 482905442
Change-Id: Ic3a1e62f8a17efee5508d94835635aa489940f2d
pull/1302/head
Abseil Team 2 years ago committed by Copybara-Service
parent 88eee8b511
commit cb436cf014
  1. 4
      absl/strings/escaping.h
  2. 42
      absl/strings/escaping_test.cc

@ -122,6 +122,8 @@ std::string Utf8SafeCHexEscape(absl::string_view src);
// Converts a `src` string encoded in Base64 to its binary equivalent, writing
// it to a `dest` buffer, returning `true` on success. If `src` contains invalid
// characters, `dest` is cleared and returns `false`.
// Padding is optional. If padding is included, it must be correct. In the
// padding, '=' and '.' are treated identically.
bool Base64Unescape(absl::string_view src, std::string* dest);
// WebSafeBase64Unescape()
@ -129,6 +131,8 @@ bool Base64Unescape(absl::string_view src, std::string* dest);
// Converts a `src` string encoded in Base64 to its binary equivalent, writing
// it to a `dest` buffer, but using '-' instead of '+', and '_' instead of '/'.
// If `src` contains invalid characters, `dest` is cleared and returns `false`.
// Padding is optional. If padding is included, it must be correct. In the
// padding, '=' and '.' are treated identically.
bool WebSafeBase64Unescape(absl::string_view src, std::string* dest);
// Base64Escape()

@ -617,6 +617,48 @@ TEST(Base64, EscapeAndUnescape) {
TestEscapeAndUnescape<std::string>();
}
TEST(Base64, Padding) {
// Padding is optional.
// '.' is an acceptable padding character, just like '='.
std::initializer_list<absl::string_view> good_padding = {
"YQ",
"YQ==",
"YQ=.",
"YQ.=",
"YQ..",
};
for (absl::string_view b64 : good_padding) {
std::string decoded;
EXPECT_TRUE(absl::Base64Unescape(b64, &decoded));
EXPECT_EQ(decoded, "a");
std::string websafe_decoded;
EXPECT_TRUE(absl::WebSafeBase64Unescape(b64, &websafe_decoded));
EXPECT_EQ(websafe_decoded, "a");
}
std::initializer_list<absl::string_view> bad_padding = {
"YQ=",
"YQ.",
"YQ===",
"YQ==.",
"YQ=.=",
"YQ=..",
"YQ.==",
"YQ.=.",
"YQ..=",
"YQ...",
"YQ====",
"YQ....",
"YQ=====",
"YQ.....",
};
for (absl::string_view b64 : bad_padding) {
std::string decoded;
EXPECT_FALSE(absl::Base64Unescape(b64, &decoded));
std::string websafe_decoded;
EXPECT_FALSE(absl::WebSafeBase64Unescape(b64, &websafe_decoded));
}
}
TEST(Base64, DISABLED_HugeData) {
const size_t kSize = size_t(3) * 1000 * 1000 * 1000;
static_assert(kSize % 3 == 0, "kSize must be divisible by 3");

Loading…
Cancel
Save