]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/uuid/include/boost/uuid/name_generator.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / uuid / include / boost / uuid / name_generator.hpp
1 // Boost name_generator.hpp header file ----------------------------------------------//
2
3 // Copyright 2010 Andy Tompkins.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifndef BOOST_UUID_NAME_GENERATOR_HPP
9 #define BOOST_UUID_NAME_GENERATOR_HPP
10
11 #include <boost/uuid/uuid.hpp>
12 #include <boost/uuid/sha1.hpp>
13 #include <boost/assert.hpp>
14 #include <string>
15 #include <cstring> // for strlen, wcslen
16
17 #ifdef BOOST_NO_STDC_NAMESPACE
18 namespace std {
19 using ::strlen;
20 using ::wcslen;
21 } //namespace std
22 #endif //BOOST_NO_STDC_NAMESPACE
23
24 namespace boost {
25 namespace uuids {
26
27 // generate a name-based uuid
28 // TODO: add in common namesspace uuids
29 class name_generator {
30 public:
31 typedef uuid result_type;
32
33 explicit name_generator(uuid const& namespace_uuid_)
34 : namespace_uuid(namespace_uuid_)
35 {}
36
37 uuid operator()(const char* name) {
38 reset();
39 process_characters(name, std::strlen(name));
40 return sha_to_uuid();
41 }
42
43 uuid operator()(const wchar_t* name) {
44 reset();
45 process_characters(name, std::wcslen(name));
46 return sha_to_uuid();
47 }
48
49 template <typename ch, typename char_traits, typename alloc>
50 uuid operator()(std::basic_string<ch, char_traits, alloc> const& name) {
51 reset();
52 process_characters(name.c_str(), name.length());
53 return sha_to_uuid();
54 }
55
56 uuid operator()(void const* buffer, std::size_t byte_count) {
57 reset();
58 sha.process_bytes(buffer, byte_count);
59 return sha_to_uuid();
60 };
61
62 private:
63 // we convert all characters to uint32_t so that each
64 // character is 4 bytes reguardless of sizeof(char) or
65 // sizeof(wchar_t). We want the name string on any
66 // platform / compiler to generate the same uuid
67 // except for char
68 template <typename char_type>
69 void process_characters(char_type const*const characters, size_t count) {
70 BOOST_ASSERT(sizeof(uint32_t) >= sizeof(char_type));
71
72 for (size_t i=0; i<count; i++) {
73 uint32_t c = characters[i];
74 sha.process_byte(static_cast<unsigned char>((c >> 0) & 0xFF));
75 sha.process_byte(static_cast<unsigned char>((c >> 8) & 0xFF));
76 sha.process_byte(static_cast<unsigned char>((c >> 16) & 0xFF));
77 sha.process_byte(static_cast<unsigned char>((c >> 24) & 0xFF));
78 }
79 }
80
81 void process_characters(char const*const characters, size_t count) {
82 sha.process_bytes(characters, count);
83 }
84
85 void reset()
86 {
87 sha.reset();
88 sha.process_bytes(namespace_uuid.begin(), namespace_uuid.size());
89 }
90
91 uuid sha_to_uuid()
92 {
93 unsigned int digest[5];
94
95 sha.get_digest(digest);
96
97 uuid u;
98 for (int i=0; i<4; ++i) {
99 *(u.begin() + i*4+0) = static_cast<uint8_t>((digest[i] >> 24) & 0xFF);
100 *(u.begin() + i*4+1) = static_cast<uint8_t>((digest[i] >> 16) & 0xFF);
101 *(u.begin() + i*4+2) = static_cast<uint8_t>((digest[i] >> 8) & 0xFF);
102 *(u.begin() + i*4+3) = static_cast<uint8_t>((digest[i] >> 0) & 0xFF);
103 }
104
105 // set variant
106 // must be 0b10xxxxxx
107 *(u.begin()+8) &= 0xBF;
108 *(u.begin()+8) |= 0x80;
109
110 // set version
111 // must be 0b0101xxxx
112 *(u.begin()+6) &= 0x5F; //0b01011111
113 *(u.begin()+6) |= 0x50; //0b01010000
114
115 return u;
116 }
117
118 private:
119 uuid namespace_uuid;
120 detail::sha1 sha;
121 };
122
123 }} // namespace boost::uuids
124
125 #endif // BOOST_UUID_NAME_GENERATOR_HPP