VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/uuid-generic.cpp@ 5031

Last change on this file since 5031 was 4750, checked in by vboxsync, 17 years ago

eol style native

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.2 KB
Line 
1/* $Id: uuid-generic.cpp 4750 2007-09-13 07:05:03Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - UUID, Generic.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/uuid.h>
23#include <iprt/assert.h>
24#include <iprt/err.h>
25#include <iprt/time.h>
26#include <iprt/asm.h>
27#include <iprt/rand.h>
28
29
30/* WARNING: This implementation ASSUMES little endian. */
31
32
33/**
34 * Generates a new UUID value.
35 *
36 * @returns iprt status code.
37 * @param pUuid Where to store generated uuid.
38 */
39RTDECL(int) RTUuidCreate(PRTUUID pUuid)
40{
41 /* validate input. */
42 AssertReturn(pUuid, VERR_INVALID_PARAMETER);
43
44 RTRandBytes(pUuid, sizeof(*pUuid));
45 pUuid->Gen.u16ClockSeq = (pUuid->Gen.u16ClockSeq & 0x3fff) | 0x8000;
46 pUuid->Gen.u16TimeHiAndVersion = (pUuid->Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
47
48 return VINF_SUCCESS;
49}
50
51
52/**
53 * Makes a null UUID value.
54 *
55 * @returns iprt status code.
56 * @param pUuid Where to store generated null uuid.
57 */
58RTDECL(int) RTUuidClear(PRTUUID pUuid)
59{
60 AssertReturn(pUuid, VERR_INVALID_PARAMETER);
61 pUuid->au64[0] = 0;
62 pUuid->au64[1] = 0;
63 return VINF_SUCCESS;
64}
65
66
67/**
68 * Checks if UUID is null.
69 *
70 * @returns true if UUID is null.
71 * @param pUuid uuid to check.
72 */
73RTDECL(int) RTUuidIsNull(PCRTUUID pUuid)
74{
75 AssertReturn(pUuid, VERR_INVALID_PARAMETER);
76 return !pUuid->au64[0]
77 && !pUuid->au64[1];
78}
79
80
81/**
82 * Compares two UUID values.
83 *
84 * @returns 0 if eq, < 0 or > 0.
85 * @param pUuid1 First value to compare.
86 * @param pUuid2 Second value to compare.
87 */
88RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
89{
90 /*
91 * Special cases.
92 */
93 if (pUuid1 == pUuid2)
94 return 0;
95 if (!pUuid1)
96 return RTUuidIsNull(pUuid2) ? 0 : -1;
97 if (!pUuid2)
98 return RTUuidIsNull(pUuid1) ? 0 : 1;
99
100 /*
101 * Standard cases.
102 */
103 if (pUuid1->Gen.u32TimeLow != pUuid2->Gen.u32TimeLow)
104 return pUuid1->Gen.u32TimeLow < pUuid2->Gen.u32TimeLow ? -1 : 1;
105 if (pUuid1->Gen.u16TimeMid != pUuid2->Gen.u16TimeMid)
106 return pUuid1->Gen.u16TimeMid < pUuid2->Gen.u16TimeMid ? -1 : 1;
107 if (pUuid1->Gen.u16TimeHiAndVersion != pUuid2->Gen.u16TimeHiAndVersion)
108 return pUuid1->Gen.u16TimeHiAndVersion < pUuid2->Gen.u16TimeHiAndVersion ? -1 : 1;
109 if (pUuid1->Gen.u16ClockSeq != pUuid2->Gen.u16ClockSeq)
110 return pUuid1->Gen.u16ClockSeq < pUuid2->Gen.u16ClockSeq ? -1 : 1;
111 if (pUuid1->Gen.au8Node[0] != pUuid2->Gen.au8Node[0])
112 return pUuid1->Gen.au8Node[0] < pUuid2->Gen.au8Node[0] ? -1 : 1;
113 if (pUuid1->Gen.au8Node[1] != pUuid2->Gen.au8Node[1])
114 return pUuid1->Gen.au8Node[1] < pUuid2->Gen.au8Node[1] ? -1 : 1;
115 if (pUuid1->Gen.au8Node[2] != pUuid2->Gen.au8Node[2])
116 return pUuid1->Gen.au8Node[2] < pUuid2->Gen.au8Node[2] ? -1 : 1;
117 if (pUuid1->Gen.au8Node[3] != pUuid2->Gen.au8Node[3])
118 return pUuid1->Gen.au8Node[3] < pUuid2->Gen.au8Node[3] ? -1 : 1;
119 if (pUuid1->Gen.au8Node[4] != pUuid2->Gen.au8Node[4])
120 return pUuid1->Gen.au8Node[4] < pUuid2->Gen.au8Node[4] ? -1 : 1;
121 if (pUuid1->Gen.au8Node[5] != pUuid2->Gen.au8Node[5])
122 return pUuid1->Gen.au8Node[5] < pUuid2->Gen.au8Node[5] ? -1 : 1;
123 return 0;
124}
125
126
127/**
128 * Converts binary UUID to its string representation.
129 *
130 * @returns iprt status code.
131 * @param pUuid Uuid to convert.
132 * @param pszString Where to store result string.
133 * @param cchString pszString buffer length, must be >= RTUUID_STR_LENGTH.
134 */
135RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString)
136{
137 /* validate parameters */
138 AssertReturn(pUuid, VERR_INVALID_PARAMETER);
139 AssertReturn(pszString, VERR_INVALID_PARAMETER);
140 AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
141
142 /*
143 * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
144 * pUuid->Gen.u32TimeLow,
145 * pUuid->Gen.u16TimeMin,
146 * pUuid->Gen.u16TimeHiAndVersion,
147 * pUuid->Gen.u16ClockSeq & 0xff,
148 * pUuid->Gen.u16ClockSeq >> 8,
149 * pUuid->Gen.au8Node[0],
150 * pUuid->Gen.au8Node[1],
151 * pUuid->Gen.au8Node[2],
152 * pUuid->Gen.au8Node[3],
153 * pUuid->Gen.au8Node[4],
154 * pUuid->Gen.au8Node[5]);
155 */
156 static const char s_achDigits[17] = "0123456789abcdef";
157 uint32_t u32TimeLow = pUuid->Gen.u32TimeLow;
158 pszString[ 0] = s_achDigits[(u32TimeLow >> 28)/*& 0xf*/];
159 pszString[ 1] = s_achDigits[(u32TimeLow >> 24) & 0xf];
160 pszString[ 2] = s_achDigits[(u32TimeLow >> 20) & 0xf];
161 pszString[ 3] = s_achDigits[(u32TimeLow >> 16) & 0xf];
162 pszString[ 4] = s_achDigits[(u32TimeLow >> 12) & 0xf];
163 pszString[ 5] = s_achDigits[(u32TimeLow >> 8) & 0xf];
164 pszString[ 6] = s_achDigits[(u32TimeLow >> 4) & 0xf];
165 pszString[ 7] = s_achDigits[(u32TimeLow/*>>0*/)& 0xf];
166 pszString[ 8] = '-';
167 unsigned u = pUuid->Gen.u16TimeMid;
168 pszString[ 9] = s_achDigits[(u >> 12)/*& 0xf*/];
169 pszString[10] = s_achDigits[(u >> 8) & 0xf];
170 pszString[11] = s_achDigits[(u >> 4) & 0xf];
171 pszString[12] = s_achDigits[(u/*>>0*/)& 0xf];
172 pszString[13] = '-';
173 u = pUuid->Gen.u16TimeHiAndVersion;
174 pszString[14] = s_achDigits[(u >> 12)/*& 0xf*/];
175 pszString[15] = s_achDigits[(u >> 8) & 0xf];
176 pszString[16] = s_achDigits[(u >> 4) & 0xf];
177 pszString[17] = s_achDigits[(u/*>>0*/)& 0xf];
178 pszString[18] = '-';
179 u = pUuid->Gen.u16ClockSeq;
180 pszString[19] = s_achDigits[(u >> 4) & 0xf];
181 pszString[20] = s_achDigits[(u/*>>0*/)& 0xf];
182 pszString[21] = s_achDigits[(u >> 12)/*& 0xf*/];
183 pszString[22] = s_achDigits[(u >> 8) & 0xf];
184 pszString[23] = '-';
185 pszString[24] = s_achDigits[pUuid->Gen.au8Node[0] >> 4];
186 pszString[25] = s_achDigits[pUuid->Gen.au8Node[0] & 0xf];
187 pszString[26] = s_achDigits[pUuid->Gen.au8Node[1] >> 4];
188 pszString[27] = s_achDigits[pUuid->Gen.au8Node[1] & 0xf];
189 pszString[28] = s_achDigits[pUuid->Gen.au8Node[2] >> 4];
190 pszString[29] = s_achDigits[pUuid->Gen.au8Node[2] & 0xf];
191 pszString[30] = s_achDigits[pUuid->Gen.au8Node[3] >> 4];
192 pszString[31] = s_achDigits[pUuid->Gen.au8Node[3] & 0xf];
193 pszString[32] = s_achDigits[pUuid->Gen.au8Node[4] >> 4];
194 pszString[33] = s_achDigits[pUuid->Gen.au8Node[4] & 0xf];
195 pszString[34] = s_achDigits[pUuid->Gen.au8Node[5] >> 4];
196 pszString[35] = s_achDigits[pUuid->Gen.au8Node[5] & 0xf];
197 pszString[36] = '\0';
198
199 return VINF_SUCCESS;
200}
201
202
203/**
204 * Converts UUID from its string representation to binary format.
205 *
206 * @returns iprt status code.
207 * @param pUuid Where to store result Uuid.
208 * @param pszString String with UUID text data.
209 */
210RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
211{
212 /* 0xff if not a hex number, otherwise the value. (Assumes UTF-8 encoded strings.) */
213 static const uint8_t s_aDigits[256] =
214 {
215 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 0..0f */
216 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 10..1f */
217 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 20..2f */
218 0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0xff,0xff, 0xff,0xff,0xff,0xff, /* 30..3f */
219 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 40..4f */
220 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 50..5f */
221 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 60..6f */
222 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 70..7f */
223 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 80..8f */
224 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 90..9f */
225 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* a0..af */
226 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* b0..bf */
227 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* c0..cf */
228 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* d0..df */
229 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* e0..ef */
230 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* f0..ff */
231 };
232
233 /*
234 * Validate parameters.
235 */
236 AssertReturn(pUuid, VERR_INVALID_PARAMETER);
237 AssertReturn(pszString, VERR_INVALID_PARAMETER);
238
239#define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
240#define MY_ISXDIGIT(ch) (s_aDigits[(ch) & 0xff] != 0xff)
241 MY_CHECK(MY_ISXDIGIT(pszString[ 0]));
242 MY_CHECK(MY_ISXDIGIT(pszString[ 1]));
243 MY_CHECK(MY_ISXDIGIT(pszString[ 2]));
244 MY_CHECK(MY_ISXDIGIT(pszString[ 3]));
245 MY_CHECK(MY_ISXDIGIT(pszString[ 4]));
246 MY_CHECK(MY_ISXDIGIT(pszString[ 5]));
247 MY_CHECK(MY_ISXDIGIT(pszString[ 6]));
248 MY_CHECK(MY_ISXDIGIT(pszString[ 7]));
249 MY_CHECK(pszString[ 8] == '-');
250 MY_CHECK(MY_ISXDIGIT(pszString[ 9]));
251 MY_CHECK(MY_ISXDIGIT(pszString[10]));
252 MY_CHECK(MY_ISXDIGIT(pszString[11]));
253 MY_CHECK(MY_ISXDIGIT(pszString[12]));
254 MY_CHECK(pszString[13] == '-');
255 MY_CHECK(MY_ISXDIGIT(pszString[14]));
256 MY_CHECK(MY_ISXDIGIT(pszString[15]));
257 MY_CHECK(MY_ISXDIGIT(pszString[16]));
258 MY_CHECK(MY_ISXDIGIT(pszString[17]));
259 MY_CHECK(pszString[18] == '-');
260 MY_CHECK(MY_ISXDIGIT(pszString[19]));
261 MY_CHECK(MY_ISXDIGIT(pszString[20]));
262 MY_CHECK(MY_ISXDIGIT(pszString[21]));
263 MY_CHECK(MY_ISXDIGIT(pszString[22]));
264 MY_CHECK(pszString[23] == '-');
265 MY_CHECK(MY_ISXDIGIT(pszString[24]));
266 MY_CHECK(MY_ISXDIGIT(pszString[25]));
267 MY_CHECK(MY_ISXDIGIT(pszString[26]));
268 MY_CHECK(MY_ISXDIGIT(pszString[27]));
269 MY_CHECK(MY_ISXDIGIT(pszString[28]));
270 MY_CHECK(MY_ISXDIGIT(pszString[29]));
271 MY_CHECK(MY_ISXDIGIT(pszString[30]));
272 MY_CHECK(MY_ISXDIGIT(pszString[31]));
273 MY_CHECK(MY_ISXDIGIT(pszString[32]));
274 MY_CHECK(MY_ISXDIGIT(pszString[33]));
275 MY_CHECK(MY_ISXDIGIT(pszString[34]));
276 MY_CHECK(MY_ISXDIGIT(pszString[35]));
277 MY_CHECK(!pszString[36]);
278#undef MY_ISXDIGIT
279#undef MY_CHECK
280
281 /*
282 * Inverse of RTUuidToStr (see above).
283 */
284#define MY_TONUM(ch) (s_aDigits[(ch) & 0xff])
285 pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pszString[ 0]) << 28
286 | (uint32_t)MY_TONUM(pszString[ 1]) << 24
287 | (uint32_t)MY_TONUM(pszString[ 2]) << 20
288 | (uint32_t)MY_TONUM(pszString[ 3]) << 16
289 | (uint32_t)MY_TONUM(pszString[ 4]) << 12
290 | (uint32_t)MY_TONUM(pszString[ 5]) << 8
291 | (uint32_t)MY_TONUM(pszString[ 6]) << 4
292 | (uint32_t)MY_TONUM(pszString[ 7]);
293 pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pszString[ 9]) << 12
294 | (uint16_t)MY_TONUM(pszString[10]) << 8
295 | (uint16_t)MY_TONUM(pszString[11]) << 4
296 | (uint16_t)MY_TONUM(pszString[12]);
297 pUuid->Gen.u16TimeHiAndVersion =
298 (uint16_t)MY_TONUM(pszString[14]) << 12
299 | (uint16_t)MY_TONUM(pszString[15]) << 8
300 | (uint16_t)MY_TONUM(pszString[16]) << 4
301 | (uint16_t)MY_TONUM(pszString[17]);
302 pUuid->Gen.u16ClockSeq =(uint16_t)MY_TONUM(pszString[19]) << 4
303 | (uint16_t)MY_TONUM(pszString[20])
304 | (uint16_t)MY_TONUM(pszString[21]) << 12
305 | (uint16_t)MY_TONUM(pszString[22]) << 8;
306 pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pszString[24]) << 4
307 | (uint8_t)MY_TONUM(pszString[25]);
308 pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pszString[26]) << 4
309 | (uint8_t)MY_TONUM(pszString[27]);
310 pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pszString[28]) << 4
311 | (uint8_t)MY_TONUM(pszString[29]);
312 pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pszString[30]) << 4
313 | (uint8_t)MY_TONUM(pszString[31]);
314 pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pszString[32]) << 4
315 | (uint8_t)MY_TONUM(pszString[33]);
316 pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pszString[34]) << 4
317 | (uint8_t)MY_TONUM(pszString[35]);
318#undef MY_TONUM
319 return VINF_SUCCESS;
320}
321
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette