blob: c1b3eb13d656377b2b9962a430230e0f893c8a55 [file] [log] [blame]
Chris Lattner24943d22010-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 Clayton19365462010-06-09 19:36:54 +000012#include <string.h>
13#include <stdio.h>
14#include <ctype.h>
15
Chris Lattner24943d22010-06-08 16:52:24 +000016// C++ Includes
Jason Molenda42b336c2013-05-03 23:56:12 +000017#include <string>
18
Chris Lattner24943d22010-06-08 16:52:24 +000019// Other libraries and framework includes
20// Project includes
21#include "lldb/Core/Stream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022
Greg Clayton0467c782011-02-04 18:53:10 +000023namespace lldb_private {
Chris Lattner24943d22010-06-08 16:52:24 +000024
Michael Sartain0be9b3b2013-05-23 20:57:03 +000025UUID::UUID() : m_num_uuid_bytes(16)
Chris Lattner24943d22010-06-08 16:52:24 +000026{
Greg Claytonddff7cc2011-02-04 21:13:05 +000027 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner24943d22010-06-08 16:52:24 +000028}
29
30UUID::UUID(const UUID& rhs)
31{
Michael Sartain0be9b3b2013-05-23 20:57:03 +000032 m_num_uuid_bytes = rhs.m_num_uuid_bytes;
Chris Lattner24943d22010-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 Sartain0be9b3b2013-05-23 20:57:03 +000038 SetBytes (uuid_bytes, num_uuid_bytes);
Chris Lattner24943d22010-06-08 16:52:24 +000039}
40
Chris Lattner24943d22010-06-08 16:52:24 +000041const UUID&
42UUID::operator=(const UUID& rhs)
43{
44 if (this != &rhs)
Michael Sartain0be9b3b2013-05-23 20:57:03 +000045 {
46 m_num_uuid_bytes = rhs.m_num_uuid_bytes;
Chris Lattner24943d22010-06-08 16:52:24 +000047 ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid));
Michael Sartain0be9b3b2013-05-23 20:57:03 +000048 }
Chris Lattner24943d22010-06-08 16:52:24 +000049 return *this;
50}
51
52UUID::~UUID()
53{
54}
55
56void
57UUID::Clear()
58{
Michael Sartain0be9b3b2013-05-23 20:57:03 +000059 m_num_uuid_bytes = 16;
Greg Claytonddff7cc2011-02-04 21:13:05 +000060 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner24943d22010-06-08 16:52:24 +000061}
62
63const void *
64UUID::GetBytes() const
65{
66 return m_uuid;
67}
68
Jason Molenda42b336c2013-05-03 23:56:12 +000069std::string
Michael Sartaina807cee2013-07-01 19:45:50 +000070UUID::GetAsString (const char *separator) const
Chris Lattner24943d22010-06-08 16:52:24 +000071{
Jason Molenda42b336c2013-05-03 23:56:12 +000072 std::string result;
Michael Sartaina807cee2013-07-01 19:45:50 +000073 char buf[256];
74 if (!separator)
75 separator = "-";
Chris Lattner24943d22010-06-08 16:52:24 +000076 const uint8_t *u = (const uint8_t *)GetBytes();
Michael Sartain0be9b3b2013-05-23 20:57:03 +000077 if (sizeof (buf) > (size_t)snprintf (buf,
Jason Molenda42b336c2013-05-03 23:56:12 +000078 sizeof (buf),
Michael Sartaina807cee2013-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 Molenda42b336c2013-05-03 23:56:12 +000085 {
86 result.append (buf);
Michael Sartain0be9b3b2013-05-23 20:57:03 +000087 if (m_num_uuid_bytes == 20)
88 {
Michael Sartaina807cee2013-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 Sartain0be9b3b2013-05-23 20:57:03 +000090 result.append (buf);
91 }
Jason Molenda42b336c2013-05-03 23:56:12 +000092 }
93 return result;
Chris Lattner24943d22010-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 Sartain0be9b3b2013-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 Lattner24943d22010-06-08 16:52:24 +0000106}
107
Greg Claytonf9215ba2013-07-08 22:22:41 +0000108bool
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000109UUID::SetBytes (const void *uuid_bytes, uint32_t num_uuid_bytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000110{
Greg Claytonf9215ba2013-07-08 22:22:41 +0000111 if (uuid_bytes)
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000112 {
Greg Claytonf9215ba2013-07-08 22:22:41 +0000113 switch (num_uuid_bytes)
114 {
115 case 20:
116 m_num_uuid_bytes = 20;
117 break;
118 case 16:
119 m_num_uuid_bytes = 16;
120 m_uuid[16] = m_uuid[17] = m_uuid[18] = m_uuid[19] = 0;
121 break;
122 default:
123 // Unsupported UUID byte size
124 m_num_uuid_bytes = 0;
125 break;
126 }
127
128 if (m_num_uuid_bytes > 0)
129 {
130 ::memcpy (m_uuid, uuid_bytes, m_num_uuid_bytes);
131 return true;
132 }
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000133 }
Greg Claytonf9215ba2013-07-08 22:22:41 +0000134 ::memset (m_uuid, 0, sizeof(m_uuid));
135 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000136}
137
138size_t
139UUID::GetByteSize()
140{
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000141 return m_num_uuid_bytes;
Chris Lattner24943d22010-06-08 16:52:24 +0000142}
143
144bool
145UUID::IsValid () const
146{
147 return m_uuid[0] ||
148 m_uuid[1] ||
149 m_uuid[2] ||
150 m_uuid[3] ||
151 m_uuid[4] ||
152 m_uuid[5] ||
153 m_uuid[6] ||
154 m_uuid[7] ||
155 m_uuid[8] ||
156 m_uuid[9] ||
157 m_uuid[10] ||
158 m_uuid[11] ||
159 m_uuid[12] ||
160 m_uuid[13] ||
161 m_uuid[14] ||
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000162 m_uuid[15] ||
163 m_uuid[16] ||
164 m_uuid[17] ||
165 m_uuid[18] ||
166 m_uuid[19];
Chris Lattner24943d22010-06-08 16:52:24 +0000167}
168
169static inline int
170xdigit_to_int (char ch)
171{
172 ch = tolower(ch);
173 if (ch >= 'a' && ch <= 'f')
174 return 10 + ch - 'a';
175 return ch - '0';
176}
177
178size_t
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000179UUID::DecodeUUIDBytesFromCString (const char *p, ValueType &uuid_bytes, const char **end, uint32_t num_uuid_bytes)
Greg Clayton437b5bc2012-09-27 22:26:11 +0000180{
181 size_t uuid_byte_idx = 0;
182 if (p)
183 {
184 while (*p)
185 {
186 if (isxdigit(p[0]) && isxdigit(p[1]))
187 {
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;
192
193 // Skip both hex digits
194 p += 2;
195
196 // Increment the byte that we are decoding within the UUID value
197 // and break out if we are done
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000198 if (++uuid_byte_idx == num_uuid_bytes)
Greg Clayton437b5bc2012-09-27 22:26:11 +0000199 break;
200 }
201 else if (*p == '-')
202 {
203 // Skip dashes
204 p++;
205 }
206 else
207 {
208 // UUID values can only consist of hex characters and '-' chars
209 break;
210 }
211 }
212 }
213 if (end)
214 *end = p;
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000215 // Clear trailing bytes to 0.
216 for (uint32_t i = uuid_byte_idx; i < sizeof(ValueType); i++)
217 uuid_bytes[i] = 0;
Greg Clayton437b5bc2012-09-27 22:26:11 +0000218 return uuid_byte_idx;
219}
220size_t
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000221UUID::SetFromCString (const char *cstr, uint32_t num_uuid_bytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000222{
223 if (cstr == NULL)
224 return 0;
225
Chris Lattner24943d22010-06-08 16:52:24 +0000226 const char *p = cstr;
227
228 // Skip leading whitespace characters
229 while (isspace(*p))
230 ++p;
Greg Clayton437b5bc2012-09-27 22:26:11 +0000231
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000232 const size_t uuid_byte_idx = UUID::DecodeUUIDBytesFromCString (p, m_uuid, &p, num_uuid_bytes);
Chris Lattner24943d22010-06-08 16:52:24 +0000233
Chris Lattner24943d22010-06-08 16:52:24 +0000234 // If we successfully decoded a UUID, return the amount of characters that
235 // were consumed
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000236 if (uuid_byte_idx == num_uuid_bytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000237 return p - cstr;
238
239 // Else return zero to indicate we were not able to parse a UUID value
240 return 0;
241}
242
Chris Lattner24943d22010-06-08 16:52:24 +0000243}
244
245bool
Greg Clayton0467c782011-02-04 18:53:10 +0000246lldb_private::operator == (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
247{
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000248 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) == 0;
Greg Clayton0467c782011-02-04 18:53:10 +0000249}
250
251bool
252lldb_private::operator != (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner24943d22010-06-08 16:52:24 +0000253{
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000254 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) != 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000255}
256
257bool
Greg Clayton0467c782011-02-04 18:53:10 +0000258lldb_private::operator < (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner24943d22010-06-08 16:52:24 +0000259{
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000260 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) < 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000261}
262
263bool
Greg Clayton0467c782011-02-04 18:53:10 +0000264lldb_private::operator <= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner24943d22010-06-08 16:52:24 +0000265{
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000266 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) <= 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000267}
268
269bool
Greg Clayton0467c782011-02-04 18:53:10 +0000270lldb_private::operator > (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner24943d22010-06-08 16:52:24 +0000271{
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000272 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) > 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000273}
274
275bool
Greg Clayton0467c782011-02-04 18:53:10 +0000276lldb_private::operator >= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner24943d22010-06-08 16:52:24 +0000277{
Michael Sartain0be9b3b2013-05-23 20:57:03 +0000278 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) >= 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000279}