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