summaryrefslogtreecommitdiff
path: root/src/crypto-types-random_source-file.adb
blob: e665990518a9f3eb56d31423f82dce9068b07cdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package body Crypto.Types.Random_Source.File is
   use Ada.Strings.Unbounded;
   use Ada.Streams.Stream_IO;

   ---------------------------------------------------------------------------
   ------------------------ Initialization -----------------------------------
   ---------------------------------------------------------------------------


   procedure Initialize(This : in out Random_Source_File) is
      Path : constant String := "/dev/random";
      Mode  : constant File_Mode := In_File;
   begin
      if This.Source_File = null then
         This.Source_File := new Ada.Streams.Stream_IO.File_Type;
      end if;
      if not Is_Open(This.Source_File.all) then
         Open(This.Source_File.all, Mode, Path, "shared=yes");
         This.Source_Path := To_Unbounded_String(Path);
      end if;
   end Initialize;

   ---------------------------------------------------------------------------

   procedure Initialize(This : in out Random_Source_File;
		File_Path : in String) is
      Mode  : constant File_Mode := In_File;
   begin
      if Is_Open(This.Source_File.all) then
         Close(This.Source_File.all);
      end if;
      if not Is_Open(This.Source_File.all) then
         Open(This.Source_File.all, Mode, File_Path, "shared=yes");
         This.Source_Path := To_Unbounded_String(File_Path);
      end if;
    end Initialize;

   ---------------------------------------------------------------------------
   ------------------------------- Read Byte ---------------------------------
   ---------------------------------------------------------------------------


   procedure Read(This : in out Random_Source_File; B : out Byte) is

   begin
      if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
         raise  RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
      else
         Byte'Read(Stream(This.Source_File.all), B);
      end if;
   end Read;

   ---------------------------------------------------------------------------

   procedure Read(This : in out Random_Source_File; Byte_Array : out Bytes) is
   begin
      if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
         raise  RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
      else
         Bytes'Read(Stream(This.Source_File.all), Byte_Array);
      end if;
   end Read;

   ---------------------------------------------------------------------------

   procedure Read(This : in out Random_Source_File; B : out B_Block128) is
   begin
      if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
         raise  RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
      else
         B_Block128'Read(Stream(This.Source_File.all), B);
      end if;
   end Read;

   ---------------------------------------------------------------------------
   ------------------------------- Read Word ---------------------------------
   ---------------------------------------------------------------------------

   procedure Read(This : in out Random_Source_File; W : out Word) is
   begin
      if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
         raise RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
      else
         Word'Read(Stream(This.Source_File.all), W);
      end if;
   end Read;

   ---------------------------------------------------------------------------

   procedure Read(This : in out Random_Source_File; Word_Array : out Words) is
   begin
      if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
         raise RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
      else
         Words'Read(Stream(This.Source_File.all), Word_Array);
      end if;
   end Read;

   ---------------------------------------------------------------------------
   ------------------------------- Read DWord --------------------------------
   ---------------------------------------------------------------------------


   procedure Read(This : in out Random_Source_File; D : out DWord) is
   begin
      if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
         raise  RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
      else
         DWord'Read(Stream(This.Source_File.all), D);
      end if;
   end Read;

   procedure Read(This : in out Random_Source_File; DWord_Array : out DWords) is
   begin
      if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
         raise  RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
      else
         DWords'Read(Stream(This.Source_File.all), DWord_Array);
      end if;
   end Read;


   ---------------------------------------------------------------------------
   ------------------------------- Finalize ----------------------------------
   ---------------------------------------------------------------------------

   procedure Finalize(This : in out  Random_Source_File) is
   begin
      if Is_Open(This.Source_File.all) then
         Close(This.Source_File.all);
      end if;
   end Finalize;

   ---------------------------------------------------------------------------
   --------------------------- Path_Starts_With ------------------------------
   ---------------------------------------------------------------------------

   function Path_Starts_With(This : Random_Source_File; S : String) return Boolean is
      Path : constant String := To_String(This.Source_Path);
   begin
      return Path(Path'First..S'Last) = S;
   end;

end Crypto.Types.Random_Source.File;