1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-06 15:49:35 +03:00

Fix the locking protocol. (CVS 280)

FossilOrigin-Name: 484b82d8a1c84f3d9725a509de93276b9fa9b294
This commit is contained in:
drh
2001-10-09 04:19:46 +00:00
parent f57b339988
commit ad75e9874b
13 changed files with 413 additions and 134 deletions

View File

@@ -15,17 +15,27 @@
** Random numbers are used by some of the database backends in order
** to generate random integer keys for tables or random filenames.
**
** $Id: random.c,v 1.7 2001/09/23 19:46:52 drh Exp $
** $Id: random.c,v 1.8 2001/10/09 04:19:47 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
/*
** Get a single 8-bit random value from the RC4 PRNG.
** Get a single 8-bit random value from the RC4 PRNG. The Mutex
** must be held while executing this routine.
*/
int sqliteRandomByte(sqlite *db){
static int randomByte(){
int t;
/* All threads share a single random number generator.
** This structure is the current state of the generator.
*/
static struct {
int isInit; /* True if initialized */
int i, j; /* State variables */
int 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
** not need to contain a lot of randomness since we are not
@@ -35,46 +45,59 @@ int sqliteRandomByte(sqlite *db){
** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
** number generator) not as an encryption device.
*/
if( !db->prng.isInit ){
if( !prng.isInit ){
int i;
char k[256];
db->prng.j = 0;
db->prng.i = 0;
prng.j = 0;
prng.i = 0;
sqliteOsRandomSeed(k);
for(i=0; i<256; i++){
db->prng.s[i] = i;
prng.s[i] = i;
}
for(i=0; i<256; i++){
int t;
db->prng.j = (db->prng.j + db->prng.s[i] + k[i]) & 0xff;
t = db->prng.s[db->prng.j];
db->prng.s[db->prng.j] = db->prng.s[i];
db->prng.s[i] = t;
prng.j = (prng.j + prng.s[i] + k[i]) & 0xff;
t = prng.s[prng.j];
prng.s[prng.j] = prng.s[i];
prng.s[i] = t;
}
db->prng.isInit = 1;
prng.isInit = 1;
}
/* Generate and return single random byte
*/
db->prng.i = (db->prng.i + 1) & 0xff;
db->prng.j = (db->prng.j + db->prng.s[db->prng.i]) & 0xff;
t = db->prng.s[db->prng.i];
db->prng.s[db->prng.i] = db->prng.s[db->prng.j];
db->prng.s[db->prng.j] = t;
t = db->prng.s[db->prng.i] + db->prng.s[db->prng.j];
return db->prng.s[t & 0xff];
prng.i = (prng.i + 1) & 0xff;
prng.j = (prng.j + prng.s[prng.i]) & 0xff;
t = prng.s[prng.i];
prng.s[prng.i] = prng.s[prng.j];
prng.s[prng.j] = t;
t = prng.s[prng.i] + prng.s[prng.j];
return prng.s[t & 0xff];
}
/*
** Return an random 8-bit integer.
*/
int sqliteRandomByte(){
int r;
sqliteOsEnterMutex();
r = randomByte();
sqliteOsLeaveMutex();
return r;
}
/*
** Return a random 32-bit integer. The integer is generated by making
** 4 calls to sqliteRandomByte().
*/
int sqliteRandomInteger(sqlite *db){
int sqliteRandomInteger(){
int r;
int i;
r = sqliteRandomByte(db);
sqliteOsEnterMutex();
r = randomByte();
for(i=1; i<4; i++){
r = (r<<8) + sqliteRandomByte(db);
r = (r<<8) + randomByte();
}
sqliteOsLeaveMutex();
return r;
}