Get rid of useless integer typedefs.
Remove unsigned32, unsigned16 and unsigned8 references in the code.
This commit is contained in:
@@ -31,6 +31,8 @@
|
|||||||
#include "global.h"
|
#include "global.h"
|
||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
|
|
||||||
|
#include "UpnpStdInt.h"
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
@@ -49,21 +51,7 @@ extern ithread_mutex_t gUUIDMutex;
|
|||||||
#define UUIDLock() ithread_mutex_lock(&gUUIDMutex)
|
#define UUIDLock() ithread_mutex_lock(&gUUIDMutex)
|
||||||
#define UUIDUnlock() ithread_mutex_unlock(&gUUIDMutex)
|
#define UUIDUnlock() ithread_mutex_unlock(&gUUIDMutex)
|
||||||
|
|
||||||
typedef unsigned long unsigned32;
|
typedef uint64_t uuid_time_t;
|
||||||
typedef unsigned short unsigned16;
|
|
||||||
typedef unsigned char unsigned8;
|
|
||||||
typedef unsigned char byte;
|
|
||||||
|
|
||||||
/*! Set this to what your compiler uses for 64 bit data type */
|
|
||||||
#ifdef WIN32
|
|
||||||
#define unsigned64_t __int64
|
|
||||||
#else
|
|
||||||
#define unsigned64_t unsigned long long
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define I64(C) C##LL
|
|
||||||
|
|
||||||
typedef unsigned64_t uuid_time_t;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char nodeID[6];
|
char nodeID[6];
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
#ifndef UUID_H
|
||||||
|
#define UUID_H
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
|
* Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
|
||||||
* Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
|
* Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
|
||||||
@@ -17,27 +20,24 @@
|
|||||||
* this software for any purpose.
|
* this software for any purpose.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "sysdep.h"
|
#include "sysdep.h"
|
||||||
|
|
||||||
|
|
||||||
/*! . */
|
/*! . */
|
||||||
typedef struct _uuid_upnp {
|
typedef struct _uuid_upnp {
|
||||||
/*! . */
|
/*! . */
|
||||||
unsigned32 time_low;
|
uint32_t time_low;
|
||||||
/*! . */
|
/*! . */
|
||||||
unsigned16 time_mid;
|
uint16_t time_mid;
|
||||||
/*! . */
|
/*! . */
|
||||||
unsigned16 time_hi_and_version;
|
uint16_t time_hi_and_version;
|
||||||
/*! . */
|
/*! . */
|
||||||
unsigned8 clock_seq_hi_and_reserved;
|
uint8_t clock_seq_hi_and_reserved;
|
||||||
/*! . */
|
/*! . */
|
||||||
unsigned8 clock_seq_low;
|
uint8_t clock_seq_low;
|
||||||
/*! . */
|
/*! . */
|
||||||
byte node[6];
|
uint8_t node[6];
|
||||||
} uuid_upnp;
|
} uuid_upnp;
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Generate a UUID.
|
* \brief Generate a UUID.
|
||||||
*/
|
*/
|
||||||
@@ -45,32 +45,28 @@ int uuid_create(
|
|||||||
/*! . */
|
/*! . */
|
||||||
uuid_upnp * id);
|
uuid_upnp * id);
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Out will be xxxx-xx-xx-xx-xxxxxx format.
|
* \brief Out will be xxxx-xx-xx-xx-xxxxxx format.
|
||||||
*/
|
*/
|
||||||
void uuid_unpack(
|
void uuid_unpack(
|
||||||
/*! . */
|
/*! . */
|
||||||
uuid_upnp *u,
|
uuid_upnp * u,
|
||||||
/*! . */
|
/*! . */
|
||||||
char *out);
|
char *out);
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Create a UUID using a "name" from a "name space"
|
* \brief Create a UUID using a "name" from a "name space"
|
||||||
*/
|
*/
|
||||||
void uuid_create_from_name(
|
void uuid_create_from_name(
|
||||||
/*! Resulting UUID. */
|
/*! Resulting UUID. */
|
||||||
uuid_upnp *uid,
|
uuid_upnp * uid,
|
||||||
/*! UUID to serve as context, so identical names from different name
|
/*! UUID to serve as context, so identical names from different name
|
||||||
* spaces generate different UUIDs. */
|
* spaces generate different UUIDs. */
|
||||||
uuid_upnp nsid,
|
uuid_upnp nsid,
|
||||||
/*! The name from which to generate a UUID. */
|
/*! The name from which to generate a UUID. */
|
||||||
void *name,
|
void *name,
|
||||||
/*! The length of the name. */
|
/*! The length of the name. */
|
||||||
int namelen
|
int namelen);
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Compare two UUID's "lexically".
|
* \brief Compare two UUID's "lexically".
|
||||||
@@ -84,7 +80,7 @@ void uuid_create_from_name(
|
|||||||
*/
|
*/
|
||||||
int uuid_compare(
|
int uuid_compare(
|
||||||
/*! . */
|
/*! . */
|
||||||
uuid_upnp *u1,
|
uuid_upnp * u1,
|
||||||
/*! . */
|
/*! . */
|
||||||
uuid_upnp *u2);
|
uuid_upnp * u2);
|
||||||
|
#endif /* UUID_H */
|
||||||
|
|||||||
@@ -17,6 +17,10 @@
|
|||||||
* this software for any purpose.
|
* this software for any purpose.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \file
|
||||||
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include "sysdep.h"
|
#include "sysdep.h"
|
||||||
@@ -26,10 +30,10 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*!
|
||||||
/*
|
* \brief System dependent call to get IEEE node ID.
|
||||||
system dependent call to get IEEE node ID.
|
*
|
||||||
This sample implementation generates a random node ID
|
* This sample implementation generates a random node ID.
|
||||||
*/
|
*/
|
||||||
void get_ieee_node_identifier(uuid_node_t *node)
|
void get_ieee_node_identifier(uuid_node_t *node)
|
||||||
{
|
{
|
||||||
@@ -46,11 +50,11 @@ void get_ieee_node_identifier(uuid_node_t *node)
|
|||||||
*node = saved_node;
|
*node = saved_node;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*!
|
||||||
/*
|
* \brief System dependent call to get the current system time.
|
||||||
system dependent call to get the current system time.
|
*
|
||||||
Returned as 100ns ticks since Oct 15, 1582, but resolution may be
|
* Returned as 100ns ticks since Oct 15, 1582, but resolution may be less
|
||||||
less than 100ns.
|
* than 100ns.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
@@ -72,7 +76,6 @@ void get_system_time(uuid_time_t *uuid_time)
|
|||||||
*uuid_time = time.QuadPart;
|
*uuid_time = time.QuadPart;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
|
||||||
void get_random_info(char seed[16])
|
void get_random_info(char seed[16])
|
||||||
{
|
{
|
||||||
MD5_CTX c;
|
MD5_CTX c;
|
||||||
@@ -89,7 +92,6 @@ void get_random_info(char seed[16])
|
|||||||
|
|
||||||
/* Initialize memory area so that valgrind does not complain */
|
/* Initialize memory area so that valgrind does not complain */
|
||||||
memset(&r, 0, sizeof r);
|
memset(&r, 0, sizeof r);
|
||||||
|
|
||||||
/* memory usage stats */
|
/* memory usage stats */
|
||||||
GlobalMemoryStatus( &r.m );
|
GlobalMemoryStatus( &r.m );
|
||||||
/* random system stats */
|
/* random system stats */
|
||||||
@@ -101,9 +103,7 @@ void get_random_info(char seed[16])
|
|||||||
/* milliseconds since last boot */
|
/* milliseconds since last boot */
|
||||||
r.tc = GetTickCount();
|
r.tc = GetTickCount();
|
||||||
r.l = MAX_COMPUTERNAME_LENGTH + 1;
|
r.l = MAX_COMPUTERNAME_LENGTH + 1;
|
||||||
|
|
||||||
GetComputerName( r.hostname, &r.l );
|
GetComputerName( r.hostname, &r.l );
|
||||||
|
|
||||||
/* MD5 it */
|
/* MD5 it */
|
||||||
MD5Init(&c);
|
MD5Init(&c);
|
||||||
MD5Update(&c, &r, sizeof r);
|
MD5Update(&c, &r, sizeof r);
|
||||||
@@ -112,22 +112,18 @@ void get_random_info(char seed[16])
|
|||||||
|
|
||||||
#else /* WIN32 */
|
#else /* WIN32 */
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
|
||||||
void get_system_time(uuid_time_t *uuid_time)
|
void get_system_time(uuid_time_t *uuid_time)
|
||||||
{
|
{
|
||||||
struct timeval tp;
|
struct timeval tp;
|
||||||
|
|
||||||
gettimeofday(&tp, (struct timezone *)0);
|
gettimeofday(&tp, (struct timezone *)0);
|
||||||
/*
|
/* Offset between UUID formatted times and Unix formatted times.
|
||||||
Offset between UUID formatted times and Unix formatted times.
|
* UUID UTC base time is October 15, 1582.
|
||||||
UUID UTC base time is October 15, 1582.
|
* Unix base time is January 1, 1970. */
|
||||||
Unix base time is January 1, 1970.
|
|
||||||
*/
|
|
||||||
*uuid_time = (uuid_time_t) (tp.tv_sec * 10000000 + tp.tv_usec * 10 +
|
*uuid_time = (uuid_time_t) (tp.tv_sec * 10000000 + tp.tv_usec * 10 +
|
||||||
I64(0x01B21DD213814000));
|
0x01B21DD213814000LL);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
|
||||||
void get_random_info(unsigned char seed[16])
|
void get_random_info(unsigned char seed[16])
|
||||||
{
|
{
|
||||||
MD5_CTX c;
|
MD5_CTX c;
|
||||||
@@ -138,12 +134,12 @@ void get_random_info(unsigned char seed[16])
|
|||||||
} randomness;
|
} randomness;
|
||||||
randomness r;
|
randomness r;
|
||||||
|
|
||||||
/* Initialize memory area so that valgrind does not complain */
|
/* Initialize memory area so that valgrind does not complain. */
|
||||||
memset(&r, 0, sizeof r);
|
memset(&r, 0, sizeof r);
|
||||||
|
|
||||||
/* Get some random stuff */
|
/* Get some random stuff. */
|
||||||
gettimeofday(&r.t, (struct timezone *)0);
|
gettimeofday(&r.t, (struct timezone *)0);
|
||||||
gethostname(r.hostname, 256 );
|
gethostname(r.hostname, 256);
|
||||||
|
|
||||||
/* MD5 it */
|
/* MD5 it */
|
||||||
MD5Init(&c);
|
MD5Init(&c);
|
||||||
@@ -152,4 +148,3 @@ void get_random_info(unsigned char seed[16])
|
|||||||
};
|
};
|
||||||
|
|
||||||
#endif /* WIN32 */
|
#endif /* WIN32 */
|
||||||
|
|
||||||
|
|||||||
@@ -19,101 +19,70 @@
|
|||||||
*
|
*
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \file
|
||||||
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
|
||||||
#include "uuid.h"
|
#include "uuid.h"
|
||||||
|
|
||||||
|
|
||||||
#include "UpnpInet.h"
|
#include "UpnpInet.h"
|
||||||
|
#include "UpnpStdInt.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
/* various forward declarations. */
|
||||||
|
static int read_state(uint16_t *clockseq,
|
||||||
|
uuid_time_t *timestamp, uuid_node_t * node);
|
||||||
|
static void write_state(uint16_t clockseq,
|
||||||
|
uuid_time_t timestamp, uuid_node_t node);
|
||||||
|
static void format_uuid_v1(uuid_upnp *uid,
|
||||||
|
uint16_t clockseq,
|
||||||
|
uuid_time_t timestamp, uuid_node_t node);
|
||||||
|
static void format_uuid_v3(uuid_upnp *uid, unsigned char hash[16]);
|
||||||
|
static void get_current_time(uuid_time_t *timestamp);
|
||||||
|
static uint16_t true_random(void);
|
||||||
|
|
||||||
/*
|
/*!
|
||||||
various forward declarations
|
* \brief Generator of a UUID.
|
||||||
*/
|
*/
|
||||||
static int read_state( unsigned16 * clockseq,
|
int uuid_create(uuid_upnp * uid)
|
||||||
uuid_time_t * timestamp,
|
|
||||||
uuid_node_t * node );
|
|
||||||
static void write_state( unsigned16 clockseq,
|
|
||||||
uuid_time_t timestamp,
|
|
||||||
uuid_node_t node );
|
|
||||||
static void format_uuid_v1( uuid_upnp * uid,
|
|
||||||
unsigned16 clockseq,
|
|
||||||
uuid_time_t timestamp,
|
|
||||||
uuid_node_t node );
|
|
||||||
static void format_uuid_v3( uuid_upnp * uid,
|
|
||||||
unsigned char hash[16] );
|
|
||||||
static void get_current_time( uuid_time_t * timestamp );
|
|
||||||
static unsigned16 true_random( void );
|
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
|
||||||
/*
|
|
||||||
uuid_create -- generator a UUID
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
uuid_create(uuid_upnp *uid)
|
|
||||||
{
|
{
|
||||||
uuid_time_t timestamp;
|
uuid_time_t timestamp;
|
||||||
uuid_time_t last_time;
|
uuid_time_t last_time;
|
||||||
unsigned16 clockseq;
|
uint16_t clockseq;
|
||||||
uuid_node_t node;
|
uuid_node_t node;
|
||||||
uuid_node_t last_node;
|
uuid_node_t last_node;
|
||||||
int f;
|
int f;
|
||||||
|
|
||||||
/*
|
/* acquire system wide lock so we're alone. */
|
||||||
acquire system wide lock so we're alone
|
UUIDLock();
|
||||||
*/
|
/* get current time. */
|
||||||
UUIDLock();
|
get_current_time(×tamp);
|
||||||
|
/* get node ID. */
|
||||||
|
get_ieee_node_identifier(&node);
|
||||||
|
/* get saved state from NV storage. */
|
||||||
|
f = read_state(&clockseq, &last_time, &last_node);
|
||||||
|
/* if no NV state, or if clock went backwards, or node ID changed
|
||||||
|
* (e.g., net card swap) change clockseq. */
|
||||||
|
if (!f || memcmp(&node, &last_node, sizeof(uuid_node_t)))
|
||||||
|
clockseq = true_random();
|
||||||
|
else if (timestamp < last_time)
|
||||||
|
clockseq++;
|
||||||
|
/* stuff fields into the UUID. */
|
||||||
|
format_uuid_v1(uid, clockseq, timestamp, node);
|
||||||
|
/* save the state for next time. */
|
||||||
|
write_state(clockseq, timestamp, node);
|
||||||
|
UUIDUnlock();
|
||||||
|
|
||||||
/*
|
return 1;
|
||||||
get current time
|
|
||||||
*/
|
|
||||||
get_current_time(×tamp);
|
|
||||||
|
|
||||||
/*
|
|
||||||
get node ID
|
|
||||||
*/
|
|
||||||
get_ieee_node_identifier(&node);
|
|
||||||
|
|
||||||
/*
|
|
||||||
get saved state from NV storage
|
|
||||||
*/
|
|
||||||
f = read_state(&clockseq, &last_time, &last_node);
|
|
||||||
|
|
||||||
/*
|
|
||||||
if no NV state, or if clock went backwards, or node ID changed
|
|
||||||
(e.g., net card swap) change clockseq
|
|
||||||
*/
|
|
||||||
if (!f || memcmp(&node, &last_node, sizeof(uuid_node_t))) {
|
|
||||||
clockseq = true_random();
|
|
||||||
} else if (timestamp < last_time) {
|
|
||||||
clockseq++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
stuff fields into the UUID
|
|
||||||
*/
|
|
||||||
format_uuid_v1(uid, clockseq, timestamp, node);
|
|
||||||
|
|
||||||
/*
|
|
||||||
save the state for next time
|
|
||||||
*/
|
|
||||||
write_state(clockseq, timestamp, node);
|
|
||||||
|
|
||||||
UUIDUnlock();
|
|
||||||
return 1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
void uuid_unpack(uuid_upnp * u, char *out)
|
||||||
void
|
|
||||||
uuid_unpack(uuid_upnp *u, char *out)
|
|
||||||
{
|
{
|
||||||
sprintf(out,
|
sprintf(out,
|
||||||
"%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
|
"%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
|
||||||
@@ -123,272 +92,209 @@ uuid_unpack(uuid_upnp *u, char *out)
|
|||||||
u->clock_seq_hi_and_reserved,
|
u->clock_seq_hi_and_reserved,
|
||||||
u->clock_seq_low,
|
u->clock_seq_low,
|
||||||
u->node[0],
|
u->node[0],
|
||||||
u->node[1],
|
u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
|
||||||
u->node[2],
|
|
||||||
u->node[3],
|
|
||||||
u->node[4],
|
|
||||||
u->node[5]);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*!
|
||||||
/*
|
* \brief Make a UUID from the timestamp, clockseq, and node ID.
|
||||||
format_uuid_v1 -- make a UUID from the timestamp, clockseq,
|
|
||||||
and node ID
|
|
||||||
*/
|
*/
|
||||||
void
|
void format_uuid_v1(uuid_upnp * uid,
|
||||||
format_uuid_v1( uuid_upnp * uid,
|
uint16_t clock_seq,
|
||||||
unsigned16 clock_seq,
|
uuid_time_t timestamp, uuid_node_t node)
|
||||||
uuid_time_t timestamp,
|
|
||||||
uuid_node_t node )
|
|
||||||
{
|
{
|
||||||
/*
|
/* Construct a version 1 uuid with the information we've gathered
|
||||||
Construct a version 1 uuid with the information we've gathered
|
* plus a few constants. */
|
||||||
* plus a few constants.
|
uid->time_low = (uint32_t)(timestamp & 0xFFFFFFFF);
|
||||||
*/
|
uid->time_mid = (uint16_t)((timestamp >> 32) & 0xFFFF);
|
||||||
uid->time_low = (unsigned long)(timestamp & 0xFFFFFFFF);
|
uid->time_hi_and_version = (uint16_t)((timestamp >> 48) & 0x0FFF);
|
||||||
uid->time_mid = (unsigned short)((timestamp >> 32) & 0xFFFF);
|
uid->time_hi_and_version |= (1 << 12);
|
||||||
uid->time_hi_and_version = (unsigned short)((timestamp >> 48) & 0x0FFF);
|
uid->clock_seq_low = (uint8_t) (clock_seq & 0xFF);
|
||||||
uid->time_hi_and_version |= (1 << 12);
|
uid->clock_seq_hi_and_reserved = (uint8_t) (clock_seq & 0x3F00) >> 8;
|
||||||
uid->clock_seq_low = (unsigned8)(clock_seq & 0xFF);
|
uid->clock_seq_hi_and_reserved |= 0x80;
|
||||||
uid->clock_seq_hi_and_reserved = (unsigned8)( clock_seq & 0x3F00 ) >> 8;
|
memcpy(&uid->node, &node, sizeof uid->node);
|
||||||
uid->clock_seq_hi_and_reserved |= 0x80;
|
|
||||||
memcpy( &uid->node, &node, sizeof uid->node );
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*! Data type for UUID generator persistent state. */
|
||||||
/*
|
|
||||||
data type for UUID generator persistent state
|
|
||||||
*/
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uuid_time_t ts; /* saved timestamp */
|
/*! Saved timestamp. */
|
||||||
uuid_node_t node; /* saved node ID */
|
uuid_time_t ts;
|
||||||
unsigned16 cs; /* saved clock sequence */
|
/*! Saved node ID. */
|
||||||
|
uuid_node_t node;
|
||||||
|
/*! Saved clock sequence. */
|
||||||
|
uint16_t cs;
|
||||||
} uuid_state;
|
} uuid_state;
|
||||||
|
|
||||||
static uuid_state st;
|
static uuid_state st;
|
||||||
static int stateInited = 0;
|
static int stateInited = 0;
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*!
|
||||||
/*
|
* \brief Read UUID generator state from non-volatile store.
|
||||||
read_state -- read UUID generator state from non-volatile store
|
|
||||||
*/
|
*/
|
||||||
int
|
int read_state(uint16_t *clockseq,
|
||||||
read_state( unsigned16 * clockseq,
|
uuid_time_t *timestamp, uuid_node_t *node)
|
||||||
uuid_time_t * timestamp,
|
|
||||||
uuid_node_t * node )
|
|
||||||
{
|
{
|
||||||
|
if (!stateInited)
|
||||||
|
return 0;
|
||||||
|
*clockseq = st.cs;
|
||||||
|
*timestamp = st.ts;
|
||||||
|
*node = st.node;
|
||||||
|
|
||||||
if( !stateInited ) {
|
return 1;
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
*clockseq = st.cs;
|
|
||||||
*timestamp = st.ts;
|
|
||||||
*node = st.node;
|
|
||||||
return 1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*!
|
||||||
/*
|
* \brief Save UUID generator state back to non-volatile storage.
|
||||||
write_state -- save UUID generator state back to non-volatile
|
|
||||||
storage
|
|
||||||
*/
|
*/
|
||||||
void
|
void write_state(uint16_t clockseq, uuid_time_t timestamp, uuid_node_t node)
|
||||||
write_state( unsigned16 clockseq,
|
|
||||||
uuid_time_t timestamp,
|
|
||||||
uuid_node_t node )
|
|
||||||
{
|
{
|
||||||
static uuid_time_t next_save;
|
static uuid_time_t next_save;
|
||||||
|
|
||||||
if( !stateInited ) {
|
if (!stateInited) {
|
||||||
next_save = timestamp;
|
next_save = timestamp;
|
||||||
stateInited = 1;
|
stateInited = 1;
|
||||||
};
|
};
|
||||||
|
/* always save state to volatile shared state. */
|
||||||
/*
|
st.cs = clockseq;
|
||||||
always save state to volatile shared state
|
st.ts = timestamp;
|
||||||
*/
|
st.node = node;
|
||||||
st.cs = clockseq;
|
if (timestamp >= next_save) {
|
||||||
st.ts = timestamp;
|
/* schedule next save for 10 seconds from now. */
|
||||||
st.node = node;
|
next_save = timestamp + (10 * 10 * 1000 * 1000);
|
||||||
if( timestamp >= next_save ) {
|
};
|
||||||
/*
|
|
||||||
schedule next save for 10 seconds from now
|
|
||||||
*/
|
|
||||||
next_save = timestamp + ( 10 * 10 * 1000 * 1000 );
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*!
|
||||||
/*
|
* \brief Get time as 60 bit 100ns ticks since whenever.
|
||||||
get-current_time -- get time as 60 bit 100ns ticks since whenever.
|
*
|
||||||
Compensate for the fact that real clock resolution is
|
* Compensate for the fact that real clock resolution is less than 100ns.
|
||||||
less than 100ns.
|
|
||||||
*/
|
*/
|
||||||
void
|
void get_current_time(uuid_time_t *timestamp)
|
||||||
get_current_time( uuid_time_t * timestamp )
|
|
||||||
{
|
{
|
||||||
uuid_time_t time_now;
|
uuid_time_t time_now;
|
||||||
static uuid_time_t time_last;
|
static uuid_time_t time_last;
|
||||||
static unsigned16 uuids_this_tick;
|
static uint16_t uuids_this_tick;
|
||||||
static int inited = 0;
|
static int inited = 0;
|
||||||
|
|
||||||
if( !inited ) {
|
if (!inited) {
|
||||||
uuids_this_tick = UUIDS_PER_TICK;
|
uuids_this_tick = UUIDS_PER_TICK;
|
||||||
inited = 1;
|
inited = 1;
|
||||||
};
|
};
|
||||||
|
while (1) {
|
||||||
while( 1 ) {
|
get_system_time(&time_now);
|
||||||
get_system_time( &time_now );
|
/* if clock reading changed since last UUID generated... */
|
||||||
|
if (time_last != time_now) {
|
||||||
/*
|
/* reset count of uuids gen'd with this clock reading. */
|
||||||
if clock reading changed since last UUID generated...
|
uuids_this_tick = 0;
|
||||||
*/
|
break;
|
||||||
if( time_last != time_now ) {
|
};
|
||||||
/*
|
if (uuids_this_tick < UUIDS_PER_TICK) {
|
||||||
reset count of uuids gen'd with this clock reading
|
uuids_this_tick++;
|
||||||
*/
|
break;
|
||||||
uuids_this_tick = 0;
|
};
|
||||||
break;
|
/* going too fast for our clock; spin. */
|
||||||
};
|
};
|
||||||
|
/* add the count of uuids to low order bits of the clock reading. */
|
||||||
if( uuids_this_tick < UUIDS_PER_TICK ) {
|
*timestamp = time_now + uuids_this_tick;
|
||||||
uuids_this_tick++;
|
time_last = *timestamp;
|
||||||
break;
|
|
||||||
};
|
|
||||||
/*
|
|
||||||
going too fast for our clock; spin
|
|
||||||
*/
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
add the count of uuids to low order bits of the clock reading
|
|
||||||
*/
|
|
||||||
*timestamp = time_now + uuids_this_tick;
|
|
||||||
time_last = *timestamp;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*!
|
||||||
/*
|
* \brief generate a crypto-quality random number.
|
||||||
true_random -- generate a crypto-quality random number.
|
* This sample doesn't do that.
|
||||||
This sample doesn't do that.
|
|
||||||
*/
|
*/
|
||||||
static unsigned16
|
static uint16_t true_random(void)
|
||||||
true_random( void )
|
|
||||||
{
|
{
|
||||||
static int inited = 0;
|
static int inited = 0;
|
||||||
uuid_time_t time_now;
|
uuid_time_t time_now;
|
||||||
|
|
||||||
if( !inited ) {
|
if (!inited) {
|
||||||
get_system_time( &time_now );
|
get_system_time(&time_now);
|
||||||
time_now = time_now / UUIDS_PER_TICK;
|
time_now = time_now / UUIDS_PER_TICK;
|
||||||
srand( ( unsigned int )( ( ( time_now >> 32 ) ^ time_now ) &
|
srand((unsigned int)(((time_now >> 32) ^ time_now) &
|
||||||
0xffffffff ) );
|
0xffffffff));
|
||||||
inited = 1;
|
inited = 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
return (unsigned16)( rand() );
|
return (uint16_t) (rand());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*!
|
||||||
/*
|
* \brief Create a UUID using a "name" from a "name space".
|
||||||
uuid_create_from_name -- create a UUID using a "name" from a "name space"
|
|
||||||
*/
|
*/
|
||||||
void
|
void uuid_create_from_name(
|
||||||
uuid_create_from_name( uuid_upnp * uid, /* resulting UUID */
|
/*! resulting UUID. */
|
||||||
|
uuid_upnp *uid,
|
||||||
uuid_upnp nsid, /* UUID to serve as context, so identical
|
/*! UUID to serve as context, so identical names from different name
|
||||||
names from different name spaces generate
|
* spaces generate different UUIDs. */
|
||||||
different UUIDs */
|
uuid_upnp nsid,
|
||||||
|
/*! The name from which to generate a UUID. */
|
||||||
void *name, /* the name from which to generate a UUID */
|
void *name,
|
||||||
|
/*! The length of the name. */
|
||||||
int namelen /* the length of the name */
|
int namelen)
|
||||||
)
|
|
||||||
{
|
{
|
||||||
MD5_CTX c;
|
MD5_CTX c;
|
||||||
unsigned char hash[16];
|
unsigned char hash[16];
|
||||||
uuid_upnp net_nsid; /* context UUID in network byte order */
|
uuid_upnp net_nsid; /* context UUID in network byte order */
|
||||||
|
|
||||||
/*
|
/* put name space ID in network byte order so it hashes the same no matter
|
||||||
put name space ID in network byte order so it hashes the same
|
* what endian machine we're on. */
|
||||||
no matter what endian machine we're on
|
net_nsid = nsid;
|
||||||
*/
|
net_nsid.time_low = htonl(net_nsid.time_low);
|
||||||
net_nsid = nsid;
|
net_nsid.time_mid = htons(net_nsid.time_mid);
|
||||||
net_nsid.time_low=htonl( (uint32_t)net_nsid.time_low );
|
net_nsid.time_hi_and_version = htons(net_nsid.time_hi_and_version);
|
||||||
net_nsid.time_mid=htons( net_nsid.time_mid );
|
MD5Init(&c);
|
||||||
net_nsid.time_hi_and_version=htons( net_nsid.time_hi_and_version );
|
MD5Update(&c, (unsigned char *)&net_nsid, sizeof(uuid_upnp));
|
||||||
|
MD5Update(&c, name, (unsigned int)namelen);
|
||||||
MD5Init( &c );
|
MD5Final(hash, &c);
|
||||||
MD5Update( &c, (unsigned char *)&net_nsid, sizeof(uuid_upnp) );
|
/* the hash is in network byte order at this point. */
|
||||||
MD5Update( &c, name, (unsigned int)namelen );
|
format_uuid_v3(uid, hash);
|
||||||
MD5Final( hash, &c );
|
|
||||||
|
|
||||||
/*
|
|
||||||
the hash is in network byte order at this point
|
|
||||||
*/
|
|
||||||
format_uuid_v3( uid, hash );
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*!
|
||||||
/*
|
* \brief Make a UUID from a (pseudo)random 128 bit number.
|
||||||
format_uuid_v3 -- make a UUID from a (pseudo)random 128 bit number
|
|
||||||
*/
|
*/
|
||||||
void
|
void format_uuid_v3(uuid_upnp *uid, unsigned char hash[16])
|
||||||
format_uuid_v3( uuid_upnp * uid,
|
|
||||||
unsigned char hash[16] )
|
|
||||||
{
|
{
|
||||||
/*
|
/* Construct a version 3 uuid with the (pseudo-)random number plus a few
|
||||||
Construct a version 3 uuid with the (pseudo-)random number
|
* constants. */
|
||||||
* plus a few constants.
|
memcpy(uid, hash, sizeof(uuid_upnp));
|
||||||
*/
|
/* convert UUID to local byte order. */
|
||||||
|
uid->time_low = ntohl(uid->time_low);
|
||||||
memcpy( uid, hash, sizeof( uuid_upnp ) );
|
uid->time_mid = ntohs(uid->time_mid);
|
||||||
|
uid->time_hi_and_version = ntohs(uid->time_hi_and_version);
|
||||||
/*
|
/* put in the variant and version bits. */
|
||||||
convert UUID to local byte order
|
uid->time_hi_and_version &= 0x0FFF;
|
||||||
*/
|
uid->time_hi_and_version |= (3 << 12);
|
||||||
uid->time_low=ntohl( (uint32_t)uid->time_low );
|
uid->clock_seq_hi_and_reserved &= 0x3F;
|
||||||
uid->time_mid=ntohs( uid->time_mid );
|
uid->clock_seq_hi_and_reserved |= 0x80;
|
||||||
uid->time_hi_and_version=ntohs( uid->time_hi_and_version );
|
|
||||||
|
|
||||||
/*
|
|
||||||
put in the variant and version bits
|
|
||||||
*/
|
|
||||||
uid->time_hi_and_version &= 0x0FFF;
|
|
||||||
uid->time_hi_and_version |= ( 3 << 12 );
|
|
||||||
uid->clock_seq_hi_and_reserved &= 0x3F;
|
|
||||||
uid->clock_seq_hi_and_reserved |= 0x80;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
|
||||||
/*
|
|
||||||
uuid_compare -- Compare two UUID's "lexically" and return
|
|
||||||
-1 u1 is lexically before u2
|
|
||||||
0 u1 is equal to u2
|
|
||||||
1 u1 is lexically after u2
|
|
||||||
|
|
||||||
Note: lexical ordering is not temporal ordering!
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
uuid_compare( uuid_upnp * u1,
|
|
||||||
uuid_upnp * u2 )
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
#define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
|
#define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
|
||||||
CHECK( u1->time_low, u2->time_low );
|
|
||||||
CHECK( u1->time_mid, u2->time_mid );
|
|
||||||
CHECK( u1->time_hi_and_version, u2->time_hi_and_version );
|
|
||||||
CHECK( u1->clock_seq_hi_and_reserved, u2->clock_seq_hi_and_reserved );
|
|
||||||
CHECK( u1->clock_seq_low, u2->clock_seq_low )
|
|
||||||
for( i = 0; i < 6; i++ ) {
|
|
||||||
if( u1->node[i] < u2->node[i] )
|
|
||||||
return -1;
|
|
||||||
if( u1->node[i] > u2->node[i] )
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
/*!
|
||||||
|
* \brief Compare two UUID's "lexically" and return.
|
||||||
|
*
|
||||||
|
* \li -1: u1 is lexically before u2
|
||||||
|
* \li 0: u1 is equal to u2
|
||||||
|
* \li 1: u1 is lexically after u2
|
||||||
|
*
|
||||||
|
* Note: Lexical ordering is not temporal ordering!
|
||||||
|
*/
|
||||||
|
int uuid_compare(uuid_upnp *u1, uuid_upnp *u2)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
CHECK(u1->time_low, u2->time_low);
|
||||||
|
CHECK(u1->time_mid, u2->time_mid);
|
||||||
|
CHECK(u1->time_hi_and_version, u2->time_hi_and_version);
|
||||||
|
CHECK(u1->clock_seq_hi_and_reserved, u2->clock_seq_hi_and_reserved);
|
||||||
|
CHECK(u1->clock_seq_low, u2->clock_seq_low)
|
||||||
|
for (i = 0; i < 6; i++) {
|
||||||
|
if (u1->node[i] < u2->node[i])
|
||||||
|
return -1;
|
||||||
|
if (u1->node[i] > u2->node[i])
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user