1 | /* $Id: rand.cpp 327 2007-01-25 19:20:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Random Number
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <iprt/rand.h>
|
---|
27 | #include <iprt/time.h>
|
---|
28 | #include <iprt/asm.h>
|
---|
29 | #include <iprt/err.h>
|
---|
30 | #include "internal/rand.h"
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | /*******************************************************************************
|
---|
35 | * Global Variables *
|
---|
36 | *******************************************************************************/
|
---|
37 | /** Lazy init. */
|
---|
38 | static volatile bool g_fInitialized = false;
|
---|
39 |
|
---|
40 |
|
---|
41 | static void rtRandLazyInit(void)
|
---|
42 | {
|
---|
43 | /*
|
---|
44 | * Call host specific init.
|
---|
45 | */
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Seed the fallback random code.
|
---|
49 | */
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Internal wrapper for the native and generic random byte methods.
|
---|
55 | *
|
---|
56 | * @param pv Where to store the random bytes.
|
---|
57 | * @param cb Number of bytes to generate.
|
---|
58 | */
|
---|
59 | static void rtRandGenBytes(void *pv, size_t cb)
|
---|
60 | {
|
---|
61 | if (RT_UNLIKELY(!g_fInitialized))
|
---|
62 | rtRandLazyInit();
|
---|
63 |
|
---|
64 | int rc = rtRandGenBytesNative(pv, cb);
|
---|
65 | if (RT_FAILURE(rc))
|
---|
66 | {
|
---|
67 | /* fallback */
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | RTDECL(int32_t) RTRandS32Ex(uint32_t i32First, int32_t i32Last)
|
---|
73 | {
|
---|
74 | /* get 4 random bytes. */
|
---|
75 | union
|
---|
76 | {
|
---|
77 | uint32_t off;
|
---|
78 | uint8_t ab[4];
|
---|
79 | } u;
|
---|
80 | rtRandGenBytes(&u.ab, sizeof(u));
|
---|
81 |
|
---|
82 |
|
---|
83 | /* squeeze it into the requested range. */
|
---|
84 | uint32_t offLast = i32Last - i32First;
|
---|
85 | if (u.off > offLasts)
|
---|
86 | {
|
---|
87 | do
|
---|
88 | {
|
---|
89 | u.off >>= 1;
|
---|
90 | } while (u.off > offLasts);
|
---|
91 | }
|
---|
92 | return i32First + u.off;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | RTDECL(int32_t) RTRandS32(void)
|
---|
97 | {
|
---|
98 | return RTRandS32Ex(INT32_MIN, INT32_MAX);
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(uint32_t) RTRandU32Ex(uint32_t u32First, uint32_t u32Last)
|
---|
103 | {
|
---|
104 | /* get 4 random bytes. */
|
---|
105 | union
|
---|
106 | {
|
---|
107 | uint32_t off;
|
---|
108 | uint8_t ab[4];
|
---|
109 | } u;
|
---|
110 | rtRandGenBytes(&u.ab, sizeof(u));
|
---|
111 |
|
---|
112 |
|
---|
113 | /* squeeze it into the requested range. */
|
---|
114 | const uint32_t offLast = u32Last - u32First;
|
---|
115 | if (u.off > offLasts)
|
---|
116 | {
|
---|
117 | do
|
---|
118 | {
|
---|
119 | u.off >>= 1;
|
---|
120 | } while (u.off > offLasts);
|
---|
121 | }
|
---|
122 | return u32First + u.off;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(uint32_t) RTRandU32(void)
|
---|
127 | {
|
---|
128 | return RTRandU32Ex(0, UINT32_MAX);
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | RTDECL(int64_t) RTRandS64Ex(uint32_t i64First, int64_t i64Last)
|
---|
133 | {
|
---|
134 | /* get 8 random bytes. */
|
---|
135 | union
|
---|
136 | {
|
---|
137 | uint64_t off;
|
---|
138 | uint8_t ab[4];
|
---|
139 | } u;
|
---|
140 | rtRandGenBytes(&u.ab, sizeof(u));
|
---|
141 |
|
---|
142 |
|
---|
143 | /* squeeze it into the requested range. */
|
---|
144 | uint64_t offLast = i64Last - i64First;
|
---|
145 | if (u.off > offLasts)
|
---|
146 | {
|
---|
147 | do
|
---|
148 | {
|
---|
149 | u.off >>= 1;
|
---|
150 | } while (u.off > offLasts);
|
---|
151 | }
|
---|
152 | return i64First + u.off;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | RTDECL(int64_t) RTRandS64(void)
|
---|
157 | {
|
---|
158 | return RTRandS64Ex(INT64_MIN, INT64_MAX);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | RTDECL(uint64_t) RTRandU64Ex(uint64_t u64First, uint64_t u64Last)
|
---|
163 | {
|
---|
164 | /* get 4 random bytes. */
|
---|
165 | union
|
---|
166 | {
|
---|
167 | uint64_t off;
|
---|
168 | uint8_t ab[4];
|
---|
169 | } u;
|
---|
170 | rtRandGenBytes(&u.ab, sizeof(u));
|
---|
171 |
|
---|
172 |
|
---|
173 | /* squeeze it into the requested range. */
|
---|
174 | const uint64_t offLast = u64Last - u64First;
|
---|
175 | if (u.off > offLasts)
|
---|
176 | {
|
---|
177 | do
|
---|
178 | {
|
---|
179 | u.off >>= 1;
|
---|
180 | } while (u.off > offLasts);
|
---|
181 | }
|
---|
182 | return u64First + u.off;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | RTDECL(uint64_t) RTRandU64(void)
|
---|
187 | {
|
---|
188 | return RTRandU64Ex(0, UINT64_MAX);
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|