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" 140b57cec5SDimitry Andric #include <stddef.h> 150b57cec5SDimitry Andric #include <stdint.h> 160b57cec5SDimitry Andric #include <string> 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric namespace lldb_private { 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric class Stream; 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric class UUID { 230b57cec5SDimitry Andric public: 240b57cec5SDimitry Andric UUID() = default; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric /// Creates a UUID from the data pointed to by the bytes argument. No special 270b57cec5SDimitry Andric /// significance is attached to any of the values. 280b57cec5SDimitry Andric static UUID fromData(const void *bytes, uint32_t num_bytes) { 290b57cec5SDimitry Andric if (bytes) 300b57cec5SDimitry Andric return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes}); 310b57cec5SDimitry Andric return UUID(); 320b57cec5SDimitry Andric } 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric /// Creates a uuid from the data pointed to by the bytes argument. No special 350b57cec5SDimitry Andric /// significance is attached to any of the values. 360b57cec5SDimitry Andric static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); } 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric /// Creates a UUID from the data pointed to by the bytes argument. Data 390b57cec5SDimitry Andric /// consisting purely of zero bytes is treated as an invalid UUID. 400b57cec5SDimitry Andric static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) { 410b57cec5SDimitry Andric if (bytes) 420b57cec5SDimitry Andric return fromOptionalData( 430b57cec5SDimitry Andric {reinterpret_cast<const uint8_t *>(bytes), num_bytes}); 440b57cec5SDimitry Andric return UUID(); 450b57cec5SDimitry Andric } 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric /// Creates a UUID from the data pointed to by the bytes argument. Data 480b57cec5SDimitry Andric /// consisting purely of zero bytes is treated as an invalid UUID. 490b57cec5SDimitry Andric static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) { 500b57cec5SDimitry Andric if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; })) 510b57cec5SDimitry Andric return UUID(); 520b57cec5SDimitry Andric return UUID(bytes); 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric void Clear() { m_bytes.clear(); } 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric void Dump(Stream *s) const; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric explicit operator bool() const { return IsValid(); } 620b57cec5SDimitry Andric bool IsValid() const { return !m_bytes.empty(); } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric std::string GetAsString(llvm::StringRef separator = "-") const; 650b57cec5SDimitry Andric 66*5ffd83dbSDimitry Andric bool SetFromStringRef(llvm::StringRef str); 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the 690b57cec5SDimitry Andric // UUID to invalid. 70*5ffd83dbSDimitry Andric bool SetFromOptionalStringRef(llvm::StringRef str); 710b57cec5SDimitry Andric 72*5ffd83dbSDimitry Andric /// Decode as many UUID bytes as possible from the C string \a cstr. 730b57cec5SDimitry Andric /// 749dba64beSDimitry Andric /// \param[in] str 759dba64beSDimitry Andric /// An llvm::StringRef that points at a UUID string value (no leading 769dba64beSDimitry Andric /// spaces). The string must contain only hex characters and optionally 779dba64beSDimitry Andric /// can contain the '-' sepearators. 780b57cec5SDimitry Andric /// 790b57cec5SDimitry Andric /// \param[in] uuid_bytes 809dba64beSDimitry Andric /// A buffer of bytes that will contain a full or partially decoded UUID. 810b57cec5SDimitry Andric /// 820b57cec5SDimitry Andric /// \return 830b57cec5SDimitry Andric /// The original string, with all decoded bytes removed. 840b57cec5SDimitry Andric static llvm::StringRef 850b57cec5SDimitry Andric DecodeUUIDBytesFromString(llvm::StringRef str, 86*5ffd83dbSDimitry Andric llvm::SmallVectorImpl<uint8_t> &uuid_bytes); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric private: 890b57cec5SDimitry Andric UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {} 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations 920b57cec5SDimitry Andric // for this case. 930b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 20> m_bytes; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric friend bool operator==(const UUID &LHS, const UUID &RHS) { 960b57cec5SDimitry Andric return LHS.m_bytes == RHS.m_bytes; 970b57cec5SDimitry Andric } 980b57cec5SDimitry Andric friend bool operator!=(const UUID &LHS, const UUID &RHS) { 990b57cec5SDimitry Andric return !(LHS == RHS); 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric friend bool operator<(const UUID &LHS, const UUID &RHS) { 1020b57cec5SDimitry Andric return LHS.m_bytes < RHS.m_bytes; 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric friend bool operator<=(const UUID &LHS, const UUID &RHS) { 1050b57cec5SDimitry Andric return !(RHS < LHS); 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; } 1080b57cec5SDimitry Andric friend bool operator>=(const UUID &LHS, const UUID &RHS) { 1090b57cec5SDimitry Andric return !(LHS < RHS); 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric }; 1120b57cec5SDimitry Andric } // namespace lldb_private 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric #endif // LLDB_UTILITY_UUID_H 115