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 <stddef.h>
15 #include <stdint.h>
16 #include <string>
17 
18 namespace lldb_private {
19 
20   class Stream;
21 
22 class UUID {
23 public:
24   UUID() = default;
25 
26   /// Creates a UUID from the data pointed to by the bytes argument. No special
27   /// significance is attached to any of the values.
28   static UUID fromData(const void *bytes, uint32_t num_bytes) {
29     if (bytes)
30       return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes});
31     return UUID();
32   }
33 
34   /// Creates a uuid from the data pointed to by the bytes argument. No special
35   /// significance is attached to any of the values.
36   static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); }
37 
38   /// Creates a UUID from the data pointed to by the bytes argument. Data
39   /// consisting purely of zero bytes is treated as an invalid UUID.
40   static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) {
41     if (bytes)
42       return fromOptionalData(
43           {reinterpret_cast<const uint8_t *>(bytes), num_bytes});
44     return UUID();
45   }
46 
47   /// Creates a UUID from the data pointed to by the bytes argument. Data
48   /// consisting purely of zero bytes is treated as an invalid UUID.
49   static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) {
50     if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; }))
51       return UUID();
52     return UUID(bytes);
53   }
54 
55   void Clear() { m_bytes.clear(); }
56 
57   void Dump(Stream *s) const;
58 
59   llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
60 
61   explicit operator bool() const { return IsValid(); }
62   bool IsValid() const { return !m_bytes.empty(); }
63 
64   std::string GetAsString(llvm::StringRef separator = "-") const;
65 
66   size_t SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes = 16);
67 
68   // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the
69   // UUID to invalid.
70   size_t SetFromOptionalStringRef(llvm::StringRef str,
71                                   uint32_t num_uuid_bytes = 16);
72 
73   // Decode as many UUID bytes (up to 16) as possible from the C string "cstr"
74   // This is used for auto completion where a partial UUID might have been
75   // typed in. It
76   /// Decode as many UUID bytes (up to 16) as possible from the C
77   /// string \a cstr.
78   ///
79   /// \param[in] str
80   ///     An llvm::StringRef that points at a UUID string value (no leading
81   ///     spaces). The string must contain only hex characters and optionally
82   ///     can contain the '-' sepearators.
83   ///
84   /// \param[in] uuid_bytes
85   ///     A buffer of bytes that will contain a full or partially decoded UUID.
86   ///
87   /// \return
88   ///     The original string, with all decoded bytes removed.
89   static llvm::StringRef
90   DecodeUUIDBytesFromString(llvm::StringRef str,
91                             llvm::SmallVectorImpl<uint8_t> &uuid_bytes,
92                             uint32_t num_uuid_bytes = 16);
93 
94 private:
95   UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {}
96 
97   // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
98   // for this case.
99   llvm::SmallVector<uint8_t, 20> m_bytes;
100 
101   friend bool operator==(const UUID &LHS, const UUID &RHS) {
102     return LHS.m_bytes == RHS.m_bytes;
103   }
104   friend bool operator!=(const UUID &LHS, const UUID &RHS) {
105     return !(LHS == RHS);
106   }
107   friend bool operator<(const UUID &LHS, const UUID &RHS) {
108     return LHS.m_bytes < RHS.m_bytes;
109   }
110   friend bool operator<=(const UUID &LHS, const UUID &RHS) {
111     return !(RHS < LHS);
112   }
113   friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
114   friend bool operator>=(const UUID &LHS, const UUID &RHS) {
115     return !(LHS < RHS);
116   }
117 };
118 } // namespace lldb_private
119 
120 #endif // LLDB_UTILITY_UUID_H
121