Commit c77bde68 by Abseil Team Committed by Copybara-Service

Add CalculateBase64EscapeLen variations for the 3 base64 escaping methods…

Add CalculateBase64EscapeLen variations for the 3 base64 escaping methods (Base64Escape, WebSafeBase64Escape, WebSafeBase64EscapeWithPadding).

Also update CalculateBase64EscapedLen() documentation (it references outdated Base64Escape API), and reference it from WebSafeBase64Escape (to match Base64Escape).

PiperOrigin-RevId: 511647760
Change-Id: I7dee18645c2a779c0762bc71da75a4684ec2493f
parent 402e1319
...@@ -121,7 +121,7 @@ std::string Utf8SafeCHexEscape(absl::string_view src); ...@@ -121,7 +121,7 @@ std::string Utf8SafeCHexEscape(absl::string_view src);
// //
// Encodes a `src` string into a base64-encoded 'dest' string with padding // Encodes a `src` string into a base64-encoded 'dest' string with padding
// characters. This function conforms with RFC 4648 section 4 (base64) and RFC // characters. This function conforms with RFC 4648 section 4 (base64) and RFC
// 2045. See also CalculateBase64EscapedLen(). // 2045.
void Base64Escape(absl::string_view src, std::string* dest); void Base64Escape(absl::string_view src, std::string* dest);
std::string Base64Escape(absl::string_view src); std::string Base64Escape(absl::string_view src);
......
...@@ -562,6 +562,7 @@ template <typename StringType> ...@@ -562,6 +562,7 @@ template <typename StringType>
void TestEscapeAndUnescape() { void TestEscapeAndUnescape() {
// Check the short strings; this tests the math (and boundaries) // Check the short strings; this tests the math (and boundaries)
for (const auto& tc : base64_tests) { for (const auto& tc : base64_tests) {
// Test plain base64.
StringType encoded("this junk should be ignored"); StringType encoded("this junk should be ignored");
absl::Base64Escape(tc.plaintext, &encoded); absl::Base64Escape(tc.plaintext, &encoded);
EXPECT_EQ(encoded, tc.cyphertext); EXPECT_EQ(encoded, tc.cyphertext);
...@@ -571,22 +572,26 @@ void TestEscapeAndUnescape() { ...@@ -571,22 +572,26 @@ void TestEscapeAndUnescape() {
EXPECT_TRUE(absl::Base64Unescape(encoded, &decoded)); EXPECT_TRUE(absl::Base64Unescape(encoded, &decoded));
EXPECT_EQ(decoded, tc.plaintext); EXPECT_EQ(decoded, tc.plaintext);
StringType websafe(tc.cyphertext); StringType websafe_with_padding(tc.cyphertext);
for (int c = 0; c < websafe.size(); ++c) { for (unsigned int c = 0; c < websafe_with_padding.size(); ++c) {
if ('+' == websafe[c]) websafe[c] = '-'; if ('+' == websafe_with_padding[c]) websafe_with_padding[c] = '-';
if ('/' == websafe[c]) websafe[c] = '_'; if ('/' == websafe_with_padding[c]) websafe_with_padding[c] = '_';
// Intentionally keeping padding aka '='.
}
// Test plain websafe (aka without padding).
StringType websafe(websafe_with_padding);
for (unsigned int c = 0; c < websafe.size(); ++c) {
if ('=' == websafe[c]) { if ('=' == websafe[c]) {
websafe.resize(c); websafe.resize(c);
break; break;
} }
} }
encoded = "this junk should be ignored"; encoded = "this junk should be ignored";
absl::WebSafeBase64Escape(tc.plaintext, &encoded); absl::WebSafeBase64Escape(tc.plaintext, &encoded);
EXPECT_EQ(encoded, websafe); EXPECT_EQ(encoded, websafe);
EXPECT_EQ(absl::WebSafeBase64Escape(tc.plaintext), websafe); EXPECT_EQ(absl::WebSafeBase64Escape(tc.plaintext), websafe);
// Let's try the string version of the decoder
decoded = "this junk should be ignored"; decoded = "this junk should be ignored";
EXPECT_TRUE(absl::WebSafeBase64Unescape(websafe, &decoded)); EXPECT_TRUE(absl::WebSafeBase64Unescape(websafe, &decoded));
EXPECT_EQ(decoded, tc.plaintext); EXPECT_EQ(decoded, tc.plaintext);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment