1
----------------------------------------------------------------
2
-- ZLib for Ada thick binding. --
4
-- Copyright (C) 2002-2004 Dmitriy Anisimkov --
6
-- Open source license information is in the zlib.ads file. --
7
----------------------------------------------------------------
9
-- $Id: buffer_demo.adb,v 1.3 2004/09/06 06:55:35 vagul Exp $
11
-- This demo program provided by Dr Steve Sangwine <sjs@essex.ac.uk>
13
-- Demonstration of a problem with Zlib-Ada (already fixed) when a buffer
14
-- of exactly the correct size is used for decompressed data, and the last
15
-- few bytes passed in to Zlib are checksum bytes.
17
-- This program compresses a string of text, and then decompresses the
18
-- compressed text into a buffer of the same size as the original text.
20
with Ada.Streams; use Ada.Streams;
25
procedure Buffer_Demo is
26
EOL : Character renames ASCII.LF;
27
Text : constant String
28
:= "Four score and seven years ago our fathers brought forth," & EOL &
29
"upon this continent, a new nation, conceived in liberty," & EOL &
30
"and dedicated to the proposition that `all men are created equal'.";
32
Source : Stream_Element_Array (1 .. Text'Length);
33
for Source'Address use Text'Address;
36
Ada.Text_IO.Put (Text);
39
("Uncompressed size : " & Positive'Image (Text'Length) & " bytes");
42
Compressed_Data : Stream_Element_Array (1 .. Text'Length);
43
L : Stream_Element_Offset;
46
Compressor : Filter_Type;
47
I : Stream_Element_Offset;
49
Deflate_Init (Compressor);
51
-- Compress the whole of T at once.
53
Translate (Compressor, Source, I, Compressed_Data, L, Finish);
54
pragma Assert (I = Source'Last);
60
& Stream_Element_Offset'Image (L) & " bytes");
63
-- Now we decompress the data, passing short blocks of data to Zlib
64
-- (because this demonstrates the problem - the last block passed will
65
-- contain checksum information and there will be no output, only a
66
-- check inside Zlib that the checksum is correct).
69
Decompressor : Filter_Type;
71
Uncompressed_Data : Stream_Element_Array (1 .. Text'Length);
73
Block_Size : constant := 4;
74
-- This makes sure that the last block contains
75
-- only Adler checksum data.
77
P : Stream_Element_Offset := Compressed_Data'First - 1;
78
O : Stream_Element_Offset;
80
Inflate_Init (Decompressor);
86
(P + 1 .. Stream_Element_Offset'Min (P + Block_Size, L)),
89
(Total_Out (Decompressor) + 1 .. Uncompressed_Data'Last),
94
("Total in : " & Count'Image (Total_In (Decompressor)) &
95
", out : " & Count'Image (Total_Out (Decompressor)));
100
Ada.Text_IO.New_Line;
102
("Decompressed text matches original text : "
103
& Boolean'Image (Uncompressed_Data = Source));