X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblutil%2Fuuid.c;h=16fa17e4f2a9cd34360c8b4f84c67a90551ebaf6;hb=2eda46998dd48e0b62a3479f3f8a88ab2db54c64;hp=e2bbe74ad6410577e49bc94e2e9e5e956d5c1f3b;hpb=3c5068bc1fa84fc5daf1e50d4f1a929cec91b7e9;p=openldap diff --git a/libraries/liblutil/uuid.c b/libraries/liblutil/uuid.c index e2bbe74ad6..16fa17e4f2 100644 --- a/libraries/liblutil/uuid.c +++ b/libraries/liblutil/uuid.c @@ -2,7 +2,7 @@ /* $OpenLDAP$ */ /* This work is part of OpenLDAP Software . * - * Copyright 2000-2007 The OpenLDAP Foundation. + * Copyright 2000-2012 The OpenLDAP Foundation. * Portions Copyright 2000-2003 Kurt D. Zeilenga. * All rights reserved. * @@ -41,6 +41,8 @@ #ifdef HAVE_UUID_TO_STR # include +#elif defined( HAVE_UUID_GENERATE ) +# include #elif defined( _WIN32 ) # include #else @@ -56,7 +58,7 @@ #include /* 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 ) { @@ -251,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: @@ -298,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; @@ -371,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)