diff --git a/include/rtl/uuid.h b/include/rtl/uuid.h index a69770831518..e65c5e12b422 100644 --- a/include/rtl/uuid.h +++ b/include/rtl/uuid.h @@ -84,6 +84,49 @@ SAL_DLLPUBLIC void SAL_CALL rtl_createUuid( SAL_DLLPUBLIC sal_Int32 SAL_CALL rtl_compareUuid( const sal_uInt8 *pUUID1 , const sal_uInt8 *pUUID2 ); +/** Creates named UUIDs. + +

+ The version 3 UUID is meant for generating UUIDs from names that + are drawn from, and unique within, some name space. Some examples + of names (and, implicitly, name spaces) might be DNS names, URLs, ISO + Object IDs (OIDs), reserved words in a programming language, or X.500 + Distinguished Names (DNs); thus, the concept of name and name space + should be broadly construed, and not limited to textual names. + +

+ The requirements for such UUIDs are as follows: + +

+ + @param pTargetUUID pointer to at least 16 bytes of memory. After the call + it contains the newly generated uuid in network byte order. + @param pNameSpaceUUID The namespace uuid. Below are some predefined ones, + but any arbitrary uuid can be used as namespace. + + @param pName the name + */ +SAL_DLLPUBLIC void SAL_CALL rtl_createNamedUuid( + sal_uInt8 *pTargetUUID, + const sal_uInt8 *pNameSpaceUUID, + const rtl_String *pName + ); + + + /* Predefined Namespaces (Use them the following way : sal_uInt8 aNsDNS[16]) = RTL_UUID_NAMESPACE_DNS; diff --git a/sal/qa/rtl/uuid/rtl_Uuid.cxx b/sal/qa/rtl/uuid/rtl_Uuid.cxx index 255e339b8db9..e7b524db89fb 100644 --- a/sal/qa/rtl/uuid/rtl_Uuid.cxx +++ b/sal/qa/rtl/uuid/rtl_Uuid.cxx @@ -95,7 +95,57 @@ public: CPPUNIT_TEST_SUITE_END(); }; // class createUuid +class createNamedUuid : public CppUnit::TestFixture +{ +public: + void createNamedUuid_001() + { + sal_uInt8 NameSpace_DNS[16] = RTL_UUID_NAMESPACE_DNS; + sal_uInt8 NameSpace_URL[16] = RTL_UUID_NAMESPACE_URL; + sal_uInt8 pPriorCalculatedUUID[16] = { + 0x52,0xc9,0x30,0xa5, + 0xd1,0x61,0x3b,0x16, + 0x98,0xc5,0xf8,0xd1, + 0x10,0x10,0x6d,0x4d }; + + sal_uInt8 pNamedUUID[16], pNamedUUID2[16]; + + // Same name does generate the same uuid + rtl_String *pName = nullptr; + rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Name" ); + rtl_createNamedUuid( pNamedUUID , NameSpace_DNS , pName ); + rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName ); + CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pNamedUUID2 , 16 )); + CPPUNIT_ASSERT_EQUAL_MESSAGE( "Same name should generate the same uuid", sal_Int32(0), rtl_compareUuid( pNamedUUID , pNamedUUID2 ) ); + CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pPriorCalculatedUUID , 16 ) ); + + // Different names does not generate the same uuid + rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Namf" ); + rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName ); + CPPUNIT_ASSERT_MESSAGE("Different names does not generate the same uuid.", memcmp( pNamedUUID , pNamedUUID2 , 16 ) ); + + // the same name with different namespace uuid produces different uuids + rtl_createNamedUuid( pNamedUUID , NameSpace_URL , pName ); + CPPUNIT_ASSERT_MESSAGE( " same name with different namespace uuid produces different uuids", memcmp( pNamedUUID , pNamedUUID2 , 16 )); + CPPUNIT_ASSERT_MESSAGE( " same name with different namespace uuid produces different uuids", rtl_compareUuid( pNamedUUID , pNamedUUID2 ) != 0); + + //test compareUuid + if ( rtl_compareUuid( pNamedUUID , pNamedUUID2 ) > 0 ) + { CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) < 0); + } + else + CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) > 0); + + rtl_string_release( pName ); + } + + CPPUNIT_TEST_SUITE(createNamedUuid); + CPPUNIT_TEST(createNamedUuid_001); + CPPUNIT_TEST_SUITE_END(); +}; // class createNamedUuid + CPPUNIT_TEST_SUITE_REGISTRATION(rtl_Uuid::createUuid); +CPPUNIT_TEST_SUITE_REGISTRATION(rtl_Uuid::createNamedUuid); } // namespace rtl_Uuid /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/rtl/uuid.cxx b/sal/rtl/uuid.cxx index db860b450861..4249c7bddedb 100644 --- a/sal/rtl/uuid.cxx +++ b/sal/rtl/uuid.cxx @@ -22,6 +22,14 @@ #include #include +#include + +#define SWAP_INT16_TO_NETWORK(x)\ + { sal_uInt16 y = x;\ + sal_uInt8 *p = reinterpret_cast(&(x)); \ + p[0] = static_cast( ( y >> 8 ) & 0xff );\ + p[1] = static_cast( ( y ) & 0xff);\ + } #define SWAP_NETWORK_TO_INT16(x)\ { sal_uInt16 y = x;\ @@ -52,6 +60,25 @@ struct UUID } +static void write_v3( sal_uInt8 *pUuid ) +{ + UUID uuid; + // copy to avoid alignment problems + memcpy(&uuid, pUuid, 16); + + SWAP_NETWORK_TO_INT16(uuid.time_hi_and_version); + + /* put in the variant and version bits */ + uuid.time_hi_and_version &= 0x0FFF; + uuid.time_hi_and_version |= (3 << 12); + uuid.clock_seq_hi_and_reserved &= 0x3F; + uuid.clock_seq_hi_and_reserved |= 0x80; + + SWAP_INT16_TO_NETWORK(uuid.time_hi_and_version); + + memcpy(pUuid, &uuid, 16); +} + extern "C" void SAL_CALL rtl_createUuid(sal_uInt8 *pTargetUUID , SAL_UNUSED_PARAMETER const sal_uInt8 *, SAL_UNUSED_PARAMETER sal_Bool) @@ -70,6 +97,21 @@ extern "C" void SAL_CALL rtl_createUuid(sal_uInt8 *pTargetUUID , pTargetUUID[8] |= 0x80; } +extern "C" void SAL_CALL rtl_createNamedUuid(sal_uInt8 *pTargetUUID, + const sal_uInt8 *pNameSpaceUUID, + const rtl_String *pName ) +{ + rtlDigest digest = rtl_digest_createMD5(); + + rtl_digest_updateMD5(digest, pNameSpaceUUID, 16); + rtl_digest_updateMD5(digest, pName->buffer, pName->length); + + rtl_digest_getMD5(digest, pTargetUUID, 16); + rtl_digest_destroyMD5(digest); + + write_v3(pTargetUUID); +} + extern "C" sal_Int32 SAL_CALL rtl_compareUuid(const sal_uInt8 *pUUID1, const sal_uInt8 *pUUID2) { int i;