/* * Calculate the checksum for a uuid. */ uint32_t checksum_uuid(uuid_t *source) { uint8_t buf[16]; uuid_enc_be(buf, source); return checksum_uint8_array(buf, 16); }
zuuid_t * zuuid_new (void) { zuuid_t *self = (zuuid_t *) zmalloc (sizeof (zuuid_t)); assert (self); #if defined (__WINDOWS__) // Windows always has UUID support UUID uuid; assert (sizeof (uuid) == ZUUID_LEN); UuidCreate (&uuid); zuuid_set (self, (byte *) &uuid); #elif defined (__UTYPE_ANDROID) || !defined (HAVE_UUID) // No UUID system calls, so generate a random string byte uuid [ZUUID_LEN]; int fd = open ("/dev/urandom", O_RDONLY); if (fd != -1) { ssize_t bytes_read = read (fd, uuid, ZUUID_LEN); assert (bytes_read == ZUUID_LEN); close (fd); zuuid_set (self, uuid); } else { // We couldn't read /dev/urandom and we have no alternative // strategy zsys_error (strerror (errno)); assert (false); } #elif defined (__UTYPE_OPENBSD) || defined (__UTYPE_FREEBSD) || defined (__UTYPE_NETBSD) uuid_t uuid; uint32_t status = 0; uuid_create (&uuid, &status); if (status != uuid_s_ok) { zuuid_destroy (&self); return NULL; } byte buffer [ZUUID_LEN]; uuid_enc_be (&buffer, &uuid); zuuid_set (self, buffer); #elif defined (__UTYPE_LINUX) || defined (__UTYPE_OSX) || defined (__UTYPE_SUNOS) || defined (__UTYPE_SUNSOLARIS) || defined (__UTYPE_GNU) uuid_t uuid; assert (sizeof (uuid) == ZUUID_LEN); uuid_generate (uuid); zuuid_set (self, (byte *) uuid); #else # error "Unknown UNIX TYPE" #endif return self; }