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