blob: e84c13ef02b7aea9add250ed411426fd4dbbc369 [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
25UUID::UUID()
26{
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{
32 ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid));
33}
34
35UUID::UUID (const void *uuid_bytes, uint32_t num_uuid_bytes)
36{
37 if (uuid_bytes && num_uuid_bytes >= 16)
38 ::memcpy (m_uuid, uuid_bytes, sizeof (m_uuid));
39 else
Greg Clayton72b77eb2011-02-04 21:13:05 +000040 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041}
42
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043const UUID&
44UUID::operator=(const UUID& rhs)
45{
46 if (this != &rhs)
47 ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid));
48 return *this;
49}
50
51UUID::~UUID()
52{
53}
54
55void
56UUID::Clear()
57{
Greg Clayton72b77eb2011-02-04 21:13:05 +000058 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059}
60
61const void *
62UUID::GetBytes() const
63{
64 return m_uuid;
65}
66
Jason Molendac16b4af2013-05-03 23:56:12 +000067std::string
68UUID::GetAsString () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069{
Jason Molendac16b4af2013-05-03 23:56:12 +000070 std::string result;
71 char buf[64];
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 const uint8_t *u = (const uint8_t *)GetBytes();
Jason Molendac16b4af2013-05-03 23:56:12 +000073 if (sizeof (buf) > snprintf (buf,
74 sizeof (buf),
Greg Claytonc8f814d2012-09-27 03:13:55 +000075 "%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",
76 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]))
Jason Molendac16b4af2013-05-03 23:56:12 +000077 {
78 result.append (buf);
79 }
80 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081}
82
83void
84UUID::Dump (Stream *s) const
85{
86 const uint8_t *u = (const uint8_t *)GetBytes();
87 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",
88 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]);
89}
90
91void
92UUID::SetBytes (const void *uuid_bytes)
93{
94 if (uuid_bytes)
95 ::memcpy (m_uuid, uuid_bytes, sizeof (m_uuid));
96 else
Greg Clayton72b77eb2011-02-04 21:13:05 +000097 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098}
99
100size_t
101UUID::GetByteSize()
102{
103 return sizeof(UUID::ValueType);
104}
105
106bool
107UUID::IsValid () const
108{
109 return m_uuid[0] ||
110 m_uuid[1] ||
111 m_uuid[2] ||
112 m_uuid[3] ||
113 m_uuid[4] ||
114 m_uuid[5] ||
115 m_uuid[6] ||
116 m_uuid[7] ||
117 m_uuid[8] ||
118 m_uuid[9] ||
119 m_uuid[10] ||
120 m_uuid[11] ||
121 m_uuid[12] ||
122 m_uuid[13] ||
123 m_uuid[14] ||
124 m_uuid[15];
125}
126
127static inline int
128xdigit_to_int (char ch)
129{
130 ch = tolower(ch);
131 if (ch >= 'a' && ch <= 'f')
132 return 10 + ch - 'a';
133 return ch - '0';
134}
135
136size_t
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000137UUID::DecodeUUIDBytesFromCString (const char *p, ValueType &uuid_bytes, const char **end)
138{
139 size_t uuid_byte_idx = 0;
140 if (p)
141 {
142 while (*p)
143 {
144 if (isxdigit(p[0]) && isxdigit(p[1]))
145 {
146 int hi_nibble = xdigit_to_int(p[0]);
147 int lo_nibble = xdigit_to_int(p[1]);
148 // Translate the two hex nibble characters into a byte
149 uuid_bytes[uuid_byte_idx] = (hi_nibble << 4) + lo_nibble;
150
151 // Skip both hex digits
152 p += 2;
153
154 // Increment the byte that we are decoding within the UUID value
155 // and break out if we are done
156 if (++uuid_byte_idx == 16)
157 break;
158 }
159 else if (*p == '-')
160 {
161 // Skip dashes
162 p++;
163 }
164 else
165 {
166 // UUID values can only consist of hex characters and '-' chars
167 break;
168 }
169 }
170 }
171 if (end)
172 *end = p;
173 return uuid_byte_idx;
174}
175size_t
176UUID::SetFromCString (const char *cstr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177{
178 if (cstr == NULL)
179 return 0;
180
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 const char *p = cstr;
182
183 // Skip leading whitespace characters
184 while (isspace(*p))
185 ++p;
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000186
Greg Claytonc7bece562013-01-25 18:06:21 +0000187 const size_t uuid_byte_idx = UUID::DecodeUUIDBytesFromCString (p, m_uuid, &p);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 // If we successfully decoded a UUID, return the amount of characters that
190 // were consumed
191 if (uuid_byte_idx == 16)
192 return p - cstr;
193
194 // Else return zero to indicate we were not able to parse a UUID value
195 return 0;
196}
197
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198}
199
200bool
Greg Clayton60830262011-02-04 18:53:10 +0000201lldb_private::operator == (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
202{
203 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) == 0;
204}
205
206bool
207lldb_private::operator != (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208{
Greg Clayton95e31422011-02-05 02:56:16 +0000209 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210}
211
212bool
Greg Clayton60830262011-02-04 18:53:10 +0000213lldb_private::operator < (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214{
Greg Clayton60830262011-02-04 18:53:10 +0000215 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216}
217
218bool
Greg Clayton60830262011-02-04 18:53:10 +0000219lldb_private::operator <= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220{
Greg Clayton60830262011-02-04 18:53:10 +0000221 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) <= 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222}
223
224bool
Greg Clayton60830262011-02-04 18:53:10 +0000225lldb_private::operator > (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226{
Greg Clayton60830262011-02-04 18:53:10 +0000227 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) > 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228}
229
230bool
Greg Clayton60830262011-02-04 18:53:10 +0000231lldb_private::operator >= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232{
Greg Clayton60830262011-02-04 18:53:10 +0000233 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) >= 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234}