| 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 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // A test random generator, for predictable output. | 145 // A test random generator, for predictable output. |
| 146 class TestRandomGenerator : public RandomGenerator { | 146 class TestRandomGenerator : public RandomGenerator { |
| 147 public: | 147 public: |
| 148 TestRandomGenerator() : seed_(7) { | 148 TestRandomGenerator() : seed_(7) { |
| 149 } | 149 } |
| 150 ~TestRandomGenerator() override { | 150 ~TestRandomGenerator() override { |
| 151 } | 151 } |
| 152 bool Init(const void* seed, size_t len) override { return true; } | 152 bool Init(const void* seed, size_t len) override { return true; } |
| 153 bool Generate(void* buf, size_t len) override { | 153 bool Generate(void* buf, size_t len) override { |
| 154 for (size_t i = 0; i < len; ++i) { | 154 for (size_t i = 0; i < len; ++i) { |
| 155 static_cast<uint8*>(buf)[i] = static_cast<uint8>(GetRandom()); | 155 static_cast<uint8_t*>(buf)[i] = static_cast<uint8_t>(GetRandom()); |
| 156 } | 156 } |
| 157 return true; | 157 return true; |
| 158 } | 158 } |
| 159 | 159 |
| 160 private: | 160 private: |
| 161 int GetRandom() { | 161 int GetRandom() { |
| 162 return ((seed_ = seed_ * 214013L + 2531011L) >> 16) & 0x7fff; | 162 return ((seed_ = seed_ * 214013L + 2531011L) >> 16) & 0x7fff; |
| 163 } | 163 } |
| 164 int seed_; | 164 int seed_; |
| 165 }; | 165 }; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 std::string CreateRandomString(size_t len) { | 212 std::string CreateRandomString(size_t len) { |
| 213 std::string str; | 213 std::string str; |
| 214 CreateRandomString(len, &str); | 214 CreateRandomString(len, &str); |
| 215 return str; | 215 return str; |
| 216 } | 216 } |
| 217 | 217 |
| 218 bool CreateRandomString(size_t len, | 218 bool CreateRandomString(size_t len, |
| 219 const char* table, int table_size, | 219 const char* table, int table_size, |
| 220 std::string* str) { | 220 std::string* str) { |
| 221 str->clear(); | 221 str->clear(); |
| 222 scoped_ptr<uint8[]> bytes(new uint8[len]); | 222 scoped_ptr<uint8_t[]> bytes(new uint8_t[len]); |
| 223 if (!Rng().Generate(bytes.get(), len)) { | 223 if (!Rng().Generate(bytes.get(), len)) { |
| 224 LOG(LS_ERROR) << "Failed to generate random string!"; | 224 LOG(LS_ERROR) << "Failed to generate random string!"; |
| 225 return false; | 225 return false; |
| 226 } | 226 } |
| 227 str->reserve(len); | 227 str->reserve(len); |
| 228 for (size_t i = 0; i < len; ++i) { | 228 for (size_t i = 0; i < len; ++i) { |
| 229 str->push_back(table[bytes[i] % table_size]); | 229 str->push_back(table[bytes[i] % table_size]); |
| 230 } | 230 } |
| 231 return true; | 231 return true; |
| 232 } | 232 } |
| 233 | 233 |
| 234 bool CreateRandomString(size_t len, std::string* str) { | 234 bool CreateRandomString(size_t len, std::string* str) { |
| 235 return CreateRandomString(len, BASE64, 64, str); | 235 return CreateRandomString(len, BASE64, 64, str); |
| 236 } | 236 } |
| 237 | 237 |
| 238 bool CreateRandomString(size_t len, const std::string& table, | 238 bool CreateRandomString(size_t len, const std::string& table, |
| 239 std::string* str) { | 239 std::string* str) { |
| 240 return CreateRandomString(len, table.c_str(), | 240 return CreateRandomString(len, table.c_str(), |
| 241 static_cast<int>(table.size()), str); | 241 static_cast<int>(table.size()), str); |
| 242 } | 242 } |
| 243 | 243 |
| 244 uint32 CreateRandomId() { | 244 uint32_t CreateRandomId() { |
| 245 uint32 id; | 245 uint32_t id; |
| 246 if (!Rng().Generate(&id, sizeof(id))) { | 246 if (!Rng().Generate(&id, sizeof(id))) { |
| 247 LOG(LS_ERROR) << "Failed to generate random id!"; | 247 LOG(LS_ERROR) << "Failed to generate random id!"; |
| 248 } | 248 } |
| 249 return id; | 249 return id; |
| 250 } | 250 } |
| 251 | 251 |
| 252 uint64 CreateRandomId64() { | 252 uint64_t CreateRandomId64() { |
| 253 return static_cast<uint64>(CreateRandomId()) << 32 | CreateRandomId(); | 253 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); |
| 254 } | 254 } |
| 255 | 255 |
| 256 uint32 CreateRandomNonZeroId() { | 256 uint32_t CreateRandomNonZeroId() { |
| 257 uint32 id; | 257 uint32_t id; |
| 258 do { | 258 do { |
| 259 id = CreateRandomId(); | 259 id = CreateRandomId(); |
| 260 } while (id == 0); | 260 } while (id == 0); |
| 261 return id; | 261 return id; |
| 262 } | 262 } |
| 263 | 263 |
| 264 double CreateRandomDouble() { | 264 double CreateRandomDouble() { |
| 265 return CreateRandomId() / (std::numeric_limits<uint32>::max() + | 265 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
| 266 std::numeric_limits<double>::epsilon()); | 266 std::numeric_limits<double>::epsilon()); |
| 267 } | 267 } |
| 268 | 268 |
| 269 } // namespace rtc | 269 } // namespace rtc |
| OLD | NEW |