Shuffling things around between dice.cc and S20prng.hh
authorfluffymormegil <mpread@chiark.greenend.org.uk>
Tue, 12 Oct 2010 22:47:00 +0000 (23:47 +0100)
committerfluffymormegil <mpread@chiark.greenend.org.uk>
Tue, 12 Oct 2010 22:47:00 +0000 (23:47 +0100)
include/libmormegil/S20prng.hh
src/dice.cc

index 14f8901..ab6889a 100644 (file)
@@ -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
index 174d310..9654172 100644 (file)
@@ -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