2010-06-09 00:52:24 +08:00
|
|
|
//===-- UUID.cpp ------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-03-04 09:28:55 +08:00
|
|
|
#include "lldb/Utility/UUID.h"
|
|
|
|
|
2017-03-04 09:30:05 +08:00
|
|
|
// Other libraries and framework includes
|
|
|
|
// Project includes
|
|
|
|
#include "lldb/Utility/Stream.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
// C Includes
|
2010-06-10 03:36:54 +08:00
|
|
|
#include <ctype.h>
|
2016-09-07 04:57:50 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2010-06-10 03:36:54 +08:00
|
|
|
|
2018-06-21 23:24:39 +08:00
|
|
|
using namespace lldb_private;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
Represent invalid UUIDs as UUIDs with length zero
Summary:
During the previous attempt to generalize the UUID class, it was
suggested that we represent invalid UUIDs as length zero (previously, we
used an all-zero UUID for that). This meant that some valid build-ids
could not be represented (it's possible however unlikely that a checksum of
some file would be zero) and complicated adding support for variable
length build-ids (should a 16-byte empty UUID compare equal to a 20-byte
empty UUID?).
This patch resolves these issues by introducing a canonical
representation for an invalid UUID. The slight complication here is that
some clients (MachO) actually use the all-zero notation to mean "no UUID
has been set". To keep this use case working (while making it very
explicit about which construction semantices are wanted), replaced the
UUID constructors and the SetBytes functions with named factory methods.
- "fromData" creates a UUID from the given data, and it treats all bytes
equally.
- "fromOptionalData" first checks the data contents - if all bytes are
zero, it treats this as an invalid/empty UUID.
Reviewers: clayborg, sas, lemo, davide, espindola
Subscribers: emaste, lldb-commits, arichardson
Differential Revision: https://reviews.llvm.org/D48479
llvm-svn: 335612
2018-06-26 23:12:20 +08:00
|
|
|
UUID::UUID(llvm::ArrayRef<uint8_t> bytes) {
|
|
|
|
if (bytes.size() != 20 && bytes.size() != 16)
|
|
|
|
bytes = {};
|
2010-06-09 00:52:24 +08:00
|
|
|
|
Represent invalid UUIDs as UUIDs with length zero
Summary:
During the previous attempt to generalize the UUID class, it was
suggested that we represent invalid UUIDs as length zero (previously, we
used an all-zero UUID for that). This meant that some valid build-ids
could not be represented (it's possible however unlikely that a checksum of
some file would be zero) and complicated adding support for variable
length build-ids (should a 16-byte empty UUID compare equal to a 20-byte
empty UUID?).
This patch resolves these issues by introducing a canonical
representation for an invalid UUID. The slight complication here is that
some clients (MachO) actually use the all-zero notation to mean "no UUID
has been set". To keep this use case working (while making it very
explicit about which construction semantices are wanted), replaced the
UUID constructors and the SetBytes functions with named factory methods.
- "fromData" creates a UUID from the given data, and it treats all bytes
equally.
- "fromOptionalData" first checks the data contents - if all bytes are
zero, it treats this as an invalid/empty UUID.
Reviewers: clayborg, sas, lemo, davide, espindola
Subscribers: emaste, lldb-commits, arichardson
Differential Revision: https://reviews.llvm.org/D48479
llvm-svn: 335612
2018-06-26 23:12:20 +08:00
|
|
|
m_num_uuid_bytes = bytes.size();
|
|
|
|
std::memcpy(m_uuid, bytes.data(), bytes.size());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string UUID::GetAsString(const char *separator) const {
|
|
|
|
std::string result;
|
|
|
|
char buf[256];
|
|
|
|
if (!separator)
|
|
|
|
separator = "-";
|
2018-06-21 23:07:43 +08:00
|
|
|
const uint8_t *u = GetBytes().data();
|
2016-09-07 04:57:50 +08:00
|
|
|
if (sizeof(buf) >
|
|
|
|
(size_t)snprintf(buf, sizeof(buf), "%2.2X%2.2X%2.2X%2.2X%s%2.2X%2.2X%s%2."
|
|
|
|
"2X%2.2X%s%2.2X%2.2X%s%2.2X%2.2X%2.2X%"
|
|
|
|
"2.2X%2.2X%2.2X",
|
|
|
|
u[0], u[1], u[2], u[3], separator, u[4], u[5], separator,
|
|
|
|
u[6], u[7], separator, u[8], u[9], separator, u[10],
|
|
|
|
u[11], u[12], u[13], u[14], u[15])) {
|
|
|
|
result.append(buf);
|
|
|
|
if (m_num_uuid_bytes == 20) {
|
|
|
|
if (sizeof(buf) > (size_t)snprintf(buf, sizeof(buf),
|
|
|
|
"%s%2.2X%2.2X%2.2X%2.2X", separator,
|
|
|
|
u[16], u[17], u[18], u[19]))
|
|
|
|
result.append(buf);
|
2013-05-24 04:57:03 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UUID::Dump(Stream *s) const {
|
2017-11-28 09:26:07 +08:00
|
|
|
s->PutCString(GetAsString().c_str());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int xdigit_to_int(char ch) {
|
|
|
|
ch = tolower(ch);
|
|
|
|
if (ch >= 'a' && ch <= 'f')
|
|
|
|
return 10 + ch - 'a';
|
|
|
|
return ch - '0';
|
|
|
|
}
|
|
|
|
|
2016-11-17 09:37:42 +08:00
|
|
|
llvm::StringRef UUID::DecodeUUIDBytesFromString(llvm::StringRef p,
|
|
|
|
ValueType &uuid_bytes,
|
2016-11-17 09:38:02 +08:00
|
|
|
uint32_t &bytes_decoded,
|
2016-11-17 09:37:42 +08:00
|
|
|
uint32_t num_uuid_bytes) {
|
|
|
|
::memset(uuid_bytes, 0, sizeof(uuid_bytes));
|
2016-09-07 04:57:50 +08:00
|
|
|
size_t uuid_byte_idx = 0;
|
2016-11-17 09:37:42 +08:00
|
|
|
while (!p.empty()) {
|
|
|
|
if (isxdigit(p[0]) && isxdigit(p[1])) {
|
|
|
|
int hi_nibble = xdigit_to_int(p[0]);
|
|
|
|
int lo_nibble = xdigit_to_int(p[1]);
|
|
|
|
// Translate the two hex nibble characters into a byte
|
|
|
|
uuid_bytes[uuid_byte_idx] = (hi_nibble << 4) + lo_nibble;
|
|
|
|
|
|
|
|
// Skip both hex digits
|
|
|
|
p = p.drop_front(2);
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Increment the byte that we are decoding within the UUID value and
|
|
|
|
// break out if we are done
|
2016-11-17 09:37:42 +08:00
|
|
|
if (++uuid_byte_idx == num_uuid_bytes)
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2016-11-17 09:37:42 +08:00
|
|
|
} else if (p.front() == '-') {
|
|
|
|
// Skip dashes
|
|
|
|
p = p.drop_front();
|
|
|
|
} else {
|
|
|
|
// UUID values can only consist of hex characters and '-' chars
|
|
|
|
break;
|
2013-05-24 04:57:03 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2016-11-17 09:37:42 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Clear trailing bytes to 0.
|
|
|
|
for (uint32_t i = uuid_byte_idx; i < sizeof(ValueType); i++)
|
|
|
|
uuid_bytes[i] = 0;
|
2016-11-17 09:38:02 +08:00
|
|
|
bytes_decoded = uuid_byte_idx;
|
2016-11-17 09:37:42 +08:00
|
|
|
return p;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2017-05-12 13:49:54 +08:00
|
|
|
size_t UUID::SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes) {
|
|
|
|
llvm::StringRef p = str;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Skip leading whitespace characters
|
2016-11-17 09:37:42 +08:00
|
|
|
p = p.ltrim();
|
2010-06-09 00:52:24 +08:00
|
|
|
|
Represent invalid UUIDs as UUIDs with length zero
Summary:
During the previous attempt to generalize the UUID class, it was
suggested that we represent invalid UUIDs as length zero (previously, we
used an all-zero UUID for that). This meant that some valid build-ids
could not be represented (it's possible however unlikely that a checksum of
some file would be zero) and complicated adding support for variable
length build-ids (should a 16-byte empty UUID compare equal to a 20-byte
empty UUID?).
This patch resolves these issues by introducing a canonical
representation for an invalid UUID. The slight complication here is that
some clients (MachO) actually use the all-zero notation to mean "no UUID
has been set". To keep this use case working (while making it very
explicit about which construction semantices are wanted), replaced the
UUID constructors and the SetBytes functions with named factory methods.
- "fromData" creates a UUID from the given data, and it treats all bytes
equally.
- "fromOptionalData" first checks the data contents - if all bytes are
zero, it treats this as an invalid/empty UUID.
Reviewers: clayborg, sas, lemo, davide, espindola
Subscribers: emaste, lldb-commits, arichardson
Differential Revision: https://reviews.llvm.org/D48479
llvm-svn: 335612
2018-06-26 23:12:20 +08:00
|
|
|
ValueType bytes;
|
2016-11-17 09:38:02 +08:00
|
|
|
uint32_t bytes_decoded = 0;
|
2016-11-17 09:37:42 +08:00
|
|
|
llvm::StringRef rest =
|
Represent invalid UUIDs as UUIDs with length zero
Summary:
During the previous attempt to generalize the UUID class, it was
suggested that we represent invalid UUIDs as length zero (previously, we
used an all-zero UUID for that). This meant that some valid build-ids
could not be represented (it's possible however unlikely that a checksum of
some file would be zero) and complicated adding support for variable
length build-ids (should a 16-byte empty UUID compare equal to a 20-byte
empty UUID?).
This patch resolves these issues by introducing a canonical
representation for an invalid UUID. The slight complication here is that
some clients (MachO) actually use the all-zero notation to mean "no UUID
has been set". To keep this use case working (while making it very
explicit about which construction semantices are wanted), replaced the
UUID constructors and the SetBytes functions with named factory methods.
- "fromData" creates a UUID from the given data, and it treats all bytes
equally.
- "fromOptionalData" first checks the data contents - if all bytes are
zero, it treats this as an invalid/empty UUID.
Reviewers: clayborg, sas, lemo, davide, espindola
Subscribers: emaste, lldb-commits, arichardson
Differential Revision: https://reviews.llvm.org/D48479
llvm-svn: 335612
2018-06-26 23:12:20 +08:00
|
|
|
UUID::DecodeUUIDBytesFromString(p, bytes, bytes_decoded, num_uuid_bytes);
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// If we successfully decoded a UUID, return the amount of characters that
|
|
|
|
// were consumed
|
2016-11-17 09:37:42 +08:00
|
|
|
if (bytes_decoded == num_uuid_bytes) {
|
Represent invalid UUIDs as UUIDs with length zero
Summary:
During the previous attempt to generalize the UUID class, it was
suggested that we represent invalid UUIDs as length zero (previously, we
used an all-zero UUID for that). This meant that some valid build-ids
could not be represented (it's possible however unlikely that a checksum of
some file would be zero) and complicated adding support for variable
length build-ids (should a 16-byte empty UUID compare equal to a 20-byte
empty UUID?).
This patch resolves these issues by introducing a canonical
representation for an invalid UUID. The slight complication here is that
some clients (MachO) actually use the all-zero notation to mean "no UUID
has been set". To keep this use case working (while making it very
explicit about which construction semantices are wanted), replaced the
UUID constructors and the SetBytes functions with named factory methods.
- "fromData" creates a UUID from the given data, and it treats all bytes
equally.
- "fromOptionalData" first checks the data contents - if all bytes are
zero, it treats this as an invalid/empty UUID.
Reviewers: clayborg, sas, lemo, davide, espindola
Subscribers: emaste, lldb-commits, arichardson
Differential Revision: https://reviews.llvm.org/D48479
llvm-svn: 335612
2018-06-26 23:12:20 +08:00
|
|
|
*this = fromData(bytes, bytes_decoded);
|
2017-05-12 13:49:54 +08:00
|
|
|
return str.size() - rest.size();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Else return zero to indicate we were not able to parse a UUID value
|
|
|
|
return 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2017-05-12 13:49:54 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool lldb_private::operator==(const lldb_private::UUID &lhs,
|
|
|
|
const lldb_private::UUID &rhs) {
|
2018-06-21 23:07:43 +08:00
|
|
|
return lhs.GetBytes() == rhs.GetBytes();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool lldb_private::operator!=(const lldb_private::UUID &lhs,
|
|
|
|
const lldb_private::UUID &rhs) {
|
2017-09-04 04:53:24 +08:00
|
|
|
return !(lhs == rhs);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool lldb_private::operator<(const lldb_private::UUID &lhs,
|
|
|
|
const lldb_private::UUID &rhs) {
|
2018-06-21 23:07:43 +08:00
|
|
|
if (lhs.GetBytes().size() != rhs.GetBytes().size())
|
|
|
|
return lhs.GetBytes().size() < rhs.GetBytes().size();
|
|
|
|
|
|
|
|
return std::memcmp(lhs.GetBytes().data(), rhs.GetBytes().data(),
|
|
|
|
lhs.GetBytes().size());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool lldb_private::operator<=(const lldb_private::UUID &lhs,
|
|
|
|
const lldb_private::UUID &rhs) {
|
2017-09-04 04:53:24 +08:00
|
|
|
return !(lhs > rhs);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool lldb_private::operator>(const lldb_private::UUID &lhs,
|
|
|
|
const lldb_private::UUID &rhs) {
|
2017-09-04 04:53:24 +08:00
|
|
|
return rhs < lhs;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool lldb_private::operator>=(const lldb_private::UUID &lhs,
|
|
|
|
const lldb_private::UUID &rhs) {
|
2017-09-04 04:53:24 +08:00
|
|
|
return !(lhs < rhs);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|