--- uuid.c 2004/01/15 12:38:32 1.25
+++ uuid.c 2004/01/15 13:45:18 1.26
@@ -51,24 +51,27 @@
#include "uuid_bm.h"
#include "uuid_ac.h"
+/* IEEE 802 MAC address octet length */
+#define MAC_OCTETS 6
+
/* UUID binary representation according to UUID standards */
typedef struct {
- uuid_uint32_t time_low;
- uuid_uint16_t time_mid;
- uuid_uint16_t time_hi_and_version;
- uuid_uint8_t clock_seq_hi_and_reserved;
- uuid_uint8_t clock_seq_low;
- uuid_uint8_t node[6];
+ uuid_uint32_t time_low; /* bits 0-31 of time field */
+ uuid_uint16_t time_mid; /* bits 32-47 of time field */
+ uuid_uint16_t time_hi_and_version; /* bits 48-59 of time field plus 4 bit version */
+ uuid_uint8_t clock_seq_hi_and_reserved; /* bits 8-13 of clock sequence field plus 2 bit variant */
+ uuid_uint8_t clock_seq_low; /* bits 0-7 of clock sequence field */
+ uuid_uint8_t node[MAC_OCTETS]; /* bits 0-47 of node MAC address */
} uuid_obj_t;
/* abstract data type (ADT) of API */
struct uuid_st {
- uuid_obj_t obj; /* inlined UUID object */
- prng_t *prng; /* RPNG sub-object */
- md5_t *md5; /* MD5 sub-object */
- uuid_uint8_t mac[6]; /* pre-determined MAC address */
- struct timeval time_last; /* last retrieved timestamp */
- unsigned long time_seq; /* last timestamp sequence counter */
+ uuid_obj_t obj; /* inlined UUID object */
+ prng_t *prng; /* RPNG sub-object */
+ md5_t *md5; /* MD5 sub-object */
+ uuid_uint8_t mac[MAC_OCTETS]; /* pre-determined MAC address */
+ struct timeval time_last; /* last retrieved timestamp */
+ unsigned long time_seq; /* last timestamp sequence counter */
};
/* create UUID object */
@@ -94,7 +97,7 @@
/* resolve MAC address for insertion into node field of UUIDs */
if (!mac_address((unsigned char *)((*uuid)->mac), sizeof((*uuid)->mac))) {
memset((*uuid)->mac, '\0', sizeof((*uuid)->mac));
- (*uuid)->mac[0] = 0x80;
+ (*uuid)->mac[0] = BM_OCTET(1,0,0,0,0,0,0,0);
}
/* initialize time attributes */
@@ -306,7 +309,7 @@
}
/* INTERNAL: check for valid UUID string representation syntax */
-static int uuid_isstr(const char *str)
+static int uuid_isstr(const char *str, size_t str_len)
{
int i;
const char *cp;
@@ -317,18 +320,17 @@
0 1 2 3 */
if (str == NULL)
return UUID_FALSE;
- if (strlen(str) != UUID_LEN_STR)
+ if (str_len == 0)
+ str_len = strlen(str);
+ if (str_len < UUID_LEN_STR)
return UUID_FALSE;
- for (i = 0, cp = str; i <= UUID_LEN_STR; i++, cp++) {
+ for (i = 0, cp = str; i < UUID_LEN_STR; i++, cp++) {
if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
if (*cp == '-')
continue;
else
return UUID_FALSE;
}
- if (i == UUID_LEN_STR)
- if (*cp == '\0')
- continue;
if (!isxdigit((int)(*cp)))
return UUID_FALSE;
}
@@ -348,7 +350,7 @@
return UUID_RC_ARG;
/* check for correct UUID string representation syntax */
- if (!uuid_isstr(str))
+ if (!uuid_isstr(str, 0))
return UUID_RC_ARG;
/* parse hex values of "time" parts */
@@ -386,7 +388,7 @@
return UUID_RC_MEM;
/* format UUID into string representation */
- sprintf(*str,
+ str_snprintf(*str, UUID_LEN_STR+1,
"%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
(unsigned long)uuid->obj.time_low,
(unsigned int)uuid->obj.time_mid,
@@ -407,12 +409,12 @@
static void uuid_brand(uuid_t *uuid, int version)
{
/* set version (as given) */
- uuid->obj.time_hi_and_version &= 0x0fff;
- uuid->obj.time_hi_and_version |= (((uuid_uint16_t)version & 0x0fff) << 12);
+ uuid->obj.time_hi_and_version &= BM_MASK(11,0);
+ uuid->obj.time_hi_and_version |= BM_SHL((uuid_uint16_t)version, 12);
/* set variant (always DCE 1.1 only) */
- uuid->obj.clock_seq_hi_and_reserved &= ~((0x03) << 6);
- uuid->obj.clock_seq_hi_and_reserved |= (0x02 << 6);
+ uuid->obj.clock_seq_hi_and_reserved &= BM_MASK(5,0);
+ uuid->obj.clock_seq_hi_and_reserved |= BM_SHL(0x02, 6);
return;
}
@@ -508,7 +510,7 @@
*/
/* retrieve current clock sequence */
- clck = ((uuid->obj.clock_seq_hi_and_reserved & ~((0x03) << 6)) << 8)
+ clck = ((uuid->obj.clock_seq_hi_and_reserved & BM_MASK(5,0)) << 8)
+ uuid->obj.clock_seq_low;
/* generate new random clock sequence (initially or if the
@@ -520,11 +522,11 @@
prng_data(uuid->prng, (void *)&clck, sizeof(clck));
else
clck++;
- clck &= ~((0x03) << 6);
+ clck &= BM_MASK(5,0);
/* store back new clock sequence */
uuid->obj.clock_seq_hi_and_reserved =
- (uuid->obj.clock_seq_hi_and_reserved & ((0x03) << 6))
+ (uuid->obj.clock_seq_hi_and_reserved & BM_MASK(7,6))
| (uuid_uint8_t)((clck >> 8) & 0xff);
uuid->obj.clock_seq_low =
(uuid_uint8_t)(clck & 0xff);
@@ -533,10 +535,10 @@
* GENERATE NODE
*/
- if ((mode & UUID_MCASTRND) || (uuid->mac[0] & 0x80)) {
+ if ((mode & UUID_MCASTRND) || (uuid->mac[0] & BM_OCTET(1,0,0,0,0,0,0,0))) {
/* use random multi-cast MAC address */
prng_data(uuid->prng, (void *)&(uuid->obj.node), sizeof(uuid->obj.node));
- uuid->obj.node[0] |= 0x80;
+ uuid->obj.node[0] |= BM_OCTET(1,0,0,0,0,0,0,0);
}
else {
/* use real regular MAC address */
@@ -594,7 +596,7 @@
return UUID_RC_MEM;
/* load the namespace UUID into MD5 context */
- if (uuid_isstr(ns)) {
+ if (uuid_isstr(ns, 0)) {
/* custom namespace via UUID string representation */
if ((rc = uuid_create(&uuid_object)) != UUID_RC_OK)
return rc;
@@ -745,7 +747,7 @@
/* decode UUID version */
version = "unknown";
- tmp16 = (BM_SHR(uuid->obj.time_hi_and_version, 12) & 0x000f);
+ tmp16 = (BM_SHR(uuid->obj.time_hi_and_version, 12) & BM_MASK(3,0));
for (i = 0; i < sizeof(uuid_dectab_version)/sizeof(uuid_dectab_version[0]); i++) {
if (uuid_dectab_version[i].num == (int)tmp16) {
version = uuid_dectab_version[i].desc;
@@ -764,7 +766,7 @@
/* decode version 1 */
/* decode system time */
- t = ui64_rol(ui64_n2i((unsigned long)(uuid->obj.time_hi_and_version & 0x0fff)), 48, NULL),
+ t = ui64_rol(ui64_n2i((unsigned long)(uuid->obj.time_hi_and_version & BM_MASK(11,0))), 48, NULL),
t = ui64_or(t, ui64_rol(ui64_n2i((unsigned long)(uuid->obj.time_mid)), 32, NULL));
t = ui64_or(t, ui64_n2i((unsigned long)(uuid->obj.time_low)));
offset = ui64_s2i(UUID_TIMEOFFSET, NULL, 16);
@@ -777,7 +779,7 @@
str_rsprintf(str, "content: time: %s.%06d.%d UTC\n", buf, t_usec, t_nsec);
/* decode clock sequence */
- tmp32 = ((uuid->obj.clock_seq_hi_and_reserved & ~((0x03) << 6)) << 8)
+ tmp32 = ((uuid->obj.clock_seq_hi_and_reserved & BM_MASK(5,0)) << 8)
+ uuid->obj.clock_seq_low;
str_rsprintf(str, " clock: %ld (usually random)\n", (unsigned long)tmp32);
|