xref: /netbsd-src/common/dist/zlib/contrib/ada/buffer_demo.adb (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1*aaf4ece6Schristos----------------------------------------------------------------
2*aaf4ece6Schristos--  ZLib for Ada thick binding.                               --
3*aaf4ece6Schristos--                                                            --
4*aaf4ece6Schristos--  Copyright (C) 2002-2004 Dmitriy Anisimkov                 --
5*aaf4ece6Schristos--                                                            --
6*aaf4ece6Schristos--  Open source license information is in the zlib.ads file.  --
7*aaf4ece6Schristos----------------------------------------------------------------
8*aaf4ece6Schristos--
9*aaf4ece6Schristos--  Id: buffer_demo.adb,v 1.3 2004/09/06 06:55:35 vagul Exp
10*aaf4ece6Schristos
11*aaf4ece6Schristos--  This demo program provided by Dr Steve Sangwine <sjs@essex.ac.uk>
12*aaf4ece6Schristos--
13*aaf4ece6Schristos--  Demonstration of a problem with Zlib-Ada (already fixed) when a buffer
14*aaf4ece6Schristos--  of exactly the correct size is used for decompressed data, and the last
15*aaf4ece6Schristos--  few bytes passed in to Zlib are checksum bytes.
16*aaf4ece6Schristos
17*aaf4ece6Schristos--  This program compresses a string of text, and then decompresses the
18*aaf4ece6Schristos--  compressed text into a buffer of the same size as the original text.
19*aaf4ece6Schristos
20*aaf4ece6Schristoswith Ada.Streams; use Ada.Streams;
21*aaf4ece6Schristoswith Ada.Text_IO;
22*aaf4ece6Schristos
23*aaf4ece6Schristoswith ZLib; use ZLib;
24*aaf4ece6Schristos
25*aaf4ece6Schristosprocedure Buffer_Demo is
26*aaf4ece6Schristos   EOL  : Character renames ASCII.LF;
27*aaf4ece6Schristos   Text : constant String
28*aaf4ece6Schristos     := "Four score and seven years ago our fathers brought forth," & EOL &
29*aaf4ece6Schristos        "upon this continent, a new nation, conceived in liberty," & EOL &
30*aaf4ece6Schristos        "and dedicated to the proposition that `all men are created equal'.";
31*aaf4ece6Schristos
32*aaf4ece6Schristos   Source : Stream_Element_Array (1 .. Text'Length);
33*aaf4ece6Schristos   for Source'Address use Text'Address;
34*aaf4ece6Schristos
35*aaf4ece6Schristosbegin
36*aaf4ece6Schristos   Ada.Text_IO.Put (Text);
37*aaf4ece6Schristos   Ada.Text_IO.New_Line;
38*aaf4ece6Schristos   Ada.Text_IO.Put_Line
39*aaf4ece6Schristos     ("Uncompressed size : " & Positive'Image (Text'Length) & " bytes");
40*aaf4ece6Schristos
41*aaf4ece6Schristos   declare
42*aaf4ece6Schristos      Compressed_Data : Stream_Element_Array (1 .. Text'Length);
43*aaf4ece6Schristos      L               : Stream_Element_Offset;
44*aaf4ece6Schristos   begin
45*aaf4ece6Schristos      Compress : declare
46*aaf4ece6Schristos         Compressor : Filter_Type;
47*aaf4ece6Schristos         I : Stream_Element_Offset;
48*aaf4ece6Schristos      begin
49*aaf4ece6Schristos         Deflate_Init (Compressor);
50*aaf4ece6Schristos
51*aaf4ece6Schristos         --  Compress the whole of T at once.
52*aaf4ece6Schristos
53*aaf4ece6Schristos         Translate (Compressor, Source, I, Compressed_Data, L, Finish);
54*aaf4ece6Schristos         pragma Assert (I = Source'Last);
55*aaf4ece6Schristos
56*aaf4ece6Schristos         Close (Compressor);
57*aaf4ece6Schristos
58*aaf4ece6Schristos         Ada.Text_IO.Put_Line
59*aaf4ece6Schristos           ("Compressed size :   "
60*aaf4ece6Schristos            & Stream_Element_Offset'Image (L) & " bytes");
61*aaf4ece6Schristos      end Compress;
62*aaf4ece6Schristos
63*aaf4ece6Schristos      --  Now we decompress the data, passing short blocks of data to Zlib
64*aaf4ece6Schristos      --  (because this demonstrates the problem - the last block passed will
65*aaf4ece6Schristos      --  contain checksum information and there will be no output, only a
66*aaf4ece6Schristos      --  check inside Zlib that the checksum is correct).
67*aaf4ece6Schristos
68*aaf4ece6Schristos      Decompress : declare
69*aaf4ece6Schristos         Decompressor : Filter_Type;
70*aaf4ece6Schristos
71*aaf4ece6Schristos         Uncompressed_Data : Stream_Element_Array (1 .. Text'Length);
72*aaf4ece6Schristos
73*aaf4ece6Schristos         Block_Size : constant := 4;
74*aaf4ece6Schristos         --  This makes sure that the last block contains
75*aaf4ece6Schristos         --  only Adler checksum data.
76*aaf4ece6Schristos
77*aaf4ece6Schristos         P : Stream_Element_Offset := Compressed_Data'First - 1;
78*aaf4ece6Schristos         O : Stream_Element_Offset;
79*aaf4ece6Schristos      begin
80*aaf4ece6Schristos         Inflate_Init (Decompressor);
81*aaf4ece6Schristos
82*aaf4ece6Schristos         loop
83*aaf4ece6Schristos            Translate
84*aaf4ece6Schristos              (Decompressor,
85*aaf4ece6Schristos               Compressed_Data
86*aaf4ece6Schristos                 (P + 1 .. Stream_Element_Offset'Min (P + Block_Size, L)),
87*aaf4ece6Schristos               P,
88*aaf4ece6Schristos               Uncompressed_Data
89*aaf4ece6Schristos                 (Total_Out (Decompressor) + 1 .. Uncompressed_Data'Last),
90*aaf4ece6Schristos               O,
91*aaf4ece6Schristos               No_Flush);
92*aaf4ece6Schristos
93*aaf4ece6Schristos               Ada.Text_IO.Put_Line
94*aaf4ece6Schristos                 ("Total in : " & Count'Image (Total_In (Decompressor)) &
95*aaf4ece6Schristos                  ", out : " & Count'Image (Total_Out (Decompressor)));
96*aaf4ece6Schristos
97*aaf4ece6Schristos               exit when P = L;
98*aaf4ece6Schristos         end loop;
99*aaf4ece6Schristos
100*aaf4ece6Schristos         Ada.Text_IO.New_Line;
101*aaf4ece6Schristos         Ada.Text_IO.Put_Line
102*aaf4ece6Schristos           ("Decompressed text matches original text : "
103*aaf4ece6Schristos             & Boolean'Image (Uncompressed_Data = Source));
104*aaf4ece6Schristos      end Decompress;
105*aaf4ece6Schristos   end;
106*aaf4ece6Schristosend Buffer_Demo;
107