annotate src/zlib-1.2.7/contrib/ada/buffer_demo.adb @ 169:223a55898ab9 tip default

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