aboutsummaryrefslogtreecommitdiff
path: root/src/uuid.cpp
blob: c064d78c93b959e8dd82e2394ac0260e2e734404 (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
/*
 * Copyright (C) 2007-2008 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 <string.h>

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

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

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

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

Bu::FString Bu::Uuid::toString()
{
	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();
}

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

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

Bu::Uuid Bu::Uuid::gen()
{
	Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read );
	char dat[36];
	fIn.read( dat, 36 );
	int iNibble = 0;
	Uuid id;
	memset( id.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);
		id.data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c);
		iNibble++;
	}

	return id;
}