mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
AK: Add a simple and inefficient Base64 encoder
The test cases are taken from RFC 4648.
This commit is contained in:
parent
dd53e070c5
commit
79529ffd47
Notes:
sideshowbarker
2024-07-19 05:33:11 +09:00
Author: https://github.com/tomleb 🔰 Commit: https://github.com/SerenityOS/serenity/commit/79529ffd471 Pull-request: https://github.com/SerenityOS/serenity/pull/2552 Reviewed-by: https://github.com/awesomekling
3 changed files with 92 additions and 0 deletions
|
@ -84,4 +84,44 @@ ByteBuffer decode_base64(const StringView& input)
|
|||
return ByteBuffer::copy(output.data(), output.size());
|
||||
}
|
||||
|
||||
ByteBuffer encode_base64(const StringView& input)
|
||||
{
|
||||
Vector<u8> 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,6 +32,9 @@ namespace AK {
|
|||
|
||||
ByteBuffer decode_base64(const StringView&);
|
||||
|
||||
ByteBuffer encode_base64(const StringView&);
|
||||
|
||||
}
|
||||
|
||||
using AK::decode_base64;
|
||||
using AK::encode_base64;
|
||||
|
|
49
AK/Tests/TestBase64.cpp
Normal file
49
AK/Tests/TestBase64.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Tom Lebreux <tomlebreux@hotmail.com>
|
||||
* 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 <AK/TestSuite.h>
|
||||
|
||||
#include <AK/Base64.h>
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/String.h>
|
||||
|
||||
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)
|
Loading…
Reference in a new issue