Chris@16: // Boost name_generator.hpp header file ----------------------------------------------// Chris@16: Chris@16: // Copyright 2010 Andy Tompkins. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_UUID_NAME_GENERATOR_HPP Chris@16: #define BOOST_UUID_NAME_GENERATOR_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // for strlen, wcslen Chris@16: Chris@16: #ifdef BOOST_NO_STDC_NAMESPACE Chris@16: namespace std { Chris@16: using ::strlen; Chris@16: using ::wcslen; Chris@16: } //namespace std Chris@16: #endif //BOOST_NO_STDC_NAMESPACE Chris@16: Chris@16: namespace boost { Chris@16: namespace uuids { Chris@16: Chris@16: // generate a name-based uuid Chris@16: // TODO: add in common namesspace uuids Chris@16: class name_generator { Chris@16: public: Chris@16: typedef uuid result_type; Chris@16: Chris@101: explicit name_generator(uuid const& namespace_uuid_) Chris@101: : namespace_uuid(namespace_uuid_) Chris@16: {} Chris@16: Chris@16: uuid operator()(const char* name) { Chris@16: reset(); Chris@16: process_characters(name, std::strlen(name)); Chris@16: return sha_to_uuid(); Chris@16: } Chris@16: Chris@16: uuid operator()(const wchar_t* name) { Chris@16: reset(); Chris@16: process_characters(name, std::wcslen(name)); Chris@16: return sha_to_uuid(); Chris@16: } Chris@16: Chris@16: template Chris@16: uuid operator()(std::basic_string const& name) { Chris@16: reset(); Chris@16: process_characters(name.c_str(), name.length()); Chris@16: return sha_to_uuid(); Chris@16: } Chris@16: Chris@16: uuid operator()(void const* buffer, std::size_t byte_count) { Chris@16: reset(); Chris@16: sha.process_bytes(buffer, byte_count); Chris@16: return sha_to_uuid(); Chris@16: }; Chris@16: Chris@16: private: Chris@16: // we convert all characters to uint32_t so that each Chris@16: // character is 4 bytes reguardless of sizeof(char) or Chris@16: // sizeof(wchar_t). We want the name string on any Chris@16: // platform / compiler to generate the same uuid Chris@16: // except for char Chris@16: template Chris@16: void process_characters(char_type const*const characters, size_t count) { Chris@16: BOOST_ASSERT(sizeof(uint32_t) >= sizeof(char_type)); Chris@16: Chris@16: for (size_t i=0; i((c >> 0) & 0xFF)); Chris@101: sha.process_byte(static_cast((c >> 8) & 0xFF)); Chris@101: sha.process_byte(static_cast((c >> 16) & 0xFF)); Chris@101: sha.process_byte(static_cast((c >> 24) & 0xFF)); Chris@16: } Chris@16: } Chris@16: Chris@16: void process_characters(char const*const characters, size_t count) { Chris@16: sha.process_bytes(characters, count); Chris@16: } Chris@16: Chris@16: void reset() Chris@16: { Chris@16: sha.reset(); Chris@16: sha.process_bytes(namespace_uuid.begin(), namespace_uuid.size()); Chris@16: } Chris@16: Chris@16: uuid sha_to_uuid() Chris@16: { Chris@16: unsigned int digest[5]; Chris@16: Chris@16: sha.get_digest(digest); Chris@16: Chris@16: uuid u; Chris@16: for (int i=0; i<4; ++i) { Chris@101: *(u.begin() + i*4+0) = static_cast((digest[i] >> 24) & 0xFF); Chris@101: *(u.begin() + i*4+1) = static_cast((digest[i] >> 16) & 0xFF); Chris@101: *(u.begin() + i*4+2) = static_cast((digest[i] >> 8) & 0xFF); Chris@101: *(u.begin() + i*4+3) = static_cast((digest[i] >> 0) & 0xFF); Chris@16: } Chris@16: Chris@16: // set variant Chris@16: // must be 0b10xxxxxx Chris@16: *(u.begin()+8) &= 0xBF; Chris@16: *(u.begin()+8) |= 0x80; Chris@16: Chris@16: // set version Chris@16: // must be 0b0101xxxx Chris@16: *(u.begin()+6) &= 0x5F; //0b01011111 Chris@16: *(u.begin()+6) |= 0x50; //0b01010000 Chris@16: Chris@16: return u; Chris@16: } Chris@16: Chris@16: private: Chris@16: uuid namespace_uuid; Chris@16: detail::sha1 sha; Chris@16: }; Chris@16: Chris@16: }} // namespace boost::uuids Chris@16: Chris@16: #endif // BOOST_UUID_NAME_GENERATOR_HPP