From c388a879d73633b6ebe8353fd2a0407b908fe26f Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 20 Jan 2022 17:18:17 +0000 Subject: AK+Userland: Make AK::decode_base64 return ErrorOr --- AK/Base64.cpp | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'AK/Base64.cpp') diff --git a/AK/Base64.cpp b/AK/Base64.cpp index 11b190ec08..364425f468 100644 --- a/AK/Base64.cpp +++ b/AK/Base64.cpp @@ -48,31 +48,24 @@ size_t calculate_base64_encoded_length(ReadonlyBytes input) return ((4 * input.size() / 3) + 3) & ~3; } -Optional decode_base64(StringView input) +ErrorOr decode_base64(StringView input) { - auto get = [&](const size_t offset, bool* is_padding) -> Optional { + auto get = [&](const size_t offset, bool* is_padding) -> ErrorOr { constexpr auto table = make_lookup_table(); if (offset >= input.length()) return 0; if (input[offset] == '=') { if (!is_padding) - return {}; + return Error::from_string_literal("Invalid '=' character outside of padding in base64 data"); *is_padding = true; return 0; } i16 result = table[static_cast(input[offset])]; if (result < 0) - return {}; + return Error::from_string_literal("Invalid character in base64 data"); VERIFY(result < 256); return { result }; }; -#define TRY_GET(index, is_padding) \ - ({ \ - auto _temporary_result = get(index, is_padding); \ - if (!_temporary_result.has_value()) \ - return {}; \ - _temporary_result.value(); \ - }) Vector output; output.ensure_capacity(calculate_base64_decoded_length(input)); @@ -81,10 +74,10 @@ Optional decode_base64(StringView input) bool in2_is_padding = false; bool in3_is_padding = false; - const u8 in0 = TRY_GET(i, nullptr); - const u8 in1 = TRY_GET(i + 1, nullptr); - const u8 in2 = TRY_GET(i + 2, &in2_is_padding); - const u8 in3 = TRY_GET(i + 3, &in3_is_padding); + const u8 in0 = TRY(get(i, nullptr)); + const u8 in1 = TRY(get(i + 1, nullptr)); + const u8 in2 = TRY(get(i + 2, &in2_is_padding)); + const u8 in3 = TRY(get(i + 3, &in3_is_padding)); const u8 out0 = (in0 << 2) | ((in1 >> 4) & 3); const u8 out1 = ((in1 & 0xf) << 4) | ((in2 >> 2) & 0xf); @@ -97,7 +90,7 @@ Optional decode_base64(StringView input) output.append(out2); } - return ByteBuffer::copy(output).release_value_but_fixme_should_propagate_errors(); + return ByteBuffer::copy(output); } String encode_base64(ReadonlyBytes input) -- cgit v1.2.3