drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (c) 2000 D. Richard Hipp |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or |
| 5 | ** modify it under the terms of the GNU General Public |
| 6 | ** License as published by the Free Software Foundation; either |
| 7 | ** version 2 of the License, or (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | ** General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU General Public |
| 15 | ** License along with this library; if not, write to the |
| 16 | ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | ** Boston, MA 02111-1307, USA. |
| 18 | ** |
| 19 | ** Author contact information: |
| 20 | ** drh@hwaci.com |
| 21 | ** http://www.hwaci.com/drh/ |
| 22 | ** |
| 23 | ************************************************************************* |
| 24 | ** This file contains code to implement a pseudo-random number |
| 25 | ** generator (PRNG) for SQLite. |
| 26 | ** |
| 27 | ** Random numbers are used by some of the database backends in order |
| 28 | ** to generate random integer keys for tables or random filenames. |
| 29 | ** |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 30 | ** $Id: random.c,v 1.4 2001/09/14 03:24:25 drh Exp $ |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 31 | */ |
| 32 | #include "sqliteInt.h" |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 33 | #include <time.h> |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | ** Get a single 8-bit random value from the RC4 PRNG. |
| 37 | */ |
| 38 | int sqliteRandomByte(void){ |
| 39 | int t; |
| 40 | |
| 41 | /* |
| 42 | ** The following structure holds the current state of the RC4 algorithm. |
| 43 | ** We use RC4 as a random number generator. Each call to RC4 gives |
| 44 | ** a random 8-bit number. |
| 45 | ** |
| 46 | ** Nothing in this file or anywhere else in SQLite does any kind of |
| 47 | ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random |
| 48 | ** number generator) not as an encryption device. |
| 49 | */ |
| 50 | static struct { |
| 51 | int isInit; |
| 52 | int i, j; |
| 53 | int s[256]; |
| 54 | } prng_state; |
| 55 | |
| 56 | /* Initialize the state of the random number generator once, |
| 57 | ** the first time this routine is called. The seed value does |
| 58 | ** not need to contain a lot of randomness since we are not |
| 59 | ** trying to do secure encryption or anything like that... |
| 60 | */ |
| 61 | if( !prng_state.isInit ){ |
| 62 | int i; |
| 63 | static char seed[] = " sqlite random seed"; |
| 64 | char k[256]; |
| 65 | time((time_t*)seed); |
| 66 | prng_state.j = 0; |
| 67 | prng_state.i = 0; |
| 68 | for(i=0; i<256; i++){ |
| 69 | prng_state.s[i] = i; |
| 70 | k[i] = seed[i%sizeof(seed)]; |
| 71 | } |
| 72 | for(i=0; i<256; i++){ |
| 73 | int t; |
| 74 | prng_state.j = (prng_state.j + prng_state.s[i] + k[i]) & 0xff; |
| 75 | t = prng_state.s[prng_state.j]; |
| 76 | prng_state.s[prng_state.j] = prng_state.s[i]; |
| 77 | prng_state.s[i] = t; |
| 78 | } |
| 79 | prng_state.isInit = 1; |
| 80 | } |
| 81 | |
| 82 | /* Generate and return single random byte |
| 83 | */ |
| 84 | prng_state.i = (prng_state.i + 1) & 0xff; |
| 85 | prng_state.j = (prng_state.j + prng_state.s[prng_state.i]) & 0xff; |
| 86 | t = prng_state.s[prng_state.i]; |
| 87 | prng_state.s[prng_state.i] = prng_state.s[prng_state.j]; |
| 88 | prng_state.s[prng_state.j] = t; |
| 89 | t = prng_state.s[prng_state.i] + prng_state.s[prng_state.j]; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 90 | return prng_state.s[t & 0xff]; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* |
| 94 | ** Return a random 32-bit integer. The integer is generated by making |
| 95 | ** 4 calls to sqliteRandomByte(). |
| 96 | */ |
| 97 | int sqliteRandomInteger(void){ |
| 98 | int r; |
| 99 | int i; |
| 100 | r = sqliteRandomByte(); |
| 101 | for(i=1; i<4; i++){ |
| 102 | r = (r<<8) + sqliteRandomByte(); |
| 103 | } |
| 104 | return r; |
| 105 | } |
| 106 | |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 107 | /* |
| 108 | ** Return a random 16-bit unsigned integer. The integer is generated by |
| 109 | ** making 2 calls to sqliteRandomByte(). |
| 110 | */ |
| 111 | int sqliteRandomShort(void){ |
| 112 | int r; |
| 113 | r = sqliteRandomByte(); |
| 114 | r = (r<<8) + sqliteRandomByte(); |
| 115 | return r; |
| 116 | } |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | ** Generate a random filename with the given prefix. The new filename |
| 120 | ** is written into zBuf[]. The calling function must insure that |
| 121 | ** zBuf[] is big enough to hold the prefix plus 20 or so extra |
| 122 | ** characters. |
| 123 | ** |
| 124 | ** Very random names are chosen so that the chance of a |
| 125 | ** collision with an existing filename is very very small. |
| 126 | */ |
| 127 | void sqliteRandomName(char *zBuf, char *zPrefix){ |
| 128 | int i, j; |
| 129 | static const char zRandomChars[] = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| 130 | strcpy(zBuf, zPrefix); |
| 131 | j = strlen(zBuf); |
| 132 | for(i=0; i<15; i++){ |
| 133 | int c = sqliteRandomByte() % (sizeof(zRandomChars) - 1); |
| 134 | zBuf[j++] = zRandomChars[c]; |
| 135 | } |
| 136 | zBuf[j] = 0; |
| 137 | } |