1 | /** @file
|
---|
2 | * IPRT - Random Numbers and Byte Streams.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_rand_h
|
---|
27 | #define ___iprt_rand_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 |
|
---|
32 | RT_C_DECLS_BEGIN
|
---|
33 |
|
---|
34 | /** @defgroup grp_rt_rand RTRand - Random Numbers and Byte Streams
|
---|
35 | * @ingroup grp_rt
|
---|
36 | * @{
|
---|
37 | */
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Fills a buffer with random bytes.
|
---|
41 | *
|
---|
42 | * @param pv Where to store the random bytes.
|
---|
43 | * @param cb Number of bytes to generate.
|
---|
44 | */
|
---|
45 | RTDECL(void) RTRandBytes(void *pv, size_t cb) RT_NO_THROW;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Generate a 32-bit signed random number in the set [i32First..i32Last].
|
---|
49 | *
|
---|
50 | * @returns The random number.
|
---|
51 | * @param i32First First number in the set.
|
---|
52 | * @param i32Last Last number in the set.
|
---|
53 | */
|
---|
54 | RTDECL(int32_t) RTRandS32Ex(int32_t i32First, int32_t i32Last) RT_NO_THROW;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Generate a 32-bit signed random number.
|
---|
58 | *
|
---|
59 | * @returns The random number.
|
---|
60 | */
|
---|
61 | RTDECL(int32_t) RTRandS32(void) RT_NO_THROW;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Generate a 32-bit unsigned random number in the set [u32First..u32Last].
|
---|
65 | *
|
---|
66 | * @returns The random number.
|
---|
67 | * @param u32First First number in the set.
|
---|
68 | * @param u32Last Last number in the set.
|
---|
69 | */
|
---|
70 | RTDECL(uint32_t) RTRandU32Ex(uint32_t u32First, uint32_t u32Last) RT_NO_THROW;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Generate a 32-bit unsigned random number.
|
---|
74 | *
|
---|
75 | * @returns The random number.
|
---|
76 | */
|
---|
77 | RTDECL(uint32_t) RTRandU32(void) RT_NO_THROW;
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Generate a 64-bit signed random number in the set [i64First..i64Last].
|
---|
81 | *
|
---|
82 | * @returns The random number.
|
---|
83 | * @param i64First First number in the set.
|
---|
84 | * @param i64Last Last number in the set.
|
---|
85 | */
|
---|
86 | RTDECL(int64_t) RTRandS64Ex(int64_t i64First, int64_t i64Last) RT_NO_THROW;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Generate a 64-bit signed random number.
|
---|
90 | *
|
---|
91 | * @returns The random number.
|
---|
92 | */
|
---|
93 | RTDECL(int64_t) RTRandS64(void) RT_NO_THROW;
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Generate a 64-bit unsigned random number in the set [u64First..u64Last].
|
---|
97 | *
|
---|
98 | * @returns The random number.
|
---|
99 | * @param u64First First number in the set.
|
---|
100 | * @param u64Last Last number in the set.
|
---|
101 | */
|
---|
102 | RTDECL(uint64_t) RTRandU64Ex(uint64_t u64First, uint64_t u64Last) RT_NO_THROW;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Generate a 64-bit unsigned random number.
|
---|
106 | *
|
---|
107 | * @returns The random number.
|
---|
108 | */
|
---|
109 | RTDECL(uint64_t) RTRandU64(void) RT_NO_THROW;
|
---|
110 |
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Create an instance of the default random number generator.
|
---|
114 | *
|
---|
115 | * @returns IPRT status code.
|
---|
116 | * @param phRand Where to return the handle to the new random number
|
---|
117 | * generator.
|
---|
118 | */
|
---|
119 | RTDECL(int) RTRandAdvCreate(PRTRAND phRand) RT_NO_THROW;
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Create an instance of the default pseudo random number generator.
|
---|
123 | *
|
---|
124 | * @returns IPRT status code.
|
---|
125 | * @param phRand Where to store the handle to the generator.
|
---|
126 | */
|
---|
127 | RTDECL(int) RTRandAdvCreatePseudo(PRTRAND phRand) RT_NO_THROW;
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Create an instance of the Park-Miller pseudo random number generator.
|
---|
131 | *
|
---|
132 | * @returns IPRT status code.
|
---|
133 | * @param phRand Where to store the handle to the generator.
|
---|
134 | */
|
---|
135 | RTDECL(int) RTRandAdvCreateParkMiller(PRTRAND phRand) RT_NO_THROW;
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Create an instance of the faster random number generator for the OS.
|
---|
139 | *
|
---|
140 | * @returns IPRT status code.
|
---|
141 | * @retval VERR_NOT_SUPPORTED on platforms which doesn't have this feature.
|
---|
142 | * @retval VERR_FILE_NOT_FOUND on system where the random generator hasn't
|
---|
143 | * been installed or configured correctly.
|
---|
144 | * @retval VERR_PATH_NOT_FOUND for the same reasons as VERR_FILE_NOT_FOUND.
|
---|
145 | *
|
---|
146 | * @param phRand Where to store the handle to the generator.
|
---|
147 | *
|
---|
148 | * @remarks Think /dev/urandom.
|
---|
149 | */
|
---|
150 | RTDECL(int) RTRandAdvCreateSystemFaster(PRTRAND phRand) RT_NO_THROW;
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Create an instance of the truer random number generator for the OS.
|
---|
154 | *
|
---|
155 | * Don't use this unless you seriously need good random numbers because most
|
---|
156 | * systems will have will have problems producing sufficient entropy for this
|
---|
157 | * and you'll end up blocking while it accumulates.
|
---|
158 | *
|
---|
159 | * @returns IPRT status code.
|
---|
160 | * @retval VERR_NOT_SUPPORTED on platforms which doesn't have this feature.
|
---|
161 | * @retval VERR_FILE_NOT_FOUND on system where the random generator hasn't
|
---|
162 | * been installed or configured correctly.
|
---|
163 | * @retval VERR_PATH_NOT_FOUND for the same reasons as VERR_FILE_NOT_FOUND.
|
---|
164 | *
|
---|
165 | * @param phRand Where to store the handle to the generator.
|
---|
166 | *
|
---|
167 | * @remarks Think /dev/random.
|
---|
168 | */
|
---|
169 | RTDECL(int) RTRandAdvCreateSystemTruer(PRTRAND phRand) RT_NO_THROW;
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Destroys a random number generator.
|
---|
173 | *
|
---|
174 | * @returns IPRT status code.
|
---|
175 | * @param hRand Handle to the random number generator.
|
---|
176 | */
|
---|
177 | RTDECL(int) RTRandAdvDestroy(RTRAND hRand) RT_NO_THROW;
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Generic method for seeding of a random number generator.
|
---|
181 | *
|
---|
182 | * The different generators may have specialized methods for
|
---|
183 | * seeding, use one of those if you desire better control
|
---|
184 | * over the result.
|
---|
185 | *
|
---|
186 | * @returns IPRT status code.
|
---|
187 | * @retval VERR_NOT_SUPPORTED if it isn't a pseudo generator.
|
---|
188 | *
|
---|
189 | * @param hRand Handle to the random number generator.
|
---|
190 | * @param u64Seed Seed.
|
---|
191 | */
|
---|
192 | RTDECL(int) RTRandAdvSeed(RTRAND hRand, uint64_t u64Seed) RT_NO_THROW;
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Save the current state of a pseudo generator.
|
---|
196 | *
|
---|
197 | * This can be use to save the state so it can later be resumed at the same
|
---|
198 | * position.
|
---|
199 | *
|
---|
200 | * @returns IPRT status code.
|
---|
201 | * @retval VINF_SUCCESS on success. *pcbState contains the length of the
|
---|
202 | * returned string and pszState contains the state string.
|
---|
203 | * @retval VERR_BUFFER_OVERFLOW if the supplied buffer is too small. *pcbState
|
---|
204 | * will contain the necessary buffer size.
|
---|
205 | * @retval VERR_NOT_SUPPORTED by non-psuedo generators.
|
---|
206 | *
|
---|
207 | * @param hRand Handle to the random number generator.
|
---|
208 | * @param pszState Where to store the state. The returned string will be
|
---|
209 | * null terminated and printable.
|
---|
210 | * @param pcbState The size of the buffer pszState points to on input, the
|
---|
211 | * size required / used on return (including the
|
---|
212 | * terminator, thus the 'cb' instead of 'cch').
|
---|
213 | */
|
---|
214 | RTDECL(int) RTRandAdvSaveState(RTRAND hRand, char *pszState, size_t *pcbState) RT_NO_THROW;
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Restores the state of a pseudo generator.
|
---|
218 | *
|
---|
219 | * The state must have been obtained using RTRandAdvGetState.
|
---|
220 | *
|
---|
221 | * @returns IPRT status code.
|
---|
222 | * @retval VERR_PARSE_ERROR if the state string is malformed.
|
---|
223 | * @retval VERR_NOT_SUPPORTED by non-psuedo generators.
|
---|
224 | *
|
---|
225 | * @param hRand Handle to the random number generator.
|
---|
226 | * @param pszState The state to load.
|
---|
227 | */
|
---|
228 | RTDECL(int) RTRandAdvRestoreState(RTRAND hRand, char const *pszState) RT_NO_THROW;
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Fills a buffer with random bytes.
|
---|
232 | *
|
---|
233 | * @param hRand Handle to the random number generator.
|
---|
234 | * @param pv Where to store the random bytes.
|
---|
235 | * @param cb Number of bytes to generate.
|
---|
236 | */
|
---|
237 | RTDECL(void) RTRandAdvBytes(RTRAND hRand, void *pv, size_t cb) RT_NO_THROW;
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Generate a 32-bit signed random number in the set [i32First..i32Last].
|
---|
241 | *
|
---|
242 | * @returns The random number.
|
---|
243 | * @param hRand Handle to the random number generator.
|
---|
244 | * @param i32First First number in the set.
|
---|
245 | * @param i32Last Last number in the set.
|
---|
246 | */
|
---|
247 | RTDECL(int32_t) RTRandAdvS32Ex(RTRAND hRand, int32_t i32First, int32_t i32Last) RT_NO_THROW;
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Generate a 32-bit signed random number.
|
---|
251 | *
|
---|
252 | * @returns The random number.
|
---|
253 | * @param hRand Handle to the random number generator.
|
---|
254 | */
|
---|
255 | RTDECL(int32_t) RTRandAdvS32(RTRAND hRand) RT_NO_THROW;
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Generate a 32-bit unsigned random number in the set [u32First..u32Last].
|
---|
259 | *
|
---|
260 | * @returns The random number.
|
---|
261 | * @param hRand Handle to the random number generator.
|
---|
262 | * @param u32First First number in the set.
|
---|
263 | * @param u32Last Last number in the set.
|
---|
264 | */
|
---|
265 | RTDECL(uint32_t) RTRandAdvU32Ex(RTRAND hRand, uint32_t u32First, uint32_t u32Last) RT_NO_THROW;
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Generate a 32-bit unsigned random number.
|
---|
269 | *
|
---|
270 | * @returns The random number.
|
---|
271 | * @param hRand Handle to the random number generator.
|
---|
272 | */
|
---|
273 | RTDECL(uint32_t) RTRandAdvU32(RTRAND hRand) RT_NO_THROW;
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Generate a 64-bit signed random number in the set [i64First..i64Last].
|
---|
277 | *
|
---|
278 | * @returns The random number.
|
---|
279 | * @param hRand Handle to the random number generator.
|
---|
280 | * @param i64First First number in the set.
|
---|
281 | * @param i64Last Last number in the set.
|
---|
282 | */
|
---|
283 | RTDECL(int64_t) RTRandAdvS64Ex(RTRAND hRand, int64_t i64First, int64_t i64Last) RT_NO_THROW;
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Generate a 64-bit signed random number.
|
---|
287 | *
|
---|
288 | * @returns The random number.
|
---|
289 | */
|
---|
290 | RTDECL(int64_t) RTRandAdvS64(RTRAND hRand) RT_NO_THROW;
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Generate a 64-bit unsigned random number in the set [u64First..u64Last].
|
---|
294 | *
|
---|
295 | * @returns The random number.
|
---|
296 | * @param hRand Handle to the random number generator.
|
---|
297 | * @param u64First First number in the set.
|
---|
298 | * @param u64Last Last number in the set.
|
---|
299 | */
|
---|
300 | RTDECL(uint64_t) RTRandAdvU64Ex(RTRAND hRand, uint64_t u64First, uint64_t u64Last) RT_NO_THROW;
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Generate a 64-bit unsigned random number.
|
---|
304 | *
|
---|
305 | * @returns The random number.
|
---|
306 | * @param hRand Handle to the random number generator.
|
---|
307 | */
|
---|
308 | RTDECL(uint64_t) RTRandAdvU64(RTRAND hRand) RT_NO_THROW;
|
---|
309 |
|
---|
310 |
|
---|
311 | /** @} */
|
---|
312 |
|
---|
313 | RT_C_DECLS_END
|
---|
314 |
|
---|
315 |
|
---|
316 | #endif
|
---|
317 |
|
---|