2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-02-09 16:08:43 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-08-06 20:04:12 +02:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-05-06 18:40:06 +03:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
2019-08-06 20:04:12 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-09-17 13:51:09 -06:00
|
|
|
inline bool KBufferBuilder::check_expand(size_t size)
|
2019-08-06 20:04:12 +02:00
|
|
|
{
|
2020-12-18 14:10:10 +01:00
|
|
|
if (!m_buffer)
|
|
|
|
return false;
|
2020-09-17 13:51:09 -06:00
|
|
|
if ((m_size + size) < m_buffer->capacity())
|
|
|
|
return true;
|
|
|
|
if (Checked<size_t>::addition_would_overflow(m_size, size))
|
|
|
|
return false;
|
|
|
|
size_t new_buffer_size = m_size + size;
|
|
|
|
if (Checked<size_t>::addition_would_overflow(new_buffer_size, 1 * MiB))
|
|
|
|
return false;
|
2021-12-24 11:22:11 -03:00
|
|
|
auto rounded_new_buffer_size_or_error = Memory::page_round_up(new_buffer_size + 1 * MiB);
|
|
|
|
if (rounded_new_buffer_size_or_error.is_error()) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-04-11 00:08:07 +02:00
|
|
|
auto new_buffer_or_error = KBuffer::try_create_with_size("KBufferBuilder"sv, rounded_new_buffer_size_or_error.value());
|
2021-09-07 15:44:45 +02:00
|
|
|
if (new_buffer_or_error.is_error())
|
|
|
|
return false;
|
|
|
|
auto new_buffer = new_buffer_or_error.release_value();
|
|
|
|
memcpy(new_buffer->data(), m_buffer->data(), m_buffer->size());
|
|
|
|
m_buffer = move(new_buffer);
|
|
|
|
return true;
|
2019-08-06 20:04:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 13:51:09 -06:00
|
|
|
bool KBufferBuilder::flush()
|
2019-08-06 20:04:12 +02:00
|
|
|
{
|
2020-09-05 15:52:14 -06:00
|
|
|
if (!m_buffer)
|
2020-09-17 13:51:09 -06:00
|
|
|
return false;
|
|
|
|
m_buffer->set_size(m_size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnPtr<KBuffer> KBufferBuilder::build()
|
|
|
|
{
|
|
|
|
if (!flush())
|
2020-09-05 15:52:14 -06:00
|
|
|
return {};
|
2021-05-12 23:34:00 -07:00
|
|
|
|
2021-09-07 15:44:45 +02:00
|
|
|
return move(m_buffer);
|
2019-08-06 20:04:12 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<KBufferBuilder> KBufferBuilder::try_create()
|
2021-09-07 15:54:23 +02:00
|
|
|
{
|
2022-04-11 00:08:07 +02:00
|
|
|
auto buffer = TRY(KBuffer::try_create_with_size("KBufferBuilder"sv, 4 * MiB, Memory::Region::Access::ReadWrite));
|
2021-09-07 15:54:23 +02:00
|
|
|
return KBufferBuilder { move(buffer) };
|
|
|
|
}
|
|
|
|
|
|
|
|
KBufferBuilder::KBufferBuilder(NonnullOwnPtr<KBuffer> buffer)
|
|
|
|
: m_buffer(move(buffer))
|
2019-08-06 20:04:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> KBufferBuilder::append_bytes(ReadonlyBytes bytes)
|
2020-09-17 13:51:09 -06:00
|
|
|
{
|
|
|
|
if (!check_expand(bytes.size()))
|
2021-09-06 18:24:13 +02:00
|
|
|
return ENOMEM;
|
2020-09-17 13:51:09 -06:00
|
|
|
memcpy(insertion_ptr(), bytes.data(), bytes.size());
|
|
|
|
m_size += bytes.size();
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2020-09-17 13:51:09 -06:00
|
|
|
}
|
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
ErrorOr<void> KBufferBuilder::append(StringView str)
|
2019-08-06 20:04:12 +02:00
|
|
|
{
|
|
|
|
if (str.is_empty())
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2020-09-17 13:51:09 -06:00
|
|
|
if (!check_expand(str.length()))
|
2021-09-06 18:24:13 +02:00
|
|
|
return ENOMEM;
|
2019-08-06 20:04:12 +02:00
|
|
|
memcpy(insertion_ptr(), str.characters_without_null_termination(), str.length());
|
|
|
|
m_size += str.length();
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-08-06 20:04:12 +02:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<void> KBufferBuilder::append(char const* characters, int length)
|
2019-08-06 20:04:12 +02:00
|
|
|
{
|
|
|
|
if (!length)
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2020-09-17 13:51:09 -06:00
|
|
|
if (!check_expand(length))
|
2021-09-06 18:24:13 +02:00
|
|
|
return ENOMEM;
|
2020-07-02 11:14:48 +03:00
|
|
|
memcpy(insertion_ptr(), characters, length);
|
2019-08-06 20:04:12 +02:00
|
|
|
m_size += length;
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-08-06 20:04:12 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> KBufferBuilder::append(char ch)
|
2019-08-06 20:04:12 +02:00
|
|
|
{
|
2020-09-17 13:51:09 -06:00
|
|
|
if (!check_expand(1))
|
2021-09-06 18:24:13 +02:00
|
|
|
return ENOMEM;
|
2019-08-06 20:04:12 +02:00
|
|
|
insertion_ptr()[0] = ch;
|
|
|
|
m_size += 1;
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-08-06 20:04:12 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
ErrorOr<void> KBufferBuilder::append_escaped_for_json(StringView string)
|
2020-11-02 12:56:36 +01:00
|
|
|
{
|
|
|
|
for (auto ch : string) {
|
|
|
|
switch (ch) {
|
|
|
|
case '\b':
|
2021-09-06 18:24:13 +02:00
|
|
|
TRY(append("\\b"));
|
2020-11-02 12:56:36 +01:00
|
|
|
break;
|
|
|
|
case '\n':
|
2021-09-06 18:24:13 +02:00
|
|
|
TRY(append("\\n"));
|
2020-11-02 12:56:36 +01:00
|
|
|
break;
|
|
|
|
case '\t':
|
2021-09-06 18:24:13 +02:00
|
|
|
TRY(append("\\t"));
|
2020-11-02 12:56:36 +01:00
|
|
|
break;
|
|
|
|
case '\"':
|
2021-09-06 18:24:13 +02:00
|
|
|
TRY(append("\\\""));
|
2020-11-02 12:56:36 +01:00
|
|
|
break;
|
|
|
|
case '\\':
|
2021-09-06 18:24:13 +02:00
|
|
|
TRY(append("\\\\"));
|
2020-11-02 12:56:36 +01:00
|
|
|
break;
|
|
|
|
default:
|
2021-09-11 16:50:31 +04:30
|
|
|
if (ch >= 0 && ch <= 0x1f)
|
2021-12-30 13:43:45 +01:00
|
|
|
TRY(appendff("\\u{:04x}", ch));
|
2021-09-11 16:50:31 +04:30
|
|
|
else
|
|
|
|
TRY(append(ch));
|
2020-11-02 12:56:36 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2020-11-02 12:56:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|