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