Get rid of useless integer typedefs.

Remove unsigned32, unsigned16 and unsigned8 references in the code.
(cherry picked from commit bb5a80c05ba8357771dafbf436e9055b567f38be)
This commit is contained in:
Marcelo Roberto Jimenez 2010-12-18 18:17:14 -02:00
parent 2ccaf7fac8
commit 82738e3ea5
4 changed files with 245 additions and 360 deletions

View File

@ -31,6 +31,8 @@
#include "global.h"
#include "md5.h"
#include "UpnpStdInt.h"
#include <sys/types.h>
#ifdef WIN32
@ -49,21 +51,7 @@ extern ithread_mutex_t gUUIDMutex;
#define UUIDLock() ithread_mutex_lock(&gUUIDMutex)
#define UUIDUnlock() ithread_mutex_unlock(&gUUIDMutex)
typedef unsigned long unsigned32;
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 uint64_t uuid_time_t;
typedef struct {
char nodeID[6];

View File

@ -1,3 +1,6 @@
#ifndef UUID_H
#define UUID_H
/*
* Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
* Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
@ -17,27 +20,24 @@
* this software for any purpose.
*/
#include "sysdep.h"
/*! . */
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;
/*!
* \brief Generate a UUID.
*/
@ -45,32 +45,28 @@ int uuid_create(
/*! . */
uuid_upnp * id);
/*!
* \brief Out will be xxxx-xx-xx-xx-xxxxxx format.
*/
void uuid_unpack(
/*! . */
uuid_upnp *u,
uuid_upnp * u,
/*! . */
char *out);
/*!
* \brief Create a UUID using a "name" from a "name space"
*/
void uuid_create_from_name(
/*! Resulting UUID. */
uuid_upnp *uid,
uuid_upnp * uid,
/*! UUID to serve as context, so identical names from different name
* spaces generate different UUIDs. */
* spaces generate different UUIDs. */
uuid_upnp nsid,
/*! The name from which to generate a UUID. */
void *name,
/*! The length of the name. */
int namelen
);
int namelen);
/*!
* \brief Compare two UUID's "lexically".
@ -84,7 +80,7 @@ void uuid_create_from_name(
*/
int uuid_compare(
/*! . */
uuid_upnp *u1,
uuid_upnp * u1,
/*! . */
uuid_upnp *u2);
uuid_upnp * u2);
#endif /* UUID_H */

View File

@ -17,6 +17,10 @@
* this software for any purpose.
*/
/*!
* \file
*/
#include "config.h"
#include "sysdep.h"
@ -26,10 +30,10 @@
#include <string.h>
#include <stdio.h>
/*-----------------------------------------------------------------------------*/
/*
system dependent call to get IEEE node ID.
This sample implementation generates a random node ID
/*!
* \brief System dependent call to get IEEE node ID.
*
* This sample implementation generates a random node ID.
*/
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;
};
/*-----------------------------------------------------------------------------*/
/*
system dependent call to get the current system time.
Returned as 100ns ticks since Oct 15, 1582, but resolution may be
less than 100ns.
/*!
* \brief System dependent call to get the current system time.
*
* Returned as 100ns ticks since Oct 15, 1582, but resolution may be less
* than 100ns.
*/
#ifdef WIN32
@ -72,7 +76,6 @@ void get_system_time(uuid_time_t *uuid_time)
*uuid_time = time.QuadPart;
};
/*-----------------------------------------------------------------------------*/
void get_random_info(char seed[16])
{
MD5_CTX c;
@ -89,7 +92,6 @@ void get_random_info(char seed[16])
/* Initialize memory area so that valgrind does not complain */
memset(&r, 0, sizeof r);
/* memory usage stats */
GlobalMemoryStatus( &r.m );
/* random system stats */
@ -101,9 +103,7 @@ void get_random_info(char seed[16])
/* milliseconds since last boot */
r.tc = GetTickCount();
r.l = MAX_COMPUTERNAME_LENGTH + 1;
GetComputerName( r.hostname, &r.l );
/* MD5 it */
MD5Init(&c);
MD5Update(&c, &r, sizeof r);
@ -112,22 +112,18 @@ void get_random_info(char seed[16])
#else /* WIN32 */
/*-----------------------------------------------------------------------------*/
void get_system_time(uuid_time_t *uuid_time)
{
struct timeval tp;
gettimeofday(&tp, (struct timezone *)0);
/*
Offset between UUID formatted times and Unix formatted times.
UUID UTC base time is October 15, 1582.
Unix base time is January 1, 1970.
*/
/* Offset between UUID formatted times and Unix formatted times.
* UUID UTC base time is October 15, 1582.
* Unix base time is January 1, 1970. */
*uuid_time = (uuid_time_t) (tp.tv_sec * 10000000 + tp.tv_usec * 10 +
I64(0x01B21DD213814000));
0x01B21DD213814000LL);
};
/*-----------------------------------------------------------------------------*/
void get_random_info(unsigned char seed[16])
{
MD5_CTX c;
@ -138,12 +134,12 @@ void get_random_info(unsigned char seed[16])
} randomness;
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);
/* Get some random stuff */
/* Get some random stuff. */
gettimeofday(&r.t, (struct timezone *)0);
gethostname(r.hostname, 256 );
gethostname(r.hostname, 256);
/* MD5 it */
MD5Init(&c);
@ -152,4 +148,3 @@ void get_random_info(unsigned char seed[16])
};
#endif /* WIN32 */

View File

@ -19,101 +19,70 @@
*
**************************************************************************/
/*!
* \file
*/
#include "config.h"
#include "uuid.h"
#include "UpnpInet.h"
#include "UpnpStdInt.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.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,
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)
int uuid_create(uuid_upnp * uid)
{
uuid_time_t timestamp;
uuid_time_t last_time;
unsigned16 clockseq;
uuid_node_t node;
uuid_node_t last_node;
int f;
uuid_time_t timestamp;
uuid_time_t last_time;
uint16_t clockseq;
uuid_node_t node;
uuid_node_t last_node;
int f;
/*
acquire system wide lock so we're alone
*/
UUIDLock();
/* acquire system wide lock so we're alone. */
UUIDLock();
/* get current time. */
get_current_time(&timestamp);
/* 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();
/*
get current time
*/
get_current_time(&timestamp);
/*
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;
return 1;
};
/*-----------------------------------------------------------------------------*/
void
uuid_unpack(uuid_upnp *u, char *out)
void uuid_unpack(uuid_upnp * u, char *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",
@ -123,272 +92,209 @@ uuid_unpack(uuid_upnp *u, char *out)
u->clock_seq_hi_and_reserved,
u->clock_seq_low,
u->node[0],
u->node[1],
u->node[2],
u->node[3],
u->node[4],
u->node[5]);
u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
};
/*-----------------------------------------------------------------------------*/
/*
format_uuid_v1 -- make a UUID from the timestamp, clockseq,
and node ID
/*!
* \brief Make a UUID from the timestamp, clockseq, and node ID.
*/
void
format_uuid_v1( uuid_upnp * uid,
unsigned16 clock_seq,
uuid_time_t timestamp,
uuid_node_t node )
void format_uuid_v1(uuid_upnp * uid,
uint16_t clock_seq,
uuid_time_t timestamp, uuid_node_t node)
{
/*
Construct a version 1 uuid with the information we've gathered
* plus a few constants.
*/
uid->time_low = (unsigned long)(timestamp & 0xFFFFFFFF);
uid->time_mid = (unsigned short)((timestamp >> 32) & 0xFFFF);
uid->time_hi_and_version = (unsigned short)((timestamp >> 48) & 0x0FFF);
uid->time_hi_and_version |= (1 << 12);
uid->clock_seq_low = (unsigned8)(clock_seq & 0xFF);
uid->clock_seq_hi_and_reserved = (unsigned8)( clock_seq & 0x3F00 ) >> 8;
uid->clock_seq_hi_and_reserved |= 0x80;
memcpy( &uid->node, &node, sizeof uid->node );
/* Construct a version 1 uuid with the information we've gathered
* plus a few constants. */
uid->time_low = (uint32_t)(timestamp & 0xFFFFFFFF);
uid->time_mid = (uint16_t)((timestamp >> 32) & 0xFFFF);
uid->time_hi_and_version = (uint16_t)((timestamp >> 48) & 0x0FFF);
uid->time_hi_and_version |= (1 << 12);
uid->clock_seq_low = (uint8_t) (clock_seq & 0xFF);
uid->clock_seq_hi_and_reserved = (uint8_t) (clock_seq & 0x3F00) >> 8;
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 {
uuid_time_t ts; /* saved timestamp */
uuid_node_t node; /* saved node ID */
unsigned16 cs; /* saved clock sequence */
/*! Saved timestamp. */
uuid_time_t ts;
/*! Saved node ID. */
uuid_node_t node;
/*! Saved clock sequence. */
uint16_t cs;
} uuid_state;
static uuid_state st;
static int stateInited = 0;
/*-----------------------------------------------------------------------------*/
/*
read_state -- read UUID generator state from non-volatile store
/*!
* \brief Read UUID generator state from non-volatile store.
*/
int
read_state( unsigned16 * clockseq,
uuid_time_t * timestamp,
uuid_node_t * node )
int read_state(uint16_t *clockseq,
uuid_time_t *timestamp, uuid_node_t *node)
{
if (!stateInited)
return 0;
*clockseq = st.cs;
*timestamp = st.ts;
*node = st.node;
if( !stateInited ) {
return 0;
};
*clockseq = st.cs;
*timestamp = st.ts;
*node = st.node;
return 1;
return 1;
};
/*-----------------------------------------------------------------------------*/
/*
write_state -- save UUID generator state back to non-volatile
storage
/*!
* \brief Save UUID generator state back to non-volatile storage.
*/
void
write_state( unsigned16 clockseq,
uuid_time_t timestamp,
uuid_node_t node )
void write_state(uint16_t clockseq, uuid_time_t timestamp, uuid_node_t node)
{
static uuid_time_t next_save;
static uuid_time_t next_save;
if( !stateInited ) {
next_save = timestamp;
stateInited = 1;
};
/*
always save state to volatile shared state
*/
st.cs = clockseq;
st.ts = timestamp;
st.node = node;
if( timestamp >= next_save ) {
/*
schedule next save for 10 seconds from now
*/
next_save = timestamp + ( 10 * 10 * 1000 * 1000 );
};
if (!stateInited) {
next_save = timestamp;
stateInited = 1;
};
/* always save state to volatile shared state. */
st.cs = clockseq;
st.ts = timestamp;
st.node = node;
if (timestamp >= next_save) {
/* schedule next save for 10 seconds from now. */
next_save = timestamp + (10 * 10 * 1000 * 1000);
};
};
/*-----------------------------------------------------------------------------*/
/*
get-current_time -- get time as 60 bit 100ns ticks since whenever.
Compensate for the fact that real clock resolution is
less than 100ns.
/*!
* \brief Get time as 60 bit 100ns ticks since whenever.
*
* Compensate for the fact that real clock resolution is less than 100ns.
*/
void
get_current_time( uuid_time_t * timestamp )
void get_current_time(uuid_time_t *timestamp)
{
uuid_time_t time_now;
static uuid_time_t time_last;
static unsigned16 uuids_this_tick;
static int inited = 0;
uuid_time_t time_now;
static uuid_time_t time_last;
static uint16_t uuids_this_tick;
static int inited = 0;
if( !inited ) {
uuids_this_tick = UUIDS_PER_TICK;
inited = 1;
};
while( 1 ) {
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
*/
uuids_this_tick = 0;
break;
};
if( uuids_this_tick < UUIDS_PER_TICK ) {
uuids_this_tick++;
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;
if (!inited) {
uuids_this_tick = UUIDS_PER_TICK;
inited = 1;
};
while (1) {
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. */
uuids_this_tick = 0;
break;
};
if (uuids_this_tick < UUIDS_PER_TICK) {
uuids_this_tick++;
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;
};
/*-----------------------------------------------------------------------------*/
/*
true_random -- generate a crypto-quality random number.
This sample doesn't do that.
/*!
* \brief generate a crypto-quality random number.
* This sample doesn't do that.
*/
static unsigned16
true_random( void )
static uint16_t true_random(void)
{
static int inited = 0;
uuid_time_t time_now;
static int inited = 0;
uuid_time_t time_now;
if( !inited ) {
get_system_time( &time_now );
time_now = time_now / UUIDS_PER_TICK;
srand( ( unsigned int )( ( ( time_now >> 32 ) ^ time_now ) &
0xffffffff ) );
inited = 1;
};
if (!inited) {
get_system_time(&time_now);
time_now = time_now / UUIDS_PER_TICK;
srand((unsigned int)(((time_now >> 32) ^ time_now) &
0xffffffff));
inited = 1;
};
return (unsigned16)( rand() );
return (uint16_t) (rand());
}
/*-----------------------------------------------------------------------------*/
/*
uuid_create_from_name -- 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( uuid_upnp * uid, /* resulting UUID */
uuid_upnp nsid, /* UUID to serve as context, so identical
names from different name spaces generate
different UUIDs */
void *name, /* the name from which to generate a UUID */
int namelen /* the length of the name */
)
void uuid_create_from_name(
/*! resulting UUID. */
uuid_upnp *uid,
/*! UUID to serve as context, so identical names from different name
* spaces generate different UUIDs. */
uuid_upnp nsid,
/*! The name from which to generate a UUID. */
void *name,
/*! The length of the name. */
int namelen)
{
MD5_CTX c;
unsigned char hash[16];
uuid_upnp net_nsid; /* context UUID in network byte order */
MD5_CTX c;
unsigned char hash[16];
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 what endian machine we're on
*/
net_nsid = nsid;
net_nsid.time_low=htonl( (uint32_t)net_nsid.time_low );
net_nsid.time_mid=htons( net_nsid.time_mid );
net_nsid.time_hi_and_version=htons( net_nsid.time_hi_and_version );
MD5Init( &c );
MD5Update( &c, (unsigned char *)&net_nsid, sizeof(uuid_upnp) );
MD5Update( &c, name, (unsigned int)namelen );
MD5Final( hash, &c );
/*
the hash is in network byte order at this point
*/
format_uuid_v3( uid, hash );
/* put name space ID in network byte order so it hashes the same no matter
* what endian machine we're on. */
net_nsid = nsid;
net_nsid.time_low = htonl(net_nsid.time_low);
net_nsid.time_mid = htons(net_nsid.time_mid);
net_nsid.time_hi_and_version = htons(net_nsid.time_hi_and_version);
MD5Init(&c);
MD5Update(&c, (unsigned char *)&net_nsid, sizeof(uuid_upnp));
MD5Update(&c, name, (unsigned int)namelen);
MD5Final(hash, &c);
/* the hash is in network byte order at this point. */
format_uuid_v3(uid, hash);
};
/*-----------------------------------------------------------------------------*/
/*
format_uuid_v3 -- make a UUID from a (pseudo)random 128 bit number
/*!
* \brief Make a UUID from a (pseudo)random 128 bit number.
*/
void
format_uuid_v3( uuid_upnp * uid,
unsigned char hash[16] )
void format_uuid_v3(uuid_upnp *uid, unsigned char hash[16])
{
/*
Construct a version 3 uuid with the (pseudo-)random number
* plus a few constants.
*/
memcpy( uid, hash, sizeof( uuid_upnp ) );
/*
convert UUID to local byte order
*/
uid->time_low=ntohl( (uint32_t)uid->time_low );
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
*/
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;
/* Construct a version 3 uuid with the (pseudo-)random number plus a few
* constants. */
memcpy(uid, hash, sizeof(uuid_upnp));
/* convert UUID to local byte order. */
uid->time_low = ntohl(uid->time_low);
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. */
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;
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;
};