blob: 8b2158b88e26a62149cdbc9d2dc4a0d39e18c6f6 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- UUID.cpp ------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/UUID.h"
11// C Includes
Greg Claytoneee5f1f2010-06-09 19:36:54 +000012#include <string.h>
13#include <stdio.h>
14#include <ctype.h>
15
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016// C++ Includes
Jason Molendac16b4af2013-05-03 23:56:12 +000017#include <string>
18
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019// Other libraries and framework includes
20// Project includes
21#include "lldb/Core/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
Greg Clayton60830262011-02-04 18:53:10 +000023namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
Michael Sartainc836ae72013-05-23 20:57:03 +000025UUID::UUID() : m_num_uuid_bytes(16)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026{
Greg Clayton72b77eb2011-02-04 21:13:05 +000027 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028}
29
30UUID::UUID(const UUID& rhs)
31{
Michael Sartainc836ae72013-05-23 20:57:03 +000032 m_num_uuid_bytes = rhs.m_num_uuid_bytes;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033 ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid));
34}
35
36UUID::UUID (const void *uuid_bytes, uint32_t num_uuid_bytes)
37{
Michael Sartainc836ae72013-05-23 20:57:03 +000038 SetBytes (uuid_bytes, num_uuid_bytes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039}
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041const UUID&
42UUID::operator=(const UUID& rhs)
43{
44 if (this != &rhs)
Michael Sartainc836ae72013-05-23 20:57:03 +000045 {
46 m_num_uuid_bytes = rhs.m_num_uuid_bytes;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047 ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid));
Michael Sartainc836ae72013-05-23 20:57:03 +000048 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049 return *this;
50}
51
52UUID::~UUID()
53{
54}
55
56void
57UUID::Clear()
58{
Michael Sartainc836ae72013-05-23 20:57:03 +000059 m_num_uuid_bytes = 16;
Greg Clayton72b77eb2011-02-04 21:13:05 +000060 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061}
62
63const void *
64UUID::GetBytes() const
65{
66 return m_uuid;
67}
68
Jason Molendac16b4af2013-05-03 23:56:12 +000069std::string
Michael Sartaina7499c92013-07-01 19:45:50 +000070UUID::GetAsString (const char *separator) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071{
Jason Molendac16b4af2013-05-03 23:56:12 +000072 std::string result;
Michael Sartaina7499c92013-07-01 19:45:50 +000073 char buf[256];
74 if (!separator)
75 separator = "-";
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076 const uint8_t *u = (const uint8_t *)GetBytes();
Michael Sartainc836ae72013-05-23 20:57:03 +000077 if (sizeof (buf) > (size_t)snprintf (buf,
Jason Molendac16b4af2013-05-03 23:56:12 +000078 sizeof (buf),
Michael Sartaina7499c92013-07-01 19:45:50 +000079 "%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,
81 u[4],u[5],separator,
82 u[6],u[7],separator,
83 u[8],u[9],separator,
84 u[10],u[11],u[12],u[13],u[14],u[15]))
Jason Molendac16b4af2013-05-03 23:56:12 +000085 {
86 result.append (buf);
Michael Sartainc836ae72013-05-23 20:57:03 +000087 if (m_num_uuid_bytes == 20)
88 {
Michael Sartaina7499c92013-07-01 19:45:50 +000089 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]))
Michael Sartainc836ae72013-05-23 20:57:03 +000090 result.append (buf);
91 }
Jason Molendac16b4af2013-05-03 23:56:12 +000092 }
93 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094}
95
96void
97UUID::Dump (Stream *s) const
98{
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]);
Michael Sartainc836ae72013-05-23 20:57:03 +0000102 if (m_num_uuid_bytes == 20)
103 {
104 s->Printf ("-%2.2X%2.2X%2.2X%2.2X", u[16],u[17],u[18],u[19]);
105 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106}
107
108void
Michael Sartainc836ae72013-05-23 20:57:03 +0000109UUID::SetBytes (const void *uuid_bytes, uint32_t num_uuid_bytes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110{
Michael Sartainc836ae72013-05-23 20:57:03 +0000111 if (uuid_bytes && num_uuid_bytes >= 20)
112 {
113 m_num_uuid_bytes = 20;
114 ::memcpy (m_uuid, uuid_bytes, m_num_uuid_bytes);
115 }
116 else if (uuid_bytes && num_uuid_bytes >= 16)
117 {
118 m_num_uuid_bytes = 16;
119 ::memcpy (m_uuid, uuid_bytes, m_num_uuid_bytes);
120 m_uuid[16] = m_uuid[17] = m_uuid[18] = m_uuid[19] = 0;
121 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 else
Michael Sartainc836ae72013-05-23 20:57:03 +0000123 {
124 m_num_uuid_bytes = 16;
Greg Clayton72b77eb2011-02-04 21:13:05 +0000125 ::memset (m_uuid, 0, sizeof(m_uuid));
Michael Sartainc836ae72013-05-23 20:57:03 +0000126 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127}
128
129size_t
130UUID::GetByteSize()
131{
Michael Sartainc836ae72013-05-23 20:57:03 +0000132 return m_num_uuid_bytes;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133}
134
135bool
136UUID::IsValid () const
137{
138 return m_uuid[0] ||
139 m_uuid[1] ||
140 m_uuid[2] ||
141 m_uuid[3] ||
142 m_uuid[4] ||
143 m_uuid[5] ||
144 m_uuid[6] ||
145 m_uuid[7] ||
146 m_uuid[8] ||
147 m_uuid[9] ||
148 m_uuid[10] ||
149 m_uuid[11] ||
150 m_uuid[12] ||
151 m_uuid[13] ||
152 m_uuid[14] ||
Michael Sartainc836ae72013-05-23 20:57:03 +0000153 m_uuid[15] ||
154 m_uuid[16] ||
155 m_uuid[17] ||
156 m_uuid[18] ||
157 m_uuid[19];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
160static inline int
161xdigit_to_int (char ch)
162{
163 ch = tolower(ch);
164 if (ch >= 'a' && ch <= 'f')
165 return 10 + ch - 'a';
166 return ch - '0';
167}
168
169size_t
Michael Sartainc836ae72013-05-23 20:57:03 +0000170UUID::DecodeUUIDBytesFromCString (const char *p, ValueType &uuid_bytes, const char **end, uint32_t num_uuid_bytes)
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000171{
172 size_t uuid_byte_idx = 0;
173 if (p)
174 {
175 while (*p)
176 {
177 if (isxdigit(p[0]) && isxdigit(p[1]))
178 {
179 int hi_nibble = xdigit_to_int(p[0]);
180 int lo_nibble = xdigit_to_int(p[1]);
181 // Translate the two hex nibble characters into a byte
182 uuid_bytes[uuid_byte_idx] = (hi_nibble << 4) + lo_nibble;
183
184 // Skip both hex digits
185 p += 2;
186
187 // Increment the byte that we are decoding within the UUID value
188 // and break out if we are done
Michael Sartainc836ae72013-05-23 20:57:03 +0000189 if (++uuid_byte_idx == num_uuid_bytes)
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000190 break;
191 }
192 else if (*p == '-')
193 {
194 // Skip dashes
195 p++;
196 }
197 else
198 {
199 // UUID values can only consist of hex characters and '-' chars
200 break;
201 }
202 }
203 }
204 if (end)
205 *end = p;
Michael Sartainc836ae72013-05-23 20:57:03 +0000206 // Clear trailing bytes to 0.
207 for (uint32_t i = uuid_byte_idx; i < sizeof(ValueType); i++)
208 uuid_bytes[i] = 0;
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000209 return uuid_byte_idx;
210}
211size_t
Michael Sartainc836ae72013-05-23 20:57:03 +0000212UUID::SetFromCString (const char *cstr, uint32_t num_uuid_bytes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213{
214 if (cstr == NULL)
215 return 0;
216
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217 const char *p = cstr;
218
219 // Skip leading whitespace characters
220 while (isspace(*p))
221 ++p;
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000222
Michael Sartainc836ae72013-05-23 20:57:03 +0000223 const size_t uuid_byte_idx = UUID::DecodeUUIDBytesFromCString (p, m_uuid, &p, num_uuid_bytes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 // If we successfully decoded a UUID, return the amount of characters that
226 // were consumed
Michael Sartainc836ae72013-05-23 20:57:03 +0000227 if (uuid_byte_idx == num_uuid_bytes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228 return p - cstr;
229
230 // Else return zero to indicate we were not able to parse a UUID value
231 return 0;
232}
233
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234}
235
236bool
Greg Clayton60830262011-02-04 18:53:10 +0000237lldb_private::operator == (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
238{
Michael Sartainc836ae72013-05-23 20:57:03 +0000239 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) == 0;
Greg Clayton60830262011-02-04 18:53:10 +0000240}
241
242bool
243lldb_private::operator != (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244{
Michael Sartainc836ae72013-05-23 20:57:03 +0000245 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
248bool
Greg Clayton60830262011-02-04 18:53:10 +0000249lldb_private::operator < (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250{
Michael Sartainc836ae72013-05-23 20:57:03 +0000251 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252}
253
254bool
Greg Clayton60830262011-02-04 18:53:10 +0000255lldb_private::operator <= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256{
Michael Sartainc836ae72013-05-23 20:57:03 +0000257 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) <= 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258}
259
260bool
Greg Clayton60830262011-02-04 18:53:10 +0000261lldb_private::operator > (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262{
Michael Sartainc836ae72013-05-23 20:57:03 +0000263 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) > 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264}
265
266bool
Greg Clayton60830262011-02-04 18:53:10 +0000267lldb_private::operator >= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268{
Michael Sartainc836ae72013-05-23 20:57:03 +0000269 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) >= 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270}