blob: 520e31e80707f928b93c5622d67fdcf995e621a5 [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
131UUID::SetfromCString (const char *cstr)
132{
133 if (cstr == NULL)
134 return 0;
135
136 uint32_t uuid_byte_idx = 0;
137 const char *p = cstr;
138
139 // Skip leading whitespace characters
140 while (isspace(*p))
141 ++p;
142
143 // Try and decode a UUID
144 while (*p != '\0')
145 {
146 if (isxdigit(*p) && isxdigit(p[1]))
147 {
148 int hi_nibble = xdigit_to_int(p[0]);
149 int lo_nibble = xdigit_to_int(p[1]);
150 // Translate the two hex nibble characters into a byte
151 m_uuid[uuid_byte_idx] = (hi_nibble << 4) + lo_nibble;
152
153 // Skip both hex digits
154 p += 2;
155
156 // Increment the byte that we are decoding within the UUID value
157 // and break out if we are done
158 if (++uuid_byte_idx == 16)
159 break;
160 }
161 else if (*p == '-')
162 {
163 // Skip dashes
164 p++;
165 }
166 else
167 {
168 // UUID values can only consist of hex characters and '-' chars
169 return 0;
170 }
171 }
172 // If we successfully decoded a UUID, return the amount of characters that
173 // were consumed
174 if (uuid_byte_idx == 16)
175 return p - cstr;
176
177 // Else return zero to indicate we were not able to parse a UUID value
178 return 0;
179}
180
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181}
182
183bool
Greg Clayton60830262011-02-04 18:53:10 +0000184lldb_private::operator == (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
185{
186 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) == 0;
187}
188
189bool
190lldb_private::operator != (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191{
Greg Clayton95e31422011-02-05 02:56:16 +0000192 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193}
194
195bool
Greg Clayton60830262011-02-04 18:53:10 +0000196lldb_private::operator < (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197{
Greg Clayton60830262011-02-04 18:53:10 +0000198 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199}
200
201bool
Greg Clayton60830262011-02-04 18:53:10 +0000202lldb_private::operator <= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203{
Greg Clayton60830262011-02-04 18:53:10 +0000204 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) <= 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205}
206
207bool
Greg Clayton60830262011-02-04 18:53:10 +0000208lldb_private::operator > (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209{
Greg Clayton60830262011-02-04 18:53:10 +0000210 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) > 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211}
212
213bool
Greg Clayton60830262011-02-04 18:53:10 +0000214lldb_private::operator >= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215{
Greg Clayton60830262011-02-04 18:53:10 +0000216 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), lldb_private::UUID::GetByteSize()) >= 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}