1 /* $Id: uuid-generic.cpp 25033 2007-10-04 07:21:02Z frank $ */
3 * innotek Portable Runtime - UUID, Generic.
7 * Copyright (C) 2006-2007 innotek GmbH
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.
19 /*******************************************************************************
21 *******************************************************************************/
23 #include "const_defines.h"
24 #include "func_defines.h"
28 /* WARNING: This implementation ASSUMES little endian. */
32 * Generates a new UUID value.
34 * @returns iprt status code.
35 * @param pUuid Where to store generated uuid.
37 int RTUuidCreate(PRTUUID pUuid
)
40 AssertReturn(pUuid
, VERR_INVALID_PARAMETER
);
42 RTRandBytes(pUuid
, sizeof(*pUuid
));
43 pUuid
->Gen
.u16ClockSeq
= (pUuid
->Gen
.u16ClockSeq
& 0x3fff) | 0x8000;
44 pUuid
->Gen
.u16TimeHiAndVersion
= (pUuid
->Gen
.u16TimeHiAndVersion
& 0x0fff) | 0x4000;
51 * Makes a null UUID value.
53 * @returns iprt status code.
54 * @param pUuid Where to store generated null uuid.
56 int RTUuidClear(PRTUUID pUuid
)
58 AssertReturn(pUuid
, VERR_INVALID_PARAMETER
);
66 * Checks if UUID is null.
68 * @returns true if UUID is null.
69 * @param pUuid uuid to check.
71 int RTUuidIsNull(PCRTUUID pUuid
)
73 AssertReturn(pUuid
, VERR_INVALID_PARAMETER
);
74 return !pUuid
->au64
[0]
80 * Compares two UUID values.
82 * @returns 0 if eq, < 0 or > 0.
83 * @param pUuid1 First value to compare.
84 * @param pUuid2 Second value to compare.
86 int RTUuidCompare(PCRTUUID pUuid1
, PCRTUUID pUuid2
)
94 return RTUuidIsNull(pUuid2
) ? 0 : -1;
96 return RTUuidIsNull(pUuid1
) ? 0 : 1;
101 if (pUuid1
->Gen
.u32TimeLow
!= pUuid2
->Gen
.u32TimeLow
)
102 return pUuid1
->Gen
.u32TimeLow
< pUuid2
->Gen
.u32TimeLow
? -1 : 1;
103 if (pUuid1
->Gen
.u16TimeMid
!= pUuid2
->Gen
.u16TimeMid
)
104 return pUuid1
->Gen
.u16TimeMid
< pUuid2
->Gen
.u16TimeMid
? -1 : 1;
105 if (pUuid1
->Gen
.u16TimeHiAndVersion
!= pUuid2
->Gen
.u16TimeHiAndVersion
)
106 return pUuid1
->Gen
.u16TimeHiAndVersion
< pUuid2
->Gen
.u16TimeHiAndVersion
? -1 : 1;
107 if (pUuid1
->Gen
.u16ClockSeq
!= pUuid2
->Gen
.u16ClockSeq
)
108 return pUuid1
->Gen
.u16ClockSeq
< pUuid2
->Gen
.u16ClockSeq
? -1 : 1;
109 if (pUuid1
->Gen
.au8Node
[0] != pUuid2
->Gen
.au8Node
[0])
110 return pUuid1
->Gen
.au8Node
[0] < pUuid2
->Gen
.au8Node
[0] ? -1 : 1;
111 if (pUuid1
->Gen
.au8Node
[1] != pUuid2
->Gen
.au8Node
[1])
112 return pUuid1
->Gen
.au8Node
[1] < pUuid2
->Gen
.au8Node
[1] ? -1 : 1;
113 if (pUuid1
->Gen
.au8Node
[2] != pUuid2
->Gen
.au8Node
[2])
114 return pUuid1
->Gen
.au8Node
[2] < pUuid2
->Gen
.au8Node
[2] ? -1 : 1;
115 if (pUuid1
->Gen
.au8Node
[3] != pUuid2
->Gen
.au8Node
[3])
116 return pUuid1
->Gen
.au8Node
[3] < pUuid2
->Gen
.au8Node
[3] ? -1 : 1;
117 if (pUuid1
->Gen
.au8Node
[4] != pUuid2
->Gen
.au8Node
[4])
118 return pUuid1
->Gen
.au8Node
[4] < pUuid2
->Gen
.au8Node
[4] ? -1 : 1;
119 if (pUuid1
->Gen
.au8Node
[5] != pUuid2
->Gen
.au8Node
[5])
120 return pUuid1
->Gen
.au8Node
[5] < pUuid2
->Gen
.au8Node
[5] ? -1 : 1;
126 * Converts binary UUID to its string representation.
128 * @returns iprt status code.
129 * @param pUuid Uuid to convert.
130 * @param pszString Where to store result string.
131 * @param cchString pszString buffer length, must be >= RTUUID_STR_LENGTH.
133 int RTUuidToStr(PCRTUUID pUuid
, char *pszString
, unsigned cchString
)
135 /* validate parameters */
136 AssertReturn(pUuid
, VERR_INVALID_PARAMETER
);
137 AssertReturn(pszString
, VERR_INVALID_PARAMETER
);
138 AssertReturn(cchString
>= RTUUID_STR_LENGTH
, VERR_INVALID_PARAMETER
);
141 * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
142 * pUuid->Gen.u32TimeLow,
143 * pUuid->Gen.u16TimeMin,
144 * pUuid->Gen.u16TimeHiAndVersion,
145 * pUuid->Gen.u16ClockSeq & 0xff,
146 * pUuid->Gen.u16ClockSeq >> 8,
147 * pUuid->Gen.au8Node[0],
148 * pUuid->Gen.au8Node[1],
149 * pUuid->Gen.au8Node[2],
150 * pUuid->Gen.au8Node[3],
151 * pUuid->Gen.au8Node[4],
152 * pUuid->Gen.au8Node[5]);
154 static const char s_achDigits
[17] = "0123456789abcdef";
155 uint32_t u32TimeLow
= pUuid
->Gen
.u32TimeLow
;
156 pszString
[ 0] = s_achDigits
[(u32TimeLow
>> 28)/*& 0xf*/];
157 pszString
[ 1] = s_achDigits
[(u32TimeLow
>> 24) & 0xf];
158 pszString
[ 2] = s_achDigits
[(u32TimeLow
>> 20) & 0xf];
159 pszString
[ 3] = s_achDigits
[(u32TimeLow
>> 16) & 0xf];
160 pszString
[ 4] = s_achDigits
[(u32TimeLow
>> 12) & 0xf];
161 pszString
[ 5] = s_achDigits
[(u32TimeLow
>> 8) & 0xf];
162 pszString
[ 6] = s_achDigits
[(u32TimeLow
>> 4) & 0xf];
163 pszString
[ 7] = s_achDigits
[(u32TimeLow
/*>>0*/)& 0xf];
165 unsigned u
= pUuid
->Gen
.u16TimeMid
;
166 pszString
[ 9] = s_achDigits
[(u
>> 12)/*& 0xf*/];
167 pszString
[10] = s_achDigits
[(u
>> 8) & 0xf];
168 pszString
[11] = s_achDigits
[(u
>> 4) & 0xf];
169 pszString
[12] = s_achDigits
[(u
/*>>0*/)& 0xf];
171 u
= pUuid
->Gen
.u16TimeHiAndVersion
;
172 pszString
[14] = s_achDigits
[(u
>> 12)/*& 0xf*/];
173 pszString
[15] = s_achDigits
[(u
>> 8) & 0xf];
174 pszString
[16] = s_achDigits
[(u
>> 4) & 0xf];
175 pszString
[17] = s_achDigits
[(u
/*>>0*/)& 0xf];
177 u
= pUuid
->Gen
.u16ClockSeq
;
178 pszString
[19] = s_achDigits
[(u
>> 4) & 0xf];
179 pszString
[20] = s_achDigits
[(u
/*>>0*/)& 0xf];
180 pszString
[21] = s_achDigits
[(u
>> 12)/*& 0xf*/];
181 pszString
[22] = s_achDigits
[(u
>> 8) & 0xf];
183 pszString
[24] = s_achDigits
[pUuid
->Gen
.au8Node
[0] >> 4];
184 pszString
[25] = s_achDigits
[pUuid
->Gen
.au8Node
[0] & 0xf];
185 pszString
[26] = s_achDigits
[pUuid
->Gen
.au8Node
[1] >> 4];
186 pszString
[27] = s_achDigits
[pUuid
->Gen
.au8Node
[1] & 0xf];
187 pszString
[28] = s_achDigits
[pUuid
->Gen
.au8Node
[2] >> 4];
188 pszString
[29] = s_achDigits
[pUuid
->Gen
.au8Node
[2] & 0xf];
189 pszString
[30] = s_achDigits
[pUuid
->Gen
.au8Node
[3] >> 4];
190 pszString
[31] = s_achDigits
[pUuid
->Gen
.au8Node
[3] & 0xf];
191 pszString
[32] = s_achDigits
[pUuid
->Gen
.au8Node
[4] >> 4];
192 pszString
[33] = s_achDigits
[pUuid
->Gen
.au8Node
[4] & 0xf];
193 pszString
[34] = s_achDigits
[pUuid
->Gen
.au8Node
[5] >> 4];
194 pszString
[35] = s_achDigits
[pUuid
->Gen
.au8Node
[5] & 0xf];
195 pszString
[36] = '\0';
202 * Converts UUID from its string representation to binary format.
204 * @returns iprt status code.
205 * @param pUuid Where to store result Uuid.
206 * @param pszString String with UUID text data.
208 int RTUuidFromStr(PRTUUID pUuid
, const char *pszString
)
210 /* 0xff if not a hex number, otherwise the value. (Assumes UTF-8 encoded strings.) */
211 static const uint8_t s_aDigits
[256] =
213 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 0..0f */
214 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 10..1f */
215 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 20..2f */
216 0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0xff,0xff, 0xff,0xff,0xff,0xff, /* 30..3f */
217 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 40..4f */
218 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 50..5f */
219 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 60..6f */
220 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 70..7f */
221 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 80..8f */
222 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 90..9f */
223 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* a0..af */
224 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* b0..bf */
225 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* c0..cf */
226 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* d0..df */
227 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* e0..ef */
228 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* f0..ff */
232 * Validate parameters.
234 AssertReturn(pUuid
, VERR_INVALID_PARAMETER
);
235 AssertReturn(pszString
, VERR_INVALID_PARAMETER
);
237 #define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
238 #define MY_ISXDIGIT(ch) (s_aDigits[(ch) & 0xff] != 0xff)
239 MY_CHECK(MY_ISXDIGIT(pszString
[ 0]));
240 MY_CHECK(MY_ISXDIGIT(pszString
[ 1]));
241 MY_CHECK(MY_ISXDIGIT(pszString
[ 2]));
242 MY_CHECK(MY_ISXDIGIT(pszString
[ 3]));
243 MY_CHECK(MY_ISXDIGIT(pszString
[ 4]));
244 MY_CHECK(MY_ISXDIGIT(pszString
[ 5]));
245 MY_CHECK(MY_ISXDIGIT(pszString
[ 6]));
246 MY_CHECK(MY_ISXDIGIT(pszString
[ 7]));
247 MY_CHECK(pszString
[ 8] == '-');
248 MY_CHECK(MY_ISXDIGIT(pszString
[ 9]));
249 MY_CHECK(MY_ISXDIGIT(pszString
[10]));
250 MY_CHECK(MY_ISXDIGIT(pszString
[11]));
251 MY_CHECK(MY_ISXDIGIT(pszString
[12]));
252 MY_CHECK(pszString
[13] == '-');
253 MY_CHECK(MY_ISXDIGIT(pszString
[14]));
254 MY_CHECK(MY_ISXDIGIT(pszString
[15]));
255 MY_CHECK(MY_ISXDIGIT(pszString
[16]));
256 MY_CHECK(MY_ISXDIGIT(pszString
[17]));
257 MY_CHECK(pszString
[18] == '-');
258 MY_CHECK(MY_ISXDIGIT(pszString
[19]));
259 MY_CHECK(MY_ISXDIGIT(pszString
[20]));
260 MY_CHECK(MY_ISXDIGIT(pszString
[21]));
261 MY_CHECK(MY_ISXDIGIT(pszString
[22]));
262 MY_CHECK(pszString
[23] == '-');
263 MY_CHECK(MY_ISXDIGIT(pszString
[24]));
264 MY_CHECK(MY_ISXDIGIT(pszString
[25]));
265 MY_CHECK(MY_ISXDIGIT(pszString
[26]));
266 MY_CHECK(MY_ISXDIGIT(pszString
[27]));
267 MY_CHECK(MY_ISXDIGIT(pszString
[28]));
268 MY_CHECK(MY_ISXDIGIT(pszString
[29]));
269 MY_CHECK(MY_ISXDIGIT(pszString
[30]));
270 MY_CHECK(MY_ISXDIGIT(pszString
[31]));
271 MY_CHECK(MY_ISXDIGIT(pszString
[32]));
272 MY_CHECK(MY_ISXDIGIT(pszString
[33]));
273 MY_CHECK(MY_ISXDIGIT(pszString
[34]));
274 MY_CHECK(MY_ISXDIGIT(pszString
[35]));
275 MY_CHECK(!pszString
[36]);
280 * Inverse of RTUuidToStr (see above).
282 #define MY_TONUM(ch) (s_aDigits[(ch) & 0xff])
283 pUuid
->Gen
.u32TimeLow
= (uint32_t)MY_TONUM(pszString
[ 0]) << 28
284 | (uint32_t)MY_TONUM(pszString
[ 1]) << 24
285 | (uint32_t)MY_TONUM(pszString
[ 2]) << 20
286 | (uint32_t)MY_TONUM(pszString
[ 3]) << 16
287 | (uint32_t)MY_TONUM(pszString
[ 4]) << 12
288 | (uint32_t)MY_TONUM(pszString
[ 5]) << 8
289 | (uint32_t)MY_TONUM(pszString
[ 6]) << 4
290 | (uint32_t)MY_TONUM(pszString
[ 7]);
291 pUuid
->Gen
.u16TimeMid
= (uint16_t)MY_TONUM(pszString
[ 9]) << 12
292 | (uint16_t)MY_TONUM(pszString
[10]) << 8
293 | (uint16_t)MY_TONUM(pszString
[11]) << 4
294 | (uint16_t)MY_TONUM(pszString
[12]);
295 pUuid
->Gen
.u16TimeHiAndVersion
=
296 (uint16_t)MY_TONUM(pszString
[14]) << 12
297 | (uint16_t)MY_TONUM(pszString
[15]) << 8
298 | (uint16_t)MY_TONUM(pszString
[16]) << 4
299 | (uint16_t)MY_TONUM(pszString
[17]);
300 pUuid
->Gen
.u16ClockSeq
=(uint16_t)MY_TONUM(pszString
[19]) << 4
301 | (uint16_t)MY_TONUM(pszString
[20])
302 | (uint16_t)MY_TONUM(pszString
[21]) << 12
303 | (uint16_t)MY_TONUM(pszString
[22]) << 8;
304 pUuid
->Gen
.au8Node
[0] = (uint8_t)MY_TONUM(pszString
[24]) << 4
305 | (uint8_t)MY_TONUM(pszString
[25]);
306 pUuid
->Gen
.au8Node
[1] = (uint8_t)MY_TONUM(pszString
[26]) << 4
307 | (uint8_t)MY_TONUM(pszString
[27]);
308 pUuid
->Gen
.au8Node
[2] = (uint8_t)MY_TONUM(pszString
[28]) << 4
309 | (uint8_t)MY_TONUM(pszString
[29]);
310 pUuid
->Gen
.au8Node
[3] = (uint8_t)MY_TONUM(pszString
[30]) << 4
311 | (uint8_t)MY_TONUM(pszString
[31]);
312 pUuid
->Gen
.au8Node
[4] = (uint8_t)MY_TONUM(pszString
[32]) << 4
313 | (uint8_t)MY_TONUM(pszString
[33]);
314 pUuid
->Gen
.au8Node
[5] = (uint8_t)MY_TONUM(pszString
[34]) << 4
315 | (uint8_t)MY_TONUM(pszString
[35]);