VirtualBox

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

Last change on this file since 893 was 893, checked in by vboxsync, 18 years ago

Use RTRand.

  • Property svn:keywords set to Id
File size: 14.7 KB
Line 
1/* $Id: uuid-generic.cpp 893 2007-02-14 09:36:39Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - UUID, Generic.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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_ISXDIGIT(ch) (s_aDigits[(ch) & 0xff] != 0xff)
244 AssertReturn(MY_ISXDIGIT(pszString[ 0]), VERR_INVALID_UUID_FORMAT);
245 AssertReturn(MY_ISXDIGIT(pszString[ 1]), VERR_INVALID_UUID_FORMAT);
246 AssertReturn(MY_ISXDIGIT(pszString[ 2]), VERR_INVALID_UUID_FORMAT);
247 AssertReturn(MY_ISXDIGIT(pszString[ 3]), VERR_INVALID_UUID_FORMAT);
248 AssertReturn(MY_ISXDIGIT(pszString[ 4]), VERR_INVALID_UUID_FORMAT);
249 AssertReturn(MY_ISXDIGIT(pszString[ 5]), VERR_INVALID_UUID_FORMAT);
250 AssertReturn(MY_ISXDIGIT(pszString[ 6]), VERR_INVALID_UUID_FORMAT);
251 AssertReturn(MY_ISXDIGIT(pszString[ 7]), VERR_INVALID_UUID_FORMAT);
252 AssertReturn(pszString[ 8] == '-', VERR_INVALID_UUID_FORMAT);
253 AssertReturn(MY_ISXDIGIT(pszString[ 9]), VERR_INVALID_UUID_FORMAT);
254 AssertReturn(MY_ISXDIGIT(pszString[10]), VERR_INVALID_UUID_FORMAT);
255 AssertReturn(MY_ISXDIGIT(pszString[11]), VERR_INVALID_UUID_FORMAT);
256 AssertReturn(MY_ISXDIGIT(pszString[12]), VERR_INVALID_UUID_FORMAT);
257 AssertReturn(pszString[13] == '-', VERR_INVALID_UUID_FORMAT);
258 AssertReturn(MY_ISXDIGIT(pszString[14]), VERR_INVALID_UUID_FORMAT);
259 AssertReturn(MY_ISXDIGIT(pszString[15]), VERR_INVALID_UUID_FORMAT);
260 AssertReturn(MY_ISXDIGIT(pszString[16]), VERR_INVALID_UUID_FORMAT);
261 AssertReturn(MY_ISXDIGIT(pszString[17]), VERR_INVALID_UUID_FORMAT);
262 AssertReturn(pszString[18] == '-', VERR_INVALID_UUID_FORMAT);
263 AssertReturn(MY_ISXDIGIT(pszString[19]), VERR_INVALID_UUID_FORMAT);
264 AssertReturn(MY_ISXDIGIT(pszString[20]), VERR_INVALID_UUID_FORMAT);
265 AssertReturn(MY_ISXDIGIT(pszString[21]), VERR_INVALID_UUID_FORMAT);
266 AssertReturn(MY_ISXDIGIT(pszString[22]), VERR_INVALID_UUID_FORMAT);
267 AssertReturn(pszString[23] == '-', VERR_INVALID_UUID_FORMAT);
268 AssertReturn(MY_ISXDIGIT(pszString[24]), VERR_INVALID_UUID_FORMAT);
269 AssertReturn(MY_ISXDIGIT(pszString[25]), VERR_INVALID_UUID_FORMAT);
270 AssertReturn(MY_ISXDIGIT(pszString[26]), VERR_INVALID_UUID_FORMAT);
271 AssertReturn(MY_ISXDIGIT(pszString[27]), VERR_INVALID_UUID_FORMAT);
272 AssertReturn(MY_ISXDIGIT(pszString[28]), VERR_INVALID_UUID_FORMAT);
273 AssertReturn(MY_ISXDIGIT(pszString[29]), VERR_INVALID_UUID_FORMAT);
274 AssertReturn(MY_ISXDIGIT(pszString[30]), VERR_INVALID_UUID_FORMAT);
275 AssertReturn(MY_ISXDIGIT(pszString[31]), VERR_INVALID_UUID_FORMAT);
276 AssertReturn(MY_ISXDIGIT(pszString[32]), VERR_INVALID_UUID_FORMAT);
277 AssertReturn(MY_ISXDIGIT(pszString[33]), VERR_INVALID_UUID_FORMAT);
278 AssertReturn(MY_ISXDIGIT(pszString[34]), VERR_INVALID_UUID_FORMAT);
279 AssertReturn(MY_ISXDIGIT(pszString[35]), VERR_INVALID_UUID_FORMAT);
280 AssertReturn(!pszString[36], VERR_INVALID_UUID_FORMAT);
281#undef MY_ISXDIGIT
282
283 /*
284 * Inverse of RTUuidToStr (see above).
285 */
286#define MY_TONUM(ch) (s_aDigits[(ch) & 0xff])
287 pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pszString[ 0]) << 28
288 | (uint32_t)MY_TONUM(pszString[ 1]) << 24
289 | (uint32_t)MY_TONUM(pszString[ 2]) << 20
290 | (uint32_t)MY_TONUM(pszString[ 3]) << 16
291 | (uint32_t)MY_TONUM(pszString[ 4]) << 12
292 | (uint32_t)MY_TONUM(pszString[ 5]) << 8
293 | (uint32_t)MY_TONUM(pszString[ 6]) << 4
294 | (uint32_t)MY_TONUM(pszString[ 7]);
295 pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pszString[ 9]) << 12
296 | (uint16_t)MY_TONUM(pszString[10]) << 8
297 | (uint16_t)MY_TONUM(pszString[11]) << 4
298 | (uint16_t)MY_TONUM(pszString[12]);
299 pUuid->Gen.u16TimeHiAndVersion =
300 (uint16_t)MY_TONUM(pszString[14]) << 12
301 | (uint16_t)MY_TONUM(pszString[15]) << 8
302 | (uint16_t)MY_TONUM(pszString[16]) << 4
303 | (uint16_t)MY_TONUM(pszString[17]);
304 pUuid->Gen.u16ClockSeq =(uint16_t)MY_TONUM(pszString[19]) << 4
305 | (uint16_t)MY_TONUM(pszString[20])
306 | (uint16_t)MY_TONUM(pszString[21]) << 12
307 | (uint16_t)MY_TONUM(pszString[22]) << 8;
308 pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pszString[24]) << 4
309 | (uint8_t)MY_TONUM(pszString[25]);
310 pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pszString[26]) << 4
311 | (uint8_t)MY_TONUM(pszString[27]);
312 pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pszString[28]) << 4
313 | (uint8_t)MY_TONUM(pszString[29]);
314 pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pszString[30]) << 4
315 | (uint8_t)MY_TONUM(pszString[31]);
316 pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pszString[32]) << 4
317 | (uint8_t)MY_TONUM(pszString[33]);
318 pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pszString[34]) << 4
319 | (uint8_t)MY_TONUM(pszString[35]);
320#undef MY_TONUM
321 return VINF_SUCCESS;
322}
323
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