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