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