[1] | 1 | /** @file
|
---|
[8245] | 2 | * IPRT - Random Numbers and Byte Streams.
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[76553] | 6 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
[1] | 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
|
---|
[5999] | 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.
|
---|
[1] | 24 | */
|
---|
| 25 |
|
---|
[76557] | 26 | #ifndef IPRT_INCLUDED_rand_h
|
---|
| 27 | #define IPRT_INCLUDED_rand_h
|
---|
[76507] | 28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 29 | # pragma once
|
---|
| 30 | #endif
|
---|
[1] | 31 |
|
---|
[889] | 32 | #include <iprt/cdefs.h>
|
---|
| 33 | #include <iprt/types.h>
|
---|
[1] | 34 |
|
---|
[20374] | 35 | RT_C_DECLS_BEGIN
|
---|
[889] | 36 |
|
---|
| 37 | /** @defgroup grp_rt_rand RTRand - Random Numbers and Byte Streams
|
---|
| 38 | * @ingroup grp_rt
|
---|
| 39 | * @{
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Fills a buffer with random bytes.
|
---|
| 44 | *
|
---|
| 45 | * @param pv Where to store the random bytes.
|
---|
| 46 | * @param cb Number of bytes to generate.
|
---|
| 47 | */
|
---|
[57432] | 48 | RTDECL(void) RTRandBytes(void *pv, size_t cb) RT_NO_THROW_PROTO;
|
---|
[889] | 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Generate a 32-bit signed random number in the set [i32First..i32Last].
|
---|
[7170] | 52 | *
|
---|
[889] | 53 | * @returns The random number.
|
---|
| 54 | * @param i32First First number in the set.
|
---|
| 55 | * @param i32Last Last number in the set.
|
---|
| 56 | */
|
---|
[57432] | 57 | RTDECL(int32_t) RTRandS32Ex(int32_t i32First, int32_t i32Last) RT_NO_THROW_PROTO;
|
---|
[889] | 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Generate a 32-bit signed random number.
|
---|
[7170] | 61 | *
|
---|
[889] | 62 | * @returns The random number.
|
---|
| 63 | */
|
---|
[57432] | 64 | RTDECL(int32_t) RTRandS32(void) RT_NO_THROW_PROTO;
|
---|
[889] | 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Generate a 32-bit unsigned random number in the set [u32First..u32Last].
|
---|
[7170] | 68 | *
|
---|
[889] | 69 | * @returns The random number.
|
---|
| 70 | * @param u32First First number in the set.
|
---|
| 71 | * @param u32Last Last number in the set.
|
---|
| 72 | */
|
---|
[57432] | 73 | RTDECL(uint32_t) RTRandU32Ex(uint32_t u32First, uint32_t u32Last) RT_NO_THROW_PROTO;
|
---|
[889] | 74 |
|
---|
| 75 | /**
|
---|
| 76 | * Generate a 32-bit unsigned random number.
|
---|
[7170] | 77 | *
|
---|
[889] | 78 | * @returns The random number.
|
---|
| 79 | */
|
---|
[57432] | 80 | RTDECL(uint32_t) RTRandU32(void) RT_NO_THROW_PROTO;
|
---|
[889] | 81 |
|
---|
| 82 | /**
|
---|
[7170] | 83 | * Generate a 64-bit signed random number in the set [i64First..i64Last].
|
---|
| 84 | *
|
---|
[889] | 85 | * @returns The random number.
|
---|
[7170] | 86 | * @param i64First First number in the set.
|
---|
| 87 | * @param i64Last Last number in the set.
|
---|
[889] | 88 | */
|
---|
[57432] | 89 | RTDECL(int64_t) RTRandS64Ex(int64_t i64First, int64_t i64Last) RT_NO_THROW_PROTO;
|
---|
[889] | 90 |
|
---|
| 91 | /**
|
---|
| 92 | * Generate a 64-bit signed random number.
|
---|
[7170] | 93 | *
|
---|
[889] | 94 | * @returns The random number.
|
---|
| 95 | */
|
---|
[57432] | 96 | RTDECL(int64_t) RTRandS64(void) RT_NO_THROW_PROTO;
|
---|
[889] | 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Generate a 64-bit unsigned random number in the set [u64First..u64Last].
|
---|
[7170] | 100 | *
|
---|
[889] | 101 | * @returns The random number.
|
---|
| 102 | * @param u64First First number in the set.
|
---|
| 103 | * @param u64Last Last number in the set.
|
---|
| 104 | */
|
---|
[57432] | 105 | RTDECL(uint64_t) RTRandU64Ex(uint64_t u64First, uint64_t u64Last) RT_NO_THROW_PROTO;
|
---|
[889] | 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Generate a 64-bit unsigned random number.
|
---|
[7170] | 109 | *
|
---|
[889] | 110 | * @returns The random number.
|
---|
| 111 | */
|
---|
[57432] | 112 | RTDECL(uint64_t) RTRandU64(void) RT_NO_THROW_PROTO;
|
---|
[889] | 113 |
|
---|
[11347] | 114 |
|
---|
| 115 | /**
|
---|
| 116 | * Create an instance of the default random number generator.
|
---|
| 117 | *
|
---|
| 118 | * @returns IPRT status code.
|
---|
[25645] | 119 | * @param phRand Where to return the handle to the new random number
|
---|
| 120 | * generator.
|
---|
[11347] | 121 | */
|
---|
[57432] | 122 | RTDECL(int) RTRandAdvCreate(PRTRAND phRand) RT_NO_THROW_PROTO;
|
---|
[11347] | 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 | */
|
---|
[57432] | 130 | RTDECL(int) RTRandAdvCreatePseudo(PRTRAND phRand) RT_NO_THROW_PROTO;
|
---|
[11347] | 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 | */
|
---|
[57432] | 138 | RTDECL(int) RTRandAdvCreateParkMiller(PRTRAND phRand) RT_NO_THROW_PROTO;
|
---|
[11347] | 139 |
|
---|
| 140 | /**
|
---|
[11558] | 141 | * Create an instance of the faster random number generator for the OS.
|
---|
[11523] | 142 | *
|
---|
| 143 | * @returns IPRT status code.
|
---|
[11558] | 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 | *
|
---|
[11523] | 149 | * @param phRand Where to store the handle to the generator.
|
---|
| 150 | *
|
---|
| 151 | * @remarks Think /dev/urandom.
|
---|
| 152 | */
|
---|
[57432] | 153 | RTDECL(int) RTRandAdvCreateSystemFaster(PRTRAND phRand) RT_NO_THROW_PROTO;
|
---|
[11523] | 154 |
|
---|
| 155 | /**
|
---|
[11558] | 156 | * Create an instance of the truer random number generator for the OS.
|
---|
[11523] | 157 | *
|
---|
| 158 | * Don't use this unless you seriously need good random numbers because most
|
---|
[11558] | 159 | * systems will have will have problems producing sufficient entropy for this
|
---|
| 160 | * and you'll end up blocking while it accumulates.
|
---|
[11523] | 161 | *
|
---|
| 162 | * @returns IPRT status code.
|
---|
[11558] | 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 | *
|
---|
[11523] | 168 | * @param phRand Where to store the handle to the generator.
|
---|
| 169 | *
|
---|
| 170 | * @remarks Think /dev/random.
|
---|
| 171 | */
|
---|
[57432] | 172 | RTDECL(int) RTRandAdvCreateSystemTruer(PRTRAND phRand) RT_NO_THROW_PROTO;
|
---|
[11523] | 173 |
|
---|
| 174 | /**
|
---|
[11347] | 175 | * Destroys a random number generator.
|
---|
| 176 | *
|
---|
| 177 | * @returns IPRT status code.
|
---|
| 178 | * @param hRand Handle to the random number generator.
|
---|
| 179 | */
|
---|
[57432] | 180 | RTDECL(int) RTRandAdvDestroy(RTRAND hRand) RT_NO_THROW_PROTO;
|
---|
[11347] | 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
|
---|
[33540] | 187 | * over the result.
|
---|
[11347] | 188 | *
|
---|
| 189 | * @returns IPRT status code.
|
---|
[11523] | 190 | * @retval VERR_NOT_SUPPORTED if it isn't a pseudo generator.
|
---|
| 191 | *
|
---|
[11347] | 192 | * @param hRand Handle to the random number generator.
|
---|
| 193 | * @param u64Seed Seed.
|
---|
| 194 | */
|
---|
[57432] | 195 | RTDECL(int) RTRandAdvSeed(RTRAND hRand, uint64_t u64Seed) RT_NO_THROW_PROTO;
|
---|
[11347] | 196 |
|
---|
| 197 | /**
|
---|
[11523] | 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 | */
|
---|
[57432] | 217 | RTDECL(int) RTRandAdvSaveState(RTRAND hRand, char *pszState, size_t *pcbState) RT_NO_THROW_PROTO;
|
---|
[11523] | 218 |
|
---|
| 219 | /**
|
---|
| 220 | * Restores the state of a pseudo generator.
|
---|
| 221 | *
|
---|
[33540] | 222 | * The state must have been obtained using RTRandAdvGetState.
|
---|
[11523] | 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 | */
|
---|
[57432] | 231 | RTDECL(int) RTRandAdvRestoreState(RTRAND hRand, char const *pszState) RT_NO_THROW_PROTO;
|
---|
[11523] | 232 |
|
---|
| 233 | /**
|
---|
[11347] | 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 | */
|
---|
[57432] | 240 | RTDECL(void) RTRandAdvBytes(RTRAND hRand, void *pv, size_t cb) RT_NO_THROW_PROTO;
|
---|
[11347] | 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 | */
|
---|
[57432] | 250 | RTDECL(int32_t) RTRandAdvS32Ex(RTRAND hRand, int32_t i32First, int32_t i32Last) RT_NO_THROW_PROTO;
|
---|
[11347] | 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 | */
|
---|
[57432] | 258 | RTDECL(int32_t) RTRandAdvS32(RTRAND hRand) RT_NO_THROW_PROTO;
|
---|
[11347] | 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 | */
|
---|
[57432] | 268 | RTDECL(uint32_t) RTRandAdvU32Ex(RTRAND hRand, uint32_t u32First, uint32_t u32Last) RT_NO_THROW_PROTO;
|
---|
[11347] | 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 | */
|
---|
[57432] | 276 | RTDECL(uint32_t) RTRandAdvU32(RTRAND hRand) RT_NO_THROW_PROTO;
|
---|
[11347] | 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 | */
|
---|
[57432] | 286 | RTDECL(int64_t) RTRandAdvS64Ex(RTRAND hRand, int64_t i64First, int64_t i64Last) RT_NO_THROW_PROTO;
|
---|
[11347] | 287 |
|
---|
| 288 | /**
|
---|
| 289 | * Generate a 64-bit signed random number.
|
---|
| 290 | *
|
---|
| 291 | * @returns The random number.
|
---|
| 292 | */
|
---|
[57432] | 293 | RTDECL(int64_t) RTRandAdvS64(RTRAND hRand) RT_NO_THROW_PROTO;
|
---|
[11347] | 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 | */
|
---|
[57432] | 303 | RTDECL(uint64_t) RTRandAdvU64Ex(RTRAND hRand, uint64_t u64First, uint64_t u64Last) RT_NO_THROW_PROTO;
|
---|
[11347] | 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 | */
|
---|
[57432] | 311 | RTDECL(uint64_t) RTRandAdvU64(RTRAND hRand) RT_NO_THROW_PROTO;
|
---|
[11347] | 312 |
|
---|
| 313 |
|
---|
[889] | 314 | /** @} */
|
---|
| 315 |
|
---|
[20374] | 316 | RT_C_DECLS_END
|
---|
[889] | 317 |
|
---|
| 318 |
|
---|
[76585] | 319 | #endif /* !IPRT_INCLUDED_rand_h */
|
---|
[1] | 320 |
|
---|