summaryrefslogtreecommitdiff
path: root/ext/pdo_sqlite/sqlite/src/random.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/pdo_sqlite/sqlite/src/random.c')
-rw-r--r--ext/pdo_sqlite/sqlite/src/random.c79
1 files changed, 50 insertions, 29 deletions
diff --git a/ext/pdo_sqlite/sqlite/src/random.c b/ext/pdo_sqlite/sqlite/src/random.c
index 51d5d72e77..466da32de5 100644
--- a/ext/pdo_sqlite/sqlite/src/random.c
+++ b/ext/pdo_sqlite/sqlite/src/random.c
@@ -18,9 +18,17 @@
** $Id$
*/
#include "sqliteInt.h"
-#include "os.h"
+/* All threads share a single random number generator.
+** This structure is the current state of the generator.
+*/
+static struct sqlite3PrngType {
+ unsigned char isInit; /* True if initialized */
+ unsigned char i, j; /* State variables */
+ unsigned char s[256]; /* State variables */
+} sqlite3Prng;
+
/*
** Get a single 8-bit random value from the RC4 PRNG. The Mutex
** must be held while executing this routine.
@@ -37,17 +45,9 @@
** (Later): Actually, OP_NewRowid does not depend on a good source of
** randomness any more. But we will leave this code in all the same.
*/
-static int randomByte(){
+static int randomByte(void){
unsigned char t;
- /* All threads share a single random number generator.
- ** This structure is the current state of the generator.
- */
- static struct {
- unsigned char isInit; /* True if initialized */
- unsigned char i, j; /* State variables */
- unsigned char s[256]; /* State variables */
- } prng;
/* Initialize the state of the random number generator once,
** the first time this routine is called. The seed value does
@@ -58,33 +58,33 @@ static int randomByte(){
** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
** number generator) not as an encryption device.
*/
- if( !prng.isInit ){
+ if( !sqlite3Prng.isInit ){
int i;
char k[256];
- prng.j = 0;
- prng.i = 0;
- sqlite3OsRandomSeed(k);
+ sqlite3Prng.j = 0;
+ sqlite3Prng.i = 0;
+ sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
for(i=0; i<256; i++){
- prng.s[i] = i;
+ sqlite3Prng.s[i] = i;
}
for(i=0; i<256; i++){
- prng.j += prng.s[i] + k[i];
- t = prng.s[prng.j];
- prng.s[prng.j] = prng.s[i];
- prng.s[i] = t;
+ sqlite3Prng.j += sqlite3Prng.s[i] + k[i];
+ t = sqlite3Prng.s[sqlite3Prng.j];
+ sqlite3Prng.s[sqlite3Prng.j] = sqlite3Prng.s[i];
+ sqlite3Prng.s[i] = t;
}
- prng.isInit = 1;
+ sqlite3Prng.isInit = 1;
}
/* Generate and return single random byte
*/
- prng.i++;
- t = prng.s[prng.i];
- prng.j += t;
- prng.s[prng.i] = prng.s[prng.j];
- prng.s[prng.j] = t;
- t += prng.s[prng.i];
- return prng.s[t];
+ sqlite3Prng.i++;
+ t = sqlite3Prng.s[sqlite3Prng.i];
+ sqlite3Prng.j += t;
+ sqlite3Prng.s[sqlite3Prng.i] = sqlite3Prng.s[sqlite3Prng.j];
+ sqlite3Prng.s[sqlite3Prng.j] = t;
+ t += sqlite3Prng.s[sqlite3Prng.i];
+ return sqlite3Prng.s[t];
}
/*
@@ -92,9 +92,30 @@ static int randomByte(){
*/
void sqlite3Randomness(int N, void *pBuf){
unsigned char *zBuf = pBuf;
- sqlite3OsEnterMutex();
+ static sqlite3_mutex *mutex = 0;
+ if( mutex==0 ){
+ mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG);
+ }
+ sqlite3_mutex_enter(mutex);
while( N-- ){
*(zBuf++) = randomByte();
}
- sqlite3OsLeaveMutex();
+ sqlite3_mutex_leave(mutex);
+}
+
+#ifdef SQLITE_TEST
+/*
+** For testing purposes, we sometimes want to preserve the state of
+** PRNG and restore the PRNG to its saved state at a later time.
+*/
+static struct sqlite3PrngType sqlite3SavedPrng;
+void sqlite3SavePrngState(void){
+ memcpy(&sqlite3SavedPrng, &sqlite3Prng, sizeof(sqlite3Prng));
+}
+void sqlite3RestorePrngState(void){
+ memcpy(&sqlite3Prng, &sqlite3SavedPrng, sizeof(sqlite3Prng));
+}
+void sqlite3ResetPrngState(void){
+ sqlite3Prng.isInit = 0;
}
+#endif /* SQLITE_TEST */