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