Files
harbour-core/harbour/contrib/hbsqlit2/random.c
Viktor Szakats 603d77d54b 2008-06-02 23:33 UTC+0100 Viktor Szakats (harbour.01 syenar hu)
* contrib/make_b32_all.bat
   * contrib/make_gcc_all.sh
   * contrib/make_vc_all.bat
   + contrib/hbsqlit2
   + contrib/hbsqlit2/Makefile
   + contrib/hbsqlit2/make_b32.bat
   + contrib/hbsqlit2/make_vc.bat
   + contrib/hbsqlit2/make_gcc.sh
   + contrib/hbsqlit2/common.mak
   + contrib/hbsqlit2/hbsqlit2.c
   + contrib/hbsqlit2/hbsqlit2.ch
   + contrib/hbsqlit2/LICENSE.txt
   + contrib/hbsqlit2/TODO.txt
   + contrib/hbsqlit2/readme.txt
   + contrib/hbsqlit2/attach.c
   + contrib/hbsqlit2/auth.c
   + contrib/hbsqlit2/btree.c
   + contrib/hbsqlit2/btree.h
   + contrib/hbsqlit2/btree_rb.c
   + contrib/hbsqlit2/build.c
   + contrib/hbsqlit2/config.h
   + contrib/hbsqlit2/copy.c
   + contrib/hbsqlit2/date.c
   + contrib/hbsqlit2/delete.c
   + contrib/hbsqlit2/encode.c
   + contrib/hbsqlit2/expr.c
   + contrib/hbsqlit2/func.c
   + contrib/hbsqlit2/hash.c
   + contrib/hbsqlit2/hash.h
   + contrib/hbsqlit2/insert.c
   + contrib/hbsqlit2/main.c
   + contrib/hbsqlit2/opcodes.c
   + contrib/hbsqlit2/opcodes.h
   + contrib/hbsqlit2/os.c
   + contrib/hbsqlit2/os.h
   + contrib/hbsqlit2/pager.c
   + contrib/hbsqlit2/pager.h
   + contrib/hbsqlit2/parse.c
   + contrib/hbsqlit2/parse.h
   + contrib/hbsqlit2/pragma.c
   + contrib/hbsqlit2/printf.c
   + contrib/hbsqlit2/random.c
   + contrib/hbsqlit2/select.c
   + contrib/hbsqlit2/sqlite.h
   + contrib/hbsqlit2/sqliteInt.h
   + contrib/hbsqlit2/table.c
   + contrib/hbsqlit2/tokenize.c
   + contrib/hbsqlit2/trigger.c
   + contrib/hbsqlit2/update.c
   + contrib/hbsqlit2/util.c
   + contrib/hbsqlit2/vacuum.c
   + contrib/hbsqlit2/vdbe.c
   + contrib/hbsqlit2/vdbe.h
   + contrib/hbsqlit2/vdbeaux.c
   + contrib/hbsqlit2/vdbeInt.h
   + contrib/hbsqlit2/where.c
   + contrib/hbsqlit2/tests/bld_b32.bat
   + contrib/hbsqlit2/tests/bld_vc.bat
   + contrib/hbsqlit2/tests/example.db
   + contrib/hbsqlit2/tests/hbsqlite.prg
     + Added hbsqlit2 SQLite 2.x interface library.

       With kind permission from the author:
       Alejandro de Gárate <alex_degarate/at/hotmail.com>

     + Some renames were done here too to better fit into 
       the contrib structure.
     + Added std make files.
     + Added to 'all' make files.
     + Added foreign SQLite 2.8.16 (final 2.x version)
       source code to the repository.
       Compiles with an okay amount of warnings under BCC but 
       doesn't compile in C++ mode in MSVC (due to missing 
       casts).
     ; NOTE: I've added this because this is the original 
             SQLite Harbour interface, clean and working 
             implementation, and SQLite 2 is incompatible 
             with 3, and some may need to access existing 
             2.x databases from Harbour.
2008-06-02 21:46:01 +00:00

98 lines
2.8 KiB
C

/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code to implement a pseudo-random number
** 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$
*/
#include "sqliteInt.h"
#include "os.h"
/*
** Get a single 8-bit random value from the RC4 PRNG. The Mutex
** must be held while executing this routine.
**
** Why not just use a library random generator like lrand48() for this?
** Because the OP_NewRecno opcode in the VDBE depends on having a very
** good source of random numbers. The lrand48() library function may
** well be good enough. But maybe not. Or maybe lrand48() has some
** subtle problems on some systems that could cause problems. It is hard
** 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(){
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];
}
/*
** Return N random bytes.
*/
void sqliteRandomness(int N, void *pBuf){
unsigned char *zBuf = pBuf;
sqliteOsEnterMutex();
while( N-- ){
*(zBuf++) = randomByte();
}
sqliteOsLeaveMutex();
}