annotate src/zlib-1.2.8/contrib/ada/buffer_demo.adb @ 84:08ae793730bd

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