uuid-generic.cpp revision cacc4e75dbbff469c10a505168208f064c6c385c
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/* $Id$ */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/** @file
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * innotek Portable Runtime - UUID, Generic.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/*
c98fb3e16fcd571a790eab772c0c66173d225205vboxsync * Copyright (C) 2006-2007 innotek GmbH
c98fb3e16fcd571a790eab772c0c66173d225205vboxsync *
c98fb3e16fcd571a790eab772c0c66173d225205vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c98fb3e16fcd571a790eab772c0c66173d225205vboxsync * available from http://www.virtualbox.org. This file is free software;
c98fb3e16fcd571a790eab772c0c66173d225205vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c98fb3e16fcd571a790eab772c0c66173d225205vboxsync * General Public License as published by the Free Software Foundation,
c98fb3e16fcd571a790eab772c0c66173d225205vboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
c98fb3e16fcd571a790eab772c0c66173d225205vboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
c98fb3e16fcd571a790eab772c0c66173d225205vboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/*******************************************************************************
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync* Header Files *
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync*******************************************************************************/
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#include <iprt/uuid.h>
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#include <iprt/assert.h>
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#include <iprt/err.h>
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#include <iprt/time.h>
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#include <iprt/asm.h>
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#include <iprt/rand.h>
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/* WARNING: This implementation ASSUMES little endian. */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/**
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Generates a new UUID value.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync *
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @returns iprt status code.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param pUuid Where to store generated uuid.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsyncRTDECL(int) RTUuidCreate(PRTUUID pUuid)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync{
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync /* validate input. */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync AssertReturn(pUuid, VERR_INVALID_PARAMETER);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync RTRandBytes(pUuid, sizeof(*pUuid));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.u16ClockSeq = (pUuid->Gen.u16ClockSeq & 0x3fff) | 0x8000;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.u16TimeHiAndVersion = (pUuid->Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return VINF_SUCCESS;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync}
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/**
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Makes a null UUID value.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync *
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @returns iprt status code.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param pUuid Where to store generated null uuid.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsyncRTDECL(int) RTUuidClear(PRTUUID pUuid)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync{
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync AssertReturn(pUuid, VERR_INVALID_PARAMETER);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->au64[0] = 0;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->au64[1] = 0;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return VINF_SUCCESS;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync}
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/**
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Checks if UUID is null.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync *
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @returns true if UUID is null.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param pUuid uuid to check.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsyncRTDECL(int) RTUuidIsNull(PCRTUUID pUuid)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync{
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync AssertReturn(pUuid, VERR_INVALID_PARAMETER);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return !pUuid->au64[0]
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync && !pUuid->au64[1];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync}
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/**
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Compares two UUID values.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync *
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @returns 0 if eq, < 0 or > 0.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param pUuid1 First value to compare.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param pUuid2 Second value to compare.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsyncRTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync{
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync /*
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Special cases.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1 == pUuid2)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return 0;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (!pUuid1)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return RTUuidIsNull(pUuid2) ? 0 : -1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (!pUuid2)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return RTUuidIsNull(pUuid1) ? 0 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync /*
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Standard cases.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.u32TimeLow != pUuid2->Gen.u32TimeLow)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.u32TimeLow < pUuid2->Gen.u32TimeLow ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.u16TimeMid != pUuid2->Gen.u16TimeMid)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.u16TimeMid < pUuid2->Gen.u16TimeMid ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.u16TimeHiAndVersion != pUuid2->Gen.u16TimeHiAndVersion)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.u16TimeHiAndVersion < pUuid2->Gen.u16TimeHiAndVersion ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.u16ClockSeq != pUuid2->Gen.u16ClockSeq)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.u16ClockSeq < pUuid2->Gen.u16ClockSeq ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.au8Node[0] != pUuid2->Gen.au8Node[0])
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.au8Node[0] < pUuid2->Gen.au8Node[0] ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.au8Node[1] != pUuid2->Gen.au8Node[1])
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.au8Node[1] < pUuid2->Gen.au8Node[1] ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.au8Node[2] != pUuid2->Gen.au8Node[2])
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.au8Node[2] < pUuid2->Gen.au8Node[2] ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.au8Node[3] != pUuid2->Gen.au8Node[3])
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.au8Node[3] < pUuid2->Gen.au8Node[3] ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.au8Node[4] != pUuid2->Gen.au8Node[4])
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.au8Node[4] < pUuid2->Gen.au8Node[4] ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync if (pUuid1->Gen.au8Node[5] != pUuid2->Gen.au8Node[5])
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return pUuid1->Gen.au8Node[5] < pUuid2->Gen.au8Node[5] ? -1 : 1;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return 0;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync}
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/**
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Converts binary UUID to its string representation.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync *
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @returns iprt status code.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param pUuid Uuid to convert.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param pszString Where to store result string.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param cchString pszString buffer length, must be >= RTUUID_STR_LENGTH.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsyncRTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync{
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync /* validate parameters */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync AssertReturn(pUuid, VERR_INVALID_PARAMETER);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync AssertReturn(pszString, VERR_INVALID_PARAMETER);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync /*
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * RTStrPrintf(,,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.u32TimeLow,
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.u16TimeMin,
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.u16TimeHiAndVersion,
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.u16ClockSeq & 0xff,
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.u16ClockSeq >> 8,
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.au8Node[0],
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.au8Node[1],
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.au8Node[2],
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.au8Node[3],
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.au8Node[4],
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * pUuid->Gen.au8Node[5]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync static const char s_achDigits[17] = "0123456789abcdef";
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync uint32_t u32TimeLow = pUuid->Gen.u32TimeLow;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 0] = s_achDigits[(u32TimeLow >> 28)/*& 0xf*/];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 1] = s_achDigits[(u32TimeLow >> 24) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 2] = s_achDigits[(u32TimeLow >> 20) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 3] = s_achDigits[(u32TimeLow >> 16) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 4] = s_achDigits[(u32TimeLow >> 12) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 5] = s_achDigits[(u32TimeLow >> 8) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 6] = s_achDigits[(u32TimeLow >> 4) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 7] = s_achDigits[(u32TimeLow/*>>0*/)& 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 8] = '-';
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync unsigned u = pUuid->Gen.u16TimeMid;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[ 9] = s_achDigits[(u >> 12)/*& 0xf*/];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[10] = s_achDigits[(u >> 8) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[11] = s_achDigits[(u >> 4) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[12] = s_achDigits[(u/*>>0*/)& 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[13] = '-';
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync u = pUuid->Gen.u16TimeHiAndVersion;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[14] = s_achDigits[(u >> 12)/*& 0xf*/];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[15] = s_achDigits[(u >> 8) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[16] = s_achDigits[(u >> 4) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[17] = s_achDigits[(u/*>>0*/)& 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[18] = '-';
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync u = pUuid->Gen.u16ClockSeq;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[19] = s_achDigits[(u >> 4) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[20] = s_achDigits[(u/*>>0*/)& 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[21] = s_achDigits[(u >> 12)/*& 0xf*/];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[22] = s_achDigits[(u >> 8) & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[23] = '-';
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[24] = s_achDigits[pUuid->Gen.au8Node[0] >> 4];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[25] = s_achDigits[pUuid->Gen.au8Node[0] & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[26] = s_achDigits[pUuid->Gen.au8Node[1] >> 4];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[27] = s_achDigits[pUuid->Gen.au8Node[1] & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[28] = s_achDigits[pUuid->Gen.au8Node[2] >> 4];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[29] = s_achDigits[pUuid->Gen.au8Node[2] & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[30] = s_achDigits[pUuid->Gen.au8Node[3] >> 4];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[31] = s_achDigits[pUuid->Gen.au8Node[3] & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[32] = s_achDigits[pUuid->Gen.au8Node[4] >> 4];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[33] = s_achDigits[pUuid->Gen.au8Node[4] & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[34] = s_achDigits[pUuid->Gen.au8Node[5] >> 4];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[35] = s_achDigits[pUuid->Gen.au8Node[5] & 0xf];
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pszString[36] = '\0';
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return VINF_SUCCESS;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync}
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync/**
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Converts UUID from its string representation to binary format.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync *
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @returns iprt status code.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param pUuid Where to store result Uuid.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * @param pszString String with UUID text data.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsyncRTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync{
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync /* 0xff if not a hex number, otherwise the value. (Assumes UTF-8 encoded strings.) */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync static const uint8_t s_aDigits[256] =
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync {
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 0..0f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 10..1f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 20..2f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0xff,0xff, 0xff,0xff,0xff,0xff, /* 30..3f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 40..4f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 50..5f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0x0a,0x0b,0x0c, 0x0d,0x0e,0x0f,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 60..6f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 70..7f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 80..8f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* 90..9f */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* a0..af */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* b0..bf */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* c0..cf */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* d0..df */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* e0..ef */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, /* f0..ff */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync };
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync /*
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Validate parameters.
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync AssertReturn(pUuid, VERR_INVALID_PARAMETER);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync AssertReturn(pszString, VERR_INVALID_PARAMETER);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#define MY_ISXDIGIT(ch) (s_aDigits[(ch) & 0xff] != 0xff)
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[ 0]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[ 1]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[ 2]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[ 3]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[ 4]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[ 5]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[ 6]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[ 7]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(pszString[ 8] == '-');
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[ 9]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[10]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[11]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[12]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(pszString[13] == '-');
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[14]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[15]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[16]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[17]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(pszString[18] == '-');
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[19]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[20]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[21]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[22]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(pszString[23] == '-');
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[24]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[25]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[26]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[27]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[28]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[29]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[30]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[31]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[32]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[33]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[34]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(MY_ISXDIGIT(pszString[35]));
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync MY_CHECK(!pszString[36]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#undef MY_ISXDIGIT
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#undef MY_CHECK
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync /*
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync * Inverse of RTUuidToStr (see above).
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync */
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#define MY_TONUM(ch) (s_aDigits[(ch) & 0xff])
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.u32TimeLow = (uint32_t)MY_TONUM(pszString[ 0]) << 28
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint32_t)MY_TONUM(pszString[ 1]) << 24
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint32_t)MY_TONUM(pszString[ 2]) << 20
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint32_t)MY_TONUM(pszString[ 3]) << 16
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint32_t)MY_TONUM(pszString[ 4]) << 12
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint32_t)MY_TONUM(pszString[ 5]) << 8
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint32_t)MY_TONUM(pszString[ 6]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint32_t)MY_TONUM(pszString[ 7]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.u16TimeMid = (uint16_t)MY_TONUM(pszString[ 9]) << 12
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint16_t)MY_TONUM(pszString[10]) << 8
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint16_t)MY_TONUM(pszString[11]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint16_t)MY_TONUM(pszString[12]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.u16TimeHiAndVersion =
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync (uint16_t)MY_TONUM(pszString[14]) << 12
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint16_t)MY_TONUM(pszString[15]) << 8
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint16_t)MY_TONUM(pszString[16]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint16_t)MY_TONUM(pszString[17]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.u16ClockSeq =(uint16_t)MY_TONUM(pszString[19]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint16_t)MY_TONUM(pszString[20])
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint16_t)MY_TONUM(pszString[21]) << 12
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint16_t)MY_TONUM(pszString[22]) << 8;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.au8Node[0] = (uint8_t)MY_TONUM(pszString[24]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint8_t)MY_TONUM(pszString[25]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.au8Node[1] = (uint8_t)MY_TONUM(pszString[26]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint8_t)MY_TONUM(pszString[27]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.au8Node[2] = (uint8_t)MY_TONUM(pszString[28]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint8_t)MY_TONUM(pszString[29]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.au8Node[3] = (uint8_t)MY_TONUM(pszString[30]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint8_t)MY_TONUM(pszString[31]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.au8Node[4] = (uint8_t)MY_TONUM(pszString[32]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint8_t)MY_TONUM(pszString[33]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync pUuid->Gen.au8Node[5] = (uint8_t)MY_TONUM(pszString[34]) << 4
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync | (uint8_t)MY_TONUM(pszString[35]);
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync#undef MY_TONUM
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync return VINF_SUCCESS;
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync}
cacc4e75dbbff469c10a505168208f064c6c385cvboxsync