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