VirtualBox

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

Last change on this file since 28587 was 25961, checked in by vboxsync, 15 years ago

IPRT: Added RTUuidCompare2Strs; renamed tstUuid to tstRTUuid.

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