/work/toxcore/tox_random.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2022-2025 The TokTok team. |
3 | | */ |
4 | | #include "tox_random.h" |
5 | | |
6 | | #include "ccompat.h" |
7 | | #include "tox_memory.h" |
8 | | #include "tox_random_impl.h" |
9 | | |
10 | | Tox_Random *tox_random_new(const Tox_Random_Funcs *funcs, void *user_data, const Tox_Memory *mem) |
11 | 0 | { |
12 | 0 | Tox_Random *rng = (Tox_Random *)tox_memory_alloc(mem, sizeof(Tox_Random)); |
13 | |
|
14 | 0 | if (rng == nullptr) { |
15 | 0 | return nullptr; |
16 | 0 | } |
17 | | |
18 | 0 | rng->funcs = funcs; |
19 | 0 | rng->user_data = user_data; |
20 | |
|
21 | 0 | rng->mem = mem; |
22 | |
|
23 | 0 | return rng; |
24 | 0 | } |
25 | | |
26 | | void tox_random_free(Tox_Random *rng) |
27 | 0 | { |
28 | 0 | if (rng == nullptr || rng->mem == nullptr) { |
29 | 0 | return; |
30 | 0 | } |
31 | 0 | tox_memory_dealloc(rng->mem, rng); |
32 | 0 | } |
33 | | |
34 | | void tox_random_bytes(const Tox_Random *rng, uint8_t *bytes, uint32_t length) |
35 | 1.41M | { |
36 | 1.41M | rng->funcs->bytes_callback(rng->user_data, bytes, length); |
37 | 1.41M | } |
38 | | |
39 | | uint32_t tox_random_uniform(const Tox_Random *rng, uint32_t upper_bound) |
40 | 247k | { |
41 | 247k | return rng->funcs->uniform_callback(rng->user_data, upper_bound); |
42 | 247k | } |