]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/uuid.c
ITS#6419
[openldap] / libraries / liblutil / uuid.c
index 579b03a1331f7ee656ac70558002c6d399d80833..a466090d9d37e30de1b3f2ed83372eb278ae4426 100644 (file)
@@ -1,14 +1,29 @@
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* uuid.c -- Universally Unique Identifier routines */
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2000-2009 The OpenLDAP Foundation.
+ * Portions Copyright 2000-2003 Kurt D. Zeilenga.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-/* Portions
- * Copyright 2000, John E. Schimmel, All rights reserved.
+/* Portions Copyright 2000, John E. Schimmel, All rights reserved.
  * This software is not subject to any license of Mirapoint, Inc.
  *
  * This is free software; you can redistribute and use it
  * under the same terms as OpenLDAP itself.
  */
+/* This work was initially developed by John E. Schimmel and adapted
+ * for inclusion in OpenLDAP Software by Kurt D. Zeilenga.
+ */
+
 /*
  * Sorry this file is so scary, but it needs to run on a wide range of
  * platforms.  The only exported routine is lutil_uuidstr() which is all
@@ -26,6 +41,8 @@
 
 #ifdef HAVE_UUID_TO_STR
 #  include <sys/uuid.h>
+#elif defined( HAVE_UUID_GENERATE )
+#  include <uuid/uuid.h>
 #elif defined( _WIN32 )
 #  include <rpc.h>
 #else
@@ -41,7 +58,7 @@
 #include <lutil.h>
 
 /* not needed for Windows */
-#if !defined(HAVE_UUID_TO_STR) && !defined(_WIN32)
+#if !defined(HAVE_UUID_TO_STR) && !defined(HAVE_UUID_GENERATE) && !defined(_WIN32)
 static unsigned char *
 lutil_eaddr( void )
 {
@@ -154,7 +171,7 @@ lutil_eaddr( void )
        if (memcmp(eaddr, zero, sizeof(eaddr)) == 0) {
                /* XXX - who knows? */
                lutil_entropy( eaddr, sizeof(eaddr) );
-               eaddr[0] |= 0x80; /* turn it into a multicast address */
+               eaddr[0] |= 0x01; /* turn it into a multicast address */
        }
 
        return eaddr;
@@ -236,7 +253,7 @@ mul64ll(unsigned long i1, unsigned long i2)
 
 #endif /* ULONG_MAX >= 64 bits || HAVE_LONG_LONG */
 
-#endif /* !HAVE_UUID_TO_STR && !_WIN32 */
+#endif /* !HAVE_UUID_TO_STR && !HAVE_UUID_GENERATE && !_WIN32 */
 
 /*
 ** All we really care about is an ISO UUID string.  The format of a UUID is:
@@ -283,6 +300,13 @@ lutil_uuidstr( char *buf, size_t len )
 
        return l;
 
+#elif defined( HAVE_UUID_GENERATE )
+       uuid_t uu;
+
+       uuid_generate( uu );
+       uuid_unparse_lower( uu, buf );
+       return strlen( buf );
+       
 #elif defined( _WIN32 )
        UUID uuid;
        unsigned char *uuidstr;
@@ -356,6 +380,47 @@ lutil_uuidstr( char *buf, size_t len )
 #endif
 }
 
+int
+lutil_uuidstr_from_normalized(
+       char            *uuid,
+       size_t          uuidlen,
+       char            *buf,
+       size_t          buflen )
+{
+       unsigned char nibble;
+       int i, d = 0;
+
+       assert( uuid != NULL );
+       assert( buf != NULL );
+
+       if ( uuidlen != 16 ) return -1;
+       if ( buflen < 36 ) return -1;
+
+       for ( i = 0; i < 16; i++ ) {
+               if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
+                       buf[(i<<1)+d] = '-';
+                       d += 1;
+               }
+
+               nibble = (uuid[i] >> 4) & 0xF;
+               if ( nibble < 10 ) {
+                       buf[(i<<1)+d] = nibble + '0';
+               } else {
+                       buf[(i<<1)+d] = nibble - 10 + 'a';
+               }
+
+               nibble = (uuid[i]) & 0xF;
+               if ( nibble < 10 ) {
+                       buf[(i<<1)+d+1] = nibble + '0';
+               } else {
+                       buf[(i<<1)+d+1] = nibble - 10 + 'a';
+               }
+       }
+
+       if ( buflen > 36 ) buf[36] = '\0';
+       return 36;
+}
+
 #ifdef TEST
 int
 main(int argc, char **argv)