| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/helpers.h" | 11 #include "webrtc/base/helpers.h" |
| 12 | 12 |
| 13 #include <limits> | 13 #include <limits> |
| 14 | 14 |
| 15 #if defined(FEATURE_ENABLE_SSL) | 15 #if defined(FEATURE_ENABLE_SSL) |
| 16 #include "webrtc/base/sslconfig.h" | 16 #include "webrtc/base/sslconfig.h" |
| 17 #if defined(SSL_USE_OPENSSL) | 17 #if defined(SSL_USE_OPENSSL) |
| 18 #include <openssl/rand.h> | 18 #include <openssl/rand.h> |
| 19 #elif defined(SSL_USE_NSS_RNG) | 19 #elif defined(SSL_USE_NSS_RNG) |
| 20 // Hack: Define+undefine int64 and uint64 to avoid typedef conflict with NSS. | 20 // Hack: Define+undefine int64_t and uint64_t to avoid typedef conflict with |
| 21 // NSS. |
| 21 // TODO(kjellander): Remove when webrtc:4497 is completed. | 22 // TODO(kjellander): Remove when webrtc:4497 is completed. |
| 22 #define uint64 foo_uint64 | 23 #define uint64_t foo_uint64 |
| 23 #define int64 foo_int64 | 24 #define int64_t foo_int64 |
| 24 #include "pk11func.h" | 25 #include "pk11func.h" |
| 25 #undef uint64 | 26 #undef uint64_t |
| 26 #undef int64 | 27 #undef int64_t |
| 27 #else | 28 #else |
| 28 #if defined(WEBRTC_WIN) | 29 #if defined(WEBRTC_WIN) |
| 29 #define WIN32_LEAN_AND_MEAN | 30 #define WIN32_LEAN_AND_MEAN |
| 30 #include <windows.h> | 31 #include <windows.h> |
| 31 #include <ntsecapi.h> | 32 #include <ntsecapi.h> |
| 32 #endif // WEBRTC_WIN | 33 #endif // WEBRTC_WIN |
| 33 #endif // else | 34 #endif // else |
| 34 #endif // FEATURE_ENABLED_SSL | 35 #endif // FEATURE_ENABLED_SSL |
| 35 | 36 |
| 36 #include "webrtc/base/base64.h" | 37 #include "webrtc/base/base64.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 // A test random generator, for predictable output. | 154 // A test random generator, for predictable output. |
| 154 class TestRandomGenerator : public RandomGenerator { | 155 class TestRandomGenerator : public RandomGenerator { |
| 155 public: | 156 public: |
| 156 TestRandomGenerator() : seed_(7) { | 157 TestRandomGenerator() : seed_(7) { |
| 157 } | 158 } |
| 158 ~TestRandomGenerator() override { | 159 ~TestRandomGenerator() override { |
| 159 } | 160 } |
| 160 bool Init(const void* seed, size_t len) override { return true; } | 161 bool Init(const void* seed, size_t len) override { return true; } |
| 161 bool Generate(void* buf, size_t len) override { | 162 bool Generate(void* buf, size_t len) override { |
| 162 for (size_t i = 0; i < len; ++i) { | 163 for (size_t i = 0; i < len; ++i) { |
| 163 static_cast<uint8*>(buf)[i] = static_cast<uint8>(GetRandom()); | 164 static_cast<uint8_t*>(buf)[i] = static_cast<uint8_t>(GetRandom()); |
| 164 } | 165 } |
| 165 return true; | 166 return true; |
| 166 } | 167 } |
| 167 | 168 |
| 168 private: | 169 private: |
| 169 int GetRandom() { | 170 int GetRandom() { |
| 170 return ((seed_ = seed_ * 214013L + 2531011L) >> 16) & 0x7fff; | 171 return ((seed_ = seed_ * 214013L + 2531011L) >> 16) & 0x7fff; |
| 171 } | 172 } |
| 172 int seed_; | 173 int seed_; |
| 173 }; | 174 }; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 std::string CreateRandomString(size_t len) { | 221 std::string CreateRandomString(size_t len) { |
| 221 std::string str; | 222 std::string str; |
| 222 CreateRandomString(len, &str); | 223 CreateRandomString(len, &str); |
| 223 return str; | 224 return str; |
| 224 } | 225 } |
| 225 | 226 |
| 226 bool CreateRandomString(size_t len, | 227 bool CreateRandomString(size_t len, |
| 227 const char* table, int table_size, | 228 const char* table, int table_size, |
| 228 std::string* str) { | 229 std::string* str) { |
| 229 str->clear(); | 230 str->clear(); |
| 230 scoped_ptr<uint8[]> bytes(new uint8[len]); | 231 scoped_ptr<uint8_t[]> bytes(new uint8_t[len]); |
| 231 if (!Rng().Generate(bytes.get(), len)) { | 232 if (!Rng().Generate(bytes.get(), len)) { |
| 232 LOG(LS_ERROR) << "Failed to generate random string!"; | 233 LOG(LS_ERROR) << "Failed to generate random string!"; |
| 233 return false; | 234 return false; |
| 234 } | 235 } |
| 235 str->reserve(len); | 236 str->reserve(len); |
| 236 for (size_t i = 0; i < len; ++i) { | 237 for (size_t i = 0; i < len; ++i) { |
| 237 str->push_back(table[bytes[i] % table_size]); | 238 str->push_back(table[bytes[i] % table_size]); |
| 238 } | 239 } |
| 239 return true; | 240 return true; |
| 240 } | 241 } |
| 241 | 242 |
| 242 bool CreateRandomString(size_t len, std::string* str) { | 243 bool CreateRandomString(size_t len, std::string* str) { |
| 243 return CreateRandomString(len, BASE64, 64, str); | 244 return CreateRandomString(len, BASE64, 64, str); |
| 244 } | 245 } |
| 245 | 246 |
| 246 bool CreateRandomString(size_t len, const std::string& table, | 247 bool CreateRandomString(size_t len, const std::string& table, |
| 247 std::string* str) { | 248 std::string* str) { |
| 248 return CreateRandomString(len, table.c_str(), | 249 return CreateRandomString(len, table.c_str(), |
| 249 static_cast<int>(table.size()), str); | 250 static_cast<int>(table.size()), str); |
| 250 } | 251 } |
| 251 | 252 |
| 252 uint32 CreateRandomId() { | 253 uint32_t CreateRandomId() { |
| 253 uint32 id; | 254 uint32_t id; |
| 254 if (!Rng().Generate(&id, sizeof(id))) { | 255 if (!Rng().Generate(&id, sizeof(id))) { |
| 255 LOG(LS_ERROR) << "Failed to generate random id!"; | 256 LOG(LS_ERROR) << "Failed to generate random id!"; |
| 256 } | 257 } |
| 257 return id; | 258 return id; |
| 258 } | 259 } |
| 259 | 260 |
| 260 uint64 CreateRandomId64() { | 261 uint64_t CreateRandomId64() { |
| 261 return static_cast<uint64>(CreateRandomId()) << 32 | CreateRandomId(); | 262 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); |
| 262 } | 263 } |
| 263 | 264 |
| 264 uint32 CreateRandomNonZeroId() { | 265 uint32_t CreateRandomNonZeroId() { |
| 265 uint32 id; | 266 uint32_t id; |
| 266 do { | 267 do { |
| 267 id = CreateRandomId(); | 268 id = CreateRandomId(); |
| 268 } while (id == 0); | 269 } while (id == 0); |
| 269 return id; | 270 return id; |
| 270 } | 271 } |
| 271 | 272 |
| 272 double CreateRandomDouble() { | 273 double CreateRandomDouble() { |
| 273 return CreateRandomId() / (std::numeric_limits<uint32>::max() + | 274 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
| 274 std::numeric_limits<double>::epsilon()); | 275 std::numeric_limits<double>::epsilon()); |
| 275 } | 276 } |
| 276 | 277 |
| 277 } // namespace rtc | 278 } // namespace rtc |
| OLD | NEW |