util: add uuid

This commit is contained in:
Michael Scire 2020-02-24 20:14:29 -08:00
parent 5a8aebf74d
commit c17b6d0d44
6 changed files with 187 additions and 4 deletions

View file

@ -16,5 +16,6 @@
#pragma once
#include "util/util_uuid_api.hpp"
#include "util/util_compression.hpp"
#include "util/util_ini.hpp"

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::util {
/* Nintendo provides UUID generation following RFC 4122. */
/* By default, UUIDs are generated as version 4 (random). */
Uuid GenerateUuid();
Uuid GenerateUuidVersion5(const void *sha1_hash);
}

View file

@ -0,0 +1,111 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::util {
namespace {
struct UuidImpl {
util::BitPack32 data[4];
using TimeLow = util::BitPack32::Field<0, BITSIZEOF(u32), u32>;
using TimeMid = util::BitPack32::Field<0, BITSIZEOF(u16), u16>;
using TimeHighAndVersion = util::BitPack32::Field<TimeMid::Next, BITSIZEOF(u16), u16>;
using Version = util::BitPack32::Field<TimeMid::Next + 12, 4, u16>;
static_assert(TimeHighAndVersion::Next == Version::Next);
using ClockSeqHiAndReserved = util::BitPack32::Field<0, BITSIZEOF(u8), u8>;
using Reserved = util::BitPack32::Field<6, 2, u8>;
using ClockSeqLow = util::BitPack32::Field<ClockSeqHiAndReserved::Next, BITSIZEOF(u8), u8>;
using NodeLow = util::BitPack32::Field<ClockSeqLow::Next, BITSIZEOF(u16), u16>;
static_assert(ClockSeqHiAndReserved::Next == Reserved::Next);
using NodeHigh = util::BitPack32::Field<0, BITSIZEOF(u32), u32>;
inline Uuid Convert() const {
/* Convert the fields from native endian to big endian. */
util::BitPack32 converted[4] = {util::BitPack32(0), util::BitPack32(0), util::BitPack32(0), util::BitPack32(0)};
converted[0].Set<TimeLow>(util::ConvertToBigEndian(this->data[0].Get<TimeLow>()));
converted[1].Set<TimeMid>(util::ConvertToBigEndian(this->data[1].Get<TimeMid>()));
converted[1].Set<TimeHighAndVersion>(util::ConvertToBigEndian(this->data[1].Get<TimeHighAndVersion>()));
converted[2].Set<ClockSeqHiAndReserved>(util::ConvertToBigEndian(this->data[2].Get<ClockSeqHiAndReserved>()));
converted[2].Set<ClockSeqLow>(util::ConvertToBigEndian(this->data[2].Get<ClockSeqLow>()));
u64 node_lo = static_cast<u64>(this->data[2].Get<NodeLow>());
u64 node_hi = static_cast<u64>(this->data[3].Get<NodeHigh>());
u64 node = util::ConvertToBigEndian48(static_cast<u64>((node_hi << BITSIZEOF(u16)) | (node_lo)));
constexpr u64 NodeLoMask = (UINT64_C(1) << BITSIZEOF(u16)) - 1u;
constexpr u64 NodeHiMask = (UINT64_C(1) << BITSIZEOF(u32)) - 1u;
converted[2].Set<NodeLow>(static_cast<u16>(node & NodeLoMask));
converted[3].Set<NodeHigh>(static_cast<u32>((node >> BITSIZEOF(u16)) & NodeHiMask));
Uuid uuid;
std::memcpy(uuid.data, converted, sizeof(uuid.data));
return uuid;
}
};
static_assert(sizeof(UuidImpl) == sizeof(Uuid));
ALWAYS_INLINE Uuid GenerateUuidVersion4() {
constexpr u16 Version = 0x4;
constexpr u8 Reserved = 0x1;
/* Generate a random uuid. */
UuidImpl uuid = {util::BitPack32(0), util::BitPack32(0), util::BitPack32(0), util::BitPack32(0)};
os::GenerateRandomBytes(uuid.data, sizeof(uuid.data));
/* Set version and reserved. */
uuid.data[1].Set<UuidImpl::Version>(Version);
uuid.data[2].Set<UuidImpl::Reserved>(Reserved);
/* Return the uuid. */
return uuid.Convert();
}
}
Uuid GenerateUuid() {
return GenerateUuidVersion4();
}
Uuid GenerateUuidVersion5(const void *sha1_hash) {
constexpr u16 Version = 0x5;
constexpr u8 Reserved = 0x1;
/* Generate a uuid from a SHA1 hash. */
UuidImpl uuid = {util::BitPack32(0), util::BitPack32(0), util::BitPack32(0), util::BitPack32(0)};
std::memcpy(uuid.data, sha1_hash, sizeof(uuid.data));
/* Set version and reserved. */
uuid.data[1].Set<UuidImpl::Version>(Version);
uuid.data[2].Set<UuidImpl::Reserved>(Reserved);
/* Return the uuid. */
return uuid.Convert();
}
}

View file

@ -21,13 +21,14 @@
#include <vapours/util/util_alignment.hpp>
#include <vapours/util/util_size.hpp>
#include <vapours/util/util_endian.hpp>
#include <vapours/util/util_fourcc.hpp>
#include <vapours/util/util_bitpack.hpp>
#include <vapours/util/util_bitset.hpp>
#include <vapours/util/util_scope_guard.hpp>
#include <vapours/util/util_specialization_of.hpp>
#include <vapours/util/util_bitpack.hpp>
#include <vapours/util/util_bitset.hpp>
#include <vapours/util/util_bitutil.hpp>
#include <vapours/util/util_typed_storage.hpp>
#include <vapours/util/util_fourcc.hpp>
#include <vapours/util/util_intrusive_list.hpp>
#include <vapours/util/util_intrusive_red_black_tree.hpp>
#include <vapours/util/util_tinymt.hpp>
#include <vapours/util/util_bitutil.hpp>
#include <vapours/util/util_uuid.hpp>

View file

@ -65,6 +65,7 @@ namespace ams::util {
((u & (ByteMask << 0)) << 8);
} else if constexpr (std::is_same<U, u8>::value) {
AMS_UNUSED(ByteMask);
return u;
} else {
static_assert(!std::is_same<U, U>::value);

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {
struct Uuid {
static constexpr size_t Size = 0x10;
u8 data[Size];
bool operator==(const Uuid &rhs) const {
return std::memcmp(this->data, rhs.data, Size) == 0;
}
bool operator!=(const Uuid &rhs) const {
return !(*this == rhs);
}
u8 operator[](size_t i) const {
return this->data[i];
}
};
}