henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_HELPERS_H_ |
| 12 | #define RTC_BASE_HELPERS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | namespace rtc { |
| 19 | |
| 20 | // For testing, we can return predictable data. |
| 21 | void SetRandomTestMode(bool test); |
| 22 | |
| 23 | // Initializes the RNG, and seeds it with the specified entropy. |
| 24 | bool InitRandom(int seed); |
| 25 | bool InitRandom(const char* seed, size_t len); |
| 26 | |
| 27 | // Generates a (cryptographically) random string of the given length. |
| 28 | // We generate base64 values so that they will be printable. |
| 29 | std::string CreateRandomString(size_t length); |
| 30 | |
| 31 | // Generates a (cryptographically) random string of the given length. |
| 32 | // We generate base64 values so that they will be printable. |
| 33 | // Return false if the random number generator failed. |
| 34 | bool CreateRandomString(size_t length, std::string* str); |
| 35 | |
| 36 | // Generates a (cryptographically) random string of the given length, |
| 37 | // with characters from the given table. Return false if the random |
| 38 | // number generator failed. |
| 39 | // For ease of implementation, the function requires that the table |
| 40 | // size evenly divide 256; otherwise, it returns false. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 41 | bool CreateRandomString(size_t length, |
| 42 | const std::string& table, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | std::string* str); |
| 44 | |
| 45 | // Generates (cryptographically) random data of the given length. |
| 46 | // Return false if the random number generator failed. |
| 47 | bool CreateRandomData(size_t length, std::string* data); |
| 48 | |
| 49 | // Generates a (cryptographically) random UUID version 4 string. |
| 50 | std::string CreateRandomUuid(); |
| 51 | |
| 52 | // Generates a random id. |
| 53 | uint32_t CreateRandomId(); |
| 54 | |
| 55 | // Generates a 64 bit random id. |
| 56 | uint64_t CreateRandomId64(); |
| 57 | |
| 58 | // Generates a random id > 0. |
| 59 | uint32_t CreateRandomNonZeroId(); |
| 60 | |
| 61 | // Generates a random double between 0.0 (inclusive) and 1.0 (exclusive). |
| 62 | double CreateRandomDouble(); |
| 63 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 64 | // Compute moving average with the given ratio between the previous average |
| 65 | // value and the current value. |
| 66 | double GetNextMovingAverage(double prev_average, double cur, double ratio); |
| 67 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 68 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 70 | #endif // RTC_BASE_HELPERS_H_ |