Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:32

0001 // Boost basic_name_generator.hpp header file  -----------------------//
0002 
0003 // Copyright 2010 Andy Tompkins.
0004 // Copyright 2017 James E. King III
0005 
0006 // Distributed under the Boost Software License, Version 1.0. (See
0007 // accompanying file LICENSE_1_0.txt or copy at
0008 //  https://www.boost.org/LICENSE_1_0.txt)
0009 
0010 #ifndef BOOST_UUID_BASIC_NAME_GENERATOR_HPP
0011 #define BOOST_UUID_BASIC_NAME_GENERATOR_HPP
0012 
0013 #include <boost/config.hpp>
0014 #include <boost/cstdint.hpp>
0015 #include <boost/static_assert.hpp>
0016 #include <boost/uuid/uuid.hpp>
0017 #include <cstring> // for strlen, wcslen
0018 #include <string>
0019 
0020 #ifdef BOOST_HAS_PRAGMA_ONCE
0021 #pragma once
0022 #endif
0023 
0024 #ifdef BOOST_NO_STDC_NAMESPACE
0025 namespace std {
0026     using ::size_t;
0027     using ::strlen;
0028     using ::wcslen;
0029 } //namespace std
0030 #endif //BOOST_NO_STDC_NAMESPACE
0031 
0032 namespace boost {
0033 namespace uuids {
0034 
0035 //! \brief Generate a name based UUID using
0036 //!        the provided hashing algorithm that
0037 //!        implements the NameHashProvider concept.
0038 template<class HashAlgo>
0039 class basic_name_generator
0040 {
0041   public:
0042     typedef uuid result_type;
0043     typedef typename HashAlgo::digest_type digest_type;
0044 
0045     explicit basic_name_generator(uuid const& namespace_uuid_)
0046         : namespace_uuid(namespace_uuid_)
0047     {}
0048 
0049     uuid operator()(const char* name) const {
0050         HashAlgo hash;
0051         hash.process_bytes(namespace_uuid.begin(), namespace_uuid.size());
0052         process_characters(hash, name, std::strlen(name));
0053         return hash_to_uuid(hash);
0054     }
0055 
0056     uuid operator()(const wchar_t* name) const {
0057         HashAlgo hash;
0058         hash.process_bytes(namespace_uuid.begin(), namespace_uuid.size());
0059         process_characters(hash, name, std::wcslen(name));
0060         return hash_to_uuid(hash);
0061     }
0062 
0063     template <typename ch, typename char_traits, typename alloc>
0064     uuid operator()(std::basic_string<ch, char_traits, alloc> const& name) const {
0065         HashAlgo hash;
0066         hash.process_bytes(namespace_uuid.begin(), namespace_uuid.size());
0067         process_characters(hash, name.c_str(), name.length());
0068         return hash_to_uuid(hash);
0069     }
0070 
0071     uuid operator()(void const* buffer, std::size_t byte_count) const {
0072         HashAlgo hash;
0073         hash.process_bytes(namespace_uuid.begin(), namespace_uuid.size());
0074         hash.process_bytes(buffer, byte_count);
0075         return hash_to_uuid(hash);
0076     }
0077 
0078 private:
0079     // we convert all characters to uint32_t so that each
0080     // character is 4 bytes regardless of sizeof(char) or
0081     // sizeof(wchar_t).  We want the name string on any
0082     // platform / compiler to generate the same uuid
0083     // except for char
0084     template <typename char_type>
0085     void process_characters(HashAlgo& hash, char_type const*const characters, std::size_t count) const {
0086         BOOST_STATIC_ASSERT(sizeof(uint32_t) >= sizeof(char_type));
0087 
0088         for (std::size_t i=0; i<count; i++) {
0089             std::size_t c = characters[i];
0090             hash.process_byte(static_cast<unsigned char>((c >>  0) & 0xFF));
0091             hash.process_byte(static_cast<unsigned char>((c >>  8) & 0xFF));
0092             hash.process_byte(static_cast<unsigned char>((c >> 16) & 0xFF));
0093             hash.process_byte(static_cast<unsigned char>((c >> 24) & 0xFF));
0094         }
0095     }
0096 
0097     void process_characters(HashAlgo& hash, char const*const characters, std::size_t count) const {
0098         hash.process_bytes(characters, count);
0099     }
0100 
0101     uuid hash_to_uuid(HashAlgo& hash) const
0102     {
0103         digest_type digest;
0104         hash.get_digest(digest);
0105 
0106         BOOST_STATIC_ASSERT(sizeof(digest_type) >= 16);
0107 
0108         uuid u;
0109         for (int i=0; i<4; ++i) {
0110             *(u.begin() + i*4+0) = static_cast<uint8_t>((digest[i] >> 24) & 0xFF);
0111             *(u.begin() + i*4+1) = static_cast<uint8_t>((digest[i] >> 16) & 0xFF);
0112             *(u.begin() + i*4+2) = static_cast<uint8_t>((digest[i] >> 8) & 0xFF);
0113             *(u.begin() + i*4+3) = static_cast<uint8_t>((digest[i] >> 0) & 0xFF);
0114         }
0115 
0116         // set variant: must be 0b10xxxxxx
0117         *(u.begin()+8) &= 0xBF;
0118         *(u.begin()+8) |= 0x80;
0119 
0120         // set version
0121         unsigned char hashver = hash.get_version();
0122         *(u.begin()+6) &= 0x0F;             // clear out the relevant bits
0123         *(u.begin()+6) |= (hashver << 4);   // and apply them
0124 
0125         return u;
0126     }
0127 
0128 private:
0129     uuid namespace_uuid;
0130 };
0131 
0132 namespace ns {
0133 
0134 BOOST_FORCEINLINE uuid dns() {
0135     uuid result = {{
0136         0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1 ,
0137         0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }};
0138     return result;
0139 }
0140 
0141 BOOST_FORCEINLINE uuid url() {
0142     uuid result = {{
0143         0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1 ,
0144         0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }};
0145     return result;
0146 }
0147 
0148 BOOST_FORCEINLINE uuid oid() {
0149     uuid result = {{
0150         0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1 ,
0151         0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }};
0152     return result;
0153 }
0154 
0155 BOOST_FORCEINLINE uuid x500dn() {
0156     uuid result = {{
0157         0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1 ,
0158         0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }};
0159     return result;
0160 }
0161 
0162 } // ns
0163 } // uuids
0164 } // boost
0165 
0166 #endif // BOOST_UUID_BASIC_NAME_GENERATOR_HPP