1 /*******************************************************************************
2 
3         Copyright:
4             Copyright (c) 2008. Fawzi Mohamed
5             Some parts copyright (c) 2009-2016 dunnhumby Germany GmbH.
6             All rights reserved.
7 
8         License:
9             Tango Dual License: 3-Clause BSD License / Academic Free License v3.0.
10             See LICENSE_TANGO.txt for details.
11 
12         Version: Initial release: July 2008
13 
14         Authors: Fawzi Mohamed
15 
16 *******************************************************************************/
17 module ocean.math.random.engines.URandom;
18 
19 import ocean.meta.types.Qualifiers;
20 import Integer = ocean.text.convert.Integer_tango;
21 import ocean.io.device.File; // use stdc read/write?
22 import ocean.core.Verify;
23 
24 /// basic source that takes data from system random device
25 /// This is an engine, do not use directly, use RandomG!(Urandom)
26 /// should use stdc rad/write?
27 struct URandom{
28     static File.Style readStyle;
29     static this(){
30         readStyle.access=File.Access.Read;
31         readStyle.open  =File.Open.Exists;
32         readStyle.share =File.Share.Read;
33         readStyle.cache =File.Cache.None;
34 
35     }
36     enum int canCheckpoint=false;
37     enum int canSeed=false;
38 
39     void skip(uint n){ }
40     ubyte nextB(){
41         union ToVoidA{
42             ubyte i;
43             void[1] a;
44         }
45         ToVoidA el;
46         auto fn = new File("/dev/urandom", readStyle);
47         if(fn.read(el.a)!=el.a.length){
48             throw new Exception("could not write the requested bytes from urandom");
49         }
50         fn.close();
51         return el.i;
52     }
53     uint next(){
54         union ToVoidA{
55             uint i;
56             void[4] a;
57         }
58         ToVoidA el;
59         auto fn = new File("/dev/urandom", readStyle);
60         if(fn.read(el.a)!=el.a.length){
61             throw new Exception("could not write the requested bytes from urandom");
62         }
63         fn.close();
64         return el.i;
65     }
66     ulong nextL(){
67         union ToVoidA{
68             ulong l;
69             void[8] a;
70         }
71         ToVoidA el;
72         auto fn = new File("/dev/urandom", readStyle);
73         if(fn.read(el.a)!=el.a.length){
74             throw new Exception("could not write the requested bytes from urandom");
75         }
76         fn.close();
77         return el.l;
78     }
79     /// does nothing
80     void seed(scope uint delegate() r) { }
81     /// writes the current status in a string
82     istring toString(){
83         return "URandom";
84     }
85     /// reads the current status from a string (that should have been trimmed)
86     /// returns the number of chars read
87     size_t fromString(cstring s){
88         auto r="URandom";
89         verify(s[0.. r.length]==r,"unxepected string instad of URandom:"~idup(s));
90         return r.length;
91     }
92 }