Skip to content

Commit 805759e

Browse files
authored
Merge pull request #23 from awslabs/UUID
Uuid implementation
2 parents ff6e373 + aa39419 commit 805759e

File tree

12 files changed

+428
-129
lines changed

12 files changed

+428
-129
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,8 +1033,10 @@ endif()
10331033

10341034
# default libraries to link in per-platform
10351035
if(PLATFORM_WINDOWS)
1036-
set(PLATFORM_DEP_LIBS Userenv )
1037-
elseif(PLATFORM_LINUX OR PLATFORM_APPLE)
1036+
set(PLATFORM_DEP_LIBS Userenv Rpcrt4)
1037+
elseif(PLATFORM_LINUX)
1038+
set(PLATFORM_DEP_LIBS pthread uuid)
1039+
elseif(PLATFORM_APPLE)
10381040
set(PLATFORM_DEP_LIBS pthread)
10391041
elseif(PLATFORM_ANDROID)
10401042
set(PLATFORM_DEP_LIBS log atomic)

README.md

Lines changed: 122 additions & 122 deletions
Large diffs are not rendered by default.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
#include <aws/external/gtest.h>
16+
#include <aws/core/utils/UUID.h>
17+
#include <aws/core/utils/HashingUtils.h>
18+
#include <aws/core/utils/memory/stl/AWSSet.h>
19+
20+
using namespace Aws::Utils;
21+
22+
TEST(UUIDTest, TestPlatformGeneratesUUID)
23+
{
24+
Aws::Set<Aws::String> generatedUUids;
25+
26+
for(size_t i = 0u; i < 1000u; ++i)
27+
{
28+
UUID uuid = UUID::RandomUUID();
29+
Aws::String uuidStr = uuid;
30+
ASSERT_EQ(36u, uuidStr.length());
31+
ByteBuffer rawUUID = uuid;
32+
ASSERT_EQ(16u, rawUUID.GetLength());
33+
34+
ASSERT_EQ(generatedUUids.end(), generatedUUids.find(uuidStr));
35+
generatedUUids.insert(uuidStr);
36+
}
37+
}
38+
39+
TEST(UUIDTest, TestUUIDToStringConversion)
40+
{
41+
ByteBuffer rawUuuid = HashingUtils::HexDecode("f81d4fae7dec11d0a76500a0c91e6bf6");
42+
const char* expectedStr = "F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6";
43+
44+
UUID uuid(rawUuuid.GetUnderlyingData());
45+
Aws::String convertedStr = uuid;
46+
ASSERT_STREQ(expectedStr, convertedStr.c_str());
47+
}
48+
49+
TEST(UUIDTest, TestUUIDFromStringConversion)
50+
{
51+
Aws::String originalStr = "F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6";
52+
53+
UUID uuid(originalStr);
54+
Aws::String convertedStr = uuid;
55+
ASSERT_STREQ(originalStr.c_str(), convertedStr.c_str());
56+
}
57+
58+

aws-cpp-sdk-core/include/aws/core/utils/StringUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
#pragma once
17+
18+
#include <aws/core/Core_EXPORTS.h>
19+
#include <aws/core/utils/memory/stl/AWSString.h>
20+
#include <aws/core/utils/Array.h>
21+
22+
namespace Aws
23+
{
24+
namespace Utils
25+
{
26+
static const size_t UUID_BINARY_SIZE = 0x10;
27+
static const size_t UUID_STR_SIZE = 0x24;
28+
29+
/**
30+
* Class encapsulating a UUID. This is platform dependent. The method you are most likely interested in is RandomUUID().
31+
*/
32+
class AWS_CORE_API UUID
33+
{
34+
public:
35+
/**
36+
* Parses a GUID string into the raw data.
37+
*/
38+
UUID(const Aws::String&);
39+
/**
40+
* Sets the raw uuid data
41+
*/
42+
UUID(const unsigned char uuid[UUID_BINARY_SIZE]);
43+
44+
/**
45+
* Returns the current UUID as a GUID string
46+
*/
47+
operator Aws::String();
48+
/**
49+
* Returns a copy of the raw uuid
50+
*/
51+
inline operator ByteBuffer() { return ByteBuffer(m_uuid, sizeof(m_uuid)); }
52+
53+
/**
54+
* Generates a UUID. It will always try to prefer a random implementation from the entropy source on the machine. If none, is available, it will
55+
* fallback to the mac address and timestamp implementation.
56+
*/
57+
static UUID RandomUUID();
58+
59+
private:
60+
unsigned char m_uuid[UUID_BINARY_SIZE];
61+
};
62+
}
63+
}

aws-cpp-sdk-core/nuget/aws-cpp-sdk-core.autopkg

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ nuget {
1313
id = AWSSDKCPP-Core;
1414

1515
// Version number. Follows NuGet standards. (currently SemVer 1.0)
16-
version : 0.13.11;
16+
version : 0.13.11;
1717

1818
// Display name for package.
1919
title: AWS SDK for C++ (Core Runtime);
@@ -151,7 +151,8 @@ nuget {
151151
Libraries += winhttp.lib;
152152
Libraries += wininet.lib;
153153
Libraries += bcrypt.lib;
154-
Libraries += userenv.lib;
154+
Libraries += userenv.lib;
155+
Libraries += Rpcrt4.lib;
155156
}
156157
}
157158

aws-cpp-sdk-core/source/platform/android/Environment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
#include <aws/core/utils/UUID.h>
17+
18+
namespace Aws
19+
{
20+
namespace Utils
21+
{
22+
UUID UUID::RandomUUID()
23+
{
24+
char uuid[UUID_STR_SIZE];
25+
memset(uuid, 0, sizeof(uuid));
26+
27+
int fd = fopen("/proc/sys/kernel/random/uuid", "r");
28+
29+
if(fd)
30+
{
31+
fread(uuid, sizeof(uuid), sizeof(uuid), fd);
32+
fclose(fd);
33+
}
34+
Aws::String uuidStr(uuid);
35+
return UUID(uuidStr);
36+
}
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
#include <aws/core/utils/UUID.h>
17+
#include <uuid/uuid.h>
18+
19+
namespace Aws
20+
{
21+
namespace Utils
22+
{
23+
UUID UUID::RandomUUID()
24+
{
25+
uuid_t uuid;
26+
uuid_generate(uuid);
27+
return UUID(uuid);
28+
}
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
#include <aws/core/utils/UUID.h>
17+
#include <rpc.h>
18+
19+
namespace Aws
20+
{
21+
namespace Utils
22+
{
23+
Aws::Utils::UUID Aws::Utils::UUID::RandomUUID()
24+
{
25+
::UUID uuidStruct;
26+
UuidCreate(&uuidStruct);
27+
28+
unsigned char newUuid[UUID_BINARY_SIZE];
29+
memcpy(newUuid, &uuidStruct.Data1, sizeof(uuidStruct.Data1));
30+
memcpy(newUuid + 4, &uuidStruct.Data2, sizeof(uuidStruct.Data2));
31+
memcpy(newUuid + 6, &uuidStruct.Data3, sizeof(uuidStruct.Data3));
32+
memcpy(newUuid + 8, &uuidStruct.Data4, sizeof(uuidStruct.Data4));
33+
return Aws::Utils::UUID(newUuid);
34+
}
35+
}
36+
}

aws-cpp-sdk-core/source/utils/StringUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
using namespace Aws::Utils;
2626

27-
void StringUtils::Replace(Aws::String &s, const char* search, const char* replace)
27+
void StringUtils::Replace(Aws::String& s, const char* search, const char* replace)
2828
{
2929
if(!search || !replace)
3030
{
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
#include <aws/core/utils/UUID.h>
17+
#include <aws/core/utils/HashingUtils.h>
18+
#include <aws/core/utils/StringUtils.h>
19+
#include <iomanip>
20+
21+
namespace Aws
22+
{
23+
namespace Utils
24+
{
25+
void WriteRangeOutToStream(Aws::StringStream& ss, unsigned char* toWrite, size_t min, size_t max)
26+
{
27+
for (size_t i = min; i < max; ++i)
28+
{
29+
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2)
30+
<< (unsigned int)toWrite[i];
31+
}
32+
}
33+
34+
UUID::UUID(const Aws::String& uuidToConvert)
35+
{
36+
//GUID has 2 characters per byte + 4 dashes = 36 bytes
37+
assert(uuidToConvert.length() == UUID_STR_SIZE);
38+
memset(m_uuid, 0, sizeof(m_uuid));
39+
Aws::String escapedHexStr(uuidToConvert);
40+
StringUtils::Replace(escapedHexStr, "-", "");
41+
assert(escapedHexStr.length() == UUID_BINARY_SIZE * 2);
42+
ByteBuffer&& rawUuid = HashingUtils::HexDecode(escapedHexStr);
43+
memcpy(m_uuid, rawUuid.GetUnderlyingData(), rawUuid.GetLength());
44+
}
45+
46+
UUID::UUID(const unsigned char toCopy[UUID_BINARY_SIZE])
47+
{
48+
memcpy(m_uuid, toCopy, sizeof(m_uuid));
49+
}
50+
51+
UUID::operator Aws::String()
52+
{
53+
Aws::StringStream ss;
54+
WriteRangeOutToStream(ss, m_uuid, 0, 4);
55+
ss << "-";
56+
57+
WriteRangeOutToStream(ss, m_uuid, 4, 6);
58+
ss << "-";
59+
60+
WriteRangeOutToStream(ss, m_uuid, 6, 8);
61+
ss << "-";
62+
63+
WriteRangeOutToStream(ss, m_uuid, 8, 10);
64+
ss << "-";
65+
66+
WriteRangeOutToStream(ss, m_uuid, 10, 16);
67+
68+
return ss.str();
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)