|
| 1 | +#ifndef BOOST_UUID_DETAIL_BASIC_NAME_GENERATOR_HPP_INCLUDED |
| 2 | +#define BOOST_UUID_DETAIL_BASIC_NAME_GENERATOR_HPP_INCLUDED |
| 3 | + |
| 4 | +// Boost basic_name_generator.hpp header file -----------------------// |
| 5 | + |
| 6 | +// Copyright 2010 Andy Tompkins. |
| 7 | +// Copyright 2017 James E. King III |
| 8 | + |
| 9 | +// Distributed under the Boost Software License, Version 1.0. (See |
| 10 | +// accompanying file LICENSE_1_0.txt or copy at |
| 11 | +// https://www.boost.org/LICENSE_1_0.txt) |
| 12 | + |
| 13 | +#include <boost/uuid/namespaces.hpp> |
| 14 | +#include <boost/uuid/uuid.hpp> |
| 15 | +#include <boost/uuid/detail/static_assert.hpp> |
| 16 | +#include <boost/config.hpp> |
| 17 | +#include <string> |
| 18 | +#include <cstdint> |
| 19 | +#include <cstring> // for strlen, wcslen |
| 20 | + |
| 21 | +namespace boost { |
| 22 | +namespace uuids { |
| 23 | +namespace detail { |
| 24 | + |
| 25 | +template<class HashAlgo> |
| 26 | +class basic_name_generator |
| 27 | +{ |
| 28 | +private: |
| 29 | + |
| 30 | + uuid namespace_uuid_; |
| 31 | + |
| 32 | +private: |
| 33 | + |
| 34 | + using digest_type = typename HashAlgo::digest_type; |
| 35 | + |
| 36 | +public: |
| 37 | + |
| 38 | + using result_type = uuid; |
| 39 | + |
| 40 | + explicit basic_name_generator( uuid const& namespace_uuid ) noexcept |
| 41 | + : namespace_uuid_( namespace_uuid ) |
| 42 | + {} |
| 43 | + |
| 44 | + template<class Ch> uuid operator()( Ch const* name ) const noexcept |
| 45 | + { |
| 46 | + HashAlgo hash; |
| 47 | + |
| 48 | + hash.process_bytes( namespace_uuid_.begin(), namespace_uuid_.size() ); |
| 49 | + process_characters( hash, name, std::char_traits<Ch>().length( name ) ); |
| 50 | + |
| 51 | + return hash_to_uuid( hash ); |
| 52 | + } |
| 53 | + |
| 54 | + template<class Ch, class Traits, class Alloc> |
| 55 | + uuid operator()( std::basic_string<Ch, Traits, Alloc> const& name ) const noexcept |
| 56 | + { |
| 57 | + HashAlgo hash; |
| 58 | + |
| 59 | + hash.process_bytes( namespace_uuid_.begin(), namespace_uuid_.size() ); |
| 60 | + process_characters( hash, name.c_str(), name.length() ); |
| 61 | + |
| 62 | + return hash_to_uuid( hash ); |
| 63 | + } |
| 64 | + |
| 65 | + uuid operator()( void const* buffer, std::size_t byte_count ) const noexcept |
| 66 | + { |
| 67 | + HashAlgo hash; |
| 68 | + |
| 69 | + hash.process_bytes( namespace_uuid_.begin(), namespace_uuid_.size() ); |
| 70 | + hash.process_bytes( buffer, byte_count ); |
| 71 | + |
| 72 | + return hash_to_uuid( hash ); |
| 73 | + } |
| 74 | + |
| 75 | +private: |
| 76 | + |
| 77 | + void process_characters( HashAlgo& hash, char const* p, std::size_t n ) const noexcept |
| 78 | + { |
| 79 | + hash.process_bytes( p, n ); |
| 80 | + } |
| 81 | + |
| 82 | + // For portability, we convert all wide characters to uint32_t so that each |
| 83 | + // character is 4 bytes regardless of sizeof(wchar_t). |
| 84 | + |
| 85 | + void process_characters( HashAlgo& hash, wchar_t const* p, std::size_t n ) const noexcept |
| 86 | + { |
| 87 | + BOOST_UUID_STATIC_ASSERT( sizeof( std::uint32_t ) >= sizeof( wchar_t ) ); |
| 88 | + |
| 89 | + for( std::size_t i = 0; i < n; ++i) |
| 90 | + { |
| 91 | + std::uint32_t ch = p[ i ]; |
| 92 | + |
| 93 | + unsigned char bytes[ 4 ] = |
| 94 | + { |
| 95 | + static_cast<unsigned char>( ( ch >> 0 ) & 0xFF ), |
| 96 | + static_cast<unsigned char>( ( ch >> 8 ) & 0xFF ), |
| 97 | + static_cast<unsigned char>( ( ch >> 16 ) & 0xFF ), |
| 98 | + static_cast<unsigned char>( ( ch >> 24 ) & 0xFF ) |
| 99 | + }; |
| 100 | + |
| 101 | + hash.process_bytes( bytes, 4 ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + void process_characters( HashAlgo& hash, char32_t const* p, std::size_t n ) const noexcept |
| 106 | + { |
| 107 | + for( std::size_t i = 0; i < n; ++i) |
| 108 | + { |
| 109 | + process_utf32_codepoint( hash, p[ i ] ); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + void process_characters( HashAlgo& hash, char16_t const* p, std::size_t n ) const noexcept |
| 114 | + { |
| 115 | + for( std::size_t i = 0; i < n; ++i) |
| 116 | + { |
| 117 | + char16_t ch = p[ i ]; |
| 118 | + |
| 119 | + if( ch >= 0xD800 && ch <= 0xDBFF && i + 1 < n && p[ i+1 ] >= 0xDC00 && p[ i+1 ] <= 0xDFFF ) |
| 120 | + { |
| 121 | + char16_t ch2 = p[ ++i ]; |
| 122 | + |
| 123 | + std::uint32_t high = ch - 0xD800; |
| 124 | + std::uint32_t low = ch2 - 0xDC00; |
| 125 | + |
| 126 | + process_utf32_codepoint( hash, ( high << 10 ) + low + 0x10000 ); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + process_utf32_codepoint( hash, ch ); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + void process_utf32_codepoint( HashAlgo& hash, std::uint32_t cp ) const noexcept |
| 136 | + { |
| 137 | + if( ( cp >= 0xD800 && cp <= 0xDFFF ) || cp > 0x10FFFF ) |
| 138 | + { |
| 139 | + cp = 0xFFFD; // Unicode replacement character |
| 140 | + } |
| 141 | + |
| 142 | + if( cp < 0x80 ) |
| 143 | + { |
| 144 | + hash.process_byte( static_cast<unsigned char>( cp ) ); |
| 145 | + } |
| 146 | + else if( cp < 0x800 ) |
| 147 | + { |
| 148 | + unsigned char bytes[ 2 ] = |
| 149 | + { |
| 150 | + static_cast<unsigned char>( 0xC0 | (cp >> 6) ), |
| 151 | + static_cast<unsigned char>( 0x80 | (cp & 0x3F) ) |
| 152 | + }; |
| 153 | + |
| 154 | + hash.process_bytes( bytes, 2 ); |
| 155 | + } |
| 156 | + else if( cp < 0x10000 ) |
| 157 | + { |
| 158 | + unsigned char bytes[ 3 ] = |
| 159 | + { |
| 160 | + static_cast<unsigned char>( 0xE0 | (cp >> 12) ), |
| 161 | + static_cast<unsigned char>( 0x80 | ((cp >> 6) & 0x3F) ), |
| 162 | + static_cast<unsigned char>( 0x80 | (cp & 0x3F) ) |
| 163 | + }; |
| 164 | + |
| 165 | + hash.process_bytes( bytes, 3 ); |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + unsigned char bytes[ 4 ] = |
| 170 | + { |
| 171 | + static_cast<unsigned char>( 0xF0 | ( cp >> 18 ) ), |
| 172 | + static_cast<unsigned char>( 0x80 | ((cp >> 12 ) & 0x3F ) ), |
| 173 | + static_cast<unsigned char>( 0x80 | ((cp >> 6 ) & 0x3F ) ), |
| 174 | + static_cast<unsigned char>( 0x80 | (cp & 0x3F) ) |
| 175 | + }; |
| 176 | + |
| 177 | + hash.process_bytes( bytes, 4 ); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | +#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L |
| 182 | + |
| 183 | + void process_characters( HashAlgo& hash, char8_t const* p, std::size_t n ) const noexcept |
| 184 | + { |
| 185 | + hash.process_bytes( p, n ); |
| 186 | + } |
| 187 | + |
| 188 | +#endif |
| 189 | + |
| 190 | + uuid hash_to_uuid( HashAlgo& hash ) const noexcept |
| 191 | + { |
| 192 | + digest_type digest; |
| 193 | + hash.get_digest(digest); |
| 194 | + |
| 195 | + BOOST_UUID_STATIC_ASSERT( sizeof(digest_type) >= 16 ); |
| 196 | + |
| 197 | + uuid u; |
| 198 | + std::memcpy( u.data, digest, 16 ); |
| 199 | + |
| 200 | + // set variant: must be 0b10xxxxxx |
| 201 | + *(u.begin()+8) &= 0x3F; |
| 202 | + *(u.begin()+8) |= 0x80; |
| 203 | + |
| 204 | + // set version |
| 205 | + unsigned char hashver = hash.get_version(); |
| 206 | + *(u.begin()+6) &= 0x0F; // clear out the relevant bits |
| 207 | + *(u.begin()+6) |= (hashver << 4); // and apply them |
| 208 | + |
| 209 | + return u; |
| 210 | + } |
| 211 | +}; |
| 212 | + |
| 213 | +} // namespace detail |
| 214 | +} // uuids |
| 215 | +} // boost |
| 216 | + |
| 217 | +#endif // BOOST_UUID_DETAIL_BASIC_NAME_GENERATOR_HPP_INCLUDED |
0 commit comments