1 | //===-- UUID.h --------------------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLDB_UTILITY_UUID_H |
10 | #define LLDB_UTILITY_UUID_H |
11 | |
12 | #include "llvm/ADT/ArrayRef.h" |
13 | #include "llvm/ADT/StringRef.h" |
14 | #include "llvm/Support/Endian.h" |
15 | #include <stddef.h> |
16 | #include <stdint.h> |
17 | #include <string> |
18 | |
19 | namespace lldb_private { |
20 | |
21 | class Stream; |
22 | |
23 | class UUID { |
24 | public: |
25 | UUID() = default; |
26 | |
27 | // Reference: |
28 | // https://crashpad.chromium.org/doxygen/structcrashpad_1_1CodeViewRecordPDB70.html |
29 | struct CvRecordPdb70 { |
30 | struct { |
31 | llvm::support::ulittle32_t Data1; |
32 | llvm::support::ulittle16_t Data2; |
33 | llvm::support::ulittle16_t Data3; |
34 | uint8_t Data4[8]; |
35 | } Uuid; |
36 | llvm::support::ulittle32_t Age; |
37 | // char PDBFileName[]; |
38 | }; |
39 | |
40 | /// Create a UUID from CvRecordPdb70. |
41 | static UUID fromCvRecord(CvRecordPdb70 debug_info); |
42 | |
43 | /// Creates a UUID from the data pointed to by the bytes argument. No special |
44 | /// significance is attached to any of the values. |
45 | static UUID fromData(const void *bytes, uint32_t num_bytes) { |
46 | if (bytes) |
47 | return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes}); |
48 | return UUID(); |
49 | } |
50 | |
51 | /// Creates a uuid from the data pointed to by the bytes argument. No special |
52 | /// significance is attached to any of the values. |
53 | static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); } |
54 | |
55 | /// Creates a UUID from the data pointed to by the bytes argument. Data |
56 | /// consisting purely of zero bytes is treated as an invalid UUID. |
57 | static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) { |
58 | if (bytes) |
59 | return fromOptionalData( |
60 | {reinterpret_cast<const uint8_t *>(bytes), num_bytes}); |
61 | return UUID(); |
62 | } |
63 | |
64 | /// Creates a UUID from the data pointed to by the bytes argument. Data |
65 | /// consisting purely of zero bytes is treated as an invalid UUID. |
66 | static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) { |
67 | if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; })) |
68 | return UUID(); |
69 | return UUID(bytes); |
70 | } |
71 | |
72 | void Clear() { m_bytes.clear(); } |
73 | |
74 | void Dump(Stream *s) const; |
75 | |
76 | llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; } |
77 | |
78 | explicit operator bool() const { return IsValid(); } |
79 | bool IsValid() const { return !m_bytes.empty(); } |
80 | |
81 | std::string GetAsString(llvm::StringRef separator = "-" ) const; |
82 | |
83 | bool SetFromStringRef(llvm::StringRef str); |
84 | |
85 | // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the |
86 | // UUID to invalid. |
87 | bool SetFromOptionalStringRef(llvm::StringRef str); |
88 | |
89 | /// Decode as many UUID bytes as possible from the C string \a cstr. |
90 | /// |
91 | /// \param[in] str |
92 | /// An llvm::StringRef that points at a UUID string value (no leading |
93 | /// spaces). The string must contain only hex characters and optionally |
94 | /// can contain the '-' sepearators. |
95 | /// |
96 | /// \param[in] uuid_bytes |
97 | /// A buffer of bytes that will contain a full or partially decoded UUID. |
98 | /// |
99 | /// \return |
100 | /// The original string, with all decoded bytes removed. |
101 | static llvm::StringRef |
102 | DecodeUUIDBytesFromString(llvm::StringRef str, |
103 | llvm::SmallVectorImpl<uint8_t> &uuid_bytes); |
104 | |
105 | private: |
106 | UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {} |
107 | |
108 | // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations |
109 | // for this case. |
110 | llvm::SmallVector<uint8_t, 20> m_bytes; |
111 | |
112 | friend bool operator==(const UUID &LHS, const UUID &RHS) { |
113 | return LHS.m_bytes == RHS.m_bytes; |
114 | } |
115 | friend bool operator!=(const UUID &LHS, const UUID &RHS) { |
116 | return !(LHS == RHS); |
117 | } |
118 | friend bool operator<(const UUID &LHS, const UUID &RHS) { |
119 | return LHS.m_bytes < RHS.m_bytes; |
120 | } |
121 | friend bool operator<=(const UUID &LHS, const UUID &RHS) { |
122 | return !(RHS < LHS); |
123 | } |
124 | friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; } |
125 | friend bool operator>=(const UUID &LHS, const UUID &RHS) { |
126 | return !(LHS < RHS); |
127 | } |
128 | }; |
129 | } // namespace lldb_private |
130 | |
131 | #endif // LLDB_UTILITY_UUID_H |
132 | |