1 | /* $Id: uuid-generic.cpp 28800 2010-04-27 08:22:32Z 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. */
|
---|
43 | static 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. */
|
---|
63 | static 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 |
|
---|
72 | RTDECL(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 | }
|
---|
79 | RT_EXPORT_SYMBOL(RTUuidClear);
|
---|
80 |
|
---|
81 |
|
---|
82 | RTDECL(bool) RTUuidIsNull(PCRTUUID pUuid)
|
---|
83 | {
|
---|
84 | AssertPtrReturn(pUuid, true);
|
---|
85 | return !pUuid->au64[0]
|
---|
86 | && !pUuid->au64[1];
|
---|
87 | }
|
---|
88 | RT_EXPORT_SYMBOL(RTUuidIsNull);
|
---|
89 |
|
---|
90 |
|
---|
91 | RTDECL(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 | }
|
---|
132 | RT_EXPORT_SYMBOL(RTUuidCompare);
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(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 | }
|
---|
152 | RT_EXPORT_SYMBOL(RTUuidCompareStr);
|
---|
153 |
|
---|
154 |
|
---|
155 | RTDECL(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 | }
|
---|
176 | RT_EXPORT_SYMBOL(RTUuidCompare2Strs);
|
---|
177 |
|
---|
178 |
|
---|
179 | RTDECL(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 | }
|
---|
246 | RT_EXPORT_SYMBOL(RTUuidToStr);
|
---|
247 |
|
---|
248 |
|
---|
249 | RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
|
---|
250 | {
|
---|
251 | /*
|
---|
252 | * Validate parameters.
|
---|
253 | */
|
---|
254 | AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
|
---|
255 | AssertPtrReturn(pszString, VERR_INVALID_PARAMETER);
|
---|
256 |
|
---|
257 | #define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
|
---|
258 | #define MY_ISXDIGIT(ch) (g_au8Digits[(ch) & 0xff] != 0xff)
|
---|
259 | MY_CHECK(MY_ISXDIGIT(pszString[ 0]));
|
---|
260 | MY_CHECK(MY_ISXDIGIT(pszString[ 1]));
|
---|
261 | MY_CHECK(MY_ISXDIGIT(pszString[ 2]));
|
---|
262 | MY_CHECK(MY_ISXDIGIT(pszString[ 3]));
|
---|
263 | MY_CHECK(MY_ISXDIGIT(pszString[ 4]));
|
---|
264 | MY_CHECK(MY_ISXDIGIT(pszString[ 5]));
|
---|
265 | MY_CHECK(MY_ISXDIGIT(pszString[ 6]));
|
---|
266 | MY_CHECK(MY_ISXDIGIT(pszString[ 7]));
|
---|
267 | MY_CHECK(pszString[ 8] == '-');
|
---|
268 | MY_CHECK(MY_ISXDIGIT(pszString[ 9]));
|
---|
269 | MY_CHECK(MY_ISXDIGIT(pszString[10]));
|
---|
270 | MY_CHECK(MY_ISXDIGIT(pszString[11]));
|
---|
271 | MY_CHECK(MY_ISXDIGIT(pszString[12]));
|
---|
272 | MY_CHECK(pszString[13] == '-');
|
---|
273 | MY_CHECK(MY_ISXDIGIT(pszString[14]));
|
---|
274 | MY_CHECK(MY_ISXDIGIT(pszString[15]));
|
---|
275 | MY_CHECK(MY_ISXDIGIT(pszString[16]));
|
---|
276 | MY_CHECK(MY_ISXDIGIT(pszString[17]));
|
---|
277 | MY_CHECK(pszString[18] == '-');
|
---|
278 | MY_CHECK(MY_ISXDIGIT(pszString[19]));
|
---|
279 | MY_CHECK(MY_ISXDIGIT(pszString[20]));
|
---|
280 | MY_CHECK(MY_ISXDIGIT(pszString[21]));
|
---|
281 | MY_CHECK(MY_ISXDIGIT(pszString[22]));
|
---|
282 | MY_CHECK(pszString[23] == '-');
|
---|
283 | MY_CHECK(MY_ISXDIGIT(pszString[24]));
|
---|
284 | MY_CHECK(MY_ISXDIGIT(pszString[25]));
|
---|
285 | MY_CHECK(MY_ISXDIGIT(pszString[26]));
|
---|
286 | MY_CHECK(MY_ISXDIGIT(pszString[27]));
|
---|
287 | MY_CHECK(MY_ISXDIGIT(pszString[28]));
|
---|
288 | MY_CHECK(MY_ISXDIGIT(pszString[29]));
|
---|
289 | MY_CHECK(MY_ISXDIGIT(pszString[30]));
|
---|
290 | MY_CHECK(MY_ISXDIGIT(pszString[31]));
|
---|
291 | MY_CHECK(MY_ISXDIGIT(pszString[32]));
|
---|
292 | MY_CHECK(MY_ISXDIGIT(pszString[33]));
|
---|
293 | MY_CHECK(MY_ISXDIGIT(pszString[34]));
|
---|
294 | MY_CHECK(MY_ISXDIGIT(pszString[35]));
|
---|
295 | MY_CHECK(!pszString[36]);
|
---|
296 | #undef MY_ISXDIGIT
|
---|
297 | #undef MY_CHECK
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * Inverse of RTUuidToStr (see above).
|
---|
301 | */
|
---|
302 | #define MY_TONUM(ch) (g_au8Digits[(ch) & 0xff])
|
---|
303 | pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pszString[ 0]) << 28
|
---|
304 | | (uint32_t)MY_TONUM(pszString[ 1]) << 24
|
---|
305 | | (uint32_t)MY_TONUM(pszString[ 2]) << 20
|
---|
306 | | (uint32_t)MY_TONUM(pszString[ 3]) << 16
|
---|
307 | | (uint32_t)MY_TONUM(pszString[ 4]) << 12
|
---|
308 | | (uint32_t)MY_TONUM(pszString[ 5]) << 8
|
---|
309 | | (uint32_t)MY_TONUM(pszString[ 6]) << 4
|
---|
310 | | (uint32_t)MY_TONUM(pszString[ 7]);
|
---|
311 | pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pszString[ 9]) << 12
|
---|
312 | | (uint16_t)MY_TONUM(pszString[10]) << 8
|
---|
313 | | (uint16_t)MY_TONUM(pszString[11]) << 4
|
---|
314 | | (uint16_t)MY_TONUM(pszString[12]);
|
---|
315 | pUuid->Gen.u16TimeHiAndVersion =
|
---|
316 | (uint16_t)MY_TONUM(pszString[14]) << 12
|
---|
317 | | (uint16_t)MY_TONUM(pszString[15]) << 8
|
---|
318 | | (uint16_t)MY_TONUM(pszString[16]) << 4
|
---|
319 | | (uint16_t)MY_TONUM(pszString[17]);
|
---|
320 | pUuid->Gen.u8ClockSeqHiAndReserved =
|
---|
321 | (uint16_t)MY_TONUM(pszString[19]) << 4
|
---|
322 | | (uint16_t)MY_TONUM(pszString[20]);
|
---|
323 | pUuid->Gen.u8ClockSeqLow =
|
---|
324 | (uint16_t)MY_TONUM(pszString[21]) << 4
|
---|
325 | | (uint16_t)MY_TONUM(pszString[22]);
|
---|
326 | pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pszString[24]) << 4
|
---|
327 | | (uint8_t)MY_TONUM(pszString[25]);
|
---|
328 | pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pszString[26]) << 4
|
---|
329 | | (uint8_t)MY_TONUM(pszString[27]);
|
---|
330 | pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pszString[28]) << 4
|
---|
331 | | (uint8_t)MY_TONUM(pszString[29]);
|
---|
332 | pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pszString[30]) << 4
|
---|
333 | | (uint8_t)MY_TONUM(pszString[31]);
|
---|
334 | pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pszString[32]) << 4
|
---|
335 | | (uint8_t)MY_TONUM(pszString[33]);
|
---|
336 | pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pszString[34]) << 4
|
---|
337 | | (uint8_t)MY_TONUM(pszString[35]);
|
---|
338 | #undef MY_TONUM
|
---|
339 | return VINF_SUCCESS;
|
---|
340 | }
|
---|
341 | RT_EXPORT_SYMBOL(RTUuidFromStr);
|
---|
342 |
|
---|
343 |
|
---|
344 | RTDECL(int) RTUuidToUtf16(PCRTUUID pUuid, PRTUTF16 pwszString, size_t cwcString)
|
---|
345 | {
|
---|
346 | uint32_t u32TimeLow;
|
---|
347 | unsigned u;
|
---|
348 |
|
---|
349 | /* validate parameters */
|
---|
350 | AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
|
---|
351 | AssertPtrReturn(pwszString, VERR_INVALID_PARAMETER);
|
---|
352 | AssertReturn(cwcString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
|
---|
353 |
|
---|
354 | /*
|
---|
355 | * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
---|
356 | * pUuid->Gen.u32TimeLow,
|
---|
357 | * pUuid->Gen.u16TimeMin,
|
---|
358 | * pUuid->Gen.u16TimeHiAndVersion,
|
---|
359 | * pUuid->Gen.u16ClockSeq & 0xff,
|
---|
360 | * pUuid->Gen.u16ClockSeq >> 8,
|
---|
361 | * pUuid->Gen.au8Node[0],
|
---|
362 | * pUuid->Gen.au8Node[1],
|
---|
363 | * pUuid->Gen.au8Node[2],
|
---|
364 | * pUuid->Gen.au8Node[3],
|
---|
365 | * pUuid->Gen.au8Node[4],
|
---|
366 | * pUuid->Gen.au8Node[5]);
|
---|
367 | */
|
---|
368 | u32TimeLow = pUuid->Gen.u32TimeLow;
|
---|
369 | pwszString[ 0] = g_achDigits[(u32TimeLow >> 28)/*& 0xf*/];
|
---|
370 | pwszString[ 1] = g_achDigits[(u32TimeLow >> 24) & 0xf];
|
---|
371 | pwszString[ 2] = g_achDigits[(u32TimeLow >> 20) & 0xf];
|
---|
372 | pwszString[ 3] = g_achDigits[(u32TimeLow >> 16) & 0xf];
|
---|
373 | pwszString[ 4] = g_achDigits[(u32TimeLow >> 12) & 0xf];
|
---|
374 | pwszString[ 5] = g_achDigits[(u32TimeLow >> 8) & 0xf];
|
---|
375 | pwszString[ 6] = g_achDigits[(u32TimeLow >> 4) & 0xf];
|
---|
376 | pwszString[ 7] = g_achDigits[(u32TimeLow/*>>0*/)& 0xf];
|
---|
377 | pwszString[ 8] = '-';
|
---|
378 | u = pUuid->Gen.u16TimeMid;
|
---|
379 | pwszString[ 9] = g_achDigits[(u >> 12)/*& 0xf*/];
|
---|
380 | pwszString[10] = g_achDigits[(u >> 8) & 0xf];
|
---|
381 | pwszString[11] = g_achDigits[(u >> 4) & 0xf];
|
---|
382 | pwszString[12] = g_achDigits[(u/*>>0*/)& 0xf];
|
---|
383 | pwszString[13] = '-';
|
---|
384 | u = pUuid->Gen.u16TimeHiAndVersion;
|
---|
385 | pwszString[14] = g_achDigits[(u >> 12)/*& 0xf*/];
|
---|
386 | pwszString[15] = g_achDigits[(u >> 8) & 0xf];
|
---|
387 | pwszString[16] = g_achDigits[(u >> 4) & 0xf];
|
---|
388 | pwszString[17] = g_achDigits[(u/*>>0*/)& 0xf];
|
---|
389 | pwszString[18] = '-';
|
---|
390 | pwszString[19] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved >> 4];
|
---|
391 | pwszString[20] = g_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved & 0xf];
|
---|
392 | pwszString[21] = g_achDigits[pUuid->Gen.u8ClockSeqLow >> 4];
|
---|
393 | pwszString[22] = g_achDigits[pUuid->Gen.u8ClockSeqLow & 0xf];
|
---|
394 | pwszString[23] = '-';
|
---|
395 | pwszString[24] = g_achDigits[pUuid->Gen.au8Node[0] >> 4];
|
---|
396 | pwszString[25] = g_achDigits[pUuid->Gen.au8Node[0] & 0xf];
|
---|
397 | pwszString[26] = g_achDigits[pUuid->Gen.au8Node[1] >> 4];
|
---|
398 | pwszString[27] = g_achDigits[pUuid->Gen.au8Node[1] & 0xf];
|
---|
399 | pwszString[28] = g_achDigits[pUuid->Gen.au8Node[2] >> 4];
|
---|
400 | pwszString[29] = g_achDigits[pUuid->Gen.au8Node[2] & 0xf];
|
---|
401 | pwszString[30] = g_achDigits[pUuid->Gen.au8Node[3] >> 4];
|
---|
402 | pwszString[31] = g_achDigits[pUuid->Gen.au8Node[3] & 0xf];
|
---|
403 | pwszString[32] = g_achDigits[pUuid->Gen.au8Node[4] >> 4];
|
---|
404 | pwszString[33] = g_achDigits[pUuid->Gen.au8Node[4] & 0xf];
|
---|
405 | pwszString[34] = g_achDigits[pUuid->Gen.au8Node[5] >> 4];
|
---|
406 | pwszString[35] = g_achDigits[pUuid->Gen.au8Node[5] & 0xf];
|
---|
407 | pwszString[36] = '\0';
|
---|
408 |
|
---|
409 | return VINF_SUCCESS;
|
---|
410 | }
|
---|
411 | RT_EXPORT_SYMBOL(RTUuidToUtf16);
|
---|
412 |
|
---|
413 |
|
---|
414 | RTDECL(int) RTUuidFromUtf16(PRTUUID pUuid, PCRTUTF16 pwszString)
|
---|
415 | {
|
---|
416 |
|
---|
417 | /*
|
---|
418 | * Validate parameters.
|
---|
419 | */
|
---|
420 | AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
|
---|
421 | AssertPtrReturn(pwszString, VERR_INVALID_PARAMETER);
|
---|
422 |
|
---|
423 | #define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
|
---|
424 | #define MY_ISXDIGIT(ch) (!((ch) & 0xff00) && g_au8Digits[(ch) & 0xff] != 0xff)
|
---|
425 | MY_CHECK(MY_ISXDIGIT(pwszString[ 0]));
|
---|
426 | MY_CHECK(MY_ISXDIGIT(pwszString[ 1]));
|
---|
427 | MY_CHECK(MY_ISXDIGIT(pwszString[ 2]));
|
---|
428 | MY_CHECK(MY_ISXDIGIT(pwszString[ 3]));
|
---|
429 | MY_CHECK(MY_ISXDIGIT(pwszString[ 4]));
|
---|
430 | MY_CHECK(MY_ISXDIGIT(pwszString[ 5]));
|
---|
431 | MY_CHECK(MY_ISXDIGIT(pwszString[ 6]));
|
---|
432 | MY_CHECK(MY_ISXDIGIT(pwszString[ 7]));
|
---|
433 | MY_CHECK(pwszString[ 8] == '-');
|
---|
434 | MY_CHECK(MY_ISXDIGIT(pwszString[ 9]));
|
---|
435 | MY_CHECK(MY_ISXDIGIT(pwszString[10]));
|
---|
436 | MY_CHECK(MY_ISXDIGIT(pwszString[11]));
|
---|
437 | MY_CHECK(MY_ISXDIGIT(pwszString[12]));
|
---|
438 | MY_CHECK(pwszString[13] == '-');
|
---|
439 | MY_CHECK(MY_ISXDIGIT(pwszString[14]));
|
---|
440 | MY_CHECK(MY_ISXDIGIT(pwszString[15]));
|
---|
441 | MY_CHECK(MY_ISXDIGIT(pwszString[16]));
|
---|
442 | MY_CHECK(MY_ISXDIGIT(pwszString[17]));
|
---|
443 | MY_CHECK(pwszString[18] == '-');
|
---|
444 | MY_CHECK(MY_ISXDIGIT(pwszString[19]));
|
---|
445 | MY_CHECK(MY_ISXDIGIT(pwszString[20]));
|
---|
446 | MY_CHECK(MY_ISXDIGIT(pwszString[21]));
|
---|
447 | MY_CHECK(MY_ISXDIGIT(pwszString[22]));
|
---|
448 | MY_CHECK(pwszString[23] == '-');
|
---|
449 | MY_CHECK(MY_ISXDIGIT(pwszString[24]));
|
---|
450 | MY_CHECK(MY_ISXDIGIT(pwszString[25]));
|
---|
451 | MY_CHECK(MY_ISXDIGIT(pwszString[26]));
|
---|
452 | MY_CHECK(MY_ISXDIGIT(pwszString[27]));
|
---|
453 | MY_CHECK(MY_ISXDIGIT(pwszString[28]));
|
---|
454 | MY_CHECK(MY_ISXDIGIT(pwszString[29]));
|
---|
455 | MY_CHECK(MY_ISXDIGIT(pwszString[30]));
|
---|
456 | MY_CHECK(MY_ISXDIGIT(pwszString[31]));
|
---|
457 | MY_CHECK(MY_ISXDIGIT(pwszString[32]));
|
---|
458 | MY_CHECK(MY_ISXDIGIT(pwszString[33]));
|
---|
459 | MY_CHECK(MY_ISXDIGIT(pwszString[34]));
|
---|
460 | MY_CHECK(MY_ISXDIGIT(pwszString[35]));
|
---|
461 | MY_CHECK(!pwszString[36]);
|
---|
462 | #undef MY_ISXDIGIT
|
---|
463 | #undef MY_CHECK
|
---|
464 |
|
---|
465 | /*
|
---|
466 | * Inverse of RTUuidToUtf8 (see above).
|
---|
467 | */
|
---|
468 | #define MY_TONUM(ch) (g_au8Digits[(ch) & 0xff])
|
---|
469 | pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pwszString[ 0]) << 28
|
---|
470 | | (uint32_t)MY_TONUM(pwszString[ 1]) << 24
|
---|
471 | | (uint32_t)MY_TONUM(pwszString[ 2]) << 20
|
---|
472 | | (uint32_t)MY_TONUM(pwszString[ 3]) << 16
|
---|
473 | | (uint32_t)MY_TONUM(pwszString[ 4]) << 12
|
---|
474 | | (uint32_t)MY_TONUM(pwszString[ 5]) << 8
|
---|
475 | | (uint32_t)MY_TONUM(pwszString[ 6]) << 4
|
---|
476 | | (uint32_t)MY_TONUM(pwszString[ 7]);
|
---|
477 | pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pwszString[ 9]) << 12
|
---|
478 | | (uint16_t)MY_TONUM(pwszString[10]) << 8
|
---|
479 | | (uint16_t)MY_TONUM(pwszString[11]) << 4
|
---|
480 | | (uint16_t)MY_TONUM(pwszString[12]);
|
---|
481 | pUuid->Gen.u16TimeHiAndVersion =
|
---|
482 | (uint16_t)MY_TONUM(pwszString[14]) << 12
|
---|
483 | | (uint16_t)MY_TONUM(pwszString[15]) << 8
|
---|
484 | | (uint16_t)MY_TONUM(pwszString[16]) << 4
|
---|
485 | | (uint16_t)MY_TONUM(pwszString[17]);
|
---|
486 | pUuid->Gen.u8ClockSeqHiAndReserved =
|
---|
487 | (uint16_t)MY_TONUM(pwszString[19]) << 4
|
---|
488 | | (uint16_t)MY_TONUM(pwszString[20]);
|
---|
489 | pUuid->Gen.u8ClockSeqLow =
|
---|
490 | (uint16_t)MY_TONUM(pwszString[21]) << 4
|
---|
491 | | (uint16_t)MY_TONUM(pwszString[22]);
|
---|
492 | pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pwszString[24]) << 4
|
---|
493 | | (uint8_t)MY_TONUM(pwszString[25]);
|
---|
494 | pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pwszString[26]) << 4
|
---|
495 | | (uint8_t)MY_TONUM(pwszString[27]);
|
---|
496 | pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pwszString[28]) << 4
|
---|
497 | | (uint8_t)MY_TONUM(pwszString[29]);
|
---|
498 | pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pwszString[30]) << 4
|
---|
499 | | (uint8_t)MY_TONUM(pwszString[31]);
|
---|
500 | pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pwszString[32]) << 4
|
---|
501 | | (uint8_t)MY_TONUM(pwszString[33]);
|
---|
502 | pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pwszString[34]) << 4
|
---|
503 | | (uint8_t)MY_TONUM(pwszString[35]);
|
---|
504 | #undef MY_TONUM
|
---|
505 | return VINF_SUCCESS;
|
---|
506 | }
|
---|
507 | RT_EXPORT_SYMBOL(RTUuidFromUtf16);
|
---|
508 |
|
---|