2022-09-06 19:14:07 -04:00
|
|
|
// Copyright 2014 The Crashpad Authors
|
2014-08-01 14:39:55 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-03-06 18:43:28 -05:00
|
|
|
#if !defined(__STDC_FORMAT_MACROS)
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
|
2014-08-01 14:39:55 -04:00
|
|
|
#include "util/misc/uuid.h"
|
|
|
|
|
2015-01-02 18:46:10 -05:00
|
|
|
#include <inttypes.h>
|
2017-03-22 21:44:05 -04:00
|
|
|
#include <stddef.h>
|
2015-01-02 18:46:10 -05:00
|
|
|
#include <stdio.h>
|
2014-08-01 14:39:55 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2024-04-11 12:48:02 -04:00
|
|
|
#include <algorithm>
|
2023-11-07 12:48:40 -08:00
|
|
|
#include <string_view>
|
2017-10-13 10:35:45 -04:00
|
|
|
#include <type_traits>
|
|
|
|
|
2024-04-11 12:48:02 -04:00
|
|
|
#include "base/containers/span.h"
|
|
|
|
#include "base/numerics/byte_conversions.h"
|
2016-10-27 17:02:48 -04:00
|
|
|
#include "base/rand_util.h"
|
2014-08-01 14:39:55 -04:00
|
|
|
#include "base/strings/stringprintf.h"
|
2015-02-11 12:17:05 -08:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2022-01-19 15:00:24 -05:00
|
|
|
#include "build/build_config.h"
|
2014-08-03 18:50:09 -04:00
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
#if BUILDFLAG(IS_APPLE)
|
2015-04-20 14:21:48 -07:00
|
|
|
#include <uuid/uuid.h>
|
2022-01-19 15:00:24 -05:00
|
|
|
#endif // BUILDFLAG(IS_APPLE)
|
2015-04-20 14:21:48 -07:00
|
|
|
|
2014-08-01 14:39:55 -04:00
|
|
|
namespace crashpad {
|
|
|
|
|
2014-10-01 12:29:01 -07:00
|
|
|
static_assert(sizeof(UUID) == 16, "UUID must be 16 bytes");
|
2024-04-22 22:18:50 +00:00
|
|
|
static_assert(std::is_standard_layout<UUID>::value,
|
|
|
|
"UUID must be a standard-layout type");
|
|
|
|
static_assert(std::is_trivial<UUID>::value, "UUID must be a trivial type");
|
2014-08-03 18:50:09 -04:00
|
|
|
|
2016-11-10 15:11:20 -05:00
|
|
|
bool UUID::operator==(const UUID& that) const {
|
|
|
|
return memcmp(this, &that, sizeof(*this)) == 0;
|
2014-08-01 16:10:58 -04:00
|
|
|
}
|
|
|
|
|
2021-11-19 14:12:08 -05:00
|
|
|
bool UUID::operator<(const UUID& that) const {
|
|
|
|
return memcmp(this, &that, sizeof(*this)) < 0;
|
|
|
|
}
|
|
|
|
|
2016-11-10 15:11:20 -05:00
|
|
|
void UUID::InitializeToZero() {
|
|
|
|
memset(this, 0, sizeof(*this));
|
2014-09-04 11:45:40 -04:00
|
|
|
}
|
|
|
|
|
2024-04-11 12:48:02 -04:00
|
|
|
void UUID::InitializeFromBytes(const uint8_t* bytes_ptr) {
|
|
|
|
// TODO(crbug.com/40284755): This span construction is unsound. The caller
|
|
|
|
// should provide a span instead of an unbounded pointer.
|
|
|
|
base::span<const uint8_t, sizeof(UUID)> bytes(bytes_ptr, sizeof(UUID));
|
|
|
|
data_1 = base::numerics::U32FromBigEndian(bytes.subspan<0u, 4u>());
|
|
|
|
data_2 = base::numerics::U16FromBigEndian(bytes.subspan<4u, 2u>());
|
|
|
|
data_3 = base::numerics::U16FromBigEndian(bytes.subspan<6u, 2u>());
|
|
|
|
std::ranges::copy(bytes.subspan<8u, 2u>(), data_4);
|
|
|
|
std::ranges::copy(bytes.subspan<10u, 6u>(), data_5);
|
2014-08-01 14:39:55 -04:00
|
|
|
}
|
|
|
|
|
2015-01-02 18:46:10 -05:00
|
|
|
bool UUID::InitializeFromString(const base::StringPiece& string) {
|
|
|
|
if (string.length() != 36)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UUID temp;
|
2017-07-25 13:34:04 -04:00
|
|
|
static constexpr char kScanFormat[] =
|
2015-01-02 18:46:10 -05:00
|
|
|
"%08" SCNx32 "-%04" SCNx16 "-%04" SCNx16
|
|
|
|
"-%02" SCNx8 "%02" SCNx8
|
|
|
|
"-%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8;
|
|
|
|
int rv = sscanf(string.data(),
|
|
|
|
kScanFormat,
|
|
|
|
&temp.data_1,
|
|
|
|
&temp.data_2,
|
|
|
|
&temp.data_3,
|
|
|
|
&temp.data_4[0],
|
|
|
|
&temp.data_4[1],
|
|
|
|
&temp.data_5[0],
|
|
|
|
&temp.data_5[1],
|
|
|
|
&temp.data_5[2],
|
|
|
|
&temp.data_5[3],
|
|
|
|
&temp.data_5[4],
|
|
|
|
&temp.data_5[5]);
|
|
|
|
if (rv != 11)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*this = temp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2023-11-07 12:48:40 -08:00
|
|
|
bool UUID::InitializeFromString(const std::wstring_view& string) {
|
2023-02-28 18:23:52 -05:00
|
|
|
return InitializeFromString(base::WideToUTF8(string));
|
2018-02-13 12:09:33 -08:00
|
|
|
}
|
2020-09-12 09:20:14 +02:00
|
|
|
#endif
|
2018-02-13 12:09:33 -08:00
|
|
|
|
2015-04-20 14:21:48 -07:00
|
|
|
bool UUID::InitializeWithNew() {
|
2022-01-19 15:00:24 -05:00
|
|
|
#if BUILDFLAG(IS_APPLE)
|
2015-04-20 14:21:48 -07:00
|
|
|
uuid_t uuid;
|
|
|
|
uuid_generate(uuid);
|
|
|
|
InitializeFromBytes(uuid);
|
|
|
|
return true;
|
2022-01-19 15:00:24 -05:00
|
|
|
#elif BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
|
|
|
|
BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_FUCHSIA)
|
2017-11-27 13:37:01 -08:00
|
|
|
// Linux, Android, and Fuchsia do not provide a UUID generator in a
|
|
|
|
// widely-available system library. On Linux and Android, uuid_generate()
|
|
|
|
// from libuuid is not available everywhere.
|
2016-12-07 11:35:07 -08:00
|
|
|
// On Windows, do not use UuidCreate() to avoid a dependency on rpcrt4, so
|
|
|
|
// that this function is usable early in DllMain().
|
2016-10-27 17:02:48 -04:00
|
|
|
base::RandBytes(this, sizeof(*this));
|
|
|
|
|
|
|
|
// Set six bits per RFC 4122 §4.4 to identify this as a pseudo-random UUID.
|
|
|
|
data_3 = (4 << 12) | (data_3 & 0x0fff); // §4.1.3
|
|
|
|
data_4[0] = 0x80 | (data_4[0] & 0x3f); // §4.1.1
|
|
|
|
|
2015-04-20 14:21:48 -07:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
#error Port.
|
2022-01-19 15:00:24 -05:00
|
|
|
#endif // BUILDFLAG(IS_APPLE)
|
2015-04-20 14:21:48 -07:00
|
|
|
}
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2015-03-13 13:53:38 -04:00
|
|
|
void UUID::InitializeFromSystemUUID(const ::UUID* system_uuid) {
|
|
|
|
static_assert(sizeof(::UUID) == sizeof(UUID),
|
|
|
|
"unexpected system uuid size");
|
|
|
|
static_assert(offsetof(::UUID, Data1) == offsetof(UUID, data_1),
|
|
|
|
"unexpected system uuid layout");
|
2016-11-10 15:11:20 -05:00
|
|
|
memcpy(this, system_uuid, sizeof(*this));
|
2015-03-13 13:53:38 -04:00
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2015-03-13 13:53:38 -04:00
|
|
|
|
2014-08-01 14:39:55 -04:00
|
|
|
std::string UUID::ToString() const {
|
2014-08-01 16:10:58 -04:00
|
|
|
return base::StringPrintf("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
|
|
data_1,
|
|
|
|
data_2,
|
|
|
|
data_3,
|
|
|
|
data_4[0],
|
|
|
|
data_4[1],
|
|
|
|
data_5[0],
|
|
|
|
data_5[1],
|
|
|
|
data_5[2],
|
|
|
|
data_5[3],
|
|
|
|
data_5[4],
|
|
|
|
data_5[5]);
|
2014-08-01 14:39:55 -04:00
|
|
|
}
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-09-12 09:20:14 +02:00
|
|
|
std::wstring UUID::ToWString() const {
|
|
|
|
return base::UTF8ToWide(ToString());
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2014-08-01 14:39:55 -04:00
|
|
|
} // namespace crashpad
|