VirtualBox

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

Last change on this file since 3672 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

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