xref: /netbsd-src/external/gpl3/binutils/dist/zlib/contrib/ada/buffer_demo.adb (revision fc4f42693f9b1c31f39f9cf50af1bf2010325808)
19573673dSchristos----------------------------------------------------------------
29573673dSchristos--  ZLib for Ada thick binding.                               --
39573673dSchristos--                                                            --
49573673dSchristos--  Copyright (C) 2002-2004 Dmitriy Anisimkov                 --
59573673dSchristos--                                                            --
69573673dSchristos--  Open source license information is in the zlib.ads file.  --
79573673dSchristos----------------------------------------------------------------
89573673dSchristos--
9*fc4f4269Schristos--  Id: buffer_demo.adb,v 1.3 2004/09/06 06:55:35 vagul Exp
109573673dSchristos
119573673dSchristos--  This demo program provided by Dr Steve Sangwine <sjs@essex.ac.uk>
129573673dSchristos--
139573673dSchristos--  Demonstration of a problem with Zlib-Ada (already fixed) when a buffer
149573673dSchristos--  of exactly the correct size is used for decompressed data, and the last
159573673dSchristos--  few bytes passed in to Zlib are checksum bytes.
169573673dSchristos
179573673dSchristos--  This program compresses a string of text, and then decompresses the
189573673dSchristos--  compressed text into a buffer of the same size as the original text.
199573673dSchristos
209573673dSchristoswith Ada.Streams; use Ada.Streams;
219573673dSchristoswith Ada.Text_IO;
229573673dSchristos
239573673dSchristoswith ZLib; use ZLib;
249573673dSchristos
259573673dSchristosprocedure Buffer_Demo is
269573673dSchristos   EOL  : Character renames ASCII.LF;
279573673dSchristos   Text : constant String
289573673dSchristos     := "Four score and seven years ago our fathers brought forth," & EOL &
299573673dSchristos        "upon this continent, a new nation, conceived in liberty," & EOL &
309573673dSchristos        "and dedicated to the proposition that `all men are created equal'.";
319573673dSchristos
329573673dSchristos   Source : Stream_Element_Array (1 .. Text'Length);
339573673dSchristos   for Source'Address use Text'Address;
349573673dSchristos
359573673dSchristosbegin
369573673dSchristos   Ada.Text_IO.Put (Text);
379573673dSchristos   Ada.Text_IO.New_Line;
389573673dSchristos   Ada.Text_IO.Put_Line
399573673dSchristos     ("Uncompressed size : " & Positive'Image (Text'Length) & " bytes");
409573673dSchristos
419573673dSchristos   declare
429573673dSchristos      Compressed_Data : Stream_Element_Array (1 .. Text'Length);
439573673dSchristos      L               : Stream_Element_Offset;
449573673dSchristos   begin
459573673dSchristos      Compress : declare
469573673dSchristos         Compressor : Filter_Type;
479573673dSchristos         I : Stream_Element_Offset;
489573673dSchristos      begin
499573673dSchristos         Deflate_Init (Compressor);
509573673dSchristos
519573673dSchristos         --  Compress the whole of T at once.
529573673dSchristos
539573673dSchristos         Translate (Compressor, Source, I, Compressed_Data, L, Finish);
549573673dSchristos         pragma Assert (I = Source'Last);
559573673dSchristos
569573673dSchristos         Close (Compressor);
579573673dSchristos
589573673dSchristos         Ada.Text_IO.Put_Line
599573673dSchristos           ("Compressed size :   "
609573673dSchristos            & Stream_Element_Offset'Image (L) & " bytes");
619573673dSchristos      end Compress;
629573673dSchristos
639573673dSchristos      --  Now we decompress the data, passing short blocks of data to Zlib
649573673dSchristos      --  (because this demonstrates the problem - the last block passed will
659573673dSchristos      --  contain checksum information and there will be no output, only a
669573673dSchristos      --  check inside Zlib that the checksum is correct).
679573673dSchristos
689573673dSchristos      Decompress : declare
699573673dSchristos         Decompressor : Filter_Type;
709573673dSchristos
719573673dSchristos         Uncompressed_Data : Stream_Element_Array (1 .. Text'Length);
729573673dSchristos
739573673dSchristos         Block_Size : constant := 4;
749573673dSchristos         --  This makes sure that the last block contains
759573673dSchristos         --  only Adler checksum data.
769573673dSchristos
779573673dSchristos         P : Stream_Element_Offset := Compressed_Data'First - 1;
789573673dSchristos         O : Stream_Element_Offset;
799573673dSchristos      begin
809573673dSchristos         Inflate_Init (Decompressor);
819573673dSchristos
829573673dSchristos         loop
839573673dSchristos            Translate
849573673dSchristos              (Decompressor,
859573673dSchristos               Compressed_Data
869573673dSchristos                 (P + 1 .. Stream_Element_Offset'Min (P + Block_Size, L)),
879573673dSchristos               P,
889573673dSchristos               Uncompressed_Data
899573673dSchristos                 (Total_Out (Decompressor) + 1 .. Uncompressed_Data'Last),
909573673dSchristos               O,
919573673dSchristos               No_Flush);
929573673dSchristos
939573673dSchristos               Ada.Text_IO.Put_Line
949573673dSchristos                 ("Total in : " & Count'Image (Total_In (Decompressor)) &
959573673dSchristos                  ", out : " & Count'Image (Total_Out (Decompressor)));
969573673dSchristos
979573673dSchristos               exit when P = L;
989573673dSchristos         end loop;
999573673dSchristos
1009573673dSchristos         Ada.Text_IO.New_Line;
1019573673dSchristos         Ada.Text_IO.Put_Line
1029573673dSchristos           ("Decompressed text matches original text : "
1039573673dSchristos             & Boolean'Image (Uncompressed_Data = Source));
1049573673dSchristos      end Decompress;
1059573673dSchristos   end;
1069573673dSchristosend Buffer_Demo;
107