Index: src/build.c ================================================================== --- src/build.c +++ src/build.c @@ -21,11 +21,11 @@ ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** PRAGMA ** -** $Id: build.c,v 1.164 2004/01/19 04:55:56 jplyon Exp $ +** $Id: build.c,v 1.165 2004/02/11 09:46:31 drh Exp $ */ #include "sqliteInt.h" #include /* @@ -817,11 +817,13 @@ ** and the probability of hitting the same cookie value is only ** 1 chance in 2^32. So we're safe enough. */ void sqliteChangeCookie(sqlite *db, Vdbe *v){ if( db->next_cookie==db->aDb[0].schema_cookie ){ - db->next_cookie = db->aDb[0].schema_cookie + sqliteRandomByte() + 1; + unsigned char r; + sqliteRandomness(1, &r); + db->next_cookie = db->aDb[0].schema_cookie + r + 1; db->flags |= SQLITE_InternChanges; sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0); sqliteVdbeAddOp(v, OP_SetCookie, 0, 0); } } Index: src/func.c ================================================================== --- src/func.c +++ src/func.c @@ -14,11 +14,11 @@ ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: func.c,v 1.38 2004/01/30 14:49:17 drh Exp $ +** $Id: func.c,v 1.39 2004/02/11 09:46:32 drh Exp $ */ #include #include #include #include @@ -193,11 +193,13 @@ /* ** Implementation of random(). Return a random integer. */ static void randomFunc(sqlite_func *context, int argc, const char **argv){ - sqlite_set_result_int(context, sqliteRandomInteger()); + int r; + sqliteRandomness(sizeof(r), &r); + sqlite_set_result_int(context, r); } /* ** Implementation of the last_insert_rowid() SQL function. The return ** value is the same as the sqlite_last_insert_rowid() API function. @@ -339,17 +341,17 @@ /* ** This function generates a string of random characters. Used for ** generating test data. */ static void randStr(sqlite_func *context, int argc, const char **argv){ - static const char zSrc[] = + static const unsigned char zSrc[] = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" ".-!,:*^+=_|?/<> "; int iMin, iMax, n, r, i; - char zBuf[1000]; + unsigned char zBuf[1000]; if( argc>=1 ){ iMin = atoi(argv[0]); if( iMin<0 ) iMin = 0; if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; }else{ @@ -362,18 +364,18 @@ }else{ iMax = 50; } n = iMin; if( iMax>iMin ){ - r = sqliteRandomInteger() & 0x7fffffff; + sqliteRandomness(sizeof(r), &r); + r &= 0x7fffffff; n += r%(iMax + 1 - iMin); } assert( n0 && zTempPath[i-1]=='\\'; i--){} zTempPath[i] = 0; for(;;){ sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath); j = strlen(zBuf); - for(i=0; i<15; i++){ - int n = sqliteRandomByte() % (sizeof(zChars) - 1); - zBuf[j++] = zChars[n]; + sqliteRandomness(15, &zBuf[j]); + for(i=0; i<15; i++, j++){ + zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; } zBuf[j] = 0; if( !sqliteOsFileExists(zBuf) ) break; } #endif @@ -863,13 +863,13 @@ if( *zTempPath == 0 ) getcwd(zTempPath, SQLITE_TEMPNAME_SIZE-24); for(;;){ sprintf(zBuf, "%s"TEMP_FILE_PREFIX, zTempPath); j = strlen(zBuf); - for(i=0; i<15; i++){ - int n = sqliteRandomByte() % sizeof(zChars); - zBuf[j++] = zChars[n]; + sqliteRandomness(15, &zBuf[j]); + for(i=0; i<15; i++, j++){ + zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; } zBuf[j] = 0; if( !sqliteOsFileExists(zBuf) ) break; } #endif @@ -1323,13 +1323,15 @@ #if OS_WIN int rc; if( id->locked>0 ){ rc = SQLITE_OK; }else{ - int lk = (sqliteRandomInteger() & 0x7ffffff)%N_LOCKBYTE+1; + int lk; int res; int cnt = 100; + sqliteRandomness(sizeof(lk), &lk); + lk = (lk & 0x7fffffff)%N_LOCKBYTE + 1; while( cnt-->0 && (res = LockFile(id->h, FIRST_LOCKBYTE, 0, 1, 0))==0 ){ Sleep(1); } if( res ){ UnlockFile(id->h, FIRST_LOCKBYTE+1, 0, N_LOCKBYTE, 0); @@ -1357,14 +1359,16 @@ #if OS_MAC int rc; if( id->locked>0 || id->refNumRF == -1 ){ rc = SQLITE_OK; }else{ - int lk = (sqliteRandomInteger() & 0x7ffffff)%N_LOCKBYTE+1; + int lk; OSErr res; int cnt = 5; ParamBlockRec params; + sqliteRandomness(sizeof(lk), &lk); + lk = (lk & 0x7fffffff)%N_LOCKBYTE + 1; memset(¶ms, 0, sizeof(params)); params.ioParam.ioRefNum = id->refNumRF; params.ioParam.ioPosMode = fsFromStart; params.ioParam.ioPosOffset = FIRST_LOCKBYTE; params.ioParam.ioReqCount = 1; Index: src/pager.c ================================================================== --- src/pager.c +++ src/pager.c @@ -16,11 +16,11 @@ ** is separate from the database file. The pager also implements file ** locking to prevent two processes from writing the same database ** file simultaneously, or one process from reading the database while ** another is writing. ** -** @(#) $Id: pager.c,v 1.99 2004/02/11 02:18:07 drh Exp $ +** @(#) $Id: pager.c,v 1.100 2004/02/11 09:46:32 drh Exp $ */ #include "os.h" /* Must be first to enable large file support */ #include "sqliteInt.h" #include "pager.h" #include @@ -1662,11 +1662,11 @@ rc = sqliteOsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3)); if( rc==SQLITE_OK ){ rc = write32bits(&pPager->jfd, pPager->noSync ? 0xffffffff : 0); } if( rc==SQLITE_OK ){ - pPager->cksumInit = (u32)sqliteRandomInteger(); + sqliteRandomness(sizeof(pPager->cksumInit), &pPager->cksumInit); rc = write32bits(&pPager->jfd, pPager->cksumInit); } }else if( journal_format==JOURNAL_FORMAT_2 ){ rc = sqliteOsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2)); }else{ Index: src/random.c ================================================================== --- src/random.c +++ src/random.c @@ -13,11 +13,11 @@ ** generator (PRNG) for SQLite. ** ** 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.10 2002/02/19 13:39:23 drh Exp $ +** $Id: random.c,v 1.11 2004/02/11 09:46:33 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -33,19 +33,19 @@ ** to know. To minimize the risk of problems due to bad lrand48() ** implementations, SQLite uses this random number generator based ** on RC4, which we know works very well. */ static int randomByte(){ - int t; + unsigned char 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 */ + 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 ** not need to contain a lot of randomness since we are not @@ -63,51 +63,35 @@ sqliteOsRandomSeed(k); for(i=0; i<256; i++){ prng.s[i] = i; } for(i=0; i<256; i++){ - int t; - prng.j = (prng.j + prng.s[i] + k[i]) & 0xff; + prng.j += prng.s[i] + k[i]; t = prng.s[prng.j]; prng.s[prng.j] = prng.s[i]; prng.s[i] = t; } prng.isInit = 1; } /* Generate and return single random byte */ - prng.i = (prng.i + 1) & 0xff; - prng.j = (prng.j + prng.s[prng.i]) & 0xff; + 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] + prng.s[prng.j]; - return prng.s[t & 0xff]; + t += prng.s[prng.i]; + return prng.s[t]; } /* -** Return an random 8-bit integer. +** Return N random bytes. */ -int sqliteRandomByte(){ - int r; +void sqliteRandomness(int N, void *pBuf){ + unsigned char *zBuf = pBuf; sqliteOsEnterMutex(); - r = randomByte(); - sqliteOsLeaveMutex(); - return r; -} - -/* -** Return a random 32-bit integer. The integer is generated by making -** 4 calls to sqliteRandomByte(). -*/ -int sqliteRandomInteger(){ - int r; - int i; - sqliteOsEnterMutex(); - r = randomByte(); - for(i=1; i<4; i++){ - r = (r<<8) + randomByte(); + while( N-- ){ + *(zBuf++) = randomByte(); } sqliteOsLeaveMutex(); - return r; } Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -9,11 +9,11 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.209 2004/01/16 15:55:38 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.210 2004/02/11 09:46:33 drh Exp $ */ #include "config.h" #include "sqlite.h" #include "hash.h" #include "vdbe.h" @@ -1149,12 +1149,11 @@ int sqliteExprCompare(Expr*, Expr*); int sqliteFuncId(Token*); int sqliteExprResolveIds(Parse*, SrcList*, ExprList*, Expr*); int sqliteExprAnalyzeAggregates(Parse*, Expr*); Vdbe *sqliteGetVdbe(Parse*); -int sqliteRandomByte(void); -int sqliteRandomInteger(void); +void sqliteRandomness(int, void*); void sqliteRollbackAll(sqlite*); void sqliteCodeVerifySchema(Parse*, int); void sqliteBeginTransaction(Parse*, int); void sqliteCommitTransaction(Parse*); void sqliteRollbackTransaction(Parse*); Index: src/vacuum.c ================================================================== --- src/vacuum.c +++ src/vacuum.c @@ -12,11 +12,11 @@ ** This file contains code used to implement the VACUUM command. ** ** Most of the code in this file may be omitted by defining the ** SQLITE_OMIT_VACUUM macro. ** -** $Id: vacuum.c,v 1.9 2003/12/07 00:24:35 drh Exp $ +** $Id: vacuum.c,v 1.10 2004/02/11 09:46:33 drh Exp $ */ #include "sqliteInt.h" #include "os.h" /* @@ -183,18 +183,18 @@ } /* ** Generate a random name of 20 character in length. */ -static void randomName(char *zBuf){ - static const char zChars[] = +static void randomName(unsigned char *zBuf){ + static const unsigned char zChars[] = "abcdefghijklmnopqrstuvwxyz" "0123456789"; int i; + sqliteRandomness(20, zBuf); for(i=0; i<20; i++){ - int n = sqliteRandomByte() % (sizeof(zChars)-1); - zBuf[i] = zChars[n]; + zBuf[i] = zChars[ zBuf[i]%(sizeof(zChars)-1) ]; } } #endif /* Index: src/vdbe.c ================================================================== --- src/vdbe.c +++ src/vdbe.c @@ -41,11 +41,11 @@ ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.258 2004/02/10 13:41:52 drh Exp $ +** $Id: vdbe.c,v 1.259 2004/02/11 09:46:33 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include #include "vdbeInt.h" @@ -2891,14 +2891,16 @@ if( pC->useRandomRowid ){ v = db->priorNewRowid; cnt = 0; do{ if( v==0 || cnt>2 ){ - v = sqliteRandomInteger(); + sqliteRandomness(sizeof(v), &v); if( cnt<5 ) v &= 0xffffff; }else{ - v += sqliteRandomByte() + 1; + unsigned char r; + sqliteRandomness(1, &r); + v += r + 1; } if( v==0 ) continue; x = intToKey(v); rx = sqliteBtreeMoveto(pC->pCursor, &x, sizeof(int), &res); cnt++;