961412475f08deeb100fe493daca8e8c369be436
[debian/amanda] / common-src / simpleprng.h
1 /*
2  * Copyright (c) 2008, 2010 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. Mathilda 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 generating
27  * reproducable bytestreams for testing purposes.  It is *not*
28  * cryptographically secure! */
29
30 typedef struct {
31     guint32 val;
32     guint64 count;
33 } simpleprng_state_t;
34
35 /* Initialize and seed the PRNG
36  *
37  * @param state: pointer to PRNG state
38  * @param seed: initial value
39  */
40 void simpleprng_seed(
41     simpleprng_state_t *state,
42     guint32 seed);
43
44 /* Get a seed that will reproduce the PRNG's current state
45  *
46  * @param state: pointer to PRNG state
47  * @returns: seed;
48  */
49 guint32 simpleprng_get_seed(
50     simpleprng_state_t *state);
51
52 /* Get a random guint32
53  *
54  * @param state: pointer to PRNG state
55  * @returns: random integer
56  */
57 guint32 simpleprng_rand(
58     simpleprng_state_t *state);
59
60 /* Get a random byte
61  *
62  * @param state: pointer to PRNG state
63  * @returns: random integer
64  */
65 /* use the high-order bytes, as they're "more random" */
66 #define simpleprng_rand_byte(state) \
67     ((guint8)(simpleprng_rand((state)) >> 24))
68
69 /* Fill the given buffer with a sequence of bytes
70  *
71  * @param state: pointer to PRNG state
72  * @param buf: buffer to fill
73  * @param len: number of bytes to write
74  */
75 void simpleprng_fill_buffer(
76     simpleprng_state_t *state,
77     gpointer buf,
78     size_t len);
79
80 /* Verify that a buffer matches the values from the PRNG.
81  *
82  * @param state: pointer to PRNG state
83  * @param buf: buffer to verify
84  * @param len: number of bytes to verify
85  * @returns: true if all bytes match
86  */
87 gboolean simpleprng_verify_buffer(
88     simpleprng_state_t *state,
89     gpointer buf,
90     size_t len);
91
92 #endif  /* SIMPLEPRNG_H */