diff options
author | Arda Cinar <kuzux92@gmail.com> | 2023-01-10 11:34:29 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-10 17:54:01 +0000 |
commit | 4ab2954210d7ff3a2275c4491c5cd12ed71b6d27 (patch) | |
tree | 055adaf357da2783061b00337310f46b7977cc8c /AK | |
parent | 6e93d89ee3108873a30bd0178b0f84fff3db3315 (diff) | |
download | serenity-4ab2954210d7ff3a2275c4491c5cd12ed71b6d27.zip |
AK: Expose Base64 tables from Base64.h
This change is necessary to move the forgiving base64 decoder to LibWeb
Diffstat (limited to 'AK')
-rw-r--r-- | AK/Base64.cpp | 35 | ||||
-rw-r--r-- | AK/Base64.h | 21 |
2 files changed, 29 insertions, 27 deletions
diff --git a/AK/Base64.cpp b/AK/Base64.cpp index 1944aa29f7..aa887b9eab 100644 --- a/AK/Base64.cpp +++ b/AK/Base64.cpp @@ -15,29 +15,6 @@ namespace AK { -static constexpr Array alphabet = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', - 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', - 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', - 'w', 'x', 'y', 'z', '0', '1', '2', '3', - '4', '5', '6', '7', '8', '9', '+', '/' -}; - -static consteval auto make_lookup_table() -{ - Array<i16, 256> table; - table.fill(-1); - for (size_t i = 0; i < alphabet.size(); ++i) { - table[alphabet[i]] = static_cast<i16>(i); - } - return table; -} - -static constexpr auto alphabet_lookup_table = make_lookup_table(); - size_t calculate_base64_decoded_length(StringView input) { return input.length() * 3 / 4; @@ -50,6 +27,8 @@ size_t calculate_base64_encoded_length(ReadonlyBytes input) ErrorOr<ByteBuffer> decode_base64(StringView input) { + auto alphabet_lookup_table = base64_lookup_table(); + auto get = [&](size_t& offset, bool* is_padding, bool& parsed_something) -> ErrorOr<u8> { while (offset < input.length() && is_ascii_space(input[offset])) ++offset; @@ -128,10 +107,10 @@ ErrorOr<String> encode_base64(ReadonlyBytes input) const u8 index2 = ((in1 << 2) | (in2 >> 6)) & 0x3f; const u8 index3 = in2 & 0x3f; - char const out0 = alphabet[index0]; - char const out1 = alphabet[index1]; - char const out2 = is_16bit ? '=' : alphabet[index2]; - char const out3 = is_8bit ? '=' : alphabet[index3]; + char const out0 = base64_alphabet[index0]; + char const out1 = base64_alphabet[index1]; + char const out2 = is_16bit ? '=' : base64_alphabet[index2]; + char const out3 = is_8bit ? '=' : base64_alphabet[index3]; TRY(output.try_append(out0)); TRY(output.try_append(out1)); @@ -202,6 +181,8 @@ ErrorOr<ByteBuffer> decode_forgiving_base64(StringView input) accumulated_bits = 0; }; + auto alphabet_lookup_table = base64_lookup_table(); + // 7. Let position be a position variable for data, initially pointing at the start of data. // 8. While position does not point past the end of data: for (auto point : data) { diff --git a/AK/Base64.h b/AK/Base64.h index 7dc870397a..5e2592a659 100644 --- a/AK/Base64.h +++ b/AK/Base64.h @@ -13,6 +13,27 @@ namespace AK { +constexpr Array base64_alphabet = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' +}; + +consteval auto base64_lookup_table() +{ + Array<i16, 256> table; + table.fill(-1); + for (size_t i = 0; i < base64_alphabet.size(); ++i) { + table[base64_alphabet[i]] = static_cast<i16>(i); + } + return table; +} + [[nodiscard]] size_t calculate_base64_decoded_length(StringView); [[nodiscard]] size_t calculate_base64_encoded_length(ReadonlyBytes); |