| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "types_internal.hpp" |
| 21 |
|
| 22 |
#include "proton/uuid.hpp" |
| 23 |
#include "proton/types_fwd.hpp" |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <chrono> |
| 27 |
#include <climits> |
| 28 |
#include <cstdlib> |
| 29 |
#include <ctime> |
| 30 |
#include <functional> |
| 31 |
#include <iomanip> |
| 32 |
#include <random> |
| 33 |
#include <sstream> |
| 34 |
|
| 35 |
#if defined(_WIN32) |
| 36 |
#include <process.h> |
| 37 |
#define GETPID _getpid |
| 38 |
#else |
| 39 |
#include <unistd.h> |
| 40 |
#define GETPID getpid |
| 41 |
#endif |
| 42 |
|
| 43 |
namespace proton { |
| 44 |
|
| 45 |
namespace { |
| 46 |
|
| 47 |
thread_local std::independent_bits_engine<std::mt19937, CHAR_BIT, unsigned int> engine = [] { |
| 48 |
// A hash of time, PID and random_device to produce an unsigned integer |
| 49 |
// for seeding the engine. |
| 50 |
std::seed_seq seq{ |
| 51 |
(unsigned long)std::chrono::system_clock::now().time_since_epoch().count(), |
| 52 |
(unsigned long)GETPID(), |
| 53 |
(unsigned long)std::random_device{}()}; |
| 54 |
|
| 55 |
return |
| 56 |
std::independent_bits_engine<std::mt19937, CHAR_BIT, unsigned int>(seq); |
| 57 |
}(); |
| 58 |
|
| 59 |
} // namespace |
| 60 |
|
| 61 |
uuid uuid::copy() { |
| 62 |
uuid u; |
| 63 |
std::fill(u.begin(), u.end(), 0); |
| 64 |
return u; |
| 65 |
} |
| 66 |
|
| 67 |
uuid uuid::copy(const char* bytes) { |
| 68 |
uuid u; |
| 69 |
if (bytes) |
| 70 |
std::copy(bytes, bytes + u.size(), u.begin()); |
| 71 |
else |
| 72 |
std::fill(u.begin(), u.end(), 0); |
| 73 |
return u; |
| 74 |
} |
| 75 |
|
| 76 |
uuid uuid::random() { |
| 77 |
uuid bytes; |
| 78 |
std::generate(bytes.begin(), bytes.end(), std::ref(engine)); |
| 79 |
|
| 80 |
// From RFC4122, the version bits are set to 0100 |
| 81 |
bytes[6] = (bytes[6] & 0x0F) | 0x40; |
| 82 |
|
| 83 |
// From RFC4122, the top two bits of byte 8 get set to 01 |
| 84 |
bytes[8] = (bytes[8] & 0x3F) | 0x80; |
| 85 |
return bytes; |
| 86 |
} |
| 87 |
|
| 88 |
/// UUID standard format: 8-4-4-4-12 (36 chars, 32 alphanumeric and 4 hyphens) |
| 89 |
std::ostream& operator<<(std::ostream& o, const uuid& u) { |
| 90 |
ios_guard restore_flags(o); |
| 91 |
o << std::hex << std::setfill('0'); |
| 92 |
static const int segments[] = {4,2,2,2,6}; // 1 byte is 2 hex chars. |
| 93 |
const uint8_t *p = reinterpret_cast<const uint8_t*>(u.begin()); |
| 94 |
for (size_t i = 0; i < sizeof(segments)/sizeof(segments[0]); ++i) { |
| 95 |
if (i > 0) |
| 96 |
o << '-'; |
| 97 |
for (int j = 0; j < segments[i]; ++j) { |
| 98 |
o << std::setw(2) << printable_byte(*(p++)); |
| 99 |
} |
| 100 |
} |
| 101 |
return o; |
| 102 |
} |
| 103 |
|
| 104 |
std::string uuid::str() const { |
| 105 |
std::ostringstream s; |
| 106 |
s << *this; |
| 107 |
return s.str(); |
| 108 |
} |
| 109 |
|
| 110 |
} |