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