|
Packit Service |
392537 |
/*
|
|
Packit Service |
392537 |
* Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
|
|
Packit Service |
392537 |
* Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
392537 |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
392537 |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
392537 |
* of the License, or (at your option) any later version.
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* This program is distributed in the hope that it will be useful, but
|
|
Packit Service |
392537 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
Packit Service |
392537 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
Packit Service |
392537 |
* for more details.
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* You should have received a copy of the GNU General Public License along
|
|
Packit Service |
392537 |
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
Packit Service |
392537 |
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* Contact information: Carbonite Inc., 756 N Pastoria Ave
|
|
Packit Service |
392537 |
* Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
|
|
Packit Service |
392537 |
*/
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
#ifndef SIMPLEPRNG_H
|
|
Packit Service |
392537 |
#define SIMPLEPRNG_H
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
#include "amanda.h"
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
/* A very simple, thread-safe PRNG. This is intended for use in generating
|
|
Packit Service |
392537 |
* reproducable bytestreams for testing purposes. It is *not*
|
|
Packit Service |
392537 |
* cryptographically secure! */
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
typedef struct {
|
|
Packit Service |
392537 |
guint32 val;
|
|
Packit Service |
392537 |
guint64 count;
|
|
Packit Service |
392537 |
} simpleprng_state_t;
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
/* Initialize and seed the PRNG
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* @param state: pointer to PRNG state
|
|
Packit Service |
392537 |
* @param seed: initial value
|
|
Packit Service |
392537 |
*/
|
|
Packit Service |
392537 |
void simpleprng_seed(
|
|
Packit Service |
392537 |
simpleprng_state_t *state,
|
|
Packit Service |
392537 |
guint32 seed);
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
/* Get a seed that will reproduce the PRNG's current state
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* @param state: pointer to PRNG state
|
|
Packit Service |
392537 |
* @returns: seed;
|
|
Packit Service |
392537 |
*/
|
|
Packit Service |
392537 |
guint32 simpleprng_get_seed(
|
|
Packit Service |
392537 |
simpleprng_state_t *state);
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
/* Get a random guint32
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* @param state: pointer to PRNG state
|
|
Packit Service |
392537 |
* @returns: random integer
|
|
Packit Service |
392537 |
*/
|
|
Packit Service |
392537 |
guint32 simpleprng_rand(
|
|
Packit Service |
392537 |
simpleprng_state_t *state);
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
/* Get a random byte
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* @param state: pointer to PRNG state
|
|
Packit Service |
392537 |
* @returns: random integer
|
|
Packit Service |
392537 |
*/
|
|
Packit Service |
392537 |
/* use the high-order bytes, as they're "more random" */
|
|
Packit Service |
392537 |
#define simpleprng_rand_byte(state) \
|
|
Packit Service |
392537 |
((guint8)(simpleprng_rand((state)) >> 24))
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
/* Fill the given buffer with a sequence of bytes
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* @param state: pointer to PRNG state
|
|
Packit Service |
392537 |
* @param buf: buffer to fill
|
|
Packit Service |
392537 |
* @param len: number of bytes to write
|
|
Packit Service |
392537 |
*/
|
|
Packit Service |
392537 |
void simpleprng_fill_buffer(
|
|
Packit Service |
392537 |
simpleprng_state_t *state,
|
|
Packit Service |
392537 |
gpointer buf,
|
|
Packit Service |
392537 |
size_t len);
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
/* Verify that a buffer matches the values from the PRNG.
|
|
Packit Service |
392537 |
*
|
|
Packit Service |
392537 |
* @param state: pointer to PRNG state
|
|
Packit Service |
392537 |
* @param buf: buffer to verify
|
|
Packit Service |
392537 |
* @param len: number of bytes to verify
|
|
Packit Service |
392537 |
* @returns: true if all bytes match
|
|
Packit Service |
392537 |
*/
|
|
Packit Service |
392537 |
gboolean simpleprng_verify_buffer(
|
|
Packit Service |
392537 |
simpleprng_state_t *state,
|
|
Packit Service |
392537 |
gpointer buf,
|
|
Packit Service |
392537 |
size_t len);
|
|
Packit Service |
392537 |
|
|
Packit Service |
392537 |
#endif /* SIMPLEPRNG_H */
|