extensions/sqlite/sqlite-source/random.c
changeset 1952 b7aa2b626807
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sqlite/sqlite-source/random.c	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,103 @@
     1.4 +/*
     1.5 +** 2001 September 15
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +*************************************************************************
    1.15 +** This file contains code to implement a pseudo-random number
    1.16 +** generator (PRNG) for SQLite.
    1.17 +**
    1.18 +** Random numbers are used by some of the database backends in order
    1.19 +** to generate random integer keys for tables or random filenames.
    1.20 +**
    1.21 +** $Id$
    1.22 +*/
    1.23 +#include "sqliteInt.h"
    1.24 +
    1.25 +
    1.26 +/*
    1.27 +** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
    1.28 +** must be held while executing this routine.
    1.29 +**
    1.30 +** Why not just use a library random generator like lrand48() for this?
    1.31 +** Because the OP_NewRowid opcode in the VDBE depends on having a very
    1.32 +** good source of random numbers.  The lrand48() library function may
    1.33 +** well be good enough.  But maybe not.  Or maybe lrand48() has some
    1.34 +** subtle problems on some systems that could cause problems.  It is hard
    1.35 +** to know.  To minimize the risk of problems due to bad lrand48()
    1.36 +** implementations, SQLite uses this random number generator based
    1.37 +** on RC4, which we know works very well.
    1.38 +**
    1.39 +** (Later):  Actually, OP_NewRowid does not depend on a good source of
    1.40 +** randomness any more.  But we will leave this code in all the same.
    1.41 +*/
    1.42 +static int randomByte(void){
    1.43 +  unsigned char t;
    1.44 +
    1.45 +  /* All threads share a single random number generator.
    1.46 +  ** This structure is the current state of the generator.
    1.47 +  */
    1.48 +  static struct {
    1.49 +    unsigned char isInit;          /* True if initialized */
    1.50 +    unsigned char i, j;            /* State variables */
    1.51 +    unsigned char s[256];          /* State variables */
    1.52 +  } prng;
    1.53 +
    1.54 +  /* Initialize the state of the random number generator once,
    1.55 +  ** the first time this routine is called.  The seed value does
    1.56 +  ** not need to contain a lot of randomness since we are not
    1.57 +  ** trying to do secure encryption or anything like that...
    1.58 +  **
    1.59 +  ** Nothing in this file or anywhere else in SQLite does any kind of
    1.60 +  ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
    1.61 +  ** number generator) not as an encryption device.
    1.62 +  */
    1.63 +  if( !prng.isInit ){
    1.64 +    int i;
    1.65 +    char k[256];
    1.66 +    prng.j = 0;
    1.67 +    prng.i = 0;
    1.68 +    sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
    1.69 +    for(i=0; i<256; i++){
    1.70 +      prng.s[i] = i;
    1.71 +    }
    1.72 +    for(i=0; i<256; i++){
    1.73 +      prng.j += prng.s[i] + k[i];
    1.74 +      t = prng.s[prng.j];
    1.75 +      prng.s[prng.j] = prng.s[i];
    1.76 +      prng.s[i] = t;
    1.77 +    }
    1.78 +    prng.isInit = 1;
    1.79 +  }
    1.80 +
    1.81 +  /* Generate and return single random byte
    1.82 +  */
    1.83 +  prng.i++;
    1.84 +  t = prng.s[prng.i];
    1.85 +  prng.j += t;
    1.86 +  prng.s[prng.i] = prng.s[prng.j];
    1.87 +  prng.s[prng.j] = t;
    1.88 +  t += prng.s[prng.i];
    1.89 +  return prng.s[t];
    1.90 +}
    1.91 +
    1.92 +/*
    1.93 +** Return N random bytes.
    1.94 +*/
    1.95 +void sqlite3Randomness(int N, void *pBuf){
    1.96 +  unsigned char *zBuf = pBuf;
    1.97 +  static sqlite3_mutex *mutex = 0;
    1.98 +  if( mutex==0 ){
    1.99 +    mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG);
   1.100 +  }
   1.101 +  sqlite3_mutex_enter(mutex);
   1.102 +  while( N-- ){
   1.103 +    *(zBuf++) = randomByte();
   1.104 +  }
   1.105 +  sqlite3_mutex_leave(mutex);
   1.106 +}