Fix build error on some platforms (#2734)

* Move uuid impl to .cpp file

* CL
This commit is contained in:
JinmingHu 2021-08-13 07:50:25 +08:00 committed by GitHub
parent a9281f2808
commit a9507ae88d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 68 deletions

View File

@ -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

View File

@ -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

View File

@ -11,14 +11,7 @@
#include "azure/core/platform.hpp"
#include <cstring>
#include <new> // for placement new
#include <random>
#include <string>
#include <utility> // for swap and move
#if defined(AZ_PLATFORM_POSIX)
#include <openssl/rand.h> //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<int>(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

View File

@ -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 <openssl/rand.h> //for RAND_bytes
#endif
#include <cstdio>
#include <random>
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<int>(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

View File

@ -6,6 +6,7 @@
#include <algorithm>
#include <chrono>
#include <future>
#include <random>
#include <azure/core/cryptography/hash.hpp>
#include <azure/storage/common/crypt.hpp>