15ffd83dbSDimitry Andric //===-- UUID.cpp ----------------------------------------------------------===//
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 #include "lldb/Utility/UUID.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
120b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
130b57cec5SDimitry Andric #include "llvm/Support/Format.h"
140b57cec5SDimitry Andric 
15fe6060f1SDimitry Andric #include <cctype>
16fe6060f1SDimitry Andric #include <cstdio>
17fe6060f1SDimitry Andric #include <cstring>
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace lldb_private;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric // Whether to put a separator after count uuid bytes.
220b57cec5SDimitry Andric // For the first 16 bytes we follow the traditional UUID format. After that, we
230b57cec5SDimitry Andric // simply put a dash after every 6 bytes.
separate(size_t count)240b57cec5SDimitry Andric static inline bool separate(size_t count) {
250b57cec5SDimitry Andric   if (count >= 10)
260b57cec5SDimitry Andric     return (count - 10) % 6 == 0;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric   switch (count) {
290b57cec5SDimitry Andric   case 4:
300b57cec5SDimitry Andric   case 6:
310b57cec5SDimitry Andric   case 8:
320b57cec5SDimitry Andric     return true;
330b57cec5SDimitry Andric   default:
340b57cec5SDimitry Andric     return false;
350b57cec5SDimitry Andric   }
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
UUID(UUID::CvRecordPdb70 debug_info)38bdd1243dSDimitry Andric UUID::UUID(UUID::CvRecordPdb70 debug_info) {
39e8d8bef9SDimitry Andric   llvm::sys::swapByteOrder(debug_info.Uuid.Data1);
40e8d8bef9SDimitry Andric   llvm::sys::swapByteOrder(debug_info.Uuid.Data2);
41e8d8bef9SDimitry Andric   llvm::sys::swapByteOrder(debug_info.Uuid.Data3);
42e8d8bef9SDimitry Andric   llvm::sys::swapByteOrder(debug_info.Age);
43e8d8bef9SDimitry Andric   if (debug_info.Age)
44bdd1243dSDimitry Andric     *this = UUID(&debug_info, sizeof(debug_info));
45bdd1243dSDimitry Andric   else
46bdd1243dSDimitry Andric     *this = UUID(&debug_info.Uuid, sizeof(debug_info.Uuid));
47e8d8bef9SDimitry Andric }
48e8d8bef9SDimitry Andric 
GetAsString(llvm::StringRef separator) const490b57cec5SDimitry Andric std::string UUID::GetAsString(llvm::StringRef separator) const {
500b57cec5SDimitry Andric   std::string result;
510b57cec5SDimitry Andric   llvm::raw_string_ostream os(result);
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   for (auto B : llvm::enumerate(GetBytes())) {
540b57cec5SDimitry Andric     if (separate(B.index()))
550b57cec5SDimitry Andric       os << separator;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric     os << llvm::format_hex_no_prefix(B.value(), 2, true);
580b57cec5SDimitry Andric   }
590b57cec5SDimitry Andric   os.flush();
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   return result;
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
Dump(Stream & s) const6406c3fb27SDimitry Andric void UUID::Dump(Stream &s) const { s.PutCString(GetAsString()); }
650b57cec5SDimitry Andric 
xdigit_to_int(char ch)660b57cec5SDimitry Andric static inline int xdigit_to_int(char ch) {
670b57cec5SDimitry Andric   ch = tolower(ch);
680b57cec5SDimitry Andric   if (ch >= 'a' && ch <= 'f')
690b57cec5SDimitry Andric     return 10 + ch - 'a';
700b57cec5SDimitry Andric   return ch - '0';
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric llvm::StringRef
DecodeUUIDBytesFromString(llvm::StringRef p,llvm::SmallVectorImpl<uint8_t> & uuid_bytes)740b57cec5SDimitry Andric UUID::DecodeUUIDBytesFromString(llvm::StringRef p,
755ffd83dbSDimitry Andric                                 llvm::SmallVectorImpl<uint8_t> &uuid_bytes) {
760b57cec5SDimitry Andric   uuid_bytes.clear();
775ffd83dbSDimitry Andric   while (p.size() >= 2) {
780b57cec5SDimitry Andric     if (isxdigit(p[0]) && isxdigit(p[1])) {
790b57cec5SDimitry Andric       int hi_nibble = xdigit_to_int(p[0]);
800b57cec5SDimitry Andric       int lo_nibble = xdigit_to_int(p[1]);
810b57cec5SDimitry Andric       // Translate the two hex nibble characters into a byte
820b57cec5SDimitry Andric       uuid_bytes.push_back((hi_nibble << 4) + lo_nibble);
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric       // Skip both hex digits
850b57cec5SDimitry Andric       p = p.drop_front(2);
860b57cec5SDimitry Andric     } else if (p.front() == '-') {
870b57cec5SDimitry Andric       // Skip dashes
880b57cec5SDimitry Andric       p = p.drop_front();
890b57cec5SDimitry Andric     } else {
900b57cec5SDimitry Andric       // UUID values can only consist of hex characters and '-' chars
910b57cec5SDimitry Andric       break;
920b57cec5SDimitry Andric     }
930b57cec5SDimitry Andric   }
940b57cec5SDimitry Andric   return p;
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric 
SetFromStringRef(llvm::StringRef str)975ffd83dbSDimitry Andric bool UUID::SetFromStringRef(llvm::StringRef str) {
980b57cec5SDimitry Andric   llvm::StringRef p = str;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   // Skip leading whitespace characters
1010b57cec5SDimitry Andric   p = p.ltrim();
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   llvm::SmallVector<uint8_t, 20> bytes;
1045ffd83dbSDimitry Andric   llvm::StringRef rest = UUID::DecodeUUIDBytesFromString(p, bytes);
1050b57cec5SDimitry Andric 
1065ffd83dbSDimitry Andric   // Return false if we could not consume the entire string or if the parsed
1075ffd83dbSDimitry Andric   // UUID is empty.
1085ffd83dbSDimitry Andric   if (!rest.empty() || bytes.empty())
1095ffd83dbSDimitry Andric     return false;
1105ffd83dbSDimitry Andric 
111bdd1243dSDimitry Andric   *this = UUID(bytes);
1125ffd83dbSDimitry Andric   return true;
1130b57cec5SDimitry Andric }
114