From 4ed70a930202b103e7e80b8dc925e0aaa4622595 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Wed, 29 Jul 2009 12:07:54 +0200 Subject: initial commit --- src/uuid.cpp | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 src/uuid.cpp (limited to 'src/uuid.cpp') diff --git a/src/uuid.cpp b/src/uuid.cpp new file mode 100644 index 0000000..d9883cd --- /dev/null +++ b/src/uuid.cpp @@ -0,0 +1,136 @@ +/* + Copyright (c) 2007-2009 FastMQ Inc. + + This file is part of 0MQ. + + 0MQ is free software; you can redistribute it and/or modify it under + the terms of the Lesser GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + 0MQ 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 + Lesser GNU General Public License for more details. + + You should have received a copy of the Lesser GNU General Public License + along with this program. If not, see . +*/ + +#include "platform.hpp" +#include "uuid.hpp" +#include "err.hpp" + +#if defined ZS_HAVE_WINDOWS + +#include + +zs::uuid_t::uuid_t () +{ + RPC_STATUS ret = UuidCreate (&uuid); + zs_assert (ret == RPC_S_OK); + ret = UuidToString (&uuid, &uuid_str); + zs_assert (ret == RPC_S_OK); +} + +zs::uuid_t::~uuid_t () +{ + RPC_STATUS ret = RpcStringFree(&uuid_str); + assert (ret == RPC_S_OK); +} + +const char *zs::uuid_t::to_string () +{ + return uuid_str; +} + +#elif defined ZS_HAVE_FREEBSD + +#include +#include + +zs::uuid_t::uuid_t () +{ + uint32_t status; + uuid_create (&uuid, &status); + zs_assert (status == uuid_s_ok); + uuid_to_string (&uuid, &uuid_str, &status); + zs_assert (status == uuid_s_ok); +} + +zs::uuid_t::~uuid_t () +{ + free (uuid_str); +} + +const char *zs::uuid_t::to_string () +{ + return uuid_str; +} + +#elif defined ZS_HAVE_LINUX || defined ZS_HAVE_SOLARIS || defined ZS_HAVE_OSX + +#include + +zs::uuid_t::uuid_t () +{ + uuid_generate (uuid); + uuid_unparse (uuid, uuid_buf); +} + +zs::uuid_t::~uuid_t () +{ +} + +const char *zs::uuid_t::to_string () +{ + return uuid_buf; +} + +#else + +#include +#include +#include + +zs::uuid_t::uuid_t () +{ + unsigned char rand_buf [16]; + int ret = RAND_bytes (rand_buf, sizeof rand_buf); + zs_assert (ret == 1); + + // Read in UUID fields. + memcpy (&time_low, rand_buf, sizeof time_low); + memcpy (&time_mid, rand_buf + 4, sizeof time_mid); + memcpy (&time_hi_and_version, rand_buf + 6, sizeof time_hi_and_version); + memcpy (&clock_seq_hi_and_reserved, rand_buf + 8, + sizeof clock_seq_hi_and_reserved); + memcpy (&clock_seq_low, rand_buf + 9, sizeof clock_seq_low); + memcpy (&node [0], rand_buf + 10, sizeof node); + + // Store UUID version number. + time_hi_and_version = (time_hi_and_version & 0x0fff) | 4 << 12; + + // Store UUID type. + clock_seq_hi_and_reserved = (clock_seq_hi_and_reserved & 0x3f) | 0x80; + + snprintf (uuid_buf, sizeof uuid_buf, + "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", + time_low, + time_mid, + time_hi_and_version, + clock_seq_hi_and_reserved, + clock_seq_low, + node [0], node [1], node [2], node [3], node [4], node [5]); +} + +zs::uuid_t::~uuid_t () +{ +} + +const char *zs::uuid_t::to_string () +{ + return uuid_buf; +} + +#endif -- cgit v1.2.3