Changeset 9741 in vbox for trunk/src/VBox/Runtime/generic
- Timestamp:
- Jun 16, 2008 10:46:46 PM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 32097
- Location:
- trunk/src/VBox/Runtime/generic
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/generic/RTUuidCreate-generic.cpp
r9738 r9741 1 1 /* $Id$ */ 2 2 /** @file 3 * IPRT - UUID, Generic .3 * IPRT - UUID, Generic RTUuidCreate implementation. 4 4 */ 5 5 … … 36 36 #include <iprt/assert.h> 37 37 #include <iprt/err.h> 38 #include <iprt/time.h>39 #include <iprt/asm.h>40 38 #include <iprt/rand.h> 41 39 42 40 43 /* WARNING: This implementation ASSUMES little endian. */41 /* WARNING: This implementation ASSUMES little endian. Needs testing on big endian! */ 44 42 45 43 … … 57 55 } 58 56 59 60 RTDECL(int) RTUuidClear(PRTUUID pUuid)61 {62 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);63 pUuid->au64[0] = 0;64 pUuid->au64[1] = 0;65 return VINF_SUCCESS;66 }67 68 69 RTDECL(bool) RTUuidIsNull(PCRTUUID pUuid)70 {71 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);72 return !pUuid->au64[0]73 && !pUuid->au64[1];74 }75 76 77 RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)78 {79 /*80 * Special cases.81 */82 /** @todo This differs in the windows implementation... check out which behavior we really want. */83 if (pUuid1 == pUuid2)84 return 0;85 if (!pUuid1)86 return RTUuidIsNull(pUuid2) ? 0 : -1;87 if (!pUuid2)88 return RTUuidIsNull(pUuid1) ? 0 : 1;89 AssertPtr(pUuid1);90 AssertPtr(pUuid2);91 92 /*93 * Standard cases.94 */95 if (pUuid1->Gen.u32TimeLow != pUuid2->Gen.u32TimeLow)96 return pUuid1->Gen.u32TimeLow < pUuid2->Gen.u32TimeLow ? -1 : 1;97 if (pUuid1->Gen.u16TimeMid != pUuid2->Gen.u16TimeMid)98 return pUuid1->Gen.u16TimeMid < pUuid2->Gen.u16TimeMid ? -1 : 1;99 if (pUuid1->Gen.u16TimeHiAndVersion != pUuid2->Gen.u16TimeHiAndVersion)100 return pUuid1->Gen.u16TimeHiAndVersion < pUuid2->Gen.u16TimeHiAndVersion ? -1 : 1;101 if (pUuid1->Gen.u16ClockSeq != pUuid2->Gen.u16ClockSeq)102 return pUuid1->Gen.u16ClockSeq < pUuid2->Gen.u16ClockSeq ? -1 : 1;103 if (pUuid1->Gen.au8Node[0] != pUuid2->Gen.au8Node[0])104 return pUuid1->Gen.au8Node[0] < pUuid2->Gen.au8Node[0] ? -1 : 1;105 if (pUuid1->Gen.au8Node[1] != pUuid2->Gen.au8Node[1])106 return pUuid1->Gen.au8Node[1] < pUuid2->Gen.au8Node[1] ? -1 : 1;107 if (pUuid1->Gen.au8Node[2] != pUuid2->Gen.au8Node[2])108 return pUuid1->Gen.au8Node[2] < pUuid2->Gen.au8Node[2] ? -1 : 1;109 if (pUuid1->Gen.au8Node[3] != pUuid2->Gen.au8Node[3])110 return pUuid1->Gen.au8Node[3] < pUuid2->Gen.au8Node[3] ? -1 : 1;111 if (pUuid1->Gen.au8Node[4] != pUuid2->Gen.au8Node[4])112 return pUuid1->Gen.au8Node[4] < pUuid2->Gen.au8Node[4] ? -1 : 1;113 if (pUuid1->Gen.au8Node[5] != pUuid2->Gen.au8Node[5])114 return pUuid1->Gen.au8Node[5] < pUuid2->Gen.au8Node[5] ? -1 : 1;115 return 0;116 }117 118 119 RTDECL(int) RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString)120 {121 /* check params */122 AssertPtrReturn(pUuid1, -1);123 AssertPtrReturn(pszString, 1);124 125 /*126 * Try convert the string to a UUID and then compare the two.127 */128 RTUUID Uuid2;129 int rc = RTUuidFromStr(&Uuid2, pszString);130 AssertRCReturn(rc, 1);131 132 return RTUuidCompare(pUuid1, &Uuid2);133 }134 135 136 RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString)137 {138 /* validate parameters */139 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);140 AssertPtrReturn(pszString, VERR_INVALID_PARAMETER);141 AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);142 143 /*144 * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",145 * pUuid->Gen.u32TimeLow,146 * pUuid->Gen.u16TimeMin,147 * pUuid->Gen.u16TimeHiAndVersion,148 * pUuid->Gen.u16ClockSeq & 0xff,149 * pUuid->Gen.u16ClockSeq >> 8,150 * pUuid->Gen.au8Node[0],151 * pUuid->Gen.au8Node[1],152 * pUuid->Gen.au8Node[2],153 * pUuid->Gen.au8Node[3],154 * pUuid->Gen.au8Node[4],155 * pUuid->Gen.au8Node[5]);156 */157 static const char s_achDigits[17] = "0123456789abcdef";158 uint32_t u32TimeLow = pUuid->Gen.u32TimeLow;159 pszString[ 0] = s_achDigits[(u32TimeLow >> 28)/*& 0xf*/];160 pszString[ 1] = s_achDigits[(u32TimeLow >> 24) & 0xf];161 pszString[ 2] = s_achDigits[(u32TimeLow >> 20) & 0xf];162 pszString[ 3] = s_achDigits[(u32TimeLow >> 16) & 0xf];163 pszString[ 4] = s_achDigits[(u32TimeLow >> 12) & 0xf];164 pszString[ 5] = s_achDigits[(u32TimeLow >> 8) & 0xf];165 pszString[ 6] = s_achDigits[(u32TimeLow >> 4) & 0xf];166 pszString[ 7] = s_achDigits[(u32TimeLow/*>>0*/)& 0xf];167 pszString[ 8] = '-';168 unsigned u = pUuid->Gen.u16TimeMid;169 pszString[ 9] = s_achDigits[(u >> 12)/*& 0xf*/];170 pszString[10] = s_achDigits[(u >> 8) & 0xf];171 pszString[11] = s_achDigits[(u >> 4) & 0xf];172 pszString[12] = s_achDigits[(u/*>>0*/)& 0xf];173 pszString[13] = '-';174 u = pUuid->Gen.u16TimeHiAndVersion;175 pszString[14] = s_achDigits[(u >> 12)/*& 0xf*/];176 pszString[15] = s_achDigits[(u >> 8) & 0xf];177 pszString[16] = s_achDigits[(u >> 4) & 0xf];178 pszString[17] = s_achDigits[(u/*>>0*/)& 0xf];179 pszString[18] = '-';180 u = pUuid->Gen.u16ClockSeq;181 pszString[19] = s_achDigits[(u >> 4) & 0xf];182 pszString[20] = s_achDigits[(u/*>>0*/)& 0xf];183 pszString[21] = s_achDigits[(u >> 12)/*& 0xf*/];184 pszString[22] = s_achDigits[(u >> 8) & 0xf];185 pszString[23] = '-';186 pszString[24] = s_achDigits[pUuid->Gen.au8Node[0] >> 4];187 pszString[25] = s_achDigits[pUuid->Gen.au8Node[0] & 0xf];188 pszString[26] = s_achDigits[pUuid->Gen.au8Node[1] >> 4];189 pszString[27] = s_achDigits[pUuid->Gen.au8Node[1] & 0xf];190 pszString[28] = s_achDigits[pUuid->Gen.au8Node[2] >> 4];191 pszString[29] = s_achDigits[pUuid->Gen.au8Node[2] & 0xf];192 pszString[30] = s_achDigits[pUuid->Gen.au8Node[3] >> 4];193 pszString[31] = s_achDigits[pUuid->Gen.au8Node[3] & 0xf];194 pszString[32] = s_achDigits[pUuid->Gen.au8Node[4] >> 4];195 pszString[33] = s_achDigits[pUuid->Gen.au8Node[4] & 0xf];196 pszString[34] = s_achDigits[pUuid->Gen.au8Node[5] >> 4];197 pszString[35] = s_achDigits[pUuid->Gen.au8Node[5] & 0xf];198 pszString[36] = '\0';199 200 return VINF_SUCCESS;201 }202 203 204 RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)205 {206 /* 0xff if not a hex number, otherwise the value. (Assumes UTF-8 encoded strings.) */207 static const uint8_t s_aDigits[256] =208 {209 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 0..0f */210 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 10..1f */211 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 20..2f */212 0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0xff,0xff, 0xff,0xff,0xff,0xff, /* 30..3f */213 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 40..4f */214 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 50..5f */215 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 60..6f */216 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 70..7f */217 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 80..8f */218 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 90..9f */219 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* a0..af */220 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* b0..bf */221 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* c0..cf */222 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* d0..df */223 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* e0..ef */224 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* f0..ff */225 };226 227 /*228 * Validate parameters.229 */230 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);231 AssertPtrReturn(pszString, VERR_INVALID_PARAMETER);232 233 #define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)234 #define MY_ISXDIGIT(ch) (s_aDigits[(ch) & 0xff] != 0xff)235 MY_CHECK(MY_ISXDIGIT(pszString[ 0]));236 MY_CHECK(MY_ISXDIGIT(pszString[ 1]));237 MY_CHECK(MY_ISXDIGIT(pszString[ 2]));238 MY_CHECK(MY_ISXDIGIT(pszString[ 3]));239 MY_CHECK(MY_ISXDIGIT(pszString[ 4]));240 MY_CHECK(MY_ISXDIGIT(pszString[ 5]));241 MY_CHECK(MY_ISXDIGIT(pszString[ 6]));242 MY_CHECK(MY_ISXDIGIT(pszString[ 7]));243 MY_CHECK(pszString[ 8] == '-');244 MY_CHECK(MY_ISXDIGIT(pszString[ 9]));245 MY_CHECK(MY_ISXDIGIT(pszString[10]));246 MY_CHECK(MY_ISXDIGIT(pszString[11]));247 MY_CHECK(MY_ISXDIGIT(pszString[12]));248 MY_CHECK(pszString[13] == '-');249 MY_CHECK(MY_ISXDIGIT(pszString[14]));250 MY_CHECK(MY_ISXDIGIT(pszString[15]));251 MY_CHECK(MY_ISXDIGIT(pszString[16]));252 MY_CHECK(MY_ISXDIGIT(pszString[17]));253 MY_CHECK(pszString[18] == '-');254 MY_CHECK(MY_ISXDIGIT(pszString[19]));255 MY_CHECK(MY_ISXDIGIT(pszString[20]));256 MY_CHECK(MY_ISXDIGIT(pszString[21]));257 MY_CHECK(MY_ISXDIGIT(pszString[22]));258 MY_CHECK(pszString[23] == '-');259 MY_CHECK(MY_ISXDIGIT(pszString[24]));260 MY_CHECK(MY_ISXDIGIT(pszString[25]));261 MY_CHECK(MY_ISXDIGIT(pszString[26]));262 MY_CHECK(MY_ISXDIGIT(pszString[27]));263 MY_CHECK(MY_ISXDIGIT(pszString[28]));264 MY_CHECK(MY_ISXDIGIT(pszString[29]));265 MY_CHECK(MY_ISXDIGIT(pszString[30]));266 MY_CHECK(MY_ISXDIGIT(pszString[31]));267 MY_CHECK(MY_ISXDIGIT(pszString[32]));268 MY_CHECK(MY_ISXDIGIT(pszString[33]));269 MY_CHECK(MY_ISXDIGIT(pszString[34]));270 MY_CHECK(MY_ISXDIGIT(pszString[35]));271 MY_CHECK(!pszString[36]);272 #undef MY_ISXDIGIT273 #undef MY_CHECK274 275 /*276 * Inverse of RTUuidToStr (see above).277 */278 #define MY_TONUM(ch) (s_aDigits[(ch) & 0xff])279 pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pszString[ 0]) << 28280 | (uint32_t)MY_TONUM(pszString[ 1]) << 24281 | (uint32_t)MY_TONUM(pszString[ 2]) << 20282 | (uint32_t)MY_TONUM(pszString[ 3]) << 16283 | (uint32_t)MY_TONUM(pszString[ 4]) << 12284 | (uint32_t)MY_TONUM(pszString[ 5]) << 8285 | (uint32_t)MY_TONUM(pszString[ 6]) << 4286 | (uint32_t)MY_TONUM(pszString[ 7]);287 pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pszString[ 9]) << 12288 | (uint16_t)MY_TONUM(pszString[10]) << 8289 | (uint16_t)MY_TONUM(pszString[11]) << 4290 | (uint16_t)MY_TONUM(pszString[12]);291 pUuid->Gen.u16TimeHiAndVersion =292 (uint16_t)MY_TONUM(pszString[14]) << 12293 | (uint16_t)MY_TONUM(pszString[15]) << 8294 | (uint16_t)MY_TONUM(pszString[16]) << 4295 | (uint16_t)MY_TONUM(pszString[17]);296 pUuid->Gen.u16ClockSeq =(uint16_t)MY_TONUM(pszString[19]) << 4297 | (uint16_t)MY_TONUM(pszString[20])298 | (uint16_t)MY_TONUM(pszString[21]) << 12299 | (uint16_t)MY_TONUM(pszString[22]) << 8;300 pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pszString[24]) << 4301 | (uint8_t)MY_TONUM(pszString[25]);302 pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pszString[26]) << 4303 | (uint8_t)MY_TONUM(pszString[27]);304 pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pszString[28]) << 4305 | (uint8_t)MY_TONUM(pszString[29]);306 pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pszString[30]) << 4307 | (uint8_t)MY_TONUM(pszString[31]);308 pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pszString[32]) << 4309 | (uint8_t)MY_TONUM(pszString[33]);310 pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pszString[34]) << 4311 | (uint8_t)MY_TONUM(pszString[35]);312 #undef MY_TONUM313 return VINF_SUCCESS;314 }315 -
trunk/src/VBox/Runtime/generic/uuid-generic.cpp
r9738 r9741 36 36 #include <iprt/assert.h> 37 37 #include <iprt/err.h> 38 #include <iprt/time.h> 39 #include <iprt/asm.h> 40 #include <iprt/rand.h> 41 42 43 /* WARNING: This implementation ASSUMES little endian. */ 44 45 46 /** @todo move to a different file. */ 47 RTDECL(int) RTUuidCreate(PRTUUID pUuid) 48 { 49 /* validate input. */ 50 AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER); 51 52 RTRandBytes(pUuid, sizeof(*pUuid)); 53 pUuid->Gen.u16ClockSeq = (pUuid->Gen.u16ClockSeq & 0x3fff) | 0x8000; 54 pUuid->Gen.u16TimeHiAndVersion = (pUuid->Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000; 55 56 return VINF_SUCCESS; 57 } 38 39 40 /* WARNING: This implementation ASSUMES little endian. Needs testing on big endian! */ 58 41 59 42
Note:
See TracChangeset
for help on using the changeset viewer.