VirtualBox

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

Last change on this file since 32994 was 32994, checked in by vboxsync, 14 years ago

RTUuidFromStr: bug fix + testcase.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 21.8 KB
Line 
1/* $Id: uuid-generic.cpp 32994 2010-10-07 23:11:10Z vboxsync $ */
2/** @file
3 * IPRT - UUID, Generic.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
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
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/uuid.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/err.h>
36
37
38/*******************************************************************************
39* Global Variables *
40*******************************************************************************/
41/** Conversion table used by the conversion functions.
42 * 0xff if not a hex number, otherwise the value. */
43static const uint8_t g_au8Digits[256] =
44{
45 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 0..0f */
46 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 10..1f */
47 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 20..2f */
48 0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0xff,0xff, 0xff,0xff,0xff,0xff, /* 30..3f */
49 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 40..4f */
50 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 50..5f */
51 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 60..6f */
52 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 70..7f */
53 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 80..8f */
54 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 90..9f */
55 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* a0..af */
56 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* b0..bf */
57 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* c0..cf */
58 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* d0..df */
59 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* e0..ef */
60 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* f0..ff */
61};
62/** Conversion to string. */
63static const char g_achDigits[17] = "0123456789abcdef";
64
65
66
67/* WARNING: This implementation ASSUMES little endian. Does not work on big endian! */
68
69/* Remember, the time fields in the UUID must be little endian. */
70
71
72RTDECL(int) RTUuidClear(PRTUUID pUuid)
73{
74 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
75 pUuid->au64[0] = 0;
76 pUuid->au64[1] = 0;
77 return VINF_SUCCESS;
78}
79RT_EXPORT_SYMBOL(RTUuidClear);
80
81
82RTDECL(bool) RTUuidIsNull(PCRTUUID pUuid)
83{
84 AssertPtrReturn(pUuid, true);
85 return !pUuid->au64[0]
86 && !pUuid->au64[1];
87}
88RT_EXPORT_SYMBOL(RTUuidIsNull);
89
90
91RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
92{
93 /*
94 * Special cases.
95 */
96 if (pUuid1 == pUuid2)
97 return 0;
98 if (!pUuid1)
99 return RTUuidIsNull(pUuid2) ? 0 : -1;
100 if (!pUuid2)
101 return RTUuidIsNull(pUuid1) ? 0 : 1;
102 AssertPtrReturn(pUuid1, -1);
103 AssertPtrReturn(pUuid2, 1);
104
105 /*
106 * Standard cases.
107 */
108 if (pUuid1->Gen.u32TimeLow != pUuid2->Gen.u32TimeLow)
109 return pUuid1->Gen.u32TimeLow < pUuid2->Gen.u32TimeLow ? -1 : 1;
110 if (pUuid1->Gen.u16TimeMid != pUuid2->Gen.u16TimeMid)
111 return pUuid1->Gen.u16TimeMid < pUuid2->Gen.u16TimeMid ? -1 : 1;
112 if (pUuid1->Gen.u16TimeHiAndVersion != pUuid2->Gen.u16TimeHiAndVersion)
113 return pUuid1->Gen.u16TimeHiAndVersion < pUuid2->Gen.u16TimeHiAndVersion ? -1 : 1;
114 if (pUuid1->Gen.u8ClockSeqHiAndReserved != pUuid2->Gen.u8ClockSeqHiAndReserved)
115 return pUuid1->Gen.u8ClockSeqHiAndReserved < pUuid2->Gen.u8ClockSeqHiAndReserved ? -1 : 1;
116 if (pUuid1->Gen.u8ClockSeqLow != pUuid2->Gen.u8ClockSeqLow)
117 return pUuid1->Gen.u8ClockSeqLow < pUuid2->Gen.u8ClockSeqLow ? -1 : 1;
118 if (pUuid1->Gen.au8Node[0] != pUuid2->Gen.au8Node[0])
119 return pUuid1->Gen.au8Node[0] < pUuid2->Gen.au8Node[0] ? -1 : 1;
120 if (pUuid1->Gen.au8Node[1] != pUuid2->Gen.au8Node[1])
121 return pUuid1->Gen.au8Node[1] < pUuid2->Gen.au8Node[1] ? -1 : 1;
122 if (pUuid1->Gen.au8Node[2] != pUuid2->Gen.au8Node[2])
123 return pUuid1->Gen.au8Node[2] < pUuid2->Gen.au8Node[2] ? -1 : 1;
124 if (pUuid1->Gen.au8Node[3] != pUuid2->Gen.au8Node[3])
125 return pUuid1->Gen.au8Node[3] < pUuid2->Gen.au8Node[3] ? -1 : 1;
126 if (pUuid1->Gen.au8Node[4] != pUuid2->Gen.au8Node[4])
127 return pUuid1->Gen.au8Node[4] < pUuid2->Gen.au8Node[4] ? -1 : 1;
128 if (pUuid1->Gen.au8Node[5] != pUuid2->Gen.au8Node[5])
129 return pUuid1->Gen.au8Node[5] < pUuid2->Gen.au8Node[5] ? -1 : 1;
130 return 0;
131}
132RT_EXPORT_SYMBOL(RTUuidCompare);
133
134
135RTDECL(int) RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString2)
136{
137 RTUUID Uuid2;
138 int rc;
139
140 /* check params */
141 AssertPtrReturn(pUuid1, -1);
142 AssertPtrReturn(pszString2, 1);
143
144 /*
145 * Try convert the string to a UUID and then compare the two.
146 */
147 rc = RTUuidFromStr(&Uuid2, pszString2);
148 AssertRCReturn(rc, 1);
149
150 return RTUuidCompare(pUuid1, &Uuid2);
151}
152RT_EXPORT_SYMBOL(RTUuidCompareStr);
153
154
155RTDECL(int) RTUuidCompare2Strs(const char *pszString1, const char *pszString2)
156{
157 RTUUID Uuid1;
158 RTUUID Uuid2;
159 int rc;
160
161 /* check params */
162 AssertPtrReturn(pszString1, -1);
163 AssertPtrReturn(pszString2, 1);
164
165 /*
166 * Try convert the strings to UUIDs and then compare them.
167 */
168 rc = RTUuidFromStr(&Uuid1, pszString1);
169 AssertRCReturn(rc, -1);
170
171 rc = RTUuidFromStr(&Uuid2, pszString2);
172 AssertRCReturn(rc, 1);
173
174 return RTUuidCompare(&Uuid1, &Uuid2);
175}
176RT_EXPORT_SYMBOL(RTUuidCompare2Strs);
177
178
179RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString)
180{
181 uint32_t u32TimeLow;
182 unsigned u;
183
184 /* validate parameters */
185 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
186 AssertPtrReturn(pszString, VERR_INVALID_PARAMETER);
187 AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
188
189 /*
190 * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
191 * pUuid->Gen.u32TimeLow,
192 * pUuid->Gen.u16TimeMin,
193 * pUuid->Gen.u16TimeHiAndVersion,
194 * pUuid->Gen.u16ClockSeq & 0xff,
195 * pUuid->Gen.u16ClockSeq >> 8,
196 * pUuid->Gen.au8Node[0],
197 * pUuid->Gen.au8Node[1],
198 * pUuid->Gen.au8Node[2],
199 * pUuid->Gen.au8Node[3],
200 * pUuid->Gen.au8Node[4],
201 * pUuid->Gen.au8Node[5]);
202 */
203 u32TimeLow = pUuid->Gen.u32TimeLow;
204 pszString[ 0] = g_achDigits[(u32TimeLow >> 28)/*& 0xf*/];
205 pszString[ 1] = g_achDigits[(u32TimeLow >> 24) & 0xf];
206 pszString[ 2] = g_achDigits[(u32TimeLow >> 20) & 0xf];
207 pszString[ 3] = g_achDigits[(u32TimeLow >> 16) & 0xf];
208 pszString[ 4] = g_achDigits[(u32TimeLow >> 12) & 0xf];
209 pszString[ 5] = g_achDigits[(u32TimeLow >> 8) & 0xf];
210 pszString[ 6] = g_achDigits[(u32TimeLow >> 4) & 0xf];
211 pszString[ 7] = g_achDigits[(u32TimeLow/*>>0*/)& 0xf];
212 pszString[ 8] = '-';
213 u = pUuid->Gen.u16TimeMid;
214 pszString[ 9] = g_achDigits[(u >> 12)/*& 0xf*/];
215 pszString[10] = g_achDigits[(u >> 8) & 0xf];
216 pszString[11] = g_achDigits[(u >> 4) & 0xf];
217 pszString[12] = g_achDigits[(u/*>>0*/)& 0xf];
218 pszString[13] = '-';
219 u = pUuid->Gen.u16TimeHiAndVersion;
220 pszString[14] = g_achDigits[(u >> 12)/*& 0xf*/];
221 pszString[15] = g_achDigits[(u >> 8) & 0xf];
222 pszString[16] = g_achDigits[(u >> 4) & 0xf];
223 pszString[17] = g_achDigits[(u/*>>0*/)& 0xf];
224 pszString[18] = '-';
225 pszString[19] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved >> 4];
226 pszString[20] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved & 0xf];
227 pszString[21] = g_achDigits[pUuid->Gen.u8ClockSeqLow >> 4];
228 pszString[22] = g_achDigits[pUuid->Gen.u8ClockSeqLow & 0xf];
229 pszString[23] = '-';
230 pszString[24] = g_achDigits[pUuid->Gen.au8Node[0] >> 4];
231 pszString[25] = g_achDigits[pUuid->Gen.au8Node[0] & 0xf];
232 pszString[26] = g_achDigits[pUuid->Gen.au8Node[1] >> 4];
233 pszString[27] = g_achDigits[pUuid->Gen.au8Node[1] & 0xf];
234 pszString[28] = g_achDigits[pUuid->Gen.au8Node[2] >> 4];
235 pszString[29] = g_achDigits[pUuid->Gen.au8Node[2] & 0xf];
236 pszString[30] = g_achDigits[pUuid->Gen.au8Node[3] >> 4];
237 pszString[31] = g_achDigits[pUuid->Gen.au8Node[3] & 0xf];
238 pszString[32] = g_achDigits[pUuid->Gen.au8Node[4] >> 4];
239 pszString[33] = g_achDigits[pUuid->Gen.au8Node[4] & 0xf];
240 pszString[34] = g_achDigits[pUuid->Gen.au8Node[5] >> 4];
241 pszString[35] = g_achDigits[pUuid->Gen.au8Node[5] & 0xf];
242 pszString[36] = '\0';
243
244 return VINF_SUCCESS;
245}
246RT_EXPORT_SYMBOL(RTUuidToStr);
247
248
249RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
250{
251 bool fHaveBraces;
252
253 /*
254 * Validate parameters.
255 */
256 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
257 AssertPtrReturn(pszString, VERR_INVALID_PARAMETER);
258
259 fHaveBraces = pszString[0] == '{';
260 pszString += fHaveBraces;
261
262#define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
263#define MY_ISXDIGIT(ch) (g_au8Digits[(ch) & 0xff] != 0xff)
264 MY_CHECK(MY_ISXDIGIT(pszString[ 0]));
265 MY_CHECK(MY_ISXDIGIT(pszString[ 1]));
266 MY_CHECK(MY_ISXDIGIT(pszString[ 2]));
267 MY_CHECK(MY_ISXDIGIT(pszString[ 3]));
268 MY_CHECK(MY_ISXDIGIT(pszString[ 4]));
269 MY_CHECK(MY_ISXDIGIT(pszString[ 5]));
270 MY_CHECK(MY_ISXDIGIT(pszString[ 6]));
271 MY_CHECK(MY_ISXDIGIT(pszString[ 7]));
272 MY_CHECK(pszString[ 8] == '-');
273 MY_CHECK(MY_ISXDIGIT(pszString[ 9]));
274 MY_CHECK(MY_ISXDIGIT(pszString[10]));
275 MY_CHECK(MY_ISXDIGIT(pszString[11]));
276 MY_CHECK(MY_ISXDIGIT(pszString[12]));
277 MY_CHECK(pszString[13] == '-');
278 MY_CHECK(MY_ISXDIGIT(pszString[14]));
279 MY_CHECK(MY_ISXDIGIT(pszString[15]));
280 MY_CHECK(MY_ISXDIGIT(pszString[16]));
281 MY_CHECK(MY_ISXDIGIT(pszString[17]));
282 MY_CHECK(pszString[18] == '-');
283 MY_CHECK(MY_ISXDIGIT(pszString[19]));
284 MY_CHECK(MY_ISXDIGIT(pszString[20]));
285 MY_CHECK(MY_ISXDIGIT(pszString[21]));
286 MY_CHECK(MY_ISXDIGIT(pszString[22]));
287 MY_CHECK(pszString[23] == '-');
288 MY_CHECK(MY_ISXDIGIT(pszString[24]));
289 MY_CHECK(MY_ISXDIGIT(pszString[25]));
290 MY_CHECK(MY_ISXDIGIT(pszString[26]));
291 MY_CHECK(MY_ISXDIGIT(pszString[27]));
292 MY_CHECK(MY_ISXDIGIT(pszString[28]));
293 MY_CHECK(MY_ISXDIGIT(pszString[29]));
294 MY_CHECK(MY_ISXDIGIT(pszString[30]));
295 MY_CHECK(MY_ISXDIGIT(pszString[31]));
296 MY_CHECK(MY_ISXDIGIT(pszString[32]));
297 MY_CHECK(MY_ISXDIGIT(pszString[33]));
298 MY_CHECK(MY_ISXDIGIT(pszString[34]));
299 MY_CHECK(MY_ISXDIGIT(pszString[35]));
300 if (fHaveBraces)
301 MY_CHECK(pszString[36] == '}');
302 MY_CHECK(!pszString[36 + fHaveBraces]);
303#undef MY_ISXDIGIT
304#undef MY_CHECK
305
306 /*
307 * Inverse of RTUuidToStr (see above).
308 */
309#define MY_TONUM(ch) (g_au8Digits[(ch) & 0xff])
310 pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pszString[ 0]) << 28
311 | (uint32_t)MY_TONUM(pszString[ 1]) << 24
312 | (uint32_t)MY_TONUM(pszString[ 2]) << 20
313 | (uint32_t)MY_TONUM(pszString[ 3]) << 16
314 | (uint32_t)MY_TONUM(pszString[ 4]) << 12
315 | (uint32_t)MY_TONUM(pszString[ 5]) << 8
316 | (uint32_t)MY_TONUM(pszString[ 6]) << 4
317 | (uint32_t)MY_TONUM(pszString[ 7]);
318 pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pszString[ 9]) << 12
319 | (uint16_t)MY_TONUM(pszString[10]) << 8
320 | (uint16_t)MY_TONUM(pszString[11]) << 4
321 | (uint16_t)MY_TONUM(pszString[12]);
322 pUuid->Gen.u16TimeHiAndVersion =
323 (uint16_t)MY_TONUM(pszString[14]) << 12
324 | (uint16_t)MY_TONUM(pszString[15]) << 8
325 | (uint16_t)MY_TONUM(pszString[16]) << 4
326 | (uint16_t)MY_TONUM(pszString[17]);
327 pUuid->Gen.u8ClockSeqHiAndReserved =
328 (uint16_t)MY_TONUM(pszString[19]) << 4
329 | (uint16_t)MY_TONUM(pszString[20]);
330 pUuid->Gen.u8ClockSeqLow =
331 (uint16_t)MY_TONUM(pszString[21]) << 4
332 | (uint16_t)MY_TONUM(pszString[22]);
333 pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pszString[24]) << 4
334 | (uint8_t)MY_TONUM(pszString[25]);
335 pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pszString[26]) << 4
336 | (uint8_t)MY_TONUM(pszString[27]);
337 pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pszString[28]) << 4
338 | (uint8_t)MY_TONUM(pszString[29]);
339 pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pszString[30]) << 4
340 | (uint8_t)MY_TONUM(pszString[31]);
341 pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pszString[32]) << 4
342 | (uint8_t)MY_TONUM(pszString[33]);
343 pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pszString[34]) << 4
344 | (uint8_t)MY_TONUM(pszString[35]);
345#undef MY_TONUM
346 return VINF_SUCCESS;
347}
348RT_EXPORT_SYMBOL(RTUuidFromStr);
349
350
351RTDECL(int) RTUuidToUtf16(PCRTUUID pUuid, PRTUTF16 pwszString, size_t cwcString)
352{
353 uint32_t u32TimeLow;
354 unsigned u;
355
356 /* validate parameters */
357 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
358 AssertPtrReturn(pwszString, VERR_INVALID_PARAMETER);
359 AssertReturn(cwcString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
360
361 /*
362 * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
363 * pUuid->Gen.u32TimeLow,
364 * pUuid->Gen.u16TimeMin,
365 * pUuid->Gen.u16TimeHiAndVersion,
366 * pUuid->Gen.u16ClockSeq & 0xff,
367 * pUuid->Gen.u16ClockSeq >> 8,
368 * pUuid->Gen.au8Node[0],
369 * pUuid->Gen.au8Node[1],
370 * pUuid->Gen.au8Node[2],
371 * pUuid->Gen.au8Node[3],
372 * pUuid->Gen.au8Node[4],
373 * pUuid->Gen.au8Node[5]);
374 */
375 u32TimeLow = pUuid->Gen.u32TimeLow;
376 pwszString[ 0] = g_achDigits[(u32TimeLow >> 28)/*& 0xf*/];
377 pwszString[ 1] = g_achDigits[(u32TimeLow >> 24) & 0xf];
378 pwszString[ 2] = g_achDigits[(u32TimeLow >> 20) & 0xf];
379 pwszString[ 3] = g_achDigits[(u32TimeLow >> 16) & 0xf];
380 pwszString[ 4] = g_achDigits[(u32TimeLow >> 12) & 0xf];
381 pwszString[ 5] = g_achDigits[(u32TimeLow >> 8) & 0xf];
382 pwszString[ 6] = g_achDigits[(u32TimeLow >> 4) & 0xf];
383 pwszString[ 7] = g_achDigits[(u32TimeLow/*>>0*/)& 0xf];
384 pwszString[ 8] = '-';
385 u = pUuid->Gen.u16TimeMid;
386 pwszString[ 9] = g_achDigits[(u >> 12)/*& 0xf*/];
387 pwszString[10] = g_achDigits[(u >> 8) & 0xf];
388 pwszString[11] = g_achDigits[(u >> 4) & 0xf];
389 pwszString[12] = g_achDigits[(u/*>>0*/)& 0xf];
390 pwszString[13] = '-';
391 u = pUuid->Gen.u16TimeHiAndVersion;
392 pwszString[14] = g_achDigits[(u >> 12)/*& 0xf*/];
393 pwszString[15] = g_achDigits[(u >> 8) & 0xf];
394 pwszString[16] = g_achDigits[(u >> 4) & 0xf];
395 pwszString[17] = g_achDigits[(u/*>>0*/)& 0xf];
396 pwszString[18] = '-';
397 pwszString[19] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved >> 4];
398 pwszString[20] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved & 0xf];
399 pwszString[21] = g_achDigits[pUuid->Gen.u8ClockSeqLow >> 4];
400 pwszString[22] = g_achDigits[pUuid->Gen.u8ClockSeqLow & 0xf];
401 pwszString[23] = '-';
402 pwszString[24] = g_achDigits[pUuid->Gen.au8Node[0] >> 4];
403 pwszString[25] = g_achDigits[pUuid->Gen.au8Node[0] & 0xf];
404 pwszString[26] = g_achDigits[pUuid->Gen.au8Node[1] >> 4];
405 pwszString[27] = g_achDigits[pUuid->Gen.au8Node[1] & 0xf];
406 pwszString[28] = g_achDigits[pUuid->Gen.au8Node[2] >> 4];
407 pwszString[29] = g_achDigits[pUuid->Gen.au8Node[2] & 0xf];
408 pwszString[30] = g_achDigits[pUuid->Gen.au8Node[3] >> 4];
409 pwszString[31] = g_achDigits[pUuid->Gen.au8Node[3] & 0xf];
410 pwszString[32] = g_achDigits[pUuid->Gen.au8Node[4] >> 4];
411 pwszString[33] = g_achDigits[pUuid->Gen.au8Node[4] & 0xf];
412 pwszString[34] = g_achDigits[pUuid->Gen.au8Node[5] >> 4];
413 pwszString[35] = g_achDigits[pUuid->Gen.au8Node[5] & 0xf];
414 pwszString[36] = '\0';
415
416 return VINF_SUCCESS;
417}
418RT_EXPORT_SYMBOL(RTUuidToUtf16);
419
420
421RTDECL(int) RTUuidFromUtf16(PRTUUID pUuid, PCRTUTF16 pwszString)
422{
423 bool fHaveBraces;
424
425 /*
426 * Validate parameters.
427 */
428 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
429 AssertPtrReturn(pwszString, VERR_INVALID_PARAMETER);
430
431 fHaveBraces = (pwszString[0] == '{' && pwszString[37] == '}');
432 if (fHaveBraces)
433 pwszString++;
434
435#define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
436#define MY_ISXDIGIT(ch) (!((ch) & 0xff00) && g_au8Digits[(ch) & 0xff] != 0xff)
437 MY_CHECK(MY_ISXDIGIT(pwszString[ 0]));
438 MY_CHECK(MY_ISXDIGIT(pwszString[ 1]));
439 MY_CHECK(MY_ISXDIGIT(pwszString[ 2]));
440 MY_CHECK(MY_ISXDIGIT(pwszString[ 3]));
441 MY_CHECK(MY_ISXDIGIT(pwszString[ 4]));
442 MY_CHECK(MY_ISXDIGIT(pwszString[ 5]));
443 MY_CHECK(MY_ISXDIGIT(pwszString[ 6]));
444 MY_CHECK(MY_ISXDIGIT(pwszString[ 7]));
445 MY_CHECK(pwszString[ 8] == '-');
446 MY_CHECK(MY_ISXDIGIT(pwszString[ 9]));
447 MY_CHECK(MY_ISXDIGIT(pwszString[10]));
448 MY_CHECK(MY_ISXDIGIT(pwszString[11]));
449 MY_CHECK(MY_ISXDIGIT(pwszString[12]));
450 MY_CHECK(pwszString[13] == '-');
451 MY_CHECK(MY_ISXDIGIT(pwszString[14]));
452 MY_CHECK(MY_ISXDIGIT(pwszString[15]));
453 MY_CHECK(MY_ISXDIGIT(pwszString[16]));
454 MY_CHECK(MY_ISXDIGIT(pwszString[17]));
455 MY_CHECK(pwszString[18] == '-');
456 MY_CHECK(MY_ISXDIGIT(pwszString[19]));
457 MY_CHECK(MY_ISXDIGIT(pwszString[20]));
458 MY_CHECK(MY_ISXDIGIT(pwszString[21]));
459 MY_CHECK(MY_ISXDIGIT(pwszString[22]));
460 MY_CHECK(pwszString[23] == '-');
461 MY_CHECK(MY_ISXDIGIT(pwszString[24]));
462 MY_CHECK(MY_ISXDIGIT(pwszString[25]));
463 MY_CHECK(MY_ISXDIGIT(pwszString[26]));
464 MY_CHECK(MY_ISXDIGIT(pwszString[27]));
465 MY_CHECK(MY_ISXDIGIT(pwszString[28]));
466 MY_CHECK(MY_ISXDIGIT(pwszString[29]));
467 MY_CHECK(MY_ISXDIGIT(pwszString[30]));
468 MY_CHECK(MY_ISXDIGIT(pwszString[31]));
469 MY_CHECK(MY_ISXDIGIT(pwszString[32]));
470 MY_CHECK(MY_ISXDIGIT(pwszString[33]));
471 MY_CHECK(MY_ISXDIGIT(pwszString[34]));
472 MY_CHECK(MY_ISXDIGIT(pwszString[35]));
473 MY_CHECK(!pwszString[36 + (fHaveBraces ? 1 : 0)]);
474#undef MY_ISXDIGIT
475#undef MY_CHECK
476
477 /*
478 * Inverse of RTUuidToUtf8 (see above).
479 */
480#define MY_TONUM(ch) (g_au8Digits[(ch) & 0xff])
481 pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pwszString[ 0]) << 28
482 | (uint32_t)MY_TONUM(pwszString[ 1]) << 24
483 | (uint32_t)MY_TONUM(pwszString[ 2]) << 20
484 | (uint32_t)MY_TONUM(pwszString[ 3]) << 16
485 | (uint32_t)MY_TONUM(pwszString[ 4]) << 12
486 | (uint32_t)MY_TONUM(pwszString[ 5]) << 8
487 | (uint32_t)MY_TONUM(pwszString[ 6]) << 4
488 | (uint32_t)MY_TONUM(pwszString[ 7]);
489 pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pwszString[ 9]) << 12
490 | (uint16_t)MY_TONUM(pwszString[10]) << 8
491 | (uint16_t)MY_TONUM(pwszString[11]) << 4
492 | (uint16_t)MY_TONUM(pwszString[12]);
493 pUuid->Gen.u16TimeHiAndVersion =
494 (uint16_t)MY_TONUM(pwszString[14]) << 12
495 | (uint16_t)MY_TONUM(pwszString[15]) << 8
496 | (uint16_t)MY_TONUM(pwszString[16]) << 4
497 | (uint16_t)MY_TONUM(pwszString[17]);
498 pUuid->Gen.u8ClockSeqHiAndReserved =
499 (uint16_t)MY_TONUM(pwszString[19]) << 4
500 | (uint16_t)MY_TONUM(pwszString[20]);
501 pUuid->Gen.u8ClockSeqLow =
502 (uint16_t)MY_TONUM(pwszString[21]) << 4
503 | (uint16_t)MY_TONUM(pwszString[22]);
504 pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pwszString[24]) << 4
505 | (uint8_t)MY_TONUM(pwszString[25]);
506 pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pwszString[26]) << 4
507 | (uint8_t)MY_TONUM(pwszString[27]);
508 pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pwszString[28]) << 4
509 | (uint8_t)MY_TONUM(pwszString[29]);
510 pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pwszString[30]) << 4
511 | (uint8_t)MY_TONUM(pwszString[31]);
512 pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pwszString[32]) << 4
513 | (uint8_t)MY_TONUM(pwszString[33]);
514 pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pwszString[34]) << 4
515 | (uint8_t)MY_TONUM(pwszString[35]);
516#undef MY_TONUM
517 return VINF_SUCCESS;
518}
519RT_EXPORT_SYMBOL(RTUuidFromUtf16);
520
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