2 * Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
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.
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
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
17 * Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
22 #include "simpleprng.h"
24 /* A *very* basic linear congruential generator; values are as cited in
25 * http://en.wikipedia.org/wiki/Linear_congruential_generator for Numerical Recipes */
32 simpleprng_state_t *state,
42 simpleprng_state_t *state)
47 guint32 simpleprng_rand(
48 simpleprng_state_t *state)
51 return (state->val = (A * state->val) + C);
54 void simpleprng_fill_buffer(
55 simpleprng_state_t *state,
61 *(p++) = simpleprng_rand_byte(state);
66 hexstr(guint8 *p, int len)
71 for (i = 0; i < len; i++) {
73 result = newvstrallocf(result, "%s %02x", result, (guint)(*(p++)));
75 result = vstrallocf("[%02x", (guint)(*(p++)));
77 result = newvstrallocf(result, "%s]", result);
82 gboolean simpleprng_verify_buffer(
83 simpleprng_state_t *state,
89 guint64 count = state->count;
90 guint8 expected = simpleprng_rand_byte(state);
92 if (expected != got) {
93 int remaining = MIN(len, 16);
94 guint8 expbytes[16] = { expected };
95 char *gotstr = hexstr(p, remaining);
99 for (i = 1; i < remaining; i++)
100 expbytes[i] = simpleprng_rand_byte(state);
101 expstr = hexstr(expbytes, remaining);
104 "random value mismatch at offset %ju: got %s, expected %s\n",
105 (uintmax_t)count, gotstr, expstr);