VirtualBox

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

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

IPRT: Fixed -Wshadow warnings, found two bugs in error paths.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 21.1 KB
Line 
1/* $Id: uuid-generic.cpp 25000 2009-11-26 14:22:44Z 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 *pszString)
140{
141 RTUUID Uuid2;
142 int rc;
143
144 /* check params */
145 AssertPtrReturn(pUuid1, -1);
146 AssertPtrReturn(pszString, 1);
147
148 /*
149 * Try convert the string to a UUID and then compare the two.
150 */
151 rc = RTUuidFromStr(&Uuid2, pszString);
152 AssertRCReturn(rc, 1);
153
154 return RTUuidCompare(pUuid1, &Uuid2);
155}
156RT_EXPORT_SYMBOL(RTUuidCompareStr);
157
158
159RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString)
160{
161 uint32_t u32TimeLow;
162 unsigned u;
163
164 /* validate parameters */
165 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
166 AssertPtrReturn(pszString, VERR_INVALID_PARAMETER);
167 AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
168
169 /*
170 * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
171 * pUuid->Gen.u32TimeLow,
172 * pUuid->Gen.u16TimeMin,
173 * pUuid->Gen.u16TimeHiAndVersion,
174 * pUuid->Gen.u16ClockSeq & 0xff,
175 * pUuid->Gen.u16ClockSeq >> 8,
176 * pUuid->Gen.au8Node[0],
177 * pUuid->Gen.au8Node[1],
178 * pUuid->Gen.au8Node[2],
179 * pUuid->Gen.au8Node[3],
180 * pUuid->Gen.au8Node[4],
181 * pUuid->Gen.au8Node[5]);
182 */
183 u32TimeLow = pUuid->Gen.u32TimeLow;
184 pszString[ 0] = g_achDigits[(u32TimeLow >> 28)/*& 0xf*/];
185 pszString[ 1] = g_achDigits[(u32TimeLow >> 24) & 0xf];
186 pszString[ 2] = g_achDigits[(u32TimeLow >> 20) & 0xf];
187 pszString[ 3] = g_achDigits[(u32TimeLow >> 16) & 0xf];
188 pszString[ 4] = g_achDigits[(u32TimeLow >> 12) & 0xf];
189 pszString[ 5] = g_achDigits[(u32TimeLow >> 8) & 0xf];
190 pszString[ 6] = g_achDigits[(u32TimeLow >> 4) & 0xf];
191 pszString[ 7] = g_achDigits[(u32TimeLow/*>>0*/)& 0xf];
192 pszString[ 8] = '-';
193 u = pUuid->Gen.u16TimeMid;
194 pszString[ 9] = g_achDigits[(u >> 12)/*& 0xf*/];
195 pszString[10] = g_achDigits[(u >> 8) & 0xf];
196 pszString[11] = g_achDigits[(u >> 4) & 0xf];
197 pszString[12] = g_achDigits[(u/*>>0*/)& 0xf];
198 pszString[13] = '-';
199 u = pUuid->Gen.u16TimeHiAndVersion;
200 pszString[14] = g_achDigits[(u >> 12)/*& 0xf*/];
201 pszString[15] = g_achDigits[(u >> 8) & 0xf];
202 pszString[16] = g_achDigits[(u >> 4) & 0xf];
203 pszString[17] = g_achDigits[(u/*>>0*/)& 0xf];
204 pszString[18] = '-';
205 pszString[19] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved >> 4];
206 pszString[20] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved & 0xf];
207 pszString[21] = g_achDigits[pUuid->Gen.u8ClockSeqLow >> 4];
208 pszString[22] = g_achDigits[pUuid->Gen.u8ClockSeqLow & 0xf];
209 pszString[23] = '-';
210 pszString[24] = g_achDigits[pUuid->Gen.au8Node[0] >> 4];
211 pszString[25] = g_achDigits[pUuid->Gen.au8Node[0] & 0xf];
212 pszString[26] = g_achDigits[pUuid->Gen.au8Node[1] >> 4];
213 pszString[27] = g_achDigits[pUuid->Gen.au8Node[1] & 0xf];
214 pszString[28] = g_achDigits[pUuid->Gen.au8Node[2] >> 4];
215 pszString[29] = g_achDigits[pUuid->Gen.au8Node[2] & 0xf];
216 pszString[30] = g_achDigits[pUuid->Gen.au8Node[3] >> 4];
217 pszString[31] = g_achDigits[pUuid->Gen.au8Node[3] & 0xf];
218 pszString[32] = g_achDigits[pUuid->Gen.au8Node[4] >> 4];
219 pszString[33] = g_achDigits[pUuid->Gen.au8Node[4] & 0xf];
220 pszString[34] = g_achDigits[pUuid->Gen.au8Node[5] >> 4];
221 pszString[35] = g_achDigits[pUuid->Gen.au8Node[5] & 0xf];
222 pszString[36] = '\0';
223
224 return VINF_SUCCESS;
225}
226RT_EXPORT_SYMBOL(RTUuidToStr);
227
228
229RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
230{
231 /*
232 * Validate parameters.
233 */
234 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
235 AssertPtrReturn(pszString, VERR_INVALID_PARAMETER);
236
237#define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
238#define MY_ISXDIGIT(ch) (g_au8Digits[(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]);
276#undef MY_ISXDIGIT
277#undef MY_CHECK
278
279 /*
280 * Inverse of RTUuidToStr (see above).
281 */
282#define MY_TONUM(ch) (g_au8Digits[(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.u8ClockSeqHiAndReserved =
301 (uint16_t)MY_TONUM(pszString[19]) << 4
302 | (uint16_t)MY_TONUM(pszString[20]);
303 pUuid->Gen.u8ClockSeqLow =
304 (uint16_t)MY_TONUM(pszString[21]) << 4
305 | (uint16_t)MY_TONUM(pszString[22]);
306 pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pszString[24]) << 4
307 | (uint8_t)MY_TONUM(pszString[25]);
308 pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pszString[26]) << 4
309 | (uint8_t)MY_TONUM(pszString[27]);
310 pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pszString[28]) << 4
311 | (uint8_t)MY_TONUM(pszString[29]);
312 pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pszString[30]) << 4
313 | (uint8_t)MY_TONUM(pszString[31]);
314 pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pszString[32]) << 4
315 | (uint8_t)MY_TONUM(pszString[33]);
316 pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pszString[34]) << 4
317 | (uint8_t)MY_TONUM(pszString[35]);
318#undef MY_TONUM
319 return VINF_SUCCESS;
320}
321RT_EXPORT_SYMBOL(RTUuidFromStr);
322
323
324RTDECL(int) RTUuidToUtf16(PCRTUUID pUuid, PRTUTF16 pwszString, size_t cwcString)
325{
326 uint32_t u32TimeLow;
327 unsigned u;
328
329 /* validate parameters */
330 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
331 AssertPtrReturn(pwszString, VERR_INVALID_PARAMETER);
332 AssertReturn(cwcString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
333
334 /*
335 * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
336 * pUuid->Gen.u32TimeLow,
337 * pUuid->Gen.u16TimeMin,
338 * pUuid->Gen.u16TimeHiAndVersion,
339 * pUuid->Gen.u16ClockSeq & 0xff,
340 * pUuid->Gen.u16ClockSeq >> 8,
341 * pUuid->Gen.au8Node[0],
342 * pUuid->Gen.au8Node[1],
343 * pUuid->Gen.au8Node[2],
344 * pUuid->Gen.au8Node[3],
345 * pUuid->Gen.au8Node[4],
346 * pUuid->Gen.au8Node[5]);
347 */
348 u32TimeLow = pUuid->Gen.u32TimeLow;
349 pwszString[ 0] = g_achDigits[(u32TimeLow >> 28)/*& 0xf*/];
350 pwszString[ 1] = g_achDigits[(u32TimeLow >> 24) & 0xf];
351 pwszString[ 2] = g_achDigits[(u32TimeLow >> 20) & 0xf];
352 pwszString[ 3] = g_achDigits[(u32TimeLow >> 16) & 0xf];
353 pwszString[ 4] = g_achDigits[(u32TimeLow >> 12) & 0xf];
354 pwszString[ 5] = g_achDigits[(u32TimeLow >> 8) & 0xf];
355 pwszString[ 6] = g_achDigits[(u32TimeLow >> 4) & 0xf];
356 pwszString[ 7] = g_achDigits[(u32TimeLow/*>>0*/)& 0xf];
357 pwszString[ 8] = '-';
358 u = pUuid->Gen.u16TimeMid;
359 pwszString[ 9] = g_achDigits[(u >> 12)/*& 0xf*/];
360 pwszString[10] = g_achDigits[(u >> 8) & 0xf];
361 pwszString[11] = g_achDigits[(u >> 4) & 0xf];
362 pwszString[12] = g_achDigits[(u/*>>0*/)& 0xf];
363 pwszString[13] = '-';
364 u = pUuid->Gen.u16TimeHiAndVersion;
365 pwszString[14] = g_achDigits[(u >> 12)/*& 0xf*/];
366 pwszString[15] = g_achDigits[(u >> 8) & 0xf];
367 pwszString[16] = g_achDigits[(u >> 4) & 0xf];
368 pwszString[17] = g_achDigits[(u/*>>0*/)& 0xf];
369 pwszString[18] = '-';
370 pwszString[19] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved >> 4];
371 pwszString[20] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved & 0xf];
372 pwszString[21] = g_achDigits[pUuid->Gen.u8ClockSeqLow >> 4];
373 pwszString[22] = g_achDigits[pUuid->Gen.u8ClockSeqLow & 0xf];
374 pwszString[23] = '-';
375 pwszString[24] = g_achDigits[pUuid->Gen.au8Node[0] >> 4];
376 pwszString[25] = g_achDigits[pUuid->Gen.au8Node[0] & 0xf];
377 pwszString[26] = g_achDigits[pUuid->Gen.au8Node[1] >> 4];
378 pwszString[27] = g_achDigits[pUuid->Gen.au8Node[1] & 0xf];
379 pwszString[28] = g_achDigits[pUuid->Gen.au8Node[2] >> 4];
380 pwszString[29] = g_achDigits[pUuid->Gen.au8Node[2] & 0xf];
381 pwszString[30] = g_achDigits[pUuid->Gen.au8Node[3] >> 4];
382 pwszString[31] = g_achDigits[pUuid->Gen.au8Node[3] & 0xf];
383 pwszString[32] = g_achDigits[pUuid->Gen.au8Node[4] >> 4];
384 pwszString[33] = g_achDigits[pUuid->Gen.au8Node[4] & 0xf];
385 pwszString[34] = g_achDigits[pUuid->Gen.au8Node[5] >> 4];
386 pwszString[35] = g_achDigits[pUuid->Gen.au8Node[5] & 0xf];
387 pwszString[36] = '\0';
388
389 return VINF_SUCCESS;
390}
391RT_EXPORT_SYMBOL(RTUuidToUtf16);
392
393
394RTDECL(int) RTUuidFromUtf16(PRTUUID pUuid, PCRTUTF16 pwszString)
395{
396
397 /*
398 * Validate parameters.
399 */
400 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
401 AssertPtrReturn(pwszString, VERR_INVALID_PARAMETER);
402
403#define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
404#define MY_ISXDIGIT(ch) (!((ch) & 0xff00) && g_au8Digits[(ch) & 0xff] != 0xff)
405 MY_CHECK(MY_ISXDIGIT(pwszString[ 0]));
406 MY_CHECK(MY_ISXDIGIT(pwszString[ 1]));
407 MY_CHECK(MY_ISXDIGIT(pwszString[ 2]));
408 MY_CHECK(MY_ISXDIGIT(pwszString[ 3]));
409 MY_CHECK(MY_ISXDIGIT(pwszString[ 4]));
410 MY_CHECK(MY_ISXDIGIT(pwszString[ 5]));
411 MY_CHECK(MY_ISXDIGIT(pwszString[ 6]));
412 MY_CHECK(MY_ISXDIGIT(pwszString[ 7]));
413 MY_CHECK(pwszString[ 8] == '-');
414 MY_CHECK(MY_ISXDIGIT(pwszString[ 9]));
415 MY_CHECK(MY_ISXDIGIT(pwszString[10]));
416 MY_CHECK(MY_ISXDIGIT(pwszString[11]));
417 MY_CHECK(MY_ISXDIGIT(pwszString[12]));
418 MY_CHECK(pwszString[13] == '-');
419 MY_CHECK(MY_ISXDIGIT(pwszString[14]));
420 MY_CHECK(MY_ISXDIGIT(pwszString[15]));
421 MY_CHECK(MY_ISXDIGIT(pwszString[16]));
422 MY_CHECK(MY_ISXDIGIT(pwszString[17]));
423 MY_CHECK(pwszString[18] == '-');
424 MY_CHECK(MY_ISXDIGIT(pwszString[19]));
425 MY_CHECK(MY_ISXDIGIT(pwszString[20]));
426 MY_CHECK(MY_ISXDIGIT(pwszString[21]));
427 MY_CHECK(MY_ISXDIGIT(pwszString[22]));
428 MY_CHECK(pwszString[23] == '-');
429 MY_CHECK(MY_ISXDIGIT(pwszString[24]));
430 MY_CHECK(MY_ISXDIGIT(pwszString[25]));
431 MY_CHECK(MY_ISXDIGIT(pwszString[26]));
432 MY_CHECK(MY_ISXDIGIT(pwszString[27]));
433 MY_CHECK(MY_ISXDIGIT(pwszString[28]));
434 MY_CHECK(MY_ISXDIGIT(pwszString[29]));
435 MY_CHECK(MY_ISXDIGIT(pwszString[30]));
436 MY_CHECK(MY_ISXDIGIT(pwszString[31]));
437 MY_CHECK(MY_ISXDIGIT(pwszString[32]));
438 MY_CHECK(MY_ISXDIGIT(pwszString[33]));
439 MY_CHECK(MY_ISXDIGIT(pwszString[34]));
440 MY_CHECK(MY_ISXDIGIT(pwszString[35]));
441 MY_CHECK(!pwszString[36]);
442#undef MY_ISXDIGIT
443#undef MY_CHECK
444
445 /*
446 * Inverse of RTUuidToUtf8 (see above).
447 */
448#define MY_TONUM(ch) (g_au8Digits[(ch) & 0xff])
449 pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pwszString[ 0]) << 28
450 | (uint32_t)MY_TONUM(pwszString[ 1]) << 24
451 | (uint32_t)MY_TONUM(pwszString[ 2]) << 20
452 | (uint32_t)MY_TONUM(pwszString[ 3]) << 16
453 | (uint32_t)MY_TONUM(pwszString[ 4]) << 12
454 | (uint32_t)MY_TONUM(pwszString[ 5]) << 8
455 | (uint32_t)MY_TONUM(pwszString[ 6]) << 4
456 | (uint32_t)MY_TONUM(pwszString[ 7]);
457 pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pwszString[ 9]) << 12
458 | (uint16_t)MY_TONUM(pwszString[10]) << 8
459 | (uint16_t)MY_TONUM(pwszString[11]) << 4
460 | (uint16_t)MY_TONUM(pwszString[12]);
461 pUuid->Gen.u16TimeHiAndVersion =
462 (uint16_t)MY_TONUM(pwszString[14]) << 12
463 | (uint16_t)MY_TONUM(pwszString[15]) << 8
464 | (uint16_t)MY_TONUM(pwszString[16]) << 4
465 | (uint16_t)MY_TONUM(pwszString[17]);
466 pUuid->Gen.u8ClockSeqHiAndReserved =
467 (uint16_t)MY_TONUM(pwszString[19]) << 4
468 | (uint16_t)MY_TONUM(pwszString[20]);
469 pUuid->Gen.u8ClockSeqLow =
470 (uint16_t)MY_TONUM(pwszString[21]) << 4
471 | (uint16_t)MY_TONUM(pwszString[22]);
472 pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pwszString[24]) << 4
473 | (uint8_t)MY_TONUM(pwszString[25]);
474 pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pwszString[26]) << 4
475 | (uint8_t)MY_TONUM(pwszString[27]);
476 pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pwszString[28]) << 4
477 | (uint8_t)MY_TONUM(pwszString[29]);
478 pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pwszString[30]) << 4
479 | (uint8_t)MY_TONUM(pwszString[31]);
480 pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pwszString[32]) << 4
481 | (uint8_t)MY_TONUM(pwszString[33]);
482 pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pwszString[34]) << 4
483 | (uint8_t)MY_TONUM(pwszString[35]);
484#undef MY_TONUM
485 return VINF_SUCCESS;
486}
487RT_EXPORT_SYMBOL(RTUuidFromUtf16);
488
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