From 89078e281093c442c5275050cad8867abc6885bf Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Sun, 30 Sep 2012 19:48:29 +0000 Subject: [PATCH] Removed unused node variable. The node is now calculated by read_state and in the state structure. The node could be reset by re-initializing the state or calling the read state function. Given that the mac address is just used to provide additional uniqueness its actual value doesn't really matter, so assuming it wasn't just a bunch of zeroes or similar, there shouldn't be any reason to re-determine it. --- Makefile | 3 +++ internal.c | 43 ++++++++++++++++++++++++++++++++++--------- pduuid.h | 1 + t/sha1.c | 6 ++++-- t/uuidgen.c | 13 ++++++++++++- 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 3dd183c..ed7deb6 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,9 @@ rng.o: rng.c internal.o: internal.c $(CC) $(CFLAGS) -Wall -fPIC -c -o $@ $+ +uuidgen2: libpduuid.a uuidgen2.o + $(CC) $(CFLAGS) -Wall -L. -o $@ uuidgen2.o -lpduuid $(LDFLAGS) + uuidgen: libpduuid.a uuidgen.o $(CC) $(CFLAGS) -Wall -L. -o $@ uuidgen.o -lpduuid $(LDFLAGS) diff --git a/internal.c b/internal.c index 59e1bbf..c911099 100644 --- a/internal.c +++ b/internal.c @@ -127,11 +127,12 @@ static int release_global_lock(void *data) { static uint64_t current_time(void) { uint64_t now; struct timeval tv; + static int seq = 0; /* TODO is this BSD specific? */ gettimeofday(&tv, 0); - now = (tv.tv_sec * 10000000ULL + tv.tv_usec * 10ULL) + GREGORIAN; + now = (tv.tv_sec * 10000000ULL + tv.tv_usec * 10ULL + seq++ % 10) + GREGORIAN; return now; } @@ -274,11 +275,19 @@ static uint16_t random_clock_sequence(struct pd_uuid_state *s) { } static int read_state(struct pd_uuid_state *s) { + uint64_t node; + s->available = 0; s->node = 0LL; s->clock_sequence = 0; s->timestamp = 0LL; + node = current_node(s); + + if (!s->available || s->node != node) { + s->clock_sequence = random_clock_sequence(s); + } + return 0; } @@ -297,6 +306,30 @@ static unsigned long get_bytes(void *buf, unsigned long n, void *state) { return i; } +int pd_uuid_init(struct pd_uuid_state *s, int flags) { + if (!s) return 0; + + s->get_lock = obtain_global_lock; + s->release_lock = release_global_lock; + s->lock_data = 0; + + if (flags & 0x1) { + s->read_state = 0; + s->save_state = 0; + s->node = current_node(s); + } else { + s->read_state = read_state; + s->save_state = 0; + } + + s->random_bytes = get_bytes; + s->rng_state = 0; + + s->available = 0; + + return 1; +} + int pd_uuid_init_state(struct pd_uuid_state *s) { if (!s) return 0; @@ -318,7 +351,6 @@ int pd_uuid_init_state(struct pd_uuid_state *s) { int pd_uuid_make_v1(struct pd_uuid_state *s, pd_uuid_t *uuid) { struct pd_uuid_state ls; uint64_t now; - uint64_t node; int err; if (!s) { @@ -341,13 +373,6 @@ int pd_uuid_make_v1(struct pd_uuid_state *s, pd_uuid_t *uuid) { } now = current_time(); - node = current_node(s); - - if (!s->available || s->node != node) { - s->clock_sequence = random_clock_sequence(s); - } - - s->node = node; if (s->available && s->timestamp > now) { s->clock_sequence++; diff --git a/pduuid.h b/pduuid.h index 86c5f6d..14ad6da 100644 --- a/pduuid.h +++ b/pduuid.h @@ -24,6 +24,7 @@ struct pd_uuid { typedef struct pd_uuid pd_uuid_t; int pd_uuid_init_state(struct pd_uuid_state *s); +int pd_uuid_init(struct pd_uuid_state *s, int f); int pd_uuid_make_v1(struct pd_uuid_state *s, pd_uuid_t *uuid); int pd_uuid_make_v1mc(struct pd_uuid_state *s, pd_uuid_t *uuid); int pd_uuid_make_v4(struct pd_uuid_state *s, pd_uuid_t *uuid); diff --git a/t/sha1.c b/t/sha1.c index 4903d43..86ba412 100644 --- a/t/sha1.c +++ b/t/sha1.c @@ -18,6 +18,7 @@ static void bytestr(unsigned char *h, char *s, int len) { } } +#if 0 static int strbytes(char *s, unsigned char *d) { unsigned int byte; int i = 0; @@ -36,6 +37,7 @@ static int strbytes(char *s, unsigned char *d) { } return i; } +#endif int main(int ac, char *av[]) { unsigned char tmp[20]; @@ -44,7 +46,7 @@ int main(int ac, char *av[]) { char want[64]; char have[64]; - static const struct { + static struct { char *msg; int repeat; unsigned char hash[20]; @@ -67,7 +69,7 @@ int main(int ac, char *av[]) { { "0123456701234567012345670123456701234567012345670123456701234567", 10, {0xDE, 0xA3, 0x56, 0xA2, 0xCD, 0xDD, 0x90, 0xC7, 0xA7, 0xEC, 0xED, 0xC5, 0xEB, 0xB5, 0x63, 0x93, 0x4F, 0x46, 0x04, 0x52}, }, - { NULL, { 0 } } + { NULL, 0, { 0 } } }; plan(8); diff --git a/t/uuidgen.c b/t/uuidgen.c index 4ab3cf4..e17b7e9 100644 --- a/t/uuidgen.c +++ b/t/uuidgen.c @@ -63,6 +63,8 @@ int str_ok(pd_uuid_t *uuid, char *s, char *name) { int main(int ac, char *av[]) { pd_uuid_t uuid, copy; struct pd_uuid_state s; + pd_uuid_t idset[10]; + int i; char fmt[37]; unsigned char hash[40]; @@ -78,7 +80,7 @@ int main(int ac, char *av[]) { srandom(time(0)); #endif - plan(15); + plan(24); pd_uuid_init_state(0); pd_uuid_make_v1mc(0, &uuid); @@ -158,5 +160,14 @@ int main(int ac, char *av[]) { ufmt(©, fmt); diag("v1b: %s", fmt); + pd_uuid_init(&s, 0x1); + for (i=0;i<10;i++) { + pd_uuid_make_v1(&s, &idset[i]); + } + for (i=0;i<9;i++) { + ok(pd_uuid_cmp(&idset[i], &idset[i+1]), "id set %d != %d", i, i+1); + pd_uuid_make_v1(&s, &idset[i]); + } + return 0; } -- 2.40.0