10b57cec5SDimitry Andric //===-- UUID.h --------------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef LLDB_UTILITY_UUID_H 100b57cec5SDimitry Andric #define LLDB_UTILITY_UUID_H 110b57cec5SDimitry Andric 129dba64beSDimitry Andric #include "llvm/ADT/ArrayRef.h" 139dba64beSDimitry Andric #include "llvm/ADT/StringRef.h" 14*e8d8bef9SDimitry Andric #include "llvm/Support/Endian.h" 150b57cec5SDimitry Andric #include <stddef.h> 160b57cec5SDimitry Andric #include <stdint.h> 170b57cec5SDimitry Andric #include <string> 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric namespace lldb_private { 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric class Stream; 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric class UUID { 240b57cec5SDimitry Andric public: 250b57cec5SDimitry Andric UUID() = default; 260b57cec5SDimitry Andric 27*e8d8bef9SDimitry Andric // Reference: 28*e8d8bef9SDimitry Andric // https://crashpad.chromium.org/doxygen/structcrashpad_1_1CodeViewRecordPDB70.html 29*e8d8bef9SDimitry Andric struct CvRecordPdb70 { 30*e8d8bef9SDimitry Andric struct { 31*e8d8bef9SDimitry Andric llvm::support::ulittle32_t Data1; 32*e8d8bef9SDimitry Andric llvm::support::ulittle16_t Data2; 33*e8d8bef9SDimitry Andric llvm::support::ulittle16_t Data3; 34*e8d8bef9SDimitry Andric uint8_t Data4[8]; 35*e8d8bef9SDimitry Andric } Uuid; 36*e8d8bef9SDimitry Andric llvm::support::ulittle32_t Age; 37*e8d8bef9SDimitry Andric // char PDBFileName[]; 38*e8d8bef9SDimitry Andric }; 39*e8d8bef9SDimitry Andric 40*e8d8bef9SDimitry Andric /// Create a UUID from CvRecordPdb70. 41*e8d8bef9SDimitry Andric static UUID fromCvRecord(CvRecordPdb70 debug_info); 42*e8d8bef9SDimitry Andric 430b57cec5SDimitry Andric /// Creates a UUID from the data pointed to by the bytes argument. No special 440b57cec5SDimitry Andric /// significance is attached to any of the values. 450b57cec5SDimitry Andric static UUID fromData(const void *bytes, uint32_t num_bytes) { 460b57cec5SDimitry Andric if (bytes) 470b57cec5SDimitry Andric return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes}); 480b57cec5SDimitry Andric return UUID(); 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric /// Creates a uuid from the data pointed to by the bytes argument. No special 520b57cec5SDimitry Andric /// significance is attached to any of the values. 530b57cec5SDimitry Andric static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric /// Creates a UUID from the data pointed to by the bytes argument. Data 560b57cec5SDimitry Andric /// consisting purely of zero bytes is treated as an invalid UUID. 570b57cec5SDimitry Andric static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) { 580b57cec5SDimitry Andric if (bytes) 590b57cec5SDimitry Andric return fromOptionalData( 600b57cec5SDimitry Andric {reinterpret_cast<const uint8_t *>(bytes), num_bytes}); 610b57cec5SDimitry Andric return UUID(); 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric /// Creates a UUID from the data pointed to by the bytes argument. Data 650b57cec5SDimitry Andric /// consisting purely of zero bytes is treated as an invalid UUID. 660b57cec5SDimitry Andric static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) { 670b57cec5SDimitry Andric if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; })) 680b57cec5SDimitry Andric return UUID(); 690b57cec5SDimitry Andric return UUID(bytes); 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric void Clear() { m_bytes.clear(); } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric void Dump(Stream *s) const; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric explicit operator bool() const { return IsValid(); } 790b57cec5SDimitry Andric bool IsValid() const { return !m_bytes.empty(); } 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric std::string GetAsString(llvm::StringRef separator = "-") const; 820b57cec5SDimitry Andric 835ffd83dbSDimitry Andric bool SetFromStringRef(llvm::StringRef str); 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the 860b57cec5SDimitry Andric // UUID to invalid. 875ffd83dbSDimitry Andric bool SetFromOptionalStringRef(llvm::StringRef str); 880b57cec5SDimitry Andric 895ffd83dbSDimitry Andric /// Decode as many UUID bytes as possible from the C string \a cstr. 900b57cec5SDimitry Andric /// 919dba64beSDimitry Andric /// \param[in] str 929dba64beSDimitry Andric /// An llvm::StringRef that points at a UUID string value (no leading 939dba64beSDimitry Andric /// spaces). The string must contain only hex characters and optionally 949dba64beSDimitry Andric /// can contain the '-' sepearators. 950b57cec5SDimitry Andric /// 960b57cec5SDimitry Andric /// \param[in] uuid_bytes 979dba64beSDimitry Andric /// A buffer of bytes that will contain a full or partially decoded UUID. 980b57cec5SDimitry Andric /// 990b57cec5SDimitry Andric /// \return 1000b57cec5SDimitry Andric /// The original string, with all decoded bytes removed. 1010b57cec5SDimitry Andric static llvm::StringRef 1020b57cec5SDimitry Andric DecodeUUIDBytesFromString(llvm::StringRef str, 1035ffd83dbSDimitry Andric llvm::SmallVectorImpl<uint8_t> &uuid_bytes); 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric private: 1060b57cec5SDimitry Andric UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {} 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations 1090b57cec5SDimitry Andric // for this case. 1100b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 20> m_bytes; 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric friend bool operator==(const UUID &LHS, const UUID &RHS) { 1130b57cec5SDimitry Andric return LHS.m_bytes == RHS.m_bytes; 1140b57cec5SDimitry Andric } 1150b57cec5SDimitry Andric friend bool operator!=(const UUID &LHS, const UUID &RHS) { 1160b57cec5SDimitry Andric return !(LHS == RHS); 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric friend bool operator<(const UUID &LHS, const UUID &RHS) { 1190b57cec5SDimitry Andric return LHS.m_bytes < RHS.m_bytes; 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric friend bool operator<=(const UUID &LHS, const UUID &RHS) { 1220b57cec5SDimitry Andric return !(RHS < LHS); 1230b57cec5SDimitry Andric } 1240b57cec5SDimitry Andric friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; } 1250b57cec5SDimitry Andric friend bool operator>=(const UUID &LHS, const UUID &RHS) { 1260b57cec5SDimitry Andric return !(LHS < RHS); 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric }; 1290b57cec5SDimitry Andric } // namespace lldb_private 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric #endif // LLDB_UTILITY_UUID_H 132