aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/uuid.cpp
blob: 6642d753f1633cdf19e2f55952590e3993ef7d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (C) 2007-2019 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/uuid.h"
#include "bu/file.h"
#include "bu/formatter.h"
#include "bu/membuf.h"
#include "bu/random.h"
#include <string.h>

#ifdef WIN32
#include <rpc.h>
#include <Rpcdce.h>
#endif

Bu::Uuid::Uuid()
{
    clear();
}

Bu::Uuid::Uuid( const Uuid &src )
{
    memcpy( data, src.data, 16 );
}

Bu::Uuid::Uuid( const Bu::String &sSrc )
{
    if( sSrc.getSize() == 16 )
    {
        memcpy( data, sSrc.getStr(), 16 );
    }
    else if( sSrc.getSize() == 36 )
    {
        // Parse it
        set( sSrc );
    }
}

Bu::Uuid::~Uuid()
{
}

Bu::String Bu::Uuid::toRawString() const
{
    return Bu::String( (char *)data, 16 );
}

Bu::String Bu::Uuid::toString() const
{
    Bu::MemBuf mb;
    Bu::Formatter f( mb );

    for( int j = 0; j < 16; j++ )
    {
        if( j == 4 || j == 6 || j == 8 || j == 10 )
            f << '-';
        f << Bu::Fmt::hex(2).caps(false) << (unsigned int)data[j];
    }

    return mb.getString();
}

Bu::String Bu::Uuid::toUrn() const
{
    return "urn:uuid:" + toString();
}

int Bu::Uuid::getVersion()
{
    return (data[6]&((8|4|2|1)<<4))>>4;
}

#define msb( i ) (1<<(7-i))

void Bu::Uuid::clear()
{
    memset( data, 0, 16 );
    data[7] = msb(0);
}

Bu::Uuid Bu::Uuid::generate( Bu::Uuid::Type eType )
{
    Uuid id;
    switch( eType )
    {
        case Version1:
        case Version2:
        case Version3:
        case Version4:
        case Version5:
            // Yeah... all of these are just random for now, a lot more
            // platform specific code is required for a lot of these.
            for( int j = 0; j < 16; j++ )
            {
               id.data[j] = Bu::Random::rand(256);
            }
            break;

        default:
        case System:
#if defined(linux)
            Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read );
            char dat[37];
            memset( dat, 0, 37 );
            fIn.read( dat, 36 );
            id.set( dat );
#elif defined(WIN32)
            UUID uuid;
            UuidCreate( &uuid );
            id.data[0] = ((unsigned char *)&uuid.Data1)[3];
            id.data[1] = ((unsigned char *)&uuid.Data1)[2];
            id.data[2] = ((unsigned char *)&uuid.Data1)[1];
            id.data[3] = ((unsigned char *)&uuid.Data1)[0];
            id.data[4] = ((unsigned char *)&uuid.Data2)[1];
            id.data[5] = ((unsigned char *)&uuid.Data2)[0];
            id.data[6] = ((unsigned char *)&uuid.Data3)[1];
            id.data[7] = ((unsigned char *)&uuid.Data3)[0];
            memcpy( id.data+8, uuid.Data4, 8 );

#else
#warning No platform specific UUID, falling back to random for now
            return generate( Version4 );
#endif
    }
    return id;
}

void Bu::Uuid::set( const Bu::String &sSrc )
{
    const char *dat = sSrc.getStr();
    int iNibble = 0;
    memset( data, 0, 16 );
    for( int j = 0; j < 36; j++ )
    {
        if( dat[j] == '-' )
            continue;
        unsigned char c = (dat[j]>='0'&&dat[j]<='9')?(dat[j]-'0'):(dat[j]-'a'+10);
        data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c);
        iNibble++;
    }
}

bool Bu::Uuid::operator==( const Uuid &rhs ) const
{
    return memcmp( data, rhs.data, 16 ) == 0;
}

bool Bu::Uuid::operator!=( const Uuid &rhs ) const
{
    return !(*this == rhs);
}

Bu::Uuid &Bu::Uuid::operator=( const Uuid &rhs )
{
    memcpy( data, rhs.data, 16 );
    return *this;
}

template<> uint32_t Bu::__calcHashCode<Bu::Uuid>( const Bu::Uuid &k )
{
    return __calcHashCode<String>( k.toRawString() );
}

template<> bool Bu::__cmpHashKeys<Bu::Uuid>( const Bu::Uuid &a, const Bu::Uuid &b )
{
    return a == b;
}

Bu::Archive &Bu::operator>>( Bu::Archive &ar, Bu::Uuid &u )
{
    ar.read( u.data, 16 );
    return ar;
}

Bu::Archive &Bu::operator<<( Bu::Archive &ar, const Bu::Uuid &u )
{
    ar.write( u.data, 16 );
    return ar;
}

Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Uuid &u )
{
    return f << u.toString();
}