uuid.hpp 3.07 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2 3
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
4 5 6 7

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
8
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
9 10 11 12 13 14
    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
15
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
16

17
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
18 19 20
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

Martin Sustrik's avatar
Martin Sustrik committed
21 22
#ifndef __ZMQ_UUID_HPP_INCLUDED__
#define __ZMQ_UUID_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
23 24

#include "platform.hpp"
unknown's avatar
unknown committed
25
#include "stdint.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
26

Martin Lucina's avatar
Martin Lucina committed
27
#if defined ZMQ_HAVE_FREEBSD || defined ZMQ_HAVE_NETBSD
Martin Sustrik's avatar
Martin Sustrik committed
28
#include <uuid.h>
Martin Lucina's avatar
Martin Lucina committed
29 30
#elif defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_SOLARIS ||\
      defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_CYGWIN
Martin Sustrik's avatar
Martin Sustrik committed
31
#include <uuid/uuid.h>
malosek's avatar
malosek committed
32
#elif defined ZMQ_HAVE_WINDOWS
33
#include "windows.hpp"
Martin Lucina's avatar
Martin Lucina committed
34
#include <rpc.h>
Brett Cameron's avatar
Brett Cameron committed
35 36 37 38 39 40 41 42
#elif defined ZMQ_HAVE_OPENVMS
typedef struct
{
    unsigned long data0;
    unsigned short data1;
    unsigned short data2;
    unsigned char data3 [8];
} uuid_t;
Martin Sustrik's avatar
Martin Sustrik committed
43 44
#endif

Martin Sustrik's avatar
Martin Sustrik committed
45
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
46 47 48 49 50 51 52 53 54 55 56 57
{

    //  This class provides RFC 4122 (a Universally Unique IDentifier)
    //  implementation.

    class uuid_t
    {
    public:

        uuid_t ();
        ~uuid_t ();

58 59 60
        //  The length of textual representation of UUID.
        enum { uuid_string_len = 36 };

Martin Sustrik's avatar
Martin Sustrik committed
61
        //  Returns a pointer to buffer containing the textual
62
        //  representation of the UUID. The callee is reponsible to
Martin Sustrik's avatar
Martin Sustrik committed
63 64 65
        //  free the allocated memory.
        const char *to_string ();

66 67 68 69 70
        //  The length of binary representation of UUID.
        enum { uuid_blob_len = 16 };

        const unsigned char *to_blob ();

Martin Sustrik's avatar
Martin Sustrik committed
71 72
    private:

73 74 75 76 77 78 79
        //  Converts one byte from hexa representation to binary.
        unsigned char convert_byte (const char *hexa_);

        //  Converts string representation of UUID into standardised BLOB.
        //  The function is endianness agnostic.
        void create_blob ();

Martin Sustrik's avatar
Martin Sustrik committed
80
#if defined ZMQ_HAVE_WINDOWS
malosek's avatar
malosek committed
81 82 83 84
#ifdef ZMQ_HAVE_MINGW32
        typedef unsigned char* RPC_CSTR;
#endif
        ::UUID uuid;
85
        RPC_CSTR string_buf;
Martin Lucina's avatar
Martin Lucina committed
86
#elif defined ZMQ_HAVE_FREEBSD || defined ZMQ_HAVE_NETBSD
Martin Sustrik's avatar
Martin Sustrik committed
87
        ::uuid_t uuid;
88
        char *string_buf;
Martin Lucina's avatar
Martin Lucina committed
89
#elif defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_SOLARIS ||\
Brett Cameron's avatar
Brett Cameron committed
90 91
      defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_CYGWIN ||\
      defined ZMQ_HAVE_OPENVMS
Martin Sustrik's avatar
Martin Sustrik committed
92
        ::uuid_t uuid;
93
        char string_buf [uuid_string_len + 1];
Martin Sustrik's avatar
Martin Sustrik committed
94 95 96 97 98 99 100 101 102
#else
        //  RFC 4122 UUID's fields
        uint32_t time_low;
        uint16_t time_mid;
        uint16_t time_hi_and_version;
        uint8_t clock_seq_hi_and_reserved;
        uint8_t clock_seq_low;
        uint8_t node [6];

103
        char string_buf [uuid_string_len + 1];
Martin Sustrik's avatar
Martin Sustrik committed
104
#endif
105 106

        unsigned char blob_buf [uuid_blob_len];
Martin Sustrik's avatar
Martin Sustrik committed
107 108 109 110 111
    };

}

#endif