Artifact
4a22746501bf36b0a088c66e38dde5daba6a35da:
- File
src/random.c
— part of check-in
[6225cd46]
at
2007-08-21 13:51:23
on branch trunk
— Remove the obsolete static mutexes. Use only the lastest static mutex code. (CVS 4259)
(user:
drh
size: 3218)
0000: 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
0010: 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
0020: 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
0030: 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
0040: 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
0050: 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
0060: 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
0070: 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
0080: 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
0090: 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
00a0: 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
00b0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
00c0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
00d0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
00e0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
00f0: 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
0100: 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
0110: 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
0120: 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
0180: 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
0190: 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 e to implement a
01a0: 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e pseudo-random n
01b0: 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74 umber.** generat
01c0: 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53 51 or (PRNG) for SQ
01d0: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e 64 Lite..**.** Rand
01e0: 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75 om numbers are u
01f0: 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20 74 sed by some of t
0200: 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 6b he database back
0210: 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a ends in order.**
0220: 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61 6e to generate ran
0230: 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79 73 dom integer keys
0240: 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20 72 for tables or r
0250: 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 2e andom filenames.
0260: 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61 6e 64 .**.** $Id: rand
0270: 6f 6d 2e 63 2c 76 20 31 2e 32 30 20 32 30 30 37 om.c,v 1.20 2007
0280: 2f 30 38 2f 32 31 20 31 33 3a 35 31 3a 32 33 20 /08/21 13:51:23
0290: 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 6e drh Exp $.*/.#in
02a0: 63 6c 75 64 65 20 22 73 71 6c 69 74 65 49 6e 74 clude "sqliteInt
02b0: 2e 68 22 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 .h".../*.** Get
02c0: 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20 72 a single 8-bit r
02d0: 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f 6d andom value from
02e0: 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20 20 the RC4 PRNG.
02f0: 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 73 The Mutex.** mus
0300: 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 t be held while
0310: 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 72 executing this r
0320: 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 outine..**.** Wh
0330: 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 20 61 y not just use a
0340: 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f 6d 20 library random
0350: 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20 6c generator like l
0360: 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 68 69 rand48() for thi
0370: 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 s?.** Because th
0380: 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f 70 e OP_NewRowid op
0390: 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 42 45 code in the VDBE
03a0: 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 76 69 depends on havi
03b0: 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f 6f ng a very.** goo
03c0: 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 d source of rand
03d0: 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 om numbers. The
03e0: 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 72 61 lrand48() libra
03f0: 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 0a ry function may.
0400: 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64 20 ** well be good
0410: 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d 61 79 enough. But may
0420: 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 62 be not. Or mayb
0430: 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 73 20 e lrand48() has
0440: 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20 70 some.** subtle p
0450: 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 20 roblems on some
0460: 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 6f 75 systems that cou
0470: 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d ld cause problem
0480: 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 0a 2a s. It is hard.*
0490: 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d * to know. To m
04a0: 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 73 6b inimize the risk
04b0: 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 75 65 of problems due
04c0: 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 38 28 to bad lrand48(
04d0: 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ).** implementat
04e0: 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 73 65 ions, SQLite use
04f0: 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e 75 s this random nu
0500: 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62 mber generator b
0510: 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20 ased.** on RC4,
0520: 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 77 6f which we know wo
0530: 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a 2a rks very well..*
0540: 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20 41 *.** (Later): A
0550: 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52 ctually, OP_NewR
0560: 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 64 65 owid does not de
0570: 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20 73 pend on a good s
0580: 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e 64 ource of.** rand
0590: 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65 2e omness any more.
05a0: 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 But we will le
05b0: 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 69 6e ave this code in
05c0: 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a 2a all the same..*
05d0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 61 6e /.static int ran
05e0: 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a 20 domByte(void){.
05f0: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 unsigned char t
0600: 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 20 74 68 72 65 ;.. /* All thre
0610: 61 64 73 20 73 68 61 72 65 20 61 20 73 69 6e 67 ads share a sing
0620: 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 le random number
0630: 20 67 65 6e 65 72 61 74 6f 72 2e 0a 20 20 2a 2a generator.. **
0640: 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
0650: 69 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 is the current s
0660: 74 61 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65 tate of the gene
0670: 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 73 74 rator.. */. st
0680: 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20 atic struct {.
0690: 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
06a0: 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 isInit;
06b0: 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 /* True if init
06c0: 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 75 ialized */. u
06d0: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 2c 20 nsigned char i,
06e0: 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a j; /*
06f0: 20 53 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 State variables
0700: 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 */. unsigned
0710: 20 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 20 20 char s[256];
0720: 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 /* State
0730: 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 7d variables */. }
0740: 20 70 72 6e 67 3b 0a 0a 20 20 2f 2a 20 49 6e 69 prng;.. /* Ini
0750: 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74 61 74 tialize the stat
0760: 65 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 e of the random
0770: 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
0780: 20 6f 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20 once,. ** the
0790: 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 first time this
07a0: 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
07b0: 64 2e 20 20 54 68 65 20 73 65 65 64 20 76 61 6c d. The seed val
07c0: 75 65 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 ue does. ** not
07d0: 20 6e 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e need to contain
07e0: 20 61 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d a lot of random
07f0: 6e 65 73 73 20 73 69 6e 63 65 20 77 65 20 61 72 ness since we ar
0800: 65 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e e not. ** tryin
0810: 67 20 74 6f 20 64 6f 20 73 65 63 75 72 65 20 65 g to do secure e
0820: 6e 63 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79 ncryption or any
0830: 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e thing like that.
0840: 2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 ... **. ** Not
0850: 68 69 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c hing in this fil
0860: 65 20 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c e or anywhere el
0870: 73 65 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65 se in SQLite doe
0880: 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20 s any kind of.
0890: 2a 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20 ** encryption.
08a0: 54 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 The RC4 algorith
08b0: 6d 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 m is being used
08c0: 61 73 20 61 20 50 52 4e 47 20 28 70 73 65 75 64 as a PRNG (pseud
08d0: 6f 2d 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 o-random. ** nu
08e0: 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20 mber generator)
08f0: 6e 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70 not as an encryp
0900: 74 69 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a tion device.. *
0910: 2f 0a 20 20 69 66 28 20 21 70 72 6e 67 2e 69 73 /. if( !prng.is
0920: 49 6e 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 Init ){. int
0930: 69 3b 0a 20 20 20 20 63 68 61 72 20 6b 5b 32 35 i;. char k[25
0940: 36 5d 3b 0a 20 20 20 20 70 72 6e 67 2e 6a 20 3d 6];. prng.j =
0950: 20 30 3b 0a 20 20 20 20 70 72 6e 67 2e 69 20 3d 0;. prng.i =
0960: 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 0;. sqlite3O
0970: 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 sRandomness(sqli
0980: 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 2c te3_vfs_find(0),
0990: 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20 20 66 6f 256, k);. fo
09a0: 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b r(i=0; i<256; i+
09b0: 2b 29 7b 0a 20 20 20 20 20 20 70 72 6e 67 2e 73 +){. prng.s
09c0: 5b 69 5d 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 [i] = i;. }.
09d0: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 for(i=0; i<25
09e0: 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 6; i++){. p
09f0: 72 6e 67 2e 6a 20 2b 3d 20 70 72 6e 67 2e 73 5b rng.j += prng.s[
0a00: 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 i] + k[i];.
0a10: 20 74 20 3d 20 70 72 6e 67 2e 73 5b 70 72 6e 67 t = prng.s[prng
0a20: 2e 6a 5d 3b 0a 20 20 20 20 20 20 70 72 6e 67 2e .j];. prng.
0a30: 73 5b 70 72 6e 67 2e 6a 5d 20 3d 20 70 72 6e 67 s[prng.j] = prng
0a40: 2e 73 5b 69 5d 3b 0a 20 20 20 20 20 20 70 72 6e .s[i];. prn
0a50: 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 20 g.s[i] = t;.
0a60: 7d 0a 20 20 20 20 70 72 6e 67 2e 69 73 49 6e 69 }. prng.isIni
0a70: 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a t = 1;. }.. /*
0a80: 20 47 65 6e 65 72 61 74 65 20 61 6e 64 20 72 65 Generate and re
0a90: 74 75 72 6e 20 73 69 6e 67 6c 65 20 72 61 6e 64 turn single rand
0aa0: 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a 20 20 70 om byte. */. p
0ab0: 72 6e 67 2e 69 2b 2b 3b 0a 20 20 74 20 3d 20 70 rng.i++;. t = p
0ac0: 72 6e 67 2e 73 5b 70 72 6e 67 2e 69 5d 3b 0a 20 rng.s[prng.i];.
0ad0: 20 70 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 prng.j += t;.
0ae0: 70 72 6e 67 2e 73 5b 70 72 6e 67 2e 69 5d 20 3d prng.s[prng.i] =
0af0: 20 70 72 6e 67 2e 73 5b 70 72 6e 67 2e 6a 5d 3b prng.s[prng.j];
0b00: 0a 20 20 70 72 6e 67 2e 73 5b 70 72 6e 67 2e 6a . prng.s[prng.j
0b10: 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 70 72 ] = t;. t += pr
0b20: 6e 67 2e 73 5b 70 72 6e 67 2e 69 5d 3b 0a 20 20 ng.s[prng.i];.
0b30: 72 65 74 75 72 6e 20 70 72 6e 67 2e 73 5b 74 5d return prng.s[t]
0b40: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
0b50: 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79 74 65 73 n N random bytes
0b60: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
0b70: 33 52 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 3Randomness(int
0b80: 4e 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a N, void *pBuf){.
0b90: 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
0ba0: 2a 7a 42 75 66 20 3d 20 70 42 75 66 3b 0a 20 20 *zBuf = pBuf;.
0bb0: 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
0bc0: 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 30 3b utex *mutex = 0;
0bd0: 0a 20 20 69 66 28 20 6d 75 74 65 78 3d 3d 30 20 . if( mutex==0
0be0: 29 7b 0a 20 20 20 20 6d 75 74 65 78 20 3d 20 73 ){. mutex = s
0bf0: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
0c00: 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
0c10: 53 54 41 54 49 43 5f 50 52 4e 47 29 3b 0a 20 20 STATIC_PRNG);.
0c20: 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
0c30: 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a x_enter(mutex);.
0c40: 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a while( N-- ){.
0c50: 20 20 20 20 2a 28 7a 42 75 66 2b 2b 29 20 3d 20 *(zBuf++) =
0c60: 72 61 6e 64 6f 6d 42 79 74 65 28 29 3b 0a 20 20 randomByte();.
0c70: 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
0c80: 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a x_leave(mutex);.
0c90: 7d 0a }.