Back to index

php5  5.3.10
Functions
random.c File Reference
#include "sqliteInt.h"
#include "os.h"

Go to the source code of this file.

Functions

static int randomByte ()
void sqliteRandomness (int N, void *pBuf)

Function Documentation

static int randomByte ( void  ) [static]

Definition at line 37 of file random.c.

                       {
  unsigned char t;

  /* All threads share a single random number generator.
  ** This structure is the current state of the generator.
  */
  static struct {
    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
  ** 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.isInit ){
    int i;
    char k[256];
    prng.j = 0;
    prng.i = 0;
    sqliteOsRandomSeed(k);
    for(i=0; i<256; i++){
      prng.s[i] = i;
    }
    for(i=0; i<256; i++){
      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++;
  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];
  return prng.s[t];
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqliteRandomness ( int  N,
void *  pBuf 
)

Definition at line 90 of file random.c.

                                        {
  unsigned char *zBuf = pBuf;
  sqliteOsEnterMutex();
  while( N-- ){
    *(zBuf++) = randomByte();
  }
  sqliteOsLeaveMutex();
}

Here is the call graph for this function:

Here is the caller graph for this function: