extensions/sqlite/sqlite-source/random.c
author Matt Woodrow <pred@alliedmods.net>
Wed Apr 16 05:41:34 2008 +0000 (2008-04-16)
changeset 2005 92033efef85f
parent 1952 b7aa2b626807
permissions -rw-r--r--
Added amb1609 - Persistant option to TF2_SetPlayerClass
ds@1952
     1
/*
ds@1952
     2
** 2001 September 15
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** This file contains code to implement a pseudo-random number
ds@1952
    13
** generator (PRNG) for SQLite.
ds@1952
    14
**
ds@1952
    15
** Random numbers are used by some of the database backends in order
ds@1952
    16
** to generate random integer keys for tables or random filenames.
ds@1952
    17
**
ds@1952
    18
** $Id$
ds@1952
    19
*/
ds@1952
    20
#include "sqliteInt.h"
ds@1952
    21
ds@1952
    22
ds@1952
    23
/*
ds@1952
    24
** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
ds@1952
    25
** must be held while executing this routine.
ds@1952
    26
**
ds@1952
    27
** Why not just use a library random generator like lrand48() for this?
ds@1952
    28
** Because the OP_NewRowid opcode in the VDBE depends on having a very
ds@1952
    29
** good source of random numbers.  The lrand48() library function may
ds@1952
    30
** well be good enough.  But maybe not.  Or maybe lrand48() has some
ds@1952
    31
** subtle problems on some systems that could cause problems.  It is hard
ds@1952
    32
** to know.  To minimize the risk of problems due to bad lrand48()
ds@1952
    33
** implementations, SQLite uses this random number generator based
ds@1952
    34
** on RC4, which we know works very well.
ds@1952
    35
**
ds@1952
    36
** (Later):  Actually, OP_NewRowid does not depend on a good source of
ds@1952
    37
** randomness any more.  But we will leave this code in all the same.
ds@1952
    38
*/
ds@1952
    39
static int randomByte(void){
ds@1952
    40
  unsigned char t;
ds@1952
    41
ds@1952
    42
  /* All threads share a single random number generator.
ds@1952
    43
  ** This structure is the current state of the generator.
ds@1952
    44
  */
ds@1952
    45
  static struct {
ds@1952
    46
    unsigned char isInit;          /* True if initialized */
ds@1952
    47
    unsigned char i, j;            /* State variables */
ds@1952
    48
    unsigned char s[256];          /* State variables */
ds@1952
    49
  } prng;
ds@1952
    50
ds@1952
    51
  /* Initialize the state of the random number generator once,
ds@1952
    52
  ** the first time this routine is called.  The seed value does
ds@1952
    53
  ** not need to contain a lot of randomness since we are not
ds@1952
    54
  ** trying to do secure encryption or anything like that...
ds@1952
    55
  **
ds@1952
    56
  ** Nothing in this file or anywhere else in SQLite does any kind of
ds@1952
    57
  ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
ds@1952
    58
  ** number generator) not as an encryption device.
ds@1952
    59
  */
ds@1952
    60
  if( !prng.isInit ){
ds@1952
    61
    int i;
ds@1952
    62
    char k[256];
ds@1952
    63
    prng.j = 0;
ds@1952
    64
    prng.i = 0;
ds@1952
    65
    sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
ds@1952
    66
    for(i=0; i<256; i++){
ds@1952
    67
      prng.s[i] = i;
ds@1952
    68
    }
ds@1952
    69
    for(i=0; i<256; i++){
ds@1952
    70
      prng.j += prng.s[i] + k[i];
ds@1952
    71
      t = prng.s[prng.j];
ds@1952
    72
      prng.s[prng.j] = prng.s[i];
ds@1952
    73
      prng.s[i] = t;
ds@1952
    74
    }
ds@1952
    75
    prng.isInit = 1;
ds@1952
    76
  }
ds@1952
    77
ds@1952
    78
  /* Generate and return single random byte
ds@1952
    79
  */
ds@1952
    80
  prng.i++;
ds@1952
    81
  t = prng.s[prng.i];
ds@1952
    82
  prng.j += t;
ds@1952
    83
  prng.s[prng.i] = prng.s[prng.j];
ds@1952
    84
  prng.s[prng.j] = t;
ds@1952
    85
  t += prng.s[prng.i];
ds@1952
    86
  return prng.s[t];
ds@1952
    87
}
ds@1952
    88
ds@1952
    89
/*
ds@1952
    90
** Return N random bytes.
ds@1952
    91
*/
ds@1952
    92
void sqlite3Randomness(int N, void *pBuf){
ds@1952
    93
  unsigned char *zBuf = pBuf;
ds@1952
    94
  static sqlite3_mutex *mutex = 0;
ds@1952
    95
  if( mutex==0 ){
ds@1952
    96
    mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG);
ds@1952
    97
  }
ds@1952
    98
  sqlite3_mutex_enter(mutex);
ds@1952
    99
  while( N-- ){
ds@1952
   100
    *(zBuf++) = randomByte();
ds@1952
   101
  }
ds@1952
   102
  sqlite3_mutex_leave(mutex);
ds@1952
   103
}