From 79529ffd471e993c0448aadbe84ef42e435d6fd3 Mon Sep 17 00:00:00 2001 From: Tom Lebreux Date: Fri, 12 Jun 2020 22:09:31 -0400 Subject: [PATCH] AK: Add a simple and inefficient Base64 encoder The test cases are taken from RFC 4648. --- AK/Base64.cpp | 40 +++++++++++++++++++++++++++++++++ AK/Base64.h | 3 +++ AK/Tests/TestBase64.cpp | 49 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 AK/Tests/TestBase64.cpp diff --git a/AK/Base64.cpp b/AK/Base64.cpp index 72016f6dfa..4d238a578d 100644 --- a/AK/Base64.cpp +++ b/AK/Base64.cpp @@ -84,4 +84,44 @@ ByteBuffer decode_base64(const StringView& input) return ByteBuffer::copy(output.data(), output.size()); } +ByteBuffer encode_base64(const StringView& input) +{ + Vector output; + + auto get = [&](size_t offset, bool* need_padding = nullptr) -> u8 { + if (offset >= input.length()) { + if (need_padding) + *need_padding = true; + return 0; + } + return (u8)input[offset]; + }; + + for (size_t i = 0; i < input.length(); i += 3) { + bool is_8bit = false; + bool is_16bit = false; + + u8 in0 = get(i); + u8 in1 = get(i + 1, &is_16bit); + u8 in2 = get(i + 2, &is_8bit); + + u8 index0 = (in0 >> 2) & 0x3f; + u8 index1 = ((in0 << 4) | (in1 >> 4)) & 0x3f; + u8 index2 = ((in1 << 2) | (in2 >> 6)) & 0x3f; + u8 index3 = in2 & 0x3f; + + u8 out0 = s_alphabet[index0]; + u8 out1 = s_alphabet[index1]; + u8 out2 = is_16bit ? '=' : s_alphabet[index2]; + u8 out3 = is_8bit ? '=' : s_alphabet[index3]; + + output.append(out0); + output.append(out1); + output.append(out2); + output.append(out3); + } + + return ByteBuffer::copy(output.data(), output.size()); +} + } diff --git a/AK/Base64.h b/AK/Base64.h index 47266b684d..d61fcbce7b 100644 --- a/AK/Base64.h +++ b/AK/Base64.h @@ -32,6 +32,9 @@ namespace AK { ByteBuffer decode_base64(const StringView&); +ByteBuffer encode_base64(const StringView&); + } using AK::decode_base64; +using AK::encode_base64; diff --git a/AK/Tests/TestBase64.cpp b/AK/Tests/TestBase64.cpp new file mode 100644 index 0000000000..40b7e494a6 --- /dev/null +++ b/AK/Tests/TestBase64.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020, Tom Lebreux + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +TEST_CASE(test_encode) +{ + auto encode_equal = [&](const char* input, const char* expected) { + auto encoded = encode_base64(StringView(input)); + EXPECT(String::copy(encoded) == String(expected)); + }; + + encode_equal("", ""); + encode_equal("f", "Zg=="); + encode_equal("fo", "Zm8="); + encode_equal("foo", "Zm9v"); + encode_equal("foob", "Zm9vYg=="); + encode_equal("fooba", "Zm9vYmE="); + encode_equal("foobar", "Zm9vYmFy"); +} + +TEST_MAIN(Base64)