blob: bfd6c1890326542cacddc2a6bd2f335a0465058b [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
17// Other libraries and framework includes
18// Project includes
19#include "lldb/Core/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
Greg Clayton60830262011-02-04 18:53:10 +000021namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23UUID::UUID()
24{
Greg Clayton72b77eb2011-02-04 21:13:05 +000025 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026}
27
28UUID::UUID(const UUID& rhs)
29{
30 ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid));
31}
32
33UUID::UUID (const void *uuid_bytes, uint32_t num_uuid_bytes)
34{
35 if (uuid_bytes && num_uuid_bytes >= 16)
36 ::memcpy (m_uuid, uuid_bytes, sizeof (m_uuid));
37 else
Greg Clayton72b77eb2011-02-04 21:13:05 +000038 ::memset (m_uuid, 0, sizeof(m_uuid));
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)
45 ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid));
46 return *this;
47}
48
49UUID::~UUID()
50{
51}
52
53void
54UUID::Clear()
55{
Greg Clayton72b77eb2011-02-04 21:13:05 +000056 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057}
58
59const void *
60UUID::GetBytes() const
61{
62 return m_uuid;
63}
64
65char *
66UUID::GetAsCString (char *dst, size_t dst_len) const
67{
68 const uint8_t *u = (const uint8_t *)GetBytes();
Greg Claytonc8f814d2012-09-27 03:13:55 +000069 if (dst_len > snprintf (dst,
70 dst_len,
71 "%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",
72 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]))
73 return dst;
74 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075}
76
77void
78UUID::Dump (Stream *s) const
79{
80 const uint8_t *u = (const uint8_t *)GetBytes();
81 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",
82 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]);
83}
84
85void
86UUID::SetBytes (const void *uuid_bytes)
87{
88 if (uuid_bytes)
89 ::memcpy (m_uuid, uuid_bytes, sizeof (m_uuid));
90 else
Greg Clayton72b77eb2011-02-04 21:13:05 +000091 ::memset (m_uuid, 0, sizeof(m_uuid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092}
93
94size_t
95UUID::GetByteSize()
96{
97 return sizeof(UUID::ValueType);
98}
99
100bool
101UUID::IsValid () const
102{
103 return m_uuid[0] ||
104 m_uuid[1] ||
105 m_uuid[2] ||
106 m_uuid[3] ||
107 m_uuid[4] ||
108 m_uuid[5] ||
109 m_uuid[6] ||
110 m_uuid[7] ||
111 m_uuid[8] ||
112 m_uuid[9] ||
113 m_uuid[10] ||
114 m_uuid[11] ||
115 m_uuid[12] ||
116 m_uuid[13] ||
117 m_uuid[14] ||
118 m_uuid[15];
119}
120
121static inline int
122xdigit_to_int (char ch)
123{
124 ch = tolower(ch);
125 if (ch >= 'a' && ch <= 'f')
126 return 10 + ch - 'a';
127 return ch - '0';
128}
129
130size_t
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000131UUID::DecodeUUIDBytesFromCString (const char *p, ValueType &uuid_bytes, const char **end)
132{
133 size_t uuid_byte_idx = 0;
134 if (p)
135 {
136 while (*p)
137 {
138 if (isxdigit(p[0]) && isxdigit(p[1]))
139 {
140 int hi_nibble = xdigit_to_int(p[0]);
141 int lo_nibble = xdigit_to_int(p[1]);
142 // Translate the two hex nibble characters into a byte
143 uuid_bytes[uuid_byte_idx] = (hi_nibble << 4) + lo_nibble;
144
145 // Skip both hex digits
146 p += 2;
147
148 // Increment the byte that we are decoding within the UUID value
149 // and break out if we are done
150 if (++uuid_byte_idx == 16)
151 break;
152 }
153 else if (*p == '-')
154 {
155 // Skip dashes
156 p++;
157 }
158 else
159 {
160 // UUID values can only consist of hex characters and '-' chars
161 break;
162 }
163 }
164 }
165 if (end)
166 *end = p;
167 return uuid_byte_idx;
168}
169size_t
170UUID::SetFromCString (const char *cstr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171{
172 if (cstr == NULL)
173 return 0;
174
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 const char *p = cstr;
176
177 // Skip leading whitespace characters
178 while (isspace(*p))
179 ++p;
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000180
Greg Claytonc7bece562013-01-25 18:06:21 +0000181 const size_t uuid_byte_idx = UUID::DecodeUUIDBytesFromCString (p, m_uuid, &p);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 // If we successfully decoded a UUID, return the amount of characters that
184 // were consumed
185 if (uuid_byte_idx == 16)
186 return p - cstr;
187
188 // Else return zero to indicate we were not able to parse a UUID value
189 return 0;
190}
191
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192}
193
194bool
Greg Clayton60830262011-02-04 18:53:10 +0000195lldb_private::operator == (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
196{
197 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) == 0;
198}
199
200bool
201lldb_private::operator != (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202{
Greg Clayton95e31422011-02-05 02:56:16 +0000203 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204}
205
206bool
Greg Clayton60830262011-02-04 18:53:10 +0000207lldb_private::operator < (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208{
Greg Clayton60830262011-02-04 18:53:10 +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}