Back to... Zip-Ada

Source file : lzma_dec.adb



   1  --  Standalone, command-line, LZMA Decoder (for .lzma files).
   2  
   3  with LZMA.Decoding;
   4  
   5  with Ada.Command_Line;                  use Ada.Command_Line;
   6  with Ada.Text_IO;                       use Ada.Text_IO;
   7  with Ada.Streams.Stream_IO;             use Ada.Streams.Stream_IO;
   8  with Interfaces;                        use Interfaces;
   9  
  10  procedure LZMA_Dec is
  11  
  12    use LZMA;
  13  
  14    f_in, f_out : Ada.Streams.Stream_IO.File_Type;
  15  
  16    --  NB: The Byte I/O below is not buffered, so it is very slow.
  17    --  You need to implement a circular buffer of type Stream_Element_Array for a fast I/O.
  18    --  For instance, see the BlockRead in the Zip package for how to do it.
  19  
  20    function Demo_Read_Byte return Byte is
  21      b : Byte;
  22    begin
  23      Byte'Read (Stream (f_in), b);
  24      return b;
  25    end Demo_Read_Byte;
  26  
  27    procedure Demo_Write_Byte (b : Byte) is
  28    begin
  29      Byte'Write (Stream (f_out), b);
  30    end Demo_Write_Byte;
  31  
  32    package My_LZMA_Decoding is new LZMA.Decoding (Demo_Read_Byte, Demo_Write_Byte);
  33    use My_LZMA_Decoding;
  34  
  35    procedure Print_Data_Bytes_Count (title : String; v : Data_Bytes_Count) is
  36      package CIO is new Integer_IO (Data_Bytes_Count);
  37    begin
  38      Put (title);
  39      Put (" : ");
  40      CIO.Put (v, 0);
  41      Put (" bytes");
  42      New_Line;
  43    end Print_Data_Bytes_Count;
  44  
  45    lzma_decoder : LZMA_Decoder_Info;
  46    res : LZMA_Result;
  47  
  48    use type Data_Bytes_Count;
  49  
  50    default_hints : constant LZMA_Hints :=
  51       (has_size               => True,
  52        given_size             => dummy_size,
  53        marker_expected        => False,
  54        fail_on_bad_range_code => False);
  55  
  56  begin
  57    New_Line;
  58    Put_Line ("LZMA_Dec: a standalone LZMA decoder.");
  59    if Argument_Count = 0 then
  60      Put_Line ("Usage: lzma_dec infile.lzma outfile");
  61      New_Line;
  62      Put_Line ("NB: The I/O is not buffered => may be slow.");
  63      Put_Line ("    Consider the UnZipAda tool for a LZMA demo with fast I/O.");
  64      New_Line;
  65      Put ("Press Return");
  66      Skip_Line;
  67      return;
  68    elsif Argument_Count /= 2 then
  69      Put_Line ("You must specify two parameters");
  70      return;
  71    end if;
  72    Open (f_in, In_File, Argument (1));
  73    Create (f_out, Out_File, Argument (2));
  74  
  75    Decode (lzma_decoder, default_hints, res);
  76  
  77    Put_Line (
  78      "lc="   & Natural'Image (Literal_context_bits (lzma_decoder)) &
  79      ", lp=" & Natural'Image (Literal_pos_bits (lzma_decoder)) &
  80      ", pb=" & Natural'Image (Pos_bits (lzma_decoder))
  81    );
  82    Put_Line ("Probability model size =" &
  83      Unsigned_32'Image (Probability_model_size (lzma_decoder)) & " bytes"
  84    );
  85    Put_Line ("Dictionary size in properties =" &
  86      Unsigned_32'Image (Dictionary_size_in_properties (lzma_decoder)) & " bytes"
  87    );
  88    Put_Line ("Dictionary size for decoding  =" &
  89      Unsigned_32'Image (Dictionary_size (lzma_decoder)) & " bytes"
  90    );
  91    New_Line;
  92  
  93    if Unpack_size_defined (lzma_decoder) then
  94      Print_Data_Bytes_Count ("Uncompressed size as in data header",
  95        Unpack_size_as_defined (lzma_decoder));
  96    else
  97      Put_Line ("Uncompressed size not defined, end marker is expected.");
  98    end if;
  99    New_Line;
 100  
 101    Print_Data_Bytes_Count ("Read    ", Data_Bytes_Count (Index (f_in) - 1));
 102    Print_Data_Bytes_Count ("Written ", Data_Bytes_Count (Index (f_out) - 1));
 103    case res is
 104      when LZMA_finished_without_marker =>
 105         Put_Line ("Finished without end marker");
 106      when LZMA_finished_with_marker =>
 107         if Unpack_size_defined (lzma_decoder) then
 108           if Data_Bytes_Count (Index (f_out) - 1) /= Unpack_size_as_defined (lzma_decoder) then
 109             Put_Line ("Warning: finished with end marker before the specified size");
 110           end if;
 111         end if;
 112         Put_Line ("Finished with end marker");
 113    end case;
 114  
 115    if Range_decoder_corrupted (lzma_decoder) then
 116      Put_Line ("Warning: LZMA stream is corrupted");
 117    end if;
 118  
 119    Close (f_in);
 120    Close (f_out);
 121  end LZMA_Dec;

Web view of Ada source code generated by GNATHTML, project: ALI_Parse version 1.0.
Zip-Ada: Ada library for zip archive files (.zip). Ada programming.
Some news about Zip-Ada and other Ada projects on Gautier's blog.