From a600eb0e88d69219ffb96ecfd0cf111b82107106 Mon Sep 17 00:00:00 2001 From: fluffymormegil Date: Tue, 12 Oct 2010 23:47:00 +0100 Subject: [PATCH] Shuffling things around between dice.cc and S20prng.hh --- include/libmormegil/S20prng.hh | 29 +++++++++++++++++++++++++++++ src/dice.cc | 15 ++------------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/include/libmormegil/S20prng.hh b/include/libmormegil/S20prng.hh index 14f8901..ab6889a 100644 --- a/include/libmormegil/S20prng.hh +++ b/include/libmormegil/S20prng.hh @@ -123,6 +123,35 @@ namespace libmormegil ++subcounter; subcounter &= 15; } + void initialize(const uint32_t *k, const uint32_t *n, + const uint64_t *c, const int *s) + { + int i; + for (i = 0; i < 8; ++i) + { + key[i] = k[i]; + } + nonce[0] = n[0]; + nonce[1] = n[1]; + counter = *c; + subcounter = (*s) & 15; + if (subcounter) + { + runstate(); + } + } + void extract_state(uint32_t *k, uint32_t *n, uint64_t *c, int *s) const + { + int i; + for (i = 0; i < 8; ++i) + { + k[i] = key[i]; + } + n[0] = nonce[0]; + n[1] = nonce[1]; + *c = counter; + *s = subcounter; + } }; } #endif diff --git a/src/dice.cc b/src/dice.cc index 174d310..9654172 100644 --- a/src/dice.cc +++ b/src/dice.cc @@ -55,23 +55,12 @@ extern "C" int dice(int count, int sides) extern "C" void dice_setstate(const uint32_t *key, const uint32_t *nonce, const uint64_t *counter, const int *subcounter) { - dice_generator.counter = *counter; - memcpy(dice_generator.key, key, 8 * sizeof(uint32_t)); - memcpy(dice_generator.nonce, nonce, 2 * sizeof(uint32_t)); - dice_generator.subcounter = (*subcounter) & 15; - if (dice_generator.subcounter != 0) - { - /* if the subcounter wasn't zero, the generator will DTwrongT. */ - dice_generator.runstate(); - } + dice_generator.initialize(key, nonce, counter, subcounter); } extern "C" void dice_getstate(uint32_t *key, uint32_t *nonce, uint64_t *counter, int *subcounter) { - memcpy(key, dice_generator.key, 8 * sizeof(uint32_t)); - memcpy(dice_generator.nonce, nonce, 2 * sizeof(uint32_t)); - *subcounter = dice_generator.subcounter; - *counter = dice_generator.counter; + dice_generator.extract_state(key, nonce, counter, subcounter); } // vim:ts=8:sw=4:expandtab:fo=cq -- 2.11.0