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

Additional test cases with locking fixes. Also, make the code thread-safe. (CVS 262)

FossilOrigin-Name: bd7d6a64afa03cc64f6537f828d6c94975bf5f02
This commit is contained in:
drh
2001-09-23 19:46:51 +00:00
parent ecdc7530dd
commit 90bfcdace3
9 changed files with 188 additions and 104 deletions

View File

@@ -15,7 +15,7 @@
** 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.6 2001/09/19 13:22:40 drh Exp $
** $Id: random.c,v 1.7 2001/09/23 19:46:52 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
@@ -23,103 +23,58 @@
/*
** Get a single 8-bit random value from the RC4 PRNG.
*/
int sqliteRandomByte(void){
int sqliteRandomByte(sqlite *db){
int t;
/*
** The following structure holds the current state of the RC4 algorithm.
** We use RC4 as a random number generator. Each call to RC4 gives
** a random 8-bit number.
/* 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
** trying to do secure encryption or anything like that...
**
** Nothing in this file or anywhere else in SQLite does any kind of
** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
** number generator) not as an encryption device.
*/
static struct {
int isInit;
int i, j;
int s[256];
} prng_state;
/* 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
** trying to do secure encryption or anything like that...
*/
if( !prng_state.isInit ){
if( !db->prng.isInit ){
int i;
static char seed[] = " sqlite random seed abcdefghijklmnop";
char k[256];
sqliteOsRandomSeed(seed);
prng_state.j = 0;
prng_state.i = 0;
db->prng.j = 0;
db->prng.i = 0;
sqliteOsRandomSeed(k);
for(i=0; i<256; i++){
prng_state.s[i] = i;
k[i] = seed[i%sizeof(seed)];
db->prng.s[i] = i;
}
for(i=0; i<256; i++){
int t;
prng_state.j = (prng_state.j + prng_state.s[i] + k[i]) & 0xff;
t = prng_state.s[prng_state.j];
prng_state.s[prng_state.j] = prng_state.s[i];
prng_state.s[i] = 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_state.isInit = 1;
db->prng.isInit = 1;
}
/* Generate and return single random byte
*/
prng_state.i = (prng_state.i + 1) & 0xff;
prng_state.j = (prng_state.j + prng_state.s[prng_state.i]) & 0xff;
t = prng_state.s[prng_state.i];
prng_state.s[prng_state.i] = prng_state.s[prng_state.j];
prng_state.s[prng_state.j] = t;
t = prng_state.s[prng_state.i] + prng_state.s[prng_state.j];
return prng_state.s[t & 0xff];
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];
}
/*
** Return a random 32-bit integer. The integer is generated by making
** 4 calls to sqliteRandomByte().
*/
int sqliteRandomInteger(void){
int sqliteRandomInteger(sqlite *db){
int r;
int i;
r = sqliteRandomByte();
r = sqliteRandomByte(db);
for(i=1; i<4; i++){
r = (r<<8) + sqliteRandomByte();
r = (r<<8) + sqliteRandomByte(db);
}
return r;
}
/*
** Return a random 16-bit unsigned integer. The integer is generated by
** making 2 calls to sqliteRandomByte().
*/
int sqliteRandomShort(void){
int r;
r = sqliteRandomByte();
r = (r<<8) + sqliteRandomByte();
return r;
}
/*
** Generate a random filename with the given prefix. The new filename
** is written into zBuf[]. The calling function must insure that
** zBuf[] is big enough to hold the prefix plus 20 or so extra
** characters.
**
** Very random names are chosen so that the chance of a
** collision with an existing filename is very very small.
*/
void sqliteRandomName(char *zBuf, char *zPrefix){
int i, j;
static const char zRandomChars[] = "abcdefghijklmnopqrstuvwxyz0123456789";
strcpy(zBuf, zPrefix);
j = strlen(zBuf);
for(i=0; i<15; i++){
int c = sqliteRandomByte() % (sizeof(zRandomChars) - 1);
zBuf[j++] = zRandomChars[c];
}
zBuf[j] = 0;
}