adagsl_335d13f0/toolkit/adalib/src/hex-dump.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
with System.Storage_Elements; use System.Storage_Elements;
with Ada.Characters.Handling;
with Ada.Text_IO;             use Ada.Text_IO;
with Ada.Integer_Text_IO;     use Ada.Integer_Text_IO;
with Ada.Streams.Stream_IO;
with Ada.Directories ;

with GNAT.Debug_Utilities;

package body Hex.dump is
   procedure Dump
     (Adr         : System.Address;
      Length      : Integer;
      show_offset : Boolean               := True;
      bare : Boolean := False;
      Blocklen    : Integer               := DEFAULT_BLOCK_LENGTH;
      Outfile     : Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output)
   is
      blockadr : System.Address := Adr;
      bigBlock : array (1 .. Length) of Interfaces.Unsigned_8;
      for bigBlock'Address use Adr;

      No_Blocks    : constant Integer := Length / Blocklen + 1;
      Blockstart   : Integer;
      Lengthleft   : Integer          := Length;
      Lengthtodump : Integer;

      adrcol : constant Integer := 1;
      piccol : constant Integer := GNAT.Debug_Utilities.Address_Image_Length + adrcol;

      function CharImage (ci : Interfaces.Unsigned_8) return Character is
         c : constant Character := Character'Val (Integer (ci));
      begin
         if not Ada.Characters.Handling.Is_ISO_646 (c) then
            return '.';
         end if;
         if Ada.Characters.Handling.Is_Alphanumeric (c) then
            return c;
         end if;

         return '.';
      end CharImage;

   begin

      for B in 1 .. No_Blocks loop
         Blockstart := (B - 1) * Blocklen + 1;
         if bare
         then
            null;
         else 
            if show_offset then
               Put (Outfile, Blockstart-1, Base => 16);
            else
               Put (Outfile, GNAT.Debug_Utilities.Image (blockadr));
            end if;
         end if;
         if Lengthleft >= Blocklen then
            Lengthtodump := Blocklen;
         else
            Lengthtodump := Lengthleft;
         end if;

         -- Set_Col (Outfile, Count (piccol));
         if bare
         then
            Put(OutFile,'"');
         else
            Put (" * ");
            for b in 1 .. Lengthtodump loop
               Put (CharImage (bigBlock (Blockstart + b - 1)));
            end loop;
            Set_Col (Outfile, Count (piccol + Blocklen + 3));
            Put (" * ");
         end if ;

         for b in 1 .. Lengthtodump loop
            Put (OutFile,Hex.Image (bigBlock (Blockstart + b - 1)));
         end loop;

         if bare
         then
            Put(OutFile,'"'); Put_Line(OutFile," &");
         else
            Set_Col (Outfile, Count (piccol + Blocklen + 3 + Blocklen * 2 + 3));
            Put_Line (" *");
         end if ;
         blockadr   := blockadr + Storage_Offset (Lengthtodump);
         Lengthleft := Lengthleft - Lengthtodump;
         exit when Lengthleft = 0;
      end loop;
   end Dump;



   procedure Dump
     (filename    : String;
      show_offset : Boolean               := True;
      bare : Boolean := False ;
      Blocklen    : Integer               := DEFAULT_BLOCK_LENGTH;
      Outfile     : Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output)
   is
      use Ada.Streams;
      file   : Ada.Streams.Stream_IO.File_Type;
      stream : Ada.Streams.Stream_IO.Stream_Access;
      filesize : Ada.Directories.File_Size ;
   begin
      
      filesize := Ada.Directories.Size( filename );
      if not bare
      then
         Ada.Text_IO.Put (Outfile, "Dump of ");
         Ada.Text_IO.Put (Outfile, filename);
         Ada.Text_Io.Put (Outfile," Size : ") ;
         Ada.Text_Io.Put (Outfile,Integer'Image(Integer(filesize))) ;
         Ada.Text_Io.New_Line;
         --Ada.Text_IO.Put_Line (Outfile, " *********************************");
      end if ;

      Ada.Streams.Stream_IO.Open
        (file,
         Ada.Streams.Stream_IO.In_File,
         filename);
      stream := Ada.Streams.Stream_IO.Stream (file);
      declare
        buffer : Ada.Streams.Stream_Element_Array
                 (1 .. Ada.Streams.Stream_Element_Offset (filesize));
        bufferlen : Ada.Streams.Stream_Element_Offset;
      begin
          stream.Read (buffer, bufferlen);
          if bufferlen > 0 then
              Hex.dump.Dump
                (buffer'Address,
                 Integer (bufferlen),
                 bare => bare,
                 show_offset => show_offset ,
                 blocklen => Blocklen ,
                 outfile => Outfile );
          end if ;
      end ;
      Ada.Streams.Stream_IO.Close (file);
   exception
      when others =>
         Ada.Text_Io.Put_Line("Exception");
         raise;
   end Dump;


end Hex.dump;