uuid functions
This commit is contained in:
parent
27ca8a7362
commit
0b8b5bf507
6 changed files with 253 additions and 0 deletions
|
|
@ -29,6 +29,7 @@ liblumi_a_SOURCES = \
|
|||
$(liblumi_a_srcdir)/rwlock.c \
|
||||
$(liblumi_a_srcdir)/condition.c \
|
||||
$(liblumi_a_srcdir)/references.c \
|
||||
$(liblumi_a_srcdir)/uuid.c \
|
||||
$(liblumi_a_srcdir)/safeclib.c
|
||||
|
||||
|
||||
|
|
@ -42,5 +43,6 @@ noinst_HEADERS += \
|
|||
$(liblumi_a_srcdir)/rwlock.h \
|
||||
$(liblumi_a_srcdir)/condition.h \
|
||||
$(liblumi_a_srcdir)/references.h \
|
||||
$(liblumi_a_srcdir)/uuid.h \
|
||||
$(liblumi_a_srcdir)/safeclib.h
|
||||
|
||||
|
|
|
|||
94
src/lib/uuid.c
Normal file
94
src/lib/uuid.c
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
uuid - Universal unique identifiers
|
||||
|
||||
Copyright (C) CinelerraCV
|
||||
2008, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include "uuid.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void
|
||||
lumiera_uuid_set_ptr (lumiera_uuid* uuid, void* ptr)
|
||||
{
|
||||
memset (uuid, 0, 16);
|
||||
*(void**)uuid = ptr;
|
||||
}
|
||||
|
||||
|
||||
void*
|
||||
lumiera_uuid_ptr_get (lumiera_uuid* uuid)
|
||||
{
|
||||
return *(void**)uuid;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
lumiera_uuid_gen (lumiera_uuid* uuid)
|
||||
{
|
||||
static int fd = -2;
|
||||
if (!uuid)
|
||||
return;
|
||||
|
||||
if (fd == -2)
|
||||
{
|
||||
fd = open ("/dev/urandom", O_RDONLY);
|
||||
if (fd == -1)
|
||||
fd = open ("/dev/random", O_RDONLY);
|
||||
if (fd >= 0)
|
||||
fcntl (fd, F_SETFD, FD_CLOEXEC);
|
||||
else
|
||||
srand (getpid () + time (NULL));
|
||||
}
|
||||
if (fd < 0)
|
||||
{
|
||||
for (int i = 0; i < 16; ++i)
|
||||
((unsigned char*)uuid)[i] = (unsigned char)(rand()>>7);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (read (fd, uuid, 16) < 0)
|
||||
abort ();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
lumiera_uuid_copy (lumiera_uuid* dest, lumiera_uuid* src)
|
||||
{
|
||||
memcpy (dest, src, 16);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lumiera_uuid_eq (lumiera_uuid* uuida, lumiera_uuid* uuidb)
|
||||
{
|
||||
return !memcmp (uuida, uuidb, 16);
|
||||
}
|
||||
|
||||
size_t
|
||||
lumiera_uuid_hash (lumiera_uuid* uuid)
|
||||
{
|
||||
return *(size_t*)uuid;
|
||||
}
|
||||
|
||||
|
||||
66
src/lib/uuid.h
Normal file
66
src/lib/uuid.h
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
uuid - Universal unique identifiers
|
||||
|
||||
Copyright (C) CinelerraCV
|
||||
2008, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
#ifndef LUMIERA_UUID_H
|
||||
#define LUMIERA_UUID_H
|
||||
#include "lumiera.h"
|
||||
|
||||
typedef unsigned char lumiera_uuid[16];
|
||||
|
||||
/**
|
||||
* Retrieve a generic pointer stored in a uuid
|
||||
*/
|
||||
void*
|
||||
lumiera_uuid_ptr_get (lumiera_uuid* uuid);
|
||||
|
||||
/**
|
||||
* Generate a new uuid
|
||||
*/
|
||||
void
|
||||
lumiera_uuid_gen (lumiera_uuid* uuid);
|
||||
|
||||
/**
|
||||
* Store a generic pointer in a uuid
|
||||
*/
|
||||
void
|
||||
lumiera_uuid_set_ptr (lumiera_uuid* uuid, void* ptr);
|
||||
|
||||
|
||||
/**
|
||||
* Copy an uuid
|
||||
*/
|
||||
void
|
||||
lumiera_uuid_copy (lumiera_uuid* dest, lumiera_uuid* src);
|
||||
|
||||
|
||||
/**
|
||||
* Test 2 uuid's for equality
|
||||
*/
|
||||
int
|
||||
lumiera_uuid_eq (lumiera_uuid* uuida, lumiera_uuid* uuidb);
|
||||
|
||||
|
||||
/**
|
||||
* Generate a hashsum over an uuid
|
||||
*/
|
||||
size_t
|
||||
lumiera_uuid_hash (lumiera_uuid* uuid);
|
||||
|
||||
#endif
|
||||
15
tests/15uuid.tests
Normal file
15
tests/15uuid.tests
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
TESTING "UUID's" ./test-uuid
|
||||
|
||||
TEST "uuid generate" uuidgen_2 <<END
|
||||
out: 0
|
||||
END
|
||||
|
||||
TEST "uuid copy" uuidgen_copy <<END
|
||||
out: 1
|
||||
END
|
||||
|
||||
TEST "storing pointers" ptrs <<END
|
||||
out: 1
|
||||
END
|
||||
|
||||
|
||||
|
|
@ -36,6 +36,11 @@ test_llist_SOURCES = $(tests_srcdir)/library/test-llist.c
|
|||
test_llist_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
|
||||
test_llist_LDADD = liblumi.a -lnobugmt -lpthread -ldl -lm
|
||||
|
||||
check_PROGRAMS += test-uuid
|
||||
test_uuid_SOURCES = $(tests_srcdir)/library/test-uuid.c
|
||||
test_uuid_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
|
||||
test_uuid_LDADD = $(builddir)/liblumi.a -lnobugmt -lpthread -ldl -lm
|
||||
|
||||
check_PROGRAMS += test-references
|
||||
test_references_SOURCES = $(tests_srcdir)/library/test-references.c
|
||||
test_references_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
|
||||
|
|
|
|||
71
tests/library/test-uuid.c
Normal file
71
tests/library/test-uuid.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
test-uuid.c - test the uuid lib
|
||||
|
||||
Copyright (C) CinelerraCV
|
||||
2007, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
//#include "cinelerra-config.h"
|
||||
|
||||
//#include <stdio.h>
|
||||
//#include <string.h>
|
||||
|
||||
#include "lib/uuid.h"
|
||||
|
||||
//CINELERRA_ERROR_DEFINE(TEST, "test error");
|
||||
|
||||
int
|
||||
main (int argc, char** argv)
|
||||
{
|
||||
NOBUG_INIT;
|
||||
|
||||
if (argc == 1)
|
||||
return 0;
|
||||
|
||||
if (!strcmp(argv[1], "uuidgen_2"))
|
||||
{
|
||||
lumiera_uuid uuid1;
|
||||
lumiera_uuid uuid2;
|
||||
|
||||
lumiera_uuid_gen (&uuid1);
|
||||
lumiera_uuid_gen (&uuid2);
|
||||
|
||||
printf ("%d\n", lumiera_uuid_eq (&uuid2, &uuid1));
|
||||
}
|
||||
else if (!strcmp(argv[1], "uuidgen_copy"))
|
||||
{
|
||||
lumiera_uuid uuid1;
|
||||
lumiera_uuid uuid2;
|
||||
|
||||
lumiera_uuid_gen (&uuid1);
|
||||
|
||||
lumiera_uuid_copy (&uuid2, &uuid1);
|
||||
|
||||
printf ("%d\n", lumiera_uuid_eq (&uuid2, &uuid1));
|
||||
}
|
||||
else if (!strcmp(argv[1], "ptrs"))
|
||||
{
|
||||
lumiera_uuid uuid;
|
||||
|
||||
lumiera_uuid_set_ptr (&uuid, &uuid);
|
||||
|
||||
printf ("%d\n", lumiera_uuid_ptr_get (&uuid) == &uuid);
|
||||
}
|
||||
else
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue