From a9507ae88d8209273fa8d3078306c05dd64f0280 Mon Sep 17 00:00:00 2001 From: JinmingHu Date: Fri, 13 Aug 2021 07:50:25 +0800 Subject: [PATCH] Fix build error on some platforms (#2734) * Move uuid impl to .cpp file * CL --- sdk/core/azure-core/CHANGELOG.md | 2 + sdk/core/azure-core/CMakeLists.txt | 1 + sdk/core/azure-core/inc/azure/core/uuid.hpp | 70 +---------------- sdk/core/azure-core/src/uuid.cpp | 77 +++++++++++++++++++ .../test/share_file_client_test.cpp | 1 + 5 files changed, 83 insertions(+), 68 deletions(-) create mode 100644 sdk/core/azure-core/src/uuid.cpp diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index a2209d74e..3c151d805 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -10,6 +10,8 @@ ### Other Changes +- Fixed compilation error on POSIX platforms where OpenSSL was not available. + ## 1.2.0 (2021-08-05) ### Features Added diff --git a/sdk/core/azure-core/CMakeLists.txt b/sdk/core/azure-core/CMakeLists.txt index ac808ee7d..78f551b0f 100644 --- a/sdk/core/azure-core/CMakeLists.txt +++ b/sdk/core/azure-core/CMakeLists.txt @@ -111,6 +111,7 @@ set( src/io/random_access_file_body_stream.cpp src/private/environment_log_level_listener.hpp src/private/package_version.hpp + src/uuid.cpp src/base64.cpp src/context.cpp src/datetime.cpp diff --git a/sdk/core/azure-core/inc/azure/core/uuid.hpp b/sdk/core/azure-core/inc/azure/core/uuid.hpp index 3af3f0351..fbf73004e 100644 --- a/sdk/core/azure-core/inc/azure/core/uuid.hpp +++ b/sdk/core/azure-core/inc/azure/core/uuid.hpp @@ -11,14 +11,7 @@ #include "azure/core/platform.hpp" #include -#include // for placement new -#include #include -#include // for swap and move - -#if defined(AZ_PLATFORM_POSIX) -#include //for RAND_bytes -#endif namespace Azure { namespace Core { /** @@ -44,71 +37,12 @@ namespace Azure { namespace Core { * @brief Gets Uuid as a string. * @details A string is in canonical format (4-2-2-2-6 lowercase hex and dashes only). */ - std::string ToString() - { - // Guid is 36 characters - // Add one byte for the \0 - char s[37]; - - std::snprintf( - s, - sizeof(s), - "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x", - m_uuid[0], - m_uuid[1], - m_uuid[2], - m_uuid[3], - m_uuid[4], - m_uuid[5], - m_uuid[6], - m_uuid[7], - m_uuid[8], - m_uuid[9], - m_uuid[10], - m_uuid[11], - m_uuid[12], - m_uuid[13], - m_uuid[14], - m_uuid[15]); - - return std::string(s); - } + std::string ToString(); /** * @brief Creates a new random UUID. * */ - static Uuid CreateUuid() - { - uint8_t uuid[UuidSize] = {}; - -#if defined(AZ_PLATFORM_WINDOWS) - std::random_device rd; - - for (size_t i = 0; i < UuidSize; i += 4) - { - const uint32_t x = rd(); - std::memcpy(uuid + i, &x, 4); - } -#elif defined(AZ_PLATFORM_POSIX) - // This static cast is safe since we know Uuid size, which is a const, will always fit an int. - int ret = RAND_bytes(uuid, static_cast(UuidSize)); - if (ret <= 0) - { - abort(); - } -#else - abort(); -#endif - - // SetVariant to ReservedRFC4122 - uuid[8] = (uuid[8] | ReservedRFC4122) & 0x7F; - - constexpr uint8_t version = 4; - - uuid[6] = (uuid[6] & 0xF) | (version << 4); - - return Uuid(uuid); - } + static Uuid CreateUuid(); }; }} // namespace Azure::Core diff --git a/sdk/core/azure-core/src/uuid.cpp b/sdk/core/azure-core/src/uuid.cpp new file mode 100644 index 000000000..864e330d0 --- /dev/null +++ b/sdk/core/azure-core/src/uuid.cpp @@ -0,0 +1,77 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: MIT + +#include "azure/core/uuid.hpp" + +#if defined(AZ_PLATFORM_POSIX) +#include //for RAND_bytes +#endif + +#include +#include + +namespace Azure { namespace Core { + std::string Uuid::ToString() + { + // Guid is 36 characters + // Add one byte for the \0 + char s[37]; + + std::snprintf( + s, + sizeof(s), + "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x", + m_uuid[0], + m_uuid[1], + m_uuid[2], + m_uuid[3], + m_uuid[4], + m_uuid[5], + m_uuid[6], + m_uuid[7], + m_uuid[8], + m_uuid[9], + m_uuid[10], + m_uuid[11], + m_uuid[12], + m_uuid[13], + m_uuid[14], + m_uuid[15]); + + return std::string(s); + } + + Uuid Uuid::CreateUuid() + { + uint8_t uuid[UuidSize] = {}; + +#if defined(AZ_PLATFORM_WINDOWS) + std::random_device rd; + + for (size_t i = 0; i < UuidSize; i += 4) + { + const uint32_t x = rd(); + std::memcpy(uuid + i, &x, 4); + } +#elif defined(AZ_PLATFORM_POSIX) + // This static cast is safe since we know Uuid size, which is a const, will always fit an int. + int ret = RAND_bytes(uuid, static_cast(UuidSize)); + if (ret <= 0) + { + abort(); + } +#else + abort(); +#endif + + // SetVariant to ReservedRFC4122 + uuid[8] = (uuid[8] | ReservedRFC4122) & 0x7F; + + constexpr uint8_t version = 4; + + uuid[6] = (uuid[6] & 0xF) | (version << 4); + + return Uuid(uuid); + } + +}} // namespace Azure::Core diff --git a/sdk/storage/azure-storage-files-shares/test/share_file_client_test.cpp b/sdk/storage/azure-storage-files-shares/test/share_file_client_test.cpp index 62407d09a..a7de73f73 100644 --- a/sdk/storage/azure-storage-files-shares/test/share_file_client_test.cpp +++ b/sdk/storage/azure-storage-files-shares/test/share_file_client_test.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include