AK: Make encode_base64 take a ByteBuffer and return a String

That makes the interface symmetric with decode_base64 and it's
what all current callers want (except for one, which is buggy).
This commit is contained in:
Nico Weber 2020-07-22 11:39:07 -04:00 committed by Andreas Kling
parent 9e32ad6c99
commit 5ba8aba197
5 changed files with 15 additions and 13 deletions

View file

@ -25,6 +25,8 @@
*/ */
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/Vector.h> #include <AK/Vector.h>
@ -84,20 +86,20 @@ ByteBuffer decode_base64(const StringView& input)
return ByteBuffer::copy(output.data(), output.size()); return ByteBuffer::copy(output.data(), output.size());
} }
ByteBuffer encode_base64(const StringView& input) String encode_base64(const ByteBuffer& input)
{ {
Vector<u8> output; StringBuilder output;
auto get = [&](size_t offset, bool* need_padding = nullptr) -> u8 { auto get = [&](size_t offset, bool* need_padding = nullptr) -> u8 {
if (offset >= input.length()) { if (offset >= input.size()) {
if (need_padding) if (need_padding)
*need_padding = true; *need_padding = true;
return 0; return 0;
} }
return (u8)input[offset]; return input[offset];
}; };
for (size_t i = 0; i < input.length(); i += 3) { for (size_t i = 0; i < input.size(); i += 3) {
bool is_8bit = false; bool is_8bit = false;
bool is_16bit = false; bool is_16bit = false;
@ -121,7 +123,7 @@ ByteBuffer encode_base64(const StringView& input)
output.append(out3); output.append(out3);
} }
return ByteBuffer::copy(output.data(), output.size()); return output.to_string();
} }
} }

View file

@ -32,7 +32,7 @@ namespace AK {
ByteBuffer decode_base64(const StringView&); ByteBuffer decode_base64(const StringView&);
ByteBuffer encode_base64(const StringView&); String encode_base64(const ByteBuffer&);
} }

View file

@ -49,8 +49,8 @@ TEST_CASE(test_decode)
TEST_CASE(test_encode) TEST_CASE(test_encode)
{ {
auto encode_equal = [&](const char* input, const char* expected) { auto encode_equal = [&](const char* input, const char* expected) {
auto encoded = encode_base64(StringView(input)); auto encoded = encode_base64(ByteBuffer::wrap(input, strlen(input)));
EXPECT(String::copy(encoded) == String(expected)); EXPECT(encoded == String(expected));
}; };
encode_equal("", ""); encode_equal("", "");

View file

@ -267,8 +267,8 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
// FIXME: This should convert string to a non-UTF-8 byte string first. // FIXME: This should convert string to a non-UTF-8 byte string first.
auto encoded = encode_base64(StringView(string)); auto encoded = encode_base64(ByteBuffer::wrap(string.characters(), string.length()));
return JS::js_string(interpreter, String::copy(encoded)); return JS::js_string(interpreter, move(encoded));
} }
JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter) JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter)

View file

@ -75,6 +75,6 @@ int main(int argc, char** argv)
return 0; return 0;
} }
auto encoded = encode_base64(StringView(buffer)); auto encoded = encode_base64(buffer);
printf("%s\n", String::copy(encoded).characters()); printf("%s\n", encoded.characters());
} }