📄 chxuuid.cpp
字号:
pCur += 4; ++count;
curVal = strtoul(pCur, &pEnd, 16);
if (pEnd == (pCur + 4) && *pEnd == '-')
{
clock_seq_hi_and_reserved = (curVal & 0xff00) >> 8;
clock_seq_low = curVal & 0xff;
}
else return count;
pCur += 4; count += 2;
curVal = strtoul(pCur, &pEnd, 16);
if (pEnd == (pCur + 6) && *(pEnd+strspn(pEnd, " \t")) == 0)
{
for (int i = 5; i >= 0; ++i, curVal = curVal >> 8)
{
node[i] = curVal & 0xff;
}
}
else return count;
pCur += 4; count += 6;
#else
count = sscanf(uuid_string, "%8lx-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x",
&time_low,
&time_mid,
&time_hi_and_version,
&clock_seq_hi_and_reserved,
&clock_seq_low,
&node[0], &node[1], &node[2], &node[3], &node[4], &node[5]);
#endif
return count;
}
HX_RESULT CHXuuid::HXUuidFromString(const char* uuid_string, uuid_tt* uuid)
{
HX_RESULT theErr = HXR_OK;
uuid_tt uuid_new; /* used for sscanf for new uuid's */
uuid_p_t uuid_ptr=0; /* pointer to correct uuid (old/new) */
int i;
/*
* If a NULL pointer or empty string, give the nil UUID.
*/
if (uuid_string == NULL || *uuid_string == '\0')
{
memcpy (uuid, &uuid_g_nil_uuid, sizeof *uuid); /* Flawfinder: ignore */
return theErr;
}
/*
* check to see that the string length is right at least
*/
if (strlen ((char *) uuid_string) != UUID_C_UUID_STRING_MAX - 1)
{
theErr = HXR_FAILED;
return theErr;
}
/*
* check for a new uuid
*/
if (uuid_string[8] == '-')
{
long time_low;
int time_mid;
int time_hi_and_version;
int clock_seq_hi_and_reserved;
int clock_seq_low;
int node[6];
// VC1.5's runtime library(lddcew.lib) doesn't have sscanf()!!
#if defined(_WIN16)
i = wsscanf((char *) uuid_string, "%8lx-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x",
(long FAR *)&time_low,
(int FAR *)&time_mid,
(int FAR *)&time_hi_and_version,
(int FAR *)&clock_seq_hi_and_reserved,
(int FAR *)&clock_seq_low,
(int FAR *)&node[0], (int FAR *)&node[1], (int FAR *)&node[2],
(int FAR *)&node[3], (int FAR *)&node[4], (int FAR *)&node[5]);
#else
i = ParseIIDString(
uuid_string,
time_low,
time_mid,
time_hi_and_version,
clock_seq_hi_and_reserved,
clock_seq_low,
node);
#ifdef Commented_out_code_20030421_114057
i = sscanf((char *) uuid_string, "%8lx-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x",
&time_low,
&time_mid,
&time_hi_and_version,
&clock_seq_hi_and_reserved,
&clock_seq_low,
&node[0], &node[1], &node[2], &node[3], &node[4], &node[5]);
#endif /* Commented_out_code_20030421_114057 */
#endif
/*
* check that sscanf worked
*/
if (i != UUID_ELEMENTS_NUM)
{
theErr = HXR_FAILED;
return theErr;
}
/*
* note that we're going through this agony because scanf is defined to
* know only to scan into "int"s or "long"s.
*/
uuid_new.time_low = time_low;
uuid_new.time_mid = time_mid;
uuid_new.time_hi_and_version = time_hi_and_version;
uuid_new.clock_seq_hi_and_reserved = clock_seq_hi_and_reserved;
uuid_new.clock_seq_low = clock_seq_low;
uuid_new.node[0] = node[0];
uuid_new.node[1] = node[1];
uuid_new.node[2] = node[2];
uuid_new.node[3] = node[3];
uuid_new.node[4] = node[4];
uuid_new.node[5] = node[5];
/*
* point to the correct uuid
*/
uuid_ptr = &uuid_new;
}
/*
* copy the uuid to user
*/
memcpy(uuid, uuid_ptr, sizeof (uuid_tt)); /* Flawfinder: ignore */
return theErr;
}
/*
**++
**
** ROUTINE NAME: uuid_from_string
**
**--
**/
HX_RESULT CHXuuid::HXPack(UINT8* pBuffer, uuid_p_t uuid)
{
uuid_p_t uuidPacket = (uuid_p_t) pBuffer;
*uuidPacket = *uuid;
uuidPacket->time_low = DwToNet(uuid->time_low);
uuidPacket->time_mid = WToNet(uuid->time_mid);
uuidPacket->time_hi_and_version = WToNet(uuid->time_hi_and_version);
return HXR_OK;
}
/*
**++
**
** ROUTINE NAME: uuid_from_string
**
**--
**/
HX_RESULT CHXuuid::HXUnpack(uuid_p_t uuid, UINT8* pBuffer)
{
uuid_p_t uuidPacket = (uuid_p_t) pBuffer;
*uuid = *uuidPacket;
uuid->time_low = DwToHost(uuidPacket->time_low);
uuid->time_mid = WToHost(uuidPacket->time_mid);
uuid->time_hi_and_version = WToHost(uuidPacket->time_hi_and_version);
return HXR_OK;
}
/*
**++
**
** ROUTINE NAME: uuid_equal
**
**--
**/
BOOL CHXuuid::HXIsEqual(uuid_p_t uuid1, uuid_p_t uuid2)
{
/*
* Note: This used to be a memcmp(), but changed to a field-by-field compare
* because of portability problems with alignment and garbage in a UUID.
*/
if ((uuid1->time_low == uuid2->time_low) &&
(uuid1->time_mid == uuid2->time_mid) &&
(uuid1->time_hi_and_version == uuid2->time_hi_and_version) &&
(uuid1->clock_seq_hi_and_reserved == uuid2->clock_seq_hi_and_reserved) &&
(uuid1->clock_seq_low == uuid2->clock_seq_low) &&
(memcmp(uuid1->node, uuid2->node, 6) == 0))
{
return ( TRUE );
}
else
{
return (FALSE);
}
}
/*****************************************************************************
*
* LOCAL MATH PROCEDURES - math procedures used internally by the UUID module
*
****************************************************************************/
/*
** T I M E _ C M P
**
** Compares two UUID times (64-bit UTC values)
**/
uuid_compval_t CHXuuid::TimeCmp(uuid_ttime_p_t time1, uuid_ttime_p_t time2)
{
/*
* first check the hi parts
*/
if (time1->hi < time2->hi) return (uuid_e_less_than);
if (time1->hi > time2->hi) return (uuid_e_greater_than);
/*
* hi parts are equal, check the lo parts
*/
if (time1->lo < time2->lo) return (uuid_e_less_than);
if (time1->lo > time2->lo) return (uuid_e_greater_than);
return (uuid_e_equal_to);
}
/*
** UnsignedExtendedMultiply
**
** Functional Description:
** 32-bit unsigned quantity * 32-bit unsigned quantity
** producing 64-bit unsigned result. This routine assumes
** long's contain at least 32 bits. It makes no assumptions
** about byte orderings.
**
** Inputs:
**
** u, v Are the numbers to be multiplied passed by value
**
** Outputs:
**
** prodPtr is a pointer to the 64-bit result
**
** Note:
** This algorithm is taken from: "The Art of Computer
** Programming", by Donald E. Knuth. Vol 2. Section 4.3.1
** Pages: 253-255.
**--
**/
void CHXuuid::UnsignedExtendedMultiply(ULONG32 u,ULONG32 v, unsigned64_t* prodPtr)
{
/*
* following the notation in Knuth, Vol. 2
*/
ULONG32 uuid1, uuid2, v1, v2, temp;
uuid1 = u >> 16;
uuid2 = u & 0xffff;
v1 = v >> 16;
v2 = v & 0xffff;
temp = uuid2 * v2;
prodPtr->lo = temp & 0xffff;
temp = uuid1 * v2 + (temp >> 16);
prodPtr->hi = temp >> 16;
temp = uuid2 * v1 + (temp & 0xffff);
prodPtr->lo += (temp & 0xffff) << 16;
prodPtr->hi += uuid1 * v1 + (temp >> 16);
}
UINT16 CHXuuid::TrueRandom()
{
ULONG32 nRand = m_pRand->GetRandomNumber();
return (UINT16)(HI_WORD(nRand) ^ (nRand & RAND_MASK));
}
/*****************************************************************************
*
* LOCAL PROCEDURES - procedures used staticly by the UUID module
*
****************************************************************************/
/*
** N E W _ C L O C K _ S E Q
**
** Ensure *clkseq is up-to-date
**
** Note: clock_seq is architected to be 14-bits (unsigned) but
** I've put it in here as 16-bits since there isn't a
** 14-bit unsigned integer type (yet)
**/
void CHXuuid::NewClockSeq(UINT16& clkseq)
{
/*
* A clkseq value of 0 indicates that it hasn't been initialized.
*/
if (clkseq == 0)
{
/*
* with a volatile clock, we always init to a random number
*/
clkseq = (UINT16) m_pRand->GetRandomNumber();
}
CLOCK_SEQ_BUMP (&clkseq);
if (clkseq == 0)
{
clkseq = clkseq + 1;
}
}
/*
* Define constant designation difference in Unix and DTSS base times:
* DTSS UTC base time is October 15, 1582.
* Unix base time is January 1, 1970.
*/
#define uuid_c_os_base_time_diff_lo 0x13814000
#define uuid_c_os_base_time_diff_hi 0x01B21DD2
/*
* U U I D _ _ G E T _ O S _ T I M E
*
* Get OS time - contains platform-specific code.
*/
void CHXuuid::GetOSTime(uuid_ttime_t * uuid_ttime)
{
HXTime tv;
unsigned64_t utc,
usecs,
os_basetime_diff;
/*
* Fill out the HXTime struct with the current time
*/
gettimeofday(&tv, NULL);
/*
* Multiply the number of seconds by the number clunks
*/
UnsignedExtendedMultiply ((long) tv.tv_sec, UUID_C_100NS_PER_SEC, &utc);
/*
* Multiply the number of milliseconds by the number clunks
* and add to the seconds
*/
UnsignedExtendedMultiply ((long) tv.tv_usec / 1000, UUID_C_100NS_PER_MSEC, &usecs);
UADD_UVLW_2_UVLW (&usecs, &utc, &utc);
/*
* Offset between DTSS formatted times and Unix formatted times.
*/
os_basetime_diff.lo = uuid_c_os_base_time_diff_lo;
os_basetime_diff.hi = uuid_c_os_base_time_diff_hi;
UADD_UVLW_2_UVLW (&utc, &os_basetime_diff, uuid_ttime);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -