From d22d29a29a6a035fa241f2f17b13417192637f5c Mon Sep 17 00:00:00 2001 From: Liav A Date: Thu, 31 Dec 2020 11:38:12 +0200 Subject: [PATCH] AK: Add the UUID container This container represents a universally unique identifier. This will be used later in the kernel for GUID partitions. --- AK/UUID.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ AK/UUID.h | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 AK/UUID.cpp create mode 100644 AK/UUID.h diff --git a/AK/UUID.cpp b/AK/UUID.cpp new file mode 100644 index 0000000000..d779e4a5ef --- /dev/null +++ b/AK/UUID.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2020, Liav A. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +namespace AK { +UUID::UUID() +{ +} + +UUID::UUID(Array uuid_buffer) +{ + uuid_buffer.span().copy_to(m_uuid_buffer); +} + +void UUID::convert_string_view_to_uuid(const StringView& uuid_string_view) +{ + ASSERT(uuid_string_view.length() == 36); + auto first_unit = decode_hex(uuid_string_view.substring_view(0, 8)); + auto second_unit = decode_hex(uuid_string_view.substring_view(9, 4)); + auto third_unit = decode_hex(uuid_string_view.substring_view(14, 4)); + auto fourth_unit = decode_hex(uuid_string_view.substring_view(19, 4)); + auto fifth_unit = decode_hex(uuid_string_view.substring_view(24, 12)); + + m_uuid_buffer.span().overwrite(0, first_unit.value().data(), first_unit.value().size()); + m_uuid_buffer.span().overwrite(4, second_unit.value().data(), second_unit.value().size()); + m_uuid_buffer.span().overwrite(6, third_unit.value().data(), third_unit.value().size()); + m_uuid_buffer.span().overwrite(8, fourth_unit.value().data(), fourth_unit.value().size()); + m_uuid_buffer.span().overwrite(10, fifth_unit.value().data(), fifth_unit.value().size()); +} + +UUID::UUID(const StringView& uuid_string_view) +{ + convert_string_view_to_uuid(uuid_string_view); +} + +String UUID::to_string() const +{ + StringBuilder builder(36); + builder.append(encode_hex(m_uuid_buffer.span().trim(4)).view()); + builder.append('-'); + builder.append(encode_hex(m_uuid_buffer.span().slice(4).trim(2)).view()); + builder.append('-'); + builder.append(encode_hex(m_uuid_buffer.span().slice(6).trim(2)).view()); + builder.append('-'); + builder.append(encode_hex(m_uuid_buffer.span().slice(8).trim(2)).view()); + builder.append('-'); + builder.append(encode_hex(m_uuid_buffer.span().slice(10).trim(6)).view()); + return builder.to_string(); +} + +bool UUID::operator==(const UUID& other) const +{ + for (size_t index = 0; index < 16; index++) { + if (m_uuid_buffer[index] != other.m_uuid_buffer[index]) + return false; + } + return true; +} + +bool UUID::is_zero() const +{ + return all_of(m_uuid_buffer.begin(), m_uuid_buffer.end(), [](const auto octet) { return octet == 0; }); +} + +} diff --git a/AK/UUID.h b/AK/UUID.h new file mode 100644 index 0000000000..d6bedf260c --- /dev/null +++ b/AK/UUID.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2020, Liav A. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include +#include +#include +#include + +namespace AK { + +class UUID { +public: + UUID(); + UUID(Array uuid_buffer); + UUID(const StringView&); + ~UUID() { } + + bool operator==(const UUID&) const; + bool operator!=(const UUID& other) const { return !(*this == other); } + bool operator<=(const UUID&) const = delete; + bool operator>=(const UUID&) const = delete; + bool operator<(const UUID&) const = delete; + bool operator>(const UUID&) const = delete; + + String to_string() const; + bool is_zero() const; + +private: + void convert_string_view_to_uuid(const StringView&); + void fill_buffer(ByteBuffer); + + Array m_uuid_buffer {}; +}; + +} + +using AK::UUID;