1 | /* $Id: uuid-generic.cpp 21337 2009-07-07 14:58:27Z 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. */
|
---|
47 | static 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. */
|
---|
67 | static 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 |
|
---|
76 | RTDECL(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 | }
|
---|
83 | RT_EXPORT_SYMBOL(RTUuidClear);
|
---|
84 |
|
---|
85 |
|
---|
86 | RTDECL(bool) RTUuidIsNull(PCRTUUID pUuid)
|
---|
87 | {
|
---|
88 | AssertPtrReturn(pUuid, true);
|
---|
89 | return !pUuid->au64[0]
|
---|
90 | && !pUuid->au64[1];
|
---|
91 | }
|
---|
92 | RT_EXPORT_SYMBOL(RTUuidIsNull);
|
---|
93 |
|
---|
94 |
|
---|
95 | RTDECL(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 | }
|
---|
136 | RT_EXPORT_SYMBOL(RTUuidCompare);
|
---|
137 |
|
---|
138 |
|
---|
139 | RTDECL(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 | }
|
---|
156 | RT_EXPORT_SYMBOL(RTUuidCompareStr);
|
---|
157 |
|
---|
158 |
|
---|
159 | RTDECL(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 | }
|
---|
226 | RT_EXPORT_SYMBOL(RTUuidToStr);
|
---|
227 |
|
---|
228 |
|
---|
229 | RTDECL(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 | }
|
---|
321 | RT_EXPORT_SYMBOL(RTUuidFromStr);
|
---|
322 |
|
---|
323 |
|
---|
324 | RTDECL(int) RTUuidToUtf16(PCRTUUID pUuid, PRTUTF16 pwszString, size_t cwcString)
|
---|
325 | {
|
---|
326 | static const char g_achDigits[17] = "0123456789abcdef";
|
---|
327 | uint32_t u32TimeLow;
|
---|
328 | unsigned u;
|
---|
329 |
|
---|
330 | /* validate parameters */
|
---|
331 | AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
|
---|
332 | AssertPtrReturn(pwszString, VERR_INVALID_PARAMETER);
|
---|
333 | AssertReturn(cwcString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
|
---|
334 |
|
---|
335 | /*
|
---|
336 | * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
---|
337 | * pUuid->Gen.u32TimeLow,
|
---|
338 | * pUuid->Gen.u16TimeMin,
|
---|
339 | * pUuid->Gen.u16TimeHiAndVersion,
|
---|
340 | * pUuid->Gen.u16ClockSeq & 0xff,
|
---|
341 | * pUuid->Gen.u16ClockSeq >> 8,
|
---|
342 | * pUuid->Gen.au8Node[0],
|
---|
343 | * pUuid->Gen.au8Node[1],
|
---|
344 | * pUuid->Gen.au8Node[2],
|
---|
345 | * pUuid->Gen.au8Node[3],
|
---|
346 | * pUuid->Gen.au8Node[4],
|
---|
347 | * pUuid->Gen.au8Node[5]);
|
---|
348 | */
|
---|
349 | u32TimeLow = pUuid->Gen.u32TimeLow;
|
---|
350 | pwszString[ 0] = g_achDigits[(u32TimeLow >> 28)/*& 0xf*/];
|
---|
351 | pwszString[ 1] = g_achDigits[(u32TimeLow >> 24) & 0xf];
|
---|
352 | pwszString[ 2] = g_achDigits[(u32TimeLow >> 20) & 0xf];
|
---|
353 | pwszString[ 3] = g_achDigits[(u32TimeLow >> 16) & 0xf];
|
---|
354 | pwszString[ 4] = g_achDigits[(u32TimeLow >> 12) & 0xf];
|
---|
355 | pwszString[ 5] = g_achDigits[(u32TimeLow >> 8) & 0xf];
|
---|
356 | pwszString[ 6] = g_achDigits[(u32TimeLow >> 4) & 0xf];
|
---|
357 | pwszString[ 7] = g_achDigits[(u32TimeLow/*>>0*/)& 0xf];
|
---|
358 | pwszString[ 8] = '-';
|
---|
359 | u = pUuid->Gen.u16TimeMid;
|
---|
360 | pwszString[ 9] = g_achDigits[(u >> 12)/*& 0xf*/];
|
---|
361 | pwszString[10] = g_achDigits[(u >> 8) & 0xf];
|
---|
362 | pwszString[11] = g_achDigits[(u >> 4) & 0xf];
|
---|
363 | pwszString[12] = g_achDigits[(u/*>>0*/)& 0xf];
|
---|
364 | pwszString[13] = '-';
|
---|
365 | u = pUuid->Gen.u16TimeHiAndVersion;
|
---|
366 | pwszString[14] = g_achDigits[(u >> 12)/*& 0xf*/];
|
---|
367 | pwszString[15] = g_achDigits[(u >> 8) & 0xf];
|
---|
368 | pwszString[16] = g_achDigits[(u >> 4) & 0xf];
|
---|
369 | pwszString[17] = g_achDigits[(u/*>>0*/)& 0xf];
|
---|
370 | pwszString[18] = '-';
|
---|
371 | pwszString[19] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved >> 4];
|
---|
372 | pwszString[20] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved & 0xf];
|
---|
373 | pwszString[21] = g_achDigits[pUuid->Gen.u8ClockSeqLow >> 4];
|
---|
374 | pwszString[22] = g_achDigits[pUuid->Gen.u8ClockSeqLow & 0xf];
|
---|
375 | pwszString[23] = '-';
|
---|
376 | pwszString[24] = g_achDigits[pUuid->Gen.au8Node[0] >> 4];
|
---|
377 | pwszString[25] = g_achDigits[pUuid->Gen.au8Node[0] & 0xf];
|
---|
378 | pwszString[26] = g_achDigits[pUuid->Gen.au8Node[1] >> 4];
|
---|
379 | pwszString[27] = g_achDigits[pUuid->Gen.au8Node[1] & 0xf];
|
---|
380 | pwszString[28] = g_achDigits[pUuid->Gen.au8Node[2] >> 4];
|
---|
381 | pwszString[29] = g_achDigits[pUuid->Gen.au8Node[2] & 0xf];
|
---|
382 | pwszString[30] = g_achDigits[pUuid->Gen.au8Node[3] >> 4];
|
---|
383 | pwszString[31] = g_achDigits[pUuid->Gen.au8Node[3] & 0xf];
|
---|
384 | pwszString[32] = g_achDigits[pUuid->Gen.au8Node[4] >> 4];
|
---|
385 | pwszString[33] = g_achDigits[pUuid->Gen.au8Node[4] & 0xf];
|
---|
386 | pwszString[34] = g_achDigits[pUuid->Gen.au8Node[5] >> 4];
|
---|
387 | pwszString[35] = g_achDigits[pUuid->Gen.au8Node[5] & 0xf];
|
---|
388 | pwszString[36] = '\0';
|
---|
389 |
|
---|
390 | return VINF_SUCCESS;
|
---|
391 | }
|
---|
392 | RT_EXPORT_SYMBOL(RTUuidToUtf16);
|
---|
393 |
|
---|
394 |
|
---|
395 | RTDECL(int) RTUuidFromUtf16(PRTUUID pUuid, PCRTUTF16 pwszString)
|
---|
396 | {
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * Validate parameters.
|
---|
400 | */
|
---|
401 | AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
|
---|
402 | AssertPtrReturn(pwszString, VERR_INVALID_PARAMETER);
|
---|
403 |
|
---|
404 | #define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
|
---|
405 | #define MY_ISXDIGIT(ch) (!((ch) & 0xff00) && g_au8Digits[(ch) & 0xff] != 0xff)
|
---|
406 | MY_CHECK(MY_ISXDIGIT(pwszString[ 0]));
|
---|
407 | MY_CHECK(MY_ISXDIGIT(pwszString[ 1]));
|
---|
408 | MY_CHECK(MY_ISXDIGIT(pwszString[ 2]));
|
---|
409 | MY_CHECK(MY_ISXDIGIT(pwszString[ 3]));
|
---|
410 | MY_CHECK(MY_ISXDIGIT(pwszString[ 4]));
|
---|
411 | MY_CHECK(MY_ISXDIGIT(pwszString[ 5]));
|
---|
412 | MY_CHECK(MY_ISXDIGIT(pwszString[ 6]));
|
---|
413 | MY_CHECK(MY_ISXDIGIT(pwszString[ 7]));
|
---|
414 | MY_CHECK(pwszString[ 8] == '-');
|
---|
415 | MY_CHECK(MY_ISXDIGIT(pwszString[ 9]));
|
---|
416 | MY_CHECK(MY_ISXDIGIT(pwszString[10]));
|
---|
417 | MY_CHECK(MY_ISXDIGIT(pwszString[11]));
|
---|
418 | MY_CHECK(MY_ISXDIGIT(pwszString[12]));
|
---|
419 | MY_CHECK(pwszString[13] == '-');
|
---|
420 | MY_CHECK(MY_ISXDIGIT(pwszString[14]));
|
---|
421 | MY_CHECK(MY_ISXDIGIT(pwszString[15]));
|
---|
422 | MY_CHECK(MY_ISXDIGIT(pwszString[16]));
|
---|
423 | MY_CHECK(MY_ISXDIGIT(pwszString[17]));
|
---|
424 | MY_CHECK(pwszString[18] == '-');
|
---|
425 | MY_CHECK(MY_ISXDIGIT(pwszString[19]));
|
---|
426 | MY_CHECK(MY_ISXDIGIT(pwszString[20]));
|
---|
427 | MY_CHECK(MY_ISXDIGIT(pwszString[21]));
|
---|
428 | MY_CHECK(MY_ISXDIGIT(pwszString[22]));
|
---|
429 | MY_CHECK(pwszString[23] == '-');
|
---|
430 | MY_CHECK(MY_ISXDIGIT(pwszString[24]));
|
---|
431 | MY_CHECK(MY_ISXDIGIT(pwszString[25]));
|
---|
432 | MY_CHECK(MY_ISXDIGIT(pwszString[26]));
|
---|
433 | MY_CHECK(MY_ISXDIGIT(pwszString[27]));
|
---|
434 | MY_CHECK(MY_ISXDIGIT(pwszString[28]));
|
---|
435 | MY_CHECK(MY_ISXDIGIT(pwszString[29]));
|
---|
436 | MY_CHECK(MY_ISXDIGIT(pwszString[30]));
|
---|
437 | MY_CHECK(MY_ISXDIGIT(pwszString[31]));
|
---|
438 | MY_CHECK(MY_ISXDIGIT(pwszString[32]));
|
---|
439 | MY_CHECK(MY_ISXDIGIT(pwszString[33]));
|
---|
440 | MY_CHECK(MY_ISXDIGIT(pwszString[34]));
|
---|
441 | MY_CHECK(MY_ISXDIGIT(pwszString[35]));
|
---|
442 | MY_CHECK(!pwszString[36]);
|
---|
443 | #undef MY_ISXDIGIT
|
---|
444 | #undef MY_CHECK
|
---|
445 |
|
---|
446 | /*
|
---|
447 | * Inverse of RTUuidToUtf8 (see above).
|
---|
448 | */
|
---|
449 | #define MY_TONUM(ch) (g_au8Digits[(ch) & 0xff])
|
---|
450 | pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pwszString[ 0]) << 28
|
---|
451 | | (uint32_t)MY_TONUM(pwszString[ 1]) << 24
|
---|
452 | | (uint32_t)MY_TONUM(pwszString[ 2]) << 20
|
---|
453 | | (uint32_t)MY_TONUM(pwszString[ 3]) << 16
|
---|
454 | | (uint32_t)MY_TONUM(pwszString[ 4]) << 12
|
---|
455 | | (uint32_t)MY_TONUM(pwszString[ 5]) << 8
|
---|
456 | | (uint32_t)MY_TONUM(pwszString[ 6]) << 4
|
---|
457 | | (uint32_t)MY_TONUM(pwszString[ 7]);
|
---|
458 | pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pwszString[ 9]) << 12
|
---|
459 | | (uint16_t)MY_TONUM(pwszString[10]) << 8
|
---|
460 | | (uint16_t)MY_TONUM(pwszString[11]) << 4
|
---|
461 | | (uint16_t)MY_TONUM(pwszString[12]);
|
---|
462 | pUuid->Gen.u16TimeHiAndVersion =
|
---|
463 | (uint16_t)MY_TONUM(pwszString[14]) << 12
|
---|
464 | | (uint16_t)MY_TONUM(pwszString[15]) << 8
|
---|
465 | | (uint16_t)MY_TONUM(pwszString[16]) << 4
|
---|
466 | | (uint16_t)MY_TONUM(pwszString[17]);
|
---|
467 | pUuid->Gen.u8ClockSeqHiAndReserved =
|
---|
468 | (uint16_t)MY_TONUM(pwszString[19]) << 4
|
---|
469 | | (uint16_t)MY_TONUM(pwszString[20]);
|
---|
470 | pUuid->Gen.u8ClockSeqLow =
|
---|
471 | (uint16_t)MY_TONUM(pwszString[21]) << 4
|
---|
472 | | (uint16_t)MY_TONUM(pwszString[22]);
|
---|
473 | pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pwszString[24]) << 4
|
---|
474 | | (uint8_t)MY_TONUM(pwszString[25]);
|
---|
475 | pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pwszString[26]) << 4
|
---|
476 | | (uint8_t)MY_TONUM(pwszString[27]);
|
---|
477 | pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pwszString[28]) << 4
|
---|
478 | | (uint8_t)MY_TONUM(pwszString[29]);
|
---|
479 | pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pwszString[30]) << 4
|
---|
480 | | (uint8_t)MY_TONUM(pwszString[31]);
|
---|
481 | pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pwszString[32]) << 4
|
---|
482 | | (uint8_t)MY_TONUM(pwszString[33]);
|
---|
483 | pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pwszString[34]) << 4
|
---|
484 | | (uint8_t)MY_TONUM(pwszString[35]);
|
---|
485 | #undef MY_TONUM
|
---|
486 | return VINF_SUCCESS;
|
---|
487 | }
|
---|
488 | RT_EXPORT_SYMBOL(RTUuidFromUtf16);
|
---|
489 |
|
---|