e5b6a7ea6160cdf777114be83548de20289507a4
[debian/amanda] / common-src / simpleprng.h
1 /*
2  * Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
18  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19  */
20
21 #ifndef SIMPLEPRNG_H
22 #define SIMPLEPRNG_H
23
24 #include "amanda.h"
25
26 /* A very simple, thread-safe PRNG.  This is intended for use in reproducable
27  * generating bytestreams for testing purposes.  It is *not* cryptographically
28  * secure! */
29
30 typedef guint32 simpleprng_state_t;
31
32 /* Initialize and seed the PRNG
33  *
34  * @param state: pointer to PRNG state
35  * @param seed: initial value
36  */
37 void simpleprng_seed(
38     simpleprng_state_t *state,
39     guint32 seed);
40
41 /* Get a random guint32
42  *
43  * @param state: pointer to PRNG state
44  * @returns: random integer
45  */
46 guint32 simpleprng_rand(
47     simpleprng_state_t *state);
48
49 /* Get a random byte
50  *
51  * @param state: pointer to PRNG state
52  * @returns: random integer
53  */
54 /* use the high-order bytes, as they're "more random" */
55 #define simpleprng_rand_byte(state) \
56     ((guint8)(simpleprng_rand((state)) >> 24))
57
58 /* Fill the given buffer with a sequence of bytes
59  *
60  * @param state: pointer to PRNG state
61  * @param buf: buffer to fill
62  * @param len: number of bytes to write
63  */
64 void simpleprng_fill_buffer(
65     simpleprng_state_t *state,
66     gpointer buf,
67     size_t len);
68
69 /* Verify that a buffer matches the values from the PRNG.
70  *
71  * @param state: pointer to PRNG state
72  * @param buf: buffer to verify
73  * @param len: number of bytes to verify
74  * @returns: true if all bytes match
75  */
76 gboolean simpleprng_verify_buffer(
77     simpleprng_state_t *state,
78     gpointer buf,
79     size_t len);
80
81 #endif  /* SIMPLEPRNG_H */