2020-12-31 12:38:12 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-31 12:38:12 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/AllOf.h>
|
|
|
|
#include <AK/Hex.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <AK/UUID.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
UUID::UUID(Array<u8, 16> uuid_buffer)
|
|
|
|
{
|
|
|
|
uuid_buffer.span().copy_to(m_uuid_buffer);
|
|
|
|
}
|
|
|
|
|
2022-01-28 21:21:40 +03:00
|
|
|
void UUID::convert_string_view_to_little_endian_uuid(StringView uuid_string_view)
|
2020-12-31 12:38:12 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(uuid_string_view.length() == 36);
|
2022-01-20 20:01:39 +03:00
|
|
|
auto first_unit = MUST(decode_hex(uuid_string_view.substring_view(0, 8)));
|
|
|
|
auto second_unit = MUST(decode_hex(uuid_string_view.substring_view(9, 4)));
|
|
|
|
auto third_unit = MUST(decode_hex(uuid_string_view.substring_view(14, 4)));
|
|
|
|
auto fourth_unit = MUST(decode_hex(uuid_string_view.substring_view(19, 4)));
|
|
|
|
auto fifth_unit = MUST(decode_hex(uuid_string_view.substring_view(24, 12)));
|
2020-12-31 12:38:12 +03:00
|
|
|
|
2022-01-20 20:01:39 +03:00
|
|
|
VERIFY(first_unit.size() == 4 && second_unit.size() == 2
|
|
|
|
&& third_unit.size() == 2 && fourth_unit.size() == 2
|
|
|
|
&& fifth_unit.size() == 6);
|
2021-05-14 21:53:27 +03:00
|
|
|
|
2022-01-20 20:01:39 +03:00
|
|
|
m_uuid_buffer.span().overwrite(0, first_unit.data(), first_unit.size());
|
|
|
|
m_uuid_buffer.span().overwrite(4, second_unit.data(), second_unit.size());
|
|
|
|
m_uuid_buffer.span().overwrite(6, third_unit.data(), third_unit.size());
|
|
|
|
m_uuid_buffer.span().overwrite(8, fourth_unit.data(), fourth_unit.size());
|
|
|
|
m_uuid_buffer.span().overwrite(10, fifth_unit.data(), fifth_unit.size());
|
2020-12-31 12:38:12 +03:00
|
|
|
}
|
|
|
|
|
2022-01-28 21:21:40 +03:00
|
|
|
void UUID::convert_string_view_to_mixed_endian_uuid(StringView uuid_string_view)
|
2020-12-31 12:38:12 +03:00
|
|
|
{
|
2022-01-28 21:21:40 +03:00
|
|
|
VERIFY(uuid_string_view.length() == 36);
|
|
|
|
auto first_unit = MUST(decode_hex(uuid_string_view.substring_view(0, 8)));
|
|
|
|
auto second_unit = MUST(decode_hex(uuid_string_view.substring_view(9, 4)));
|
|
|
|
auto third_unit = MUST(decode_hex(uuid_string_view.substring_view(14, 4)));
|
|
|
|
auto fourth_unit = MUST(decode_hex(uuid_string_view.substring_view(19, 4)));
|
|
|
|
auto fifth_unit = MUST(decode_hex(uuid_string_view.substring_view(24, 12)));
|
|
|
|
|
|
|
|
VERIFY(first_unit.size() == 4 && second_unit.size() == 2
|
|
|
|
&& third_unit.size() == 2 && fourth_unit.size() == 2
|
|
|
|
&& fifth_unit.size() == 6);
|
|
|
|
|
|
|
|
// Revert endianness for first 4 bytes
|
|
|
|
for (size_t index = 0; index < 4; index++) {
|
|
|
|
m_uuid_buffer[3 - index] = first_unit[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revert endianness for second 2 bytes and again for 2 bytes
|
|
|
|
for (size_t index = 0; index < 2; index++) {
|
|
|
|
m_uuid_buffer[3 + 2 - index] = second_unit[index];
|
|
|
|
m_uuid_buffer[5 + 2 - index] = third_unit[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
m_uuid_buffer.span().overwrite(8, fourth_unit.data(), fourth_unit.size());
|
|
|
|
m_uuid_buffer.span().overwrite(10, fifth_unit.data(), fifth_unit.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
UUID::UUID(StringView uuid_string_view, Endianness endianness)
|
|
|
|
{
|
|
|
|
if (endianness == Endianness::Little) {
|
|
|
|
convert_string_view_to_little_endian_uuid(uuid_string_view);
|
|
|
|
return;
|
|
|
|
} else if (endianness == Endianness::Mixed) {
|
|
|
|
convert_string_view_to_mixed_endian_uuid(uuid_string_view);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-12-31 12:38:12 +03:00
|
|
|
}
|
|
|
|
|
2022-02-15 21:55:53 +03:00
|
|
|
#ifdef KERNEL
|
|
|
|
ErrorOr<NonnullOwnPtr<Kernel::KString>> UUID::to_string() const
|
|
|
|
{
|
|
|
|
StringBuilder builder(36);
|
2022-02-16 00:25:25 +03:00
|
|
|
auto nibble0 = TRY(encode_hex(m_uuid_buffer.span().trim(4)));
|
|
|
|
TRY(builder.try_append(nibble0->view()));
|
2022-02-15 21:55:53 +03:00
|
|
|
TRY(builder.try_append('-'));
|
2022-02-16 00:25:25 +03:00
|
|
|
auto nibble1 = TRY(encode_hex(m_uuid_buffer.span().slice(4).trim(2)));
|
|
|
|
TRY(builder.try_append(nibble1->view()));
|
2022-02-15 21:55:53 +03:00
|
|
|
TRY(builder.try_append('-'));
|
2022-02-16 00:25:25 +03:00
|
|
|
auto nibble2 = TRY(encode_hex(m_uuid_buffer.span().slice(6).trim(2)));
|
|
|
|
TRY(builder.try_append(nibble2->view()));
|
2022-02-15 21:55:53 +03:00
|
|
|
TRY(builder.try_append('-'));
|
2022-02-16 00:25:25 +03:00
|
|
|
auto nibble3 = TRY(encode_hex(m_uuid_buffer.span().slice(8).trim(2)));
|
|
|
|
TRY(builder.try_append(nibble3->view()));
|
2022-02-15 21:55:53 +03:00
|
|
|
TRY(builder.try_append('-'));
|
2022-02-16 00:25:25 +03:00
|
|
|
auto nibble4 = TRY(encode_hex(m_uuid_buffer.span().slice(10).trim(6)));
|
|
|
|
TRY(builder.try_append(nibble4->view()));
|
2022-02-15 21:55:53 +03:00
|
|
|
return Kernel::KString::try_create(builder.string_view());
|
|
|
|
}
|
|
|
|
#else
|
2022-12-19 01:24:02 +03:00
|
|
|
ErrorOr<String> UUID::to_string() const
|
2020-12-31 12:38:12 +03:00
|
|
|
{
|
2022-12-19 01:24:02 +03:00
|
|
|
auto buffer_span = m_uuid_buffer.span();
|
2020-12-31 12:38:12 +03:00
|
|
|
StringBuilder builder(36);
|
2022-12-19 01:24:02 +03:00
|
|
|
TRY(builder.try_append(encode_hex(buffer_span.trim(4)).view()));
|
|
|
|
TRY(builder.try_append('-'));
|
|
|
|
TRY(builder.try_append(encode_hex(buffer_span.slice(4, 2)).view()));
|
|
|
|
TRY(builder.try_append('-'));
|
|
|
|
TRY(builder.try_append(encode_hex(buffer_span.slice(6, 2)).view()));
|
|
|
|
TRY(builder.try_append('-'));
|
|
|
|
TRY(builder.try_append(encode_hex(buffer_span.slice(8, 2)).view()));
|
|
|
|
TRY(builder.try_append('-'));
|
|
|
|
TRY(builder.try_append(encode_hex(buffer_span.slice(10, 6)).view()));
|
|
|
|
return builder.to_string();
|
2020-12-31 12:38:12 +03:00
|
|
|
}
|
2022-02-15 21:55:53 +03:00
|
|
|
#endif
|
2020-12-31 12:38:12 +03:00
|
|
|
|
|
|
|
bool UUID::is_zero() const
|
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
return all_of(m_uuid_buffer, [](auto const octet) { return octet == 0; });
|
2020-12-31 12:38:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|