1 //===-- UUID.cpp ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/UUID.h"
19 // Other libraries and framework includes
21 #include "lldb/Core/Stream.h"
23 namespace lldb_private {
25 UUID::UUID() : m_num_uuid_bytes(16)
27 ::memset (m_uuid, 0, sizeof(m_uuid));
30 UUID::UUID(const UUID& rhs)
32 m_num_uuid_bytes = rhs.m_num_uuid_bytes;
33 ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid));
36 UUID::UUID (const void *uuid_bytes, uint32_t num_uuid_bytes)
38 SetBytes (uuid_bytes, num_uuid_bytes);
42 UUID::operator=(const UUID& rhs)
46 m_num_uuid_bytes = rhs.m_num_uuid_bytes;
47 ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid));
59 m_num_uuid_bytes = 16;
60 ::memset (m_uuid, 0, sizeof(m_uuid));
64 UUID::GetBytes() const
70 UUID::GetAsString (const char *separator) const
76 const uint8_t *u = (const uint8_t *)GetBytes();
77 if (sizeof (buf) > (size_t)snprintf (buf,
79 "%2.2X%2.2X%2.2X%2.2X%s%2.2X%2.2X%s%2.2X%2.2X%s%2.2X%2.2X%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
80 u[0],u[1],u[2],u[3],separator,
84 u[10],u[11],u[12],u[13],u[14],u[15]))
87 if (m_num_uuid_bytes == 20)
89 if (sizeof (buf) > (size_t)snprintf (buf, sizeof (buf), "%s%2.2X%2.2X%2.2X%2.2X", separator,u[16],u[17],u[18],u[19]))
97 UUID::Dump (Stream *s) const
99 const uint8_t *u = (const uint8_t *)GetBytes();
100 s->Printf ("%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
101 u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7],u[8],u[9],u[10],u[11],u[12],u[13],u[14],u[15]);
102 if (m_num_uuid_bytes == 20)
104 s->Printf ("-%2.2X%2.2X%2.2X%2.2X", u[16],u[17],u[18],u[19]);
109 UUID::SetBytes (const void *uuid_bytes, uint32_t num_uuid_bytes)
113 switch (num_uuid_bytes)
116 m_num_uuid_bytes = 20;
119 m_num_uuid_bytes = 16;
120 m_uuid[16] = m_uuid[17] = m_uuid[18] = m_uuid[19] = 0;
123 // Unsupported UUID byte size
124 m_num_uuid_bytes = 0;
128 if (m_num_uuid_bytes > 0)
130 ::memcpy (m_uuid, uuid_bytes, m_num_uuid_bytes);
134 ::memset (m_uuid, 0, sizeof(m_uuid));
141 return m_num_uuid_bytes;
145 UUID::IsValid () const
170 xdigit_to_int (char ch)
173 if (ch >= 'a' && ch <= 'f')
174 return 10 + ch - 'a';
179 UUID::DecodeUUIDBytesFromCString (const char *p, ValueType &uuid_bytes, const char **end, uint32_t num_uuid_bytes)
181 size_t uuid_byte_idx = 0;
186 if (isxdigit(p[0]) && isxdigit(p[1]))
188 int hi_nibble = xdigit_to_int(p[0]);
189 int lo_nibble = xdigit_to_int(p[1]);
190 // Translate the two hex nibble characters into a byte
191 uuid_bytes[uuid_byte_idx] = (hi_nibble << 4) + lo_nibble;
193 // Skip both hex digits
196 // Increment the byte that we are decoding within the UUID value
197 // and break out if we are done
198 if (++uuid_byte_idx == num_uuid_bytes)
208 // UUID values can only consist of hex characters and '-' chars
215 // Clear trailing bytes to 0.
216 for (uint32_t i = uuid_byte_idx; i < sizeof(ValueType); i++)
218 return uuid_byte_idx;
221 UUID::SetFromCString (const char *cstr, uint32_t num_uuid_bytes)
226 const char *p = cstr;
228 // Skip leading whitespace characters
232 const size_t uuid_byte_idx = UUID::DecodeUUIDBytesFromCString (p, m_uuid, &p, num_uuid_bytes);
234 // If we successfully decoded a UUID, return the amount of characters that
236 if (uuid_byte_idx == num_uuid_bytes)
239 // Else return zero to indicate we were not able to parse a UUID value
246 lldb_private::operator == (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
248 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) == 0;
252 lldb_private::operator != (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
254 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) != 0;
258 lldb_private::operator < (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
260 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) < 0;
264 lldb_private::operator <= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
266 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) <= 0;
270 lldb_private::operator > (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
272 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) > 0;
276 lldb_private::operator >= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
278 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) >= 0;