mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
Bug #772
- Implemented custom uuid function that doesn't suffer from the precision and cyclic lack of randomness of the previous implementation. Signed-off-by: Paul Beckingham <paul@beckingham.net>
This commit is contained in:
parent
9f2c5a6190
commit
3b2e93f1a0
2 changed files with 19 additions and 57 deletions
12
src/main.cpp
12
src/main.cpp
|
@ -42,23 +42,19 @@
|
||||||
|
|
||||||
Context context;
|
Context context;
|
||||||
|
|
||||||
|
#ifdef HAVE_SRANDOM
|
||||||
|
#define srand(x) srandom(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
int main (int argc, const char** argv)
|
int main (int argc, const char** argv)
|
||||||
{
|
{
|
||||||
// Set up randomness.
|
// Set up randomness.
|
||||||
#ifdef CYGWIN
|
#ifdef CYGWIN
|
||||||
#ifdef HAVE_SRANDOM
|
|
||||||
srandom (time (NULL));
|
|
||||||
#else
|
|
||||||
srand (time (NULL));
|
srand (time (NULL));
|
||||||
#endif
|
|
||||||
#else
|
#else
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
gettimeofday (&tv, NULL);
|
gettimeofday (&tv, NULL);
|
||||||
#ifdef HAVE_SRANDOM
|
|
||||||
srandom (tv.tv_usec);
|
|
||||||
#else
|
|
||||||
srand (tv.tv_usec);
|
srand (tv.tv_usec);
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int status = 0;
|
int status = 0;
|
||||||
|
|
64
src/util.cpp
64
src/util.cpp
|
@ -249,62 +249,28 @@ const std::string uuid ()
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
#else
|
#else
|
||||||
#include <stdlib.h>
|
|
||||||
static char randomHexDigit ()
|
|
||||||
{
|
|
||||||
static char digits[] = "0123456789abcdef"; // no l10n
|
|
||||||
#ifdef HAVE_RANDOM
|
#ifdef HAVE_RANDOM
|
||||||
// random is better than rand.
|
#define rand() random()
|
||||||
return digits[random () % 16];
|
|
||||||
#else
|
|
||||||
return digits[rand () % 16];
|
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
const std::string uuid ()
|
const std::string uuid ()
|
||||||
{
|
{
|
||||||
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
uint32_t time_low = ((rand () << 16) & 0xffff0000) | (rand () & 0xffff);
|
||||||
char id [48] = {0};
|
uint16_t time_mid = rand () & 0xffff;
|
||||||
id[0] = randomHexDigit ();
|
uint16_t time_high_and_version = (rand () & 0x0fff) | 0x4000;
|
||||||
id[1] = randomHexDigit ();
|
uint16_t clock_seq = (rand () & 0x3fff) | 0x8000;
|
||||||
id[2] = randomHexDigit ();
|
uint8_t node [6];
|
||||||
id[3] = randomHexDigit ();
|
for (size_t i = 0; i < 6; i++)
|
||||||
id[4] = randomHexDigit ();
|
node[i] = rand() & 0xff;
|
||||||
id[5] = randomHexDigit ();
|
|
||||||
id[6] = randomHexDigit ();
|
|
||||||
id[7] = randomHexDigit ();
|
|
||||||
id[8] = '-';
|
|
||||||
id[9] = randomHexDigit ();
|
|
||||||
id[10] = randomHexDigit ();
|
|
||||||
id[11] = randomHexDigit ();
|
|
||||||
id[12] = randomHexDigit ();
|
|
||||||
id[13] = '-';
|
|
||||||
id[14] = randomHexDigit ();
|
|
||||||
id[15] = randomHexDigit ();
|
|
||||||
id[16] = randomHexDigit ();
|
|
||||||
id[17] = randomHexDigit ();
|
|
||||||
id[18] = '-';
|
|
||||||
id[19] = randomHexDigit ();
|
|
||||||
id[20] = randomHexDigit ();
|
|
||||||
id[21] = randomHexDigit ();
|
|
||||||
id[22] = randomHexDigit ();
|
|
||||||
id[23] = '-';
|
|
||||||
id[24] = randomHexDigit ();
|
|
||||||
id[25] = randomHexDigit ();
|
|
||||||
id[26] = randomHexDigit ();
|
|
||||||
id[27] = randomHexDigit ();
|
|
||||||
id[28] = randomHexDigit ();
|
|
||||||
id[29] = randomHexDigit ();
|
|
||||||
id[30] = randomHexDigit ();
|
|
||||||
id[31] = randomHexDigit ();
|
|
||||||
id[32] = randomHexDigit ();
|
|
||||||
id[33] = randomHexDigit ();
|
|
||||||
id[34] = randomHexDigit ();
|
|
||||||
id[35] = randomHexDigit ();
|
|
||||||
id[36] = '\0';
|
|
||||||
|
|
||||||
return id;
|
char buffer[37];
|
||||||
|
sprintf(buffer, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
||||||
|
time_low, time_mid, time_high_and_version, clock_seq >> 8, clock_seq & 0xff,
|
||||||
|
node[0], node[1], node[2], node[3], node[4], node[5]);
|
||||||
|
|
||||||
|
return std::string (buffer);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue