b2ssum_0.1.3_63b5d12e/bin/b2ssum.adb

  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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
------------------------------------------------------------------------------
--  Copyright (c) 2021, Lev Kujawski.
--
--  Permission is hereby granted, free of charge, to any person obtaining a
--  copy of this software and associated documentation files (the "Software")
--  to deal in the Software without restriction, including without limitation
--  the rights to use, copy, modify, merge, publish, distribute, sublicense,
--  and sell copies of the Software, and to permit persons to whom the
--  Software is furnished to do so.
--
--  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
--  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
--  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
--  DEALINGS IN THE SOFTWARE.
--
--  SPDX-License-Identifier: MIT-0
--
--  File:          b2ssum.adb (Ada Subprogram Body)
--  Language:      Ada (1995) [1]
--  Author:        Lev Kujawski
--  Description:   BLAKE2s [2] file hashing utility
--
--  References:
--  [1] Information technology - Programming languages - Ada,
--      ISO/IEC 8652:1995(E), 15 Feb. 1995.
--  [3] M-J. Saarinen and J-P. Aumasson, "The BLAKE2 Cryptographic Hash and
--      Message Authentication Code (MAC)," RFC 7693, Nov. 2015.
------------------------------------------------------------------------------
--  NOTE: Unlike the rest of the BLAKE2s for Ada package, b2ssum is written
--        in non-SPARK Ada (1995) so that streaming I/O may be utilized.
--! rule off Exception_Rule

with Ada.Characters.Handling;
with Ada.Command_Line;

with Ada.Streams;
use type Ada.Streams.Stream_Element_Offset;

with Ada.Streams.Stream_IO;
with Ada.Text_IO;
with Ada.Text_IO.Text_Streams;
with Ada.Strings.Unbounded;

with BLAKE2S;
use type BLAKE2S.Digest_T;
--  Quash false positive from AdaControl:
--! rule off Use_Rule
use type BLAKE2S.Status_T;
--! rule on Use_Rule

with Octets;
use type Octets.T;

with Octet_Arrays;

procedure B2SSum is

   package ACH renames Ada.Characters.Handling;
   package ACL renames Ada.Command_Line;
   package ATI renames Ada.Text_IO;
   package ATS renames Ada.Text_IO.Text_Streams;
   package AST renames Ada.Streams;
   package ASI renames Ada.Streams.Stream_IO;
   package UST renames Ada.Strings.Unbounded;

   subtype Hex_Index_T is Positive range 1 .. 2;
   subtype Hex_T is String (Hex_Index_T);
   subtype Hex_Digit_T is Natural range 0 .. 15;

   function Character_To_Hex_Digit
     (Hex_Character : in Character) return Hex_Digit_T
   is
      Normal : constant Character := ACH.To_Upper (Hex_Character);
      Result : Hex_Digit_T;
   begin
      case Normal is
         when '0' =>
            Result := 0;
         when '1' =>
            Result := 1;
         when '2' =>
            Result := 2;
         when '3' =>
            Result := 3;
         when '4' =>
            Result := 4;
         when '5' =>
            Result := 5;
         when '6' =>
            Result := 6;
         when '7' =>
            Result := 7;
         when '8' =>
            Result := 8;
         when '9' =>
            Result := 9;
         when 'A' =>
            Result := 10;
         when 'B' =>
            Result := 11;
         when 'C' =>
            Result := 12;
         when 'D' =>
            Result := 13;
         when 'E' =>
            Result := 14;
         when 'F' =>
            Result := 15;
         when others =>
            raise Constraint_Error;
      end case;

      return Result;
   end Character_To_Hex_Digit;

   function Hex_Digit_To_Character
     (Hex_Digit : in Hex_Digit_T) return Character
   is
      Result    : Character;
   begin
      case Hex_Digit is
         when  0 =>
            Result := '0';
         when  1 =>
            Result := '1';
         when  2 =>
            Result := '2';
         when  3 =>
            Result := '3';
         when  4 =>
            Result := '4';
         when  5 =>
            Result := '5';
         when  6 =>
            Result := '6';
         when  7 =>
            Result := '7';
         when  8 =>
            Result := '8';
         when  9 =>
            Result := '9';
         when 10 =>
            Result := 'a';
         when 11 =>
            Result := 'b';
         when 12 =>
            Result := 'c';
         when 13 =>
            Result := 'd';
         when 14 =>
            Result := 'e';
         when 15 =>
            Result := 'f';
      end case;

      return Result;
   end Hex_Digit_To_Character;

   function Hex
     (Value : in Octets.T) return Hex_T
   is
   begin
      return Hex_T'(1 => Hex_Digit_To_Character (Hex_Digit_T (Value / 16)),
                    2 => Hex_Digit_To_Character (Hex_Digit_T (Value mod 16)));
   end Hex;

   Buffer_Octets : constant := 16384;
   Buffer_Bits   : constant := Buffer_Octets * Octets.Bits;

   subtype Buffer_Index_T is Positive range 1 .. Buffer_Octets;
   subtype SEA_Buffer_Index_T is AST.Stream_Element_Offset
     range 1 .. Buffer_Octets;

   subtype Buffer_T     is Octet_Arrays.T           (Buffer_Index_T);
   subtype SEA_Buffer_T is AST.Stream_Element_Array (SEA_Buffer_Index_T);

   Buffer : SEA_Buffer_T;
   for Buffer'Size use Buffer_Bits;
   View : Buffer_T;
   for View'Address use Buffer'Address;
   for View'Size use Buffer_Bits;

   Invalid_Hash_List : exception;

   Digest : BLAKE2S.Digest_Default_T;

   procedure Hash_File
     (File_Name : in String)
   is
      File      : ASI.File_Type;
      Stream    : ASI.Stream_Access;
      First     : constant Positive := Positive (Buffer'First);

      procedure Hash_Stream
      is
         Context : BLAKE2S.T;
         Last    : AST.Stream_Element_Offset;
      begin
         Context := BLAKE2S.Initial (BLAKE2S.Digest_Length_Default);
         loop
            AST.Read (Stream.all, Buffer, Last);
            BLAKE2S.Incorporate_Flex (Context, View, First, Natural (Last));
            exit when Last < Buffer'Last;
         end loop;
         BLAKE2S.Finalize (Context, Digest);
      end Hash_Stream;
   begin  --  Hash_File
      if File_Name = "-" then
         Stream := ASI.Stream_Access (ATS.Stream (ATI.Standard_Input));
         Hash_Stream;
      else
         ASI.Open (File, ASI.In_File, File_Name);
         Stream := ASI.Stream (File);
         Hash_Stream;
         ASI.Close (File);
      end if;
   end Hash_File;

   procedure Hash_Files
   is
      procedure Print_Hash
        (File_Name : in String)
      is
      begin
         for J in Digest'Range loop
            ATI.Put (Item => Hex (Digest (J)));
         end loop;
         ATI.Put ("  ");
         ATI.Put (File_Name);
         ATI.New_Line;
      end Print_Hash;
   begin  --  Hash_Files
      for I in Positive range 1 .. ACL.Argument_Count loop
         Hash_File (ACL.Argument (I));
         Print_Hash (ACL.Argument (I));
      end loop;
   end Hash_Files;

   procedure Verify_Lists
   is
      List_File : ATI.File_Type;

      procedure Verify_List
      is
         Hash  : BLAKE2S.Digest_Default_T;
         Hex_1 : Character;
         Hex_2 : Character;
      begin
         while not ATI.End_Of_Line loop
            for I in Positive range Hash'Range loop
               ATI.Get (Hex_1);
               ATI.Get (Hex_2);
               if
                 not ACH.Is_Hexadecimal_Digit (Hex_1) or else
                 not ACH.Is_Hexadecimal_Digit (Hex_2)
               then
                  raise Invalid_Hash_List;
               end if;

               Hash (I) :=
                 Octets.T (Character_To_Hex_Digit (Hex_1)) * 16 +
                 Octets.T (Character_To_Hex_Digit (Hex_2));
            end loop;

            ATI.Get (Hex_1);
            ATI.Get (Hex_2);

            if Hex_1 /= ' ' or else Hex_2 /= ' ' then
               raise Invalid_Hash_List;
            end if;

            declare
               subtype Partial_Index_T is Positive range 1 .. 256;
               Unbounded_File_Name : UST.Unbounded_String :=
                 UST.Null_Unbounded_String;
               Partial : String (Partial_Index_T);
               Last : Natural;
            begin
               loop
                  ATI.Get_Line (Partial, Last);
                  UST.Append (Unbounded_File_Name, Partial (1 .. Last));
                  exit when Last < Partial'Last;
               end loop;

               declare
                  File_Name : constant String :=
                    UST.To_String (Unbounded_File_Name);
               begin
                  ATI.Put (File_Name & ": ");
                  Hash_File (File_Name);
                  if Digest = Hash then
                     ATI.Put_Line ("OK");
                  else
                     ATI.Put_Line ("FAILED");
                  end if;
               end;
            end;
         end loop;
      end Verify_List;
   begin  --  Verify_Lists
      for I in Positive range 2 .. ACL.Argument_Count loop
         declare
         begin
            if ACL.Argument (I) = "-" then
               Verify_List;
            else
               ATI.Open (List_File, ATI.In_File, ACL.Argument (I));
               ATI.Set_Input (List_File);
               Verify_List;
               ATI.Set_Input (ATI.Standard_Input);
               ATI.Close (List_File);
            end if;
         exception
            when Invalid_Hash_List =>
               ATI.Put_Line
                 ("Error on line " & ATI.Positive_Count'Image (ATI.Line) &
                  ", skipping invalid hash list: " & ACL.Argument (I));
         end;
      end loop;
   end Verify_Lists;

begin  --  B2SSum
   pragma Assert (BLAKE2S.Self_Test = BLAKE2S.Success);

   if ACL.Argument_Count = 0 then
      --  Print usage
      ATI.Put_Line ("Usage: " & Ada.Command_Line.Command_Name &
                      " [OPTION]... [FILE]...");
   elsif ACL.Argument (1) = "-c" then
      Verify_Lists;
   else
      Hash_Files;
   end if;

end B2SSum;