chiark / gitweb /
configure.ac: Replace with a new version.
[catacomb] / grand.c
1 /* -*-c-*-
2  *
3  * $Id: grand.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
4  *
5  * Generic interface to random number generators
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------*
11  *
12  * This file is part of Catacomb.
13  *
14  * Catacomb is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (at your option) any later version.
18  *
19  * Catacomb is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Library General Public License for more details.
23  *
24  * You should have received a copy of the GNU Library General Public
25  * License along with Catacomb; if not, write to the Free
26  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include <stddef.h>
33
34 #include <mLib/bits.h>
35
36 #include "grand.h"
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 /* --- @grand_byte@ --- *
41  *
42  * Arguments:   @grand *r@ = pointet to generic generator
43  *
44  * Returns:     A uniformly-distributed pseudorandom integer in the interval
45  *              %$[0, 256)$%.
46  */
47
48 octet grand_byte(grand *r)
49 {
50   if (r->ops->byte != grand_byte)
51     return (r->ops->byte(r));
52   else if (r->ops->word != grand_word)
53     return (r->ops->word(r) & 0xff);
54   else if (r->ops->fill != grand_fill) {
55     octet o;
56     r->ops->fill(r, &o, 1);
57     return (o);
58   } else
59     return (grand_range(r, 256));
60 }
61
62 /* --- @grand_word@ --- *
63  *
64  * Arguments:   @grand *r@ = pointet to generic generator
65  *
66  * Returns:     A uniformly-distributed pseudorandom integer in the interval
67  *              %$[0, 2^{32})$%.
68  */
69
70 uint32 grand_word(grand *r)
71 {
72   if (r->ops->word != grand_word)
73     return (r->ops->word(r));
74   else {
75     octet b[4];
76     grand_fill(r, b, sizeof(b));
77     return (LOAD32(b));
78   }
79 }
80
81 /* --- @grand_range@ --- *
82  *
83  * Arguments:   @grand *r@ = pointet to generic generator
84  *              @uint32 l@ = limit for acceptable results
85  *
86  * Returns:     A uniformly-distributed pseudorandom integer in the interval
87  *              %$[0, l)$%.
88  */
89
90 uint32 grand_range(grand *r, uint32 l)
91 {
92   if (r->ops->range != grand_range)
93     return (r->ops->range(r, l));
94   else {
95     uint32 m, z;
96     uint32 (*w)(grand */*r*/);
97     uint32 x;
98
99     /* --- Decide where to get data from --- *
100      *
101      * The choice of %$2^{32} - 1$% as a limit when using @grand_word@ isn't
102      * wonderful, but working with %$2^{32}$% is awkward and the loss of a
103      * few return values isn't significant.  The algorithm below still
104      * successfully returns uniformly distributed results.
105      */
106
107     if (r->ops->max) {
108       w = r->ops->raw;
109       m = r->ops->max;
110     } else {
111       w = grand_word;
112       m = 0xffffffff;
113     }
114
115     /* --- Work out maximum acceptable return value --- *
116      *
117      * This will be the highest multiple of @l@ less than @m@.
118      */
119
120     z = m - (m % l);
121
122     /* --- Generate numbers until something acceptable is found --- *
123      *
124      * This will require an expected number of attempts less than 2.
125      */
126
127     do x = w(r); while (x >= z);
128     return (x % l);
129   }
130 }
131
132 /* --- @grand_fill@ --- *
133  *
134  * Arguments:   @grand *r@ = pointet to generic generator
135  *              @void *p@ = pointer to a buffer
136  *              @size_t sz@ = size of the buffer
137  *
138  * Returns:     ---
139  *
140  * Use:         Fills a buffer with uniformly distributed pseudorandom bytes
141  *              (see @grand_byte@).
142  */
143
144 void grand_fill(grand *r, void *p, size_t sz)
145 {
146   if (r->ops->fill != grand_fill)
147     r->ops->fill(r, p, sz);
148   else {
149     octet *q = p;
150     while (sz) {
151       *q++ = r->ops->byte(r);
152       sz--;
153     }
154   }
155 }
156
157 /*----- That's all, folks -------------------------------------------------*/