VirtualBox

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

Last change on this file since 8863 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

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