diff --git a/Makefile.am b/Makefile.am index 379d36803..2edbc6e28 100644 --- a/Makefile.am +++ b/Makefile.am @@ -38,6 +38,9 @@ include $(top_srcdir)/src/common/Makefile.am include $(top_srcdir)/src/lib/Makefile.am include $(top_srcdir)/src/backend/Makefile.am +# tools +include $(top_srcdir)/src/tool/Makefile.am + # gui include $(top_srcdir)/src/gui/Makefile.am diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index a4d8b825f..2ae8a9b2d 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -28,11 +28,11 @@ 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)/luid.c \ $(liblumi_a_srcdir)/safeclib.c \ $(liblumi_a_srcdir)/cuckoo.c \ $(liblumi_a_srcdir)/mrucache.c \ - $(liblumi_a_srcdir)/time.c \ + $(liblumi_a_srcdir)/time.c \ $(liblumi_a_srcdir)/appconfig.cpp noinst_HEADERS += \ @@ -43,11 +43,11 @@ 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)/luid.h \ $(liblumi_a_srcdir)/safeclib.h \ $(liblumi_a_srcdir)/cuckoo.h \ $(liblumi_a_srcdir)/mrucache.h \ - $(liblumi_a_srcdir)/time.h \ + $(liblumi_a_srcdir)/time.h \ $(liblumi_a_srcdir)/appconfig.hpp \ $(liblumi_a_srcdir)/appconfig.hpp diff --git a/src/lib/luid.c b/src/lib/luid.c new file mode 100644 index 000000000..c8d766094 --- /dev/null +++ b/src/lib/luid.c @@ -0,0 +1,113 @@ +/* + luid - Lumiera unique identifiers + + Copyright (C) Lumiera.org + 2008, Christian Thaeter + + 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 "lib/luid.h" + +#include +#include +#include +#include +#include +#include +#include + +void +lumiera_uid_set_ptr (lumiera_uid* luid, void* ptr) +{ + memset (luid, 0, 16); + *(void**)luid = ptr; +} + + +void* +lumiera_uid_ptr_get (lumiera_uid* luid) +{ + return *(void**)luid; +} + + +void +lumiera_uid_gen (lumiera_uid* luid) +{ + static int fd = -2; + if (!luid) + return; + + if (fd == -2) + { + fd = open ("/dev/urandom", O_RDONLY); + /* on linux /dev/random would be way to slow for our purpose, so we comment that out for now. + other unixiods offer a /dev/random which has the same semantics as linux /dev/urandom has, + configuration should do this right some day. + if (fd == -1) + fd = open ("/dev/random", O_RDONLY); + */ + if (fd >= 0) + fcntl (fd, F_SETFD, FD_CLOEXEC); + else + srand (getpid () + time (NULL)); + } + + do + { + if (fd < 0) + { + for (int i = 0; i < 16; ++i) + ((unsigned char*)luid)[i] = (unsigned char)(rand()>>7); + } + else + { + if (read (fd, luid, 16) < 16) + abort (); + } + } + /* we identify generic pointers by having some zeros in the luid, + * this happens very unlikely to be in a random luid, just regenerate it then */ + while (!*(((intptr_t*)luid)+1)); +} + + +void +lumiera_uid_copy (lumiera_uid* dest, lumiera_uid* src) +{ + memcpy (dest, src, 16); +} + + +int +lumiera_uid_eq (lumiera_uid* luida, lumiera_uid* luidb) +{ + return !memcmp (luida, luidb, 16); +} + +size_t +lumiera_uid_hash (lumiera_uid* luid) +{ + return *(size_t*)luid; +} + +/* +// Local Variables: +// mode: C +// c-file-style: "gnu" +// indent-tabs-mode: nil +// End: +*/ diff --git a/src/lib/luid.h b/src/lib/luid.h new file mode 100644 index 000000000..a93d004a3 --- /dev/null +++ b/src/lib/luid.h @@ -0,0 +1,81 @@ +/* + luid - Lumiera unique identifiers + + Copyright (C) Lumiera.org + 2008, Christian Thaeter + + 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_LUID_H +#define LUMIERA_LUID_H + +#include + +/** + * @file + * Lumiera unique identifiers are 128 byte random value. Unlike standard uuid's we + * don't tag a version within them and we may store generic pointers in the space + * occupied by an luid. + */ + +typedef unsigned char lumiera_uid[16]; + +/** + * Retrieve a generic pointer stored in a luid + */ +void* +lumiera_uid_ptr_get (lumiera_uid* luid); + +/** + * Generate a new luid + */ +void +lumiera_uid_gen (lumiera_uid* luid); + +/** + * Store a generic pointer in a luid + */ +void +lumiera_uid_set_ptr (lumiera_uid* luid, void* ptr); + + +/** + * Copy an luid + */ +void +lumiera_uid_copy (lumiera_uid* dest, lumiera_uid* src); + + +/** + * Test 2 luid's for equality + */ +int +lumiera_uid_eq (lumiera_uid* luida, lumiera_uid* luidb); + + +/** + * Generate a hashsum over an luid + */ +size_t +lumiera_uid_hash (lumiera_uid* luid); + +#endif +/* +// Local Variables: +// mode: C +// c-file-style: "gnu" +// indent-tabs-mode: nil +// End: +*/ diff --git a/src/lib/uuid.c b/src/lib/uuid.c deleted file mode 100644 index d4eee69be..000000000 --- a/src/lib/uuid.c +++ /dev/null @@ -1,95 +0,0 @@ -/* - uuid - Universal unique identifiers - - Copyright (C) CinelerraCV - 2008, Christian Thaeter - - 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 "lib/uuid.h" - -#include -#include -#include -#include -#include -#include - -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; -} - - diff --git a/src/lib/uuid.h b/src/lib/uuid.h deleted file mode 100644 index d4cf8ce9d..000000000 --- a/src/lib/uuid.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - uuid - Universal unique identifiers - - Copyright (C) CinelerraCV - 2008, Christian Thaeter - - 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 - -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 diff --git a/src/tool/Makefile.am b/src/tool/Makefile.am new file mode 100644 index 000000000..69c4a7f25 --- /dev/null +++ b/src/tool/Makefile.am @@ -0,0 +1,28 @@ +# Copyright (C) Lumiera.org +# 2008 Christian Thaeter +# +# 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. + +lumitool_srcdir = $(top_srcdir)/src/tool + +noinst_PROGRAMS += luidgen + +luidgen_CFLAGS = $(CFLAGS) -std=gnu99 -Wall -Werror +luidgen_CPPFLAGS = -I$(top_srcdir)/src/ +luidgen_SOURCES = \ + $(lumitool_srcdir)/luidgen.c + +luidgen_LDADD = liblumi.a + diff --git a/src/tool/luidgen.c b/src/tool/luidgen.c new file mode 100644 index 000000000..41081ca97 --- /dev/null +++ b/src/tool/luidgen.c @@ -0,0 +1,51 @@ +/* + luidgen.c - generate a lumiera uuid + + Copyright (C) Lumiera.org + 2008 Christian Thaeter + + 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 "lib/luid.h" + +#include + +/** + * @file + * Generate amd print a Lumiera uid as octal escaped string + */ + +int +main (int argc, char** argv) +{ + lumiera_uid luid; + lumiera_uid_gen (&luid); + + printf ("\""); + for (int i = 0; i < 16; ++i) + printf ("\\%.3hho", *(((char*)&luid)+i)); + printf ("\"\n"); + + return 0; +} + +/* +// Local Variables: +// mode: C +// c-file-style: "gnu" +// indent-tabs-mode: nil +// End: +*/ diff --git a/tests/15uuid.tests b/tests/15luid.tests similarity index 100% rename from tests/15uuid.tests rename to tests/15luid.tests diff --git a/tests/Makefile.am b/tests/Makefile.am index c51133995..74d035034 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -39,12 +39,12 @@ test_llist_LDADD = liblumi.a -lnobugmt -lpthread -ldl -lm check_PROGRAMS += test-safeclib test_safeclib_SOURCES = $(tests_srcdir)/library/test-safeclib.c test_safeclib_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/ -test_safeclib_LDADD = $(builddir)/liblumi.a -lnobugmt -lpthread -ldl -lm +test_safeclib_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-luid +test_luid_SOURCES = $(tests_srcdir)/library/test-luid.c +test_luid_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/ +test_luid_LDADD = liblumi.a -lnobugmt -lpthread -ldl -lm check_PROGRAMS += test-references test_references_SOURCES = $(tests_srcdir)/library/test-references.c diff --git a/tests/library/test-uuid.c b/tests/library/test-luid.c similarity index 60% rename from tests/library/test-uuid.c rename to tests/library/test-luid.c index 4a6050163..1695bbec6 100644 --- a/tests/library/test-uuid.c +++ b/tests/library/test-luid.c @@ -1,8 +1,8 @@ /* - test-uuid.c - test the uuid lib + test-luid.c - test the luid lib - Copyright (C) CinelerraCV - 2007, Christian Thaeter + Copyright (C) Lumiera.org + 2007, 2008 Christian Thaeter This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -23,7 +23,7 @@ //#include -#include "lib/uuid.h" +#include "lib/luid.h" #include @@ -39,35 +39,43 @@ main (int argc, char** argv) if (!strcmp(argv[1], "uuidgen_2")) { - lumiera_uuid uuid1; - lumiera_uuid uuid2; + lumiera_uid luid1; + lumiera_uid luid2; - lumiera_uuid_gen (&uuid1); - lumiera_uuid_gen (&uuid2); + lumiera_uid_gen (&luid1); + lumiera_uid_gen (&luid2); - printf ("%d\n", lumiera_uuid_eq (&uuid2, &uuid1)); + printf ("%d\n", lumiera_uid_eq (&luid2, &luid1)); } else if (!strcmp(argv[1], "uuidgen_copy")) { - lumiera_uuid uuid1; - lumiera_uuid uuid2; + lumiera_uid luid1; + lumiera_uid luid2; - lumiera_uuid_gen (&uuid1); + lumiera_uid_gen (&luid1); - lumiera_uuid_copy (&uuid2, &uuid1); + lumiera_uid_copy (&luid2, &luid1); - printf ("%d\n", lumiera_uuid_eq (&uuid2, &uuid1)); + printf ("%d\n", lumiera_uid_eq (&luid2, &luid1)); } else if (!strcmp(argv[1], "ptrs")) { - lumiera_uuid uuid; + lumiera_uid luid; - lumiera_uuid_set_ptr (&uuid, &uuid); + lumiera_uid_set_ptr (&luid, &luid); - printf ("%d\n", lumiera_uuid_ptr_get (&uuid) == &uuid); + printf ("%d\n", lumiera_uid_ptr_get (&luid) == &luid); } else return 1; return 0; } + +/* +// Local Variables: +// mode: C +// c-file-style: "gnu" +// indent-tabs-mode: nil +// End: +*/