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