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