04e7a9f211b7463bb78e532d97d9ec488d2bdd57
[debian/amanda] / common-src / simpleprng.c
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 #include "simpleprng.h"
22
23 /* A *very* basic linear congruential generator; values are as cited in
24  * http://en.wikipedia.org/wiki/Linear_congruential_generator for Numerical Recipes */
25
26 #define A 1664525
27 #define C 1013904223
28
29 void
30 simpleprng_seed(
31     simpleprng_state_t *state,
32     guint32 seed)
33 {
34     g_assert(seed != 0);
35     state->val = seed;
36     state->count = 0;
37 }
38
39 guint32
40 simpleprng_get_seed(
41     simpleprng_state_t *state)
42 {
43     return state->val;
44 }
45
46 guint32 simpleprng_rand(
47     simpleprng_state_t *state)
48 {
49     state->count++;
50     return (state->val = (A * state->val) + C);
51 }
52
53 void simpleprng_fill_buffer(
54     simpleprng_state_t *state,
55     gpointer buf,
56     size_t len)
57 {
58     guint8 *p = buf;
59     while (len--) {
60         *(p++) = simpleprng_rand_byte(state);
61     }
62 }
63
64 static char *
65 hexstr(guint8 *p, int len)
66 {
67     char *result = NULL;
68     int i;
69
70     for (i = 0; i < len; i++) {
71         if (result)
72             result = newvstrallocf(result, "%s %02x", result, (guint)(*(p++)));
73         else
74             result = vstrallocf("[%02x", (guint)(*(p++)));
75     }
76     result = newvstrallocf(result, "%s]", result);
77
78     return result;
79 }
80
81 gboolean simpleprng_verify_buffer(
82     simpleprng_state_t *state,
83     gpointer buf,
84     size_t len)
85 {
86     guint8 *p = buf;
87     while (len--) {
88         guint64 count = state->count;
89         guint8 expected = simpleprng_rand_byte(state);
90         guint8 got = *p;
91         if (expected != got) {
92             int remaining = MIN(len, 16);
93             guint8 expbytes[16] = { expected };
94             char *gotstr = hexstr(p, remaining);
95             char *expstr;
96             int i;
97
98             for (i = 1; i < remaining; i++)
99                 expbytes[i] = simpleprng_rand_byte(state);
100             expstr = hexstr(expbytes, remaining);
101
102             g_fprintf(stderr,
103                     "random value mismatch at offset %ju: got %s, expected %s\n",
104                     (uintmax_t)count, gotstr, expstr);
105             g_free(gotstr);
106             return FALSE;
107         }
108         p++;
109     }
110
111     return TRUE;
112 }