persistentstorage/sql/SQLite/random.c
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 /*
       
     2 ** 2001 September 15
       
     3 **
       
     4 ** The author disclaims copyright to this source code.  In place of
       
     5 ** a legal notice, here is a blessing:
       
     6 **
       
     7 **    May you do good and not evil.
       
     8 **    May you find forgiveness for yourself and forgive others.
       
     9 **    May you share freely, never taking more than you give.
       
    10 **
       
    11 *************************************************************************
       
    12 ** This file contains code to implement a pseudo-random number
       
    13 ** generator (PRNG) for SQLite.
       
    14 **
       
    15 ** Random numbers are used by some of the database backends in order
       
    16 ** to generate random integer keys for tables or random filenames.
       
    17 **
       
    18 ** $Id: random.c,v 1.25 2008/06/19 01:03:18 drh Exp $
       
    19 */
       
    20 #include "sqliteInt.h"
       
    21 
       
    22 
       
    23 /* All threads share a single random number generator.
       
    24 ** This structure is the current state of the generator.
       
    25 */
       
    26 static struct sqlite3PrngType {
       
    27   unsigned char isInit;          /* True if initialized */
       
    28   unsigned char i, j;            /* State variables */
       
    29   unsigned char s[256];          /* State variables */
       
    30 } sqlite3Prng;
       
    31 
       
    32 /*
       
    33 ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
       
    34 ** must be held while executing this routine.
       
    35 **
       
    36 ** Why not just use a library random generator like lrand48() for this?
       
    37 ** Because the OP_NewRowid opcode in the VDBE depends on having a very
       
    38 ** good source of random numbers.  The lrand48() library function may
       
    39 ** well be good enough.  But maybe not.  Or maybe lrand48() has some
       
    40 ** subtle problems on some systems that could cause problems.  It is hard
       
    41 ** to know.  To minimize the risk of problems due to bad lrand48()
       
    42 ** implementations, SQLite uses this random number generator based
       
    43 ** on RC4, which we know works very well.
       
    44 **
       
    45 ** (Later):  Actually, OP_NewRowid does not depend on a good source of
       
    46 ** randomness any more.  But we will leave this code in all the same.
       
    47 */
       
    48 static int randomByte(void){
       
    49   unsigned char t;
       
    50 
       
    51 
       
    52   /* Initialize the state of the random number generator once,
       
    53   ** the first time this routine is called.  The seed value does
       
    54   ** not need to contain a lot of randomness since we are not
       
    55   ** trying to do secure encryption or anything like that...
       
    56   **
       
    57   ** Nothing in this file or anywhere else in SQLite does any kind of
       
    58   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
       
    59   ** number generator) not as an encryption device.
       
    60   */
       
    61   if( !sqlite3Prng.isInit ){
       
    62     int i;
       
    63     char k[256];
       
    64     sqlite3Prng.j = 0;
       
    65     sqlite3Prng.i = 0;
       
    66     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
       
    67     for(i=0; i<256; i++){
       
    68       sqlite3Prng.s[i] = i;
       
    69     }
       
    70     for(i=0; i<256; i++){
       
    71       sqlite3Prng.j += sqlite3Prng.s[i] + k[i];
       
    72       t = sqlite3Prng.s[sqlite3Prng.j];
       
    73       sqlite3Prng.s[sqlite3Prng.j] = sqlite3Prng.s[i];
       
    74       sqlite3Prng.s[i] = t;
       
    75     }
       
    76     sqlite3Prng.isInit = 1;
       
    77   }
       
    78 
       
    79   /* Generate and return single random byte
       
    80   */
       
    81   sqlite3Prng.i++;
       
    82   t = sqlite3Prng.s[sqlite3Prng.i];
       
    83   sqlite3Prng.j += t;
       
    84   sqlite3Prng.s[sqlite3Prng.i] = sqlite3Prng.s[sqlite3Prng.j];
       
    85   sqlite3Prng.s[sqlite3Prng.j] = t;
       
    86   t += sqlite3Prng.s[sqlite3Prng.i];
       
    87   return sqlite3Prng.s[t];
       
    88 }
       
    89 
       
    90 /*
       
    91 ** Return N random bytes.
       
    92 */
       
    93 void sqlite3_randomness(int N, void *pBuf){
       
    94   unsigned char *zBuf = pBuf;
       
    95 #ifndef SQLITE_MUTEX_NOOP
       
    96   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
       
    97 #endif
       
    98   sqlite3_mutex_enter(mutex);
       
    99   while( N-- ){
       
   100     *(zBuf++) = randomByte();
       
   101   }
       
   102   sqlite3_mutex_leave(mutex);
       
   103 }
       
   104 
       
   105 #ifndef SQLITE_OMIT_BUILTIN_TEST
       
   106 /*
       
   107 ** For testing purposes, we sometimes want to preserve the state of
       
   108 ** PRNG and restore the PRNG to its saved state at a later time.
       
   109 ** The sqlite3_test_control() interface calls these routines to
       
   110 ** control the PRNG.
       
   111 */
       
   112 static struct sqlite3PrngType sqlite3SavedPrng;
       
   113 void sqlite3PrngSaveState(void){
       
   114   memcpy(&sqlite3SavedPrng, &sqlite3Prng, sizeof(sqlite3Prng));
       
   115 }
       
   116 void sqlite3PrngRestoreState(void){
       
   117   memcpy(&sqlite3Prng, &sqlite3SavedPrng, sizeof(sqlite3Prng));
       
   118 }
       
   119 void sqlite3PrngResetState(void){
       
   120   sqlite3Prng.isInit = 0;
       
   121 }
       
   122 #endif /* SQLITE_OMIT_BUILTIN_TEST */