diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/build.c | 4 | ||||
-rw-r--r-- | src/os.c | 18 | ||||
-rw-r--r-- | src/random.c | 97 | ||||
-rw-r--r-- | src/sqliteInt.h | 13 | ||||
-rw-r--r-- | src/vdbe.c | 31 |
5 files changed, 74 insertions, 89 deletions
diff --git a/src/build.c b/src/build.c index 24862028c..2a7ee3168 100644 --- a/src/build.c +++ b/src/build.c @@ -25,7 +25,7 @@ ** ROLLBACK ** PRAGMA ** -** $Id: build.c,v 1.39 2001/09/23 02:35:53 drh Exp $ +** $Id: build.c,v 1.40 2001/09/23 19:46:52 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -445,7 +445,7 @@ void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ */ static void changeCookie(sqlite *db){ if( db->next_cookie==db->schema_cookie ){ - db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1; + db->next_cookie = db->schema_cookie + sqliteRandomByte(db) + 1; db->flags |= SQLITE_InternChanges; } } @@ -246,7 +246,7 @@ int sqliteOsTempFileName(char *zBuf){ sprintf(zBuf, "%s/sqlite_", zDir); j = strlen(zBuf); for(i=0; i<15; i++){ - int n = sqliteRandomByte() % sizeof(zChars); + int n = rand() % sizeof(zChars); zBuf[j++] = zChars[n]; } zBuf[j] = 0; @@ -264,7 +264,7 @@ int sqliteOsTempFileName(char *zBuf){ sprintf(zBuf, "%s/sqlite_", zTempPath); j = strlen(zBuf); for(i=0; i<15; i++){ - int n = sqliteRandomByte() % sizeof(zChars); + int n = rand() % sizeof(zChars); zBuf[j++] = zChars[n]; } zBuf[j] = 0; @@ -429,19 +429,23 @@ int sqliteOsUnlock(OsFile id){ ** Get information to seed the random number generator. */ int sqliteOsRandomSeed(char *zBuf){ + static int once = 1; #if OS_UNIX int pid; time((time_t*)zBuf); - zBuf += sizeof(time_t); pid = getpid(); - memcpy(zBuf, &pid, sizeof(pid)); - zBuf += pid; - return SQLITE_OK; + memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid)); #endif #if OS_WIN GetSystemTime((LPSYSTEMTIME)zBuf); +#endif + if( once ){ + int seed; + memcpy(&seed, zBuf, sizeof(seed)); + srand(seed); + once = 0; + } return SQLITE_OK; -#endif } /* diff --git a/src/random.c b/src/random.c index f4a9f1912..ee55b7a9f 100644 --- a/src/random.c +++ b/src/random.c @@ -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. - ** - ** 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... + ** + ** 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. */ - 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; -} diff --git a/src/sqliteInt.h b/src/sqliteInt.h index a8d2f81db..1a5f05aab 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.53 2001/09/22 18:12:10 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.54 2001/09/23 19:46:52 drh Exp $ */ #include "sqlite.h" #include "hash.h" @@ -148,6 +148,12 @@ struct sqlite { int (*xBusyCallback)(void *,const char*,int); /* The busy callback */ Hash tblHash; /* All tables indexed by name */ Hash idxHash; /* All (named) indices indexed by name */ + struct { /* State of the RC4 random number generator */ + int isInit; /* True if initialized */ + int i, j; /* State variables */ + int s[256]; /* State variables */ + } prng; + int nextRowid; /* Next generated rowID */ }; /* @@ -451,9 +457,8 @@ void sqliteExprResolveInSelect(Parse*, Expr*); int sqliteExprAnalyzeAggregates(Parse*, Expr*); void sqliteParseInfoReset(Parse*); Vdbe *sqliteGetVdbe(Parse*); -int sqliteRandomByte(void); -int sqliteRandomInteger(void); -void sqliteRandomName(char*,char*); +int sqliteRandomByte(sqlite*); +int sqliteRandomInteger(sqlite*); void sqliteBeginTransaction(Parse*); void sqliteCommitTransaction(Parse*); void sqliteRollbackTransaction(Parse*); diff --git a/src/vdbe.c b/src/vdbe.c index 88fcaa2a3..9730971e5 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -30,7 +30,7 @@ ** But other routines are also provided to help in building up ** a program instruction by instruction. ** -** $Id: vdbe.c,v 1.74 2001/09/23 02:35:53 drh Exp $ +** $Id: vdbe.c,v 1.75 2001/09/23 19:46:52 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -1886,7 +1886,26 @@ case OP_MakeIdxKey: { ** database. */ case OP_Transaction: { - rc = sqliteBtreeBeginTrans(pBt); + int busy = 0; + do{ + rc = sqliteBtreeBeginTrans(pBt); + switch( rc ){ + case SQLITE_BUSY: { + if( xBusy==0 || (*xBusy)(pBusyArg, "", ++busy)==0 ){ + sqliteSetString(pzErrMsg, sqliteErrStr(rc), 0); + busy = 0; + } + break; + } + case SQLITE_OK: { + busy = 0; + break; + } + default: { + goto abort_due_to_error; + } + } + }while( busy ); break; } @@ -2262,17 +2281,18 @@ case OP_NewRecno: { ** to double the speed of the COPY operation. */ int res, rx, cnt; - static int x = 0; + int x; union { char zBuf[sizeof(int)]; int i; } ux; cnt = 0; + x = db->nextRowid; do{ if( cnt>5 ){ - x = sqliteRandomInteger(); + x = sqliteRandomInteger(db); }else{ - x += sqliteRandomByte() + 1; + x += sqliteRandomByte(db) + 1; } if( x==0 ) continue; ux.zBuf[3] = x&0xff; @@ -2283,6 +2303,7 @@ case OP_NewRecno: { rx = sqliteBtreeMoveto(p->aCsr[i].pCursor, &v, sizeof(v), &res); cnt++; }while( cnt<1000 && rx==SQLITE_OK && res==0 ); + db->nextRowid = x; if( rx==SQLITE_OK && res==0 ){ rc = SQLITE_FULL; goto abort_due_to_error; |