Chromium Code Reviews| Index: webrtc/base/helpers.cc |
| diff --git a/webrtc/base/helpers.cc b/webrtc/base/helpers.cc |
| index 0a39ee923e1d61b3db9fa6399aee34049595fff6..15c687f73402c9e48a2dcfc5a447162a5f33f316 100644 |
| --- a/webrtc/base/helpers.cc |
| +++ b/webrtc/base/helpers.cc |
| @@ -28,6 +28,7 @@ |
| #include "webrtc/base/base64.h" |
| #include "webrtc/base/basictypes.h" |
| +#include "webrtc/base/checks.h" |
| #include "webrtc/base/logging.h" |
| #include "webrtc/base/timeutils.h" |
| @@ -215,7 +216,7 @@ bool InitRandom(const char* seed, size_t len) { |
| std::string CreateRandomString(size_t len) { |
| std::string str; |
| - CreateRandomString(len, &str); |
| + RTC_CHECK(CreateRandomString(len, &str)); |
| return str; |
| } |
| @@ -245,64 +246,106 @@ bool CreateRandomString(size_t len, const std::string& table, |
| static_cast<int>(table.size()), str); |
| } |
| +std::string CreateRandomUuid() { |
| + std::string uuid; |
| + RTC_CHECK(CreateRandomUuid(&uuid)); |
| + return uuid; |
| +} |
| + |
| // Version 4 UUID is of the form: |
| // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx |
| // Where 'x' is a hex digit, and 'y' is 8, 9, a or b. |
| -std::string CreateRandomUuid() { |
| - std::string str; |
| +bool CreateRandomUuid(std::string *str) { |
| + str->clear(); |
| std::unique_ptr<uint8_t[]> bytes(new uint8_t[31]); |
| if (!Rng().Generate(bytes.get(), 31)) { |
| LOG(LS_ERROR) << "Failed to generate random string!"; |
| - return str; |
| + return false; |
| } |
| - str.reserve(36); |
| + str->reserve(36); |
| for (size_t i = 0; i < 8; ++i) { |
| - str.push_back(kHex[bytes[i] % 16]); |
| + str->push_back(kHex[bytes[i] % 16]); |
| } |
| - str.push_back('-'); |
| + str->push_back('-'); |
| for (size_t i = 8; i < 12; ++i) { |
| - str.push_back(kHex[bytes[i] % 16]); |
| + str->push_back(kHex[bytes[i] % 16]); |
| } |
| - str.push_back('-'); |
| - str.push_back('4'); |
| + str->push_back('-'); |
| + str->push_back('4'); |
| for (size_t i = 12; i < 15; ++i) { |
| - str.push_back(kHex[bytes[i] % 16]); |
| + str->push_back(kHex[bytes[i] % 16]); |
| } |
| - str.push_back('-'); |
| - str.push_back(kUuidDigit17[bytes[15] % 4]); |
| + str->push_back('-'); |
| + str->push_back(kUuidDigit17[bytes[15] % 4]); |
| for (size_t i = 16; i < 19; ++i) { |
| - str.push_back(kHex[bytes[i] % 16]); |
| + str->push_back(kHex[bytes[i] % 16]); |
| } |
| - str.push_back('-'); |
| + str->push_back('-'); |
| for (size_t i = 19; i < 31; ++i) { |
| - str.push_back(kHex[bytes[i] % 16]); |
| + str->push_back(kHex[bytes[i] % 16]); |
| } |
| - return str; |
| + return true; |
| } |
| uint32_t CreateRandomId() { |
| uint32_t id; |
| - if (!Rng().Generate(&id, sizeof(id))) { |
| + RTC_CHECK(CreateRandomId(&id)); |
| + return id; |
| +} |
| + |
| +bool CreateRandomId(uint32_t* id) { |
| + if (!Rng().Generate(id, sizeof(*id))) { |
| LOG(LS_ERROR) << "Failed to generate random id!"; |
| + return false; |
| } |
| - return id; |
| + return true; |
| } |
| uint64_t CreateRandomId64() { |
| - return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); |
| + uint64_t id; |
| + RTC_CHECK(CreateRandomId64(&id)); |
| + return id; |
| +} |
| + |
| +bool CreateRandomId64(uint64_t* id) { |
| + uint32_t high; |
| + uint32_t low; |
| + if (!CreateRandomId(&high) || !CreateRandomId(&low)) { |
|
joachim
2016/07/01 23:12:29
Maybe get the uint64_t directly from the RNG?
|
| + return false; |
| + } |
| + *id = static_cast<uint64_t>(high) << 32 | low; |
| + return true; |
| } |
| uint32_t CreateRandomNonZeroId() { |
| uint32_t id; |
| - do { |
| - id = CreateRandomId(); |
| - } while (id == 0); |
| + RTC_CHECK(CreateRandomNonZeroId(&id)); |
| return id; |
| } |
| +bool CreateRandomNonZeroId(uint32_t* id) { |
| + do { |
| + if (!CreateRandomId(id)) { |
| + return false; |
| + } |
| + } while (*id == 0); |
| + return true; |
| +} |
| + |
| double CreateRandomDouble() { |
| - return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
| - std::numeric_limits<double>::epsilon()); |
| + double value; |
| + RTC_CHECK(CreateRandomDouble(&value)); |
| + return value; |
| +} |
| + |
| +bool CreateRandomDouble(double* value) { |
| + uint32_t id; |
| + if (!CreateRandomId(&id)) { |
| + return false; |
| + } |
| + *value = id / (std::numeric_limits<uint32_t>::max() + |
| + std::numeric_limits<double>::epsilon()); |
| + return true; |
| } |
| } // namespace rtc |