kusano 7d535a
----------------------------------------------------------------
kusano 7d535a
--  ZLib for Ada thick binding.                               --
kusano 7d535a
--                                                            --
kusano 7d535a
--  Copyright (C) 2002-2004 Dmitriy Anisimkov                 --
kusano 7d535a
--                                                            --
kusano 7d535a
--  Open source license information is in the zlib.ads file.  --
kusano 7d535a
----------------------------------------------------------------
kusano 7d535a
--
kusano 7d535a
--  $Id: buffer_demo.adb,v 1.3 2004/09/06 06:55:35 vagul Exp $
kusano 7d535a
kusano 7d535a
--  This demo program provided by Dr Steve Sangwine <sjs@essex.ac.uk></sjs@essex.ac.uk>
kusano 7d535a
--
kusano 7d535a
--  Demonstration of a problem with Zlib-Ada (already fixed) when a buffer
kusano 7d535a
--  of exactly the correct size is used for decompressed data, and the last
kusano 7d535a
--  few bytes passed in to Zlib are checksum bytes.
kusano 7d535a
kusano 7d535a
--  This program compresses a string of text, and then decompresses the
kusano 7d535a
--  compressed text into a buffer of the same size as the original text.
kusano 7d535a
kusano 7d535a
with Ada.Streams; use Ada.Streams;
kusano 7d535a
with Ada.Text_IO;
kusano 7d535a
kusano 7d535a
with ZLib; use ZLib;
kusano 7d535a
kusano 7d535a
procedure Buffer_Demo is
kusano 7d535a
   EOL  : Character renames ASCII.LF;
kusano 7d535a
   Text : constant String
kusano 7d535a
     := "Four score and seven years ago our fathers brought forth," & EOL &
kusano 7d535a
        "upon this continent, a new nation, conceived in liberty," & EOL &
kusano 7d535a
        "and dedicated to the proposition that `all men are created equal'.";
kusano 7d535a
kusano 7d535a
   Source : Stream_Element_Array (1 .. Text'Length);
kusano 7d535a
   for Source'Address use Text'Address;
kusano 7d535a
kusano 7d535a
begin
kusano 7d535a
   Ada.Text_IO.Put (Text);
kusano 7d535a
   Ada.Text_IO.New_Line;
kusano 7d535a
   Ada.Text_IO.Put_Line
kusano 7d535a
     ("Uncompressed size : " & Positive'Image (Text'Length) & " bytes");
kusano 7d535a
kusano 7d535a
   declare
kusano 7d535a
      Compressed_Data : Stream_Element_Array (1 .. Text'Length);
kusano 7d535a
      L               : Stream_Element_Offset;
kusano 7d535a
   begin
kusano 7d535a
      Compress : declare
kusano 7d535a
         Compressor : Filter_Type;
kusano 7d535a
         I : Stream_Element_Offset;
kusano 7d535a
      begin
kusano 7d535a
         Deflate_Init (Compressor);
kusano 7d535a
kusano 7d535a
         --  Compress the whole of T at once.
kusano 7d535a
kusano 7d535a
         Translate (Compressor, Source, I, Compressed_Data, L, Finish);
kusano 7d535a
         pragma Assert (I = Source'Last);
kusano 7d535a
kusano 7d535a
         Close (Compressor);
kusano 7d535a
kusano 7d535a
         Ada.Text_IO.Put_Line
kusano 7d535a
           ("Compressed size :   "
kusano 7d535a
            & Stream_Element_Offset'Image (L) & " bytes");
kusano 7d535a
      end Compress;
kusano 7d535a
kusano 7d535a
      --  Now we decompress the data, passing short blocks of data to Zlib
kusano 7d535a
      --  (because this demonstrates the problem - the last block passed will
kusano 7d535a
      --  contain checksum information and there will be no output, only a
kusano 7d535a
      --  check inside Zlib that the checksum is correct).
kusano 7d535a
kusano 7d535a
      Decompress : declare
kusano 7d535a
         Decompressor : Filter_Type;
kusano 7d535a
kusano 7d535a
         Uncompressed_Data : Stream_Element_Array (1 .. Text'Length);
kusano 7d535a
kusano 7d535a
         Block_Size : constant := 4;
kusano 7d535a
         --  This makes sure that the last block contains
kusano 7d535a
         --  only Adler checksum data.
kusano 7d535a
kusano 7d535a
         P : Stream_Element_Offset := Compressed_Data'First - 1;
kusano 7d535a
         O : Stream_Element_Offset;
kusano 7d535a
      begin
kusano 7d535a
         Inflate_Init (Decompressor);
kusano 7d535a
kusano 7d535a
         loop
kusano 7d535a
            Translate
kusano 7d535a
              (Decompressor,
kusano 7d535a
               Compressed_Data
kusano 7d535a
                 (P + 1 .. Stream_Element_Offset'Min (P + Block_Size, L)),
kusano 7d535a
               P,
kusano 7d535a
               Uncompressed_Data
kusano 7d535a
                 (Total_Out (Decompressor) + 1 .. Uncompressed_Data'Last),
kusano 7d535a
               O,
kusano 7d535a
               No_Flush);
kusano 7d535a
kusano 7d535a
               Ada.Text_IO.Put_Line
kusano 7d535a
                 ("Total in : " & Count'Image (Total_In (Decompressor)) &
kusano 7d535a
                  ", out : " & Count'Image (Total_Out (Decompressor)));
kusano 7d535a
kusano 7d535a
               exit when P = L;
kusano 7d535a
         end loop;
kusano 7d535a
kusano 7d535a
         Ada.Text_IO.New_Line;
kusano 7d535a
         Ada.Text_IO.Put_Line
kusano 7d535a
           ("Decompressed text matches original text : "
kusano 7d535a
             & Boolean'Image (Uncompressed_Data = Source));
kusano 7d535a
      end Decompress;
kusano 7d535a
   end;
kusano 7d535a
end Buffer_Demo;