mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
5e1499d104
This commit un-deprecates DeprecatedString, and repurposes it as a byte string. As the null state has already been removed, there are no other particularly hairy blockers in repurposing this type as a byte string (what it _really_ is). This commit is auto-generated: $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \ Meta Ports Ladybird Tests Kernel) $ perl -pie 's/\bDeprecatedString\b/ByteString/g; s/deprecated_string/byte_string/g' $xs $ clang-format --style=file -i \ $(git diff --name-only | grep \.cpp\|\.h) $ gn format $(git ls-files '*.gn' '*.gni')
152 lines
3.5 KiB
C++
152 lines
3.5 KiB
C++
/*
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/BitCast.h>
|
|
#include <AK/ByteBuffer.h>
|
|
#include <AK/ByteString.h>
|
|
#include <AK/JsonObject.h>
|
|
#include <AK/JsonValue.h>
|
|
#include <AK/NumericLimits.h>
|
|
#include <AK/String.h>
|
|
#include <AK/Time.h>
|
|
#include <AK/URL.h>
|
|
#include <LibCore/AnonymousBuffer.h>
|
|
#include <LibCore/DateTime.h>
|
|
#include <LibCore/Proxy.h>
|
|
#include <LibCore/System.h>
|
|
#include <LibIPC/Encoder.h>
|
|
#include <LibIPC/File.h>
|
|
|
|
namespace IPC {
|
|
|
|
ErrorOr<void> Encoder::encode_size(size_t size)
|
|
{
|
|
if (static_cast<u64>(size) > static_cast<u64>(NumericLimits<u32>::max()))
|
|
return Error::from_string_literal("Container exceeds the maximum allowed size");
|
|
return encode(static_cast<u32>(size));
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, float const& value)
|
|
{
|
|
return encoder.encode(bit_cast<u32>(value));
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, double const& value)
|
|
{
|
|
return encoder.encode(bit_cast<u64>(value));
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, String const& value)
|
|
{
|
|
auto bytes = value.bytes();
|
|
TRY(encoder.encode_size(bytes.size()));
|
|
TRY(encoder.append(bytes.data(), bytes.size()));
|
|
return {};
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, StringView const& value)
|
|
{
|
|
// NOTE: Do not change this encoding without also updating LibC/netdb.cpp.
|
|
if (value.is_null())
|
|
return encoder.encode(NumericLimits<u32>::max());
|
|
|
|
TRY(encoder.encode_size(value.length()));
|
|
TRY(encoder.append(reinterpret_cast<u8 const*>(value.characters_without_null_termination()), value.length()));
|
|
return {};
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, ByteString const& value)
|
|
{
|
|
return encoder.encode(value.view());
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, ByteBuffer const& value)
|
|
{
|
|
TRY(encoder.encode_size(value.size()));
|
|
TRY(encoder.append(value.data(), value.size()));
|
|
return {};
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, JsonValue const& value)
|
|
{
|
|
return encoder.encode(value.serialized<StringBuilder>());
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, Duration const& value)
|
|
{
|
|
return encoder.encode(value.to_nanoseconds());
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, UnixDateTime const& value)
|
|
{
|
|
return encoder.encode(value.nanoseconds_since_epoch());
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, URL const& value)
|
|
{
|
|
return encoder.encode(value.to_byte_string());
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, File const& file)
|
|
{
|
|
int fd = file.fd();
|
|
|
|
if (fd != -1)
|
|
fd = TRY(Core::System::dup(fd));
|
|
|
|
TRY(encoder.append_file_descriptor(fd));
|
|
return {};
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder&, Empty const&)
|
|
{
|
|
return {};
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
|
|
{
|
|
TRY(encoder.encode(buffer.is_valid()));
|
|
|
|
if (buffer.is_valid()) {
|
|
TRY(encoder.encode_size(buffer.size()));
|
|
TRY(encoder.encode(IPC::File { buffer.fd() }));
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, Core::DateTime const& datetime)
|
|
{
|
|
return encoder.encode(static_cast<i64>(datetime.timestamp()));
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, Core::ProxyData const& proxy)
|
|
{
|
|
TRY(encoder.encode(proxy.type));
|
|
TRY(encoder.encode(proxy.host_ipv4));
|
|
TRY(encoder.encode(proxy.port));
|
|
return {};
|
|
}
|
|
|
|
}
|