/*
  Noiz-based random number generator
  Copyright 2002  PhaethonH <phaethon@linux.ucla.edu>

  Permission granted to copy, modify, distribute, or otherwise use this code,
  provided this copyright notice remains intact.  Software is provided as-is
  without warranties of any kind.
*/

#ifndef _NOIZ_H_
#define _NOIZ_H_

#include "qmd5.h"

enum {
  NOIZ_POOLSIZE = 256,
  NOIZ_ROWSIZE = 16,
  NOIZ_AUTOSAVE_PERIOD = 10000, /* in ms */
};

extern const char *NOIZ_CVARNAME;


typedef unsigned char noiz_byte;

struct noiz_s {
  struct MD5Context md5context_opaque, *md5context;
  noiz_byte pool[NOIZ_POOLSIZE];
  noiz_byte in_hash[NOIZ_ROWSIZE];
  noiz_byte out_hash[NOIZ_ROWSIZE];
  char store[(NOIZ_POOLSIZE * 2) + 1];
  int autosave;  /* next level.time to autosave to cvar. */
};

typedef struct noiz_s noiz_t;


extern int noiz_init (noiz_t *);  /* initialize entropy pool. */
extern int noiz_stir (noiz_t *, unsigned char *, int len);  /* stir entropy pool and return a random number. */
extern int noiz_load (noiz_t *);  /* load entropy pool state from cvar. */
extern int noiz_save (noiz_t *);  /* save entropy pool state to cvar. */
extern void noiz_alarm (noiz_t *);  /* call every once in a while (per second?) to enable auto-save of pool state. */
extern int noiz_destroy (noiz_t *); /* save and deinit entropy pool. */

#define NOIZ_STIR(somestruct) (noiz_stir(&noiz, (unsigned char*)&(somestruct), sizeof(somestruct)))
#define NOIZ_RAND() (noiz_stir(&noiz, NULL, 0))

#endif /* _NOIZ_H_ */
