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