2 * Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
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.
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
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
18 * Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
19 * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
23 #include "simpleprng.h"
25 /* A *very* basic linear congruential generator; values are as cited in
26 * http://en.wikipedia.org/wiki/Linear_congruential_generator for Numerical Recipes */
33 simpleprng_state_t *state,
43 simpleprng_state_t *state)
48 guint32 simpleprng_rand(
49 simpleprng_state_t *state)
52 return (state->val = (A * state->val) + C);
55 void simpleprng_fill_buffer(
56 simpleprng_state_t *state,
62 *(p++) = simpleprng_rand_byte(state);
67 hexstr(guint8 *p, int len)
72 for (i = 0; i < len; i++) {
74 result = newvstrallocf(result, "%s %02x", result, (guint)(*(p++)));
76 result = vstrallocf("[%02x", (guint)(*(p++)));
78 result = newvstrallocf(result, "%s]", result);
83 gboolean simpleprng_verify_buffer(
84 simpleprng_state_t *state,
90 guint64 count = state->count;
91 guint8 expected = simpleprng_rand_byte(state);
93 if (expected != got) {
94 int remaining = MIN(len, 16);
95 guint8 expbytes[16] = { expected };
96 char *gotstr = hexstr(p, remaining);
100 for (i = 1; i < remaining; i++)
101 expbytes[i] = simpleprng_rand_byte(state);
102 expstr = hexstr(expbytes, remaining);
105 "random value mismatch at offset %ju: got %s, expected %s\n",
106 (uintmax_t)count, gotstr, expstr);