Download code

Jump to: navigation, search

Back to R250/521_(C)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

r250_521.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/R250/521_(C)?oldid=6914
14 */
15 
16 #include <stdlib.h>
17 #define R250_LEN     250
18 #define R521_LEN     521
19 #define ULONG_BITS   (8*sizeof(unsigned long))
20 
21 unsigned long r250_buffer[R250_LEN];
22 unsigned long r521_buffer[R521_LEN];
23 
24 int r250_index;
25 int r521_index;
26 
27 void r250_521_init() {
28     unsigned long mask1 = 1;
29     unsigned long mask2 = (unsigned long)(-1L);
30 
31     int i = R521_LEN;
32     
33     while (i-- > R250_LEN) {
34         r521_buffer[i] = rand();
35     }
36     while (i-- > ULONG_BITS) {
37         r250_buffer[i] = rand();
38         r521_buffer[i] = rand();
39     }
40     
41     while (i-- > 0) {
42         r250_buffer[i] = (rand() | mask1) & mask2;
43         r521_buffer[i] = (rand() | mask1) & mask2;
44         mask2 ^= mask1;
45         mask1 >>= 1;
46     }
47     r250_buffer[0] = mask1;
48     r521_buffer[0] = mask2;
49     r250_index = 0;
50     r521_index = 0;
51 
52 }
53 
54 #define R250_IA  (sizeof(unsigned long)*103)
55 #define R250_IB  (sizeof(unsigned long)*R250_LEN - R250_IA)
56 #define R521_IA  (sizeof(unsigned long)*168)
57 #define R521_IB  (sizeof(unsigned long)*R521_LEN - R521_IA)
58 unsigned long r250_521_random() {
59     unsigned long r, s;
60     unsigned char * b1 = (unsigned char*)r250_buffer;
61     unsigned char * b2 = (unsigned char*)r521_buffer;
62     unsigned long * tmp1, * tmp2;
63 
64     int i1 = r250_index;
65     int i2 = r521_index;
66     int j1, j2;
67         
68     j1 = i1 - R250_IB;
69     if (j1 < 0)
70         j1 = i1 + R250_IA;
71     j2 = i2 - R521_IB;
72     if (j2 < 0)
73         j2 = i2 + R521_IA;
74     tmp1 = (unsigned long *)(b1 + i1);
75     r = (*(unsigned long *)(b1 + j1)) ^ (*tmp1);
76     *tmp1 = r;
77     tmp2 = (unsigned long *)(b2 + i2);
78     s = (*(unsigned long *)(b2 + j2)) ^ (*tmp2);
79     *tmp2 = s;
80     i1 = (i1 != sizeof(unsigned long)*(R250_LEN-1)) ? (i1 + sizeof(unsigned long)) : 0;
81     r250_index = i1;
82     i2 = (i2 != sizeof(unsigned long)*(R521_LEN-1)) ? (i2 + sizeof(unsigned long)) : 0;
83     r521_index = i2;
84     return r ^ s;
85 }


hijacker
hijacker
hijacker
hijacker