Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | 1936546 | 2010-06-09 19:36:54 +0000 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <stdio.h> |
| 14 | #include <ctype.h> |
| 15 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | // C++ Includes |
Jason Molenda | 42b336c | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 17 | #include <string> |
| 18 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | // Other libraries and framework includes |
| 20 | // Project includes |
| 21 | #include "lldb/Core/Stream.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 23 | namespace lldb_private { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 25 | UUID::UUID() : m_num_uuid_bytes(16) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | { |
Greg Clayton | ddff7cc | 2011-02-04 21:13:05 +0000 | [diff] [blame] | 27 | ::memset (m_uuid, 0, sizeof(m_uuid)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | UUID::UUID(const UUID& rhs) |
| 31 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 32 | m_num_uuid_bytes = rhs.m_num_uuid_bytes; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid)); |
| 34 | } |
| 35 | |
| 36 | UUID::UUID (const void *uuid_bytes, uint32_t num_uuid_bytes) |
| 37 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 38 | SetBytes (uuid_bytes, num_uuid_bytes); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | const UUID& |
| 42 | UUID::operator=(const UUID& rhs) |
| 43 | { |
| 44 | if (this != &rhs) |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 45 | { |
| 46 | m_num_uuid_bytes = rhs.m_num_uuid_bytes; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | ::memcpy (m_uuid, rhs.m_uuid, sizeof (m_uuid)); |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 48 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | UUID::~UUID() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | UUID::Clear() |
| 58 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 59 | m_num_uuid_bytes = 16; |
Greg Clayton | ddff7cc | 2011-02-04 21:13:05 +0000 | [diff] [blame] | 60 | ::memset (m_uuid, 0, sizeof(m_uuid)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | const void * |
| 64 | UUID::GetBytes() const |
| 65 | { |
| 66 | return m_uuid; |
| 67 | } |
| 68 | |
Jason Molenda | 42b336c | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 69 | std::string |
Michael Sartain | a807cee | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 70 | UUID::GetAsString (const char *separator) const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | { |
Jason Molenda | 42b336c | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 72 | std::string result; |
Michael Sartain | a807cee | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 73 | char buf[256]; |
| 74 | if (!separator) |
| 75 | separator = "-"; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | const uint8_t *u = (const uint8_t *)GetBytes(); |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 77 | if (sizeof (buf) > (size_t)snprintf (buf, |
Jason Molenda | 42b336c | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 78 | sizeof (buf), |
Michael Sartain | a807cee | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 79 | "%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 Molenda | 42b336c | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 85 | { |
| 86 | result.append (buf); |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 87 | if (m_num_uuid_bytes == 20) |
| 88 | { |
Michael Sartain | a807cee | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 89 | 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 Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 90 | result.append (buf); |
| 91 | } |
Jason Molenda | 42b336c | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 92 | } |
| 93 | return result; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
| 97 | UUID::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 Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 102 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Greg Clayton | f9215ba | 2013-07-08 22:22:41 +0000 | [diff] [blame] | 108 | bool |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 109 | UUID::SetBytes (const void *uuid_bytes, uint32_t num_uuid_bytes) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | { |
Greg Clayton | f9215ba | 2013-07-08 22:22:41 +0000 | [diff] [blame] | 111 | if (uuid_bytes) |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 112 | { |
Greg Clayton | f9215ba | 2013-07-08 22:22:41 +0000 | [diff] [blame] | 113 | 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 Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 133 | } |
Greg Clayton | f9215ba | 2013-07-08 22:22:41 +0000 | [diff] [blame] | 134 | ::memset (m_uuid, 0, sizeof(m_uuid)); |
| 135 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | size_t |
| 139 | UUID::GetByteSize() |
| 140 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 141 | return m_num_uuid_bytes; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool |
| 145 | UUID::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 Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 162 | m_uuid[15] || |
| 163 | m_uuid[16] || |
| 164 | m_uuid[17] || |
| 165 | m_uuid[18] || |
| 166 | m_uuid[19]; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static inline int |
| 170 | xdigit_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 | |
| 178 | size_t |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 179 | UUID::DecodeUUIDBytesFromCString (const char *p, ValueType &uuid_bytes, const char **end, uint32_t num_uuid_bytes) |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 180 | { |
| 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 Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 198 | if (++uuid_byte_idx == num_uuid_bytes) |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 199 | 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 Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 215 | // Clear trailing bytes to 0. |
| 216 | for (uint32_t i = uuid_byte_idx; i < sizeof(ValueType); i++) |
| 217 | uuid_bytes[i] = 0; |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 218 | return uuid_byte_idx; |
| 219 | } |
| 220 | size_t |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 221 | UUID::SetFromCString (const char *cstr, uint32_t num_uuid_bytes) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 222 | { |
| 223 | if (cstr == NULL) |
| 224 | return 0; |
| 225 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 226 | const char *p = cstr; |
| 227 | |
| 228 | // Skip leading whitespace characters |
| 229 | while (isspace(*p)) |
| 230 | ++p; |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 231 | |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 232 | const size_t uuid_byte_idx = UUID::DecodeUUIDBytesFromCString (p, m_uuid, &p, num_uuid_bytes); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 234 | // If we successfully decoded a UUID, return the amount of characters that |
| 235 | // were consumed |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 236 | if (uuid_byte_idx == num_uuid_bytes) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | bool |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 246 | lldb_private::operator == (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs) |
| 247 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 248 | return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) == 0; |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | bool |
| 252 | lldb_private::operator != (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 254 | return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) != 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | bool |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 258 | lldb_private::operator < (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 260 | return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) < 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | bool |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 264 | lldb_private::operator <= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 265 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 266 | return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) <= 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | bool |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 270 | lldb_private::operator > (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 271 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 272 | return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) > 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | bool |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 276 | lldb_private::operator >= (const lldb_private::UUID &lhs, const lldb_private::UUID &rhs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | { |
Michael Sartain | 0be9b3b | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 278 | return ::memcmp (lhs.GetBytes(), rhs.GetBytes(), sizeof (lldb_private::UUID::ValueType)) >= 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 279 | } |