Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / sqlite / sqlite-src-3080704 / src / random.c
blobb82566524c40c6bb87154a259d7c24077d54e394
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.
11 *************************************************************************
12 ** This file contains code to implement a pseudo-random number
13 ** generator (PRNG) for SQLite.
15 ** Random numbers are used by some of the database backends in order
16 ** to generate random integer keys for tables or random filenames.
18 #include "sqliteInt.h"
21 /* All threads share a single random number generator.
22 ** This structure is the current state of the generator.
24 static SQLITE_WSD struct sqlite3PrngType {
25 unsigned char isInit; /* True if initialized */
26 unsigned char i, j; /* State variables */
27 unsigned char s[256]; /* State variables */
28 } sqlite3Prng;
31 ** Return N random bytes.
33 void sqlite3_randomness(int N, void *pBuf){
34 unsigned char t;
35 unsigned char *zBuf = pBuf;
37 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
38 ** state vector. If writable static data is unsupported on the target,
39 ** we have to locate the state vector at run-time. In the more common
40 ** case where writable static data is supported, wsdPrng can refer directly
41 ** to the "sqlite3Prng" state vector declared above.
43 #ifdef SQLITE_OMIT_WSD
44 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
45 # define wsdPrng p[0]
46 #else
47 # define wsdPrng sqlite3Prng
48 #endif
50 #if SQLITE_THREADSAFE
51 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
52 sqlite3_mutex_enter(mutex);
53 #endif
55 if( N<=0 ){
56 wsdPrng.isInit = 0;
57 sqlite3_mutex_leave(mutex);
58 return;
61 /* Initialize the state of the random number generator once,
62 ** the first time this routine is called. The seed value does
63 ** not need to contain a lot of randomness since we are not
64 ** trying to do secure encryption or anything like that...
66 ** Nothing in this file or anywhere else in SQLite does any kind of
67 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
68 ** number generator) not as an encryption device.
70 if( !wsdPrng.isInit ){
71 int i;
72 char k[256];
73 wsdPrng.j = 0;
74 wsdPrng.i = 0;
75 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
76 for(i=0; i<256; i++){
77 wsdPrng.s[i] = (u8)i;
79 for(i=0; i<256; i++){
80 wsdPrng.j += wsdPrng.s[i] + k[i];
81 t = wsdPrng.s[wsdPrng.j];
82 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
83 wsdPrng.s[i] = t;
85 wsdPrng.isInit = 1;
88 assert( N>0 );
89 do{
90 wsdPrng.i++;
91 t = wsdPrng.s[wsdPrng.i];
92 wsdPrng.j += t;
93 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
94 wsdPrng.s[wsdPrng.j] = t;
95 t += wsdPrng.s[wsdPrng.i];
96 *(zBuf++) = wsdPrng.s[t];
97 }while( --N );
98 sqlite3_mutex_leave(mutex);
101 #ifndef SQLITE_OMIT_BUILTIN_TEST
103 ** For testing purposes, we sometimes want to preserve the state of
104 ** PRNG and restore the PRNG to its saved state at a later time, or
105 ** to reset the PRNG to its initial state. These routines accomplish
106 ** those tasks.
108 ** The sqlite3_test_control() interface calls these routines to
109 ** control the PRNG.
111 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
112 void sqlite3PrngSaveState(void){
113 memcpy(
114 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
115 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
116 sizeof(sqlite3Prng)
119 void sqlite3PrngRestoreState(void){
120 memcpy(
121 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
122 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
123 sizeof(sqlite3Prng)
126 #endif /* SQLITE_OMIT_BUILTIN_TEST */