blob: 0394141e711dbafbd989c9af98271353a5d13043 [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
21using namespace lldb_private;
22
23UUID::UUID()
24{
25 ::bzero (m_uuid, sizeof(m_uuid));
26}
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
38 ::bzero (m_uuid, sizeof(m_uuid));
39}
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{
56 ::bzero (m_uuid, sizeof(m_uuid));
57}
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();
69 snprintf(dst, dst_len, "%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",
70 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]);
71 return dst;
72}
73
74void
75UUID::Dump (Stream *s) const
76{
77 const uint8_t *u = (const uint8_t *)GetBytes();
78 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",
79 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]);
80}
81
82void
83UUID::SetBytes (const void *uuid_bytes)
84{
85 if (uuid_bytes)
86 ::memcpy (m_uuid, uuid_bytes, sizeof (m_uuid));
87 else
88 ::bzero (m_uuid, sizeof(m_uuid));
89}
90
91size_t
92UUID::GetByteSize()
93{
94 return sizeof(UUID::ValueType);
95}
96
97bool
98UUID::IsValid () const
99{
100 return m_uuid[0] ||
101 m_uuid[1] ||
102 m_uuid[2] ||
103 m_uuid[3] ||
104 m_uuid[4] ||
105 m_uuid[5] ||
106 m_uuid[6] ||
107 m_uuid[7] ||
108 m_uuid[8] ||
109 m_uuid[9] ||
110 m_uuid[10] ||
111 m_uuid[11] ||
112 m_uuid[12] ||
113 m_uuid[13] ||
114 m_uuid[14] ||
115 m_uuid[15];
116}
117
118static inline int
119xdigit_to_int (char ch)
120{
121 ch = tolower(ch);
122 if (ch >= 'a' && ch <= 'f')
123 return 10 + ch - 'a';
124 return ch - '0';
125}
126
127size_t
128UUID::SetfromCString (const char *cstr)
129{
130 if (cstr == NULL)
131 return 0;
132
133 uint32_t uuid_byte_idx = 0;
134 const char *p = cstr;
135
136 // Skip leading whitespace characters
137 while (isspace(*p))
138 ++p;
139
140 // Try and decode a UUID
141 while (*p != '\0')
142 {
143 if (isxdigit(*p) && isxdigit(p[1]))
144 {
145 int hi_nibble = xdigit_to_int(p[0]);
146 int lo_nibble = xdigit_to_int(p[1]);
147 // Translate the two hex nibble characters into a byte
148 m_uuid[uuid_byte_idx] = (hi_nibble << 4) + lo_nibble;
149
150 // Skip both hex digits
151 p += 2;
152
153 // Increment the byte that we are decoding within the UUID value
154 // and break out if we are done
155 if (++uuid_byte_idx == 16)
156 break;
157 }
158 else if (*p == '-')
159 {
160 // Skip dashes
161 p++;
162 }
163 else
164 {
165 // UUID values can only consist of hex characters and '-' chars
166 return 0;
167 }
168 }
169 // If we successfully decoded a UUID, return the amount of characters that
170 // were consumed
171 if (uuid_byte_idx == 16)
172 return p - cstr;
173
174 // Else return zero to indicate we were not able to parse a UUID value
175 return 0;
176}
177
178
179
180bool
181lldb_private::operator == (const UUID &lhs, const UUID &rhs)
182{
183 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), UUID::GetByteSize()) == 0;
184}
185
186bool
187lldb_private::operator != (const UUID &lhs, const UUID &rhs)
188{
189 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), UUID::GetByteSize()) != 0;
190}
191
192bool
193lldb_private::operator < (const UUID &lhs, const UUID &rhs)
194{
195 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), UUID::GetByteSize()) < 0;
196}
197
198bool
199lldb_private::operator <= (const UUID &lhs, const UUID &rhs)
200{
201 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), UUID::GetByteSize()) <= 0;
202}
203
204bool
205lldb_private::operator > (const UUID &lhs, const UUID &rhs)
206{
207 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), UUID::GetByteSize()) > 0;
208}
209
210bool
211lldb_private::operator >= (const UUID &lhs, const UUID &rhs)
212{
213 return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), UUID::GetByteSize()) >= 0;
214}