annotate src/zlib-1.2.7/contrib/ada/buffer_demo.adb @ 83:ae30d91d2ffe

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