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