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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 } | 212 } |
213 return true; | 213 return true; |
214 } | 214 } |
215 | 215 |
216 std::string CreateRandomString(size_t len) { | 216 std::string CreateRandomString(size_t len) { |
217 std::string str; | 217 std::string str; |
218 CreateRandomString(len, &str); | 218 CreateRandomString(len, &str); |
219 return str; | 219 return str; |
220 } | 220 } |
221 | 221 |
222 bool CreateRandomString(size_t len, | 222 static bool CreateRandomString(size_t len, |
223 const char* table, int table_size, | 223 const char* table, int table_size, |
224 std::string* str) { | 224 std::string* str) { |
225 str->clear(); | 225 str->clear(); |
| 226 // Avoid biased modulo division below. |
| 227 if (256 % table_size) { |
| 228 LOG(LS_ERROR) << "Table size must divide 256 evenly!"; |
| 229 return false; |
| 230 } |
226 std::unique_ptr<uint8_t[]> bytes(new uint8_t[len]); | 231 std::unique_ptr<uint8_t[]> bytes(new uint8_t[len]); |
227 if (!Rng().Generate(bytes.get(), len)) { | 232 if (!Rng().Generate(bytes.get(), len)) { |
228 LOG(LS_ERROR) << "Failed to generate random string!"; | 233 LOG(LS_ERROR) << "Failed to generate random string!"; |
229 return false; | 234 return false; |
230 } | 235 } |
231 str->reserve(len); | 236 str->reserve(len); |
232 for (size_t i = 0; i < len; ++i) { | 237 for (size_t i = 0; i < len; ++i) { |
233 str->push_back(table[bytes[i] % table_size]); | 238 str->push_back(table[bytes[i] % table_size]); |
234 } | 239 } |
235 return true; | 240 return true; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 } while (id == 0); | 311 } while (id == 0); |
307 return id; | 312 return id; |
308 } | 313 } |
309 | 314 |
310 double CreateRandomDouble() { | 315 double CreateRandomDouble() { |
311 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + | 316 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
312 std::numeric_limits<double>::epsilon()); | 317 std::numeric_limits<double>::epsilon()); |
313 } | 318 } |
314 | 319 |
315 } // namespace rtc | 320 } // namespace rtc |
OLD | NEW |