Skip to content

Commit

Permalink
AK: Add a simple and inefficient Base64 encoder
Browse files Browse the repository at this point in the history
The test cases are taken from RFC 4648.
  • Loading branch information
tomleb authored and awesomekling committed Jun 18, 2020
1 parent dd53e07 commit 79529ff
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
40 changes: 40 additions & 0 deletions AK/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
3 changes: 3 additions & 0 deletions AK/Base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 49 additions & 0 deletions AK/Tests/TestBase64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2020, Tom Lebreux <[email protected]>
* 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)

0 comments on commit 79529ff

Please sign in to comment.