From 4ecf4c7617f805ba6fe99d660dbf5da7deed21ef Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 24 Mar 2024 08:55:53 -0400 Subject: [PATCH] AK: Compute the exact size of decoded Base64 strings --- AK/Base64.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/AK/Base64.cpp b/AK/Base64.cpp index 112c02e603c..058fd808a69 100644 --- a/AK/Base64.cpp +++ b/AK/Base64.cpp @@ -15,7 +15,14 @@ namespace AK { size_t calculate_base64_decoded_length(StringView input) { - return input.length() * 3 / 4; + auto length = input.length() * 3 / 4; + + if (input.ends_with("="sv)) + --length; + if (input.ends_with("=="sv)) + --length; + + return length; } size_t calculate_base64_encoded_length(ReadonlyBytes input) @@ -70,9 +77,6 @@ static ErrorOr decode_base64_impl(StringView input, ReadonlySpan